blob: 3877b9496ac2ca0df39b6ffb8b679c4f00ca1921 [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 Goyale2df0622015-04-24 11:27:00 -070063import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070066import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080067
Michael Jurkac2f801e2011-07-12 14:19:46 -070068import java.lang.ref.WeakReference;
69import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070070import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070072import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.Collections;
74import java.util.Comparator;
75import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070077import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070079import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070081import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083/**
84 * Maintains in-memory state of the Launcher. It is expected that there should be only one
85 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070086 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 */
Kenny Guyed131872014-04-30 03:02:21 +010088public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010089 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080090 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040091 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070092 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040093
Joe Onorato9c1289c2009-08-17 11:03:03 -040094 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070095
Dan Sandlerd5024042014-01-09 15:01:33 -050096 public static final int LOADER_FLAG_NONE = 0;
97 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
98 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
99
Joe Onorato36115782010-06-17 13:28:48 -0400100 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500101 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800104 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final LauncherAppState mApp;
107 @Thunk final Object mLock = new Object();
108 @Thunk DeferredHandler mHandler = new DeferredHandler();
109 @Thunk LoaderTask mLoaderTask;
110 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700111 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Jason Monkbbe1e242014-05-16 17:37:34 -0400113 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700114
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700116 static {
117 sWorkerThread.start();
118 }
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 // We start off with everything not loaded. After that, we assume that
122 // our monitoring of the package manager provides all updates and we never
123 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk boolean mWorkspaceLoaded;
125 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700126
Sunny Goyal756a28a2015-04-23 17:07:55 -0700127 /**
128 * Set of runnables to be called on the background thread after the workspace binding
129 * is complete.
130 */
131 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
132
Adam Cohen091440a2015-03-18 14:16:05 -0700133 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700135 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800136 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700137 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800138 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800139
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700140 // The lock that must be acquired before referencing any static bg data structures. Unlike
141 // other locks, this one can generally be held long-term because we never expect any of these
142 // static data structures to be referenced outside of the worker thread except on the first
143 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700144 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700147 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700148 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
151 // created by LauncherModel that are directly on the home screen (however, no widgets or
152 // shortcuts within folders).
153 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
156 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157 new ArrayList<LauncherAppWidgetInfo>();
158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700160 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700161
Adam Cohendcd297f2013-06-18 13:13:40 -0700162 // sBgWorkspaceScreens is the ordered set of workspace screens.
163 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
164
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700165 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700166 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
167 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700168
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700169 // </ only access in worker thread >
170
Adam Cohen091440a2015-03-18 14:16:05 -0700171 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Adam Cohen091440a2015-03-18 14:16:05 -0700173 @Thunk final LauncherAppsCompat mLauncherApps;
174 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100175
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700177 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700179 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700181 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
182 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700183 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700184 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700185 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800186 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700189 public void bindAppsAdded(ArrayList<Long> newScreens,
190 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700191 ArrayList<ItemInfo> addAnimated,
192 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700194 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
195 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
196 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700197 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700198 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700199 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800200 public void notifyWidgetProvidersChanged();
201 public void bindWidgetsModel(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700202 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700203 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700204 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700205 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chung64359a52013-07-08 17:17:08 -0700208 public interface ItemInfoFilter {
209 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
210 }
211
Bjorn Bringert1307f632013-10-03 22:31:03 +0100212 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800213 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400214
Winson Chungee055712013-07-30 14:46:24 -0700215 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700216 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400217 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
218 // resource string.
219 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
220 ProviderInfo providerInfo =
221 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
222 ProviderInfo redirectProvider =
223 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700224
225 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400226 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700227
228 if (mOldContentProviderExists) {
229 Log.d(TAG, "Old launcher provider exists.");
230 } else {
231 Log.d(TAG, "Old launcher provider does not exist.");
232 }
233
Daniel Sandlere4f98912013-06-25 15:13:26 -0400234 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100235 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700236 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800237 mIconCache = iconCache;
238
Kenny Guyed131872014-04-30 03:02:21 +0100239 mLauncherApps = LauncherAppsCompat.getInstance(context);
240 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 }
242
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700243 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
244 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700246 if (sWorkerThread.getThreadId() == Process.myTid()) {
247 // If we are on the worker thread, post onto the main handler
248 mHandler.post(r);
249 } else {
250 r.run();
251 }
252 }
253
254 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
255 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700256 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700257 if (sWorkerThread.getThreadId() == Process.myTid()) {
258 r.run();
259 } else {
260 // If we are not on the worker thread, then post to the worker handler
261 sWorker.post(r);
262 }
263 }
264
Winson Chunge43a1e72014-01-15 10:33:02 -0800265 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
266 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800267 }
268
Sunny Goyal756adbc2015-04-16 15:20:51 -0700269 public void setPackageState(final PackageInstallInfo installInfo) {
270 Runnable updateRunnable = new Runnable() {
271
272 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500273 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700274 synchronized (sBgLock) {
275 final HashSet<ItemInfo> updates = new HashSet<>();
276
277 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
278 // Ignore install success events as they are handled by Package add events.
279 return;
280 }
281
Sunny Goyale2df0622015-04-24 11:27:00 -0700282 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700283 if (info instanceof ShortcutInfo) {
284 ShortcutInfo si = (ShortcutInfo) info;
285 ComponentName cn = si.getTargetComponent();
286 if (si.isPromise() && (cn != null)
287 && installInfo.packageName.equals(cn.getPackageName())) {
288 si.setInstallProgress(installInfo.progress);
289
290 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
291 // Mark this info as broken.
292 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
293 }
294 updates.add(si);
295 }
296 }
297 }
298
299 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
300 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
301 widget.installProgress = installInfo.progress;
302 updates.add(widget);
303 }
304 }
305
306 if (!updates.isEmpty()) {
307 // Push changes to the callback.
308 Runnable r = new Runnable() {
309 public void run() {
310 Callbacks callbacks = getCallback();
311 if (callbacks != null) {
312 callbacks.bindRestoreItemsChange(updates);
313 }
314 }
315 };
316 mHandler.post(r);
317 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500318 }
319 }
320 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700321 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500322 }
323
Sunny Goyal756adbc2015-04-16 15:20:51 -0700324 /**
325 * Updates the icons and label of all pending icons for the provided package name.
326 */
327 public void updateSessionDisplayInfo(final String packageName) {
328 Runnable updateRunnable = new Runnable() {
329
330 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700331 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700332 synchronized (sBgLock) {
333 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
334 final UserHandleCompat user = UserHandleCompat.myUserHandle();
335
Sunny Goyale2df0622015-04-24 11:27:00 -0700336 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700337 if (info instanceof ShortcutInfo) {
338 ShortcutInfo si = (ShortcutInfo) info;
339 ComponentName cn = si.getTargetComponent();
340 if (si.isPromise() && (cn != null)
341 && packageName.equals(cn.getPackageName())) {
342 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
343 // For auto install apps update the icon as well as label.
344 mIconCache.getTitleAndIcon(si,
345 si.promisedIntent, user,
346 si.shouldUseLowResIcon());
347 } else {
348 // Only update the icon for restored apps.
349 si.updateIcon(mIconCache);
350 }
351 updates.add(si);
352 }
353 }
354 }
355
356 if (!updates.isEmpty()) {
357 // Push changes to the callback.
358 Runnable r = new Runnable() {
359 public void run() {
360 Callbacks callbacks = getCallback();
361 if (callbacks != null) {
362 callbacks.bindShortcutsChanged(updates,
363 new ArrayList<ShortcutInfo>(), user);
364 }
365 }
366 };
367 mHandler.post(r);
368 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700369 }
370 }
371 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700372 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700373 }
374
Adam Cohen76a47a12014-02-05 11:47:43 -0800375 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800376 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800377
378 if (allAppsApps == null) {
379 throw new RuntimeException("allAppsApps must not be null");
380 }
381 if (allAppsApps.isEmpty()) {
382 return;
383 }
384
385 // Process the newly added applications and add them to the database first
386 Runnable r = new Runnable() {
387 public void run() {
388 runOnMainThread(new Runnable() {
389 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800390 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800391 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500392 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800393 }
394 }
395 });
396 }
397 };
398 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700399 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800400
Sunny Goyala9116722015-04-29 13:55:58 -0700401 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800402 int[] xy, int spanX, int spanY) {
403 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700404 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
405 final int xCount = (int) profile.numColumns;
406 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800407 boolean[][] occupied = new boolean[xCount][yCount];
408 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700409 for (ItemInfo r : occupiedPos) {
410 int right = r.cellX + r.spanX;
411 int bottom = r.cellY + r.spanY;
412 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
413 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 occupied[x][y] = true;
415 }
416 }
417 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800418 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700419 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800420 }
421
422 /**
423 * Find a position on the screen for the given size or adds a new screen.
424 * @return screenId and the coordinates for the item.
425 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700426 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700427 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 ArrayList<Long> workspaceScreens,
429 ArrayList<Long> addedWorkspaceScreensFinal,
430 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700431 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432
Sunny Goyala9116722015-04-29 13:55:58 -0700433 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700434 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700435 synchronized (sBgLock) {
436 for (ItemInfo info : sBgItemsIdMap) {
437 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
438 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
439 if (items == null) {
440 items = new ArrayList<>();
441 screenItems.put(info.screenId, items);
442 }
443 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446 }
447
448 // Find appropriate space for the item.
449 long screenId = 0;
450 int[] cordinates = new int[2];
451 boolean found = false;
452
453 int screenCount = workspaceScreens.size();
454 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700455 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800456 if (preferredScreenIndex < screenCount) {
457 screenId = workspaceScreens.get(preferredScreenIndex);
458 found = findNextAvailableIconSpaceInScreen(
459 screenItems.get(screenId), cordinates, spanX, spanY);
460 }
461
462 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700463 // Search on any of the screens starting from the first screen.
464 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 screenId = workspaceScreens.get(screen);
466 if (findNextAvailableIconSpaceInScreen(
467 screenItems.get(screenId), cordinates, spanX, spanY)) {
468 // We found a space for it
469 found = true;
470 break;
471 }
472 }
473 }
474
475 if (!found) {
476 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700477 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
478 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
479 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480
481 // Save the screen id for binding in the workspace
482 workspaceScreens.add(screenId);
483 addedWorkspaceScreensFinal.add(screenId);
484
485 // If we still can't find an empty space, then God help us all!!!
486 if (!findNextAvailableIconSpaceInScreen(
487 screenItems.get(screenId), cordinates, spanX, spanY)) {
488 throw new RuntimeException("Can't find space to add the item");
489 }
490 }
491 return Pair.create(screenId, cordinates);
492 }
493
494 /**
495 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800496 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700497 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700498 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800499 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800500 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700501 return;
Winson Chung997a9232013-07-24 15:33:46 -0700502 }
Winson Chung64359a52013-07-08 17:17:08 -0700503 // Process the newly added applications and add them to the database first
504 Runnable r = new Runnable() {
505 public void run() {
506 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
507 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
508
Winson Chung76828c82013-08-19 15:43:29 -0700509 // Get the list of workspace screens. We need to append to this list and
510 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
511 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800512 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700513 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700515 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800516 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700517 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700519 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 }
Winson Chung76828c82013-08-19 15:43:29 -0700521
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700523 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700524 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800525 long screenId = coords.first;
526 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700527
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700528 ItemInfo itemInfo;
529 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
530 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700532 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700533 } else {
534 throw new RuntimeException("Unexpected info type");
535 }
Winson Chung94d67682013-09-25 16:29:40 -0700536
Winson Chung64359a52013-07-08 17:17:08 -0700537 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700538 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700539 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700540 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700541 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700542 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700543 }
544 }
545
Winson Chung76828c82013-08-19 15:43:29 -0700546 // Update the workspace screens
547 updateWorkspaceScreenOrder(context, workspaceScreens);
548
Adam Cohen76a47a12014-02-05 11:47:43 -0800549 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700550 runOnMainThread(new Runnable() {
551 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800552 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700553 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700554 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
555 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700556 if (!addedShortcutsFinal.isEmpty()) {
557 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
558 long lastScreenId = info.screenId;
559 for (ItemInfo i : addedShortcutsFinal) {
560 if (i.screenId == lastScreenId) {
561 addAnimated.add(i);
562 } else {
563 addNotAnimated.add(i);
564 }
Winson Chung997a9232013-07-24 15:33:46 -0700565 }
566 }
Winson Chungd64d1762013-08-20 14:37:16 -0700567 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800568 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700569 }
Winson Chung64359a52013-07-08 17:17:08 -0700570 }
Winson Chung997a9232013-07-24 15:33:46 -0700571 });
572 }
Winson Chung64359a52013-07-08 17:17:08 -0700573 }
574 };
575 runOnWorkerThread(r);
576 }
577
Sunny Goyald33860f2015-04-23 16:02:20 -0700578 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700579 if (sWorkerThread.getThreadId() == Process.myTid()) {
580 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
581 "main thread");
582 }
583
Sunny Goyald33860f2015-04-23 16:02:20 -0700584 // Remove any queued UI runnables
585 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700586 // Unbind all the workspace items
587 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700588 }
589
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700590 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700591 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700592 // Ensure that we don't use the same workspace items data structure on the main thread
593 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700594 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700595 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700596 tmpItems.addAll(sBgWorkspaceItems);
597 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700598 }
599 Runnable r = new Runnable() {
600 @Override
601 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700602 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700603 item.unbind();
604 }
605 }
606 };
607 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700608 }
609
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610 /**
611 * Adds an item to the DB if it was not created previously, or move it to a new
612 * <container, screen, cellX, cellY>
613 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800614 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700615 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 if (item.container == ItemInfo.NO_ID) {
617 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700618 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 } else {
620 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700621 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 }
623 }
624
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700625 static void checkItemInfoLocked(
626 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
627 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
628 if (modelItem != null && item != modelItem) {
629 // check all the data is consistent
630 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
631 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
632 ShortcutInfo shortcut = (ShortcutInfo) item;
633 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
634 modelShortcut.intent.filterEquals(shortcut.intent) &&
635 modelShortcut.id == shortcut.id &&
636 modelShortcut.itemType == shortcut.itemType &&
637 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700639 modelShortcut.cellX == shortcut.cellX &&
640 modelShortcut.cellY == shortcut.cellY &&
641 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700642 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700643 // For all intents and purposes, this is the same object
644 return;
645 }
646 }
647
648 // the modelItem needs to match up perfectly with item if our model is
649 // to be consistent with the database-- for now, just require
650 // modelItem == item or the equality check above
651 String msg = "item: " + ((item != null) ? item.toString() : "null") +
652 "modelItem: " +
653 ((modelItem != null) ? modelItem.toString() : "null") +
654 "Error: ItemInfo passed to checkItemInfo doesn't match original";
655 RuntimeException e = new RuntimeException(msg);
656 if (stackTrace != null) {
657 e.setStackTrace(stackTrace);
658 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800659 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700660 }
661 }
662
Michael Jurka816474f2012-06-25 14:49:02 -0700663 static void checkItemInfo(final ItemInfo item) {
664 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
665 final long itemId = item.id;
666 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700667 public void run() {
668 synchronized (sBgLock) {
669 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700670 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700671 }
672 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700673 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700674 }
675
Michael Jurkac9d95c52011-08-29 14:03:34 -0700676 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
677 final ItemInfo item, final String callingFunction) {
678 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700679 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700680 final ContentResolver cr = context.getContentResolver();
681
Adam Cohen487f7dd2012-06-28 18:12:10 -0700682 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700683 Runnable r = new Runnable() {
684 public void run() {
685 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686 updateItemArrays(item, itemId, stackTrace);
687 }
688 };
689 runOnWorkerThread(r);
690 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700691
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700692 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
693 final ArrayList<ItemInfo> items, final String callingFunction) {
694 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700695
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
697 Runnable r = new Runnable() {
698 public void run() {
699 ArrayList<ContentProviderOperation> ops =
700 new ArrayList<ContentProviderOperation>();
701 int count = items.size();
702 for (int i = 0; i < count; i++) {
703 ItemInfo item = items.get(i);
704 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700705 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700706 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700707
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700708 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
709 updateItemArrays(item, itemId, stackTrace);
710
711 }
712 try {
713 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
714 } catch (Exception e) {
715 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700716 }
717 }
718 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700719 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700721
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700722 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
723 // Lock on mBgLock *after* the db operation
724 synchronized (sBgLock) {
725 checkItemInfoLocked(itemId, item, stackTrace);
726
727 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
728 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
729 // Item is in a folder, make sure this folder exists
730 if (!sBgFolders.containsKey(item.container)) {
731 // An items container is being set to a that of an item which is not in
732 // the list of Folders.
733 String msg = "item: " + item + " container being set to: " +
734 item.container + ", not in the list of folders";
735 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 }
737 }
738
739 // Items are added/removed from the corresponding FolderInfo elsewhere, such
740 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
741 // that are on the desktop, as appropriate
742 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800743 if (modelItem != null &&
744 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
745 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700746 switch (modelItem.itemType) {
747 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
748 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
749 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
750 if (!sBgWorkspaceItems.contains(modelItem)) {
751 sBgWorkspaceItems.add(modelItem);
752 }
753 break;
754 default:
755 break;
756 }
757 } else {
758 sBgWorkspaceItems.remove(modelItem);
759 }
760 }
761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700765 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700766 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400769 item.cellX = cellX;
770 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700771
Winson Chung3d503fb2011-07-13 17:25:49 -0700772 // We store hotseat items in canonical form which is this orientation invariant position
773 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700775 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700777 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700779 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780
781 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
784 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800785 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787
Michael Jurkac9d95c52011-08-29 14:03:34 -0700788 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700789 }
790
791 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700792 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
793 * cellX, cellY have already been updated on the ItemInfos.
794 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800795 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700796 final long container, final int screen) {
797
798 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
799 int count = items.size();
800
801 for (int i = 0; i < count; i++) {
802 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700803 item.container = container;
804
805 // We store hotseat items in canonical form which is this orientation invariant position
806 // in the hotseat
807 if (context instanceof Launcher && screen < 0 &&
808 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700809 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700810 item.cellY);
811 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 }
814
815 final ContentValues values = new ContentValues();
816 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
817 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
818 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800819 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700820 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821
822 contentValues.add(values);
823 }
824 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
825 }
826
827 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700828 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800829 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700830 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700831 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700832 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800833 item.cellX = cellX;
834 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700835 item.spanX = spanX;
836 item.spanY = spanY;
837
838 // We store hotseat items in canonical form which is this orientation invariant position
839 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700840 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700841 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700843 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 }
Adam Cohend4844c32011-02-18 19:25:06 -0800846
Adam Cohend4844c32011-02-18 19:25:06 -0800847 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800848 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
850 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800851 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700852 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
853 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800855
Michael Jurka816474f2012-06-25 14:49:02 -0700856 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700858
859 /**
860 * Update an item to the database in a specified container.
861 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700862 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700863 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100864 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700865 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800866 }
867
Sunny Goyal756a28a2015-04-23 17:07:55 -0700868 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700869 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700870 synchronized (mLock) {
871 if (!mHasLoaderCompletedOnce ||
872 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
873 throw new RuntimeException("Trying to add shortcut while loader is running");
874 }
875 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700876 }
877 }
878
Adam Cohend4844c32011-02-18 19:25:06 -0800879 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700880 * Returns true if the shortcuts already exists on the workspace. This must be called after
881 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700883 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
884 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700885 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700886 if (intent.getComponent() != null) {
887 // If component is not null, an intent with null package will produce
888 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700889 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700890 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700891 intentWithPkg = intent.toUri(0);
892 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700893 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700894 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
895 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700896 }
897 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700898 intentWithPkg = intent.toUri(0);
899 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700901
902 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700903 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700904 if (item instanceof ShortcutInfo) {
905 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700906 Intent targetIntent = info.promisedIntent == null
907 ? info.intent : info.promisedIntent;
908 if (targetIntent != null && info.user.equals(user)) {
909 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700910 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
911 return true;
912 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700913 }
914 }
915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700917 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700918 }
919
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 * Add an item to the database in a specified container. Sets the container, screen, cellX and
922 * cellY fields of the item. Also assigns an ID to the item.
923 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700924 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700925 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 item.cellX = cellX;
928 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700929 // We store hotseat items in canonical form which is this orientation invariant position
930 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700931 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700932 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937
938 final ContentValues values = new ContentValues();
939 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100940 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941
Sunny Goyald2497482015-09-22 18:24:19 -0700942 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
943 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
944
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700945 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700946
Jason Monk8e19cf22014-03-20 15:06:57 -0400947 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700948 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700949 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700950 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700952 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700953 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400954 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 sBgItemsIdMap.put(item.id, item);
956 switch (item.itemType) {
957 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
958 sBgFolders.put(item.id, (FolderInfo) item);
959 // Fall through
960 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
961 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
962 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
963 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
964 sBgWorkspaceItems.add(item);
965 } else {
966 if (!sBgFolders.containsKey(item.container)) {
967 // Adding an item to a folder that doesn't exist.
968 String msg = "adding item: " + item + " to a folder that " +
969 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700970 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700971 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700972 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700973 break;
974 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
975 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
976 break;
977 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700978 }
979 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700980 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700981 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700982 }
983
Sunny Goyal34942622014-08-29 17:20:55 -0700984 private static ArrayList<ItemInfo> getItemsByPackageName(
985 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700986 ItemInfoFilter filter = new ItemInfoFilter() {
987 @Override
988 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
989 return cn.getPackageName().equals(pn) && info.user.equals(user);
990 }
991 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700992 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700993 }
994
995 /**
996 * Removes all the items from the database corresponding to the specified package.
997 */
998 static void deletePackageFromDatabase(Context context, final String pn,
999 final UserHandleCompat user) {
1000 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001001 }
1002
1003 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001004 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001006 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001007 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1008 items.add(item);
1009 deleteItemsFromDatabase(context, items);
1010 }
1011
1012 /**
1013 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001014 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001015 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001017 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001018 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001019 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001020 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001021 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001023 // Lock on mBgLock *after* the db operation
1024 synchronized (sBgLock) {
1025 switch (item.itemType) {
1026 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1027 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001028 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001029 if (info.container == item.id) {
1030 // We are deleting a folder which still contains items that
1031 // think they are contained by that folder.
1032 String msg = "deleting a folder (" + item + ") which still " +
1033 "contains items (" + info + ")";
1034 Log.e(TAG, msg);
1035 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001036 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001037 sBgWorkspaceItems.remove(item);
1038 break;
1039 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1040 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1041 sBgWorkspaceItems.remove(item);
1042 break;
1043 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1044 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1045 break;
1046 }
1047 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001049 }
1050 }
Michael Jurka83df1882011-08-31 20:59:26 -07001051 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 }
1054
1055 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 * Update the order of the workspace screens in the database. The array list contains
1057 * a list of screen ids in the order that they should appear.
1058 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001059 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001060 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001061 final ContentResolver cr = context.getContentResolver();
1062 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1063
1064 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001065 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 while (iter.hasNext()) {
1067 long id = iter.next();
1068 if (id < 0) {
1069 iter.remove();
1070 }
1071 }
1072
1073 Runnable r = new Runnable() {
1074 @Override
1075 public void run() {
Yura085c8532014-02-11 15:15:29 +00001076 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001078 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001079 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 for (int i = 0; i < count; i++) {
1081 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001082 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1084 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001085 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 }
Yura085c8532014-02-11 15:15:29 +00001087
1088 try {
1089 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1090 } catch (Exception ex) {
1091 throw new RuntimeException(ex);
1092 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001093
Winson Chungba9c37f2013-08-30 14:11:37 -07001094 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001095 sBgWorkspaceScreens.clear();
1096 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001097 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 }
1099 };
1100 runOnWorkerThread(r);
1101 }
1102
1103 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001104 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 */
Winsonc0b52fe2015-09-09 16:38:15 -07001106 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 final ContentResolver cr = context.getContentResolver();
1108
Michael Jurkac9d95c52011-08-29 14:03:34 -07001109 Runnable r = new Runnable() {
1110 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001111 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001113 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 sBgItemsIdMap.remove(info.id);
1115 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 sBgWorkspaceItems.remove(info);
1117 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001118
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001119 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001120 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001122 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 for (ItemInfo childInfo : info.contents) {
1124 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001126 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001127 }
1128 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 }
1131
1132 /**
1133 * Set this as the current Launcher activity object for the loader.
1134 */
1135 public void initialize(Callbacks callbacks) {
1136 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001137 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1138 // workspace to prevent leaking Launcher activities on orientation change.
1139 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 mCallbacks = new WeakReference<Callbacks>(callbacks);
1141 }
1142 }
1143
Kenny Guyed131872014-04-30 03:02:21 +01001144 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001145 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001146 int op = PackageUpdatedTask.OP_UPDATE;
1147 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1148 user));
1149 }
1150
1151 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001152 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001153 int op = PackageUpdatedTask.OP_REMOVE;
1154 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1155 user));
1156 }
1157
1158 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001159 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001160 int op = PackageUpdatedTask.OP_ADD;
1161 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1162 user));
1163 }
1164
1165 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001166 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001167 boolean replacing) {
1168 if (!replacing) {
1169 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1170 user));
1171 if (mAppsCanBeOnRemoveableStorage) {
1172 // Only rebind if we support removable storage. It catches the
1173 // case where
1174 // apps on the external sd card need to be reloaded
1175 startLoaderFromBackground();
1176 }
1177 } else {
1178 // If we are replacing then just update the packages in the list
1179 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1180 packageNames, user));
1181 }
1182 }
1183
1184 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001185 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001186 boolean replacing) {
1187 if (!replacing) {
1188 enqueuePackageUpdated(new PackageUpdatedTask(
1189 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1190 user));
1191 }
Kenny Guyed131872014-04-30 03:02:21 +01001192 }
1193
Kenny Guy44cba692016-01-21 19:50:02 +00001194 @Override
1195 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1196 enqueuePackageUpdated(new PackageUpdatedTask(
1197 PackageUpdatedTask.OP_SUSPEND, packageNames,
1198 user));
1199 }
1200
1201 @Override
1202 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1203 enqueuePackageUpdated(new PackageUpdatedTask(
1204 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1205 user));
1206 }
1207
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001208 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1210 * ACTION_PACKAGE_CHANGED.
1211 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001212 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001213 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001214 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001215
Joe Onorato36115782010-06-17 13:28:48 -04001216 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001217 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001218 // If we have changed locale we need to clear out the labels in all apps/workspace.
1219 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001220 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001221 Callbacks callbacks = getCallback();
1222 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001223 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001224 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001225 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Kenny Guyff05f432016-01-22 17:48:29 +00001226 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)
1227 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001228 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001229 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001230 }
1231 }
1232
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001233 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001234 resetLoadedState(true, true);
1235
Reena Lee93f824a2011-09-23 17:20:28 -07001236 // Do this here because if the launcher activity is running it will be restarted.
1237 // If it's not running startLoaderFromBackground will merely tell it that it needs
1238 // to reload.
1239 startLoaderFromBackground();
1240 }
1241
Winson Chungf0c6ae02012-03-21 16:10:31 -07001242 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1243 synchronized (mLock) {
1244 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1245 // mWorkspaceLoaded to true later
1246 stopLoaderLocked();
1247 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1248 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1249 }
1250 }
1251
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001252 /**
1253 * When the launcher is in the background, it's possible for it to miss paired
1254 * configuration changes. So whenever we trigger the loader from the background
1255 * tell the launcher that it needs to re-run the loader when it comes back instead
1256 * of doing it now.
1257 */
1258 public void startLoaderFromBackground() {
1259 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001260 Callbacks callbacks = getCallback();
1261 if (callbacks != null) {
1262 // Only actually run the loader if they're not paused.
1263 if (!callbacks.setLoadOnResume()) {
1264 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001265 }
1266 }
1267 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001268 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001269 }
Joe Onorato36115782010-06-17 13:28:48 -04001270 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001271
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001272 /**
1273 * If there is already a loader task running, tell it to stop.
1274 */
1275 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001276 LoaderTask oldTask = mLoaderTask;
1277 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001278 oldTask.stopLocked();
1279 }
Reena Lee93f824a2011-09-23 17:20:28 -07001280 }
1281
Adam Cohen1a85c582014-09-30 09:48:49 -07001282 public boolean isCurrentCallbacks(Callbacks callbacks) {
1283 return (mCallbacks != null && mCallbacks.get() == callbacks);
1284 }
1285
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001286 public void startLoader(int synchronousBindPage) {
1287 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001288 }
1289
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001290 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001291 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1292 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001293 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001294 // Don't bother to start the thread if we know it's not going to do anything
1295 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001296 final Callbacks oldCallbacks = mCallbacks.get();
1297 // Clear any pending bind-runnables from the synchronized load process.
1298 runOnMainThread(new Runnable() {
1299 public void run() {
1300 oldCallbacks.clearPendingBinds();
1301 }
1302 });
1303
Joe Onorato36115782010-06-17 13:28:48 -04001304 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001305 stopLoaderLocked();
1306 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001307 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001308 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1310 } else {
1311 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1312 sWorker.post(mLoaderTask);
1313 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 }
1316 }
1317
Joe Onorato36115782010-06-17 13:28:48 -04001318 public void stopLoader() {
1319 synchronized (mLock) {
1320 if (mLoaderTask != null) {
1321 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322 }
1323 }
Joe Onorato36115782010-06-17 13:28:48 -04001324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001326 /**
1327 * Loads the workspace screen ids in an ordered list.
1328 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001329 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001330 final ContentResolver contentResolver = context.getContentResolver();
1331 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001332
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001333 // Get screens ordered by rank.
1334 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1335 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1336 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001337 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001338 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001339 while (sc.moveToNext()) {
1340 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001341 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001342 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001343 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001344 }
1345 }
1346 } finally {
1347 sc.close();
1348 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001349 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001350 }
1351
Joe Onorato36115782010-06-17 13:28:48 -04001352 /**
1353 * Runnable for the thread that loads the contents of the launcher:
1354 * - workspace icons
1355 * - widgets
1356 * - all apps icons
1357 */
1358 private class LoaderTask implements Runnable {
1359 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001360 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001361 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001362 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001363 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001365 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001366 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001367 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 }
1369
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001370 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001371 mIsLoadingAndBindingWorkspace = true;
1372
Joe Onorato36115782010-06-17 13:28:48 -04001373 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001374 if (DEBUG_LOADERS) {
1375 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001377
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001378 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001379 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001380 synchronized (LoaderTask.this) {
1381 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001382 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001383 }
1384 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 }
1386 }
1387
Joe Onorato36115782010-06-17 13:28:48 -04001388 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001389 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001390 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001391
Joe Onorato36115782010-06-17 13:28:48 -04001392 private void waitForIdle() {
1393 // Wait until the either we're stopped or the other threads are done.
1394 // This way we don't start loading all apps until the workspace has settled
1395 // down.
1396 synchronized (LoaderTask.this) {
1397 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001398
Joe Onorato36115782010-06-17 13:28:48 -04001399 mHandler.postIdle(new Runnable() {
1400 public void run() {
1401 synchronized (LoaderTask.this) {
1402 mLoadAndBindStepFinished = true;
1403 if (DEBUG_LOADERS) {
1404 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001405 }
Joe Onorato36115782010-06-17 13:28:48 -04001406 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 }
Joe Onorato36115782010-06-17 13:28:48 -04001409 });
1410
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001411 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001412 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001413 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1414 // wait no longer than 1sec at a time
1415 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001416 } catch (InterruptedException ex) {
1417 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001418 }
1419 }
Joe Onorato36115782010-06-17 13:28:48 -04001420 if (DEBUG_LOADERS) {
1421 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001422 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001423 + "ms for previous step to finish binding");
1424 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001425 }
Joe Onorato36115782010-06-17 13:28:48 -04001426 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001427
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001428 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001429 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430 // Ensure that we have a valid page index to load synchronously
1431 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1432 "valid page index");
1433 }
1434 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1435 // Ensure that we don't try and bind a specified page when the pages have not been
1436 // loaded already (we should load everything asynchronously in that case)
1437 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1438 }
1439 synchronized (mLock) {
1440 if (mIsLoaderTaskRunning) {
1441 // Ensure that we are never running the background loading at this point since
1442 // we also touch the background collections
1443 throw new RuntimeException("Error! Background loading is already running");
1444 }
1445 }
1446
1447 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1448 // data structures, we can't allow any other thread to touch that data, but because
1449 // this call is synchronous, we can get away with not locking).
1450
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001451 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001452 // operations from the previous activity. We need to ensure that all queued operations
1453 // are executed before any synchronous binding work is done.
1454 mHandler.flush();
1455
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 // Divide the set of loaded items into those that we are binding synchronously, and
1457 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001458 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001459 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1460 // arise from that.
1461 onlyBindAllApps();
1462 }
1463
Joe Onorato36115782010-06-17 13:28:48 -04001464 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001466 if (mStopped) {
1467 return;
1468 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 mIsLoaderTaskRunning = true;
1470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471 // Optimize for end-user experience: if the Launcher is up and // running with the
1472 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1473 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001474 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001475 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001476 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001477
Joe Onorato36115782010-06-17 13:28:48 -04001478 if (mStopped) {
1479 break keep_running;
1480 }
1481
Joe Onorato36115782010-06-17 13:28:48 -04001482 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001483
1484 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001485 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1486 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488
Joe Onorato36115782010-06-17 13:28:48 -04001489 // Clear out this reference, otherwise we end up holding it until all of the
1490 // callback runnables are done.
1491 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001492
Joe Onorato36115782010-06-17 13:28:48 -04001493 synchronized (mLock) {
1494 // If we are still the last one to be scheduled, remove ourselves.
1495 if (mLoaderTask == this) {
1496 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001498 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001499 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001500 }
Joe Onorato36115782010-06-17 13:28:48 -04001501 }
1502
1503 public void stopLocked() {
1504 synchronized (LoaderTask.this) {
1505 mStopped = true;
1506 this.notify();
1507 }
1508 }
1509
1510 /**
1511 * Gets the callbacks object. If we've been stopped, or if the launcher object
1512 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1513 * object that was around when the deferred message was scheduled, and if there's
1514 * a new Callbacks object around then also return null. This will save us from
1515 * calling onto it with data that will be ignored.
1516 */
1517 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1518 synchronized (mLock) {
1519 if (mStopped) {
1520 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001521 }
Joe Onorato36115782010-06-17 13:28:48 -04001522
1523 if (mCallbacks == null) {
1524 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001525 }
Joe Onorato36115782010-06-17 13:28:48 -04001526
1527 final Callbacks callbacks = mCallbacks.get();
1528 if (callbacks != oldCallbacks) {
1529 return null;
1530 }
1531 if (callbacks == null) {
1532 Log.w(TAG, "no mCallbacks");
1533 return null;
1534 }
1535
1536 return callbacks;
1537 }
1538 }
1539
1540 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001541 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1542 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001543 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001544 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001545 final int countX = profile.numColumns;
1546 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001549 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001550 // Return early if we detect that an item is under the hotseat button
1551 if (mCallbacks == null ||
1552 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001553 Log.e(TAG, "Error loading shortcut into hotseat " + item
1554 + " into position (" + item.screenId + ":" + item.cellX + ","
1555 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001556 return false;
1557 }
1558
Dan Sandler295ae182013-12-10 16:05:47 -05001559 final ItemInfo[][] hotseatItems =
1560 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1561
Adam Cohen2e6da152015-05-06 11:42:25 -07001562 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001563 Log.e(TAG, "Error loading shortcut " + item
1564 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001565 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001566 + ")");
1567 return false;
1568 }
1569
Dan Sandler295ae182013-12-10 16:05:47 -05001570 if (hotseatItems != null) {
1571 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001572 Log.e(TAG, "Error loading shortcut into hotseat " + item
1573 + " into position (" + item.screenId + ":" + item.cellX + ","
1574 + item.cellY + ") occupied by "
1575 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1576 [(int) item.screenId][0]);
1577 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001578 } else {
1579 hotseatItems[(int) item.screenId][0] = item;
1580 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001581 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001582 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001583 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001584 items[(int) item.screenId][0] = item;
1585 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001586 return true;
1587 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001588 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1589 if (!workspaceScreens.contains((Long) item.screenId)) {
1590 // The item has an invalid screen id.
1591 return false;
1592 }
1593 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001594 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001595 return true;
1596 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001597
Adam Cohendcd297f2013-06-18 13:13:40 -07001598 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001599 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001600 occupied.put(item.screenId, items);
1601 }
1602
Dan Sandler295ae182013-12-10 16:05:47 -05001603 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001604 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1605 item.cellX < 0 || item.cellY < 0 ||
1606 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1607 Log.e(TAG, "Error loading shortcut " + item
1608 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1609 + item.cellX + "," + item.cellY
1610 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1611 return false;
1612 }
1613
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001614 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001615 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1616 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001617 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001618 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001619 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001620 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001621 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001622 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001623 return false;
1624 }
1625 }
1626 }
1627 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1628 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001630 }
1631 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001632
Joe Onorato36115782010-06-17 13:28:48 -04001633 return true;
1634 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635
Winson Chungba9c37f2013-08-30 14:11:37 -07001636 /** Clears all the sBg data structures */
1637 private void clearSBgDataStructures() {
1638 synchronized (sBgLock) {
1639 sBgWorkspaceItems.clear();
1640 sBgAppWidgets.clear();
1641 sBgFolders.clear();
1642 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001643 sBgWorkspaceScreens.clear();
1644 }
1645 }
1646
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001647 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001648 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001649
Joe Onorato36115782010-06-17 13:28:48 -04001650 final Context context = mContext;
1651 final ContentResolver contentResolver = context.getContentResolver();
1652 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001653 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001654 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001655 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001656
Winson Chung892c74d2013-08-22 16:15:50 -07001657 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001658 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001659 int countX = profile.numColumns;
1660 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001661
Sunny Goyalf076eae2016-01-11 12:25:10 -08001662 if (GridSizeMigrationTask.ENABLED &&
1663 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1664 // Migration failed. Clear workspace.
1665 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001666 }
1667
Dan Sandlerd5024042014-01-09 15:01:33 -05001668 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001669 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001670 LauncherSettings.Settings.call(contentResolver,
1671 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001672 }
1673
1674 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1675 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001676 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001677 LauncherSettings.Settings.call(contentResolver,
1678 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001679 } else {
1680 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001681 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001682 LauncherSettings.Settings.call(contentResolver,
1683 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001684 }
Adam Cohene25af792013-06-06 23:08:25 -07001685
Winson Chung2abf94d2012-07-18 18:16:38 -07001686 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001687 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001688 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001689 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001690 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001691
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001692 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1693 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001694 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001695 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001696 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001697
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001698 // +1 for the hotseat (it can be larger than the workspace)
1699 // Load workspace in reverse order to ensure that latest items are loaded first (and
1700 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001701 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001702 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001703
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 try {
1705 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1706 final int intentIndex = c.getColumnIndexOrThrow
1707 (LauncherSettings.Favorites.INTENT);
1708 final int titleIndex = c.getColumnIndexOrThrow
1709 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 final int containerIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.CONTAINER);
1712 final int itemTypeIndex = c.getColumnIndexOrThrow(
1713 LauncherSettings.Favorites.ITEM_TYPE);
1714 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1715 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001716 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1717 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 final int screenIndex = c.getColumnIndexOrThrow(
1719 LauncherSettings.Favorites.SCREEN);
1720 final int cellXIndex = c.getColumnIndexOrThrow
1721 (LauncherSettings.Favorites.CELLX);
1722 final int cellYIndex = c.getColumnIndexOrThrow
1723 (LauncherSettings.Favorites.CELLY);
1724 final int spanXIndex = c.getColumnIndexOrThrow
1725 (LauncherSettings.Favorites.SPANX);
1726 final int spanYIndex = c.getColumnIndexOrThrow(
1727 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001728 final int rankIndex = c.getColumnIndexOrThrow(
1729 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001730 final int restoredIndex = c.getColumnIndexOrThrow(
1731 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001732 final int profileIdIndex = c.getColumnIndexOrThrow(
1733 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001734 final int optionsIndex = c.getColumnIndexOrThrow(
1735 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001736 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737
Sunny Goyal7f834d22015-04-21 10:10:23 -07001738 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001739 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001740 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001741 long serialNo = mUserManager.getSerialNumberForUser(user);
1742 allUsers.put(serialNo, user);
1743 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001744 }
1745
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 ShortcutInfo info;
1747 String intentDescription;
1748 LauncherAppWidgetInfo appWidgetInfo;
1749 int container;
1750 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001751 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001753 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 while (!mStopped && c.moveToNext()) {
1756 try {
1757 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001758 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001759 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001760 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 switch (itemType) {
1763 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1764 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001765 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001767 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001768 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001769 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001770 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001771 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001772 if (user == null) {
1773 // User has been deleted remove the item.
1774 itemsToRemove.add(id);
1775 continue;
1776 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 try {
1778 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001779 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001780 if (cn != null && cn.getPackageName() != null) {
1781 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1782 cn.getPackageName(), user);
1783 boolean validComponent = validPkg &&
1784 launcherApps.isActivityEnabledForProfile(cn, user);
1785
1786 if (validComponent) {
1787 if (restored) {
1788 // no special handling necessary for this item
1789 restoredRows.add(id);
1790 restored = false;
1791 }
Kenny Guy44cba692016-01-21 19:50:02 +00001792 boolean isSuspended = launcherApps.isPackageSuspendedForProfile(
1793 cn.getPackageName(), user);
1794 if (isSuspended) {
1795 disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1796 }
Kenny Guyff05f432016-01-22 17:48:29 +00001797 if (quietMode.get(serialNumber)) {
1798 disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER;
1799 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001800 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001801 intent = null;
1802 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1803 // We allow auto install apps to have their intent
1804 // updated after an install.
1805 intent = manager.getLaunchIntentForPackage(
1806 cn.getPackageName());
1807 if (intent != null) {
1808 ContentValues values = new ContentValues();
1809 values.put(LauncherSettings.Favorites.INTENT,
1810 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001811 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001812 }
1813 }
1814
1815 if (intent == null) {
1816 // The app is installed but the component is no
1817 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001818 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001819 itemsToRemove.add(id);
1820 continue;
1821 } else {
1822 // no special handling necessary for this item
1823 restoredRows.add(id);
1824 restored = false;
1825 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001826 } else if (restored) {
1827 // Package is not yet available but might be
1828 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001829 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001830
1831 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1832 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001833 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001834 // App restore has started. Update the flag
1835 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1836 ContentValues values = new ContentValues();
1837 values.put(LauncherSettings.Favorites.RESTORED,
1838 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001839 updateItem(id, values);
1840 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1841 // This is a common app. Try to replace this.
1842 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1843 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1844 if (parser.findDefaultApp()) {
1845 // Default app found. Replace it.
1846 intent = parser.parsedIntent;
1847 cn = intent.getComponent();
1848 ContentValues values = parser.parsedValues;
1849 values.put(LauncherSettings.Favorites.RESTORED, 0);
1850 updateItem(id, values);
1851 restored = false;
1852 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001853
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001854 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001855 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001856 itemsToRemove.add(id);
1857 continue;
1858 }
Sunny Goyal94485362014-09-18 16:13:58 -07001859 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001860 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001861 itemsToRemove.add(id);
1862 continue;
1863 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001864 } else if (launcherApps.isAppEnabled(
1865 manager, cn.getPackageName(),
1866 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1867 // Package is present but not available.
1868 allowMissingTarget = true;
1869 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1870 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001871 // SdCard is not ready yet. Package might get available,
1872 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001873 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001874 HashSet<String> pkgs = sPendingPackages.get(user);
1875 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001876 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001877 sPendingPackages.put(user, pkgs);
1878 }
1879 pkgs.add(cn.getPackageName());
1880 allowMissingTarget = true;
1881 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001882
1883 } else {
1884 // Do not wait for external media load anymore.
1885 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001886 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001887 itemsToRemove.add(id);
1888 continue;
Winson Chungee055712013-07-30 14:46:24 -07001889 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001890 } else if (cn == null) {
1891 // For shortcuts with no component, keep them as they are
1892 restoredRows.add(id);
1893 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001894 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001896 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001897 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 continue;
1899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001900
Sunny Goyal34b65272015-03-11 16:56:52 -07001901 boolean useLowResIcon = container >= 0 &&
1902 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1903
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001904 if (itemReplaced) {
1905 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001906 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001907 cursorIconInfo.iconIndex, titleIndex,
1908 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001909 } else {
1910 // Don't replace items for other profiles.
1911 itemsToRemove.add(id);
1912 continue;
1913 }
1914 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001915 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001916 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001917 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001918 intent = getRestoredItemIntent(c, context, intent);
1919 } else {
1920 // Don't restore items for other profiles.
1921 itemsToRemove.add(id);
1922 continue;
1923 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001924 } else if (itemType ==
1925 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001926 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001927 cursorIconInfo.iconIndex, titleIndex,
1928 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001930 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001931
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 // App shortcuts that used to be automatically added to Launcher
1933 // didn't always have the correct intent flags set, so do that
1934 // here
1935 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001936 intent.getCategories() != null &&
1937 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001938 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001939 intent.addFlags(
1940 Intent.FLAG_ACTIVITY_NEW_TASK |
1941 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1942 }
Michael Jurka96879562012-03-22 05:54:33 -07001943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001944
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001945 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001946 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 info.cellX = c.getInt(cellXIndex);
1951 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001952 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001953 info.spanX = 1;
1954 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001955 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001956 if (info.promisedIntent != null) {
1957 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1958 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001959 info.isDisabled = disabledState;
1960 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1961 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1962 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001963
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001964 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001965 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001966 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 break;
1968 }
1969
Sunny Goyal756adbc2015-04-16 15:20:51 -07001970 if (restored) {
1971 ComponentName cn = info.getTargetComponent();
1972 if (cn != null) {
1973 Integer progress = installingPkgs.get(cn.getPackageName());
1974 if (progress != null) {
1975 info.setInstallProgress(progress);
1976 } else {
1977 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1978 }
1979 }
1980 }
1981
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 switch (container) {
1983 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1984 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1985 sBgWorkspaceItems.add(info);
1986 break;
1987 default:
1988 // Item is in a user folder
1989 FolderInfo folderInfo =
1990 findOrMakeFolder(sBgFolders, container);
1991 folderInfo.add(info);
1992 break;
1993 }
1994 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001995 } else {
1996 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 }
1998 break;
1999
2000 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2001 id = c.getLong(idIndex);
2002 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2003
Sunny Goyala508e4f2015-05-21 09:33:57 -07002004 // Do not trim the folder label, as is was set by the user.
2005 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002008 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 folderInfo.cellX = c.getInt(cellXIndex);
2010 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002011 folderInfo.spanX = 1;
2012 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002013 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002014
Daniel Sandler8802e962010-05-26 16:28:16 -04002015 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002016 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002017 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002018 break;
2019 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002020
Joe Onorato9c1289c2009-08-17 11:03:03 -04002021 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2023 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2024 sBgWorkspaceItems.add(folderInfo);
2025 break;
Joe Onorato36115782010-06-17 13:28:48 -04002026 }
Joe Onorato17a89222011-02-08 17:26:11 -08002027
Chris Wrenf4d08112014-01-16 18:13:56 -05002028 if (restored) {
2029 // no special handling required for restored folders
2030 restoredRows.add(id);
2031 }
2032
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2034 sBgFolders.put(folderInfo.id, folderInfo);
2035 break;
2036
2037 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002038 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002040 boolean customWidget = itemType ==
2041 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2042
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002044 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002045 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002046 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002047 user = allUsers.get(serialNumber);
2048 if (user == null) {
2049 itemsToRemove.add(id);
2050 continue;
2051 }
2052
Sunny Goyalff572272014-07-23 13:58:07 -07002053 final ComponentName component =
2054 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002055
Sunny Goyal651077b2014-06-30 14:15:31 -07002056 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002057 final boolean isIdValid = (restoreStatus &
2058 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002059 final boolean wasProviderReady = (restoreStatus &
2060 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002061
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002062 if (widgetProvidersMap == null) {
2063 widgetProvidersMap = AppWidgetManagerCompat
2064 .getInstance(mContext).getAllProvidersMap();
2065 }
2066 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2067 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002068 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002069 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002070
2071 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002072 if (!isSafeMode && !customWidget &&
2073 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002074 addDumpLog("Deleting widget that isn't installed anymore: "
2075 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 itemsToRemove.add(id);
2077 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002078 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002079 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2080 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002081
Sunny Goyal53f96722015-07-13 19:54:53 -07002082 // The provider is available. So the widget is either
2083 // available or not available. We do not need to track
2084 // any future restore updates.
2085 int status = restoreStatus &
2086 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002087 if (!wasProviderReady) {
2088 // If provider was not previously ready, update the
2089 // status and UI flag.
2090
2091 // Id would be valid only if the widget restore broadcast was received.
2092 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002093 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002094 } else {
2095 status &= ~LauncherAppWidgetInfo
2096 .FLAG_PROVIDER_NOT_READY;
2097 }
2098 }
2099 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002100 } else {
2101 Log.v(TAG, "Widget restore pending id=" + id
2102 + " appWidgetId=" + appWidgetId
2103 + " status =" + restoreStatus);
2104 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002105 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002106 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002107 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002108
2109 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2110 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002111 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002112 // App restore has started. Update the flag
2113 appWidgetInfo.restoreStatus |=
2114 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002115 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002116 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002117 itemsToRemove.add(id);
2118 continue;
2119 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002120
2121 appWidgetInfo.installProgress =
2122 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002123 }
Sunny Goyalff572272014-07-23 13:58:07 -07002124
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002126 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002127 appWidgetInfo.cellX = c.getInt(cellXIndex);
2128 appWidgetInfo.cellY = c.getInt(cellYIndex);
2129 appWidgetInfo.spanX = c.getInt(spanXIndex);
2130 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002131 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002132
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2134 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2135 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002136 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2137 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 continue;
2139 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002140
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002141 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002143 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002144 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 break;
2146 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002147
Adam Cohen59400422014-03-05 18:07:04 -08002148 if (!customWidget) {
2149 String providerName =
2150 appWidgetInfo.providerName.flattenToString();
2151 if (!providerName.equals(savedProvider) ||
2152 (appWidgetInfo.restoreStatus != restoreStatus)) {
2153 ContentValues values = new ContentValues();
2154 values.put(
2155 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2156 providerName);
2157 values.put(LauncherSettings.Favorites.RESTORED,
2158 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002159 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002160 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002161 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002162 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2163 sBgAppWidgets.add(appWidgetInfo);
2164 }
Joe Onorato36115782010-06-17 13:28:48 -04002165 break;
2166 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002168 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002169 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002170 }
2171 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002172 if (c != null) {
2173 c.close();
2174 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 }
2176
Winson Chungba9c37f2013-08-30 14:11:37 -07002177 // Break early if we've stopped loading
2178 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002179 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002180 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002181 }
2182
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002185 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2186 Utilities.createDbSelectionQuery(
2187 LauncherSettings.Favorites._ID, itemsToRemove), null);
2188 if (DEBUG_LOADERS) {
2189 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2190 LauncherSettings.Favorites._ID, itemsToRemove));
2191 }
2192
2193 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002194 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2195 .call(contentResolver,
2196 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2197 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2198 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002199 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2200 sBgFolders.remove(folderId);
2201 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002202 }
2203 }
2204
Sunny Goyal317698b2015-07-29 11:45:41 -07002205 // Sort all the folder items and make sure the first 3 items are high resolution.
2206 for (FolderInfo folder : sBgFolders) {
2207 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2208 int pos = 0;
2209 for (ShortcutInfo info : folder.contents) {
2210 if (info.usingLowResIcon) {
2211 info.updateIcon(mIconCache, false);
2212 }
2213 pos ++;
2214 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2215 break;
2216 }
2217 }
2218 }
2219
Chris Wrenf4d08112014-01-16 18:13:56 -05002220 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002221 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002222 ContentValues values = new ContentValues();
2223 values.put(LauncherSettings.Favorites.RESTORED, 0);
2224 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2225 Utilities.createDbSelectionQuery(
2226 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002227 }
2228
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002229 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2230 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002231 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002232 null, sWorker);
2233 }
2234
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002235 // Remove any empty screens
2236 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002237 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002238 long screenId = item.screenId;
2239 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2240 unusedScreens.contains(screenId)) {
2241 unusedScreens.remove(screenId);
2242 }
2243 }
2244
2245 // If there are any empty screens remove them, and update.
2246 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002247 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 }
2250
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 if (DEBUG_LOADERS) {
2252 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2253 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002254 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002255 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002257
Sunny Goyale2df0622015-04-24 11:27:00 -07002258 for (int i = 0; i < nScreens; i++) {
2259 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002260 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 line += " | ";
2262 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002263 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002264 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002265 if (x < screen.length && y < screen[x].length) {
2266 line += (screen[x][y] != null) ? "#" : ".";
2267 } else {
2268 line += "!";
2269 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002270 }
Joe Onorato36115782010-06-17 13:28:48 -04002271 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002273 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002274 }
Joe Onorato36115782010-06-17 13:28:48 -04002275 }
Adam Cohene25af792013-06-06 23:08:25 -07002276 }
2277
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002278 /**
2279 * Partially updates the item without any notification. Must be called on the worker thread.
2280 */
2281 private void updateItem(long itemId, ContentValues update) {
2282 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002283 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002284 update,
2285 BaseColumns._ID + "= ?",
2286 new String[]{Long.toString(itemId)});
2287 }
2288
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 /** Filters the set of items who are directly or indirectly (via another container) on the
2290 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002291 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 ArrayList<ItemInfo> allWorkspaceItems,
2293 ArrayList<ItemInfo> currentScreenItems,
2294 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002295 // Purge any null ItemInfos
2296 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2297 while (iter.hasNext()) {
2298 ItemInfo i = iter.next();
2299 if (i == null) {
2300 iter.remove();
2301 }
2302 }
2303
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 // Order the set of items by their containers first, this allows use to walk through the
2305 // list sequentially, build up a list of containers that are in the specified screen,
2306 // as well as all items in those containers.
2307 Set<Long> itemsOnScreen = new HashSet<Long>();
2308 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2309 @Override
2310 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002311 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 }
2313 });
2314 for (ItemInfo info : allWorkspaceItems) {
2315 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002316 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 currentScreenItems.add(info);
2318 itemsOnScreen.add(info.id);
2319 } else {
2320 otherScreenItems.add(info);
2321 }
2322 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2323 currentScreenItems.add(info);
2324 itemsOnScreen.add(info.id);
2325 } else {
2326 if (itemsOnScreen.contains(info.container)) {
2327 currentScreenItems.add(info);
2328 itemsOnScreen.add(info.id);
2329 } else {
2330 otherScreenItems.add(info);
2331 }
2332 }
2333 }
2334 }
2335
2336 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002337 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 ArrayList<LauncherAppWidgetInfo> appWidgets,
2339 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2340 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341
2342 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002343 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002345 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002346 currentScreenWidgets.add(widget);
2347 } else {
2348 otherScreenWidgets.add(widget);
2349 }
2350 }
2351 }
2352
2353 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002354 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002355 LongArrayMap<ItemInfo> itemsIdMap,
2356 LongArrayMap<FolderInfo> folders,
2357 LongArrayMap<FolderInfo> currentScreenFolders,
2358 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359
Sunny Goyale2df0622015-04-24 11:27:00 -07002360 int total = folders.size();
2361 for (int i = 0; i < total; i++) {
2362 long id = folders.keyAt(i);
2363 FolderInfo folder = folders.valueAt(i);
2364
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002365 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002366 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002367 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002368 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 currentScreenFolders.put(id, folder);
2370 } else {
2371 otherScreenFolders.put(id, folder);
2372 }
2373 }
2374 }
2375
2376 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2377 * right) */
2378 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002379 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002380 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002381 final int screenCols = profile.numColumns;
2382 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002384 @Override
2385 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002386 if (lhs.container == rhs.container) {
2387 // Within containers, order by their spatial position in that container
2388 switch ((int) lhs.container) {
2389 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2390 long lr = (lhs.screenId * screenCellCount +
2391 lhs.cellY * screenCols + lhs.cellX);
2392 long rr = (rhs.screenId * screenCellCount +
2393 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002394 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002395 }
2396 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2397 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002398 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002399 }
2400 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002401 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002402 throw new RuntimeException("Unexpected container type when " +
2403 "sorting workspace items.");
2404 }
2405 return 0;
2406 }
2407 } else {
2408 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002409 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002410 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002411 }
2412 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002414
Adam Cohendcd297f2013-06-18 13:13:40 -07002415 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2416 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002417 final Runnable r = new Runnable() {
2418 @Override
2419 public void run() {
2420 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2421 if (callbacks != null) {
2422 callbacks.bindScreens(orderedScreens);
2423 }
2424 }
2425 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002426 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002427 }
2428
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002429 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2430 final ArrayList<ItemInfo> workspaceItems,
2431 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002432 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002433 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002434
2435 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002436 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002438 final int start = i;
2439 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002440 final Runnable r = new Runnable() {
2441 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002442 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002443 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002444 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002445 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2446 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002447 }
2448 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002450 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002451 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452
2453 // Bind the folders
2454 if (!folders.isEmpty()) {
2455 final Runnable r = new Runnable() {
2456 public void run() {
2457 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2458 if (callbacks != null) {
2459 callbacks.bindFolders(folders);
2460 }
2461 }
2462 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002463 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 }
2465
2466 // Bind the widgets, one at a time
2467 N = appWidgets.size();
2468 for (int i = 0; i < N; i++) {
2469 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2470 final Runnable r = new Runnable() {
2471 public void run() {
2472 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2473 if (callbacks != null) {
2474 callbacks.bindAppWidget(widget);
2475 }
2476 }
2477 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002478 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 }
2480 }
2481
2482 /**
2483 * Binds all loaded data to actual views on the main thread.
2484 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002485 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 final long t = SystemClock.uptimeMillis();
2487 Runnable r;
2488
2489 // Don't use these two variables in any of the callback runnables.
2490 // Otherwise we hold a reference to them.
2491 final Callbacks oldCallbacks = mCallbacks.get();
2492 if (oldCallbacks == null) {
2493 // This launcher has exited and nobody bothered to tell us. Just bail.
2494 Log.w(TAG, "LoaderTask running with no launcher");
2495 return;
2496 }
2497
Winson Chung9b9fb962013-11-15 15:39:34 -08002498 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2500 ArrayList<LauncherAppWidgetInfo> appWidgets =
2501 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002502 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002503
2504 final LongArrayMap<FolderInfo> folders;
2505 final LongArrayMap<ItemInfo> itemsIdMap;
2506
Winson Chung2abf94d2012-07-18 18:16:38 -07002507 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 workspaceItems.addAll(sBgWorkspaceItems);
2509 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002510 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002511
2512 folders = sBgFolders.clone();
2513 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 }
2515
Derek Prothro7aff3992013-12-10 14:00:37 -05002516 final boolean isLoadingSynchronously =
2517 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002518 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002519 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002520 if (currScreen >= orderedScreenIds.size()) {
2521 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002522 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002523 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002524 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002525 final long currentScreenId = currentScreen < 0
2526 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002527
2528 // Load all the items that are on the current page first (and in the process, unbind
2529 // all the existing workspace items before we call startBinding() below.
2530 unbindWorkspaceItemsOnMainThread();
2531
2532 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2534 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2535 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2536 new ArrayList<LauncherAppWidgetInfo>();
2537 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2538 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002539 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2540 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002541
Winson Chung9b9fb962013-11-15 15:39:34 -08002542 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002544 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002546 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 otherFolders);
2548 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2549 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2550
2551 // Tell the workspace that we're about to start binding items
2552 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002553 public void run() {
2554 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2555 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002556 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002558 }
2559 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002560 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002561 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562
Adam Cohendcd297f2013-06-18 13:13:40 -07002563 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2564
Sunny Goyal527c7d32015-08-28 15:19:36 -07002565 Executor mainExecutor = new DeferredMainThreadExecutor();
2566 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002568 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002569
Sunny Goyal527c7d32015-08-28 15:19:36 -07002570 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2571 // remaining screens after first onDraw is called. This ensures that the first screen
2572 // is immediately visible (eg. during rotation)
2573 // In case of !isLoadingSynchronously, bind all pages one after other.
2574 final Executor deferredExecutor = isLoadingSynchronously ?
2575 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2576
2577 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2578 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002579
2580 // Tell the workspace that we're done binding items
2581 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002582 public void run() {
2583 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2584 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002585 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002586 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002587
Sunny Goyal639e9062015-08-19 19:17:06 -07002588 mIsLoadingAndBindingWorkspace = false;
2589
2590 // Run all the bind complete runnables after workspace is bound.
2591 if (!mBindCompleteRunnables.isEmpty()) {
2592 synchronized (mBindCompleteRunnables) {
2593 for (final Runnable r : mBindCompleteRunnables) {
2594 runOnWorkerThread(r);
2595 }
2596 mBindCompleteRunnables.clear();
2597 }
2598 }
2599
Winson Chung98e030b2012-05-07 16:01:11 -07002600 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002601 if (DEBUG_LOADERS) {
2602 Log.d(TAG, "bound workspace in "
2603 + (SystemClock.uptimeMillis()-t) + "ms");
2604 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002605
Joe Onorato36115782010-06-17 13:28:48 -04002606 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002607 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002608 deferredExecutor.execute(r);
2609
Winson Chung4a2afa32012-07-19 14:53:05 -07002610 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002611 r = new Runnable() {
2612 public void run() {
2613 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2614 if (callbacks != null) {
2615 // We are loading synchronously, which means, some of the pages will be
2616 // bound after first draw. Inform the callbacks that page binding is
2617 // not complete, and schedule the remaining pages.
2618 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2619 callbacks.onPageBoundSynchronously(currentScreen);
2620 }
2621 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2622 }
2623 }
2624 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002625 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002626 }
Joe Onorato36115782010-06-17 13:28:48 -04002627 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002628
Joe Onorato36115782010-06-17 13:28:48 -04002629 private void loadAndBindAllApps() {
2630 if (DEBUG_LOADERS) {
2631 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2632 }
2633 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002634 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002635 synchronized (LoaderTask.this) {
2636 if (mStopped) {
2637 return;
2638 }
2639 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002640 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002641 synchronized (LoaderTask.this) {
2642 if (mStopped) {
2643 return;
2644 }
2645 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002646 }
Joe Onorato36115782010-06-17 13:28:48 -04002647 } else {
2648 onlyBindAllApps();
2649 }
2650 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002651
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002652 private void updateIconCache() {
2653 // Ignore packages which have a promise icon.
2654 HashSet<String> packagesToIgnore = new HashSet<>();
2655 synchronized (sBgLock) {
2656 for (ItemInfo info : sBgItemsIdMap) {
2657 if (info instanceof ShortcutInfo) {
2658 ShortcutInfo si = (ShortcutInfo) info;
2659 if (si.isPromise() && si.getTargetComponent() != null) {
2660 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2661 }
2662 } else if (info instanceof LauncherAppWidgetInfo) {
2663 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2664 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2665 packagesToIgnore.add(lawi.providerName.getPackageName());
2666 }
2667 }
2668 }
2669 }
2670 mIconCache.updateDbIcons(packagesToIgnore);
2671 }
2672
Joe Onorato36115782010-06-17 13:28:48 -04002673 private void onlyBindAllApps() {
2674 final Callbacks oldCallbacks = mCallbacks.get();
2675 if (oldCallbacks == null) {
2676 // This launcher has exited and nobody bothered to tell us. Just bail.
2677 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2678 return;
2679 }
2680
2681 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002682 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002683 final ArrayList<AppInfo> list
2684 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002685 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002686 public void run() {
2687 final long t = SystemClock.uptimeMillis();
2688 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2689 if (callbacks != null) {
2690 callbacks.bindAllApplications(list);
2691 }
2692 if (DEBUG_LOADERS) {
2693 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002694 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002695 }
2696 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002697 };
2698 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002699 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002700 r.run();
2701 } else {
2702 mHandler.post(r);
2703 }
Joe Onorato36115782010-06-17 13:28:48 -04002704 }
2705
Winson Chung64359a52013-07-08 17:17:08 -07002706 private void loadAllApps() {
2707 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002708
Joe Onorato36115782010-06-17 13:28:48 -04002709 final Callbacks oldCallbacks = mCallbacks.get();
2710 if (oldCallbacks == null) {
2711 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002712 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002713 return;
2714 }
2715
Kenny Guyed131872014-04-30 03:02:21 +01002716 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2717
Winson Chung64359a52013-07-08 17:17:08 -07002718 // Clear the list of apps
2719 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002720 for (UserHandleCompat user : profiles) {
2721 // Query for the set of apps
2722 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002723 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002724 if (DEBUG_LOADERS) {
2725 Log.d(TAG, "getActivityList took "
2726 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2727 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2728 }
2729 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002730 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002731 if (apps == null || apps.isEmpty()) {
2732 return;
2733 }
Kenny Guyff05f432016-01-22 17:48:29 +00002734 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002735 // Create the ApplicationInfos
2736 for (int i = 0; i < apps.size(); i++) {
2737 LauncherActivityInfoCompat app = apps.get(i);
2738 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002739 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002740 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002741
Sunny Goyal756a28a2015-04-23 17:07:55 -07002742 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2743 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002744 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002745
2746 @Override
2747 public void run() {
2748 heuristic.processUserApps(apps);
2749 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002750 };
2751 runOnMainThread(new Runnable() {
2752
2753 @Override
2754 public void run() {
2755 // Check isLoadingWorkspace on the UI thread, as it is updated on
2756 // the UI thread.
2757 if (mIsLoadingAndBindingWorkspace) {
2758 synchronized (mBindCompleteRunnables) {
2759 mBindCompleteRunnables.add(r);
2760 }
2761 } else {
2762 runOnWorkerThread(r);
2763 }
2764 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002765 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002766 }
Winson Chung64359a52013-07-08 17:17:08 -07002767 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002768 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002769 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2770 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002771
2772 // Post callback on main thread
2773 mHandler.post(new Runnable() {
2774 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002775
Winson Chung64359a52013-07-08 17:17:08 -07002776 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002777 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002778 if (callbacks != null) {
2779 callbacks.bindAllApplications(added);
2780 if (DEBUG_LOADERS) {
2781 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002782 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002783 }
2784 } else {
2785 Log.i(TAG, "not binding apps: no Launcher activity");
2786 }
2787 }
2788 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002789 // Cleanup any data stored for a deleted user.
2790 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002791 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002792 Log.d(TAG, "Icons processed in "
2793 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002794 }
2795 }
2796
2797 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002798 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002799 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002800 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2801 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2802 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2803 }
Joe Onorato36115782010-06-17 13:28:48 -04002804 }
2805 }
2806
Sunny Goyal75b0f552015-05-20 21:57:06 -07002807 /**
2808 * Called when the icons for packages have been updated in the icon cache.
2809 */
2810 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2811 final Callbacks callbacks = getCallback();
2812 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2813 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2814
2815 // If any package icon has changed (app was updated while launcher was dead),
2816 // update the corresponding shortcuts.
2817 synchronized (sBgLock) {
2818 for (ItemInfo info : sBgItemsIdMap) {
2819 if (info instanceof ShortcutInfo && user.equals(info.user)
2820 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2821 ShortcutInfo si = (ShortcutInfo) info;
2822 ComponentName cn = si.getTargetComponent();
2823 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2824 si.updateIcon(mIconCache);
2825 updatedShortcuts.add(si);
2826 }
2827 }
2828 }
2829 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2830 }
2831
2832 if (!updatedShortcuts.isEmpty()) {
2833 final UserHandleCompat userFinal = user;
2834 mHandler.post(new Runnable() {
2835
2836 public void run() {
2837 Callbacks cb = getCallback();
2838 if (cb != null && callbacks == cb) {
2839 cb.bindShortcutsChanged(updatedShortcuts,
2840 new ArrayList<ShortcutInfo>(), userFinal);
2841 }
2842 }
2843 });
2844 }
2845
2846 if (!updatedApps.isEmpty()) {
2847 mHandler.post(new Runnable() {
2848
2849 public void run() {
2850 Callbacks cb = getCallback();
2851 if (cb != null && callbacks == cb) {
2852 cb.bindAppsUpdated(updatedApps);
2853 }
2854 }
2855 });
2856 }
2857 }
2858
Joe Onorato36115782010-06-17 13:28:48 -04002859 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002860 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002861 }
2862
Adam Cohen091440a2015-03-18 14:16:05 -07002863 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002864
2865 @Override
2866 public void onReceive(Context context, Intent intent) {
2867 synchronized (sBgLock) {
2868 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2869 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002870 final PackageManager manager = context.getPackageManager();
2871 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2872 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002873 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2874 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002875 packagesRemoved.clear();
2876 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002877 for (String pkg : entry.getValue()) {
2878 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002879 boolean packageOnSdcard = launcherApps.isAppEnabled(
2880 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2881 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002882 packagesUnavailable.add(pkg);
2883 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002884 packagesRemoved.add(pkg);
2885 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002886 }
2887 }
2888 if (!packagesRemoved.isEmpty()) {
2889 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2890 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2891 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002892 if (!packagesUnavailable.isEmpty()) {
2893 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2894 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2895 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002896 }
Sunny Goyal34942622014-08-29 17:20:55 -07002897 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002898 }
2899 }
2900 }
2901
Joe Onorato36115782010-06-17 13:28:48 -04002902 private class PackageUpdatedTask implements Runnable {
2903 int mOp;
2904 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002905 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002906
2907 public static final int OP_NONE = 0;
2908 public static final int OP_ADD = 1;
2909 public static final int OP_UPDATE = 2;
2910 public static final int OP_REMOVE = 3; // uninstlled
2911 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002912 public static final int OP_SUSPEND = 5; // package suspended
2913 public static final int OP_UNSUSPEND = 6; // package unsuspended
Joe Onorato36115782010-06-17 13:28:48 -04002914
2915
Kenny Guyed131872014-04-30 03:02:21 +01002916 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002917 mOp = op;
2918 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002919 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002920 }
2921
2922 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002923 if (!mHasLoaderCompletedOnce) {
2924 // Loader has not yet run.
2925 return;
2926 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002927 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002928
2929 final String[] packages = mPackages;
2930 final int N = packages.length;
2931 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002932 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002933 for (int i=0; i<N; i++) {
2934 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002935 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002936 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002937 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002938
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002939 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2940 if (heuristic != null) {
2941 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002942 }
Joe Onorato36115782010-06-17 13:28:48 -04002943 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002944 }
Joe Onorato36115782010-06-17 13:28:48 -04002945 case OP_UPDATE:
2946 for (int i=0; i<N; i++) {
2947 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002948 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002949 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002950 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002951 }
2952 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002953 case OP_REMOVE: {
2954 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2955 if (heuristic != null) {
2956 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002957 }
Joe Onorato36115782010-06-17 13:28:48 -04002958 for (int i=0; i<N; i++) {
2959 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002960 mIconCache.removeIconsForPkg(packages[i], mUser);
2961 }
2962 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002963 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002964 case OP_UNAVAILABLE:
2965 for (int i=0; i<N; i++) {
2966 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2967 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002968 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002969 }
2970 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002971 case OP_SUSPEND:
2972 case OP_UNSUSPEND:
2973 boolean suspend = mOp == OP_SUSPEND;
2974 for (int i=0; i<N; i++) {
2975 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.suspendPackage "
2976 + suspend + " " + packages[i]);
2977 mBgAllAppsList.suspendPackage(packages[i], mUser, suspend);
2978 }
2979 break;
Joe Onorato36115782010-06-17 13:28:48 -04002980 }
2981
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002982 ArrayList<AppInfo> added = null;
2983 ArrayList<AppInfo> modified = null;
2984 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002985
Adam Cohen487f7dd2012-06-28 18:12:10 -07002986 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002987 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002988 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002989 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002990 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002991 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002992 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002993 }
Winson Chung5d55f332012-07-16 20:45:03 -07002994 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002995 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002996 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002997 }
2998
Sunny Goyale0f58d72014-11-10 18:05:31 -08002999 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003000 if (callbacks == null) {
3001 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3002 return;
3003 }
3004
Sunny Goyal4390ace2014-10-13 11:33:11 -07003005 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3006 new HashMap<ComponentName, AppInfo>();
3007
Joe Onorato36115782010-06-17 13:28:48 -04003008 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003009 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003010 for (AppInfo ai : added) {
3011 addedOrUpdatedApps.put(ai.componentName, ai);
3012 }
Joe Onorato36115782010-06-17 13:28:48 -04003013 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003014
Joe Onorato36115782010-06-17 13:28:48 -04003015 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003016 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003017 for (AppInfo ai : modified) {
3018 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003019 }
3020
Joe Onorato36115782010-06-17 13:28:48 -04003021 mHandler.post(new Runnable() {
3022 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003023 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003024 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003025 callbacks.bindAppsUpdated(modifiedFinal);
3026 }
3027 }
3028 });
3029 }
Winson Chung83892cc2013-05-01 16:53:33 -07003030
Sunny Goyal4390ace2014-10-13 11:33:11 -07003031 // Update shortcut infos
Kenny Guy44cba692016-01-21 19:50:02 +00003032 if (mOp == OP_ADD || mOp == OP_UPDATE || mOp == OP_UNSUSPEND) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003033 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3034 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3035 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3036
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003037 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003038 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003039 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003040 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3041 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003042 boolean infoUpdated = false;
3043 boolean shortcutUpdated = false;
3044
Kenny Guy44cba692016-01-21 19:50:02 +00003045 if (mOp == OP_UNSUSPEND) {
3046 si.isDisabled &= ~ ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3047 infoUpdated = true;
3048 }
3049
Sunny Goyal4390ace2014-10-13 11:33:11 -07003050 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003051 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003052 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003053 Bitmap icon = Utilities.createIconBitmap(
3054 si.iconResource.packageName,
3055 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003056 if (icon != null) {
3057 si.setIcon(icon);
3058 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003059 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003060 }
3061 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003062
3063 ComponentName cn = si.getTargetComponent();
3064 if (cn != null && packageSet.contains(cn.getPackageName())) {
3065 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3066
3067 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003068 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3069 // Auto install icon
3070 PackageManager pm = context.getPackageManager();
3071 ResolveInfo matched = pm.resolveActivity(
3072 new Intent(Intent.ACTION_MAIN)
3073 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3074 PackageManager.MATCH_DEFAULT_ONLY);
3075 if (matched == null) {
3076 // Try to find the best match activity.
3077 Intent intent = pm.getLaunchIntentForPackage(
3078 cn.getPackageName());
3079 if (intent != null) {
3080 cn = intent.getComponent();
3081 appInfo = addedOrUpdatedApps.get(cn);
3082 }
3083
3084 if ((intent == null) || (appInfo == null)) {
3085 removedShortcuts.add(si);
3086 continue;
3087 }
3088 si.promisedIntent = intent;
3089 }
3090 }
3091
3092 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003093 if (appInfo != null) {
3094 si.flags = appInfo.flags;
3095 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003096
Sunny Goyal756adbc2015-04-16 15:20:51 -07003097 si.intent = si.promisedIntent;
3098 si.promisedIntent = null;
3099 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003100 infoUpdated = true;
3101 si.updateIcon(mIconCache);
3102 }
3103
3104 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3105 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3106 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003107 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 si.contentDescription = appInfo.contentDescription;
3109 infoUpdated = true;
3110 }
3111
3112 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3113 // Since package was just updated, the target must be available now.
3114 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3115 shortcutUpdated = true;
3116 }
3117 }
3118
3119 if (infoUpdated || shortcutUpdated) {
3120 updatedShortcuts.add(si);
3121 }
3122 if (infoUpdated) {
3123 updateItemInDatabase(context, si);
3124 }
3125 } else if (info instanceof LauncherAppWidgetInfo) {
3126 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3127 if (mUser.equals(widgetInfo.user)
3128 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3129 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003130 widgetInfo.restoreStatus &=
3131 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3132 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003133
3134 // adding this flag ensures that launcher shows 'click to setup'
3135 // if the widget has a config activity. In case there is no config
3136 // activity, it will be marked as 'restored' during bind.
3137 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3138
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139 widgets.add(widgetInfo);
3140 updateItemInDatabase(context, widgetInfo);
3141 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003142 }
3143 }
3144 }
3145
Sunny Goyal4390ace2014-10-13 11:33:11 -07003146 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3147 mHandler.post(new Runnable() {
3148
3149 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003150 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003151 if (callbacks == cb && cb != null) {
3152 callbacks.bindShortcutsChanged(
3153 updatedShortcuts, removedShortcuts, mUser);
3154 }
3155 }
3156 });
3157 if (!removedShortcuts.isEmpty()) {
3158 deleteItemsFromDatabase(context, removedShortcuts);
3159 }
3160 }
3161 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003162 mHandler.post(new Runnable() {
3163 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003164 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003165 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003166 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003167 }
3168 }
3169 });
3170 }
3171 }
3172
Winson Chungdf95eb12013-10-16 14:57:07 -07003173 final ArrayList<String> removedPackageNames =
3174 new ArrayList<String>();
Kenny Guy44cba692016-01-21 19:50:02 +00003175 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE || mOp == OP_SUSPEND) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003176 // Mark all packages in the broadcast to be removed
3177 removedPackageNames.addAll(Arrays.asList(packages));
3178 } else if (mOp == OP_UPDATE) {
3179 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003180 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003181 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003182 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003183 }
3184 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003185 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003186
Winson Chungdf95eb12013-10-16 14:57:07 -07003187 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003188 final int removeReason;
3189 if (mOp == OP_UNAVAILABLE) {
3190 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
Kenny Guy44cba692016-01-21 19:50:02 +00003191 } else if (mOp == OP_SUSPEND) {
3192 removeReason = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
Sunny Goyal1a745e82014-10-02 15:58:31 -07003193 } else {
3194 // Remove all the components associated with this package
3195 for (String pn : removedPackageNames) {
3196 deletePackageFromDatabase(context, pn, mUser);
3197 }
3198 // Remove all the specific components
3199 for (AppInfo a : removedApps) {
3200 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3201 deleteItemsFromDatabase(context, infos);
3202 }
3203 removeReason = 0;
3204 }
3205
Winson Chungdf95eb12013-10-16 14:57:07 -07003206 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003207 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003208 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003209 mHandler.post(new Runnable() {
3210 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003211 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003212 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003213 callbacks.bindComponentsRemoved(
3214 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003215 }
3216 }
3217 });
Joe Onoratobe386092009-11-17 17:32:16 -08003218 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003219
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003220 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3221 // get widget update signals.
3222 if (!Utilities.ATLEAST_MARSHMALLOW &&
3223 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003224 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003225 public void run() {
3226 Callbacks cb = getCallback();
3227 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003228 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003229 }
3230 }
3231 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003232 }
3233 }
3234 }
3235
3236 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3237 mHandler.post(new Runnable() {
3238 @Override
3239 public void run() {
3240 Callbacks cb = getCallback();
3241 if (callbacks == cb && cb != null) {
3242 callbacks.bindWidgetsModel(model);
3243 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003244 }
3245 });
3246 }
3247
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003248 public void refreshAndBindWidgetsAndShortcuts(
3249 final Callbacks callbacks, final boolean bindFirst) {
3250 runOnWorkerThread(new Runnable() {
3251 @Override
3252 public void run() {
3253 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3254 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003255 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003256 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3257 bindWidgetsModel(callbacks, model);
3258 // update the Widget entries inside DB on the worker thread.
3259 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3260 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003261 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003262 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003263 }
3264
Adam Cohen091440a2015-03-18 14:16:05 -07003265 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003266 UserHandleCompat user) {
3267 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3268 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003269 }
Adam Cohen556f6132014-01-15 15:18:08 -08003270
Kenny Guyed131872014-04-30 03:02:21 +01003271 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3272 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003273 if (cn == null) {
3274 return false;
3275 }
Kenny Guyed131872014-04-30 03:02:21 +01003276 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3277 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003278 return false;
3279 }
Kenny Guyed131872014-04-30 03:02:21 +01003280 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003281 }
3282
Adam Cohena28b78e2014-05-20 17:03:04 -07003283 public static boolean isValidPackage(Context context, String packageName,
3284 UserHandleCompat user) {
3285 if (packageName == null) {
3286 return false;
3287 }
3288 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3289 return launcherApps.isPackageEnabledForProfile(packageName, user);
3290 }
3291
Joe Onorato9c1289c2009-08-17 11:03:03 -04003292 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003293 * Make an ShortcutInfo object for a restored application or shortcut item that points
3294 * to a package that is not yet installed on the system.
3295 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003296 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003297 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003298 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003299 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003300
3301 Bitmap icon = iconInfo.loadIcon(c, info, context);
3302 // the fallback icon
3303 if (icon == null) {
3304 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3305 } else {
3306 info.setIcon(icon);
3307 }
Sunny Goyal34942622014-08-29 17:20:55 -07003308
3309 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003310 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003311 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003312 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003313 }
Sunny Goyal34942622014-08-29 17:20:55 -07003314 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3315 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003316 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003317 }
Sunny Goyal34942622014-08-29 17:20:55 -07003318 } else {
3319 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3320 }
3321
Winson Chung82b016c2015-05-08 17:00:10 -07003322 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003323 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003324 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003325 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003326 return info;
3327 }
3328
3329 /**
3330 * Make an Intent object for a restored application or shortcut item that points
3331 * to the market page for the item.
3332 */
Adam Cohen091440a2015-03-18 14:16:05 -07003333 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003334 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003335 return getMarketIntent(componentName.getPackageName());
3336 }
3337
3338 static Intent getMarketIntent(String packageName) {
3339 return new Intent(Intent.ACTION_VIEW)
3340 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003341 .scheme("market")
3342 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003343 .appendQueryParameter("id", packageName)
3344 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003345 }
3346
3347 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003348 * Make an ShortcutInfo object for a shortcut that is an application.
3349 *
3350 * If c is not null, then it will be used to fill in missing data like the title and icon.
3351 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003352 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003353 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003354 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003355 if (user == null) {
3356 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003357 return null;
3358 }
3359
Kenny Guyed131872014-04-30 03:02:21 +01003360 ComponentName componentName = intent.getComponent();
3361 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003362 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003363 return null;
3364 }
3365
3366 Intent newIntent = new Intent(intent.getAction(), null);
3367 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3368 newIntent.setComponent(componentName);
3369 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003370 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003371 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3372 return null;
3373 }
3374
3375 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003376 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003377 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3378 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3379 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003380 }
3381
Joe Onorato56d82912010-03-07 14:32:10 -05003382 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003383 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003384 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003385 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003386
Joe Onorato56d82912010-03-07 14:32:10 -05003387 // fall back to the class name of the activity
3388 if (info.title == null) {
3389 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003390 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003391
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003393 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003394 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003395 if (lai != null) {
3396 info.flags = AppInfo.initFlags(lai);
3397 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003399 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003400
Sunny Goyale2df0622015-04-24 11:27:00 -07003401 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003402 ItemInfoFilter f) {
3403 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3404 for (ItemInfo i : infos) {
3405 if (i instanceof ShortcutInfo) {
3406 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003407 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003408 if (cn != null && f.filterItem(null, info, cn)) {
3409 filtered.add(info);
3410 }
3411 } else if (i instanceof FolderInfo) {
3412 FolderInfo info = (FolderInfo) i;
3413 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003414 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003415 if (cn != null && f.filterItem(info, s, cn)) {
3416 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003417 }
3418 }
Winson Chung64359a52013-07-08 17:17:08 -07003419 } else if (i instanceof LauncherAppWidgetInfo) {
3420 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3421 ComponentName cn = info.providerName;
3422 if (cn != null && f.filterItem(null, info, cn)) {
3423 filtered.add(info);
3424 }
Winson Chung8a435102012-08-30 17:16:53 -07003425 }
3426 }
Winson Chung64359a52013-07-08 17:17:08 -07003427 return new ArrayList<ItemInfo>(filtered);
3428 }
3429
Adam Cohen091440a2015-03-18 14:16:05 -07003430 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003431 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003432 ItemInfoFilter filter = new ItemInfoFilter() {
3433 @Override
3434 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003435 if (info.user == null) {
3436 return cn.equals(cname);
3437 } else {
3438 return cn.equals(cname) && info.user.equals(user);
3439 }
Winson Chung64359a52013-07-08 17:17:08 -07003440 }
3441 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003442 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003443 }
3444
Sunny Goyal1a745e82014-10-02 15:58:31 -07003445 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003446 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003447 */
Adam Cohen091440a2015-03-18 14:16:05 -07003448 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003449 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003450 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003451 // Non-app shortcuts are only supported for current user.
3452 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003453 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003454
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003455 // TODO: If there's an explicit component and we can't install that, delete it.
3456
Winson Chung82b016c2015-05-08 17:00:10 -07003457 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003458
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003459 Bitmap icon = iconInfo.loadIcon(c, info, context);
3460 // the fallback icon
3461 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003462 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003463 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003464 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003465 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003466 return info;
3467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003468
Sunny Goyal2350bc92014-10-14 16:42:54 -07003469 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003470 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3471 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3472 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3473
Adam Cohend9198822011-11-22 16:42:47 -08003474 if (intent == null) {
3475 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3476 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3477 return null;
3478 }
3479
Joe Onorato0589f0f2010-02-08 13:44:00 -08003480 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003481 boolean customIcon = false;
3482 ShortcutIconResource iconResource = null;
3483
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003484 if (bitmap instanceof Bitmap) {
3485 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003486 customIcon = true;
3487 } else {
3488 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003489 if (extra instanceof ShortcutIconResource) {
3490 iconResource = (ShortcutIconResource) extra;
3491 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003492 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003493 }
3494 }
3495
Michael Jurkac9d95c52011-08-29 14:03:34 -07003496 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003497
Kenny Guyed131872014-04-30 03:02:21 +01003498 // Only support intents for current user for now. Intents sent from other
3499 // users wouldn't get here without intent forwarding anyway.
3500 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003501 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003502 icon = mIconCache.getDefaultIcon(info.user);
3503 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003504 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003505 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003506
Winson Chung82b016c2015-05-08 17:00:10 -07003507 info.title = Utilities.trim(name);
3508 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003509 info.intent = intent;
3510 info.customIcon = customIcon;
3511 info.iconResource = iconResource;
3512
3513 return info;
3514 }
3515
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003517 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518 * or make a new one.
3519 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003520 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 // See if a placeholder was created for us already
3522 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003523 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003525 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 folders.put(id, folderInfo);
3527 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003528 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003529 }
3530
Joe Onoratobe386092009-11-17 17:32:16 -08003531
Sunny Goyal651077b2014-06-30 14:15:31 -07003532 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3533 return (provider != null) && (provider.provider != null)
3534 && (provider.provider.getPackageName() != null);
3535 }
3536
Joe Onoratobe386092009-11-17 17:32:16 -08003537 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003538 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003539 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3540 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3541 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3542 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003543 if (mLoaderTask != null) {
3544 mLoaderTask.dumpState();
3545 } else {
3546 Log.d(TAG, "mLoaderTask=null");
3547 }
Joe Onoratobe386092009-11-17 17:32:16 -08003548 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003549
3550 public Callbacks getCallback() {
3551 return mCallbacks != null ? mCallbacks.get() : null;
3552 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003553
3554 /**
3555 * @return {@link FolderInfo} if its already loaded.
3556 */
3557 public FolderInfo findFolderById(Long folderId) {
3558 synchronized (sBgLock) {
3559 return sBgFolders.get(folderId);
3560 }
3561 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003562
Sunny Goyal527c7d32015-08-28 15:19:36 -07003563 @Thunk class DeferredMainThreadExecutor implements Executor {
3564
3565 @Override
3566 public void execute(Runnable command) {
3567 runOnMainThread(command);
3568 }
3569 }
3570
Sunny Goyal756adbc2015-04-16 15:20:51 -07003571 /**
3572 * @return the looper for the worker thread which can be used to start background tasks.
3573 */
3574 public static Looper getWorkerLooper() {
3575 return sWorkerThread.getLooper();
3576 }
Sunny Goyala1365452015-10-01 15:46:24 -07003577
3578 @Thunk static final void addDumpLog(String log) {
3579 Launcher.addDumpLog(TAG, log);
3580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003581}