blob: a82fa29a079cb2503424200000d76c22fde647ca [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.database.Cursor;
34import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080036import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040037import android.os.Handler;
38import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070039import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040043import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080044import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080046import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070047import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010048
Sunny Goyalffe83f12014-08-14 17:39:34 -070049import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010050import com.android.launcher3.compat.LauncherActivityInfoCompat;
51import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070052import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070053import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010054import com.android.launcher3.compat.UserHandleCompat;
55import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Sunny Goyal26119432016-02-18 22:09:23 +000057import com.android.launcher3.folder.Folder;
58import com.android.launcher3.folder.FolderIcon;
Sunny Goyalf862a262015-12-14 14:27:38 -080059import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070063import com.android.launcher3.util.FlagOp;
Sunny Goyale2df0622015-04-24 11:27:00 -070064import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070065import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyalda891c12016-03-18 18:29:24 -070066import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070067import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070068import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080069
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.lang.ref.WeakReference;
71import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070072import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070074import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070075import java.util.Collections;
76import java.util.Comparator;
77import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070079import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070081import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070083import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085/**
86 * Maintains in-memory state of the Launcher. It is expected that there should be only one
87 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070088 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 */
Kenny Guyed131872014-04-30 03:02:21 +010090public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010091 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080092 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040093 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070094 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070097
Dan Sandlerd5024042014-01-09 15:01:33 -050098 public static final int LOADER_FLAG_NONE = 0;
99 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
100 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
101
Joe Onorato36115782010-06-17 13:28:48 -0400102 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500103 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800104
Winson Chunga6945242014-01-08 14:04:34 -0800105 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400106
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk final LauncherAppState mApp;
108 @Thunk final Object mLock = new Object();
109 @Thunk DeferredHandler mHandler = new DeferredHandler();
110 @Thunk LoaderTask mLoaderTask;
111 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700112 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Jason Monkbbe1e242014-05-16 17:37:34 -0400114 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700115
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700117 static {
118 sWorkerThread.start();
119 }
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 // We start off with everything not loaded. After that, we assume that
123 // our monitoring of the package manager provides all updates and we never
124 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk boolean mWorkspaceLoaded;
126 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700127
Sunny Goyal756a28a2015-04-23 17:07:55 -0700128 /**
129 * Set of runnables to be called on the background thread after the workspace binding
130 * is complete.
131 */
132 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
133
Adam Cohen091440a2015-03-18 14:16:05 -0700134 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700136 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800137 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700138 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800139 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800140
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700141 // The lock that must be acquired before referencing any static bg data structures. Unlike
142 // other locks, this one can generally be held long-term because we never expect any of these
143 // static data structures to be referenced outside of the worker thread except on the first
144 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700145 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700146
Adam Cohen487f7dd2012-06-28 18:12:10 -0700147 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700148 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700149 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
152 // created by LauncherModel that are directly on the home screen (however, no widgets or
153 // shortcuts within folders).
154 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
157 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158 new ArrayList<LauncherAppWidgetInfo>();
159
Adam Cohen487f7dd2012-06-28 18:12:10 -0700160 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700161 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700162
Adam Cohendcd297f2013-06-18 13:13:40 -0700163 // sBgWorkspaceScreens is the ordered set of workspace screens.
164 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
165
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700166 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700167 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
168 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700169
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700170 // </ only access in worker thread >
171
Adam Cohen091440a2015-03-18 14:16:05 -0700172 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Adam Cohen091440a2015-03-18 14:16:05 -0700174 @Thunk final LauncherAppsCompat mLauncherApps;
175 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700178 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700180 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700182 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
183 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700184 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700185 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700186 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800187 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200189 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700190 public void bindAppsAdded(ArrayList<Long> newScreens,
191 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700192 ArrayList<ItemInfo> addAnimated,
193 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700195 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
196 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
197 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700198 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700199 public void bindWorkspaceComponentsRemoved(
200 HashSet<String> packageNames, HashSet<ComponentName> components,
201 UserHandleCompat user);
202 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800203 public void notifyWidgetProvidersChanged();
204 public void bindWidgetsModel(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700205 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700206 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700207 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700208 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chung64359a52013-07-08 17:17:08 -0700211 public interface ItemInfoFilter {
212 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
213 }
214
Bjorn Bringert1307f632013-10-03 22:31:03 +0100215 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800216 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400217
Adam Cohen71483f42014-05-15 14:04:01 -0700218 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400219 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
220 // resource string.
221 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
222 ProviderInfo providerInfo =
223 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
224 ProviderInfo redirectProvider =
225 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700226
227 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400228 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700229
230 if (mOldContentProviderExists) {
231 Log.d(TAG, "Old launcher provider exists.");
232 } else {
233 Log.d(TAG, "Old launcher provider does not exist.");
234 }
235
Daniel Sandlere4f98912013-06-25 15:13:26 -0400236 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100237 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700238 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800239 mIconCache = iconCache;
240
Kenny Guyed131872014-04-30 03:02:21 +0100241 mLauncherApps = LauncherAppsCompat.getInstance(context);
242 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 }
244
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700245 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
246 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700248 if (sWorkerThread.getThreadId() == Process.myTid()) {
249 // If we are on the worker thread, post onto the main handler
250 mHandler.post(r);
251 } else {
252 r.run();
253 }
254 }
255
256 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
257 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700258 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700259 if (sWorkerThread.getThreadId() == Process.myTid()) {
260 r.run();
261 } else {
262 // If we are not on the worker thread, then post to the worker handler
263 sWorker.post(r);
264 }
265 }
266
Winson Chunge43a1e72014-01-15 10:33:02 -0800267 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
268 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800269 }
270
Sunny Goyal756adbc2015-04-16 15:20:51 -0700271 public void setPackageState(final PackageInstallInfo installInfo) {
272 Runnable updateRunnable = new Runnable() {
273
274 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500275 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700276 synchronized (sBgLock) {
277 final HashSet<ItemInfo> updates = new HashSet<>();
278
279 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
280 // Ignore install success events as they are handled by Package add events.
281 return;
282 }
283
Sunny Goyale2df0622015-04-24 11:27:00 -0700284 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700285 if (info instanceof ShortcutInfo) {
286 ShortcutInfo si = (ShortcutInfo) info;
287 ComponentName cn = si.getTargetComponent();
288 if (si.isPromise() && (cn != null)
289 && installInfo.packageName.equals(cn.getPackageName())) {
290 si.setInstallProgress(installInfo.progress);
291
292 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
293 // Mark this info as broken.
294 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
295 }
296 updates.add(si);
297 }
298 }
299 }
300
301 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
302 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
303 widget.installProgress = installInfo.progress;
304 updates.add(widget);
305 }
306 }
307
308 if (!updates.isEmpty()) {
309 // Push changes to the callback.
310 Runnable r = new Runnable() {
311 public void run() {
312 Callbacks callbacks = getCallback();
313 if (callbacks != null) {
314 callbacks.bindRestoreItemsChange(updates);
315 }
316 }
317 };
318 mHandler.post(r);
319 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500320 }
321 }
322 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700323 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500324 }
325
Sunny Goyal756adbc2015-04-16 15:20:51 -0700326 /**
327 * Updates the icons and label of all pending icons for the provided package name.
328 */
329 public void updateSessionDisplayInfo(final String packageName) {
330 Runnable updateRunnable = new Runnable() {
331
332 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700333 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700334 synchronized (sBgLock) {
335 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
336 final UserHandleCompat user = UserHandleCompat.myUserHandle();
337
Sunny Goyale2df0622015-04-24 11:27:00 -0700338 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 if (info instanceof ShortcutInfo) {
340 ShortcutInfo si = (ShortcutInfo) info;
341 ComponentName cn = si.getTargetComponent();
342 if (si.isPromise() && (cn != null)
343 && packageName.equals(cn.getPackageName())) {
344 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
345 // For auto install apps update the icon as well as label.
346 mIconCache.getTitleAndIcon(si,
347 si.promisedIntent, user,
348 si.shouldUseLowResIcon());
349 } else {
350 // Only update the icon for restored apps.
351 si.updateIcon(mIconCache);
352 }
353 updates.add(si);
354 }
355 }
356 }
357
358 if (!updates.isEmpty()) {
359 // Push changes to the callback.
360 Runnable r = new Runnable() {
361 public void run() {
362 Callbacks callbacks = getCallback();
363 if (callbacks != null) {
364 callbacks.bindShortcutsChanged(updates,
365 new ArrayList<ShortcutInfo>(), user);
366 }
367 }
368 };
369 mHandler.post(r);
370 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700371 }
372 }
373 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700374 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700375 }
376
Adam Cohen76a47a12014-02-05 11:47:43 -0800377 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800378 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800379
380 if (allAppsApps == null) {
381 throw new RuntimeException("allAppsApps must not be null");
382 }
383 if (allAppsApps.isEmpty()) {
384 return;
385 }
386
387 // Process the newly added applications and add them to the database first
388 Runnable r = new Runnable() {
389 public void run() {
390 runOnMainThread(new Runnable() {
391 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800392 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800393 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500394 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800395 }
396 }
397 });
398 }
399 };
400 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700401 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800402
Sunny Goyala9116722015-04-29 13:55:58 -0700403 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 int[] xy, int spanX, int spanY) {
405 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700406 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
407 final int xCount = (int) profile.numColumns;
408 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 boolean[][] occupied = new boolean[xCount][yCount];
410 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700411 for (ItemInfo r : occupiedPos) {
412 int right = r.cellX + r.spanX;
413 int bottom = r.cellY + r.spanY;
414 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
415 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800416 occupied[x][y] = true;
417 }
418 }
419 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800420 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700421 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800422 }
423
424 /**
425 * Find a position on the screen for the given size or adds a new screen.
426 * @return screenId and the coordinates for the item.
427 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700428 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700429 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 ArrayList<Long> workspaceScreens,
431 ArrayList<Long> addedWorkspaceScreensFinal,
432 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700433 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434
Sunny Goyala9116722015-04-29 13:55:58 -0700435 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700436 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700437 synchronized (sBgLock) {
438 for (ItemInfo info : sBgItemsIdMap) {
439 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
440 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
441 if (items == null) {
442 items = new ArrayList<>();
443 screenItems.put(info.screenId, items);
444 }
445 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800446 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 }
449
450 // Find appropriate space for the item.
451 long screenId = 0;
452 int[] cordinates = new int[2];
453 boolean found = false;
454
455 int screenCount = workspaceScreens.size();
456 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700457 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800458 if (preferredScreenIndex < screenCount) {
459 screenId = workspaceScreens.get(preferredScreenIndex);
460 found = findNextAvailableIconSpaceInScreen(
461 screenItems.get(screenId), cordinates, spanX, spanY);
462 }
463
464 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700465 // Search on any of the screens starting from the first screen.
466 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800467 screenId = workspaceScreens.get(screen);
468 if (findNextAvailableIconSpaceInScreen(
469 screenItems.get(screenId), cordinates, spanX, spanY)) {
470 // We found a space for it
471 found = true;
472 break;
473 }
474 }
475 }
476
477 if (!found) {
478 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700479 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
480 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
481 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482
483 // Save the screen id for binding in the workspace
484 workspaceScreens.add(screenId);
485 addedWorkspaceScreensFinal.add(screenId);
486
487 // If we still can't find an empty space, then God help us all!!!
488 if (!findNextAvailableIconSpaceInScreen(
489 screenItems.get(screenId), cordinates, spanX, spanY)) {
490 throw new RuntimeException("Can't find space to add the item");
491 }
492 }
493 return Pair.create(screenId, cordinates);
494 }
495
496 /**
497 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700499 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700500 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800502 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700503 return;
Winson Chung997a9232013-07-24 15:33:46 -0700504 }
Winson Chung64359a52013-07-08 17:17:08 -0700505 // Process the newly added applications and add them to the database first
506 Runnable r = new Runnable() {
507 public void run() {
508 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
509 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
510
Winson Chung76828c82013-08-19 15:43:29 -0700511 // Get the list of workspace screens. We need to append to this list and
512 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
513 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800514 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700515 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800516 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700517 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700519 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700521 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 }
Winson Chung76828c82013-08-19 15:43:29 -0700523
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700525 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700526 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800527 long screenId = coords.first;
528 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700529
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700530 ItemInfo itemInfo;
531 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
532 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700534 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700535 } else {
536 throw new RuntimeException("Unexpected info type");
537 }
Winson Chung94d67682013-09-25 16:29:40 -0700538
Winson Chung64359a52013-07-08 17:17:08 -0700539 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700540 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700541 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700542 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700543 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700544 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700545 }
546 }
547
Winson Chung76828c82013-08-19 15:43:29 -0700548 // Update the workspace screens
549 updateWorkspaceScreenOrder(context, workspaceScreens);
550
Adam Cohen76a47a12014-02-05 11:47:43 -0800551 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700552 runOnMainThread(new Runnable() {
553 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800554 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700555 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700556 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
557 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700558 if (!addedShortcutsFinal.isEmpty()) {
559 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
560 long lastScreenId = info.screenId;
561 for (ItemInfo i : addedShortcutsFinal) {
562 if (i.screenId == lastScreenId) {
563 addAnimated.add(i);
564 } else {
565 addNotAnimated.add(i);
566 }
Winson Chung997a9232013-07-24 15:33:46 -0700567 }
568 }
Winson Chungd64d1762013-08-20 14:37:16 -0700569 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800570 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700571 }
Winson Chung64359a52013-07-08 17:17:08 -0700572 }
Winson Chung997a9232013-07-24 15:33:46 -0700573 });
574 }
Winson Chung64359a52013-07-08 17:17:08 -0700575 }
576 };
577 runOnWorkerThread(r);
578 }
579
Sunny Goyald33860f2015-04-23 16:02:20 -0700580 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700581 if (sWorkerThread.getThreadId() == Process.myTid()) {
582 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
583 "main thread");
584 }
585
Sunny Goyald33860f2015-04-23 16:02:20 -0700586 // Remove any queued UI runnables
587 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700588 // Unbind all the workspace items
589 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700590 }
591
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700592 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700593 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700594 // Ensure that we don't use the same workspace items data structure on the main thread
595 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700596 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700597 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700598 tmpItems.addAll(sBgWorkspaceItems);
599 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700600 }
601 Runnable r = new Runnable() {
602 @Override
603 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700604 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700605 item.unbind();
606 }
607 }
608 };
609 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700610 }
611
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 /**
613 * Adds an item to the DB if it was not created previously, or move it to a new
614 * <container, screen, cellX, cellY>
615 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800616 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700617 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618 if (item.container == ItemInfo.NO_ID) {
619 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700620 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 } else {
622 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700623 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 }
625 }
626
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700627 static void checkItemInfoLocked(
628 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
629 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
630 if (modelItem != null && item != modelItem) {
631 // check all the data is consistent
632 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
633 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
634 ShortcutInfo shortcut = (ShortcutInfo) item;
635 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
636 modelShortcut.intent.filterEquals(shortcut.intent) &&
637 modelShortcut.id == shortcut.id &&
638 modelShortcut.itemType == shortcut.itemType &&
639 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700641 modelShortcut.cellX == shortcut.cellX &&
642 modelShortcut.cellY == shortcut.cellY &&
643 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700644 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700645 // For all intents and purposes, this is the same object
646 return;
647 }
648 }
649
650 // the modelItem needs to match up perfectly with item if our model is
651 // to be consistent with the database-- for now, just require
652 // modelItem == item or the equality check above
653 String msg = "item: " + ((item != null) ? item.toString() : "null") +
654 "modelItem: " +
655 ((modelItem != null) ? modelItem.toString() : "null") +
656 "Error: ItemInfo passed to checkItemInfo doesn't match original";
657 RuntimeException e = new RuntimeException(msg);
658 if (stackTrace != null) {
659 e.setStackTrace(stackTrace);
660 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800661 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700662 }
663 }
664
Michael Jurka816474f2012-06-25 14:49:02 -0700665 static void checkItemInfo(final ItemInfo item) {
666 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
667 final long itemId = item.id;
668 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700669 public void run() {
670 synchronized (sBgLock) {
671 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700672 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700673 }
674 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700675 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700676 }
677
Michael Jurkac9d95c52011-08-29 14:03:34 -0700678 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
679 final ItemInfo item, final String callingFunction) {
680 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700681 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700682 final ContentResolver cr = context.getContentResolver();
683
Adam Cohen487f7dd2012-06-28 18:12:10 -0700684 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700685 Runnable r = new Runnable() {
686 public void run() {
687 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688 updateItemArrays(item, itemId, stackTrace);
689 }
690 };
691 runOnWorkerThread(r);
692 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700693
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
695 final ArrayList<ItemInfo> items, final String callingFunction) {
696 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700697
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
699 Runnable r = new Runnable() {
700 public void run() {
701 ArrayList<ContentProviderOperation> ops =
702 new ArrayList<ContentProviderOperation>();
703 int count = items.size();
704 for (int i = 0; i < count; i++) {
705 ItemInfo item = items.get(i);
706 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700707 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700708 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700709
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
711 updateItemArrays(item, itemId, stackTrace);
712
713 }
714 try {
715 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
716 } catch (Exception e) {
717 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700718 }
719 }
720 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700721 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700722 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700723
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700724 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
725 // Lock on mBgLock *after* the db operation
726 synchronized (sBgLock) {
727 checkItemInfoLocked(itemId, item, stackTrace);
728
729 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
730 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
731 // Item is in a folder, make sure this folder exists
732 if (!sBgFolders.containsKey(item.container)) {
733 // An items container is being set to a that of an item which is not in
734 // the list of Folders.
735 String msg = "item: " + item + " container being set to: " +
736 item.container + ", not in the list of folders";
737 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700738 }
739 }
740
741 // Items are added/removed from the corresponding FolderInfo elsewhere, such
742 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
743 // that are on the desktop, as appropriate
744 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800745 if (modelItem != null &&
746 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
747 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 switch (modelItem.itemType) {
749 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
750 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
751 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
752 if (!sBgWorkspaceItems.contains(modelItem)) {
753 sBgWorkspaceItems.add(modelItem);
754 }
755 break;
756 default:
757 break;
758 }
759 } else {
760 sBgWorkspaceItems.remove(modelItem);
761 }
762 }
763 }
764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400766 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700767 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700768 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700769 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 item.cellX = cellX;
772 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700773
Winson Chung3d503fb2011-07-13 17:25:49 -0700774 // We store hotseat items in canonical form which is this orientation invariant position
775 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700777 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700779 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782
783 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
786 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800787 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789
Michael Jurkac9d95c52011-08-29 14:03:34 -0700790 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700791 }
792
793 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700794 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
795 * cellX, cellY have already been updated on the ItemInfos.
796 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800797 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700798 final long container, final int screen) {
799
800 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
801 int count = items.size();
802
803 for (int i = 0; i < count; i++) {
804 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700805 item.container = container;
806
807 // We store hotseat items in canonical form which is this orientation invariant position
808 // in the hotseat
809 if (context instanceof Launcher && screen < 0 &&
810 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700811 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700812 item.cellY);
813 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700814 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700815 }
816
817 final ContentValues values = new ContentValues();
818 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
819 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
820 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800821 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700822 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823
824 contentValues.add(values);
825 }
826 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
827 }
828
829 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700830 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800831 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700832 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700833 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700834 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800835 item.cellX = cellX;
836 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700837 item.spanX = spanX;
838 item.spanY = spanY;
839
840 // We store hotseat items in canonical form which is this orientation invariant position
841 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700843 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700847 }
Adam Cohend4844c32011-02-18 19:25:06 -0800848
Adam Cohend4844c32011-02-18 19:25:06 -0800849 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800850 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
852 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800853 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700854 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
855 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800857
Michael Jurka816474f2012-06-25 14:49:02 -0700858 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700860
861 /**
862 * Update an item to the database in a specified container.
863 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700864 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700865 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100866 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700867 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800868 }
869
Sunny Goyal756a28a2015-04-23 17:07:55 -0700870 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700871 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700872 synchronized (mLock) {
873 if (!mHasLoaderCompletedOnce ||
874 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
875 throw new RuntimeException("Trying to add shortcut while loader is running");
876 }
877 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700878 }
879 }
880
Adam Cohend4844c32011-02-18 19:25:06 -0800881 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700882 * Returns true if the shortcuts already exists on the workspace. This must be called after
883 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700885 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
886 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700887 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700888 if (intent.getComponent() != null) {
889 // If component is not null, an intent with null package will produce
890 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700891 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700892 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700893 intentWithPkg = intent.toUri(0);
894 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700895 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700896 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
897 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700898 }
899 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700900 intentWithPkg = intent.toUri(0);
901 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700902 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700903
904 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700905 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700906 if (item instanceof ShortcutInfo) {
907 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700908 Intent targetIntent = info.promisedIntent == null
909 ? info.intent : info.promisedIntent;
910 if (targetIntent != null && info.user.equals(user)) {
911 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700912 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
913 return true;
914 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700915 }
916 }
917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700919 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700920 }
921
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 * Add an item to the database in a specified container. Sets the container, screen, cellX and
924 * cellY fields of the item. Also assigns an ID to the item.
925 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700926 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700927 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 item.cellX = cellX;
930 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700931 // We store hotseat items in canonical form which is this orientation invariant position
932 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939
940 final ContentValues values = new ContentValues();
941 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100942 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943
Sunny Goyald2497482015-09-22 18:24:19 -0700944 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
945 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
946
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700947 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700948
Jason Monk8e19cf22014-03-20 15:06:57 -0400949 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700950 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700952 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700954 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700955 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400956 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 sBgItemsIdMap.put(item.id, item);
958 switch (item.itemType) {
959 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
960 sBgFolders.put(item.id, (FolderInfo) item);
961 // Fall through
962 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
963 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
964 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
965 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
966 sBgWorkspaceItems.add(item);
967 } else {
968 if (!sBgFolders.containsKey(item.container)) {
969 // Adding an item to a folder that doesn't exist.
970 String msg = "adding item: " + item + " to a folder that " +
971 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700972 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700973 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700974 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700975 break;
976 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
977 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
978 break;
979 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700980 }
981 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700982 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700984 }
985
Sunny Goyal34942622014-08-29 17:20:55 -0700986 private static ArrayList<ItemInfo> getItemsByPackageName(
987 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700988 ItemInfoFilter filter = new ItemInfoFilter() {
989 @Override
990 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
991 return cn.getPackageName().equals(pn) && info.user.equals(user);
992 }
993 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700994 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700995 }
996
997 /**
998 * Removes all the items from the database corresponding to the specified package.
999 */
1000 static void deletePackageFromDatabase(Context context, final String pn,
1001 final UserHandleCompat user) {
1002 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001003 }
1004
1005 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001006 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001008 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001009 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1010 items.add(item);
1011 deleteItemsFromDatabase(context, items);
1012 }
1013
1014 /**
1015 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001016 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001017 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001019 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001020 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001021 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001022 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001023 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001025 // Lock on mBgLock *after* the db operation
1026 synchronized (sBgLock) {
1027 switch (item.itemType) {
1028 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1029 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001030 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001031 if (info.container == item.id) {
1032 // We are deleting a folder which still contains items that
1033 // think they are contained by that folder.
1034 String msg = "deleting a folder (" + item + ") which still " +
1035 "contains items (" + info + ")";
1036 Log.e(TAG, msg);
1037 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001039 sBgWorkspaceItems.remove(item);
1040 break;
1041 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1042 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1043 sBgWorkspaceItems.remove(item);
1044 break;
1045 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1046 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1047 break;
1048 }
1049 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001051 }
1052 }
Michael Jurka83df1882011-08-31 20:59:26 -07001053 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 }
1056
1057 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 * Update the order of the workspace screens in the database. The array list contains
1059 * a list of screen ids in the order that they should appear.
1060 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001061 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001062 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 final ContentResolver cr = context.getContentResolver();
1064 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1065
1066 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001067 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 while (iter.hasNext()) {
1069 long id = iter.next();
1070 if (id < 0) {
1071 iter.remove();
1072 }
1073 }
1074
1075 Runnable r = new Runnable() {
1076 @Override
1077 public void run() {
Yura085c8532014-02-11 15:15:29 +00001078 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001080 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001081 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 for (int i = 0; i < count; i++) {
1083 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001084 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1086 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001087 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001088 }
Yura085c8532014-02-11 15:15:29 +00001089
1090 try {
1091 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1092 } catch (Exception ex) {
1093 throw new RuntimeException(ex);
1094 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001095
Winson Chungba9c37f2013-08-30 14:11:37 -07001096 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001097 sBgWorkspaceScreens.clear();
1098 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001099 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 }
1101 };
1102 runOnWorkerThread(r);
1103 }
1104
1105 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001106 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 */
Winsonc0b52fe2015-09-09 16:38:15 -07001108 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 final ContentResolver cr = context.getContentResolver();
1110
Michael Jurkac9d95c52011-08-29 14:03:34 -07001111 Runnable r = new Runnable() {
1112 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001113 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001115 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 sBgItemsIdMap.remove(info.id);
1117 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 sBgWorkspaceItems.remove(info);
1119 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001120
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001121 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001122 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001124 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 for (ItemInfo childInfo : info.contents) {
1126 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001128 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001129 }
1130 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
1133
1134 /**
1135 * Set this as the current Launcher activity object for the loader.
1136 */
1137 public void initialize(Callbacks callbacks) {
1138 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001139 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1140 // workspace to prevent leaking Launcher activities on orientation change.
1141 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 mCallbacks = new WeakReference<Callbacks>(callbacks);
1143 }
1144 }
1145
Kenny Guyed131872014-04-30 03:02:21 +01001146 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001147 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001148 int op = PackageUpdatedTask.OP_UPDATE;
1149 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1150 user));
1151 }
1152
1153 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001154 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001155 int op = PackageUpdatedTask.OP_REMOVE;
1156 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1157 user));
1158 }
1159
1160 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001161 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001162 int op = PackageUpdatedTask.OP_ADD;
1163 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1164 user));
1165 }
1166
1167 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001168 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001169 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001170 enqueuePackageUpdated(
1171 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001172 }
1173
1174 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001175 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001176 boolean replacing) {
1177 if (!replacing) {
1178 enqueuePackageUpdated(new PackageUpdatedTask(
1179 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1180 user));
1181 }
Kenny Guyed131872014-04-30 03:02:21 +01001182 }
1183
Kenny Guy44cba692016-01-21 19:50:02 +00001184 @Override
1185 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1186 enqueuePackageUpdated(new PackageUpdatedTask(
1187 PackageUpdatedTask.OP_SUSPEND, packageNames,
1188 user));
1189 }
1190
1191 @Override
1192 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1193 enqueuePackageUpdated(new PackageUpdatedTask(
1194 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1195 user));
1196 }
1197
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001198 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1200 * ACTION_PACKAGE_CHANGED.
1201 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001202 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001203 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001204 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001205
Joe Onorato36115782010-06-17 13:28:48 -04001206 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001207 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001208 // If we have changed locale we need to clear out the labels in all apps/workspace.
1209 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001210 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001211 Callbacks callbacks = getCallback();
1212 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001213 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001214 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001215 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001216 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001217 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001218 forceReload();
Sunny Goyalda891c12016-03-18 18:29:24 -07001219 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED.equals(action)) {
1220 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1221 if (user != null) {
1222 enqueuePackageUpdated(new PackageUpdatedTask(
1223 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1224 new String[0], user));
1225 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001226 }
1227 }
1228
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001229 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001230 resetLoadedState(true, true);
1231
Reena Lee93f824a2011-09-23 17:20:28 -07001232 // Do this here because if the launcher activity is running it will be restarted.
1233 // If it's not running startLoaderFromBackground will merely tell it that it needs
1234 // to reload.
1235 startLoaderFromBackground();
1236 }
1237
Winson Chungf0c6ae02012-03-21 16:10:31 -07001238 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1239 synchronized (mLock) {
1240 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1241 // mWorkspaceLoaded to true later
1242 stopLoaderLocked();
1243 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1244 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1245 }
1246 }
1247
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001248 /**
1249 * When the launcher is in the background, it's possible for it to miss paired
1250 * configuration changes. So whenever we trigger the loader from the background
1251 * tell the launcher that it needs to re-run the loader when it comes back instead
1252 * of doing it now.
1253 */
1254 public void startLoaderFromBackground() {
1255 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001256 Callbacks callbacks = getCallback();
1257 if (callbacks != null) {
1258 // Only actually run the loader if they're not paused.
1259 if (!callbacks.setLoadOnResume()) {
1260 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001261 }
1262 }
1263 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001264 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001265 }
Joe Onorato36115782010-06-17 13:28:48 -04001266 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001267
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001268 /**
1269 * If there is already a loader task running, tell it to stop.
1270 */
1271 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001272 LoaderTask oldTask = mLoaderTask;
1273 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001274 oldTask.stopLocked();
1275 }
Reena Lee93f824a2011-09-23 17:20:28 -07001276 }
1277
Adam Cohen1a85c582014-09-30 09:48:49 -07001278 public boolean isCurrentCallbacks(Callbacks callbacks) {
1279 return (mCallbacks != null && mCallbacks.get() == callbacks);
1280 }
1281
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001282 public void startLoader(int synchronousBindPage) {
1283 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001284 }
1285
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001286 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001287 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1288 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001289 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001290 // Don't bother to start the thread if we know it's not going to do anything
1291 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001292 final Callbacks oldCallbacks = mCallbacks.get();
1293 // Clear any pending bind-runnables from the synchronized load process.
1294 runOnMainThread(new Runnable() {
1295 public void run() {
1296 oldCallbacks.clearPendingBinds();
1297 }
1298 });
1299
Joe Onorato36115782010-06-17 13:28:48 -04001300 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001301 stopLoaderLocked();
1302 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001303 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001304 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001305 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1306 } else {
1307 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1308 sWorker.post(mLoaderTask);
1309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001311 }
1312 }
1313
Joe Onorato36115782010-06-17 13:28:48 -04001314 public void stopLoader() {
1315 synchronized (mLock) {
1316 if (mLoaderTask != null) {
1317 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318 }
1319 }
Joe Onorato36115782010-06-17 13:28:48 -04001320 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001322 /**
1323 * Loads the workspace screen ids in an ordered list.
1324 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001325 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001326 final ContentResolver contentResolver = context.getContentResolver();
1327 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001328
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001329 // Get screens ordered by rank.
1330 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1331 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1332 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001333 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001334 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001335 while (sc.moveToNext()) {
1336 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001337 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001338 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001339 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001340 }
1341 }
1342 } finally {
1343 sc.close();
1344 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001345 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001346 }
1347
Joe Onorato36115782010-06-17 13:28:48 -04001348 /**
1349 * Runnable for the thread that loads the contents of the launcher:
1350 * - workspace icons
1351 * - widgets
1352 * - all apps icons
1353 */
1354 private class LoaderTask implements Runnable {
1355 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001356 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001357 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001358 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001359 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001360
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001361 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001362 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001363 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001364 }
1365
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001366 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001367 mIsLoadingAndBindingWorkspace = true;
1368
Joe Onorato36115782010-06-17 13:28:48 -04001369 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001370 if (DEBUG_LOADERS) {
1371 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001373
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001374 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001375 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001376 synchronized (LoaderTask.this) {
1377 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001378 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001379 }
1380 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 }
1382 }
1383
Joe Onorato36115782010-06-17 13:28:48 -04001384 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001385 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001386 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001387
Joe Onorato36115782010-06-17 13:28:48 -04001388 private void waitForIdle() {
1389 // Wait until the either we're stopped or the other threads are done.
1390 // This way we don't start loading all apps until the workspace has settled
1391 // down.
1392 synchronized (LoaderTask.this) {
1393 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001394
Joe Onorato36115782010-06-17 13:28:48 -04001395 mHandler.postIdle(new Runnable() {
1396 public void run() {
1397 synchronized (LoaderTask.this) {
1398 mLoadAndBindStepFinished = true;
1399 if (DEBUG_LOADERS) {
1400 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001401 }
Joe Onorato36115782010-06-17 13:28:48 -04001402 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001403 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001404 }
Joe Onorato36115782010-06-17 13:28:48 -04001405 });
1406
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001407 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001408 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001409 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1410 // wait no longer than 1sec at a time
1411 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001412 } catch (InterruptedException ex) {
1413 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001414 }
1415 }
Joe Onorato36115782010-06-17 13:28:48 -04001416 if (DEBUG_LOADERS) {
1417 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001418 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001419 + "ms for previous step to finish binding");
1420 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001421 }
Joe Onorato36115782010-06-17 13:28:48 -04001422 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001423
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001424 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001425 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001426 // Ensure that we have a valid page index to load synchronously
1427 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1428 "valid page index");
1429 }
1430 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1431 // Ensure that we don't try and bind a specified page when the pages have not been
1432 // loaded already (we should load everything asynchronously in that case)
1433 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1434 }
1435 synchronized (mLock) {
1436 if (mIsLoaderTaskRunning) {
1437 // Ensure that we are never running the background loading at this point since
1438 // we also touch the background collections
1439 throw new RuntimeException("Error! Background loading is already running");
1440 }
1441 }
1442
1443 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1444 // data structures, we can't allow any other thread to touch that data, but because
1445 // this call is synchronous, we can get away with not locking).
1446
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001447 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001448 // operations from the previous activity. We need to ensure that all queued operations
1449 // are executed before any synchronous binding work is done.
1450 mHandler.flush();
1451
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001452 // Divide the set of loaded items into those that we are binding synchronously, and
1453 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001454 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001455 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1456 // arise from that.
1457 onlyBindAllApps();
1458 }
1459
Joe Onorato36115782010-06-17 13:28:48 -04001460 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001462 if (mStopped) {
1463 return;
1464 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 mIsLoaderTaskRunning = true;
1466 }
Joe Onorato36115782010-06-17 13:28:48 -04001467 // Optimize for end-user experience: if the Launcher is up and // running with the
1468 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1469 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001470 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001471 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001472 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001473
Joe Onorato36115782010-06-17 13:28:48 -04001474 if (mStopped) {
1475 break keep_running;
1476 }
1477
Joe Onorato36115782010-06-17 13:28:48 -04001478 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001479
1480 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001481 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1482 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001483 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484
Joe Onorato36115782010-06-17 13:28:48 -04001485 // Clear out this reference, otherwise we end up holding it until all of the
1486 // callback runnables are done.
1487 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488
Joe Onorato36115782010-06-17 13:28:48 -04001489 synchronized (mLock) {
1490 // If we are still the last one to be scheduled, remove ourselves.
1491 if (mLoaderTask == this) {
1492 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001494 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001495 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
Joe Onorato36115782010-06-17 13:28:48 -04001497 }
1498
1499 public void stopLocked() {
1500 synchronized (LoaderTask.this) {
1501 mStopped = true;
1502 this.notify();
1503 }
1504 }
1505
1506 /**
1507 * Gets the callbacks object. If we've been stopped, or if the launcher object
1508 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1509 * object that was around when the deferred message was scheduled, and if there's
1510 * a new Callbacks object around then also return null. This will save us from
1511 * calling onto it with data that will be ignored.
1512 */
1513 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1514 synchronized (mLock) {
1515 if (mStopped) {
1516 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001517 }
Joe Onorato36115782010-06-17 13:28:48 -04001518
1519 if (mCallbacks == null) {
1520 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001521 }
Joe Onorato36115782010-06-17 13:28:48 -04001522
1523 final Callbacks callbacks = mCallbacks.get();
1524 if (callbacks != oldCallbacks) {
1525 return null;
1526 }
1527 if (callbacks == null) {
1528 Log.w(TAG, "no mCallbacks");
1529 return null;
1530 }
1531
1532 return callbacks;
1533 }
1534 }
1535
1536 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001537 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1538 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001539 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001540 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001541 final int countX = profile.numColumns;
1542 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001545 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001546 // Return early if we detect that an item is under the hotseat button
1547 if (mCallbacks == null ||
1548 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001549 Log.e(TAG, "Error loading shortcut into hotseat " + item
1550 + " into position (" + item.screenId + ":" + item.cellX + ","
1551 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001552 return false;
1553 }
1554
Dan Sandler295ae182013-12-10 16:05:47 -05001555 final ItemInfo[][] hotseatItems =
1556 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1557
Adam Cohen2e6da152015-05-06 11:42:25 -07001558 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001559 Log.e(TAG, "Error loading shortcut " + item
1560 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001561 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001562 + ")");
1563 return false;
1564 }
1565
Dan Sandler295ae182013-12-10 16:05:47 -05001566 if (hotseatItems != null) {
1567 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 Log.e(TAG, "Error loading shortcut into hotseat " + item
1569 + " into position (" + item.screenId + ":" + item.cellX + ","
1570 + item.cellY + ") occupied by "
1571 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1572 [(int) item.screenId][0]);
1573 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001574 } else {
1575 hotseatItems[(int) item.screenId][0] = item;
1576 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001578 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001579 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 items[(int) item.screenId][0] = item;
1581 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001582 return true;
1583 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001584 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1585 if (!workspaceScreens.contains((Long) item.screenId)) {
1586 // The item has an invalid screen id.
1587 return false;
1588 }
1589 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001590 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001591 return true;
1592 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001593
Adam Cohendcd297f2013-06-18 13:13:40 -07001594 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001595 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001596 occupied.put(item.screenId, items);
1597 }
1598
Dan Sandler295ae182013-12-10 16:05:47 -05001599 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001600 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1601 item.cellX < 0 || item.cellY < 0 ||
1602 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1603 Log.e(TAG, "Error loading shortcut " + item
1604 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1605 + item.cellX + "," + item.cellY
1606 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1607 return false;
1608 }
1609
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001610 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001611 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1612 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001613 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001614 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001615 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001616 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001617 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001618 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001619 return false;
1620 }
1621 }
1622 }
1623 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1624 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001625 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
1627 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001628
Joe Onorato36115782010-06-17 13:28:48 -04001629 return true;
1630 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631
Winson Chungba9c37f2013-08-30 14:11:37 -07001632 /** Clears all the sBg data structures */
1633 private void clearSBgDataStructures() {
1634 synchronized (sBgLock) {
1635 sBgWorkspaceItems.clear();
1636 sBgAppWidgets.clear();
1637 sBgFolders.clear();
1638 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001639 sBgWorkspaceScreens.clear();
1640 }
1641 }
1642
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001643 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001644 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645
Joe Onorato36115782010-06-17 13:28:48 -04001646 final Context context = mContext;
1647 final ContentResolver contentResolver = context.getContentResolver();
1648 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001649 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001650 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001651 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001652
Winson Chung892c74d2013-08-22 16:15:50 -07001653 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001654 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001655 int countX = profile.numColumns;
1656 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001657
Sunny Goyalf076eae2016-01-11 12:25:10 -08001658 if (GridSizeMigrationTask.ENABLED &&
1659 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1660 // Migration failed. Clear workspace.
1661 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001662 }
1663
Dan Sandlerd5024042014-01-09 15:01:33 -05001664 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001665 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001666 LauncherSettings.Settings.call(contentResolver,
1667 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001668 }
1669
1670 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1671 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001672 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001673 LauncherSettings.Settings.call(contentResolver,
1674 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001675 } else {
1676 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001677 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001678 LauncherSettings.Settings.call(contentResolver,
1679 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001680 }
Adam Cohene25af792013-06-06 23:08:25 -07001681
Winson Chung2abf94d2012-07-18 18:16:38 -07001682 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001683 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001684 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001685 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001686 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001687
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001688 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1689 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001690 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001691 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001692 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001693
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 // +1 for the hotseat (it can be larger than the workspace)
1695 // Load workspace in reverse order to ensure that latest items are loaded first (and
1696 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001697 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001698 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001699
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 try {
1701 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1702 final int intentIndex = c.getColumnIndexOrThrow
1703 (LauncherSettings.Favorites.INTENT);
1704 final int titleIndex = c.getColumnIndexOrThrow
1705 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 final int containerIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.CONTAINER);
1708 final int itemTypeIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.ITEM_TYPE);
1710 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001712 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1713 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 final int screenIndex = c.getColumnIndexOrThrow(
1715 LauncherSettings.Favorites.SCREEN);
1716 final int cellXIndex = c.getColumnIndexOrThrow
1717 (LauncherSettings.Favorites.CELLX);
1718 final int cellYIndex = c.getColumnIndexOrThrow
1719 (LauncherSettings.Favorites.CELLY);
1720 final int spanXIndex = c.getColumnIndexOrThrow
1721 (LauncherSettings.Favorites.SPANX);
1722 final int spanYIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001724 final int rankIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001726 final int restoredIndex = c.getColumnIndexOrThrow(
1727 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001728 final int profileIdIndex = c.getColumnIndexOrThrow(
1729 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001730 final int optionsIndex = c.getColumnIndexOrThrow(
1731 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001732 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733
Sunny Goyal7f834d22015-04-21 10:10:23 -07001734 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001735 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001736 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001737 long serialNo = mUserManager.getSerialNumberForUser(user);
1738 allUsers.put(serialNo, user);
1739 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001740 }
1741
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001742 ShortcutInfo info;
1743 String intentDescription;
1744 LauncherAppWidgetInfo appWidgetInfo;
1745 int container;
1746 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001747 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001749 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 while (!mStopped && c.moveToNext()) {
1752 try {
1753 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001754 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001755 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001756 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758 switch (itemType) {
1759 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1760 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001761 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001763 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001764 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001765 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001766 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001767 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001768 if (user == null) {
1769 // User has been deleted remove the item.
1770 itemsToRemove.add(id);
1771 continue;
1772 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 try {
1774 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001775 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001776 if (cn != null && cn.getPackageName() != null) {
1777 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1778 cn.getPackageName(), user);
1779 boolean validComponent = validPkg &&
1780 launcherApps.isActivityEnabledForProfile(cn, user);
1781
1782 if (validComponent) {
1783 if (restored) {
1784 // no special handling necessary for this item
1785 restoredRows.add(id);
1786 restored = false;
1787 }
Kenny Guy44cba692016-01-21 19:50:02 +00001788 boolean isSuspended = launcherApps.isPackageSuspendedForProfile(
1789 cn.getPackageName(), user);
1790 if (isSuspended) {
1791 disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1792 }
Kenny Guyff05f432016-01-22 17:48:29 +00001793 if (quietMode.get(serialNumber)) {
1794 disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER;
1795 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001796 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001797 intent = null;
1798 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1799 // We allow auto install apps to have their intent
1800 // updated after an install.
1801 intent = manager.getLaunchIntentForPackage(
1802 cn.getPackageName());
1803 if (intent != null) {
1804 ContentValues values = new ContentValues();
1805 values.put(LauncherSettings.Favorites.INTENT,
1806 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001807 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001808 }
1809 }
1810
1811 if (intent == null) {
1812 // The app is installed but the component is no
1813 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001814 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001815 itemsToRemove.add(id);
1816 continue;
1817 } else {
1818 // no special handling necessary for this item
1819 restoredRows.add(id);
1820 restored = false;
1821 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001822 } else if (restored) {
1823 // Package is not yet available but might be
1824 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001825 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001826
1827 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1828 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001829 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001830 // App restore has started. Update the flag
1831 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1832 ContentValues values = new ContentValues();
1833 values.put(LauncherSettings.Favorites.RESTORED,
1834 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001835 updateItem(id, values);
1836 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1837 // This is a common app. Try to replace this.
1838 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1839 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1840 if (parser.findDefaultApp()) {
1841 // Default app found. Replace it.
1842 intent = parser.parsedIntent;
1843 cn = intent.getComponent();
1844 ContentValues values = parser.parsedValues;
1845 values.put(LauncherSettings.Favorites.RESTORED, 0);
1846 updateItem(id, values);
1847 restored = false;
1848 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001849
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001850 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001851 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001852 itemsToRemove.add(id);
1853 continue;
1854 }
Sunny Goyal94485362014-09-18 16:13:58 -07001855 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001856 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001857 itemsToRemove.add(id);
1858 continue;
1859 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001860 } else if (launcherApps.isAppEnabled(
1861 manager, cn.getPackageName(),
1862 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1863 // Package is present but not available.
1864 allowMissingTarget = true;
1865 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1866 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001867 // SdCard is not ready yet. Package might get available,
1868 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001869 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001870 HashSet<String> pkgs = sPendingPackages.get(user);
1871 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001872 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001873 sPendingPackages.put(user, pkgs);
1874 }
1875 pkgs.add(cn.getPackageName());
1876 allowMissingTarget = true;
1877 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001878
1879 } else {
1880 // Do not wait for external media load anymore.
1881 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001882 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001883 itemsToRemove.add(id);
1884 continue;
Winson Chungee055712013-07-30 14:46:24 -07001885 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001886 } else if (cn == null) {
1887 // For shortcuts with no component, keep them as they are
1888 restoredRows.add(id);
1889 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001892 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001893 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 continue;
1895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001896
Sunny Goyal34b65272015-03-11 16:56:52 -07001897 boolean useLowResIcon = container >= 0 &&
1898 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1899
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001900 if (itemReplaced) {
1901 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001902 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001903 cursorIconInfo.iconIndex, titleIndex,
1904 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001905 } else {
1906 // Don't replace items for other profiles.
1907 itemsToRemove.add(id);
1908 continue;
1909 }
1910 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001911 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001912 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001913 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001914 intent = getRestoredItemIntent(c, context, intent);
1915 } else {
1916 // Don't restore items for other profiles.
1917 itemsToRemove.add(id);
1918 continue;
1919 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001920 } else if (itemType ==
1921 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001922 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001923 cursorIconInfo.iconIndex, titleIndex,
1924 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001926 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001927
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 // App shortcuts that used to be automatically added to Launcher
1929 // didn't always have the correct intent flags set, so do that
1930 // here
1931 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001932 intent.getCategories() != null &&
1933 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001934 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001935 intent.addFlags(
1936 Intent.FLAG_ACTIVITY_NEW_TASK |
1937 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1938 }
Michael Jurka96879562012-03-22 05:54:33 -07001939 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001940
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001941 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001942 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001943 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001945 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001946 info.cellX = c.getInt(cellXIndex);
1947 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001948 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001949 info.spanX = 1;
1950 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001951 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001952 if (info.promisedIntent != null) {
1953 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1954 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001955 info.isDisabled = disabledState;
1956 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1957 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1958 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001959
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001960 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001961 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001962 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 break;
1964 }
1965
Sunny Goyal756adbc2015-04-16 15:20:51 -07001966 if (restored) {
1967 ComponentName cn = info.getTargetComponent();
1968 if (cn != null) {
1969 Integer progress = installingPkgs.get(cn.getPackageName());
1970 if (progress != null) {
1971 info.setInstallProgress(progress);
1972 } else {
1973 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1974 }
1975 }
1976 }
1977
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 switch (container) {
1979 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1980 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1981 sBgWorkspaceItems.add(info);
1982 break;
1983 default:
1984 // Item is in a user folder
1985 FolderInfo folderInfo =
1986 findOrMakeFolder(sBgFolders, container);
1987 folderInfo.add(info);
1988 break;
1989 }
1990 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001991 } else {
1992 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 }
1994 break;
1995
1996 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1997 id = c.getLong(idIndex);
1998 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1999
Sunny Goyala508e4f2015-05-21 09:33:57 -07002000 // Do not trim the folder label, as is was set by the user.
2001 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002004 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 folderInfo.cellX = c.getInt(cellXIndex);
2006 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002007 folderInfo.spanX = 1;
2008 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002009 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002010
Daniel Sandler8802e962010-05-26 16:28:16 -04002011 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002012 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002013 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002014 break;
2015 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002016
Joe Onorato9c1289c2009-08-17 11:03:03 -04002017 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2019 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2020 sBgWorkspaceItems.add(folderInfo);
2021 break;
Joe Onorato36115782010-06-17 13:28:48 -04002022 }
Joe Onorato17a89222011-02-08 17:26:11 -08002023
Chris Wrenf4d08112014-01-16 18:13:56 -05002024 if (restored) {
2025 // no special handling required for restored folders
2026 restoredRows.add(id);
2027 }
2028
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2030 sBgFolders.put(folderInfo.id, folderInfo);
2031 break;
2032
2033 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002034 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002036 boolean customWidget = itemType ==
2037 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2038
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002040 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002041 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002042 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002043 user = allUsers.get(serialNumber);
2044 if (user == null) {
2045 itemsToRemove.add(id);
2046 continue;
2047 }
2048
Sunny Goyalff572272014-07-23 13:58:07 -07002049 final ComponentName component =
2050 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002051
Sunny Goyal651077b2014-06-30 14:15:31 -07002052 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002053 final boolean isIdValid = (restoreStatus &
2054 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002055 final boolean wasProviderReady = (restoreStatus &
2056 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002057
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002058 if (widgetProvidersMap == null) {
2059 widgetProvidersMap = AppWidgetManagerCompat
2060 .getInstance(mContext).getAllProvidersMap();
2061 }
2062 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2063 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002064 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002065 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002066
2067 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002068 if (!isSafeMode && !customWidget &&
2069 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002070 addDumpLog("Deleting widget that isn't installed anymore: "
2071 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 itemsToRemove.add(id);
2073 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002074 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002075 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2076 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002077
Sunny Goyal53f96722015-07-13 19:54:53 -07002078 // The provider is available. So the widget is either
2079 // available or not available. We do not need to track
2080 // any future restore updates.
2081 int status = restoreStatus &
2082 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002083 if (!wasProviderReady) {
2084 // If provider was not previously ready, update the
2085 // status and UI flag.
2086
2087 // Id would be valid only if the widget restore broadcast was received.
2088 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002089 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002090 } else {
2091 status &= ~LauncherAppWidgetInfo
2092 .FLAG_PROVIDER_NOT_READY;
2093 }
2094 }
2095 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002096 } else {
2097 Log.v(TAG, "Widget restore pending id=" + id
2098 + " appWidgetId=" + appWidgetId
2099 + " status =" + restoreStatus);
2100 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002101 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002102 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002103 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002104
2105 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2106 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002107 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002108 // App restore has started. Update the flag
2109 appWidgetInfo.restoreStatus |=
2110 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002111 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002112 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002113 itemsToRemove.add(id);
2114 continue;
2115 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002116
2117 appWidgetInfo.installProgress =
2118 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002119 }
Sunny Goyalff572272014-07-23 13:58:07 -07002120
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002122 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002123 appWidgetInfo.cellX = c.getInt(cellXIndex);
2124 appWidgetInfo.cellY = c.getInt(cellYIndex);
2125 appWidgetInfo.spanX = c.getInt(spanXIndex);
2126 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002127 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002128
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2130 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2131 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002132 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2133 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 continue;
2135 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002137 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002139 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002140 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002141 break;
2142 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002143
Adam Cohen59400422014-03-05 18:07:04 -08002144 if (!customWidget) {
2145 String providerName =
2146 appWidgetInfo.providerName.flattenToString();
2147 if (!providerName.equals(savedProvider) ||
2148 (appWidgetInfo.restoreStatus != restoreStatus)) {
2149 ContentValues values = new ContentValues();
2150 values.put(
2151 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2152 providerName);
2153 values.put(LauncherSettings.Favorites.RESTORED,
2154 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002155 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002156 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002157 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2159 sBgAppWidgets.add(appWidgetInfo);
2160 }
Joe Onorato36115782010-06-17 13:28:48 -04002161 break;
2162 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002164 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002165 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 }
2167 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002168 if (c != null) {
2169 c.close();
2170 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 }
2172
Winson Chungba9c37f2013-08-30 14:11:37 -07002173 // Break early if we've stopped loading
2174 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002175 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002176 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002177 }
2178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002181 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2182 Utilities.createDbSelectionQuery(
2183 LauncherSettings.Favorites._ID, itemsToRemove), null);
2184 if (DEBUG_LOADERS) {
2185 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2186 LauncherSettings.Favorites._ID, itemsToRemove));
2187 }
2188
2189 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002190 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2191 .call(contentResolver,
2192 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2193 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2194 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002195 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2196 sBgFolders.remove(folderId);
2197 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002198 }
2199 }
2200
Sunny Goyal317698b2015-07-29 11:45:41 -07002201 // Sort all the folder items and make sure the first 3 items are high resolution.
2202 for (FolderInfo folder : sBgFolders) {
2203 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2204 int pos = 0;
2205 for (ShortcutInfo info : folder.contents) {
2206 if (info.usingLowResIcon) {
2207 info.updateIcon(mIconCache, false);
2208 }
2209 pos ++;
2210 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2211 break;
2212 }
2213 }
2214 }
2215
Chris Wrenf4d08112014-01-16 18:13:56 -05002216 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002217 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002218 ContentValues values = new ContentValues();
2219 values.put(LauncherSettings.Favorites.RESTORED, 0);
2220 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2221 Utilities.createDbSelectionQuery(
2222 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002223 }
2224
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002225 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2226 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002227 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002228 null, sWorker);
2229 }
2230
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002231 // Remove any empty screens
2232 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002233 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002234 long screenId = item.screenId;
2235 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2236 unusedScreens.contains(screenId)) {
2237 unusedScreens.remove(screenId);
2238 }
2239 }
2240
2241 // If there are any empty screens remove them, and update.
2242 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002243 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002245 }
2246
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 if (DEBUG_LOADERS) {
2248 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2249 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002251 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002253
Sunny Goyale2df0622015-04-24 11:27:00 -07002254 for (int i = 0; i < nScreens; i++) {
2255 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002256 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257 line += " | ";
2258 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002259 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002260 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002261 if (x < screen.length && y < screen[x].length) {
2262 line += (screen[x][y] != null) ? "#" : ".";
2263 } else {
2264 line += "!";
2265 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 }
Joe Onorato36115782010-06-17 13:28:48 -04002267 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002269 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002270 }
Joe Onorato36115782010-06-17 13:28:48 -04002271 }
Adam Cohene25af792013-06-06 23:08:25 -07002272 }
2273
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002274 /**
2275 * Partially updates the item without any notification. Must be called on the worker thread.
2276 */
2277 private void updateItem(long itemId, ContentValues update) {
2278 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002279 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002280 update,
2281 BaseColumns._ID + "= ?",
2282 new String[]{Long.toString(itemId)});
2283 }
2284
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 /** Filters the set of items who are directly or indirectly (via another container) on the
2286 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002287 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288 ArrayList<ItemInfo> allWorkspaceItems,
2289 ArrayList<ItemInfo> currentScreenItems,
2290 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002291 // Purge any null ItemInfos
2292 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2293 while (iter.hasNext()) {
2294 ItemInfo i = iter.next();
2295 if (i == null) {
2296 iter.remove();
2297 }
2298 }
2299
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 // Order the set of items by their containers first, this allows use to walk through the
2301 // list sequentially, build up a list of containers that are in the specified screen,
2302 // as well as all items in those containers.
2303 Set<Long> itemsOnScreen = new HashSet<Long>();
2304 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2305 @Override
2306 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002307 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 }
2309 });
2310 for (ItemInfo info : allWorkspaceItems) {
2311 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002312 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 currentScreenItems.add(info);
2314 itemsOnScreen.add(info.id);
2315 } else {
2316 otherScreenItems.add(info);
2317 }
2318 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2319 currentScreenItems.add(info);
2320 itemsOnScreen.add(info.id);
2321 } else {
2322 if (itemsOnScreen.contains(info.container)) {
2323 currentScreenItems.add(info);
2324 itemsOnScreen.add(info.id);
2325 } else {
2326 otherScreenItems.add(info);
2327 }
2328 }
2329 }
2330 }
2331
2332 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002333 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 ArrayList<LauncherAppWidgetInfo> appWidgets,
2335 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2336 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337
2338 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002339 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002341 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002342 currentScreenWidgets.add(widget);
2343 } else {
2344 otherScreenWidgets.add(widget);
2345 }
2346 }
2347 }
2348
2349 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002350 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002351 LongArrayMap<ItemInfo> itemsIdMap,
2352 LongArrayMap<FolderInfo> folders,
2353 LongArrayMap<FolderInfo> currentScreenFolders,
2354 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355
Sunny Goyale2df0622015-04-24 11:27:00 -07002356 int total = folders.size();
2357 for (int i = 0; i < total; i++) {
2358 long id = folders.keyAt(i);
2359 FolderInfo folder = folders.valueAt(i);
2360
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002362 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002363 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002364 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002365 currentScreenFolders.put(id, folder);
2366 } else {
2367 otherScreenFolders.put(id, folder);
2368 }
2369 }
2370 }
2371
2372 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2373 * right) */
2374 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002375 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002376 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002377 final int screenCols = profile.numColumns;
2378 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002380 @Override
2381 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002382 if (lhs.container == rhs.container) {
2383 // Within containers, order by their spatial position in that container
2384 switch ((int) lhs.container) {
2385 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2386 long lr = (lhs.screenId * screenCellCount +
2387 lhs.cellY * screenCols + lhs.cellX);
2388 long rr = (rhs.screenId * screenCellCount +
2389 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002390 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002391 }
2392 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2393 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002394 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002395 }
2396 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002397 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002398 throw new RuntimeException("Unexpected container type when " +
2399 "sorting workspace items.");
2400 }
2401 return 0;
2402 }
2403 } else {
2404 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002405 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002406 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002407 }
2408 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002410
Adam Cohendcd297f2013-06-18 13:13:40 -07002411 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2412 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002413 final Runnable r = new Runnable() {
2414 @Override
2415 public void run() {
2416 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2417 if (callbacks != null) {
2418 callbacks.bindScreens(orderedScreens);
2419 }
2420 }
2421 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002422 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002423 }
2424
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2426 final ArrayList<ItemInfo> workspaceItems,
2427 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002428 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002429 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430
2431 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002432 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002434 final int start = i;
2435 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 final Runnable r = new Runnable() {
2437 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002438 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002439 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002440 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002441 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2442 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002443 }
2444 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002446 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002447 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448
2449 // Bind the folders
2450 if (!folders.isEmpty()) {
2451 final Runnable r = new Runnable() {
2452 public void run() {
2453 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2454 if (callbacks != null) {
2455 callbacks.bindFolders(folders);
2456 }
2457 }
2458 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002459 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 }
2461
2462 // Bind the widgets, one at a time
2463 N = appWidgets.size();
2464 for (int i = 0; i < N; i++) {
2465 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2466 final Runnable r = new Runnable() {
2467 public void run() {
2468 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2469 if (callbacks != null) {
2470 callbacks.bindAppWidget(widget);
2471 }
2472 }
2473 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002474 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475 }
2476 }
2477
2478 /**
2479 * Binds all loaded data to actual views on the main thread.
2480 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002481 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482 final long t = SystemClock.uptimeMillis();
2483 Runnable r;
2484
2485 // Don't use these two variables in any of the callback runnables.
2486 // Otherwise we hold a reference to them.
2487 final Callbacks oldCallbacks = mCallbacks.get();
2488 if (oldCallbacks == null) {
2489 // This launcher has exited and nobody bothered to tell us. Just bail.
2490 Log.w(TAG, "LoaderTask running with no launcher");
2491 return;
2492 }
2493
Winson Chung9b9fb962013-11-15 15:39:34 -08002494 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2496 ArrayList<LauncherAppWidgetInfo> appWidgets =
2497 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002498 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002499
2500 final LongArrayMap<FolderInfo> folders;
2501 final LongArrayMap<ItemInfo> itemsIdMap;
2502
Winson Chung2abf94d2012-07-18 18:16:38 -07002503 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 workspaceItems.addAll(sBgWorkspaceItems);
2505 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002506 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002507
2508 folders = sBgFolders.clone();
2509 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 }
2511
Derek Prothro7aff3992013-12-10 14:00:37 -05002512 final boolean isLoadingSynchronously =
2513 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002514 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002515 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002516 if (currScreen >= orderedScreenIds.size()) {
2517 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002518 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002519 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002520 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002521 final long currentScreenId = currentScreen < 0
2522 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002523
2524 // Load all the items that are on the current page first (and in the process, unbind
2525 // all the existing workspace items before we call startBinding() below.
2526 unbindWorkspaceItemsOnMainThread();
2527
2528 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2530 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2531 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2532 new ArrayList<LauncherAppWidgetInfo>();
2533 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2534 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002535 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2536 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537
Winson Chung9b9fb962013-11-15 15:39:34 -08002538 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002539 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002540 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002541 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002542 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 otherFolders);
2544 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2545 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2546
2547 // Tell the workspace that we're about to start binding items
2548 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002549 public void run() {
2550 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2551 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002552 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002553 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002554 }
2555 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002557 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558
Adam Cohendcd297f2013-06-18 13:13:40 -07002559 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2560
Sunny Goyal527c7d32015-08-28 15:19:36 -07002561 Executor mainExecutor = new DeferredMainThreadExecutor();
2562 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002564 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565
Sunny Goyal527c7d32015-08-28 15:19:36 -07002566 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2567 // remaining screens after first onDraw is called. This ensures that the first screen
2568 // is immediately visible (eg. during rotation)
2569 // In case of !isLoadingSynchronously, bind all pages one after other.
2570 final Executor deferredExecutor = isLoadingSynchronously ?
2571 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2572
2573 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2574 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575
2576 // Tell the workspace that we're done binding items
2577 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002578 public void run() {
2579 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2580 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002581 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002582 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583
Sunny Goyal639e9062015-08-19 19:17:06 -07002584 mIsLoadingAndBindingWorkspace = false;
2585
2586 // Run all the bind complete runnables after workspace is bound.
2587 if (!mBindCompleteRunnables.isEmpty()) {
2588 synchronized (mBindCompleteRunnables) {
2589 for (final Runnable r : mBindCompleteRunnables) {
2590 runOnWorkerThread(r);
2591 }
2592 mBindCompleteRunnables.clear();
2593 }
2594 }
2595
Winson Chung98e030b2012-05-07 16:01:11 -07002596 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002597 if (DEBUG_LOADERS) {
2598 Log.d(TAG, "bound workspace in "
2599 + (SystemClock.uptimeMillis()-t) + "ms");
2600 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002601
Joe Onorato36115782010-06-17 13:28:48 -04002602 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002603 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002604 deferredExecutor.execute(r);
2605
Winson Chung4a2afa32012-07-19 14:53:05 -07002606 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002607 r = new Runnable() {
2608 public void run() {
2609 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2610 if (callbacks != null) {
2611 // We are loading synchronously, which means, some of the pages will be
2612 // bound after first draw. Inform the callbacks that page binding is
2613 // not complete, and schedule the remaining pages.
2614 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2615 callbacks.onPageBoundSynchronously(currentScreen);
2616 }
2617 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2618 }
2619 }
2620 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002621 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002622 }
Joe Onorato36115782010-06-17 13:28:48 -04002623 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002624
Joe Onorato36115782010-06-17 13:28:48 -04002625 private void loadAndBindAllApps() {
2626 if (DEBUG_LOADERS) {
2627 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2628 }
2629 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002630 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002631 synchronized (LoaderTask.this) {
2632 if (mStopped) {
2633 return;
2634 }
2635 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002636 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002637 synchronized (LoaderTask.this) {
2638 if (mStopped) {
2639 return;
2640 }
2641 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002642 }
Joe Onorato36115782010-06-17 13:28:48 -04002643 } else {
2644 onlyBindAllApps();
2645 }
2646 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002647
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002648 private void updateIconCache() {
2649 // Ignore packages which have a promise icon.
2650 HashSet<String> packagesToIgnore = new HashSet<>();
2651 synchronized (sBgLock) {
2652 for (ItemInfo info : sBgItemsIdMap) {
2653 if (info instanceof ShortcutInfo) {
2654 ShortcutInfo si = (ShortcutInfo) info;
2655 if (si.isPromise() && si.getTargetComponent() != null) {
2656 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2657 }
2658 } else if (info instanceof LauncherAppWidgetInfo) {
2659 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2660 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2661 packagesToIgnore.add(lawi.providerName.getPackageName());
2662 }
2663 }
2664 }
2665 }
2666 mIconCache.updateDbIcons(packagesToIgnore);
2667 }
2668
Joe Onorato36115782010-06-17 13:28:48 -04002669 private void onlyBindAllApps() {
2670 final Callbacks oldCallbacks = mCallbacks.get();
2671 if (oldCallbacks == null) {
2672 // This launcher has exited and nobody bothered to tell us. Just bail.
2673 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2674 return;
2675 }
2676
2677 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002678 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002679 final ArrayList<AppInfo> list
2680 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002681 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002682 public void run() {
2683 final long t = SystemClock.uptimeMillis();
2684 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2685 if (callbacks != null) {
2686 callbacks.bindAllApplications(list);
2687 }
2688 if (DEBUG_LOADERS) {
2689 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002690 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002691 }
2692 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002693 };
2694 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002695 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002696 r.run();
2697 } else {
2698 mHandler.post(r);
2699 }
Joe Onorato36115782010-06-17 13:28:48 -04002700 }
2701
Winson Chung64359a52013-07-08 17:17:08 -07002702 private void loadAllApps() {
2703 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002704
Joe Onorato36115782010-06-17 13:28:48 -04002705 final Callbacks oldCallbacks = mCallbacks.get();
2706 if (oldCallbacks == null) {
2707 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002708 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002709 return;
2710 }
2711
Kenny Guyed131872014-04-30 03:02:21 +01002712 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2713
Winson Chung64359a52013-07-08 17:17:08 -07002714 // Clear the list of apps
2715 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002716 for (UserHandleCompat user : profiles) {
2717 // Query for the set of apps
2718 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002719 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002720 if (DEBUG_LOADERS) {
2721 Log.d(TAG, "getActivityList took "
2722 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2723 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2724 }
2725 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002726 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002727 if (apps == null || apps.isEmpty()) {
2728 return;
2729 }
Kenny Guyff05f432016-01-22 17:48:29 +00002730 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002731 // Create the ApplicationInfos
2732 for (int i = 0; i < apps.size(); i++) {
2733 LauncherActivityInfoCompat app = apps.get(i);
2734 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002735 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002736 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002737
Sunny Goyal756a28a2015-04-23 17:07:55 -07002738 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2739 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002740 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002741
2742 @Override
2743 public void run() {
2744 heuristic.processUserApps(apps);
2745 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002746 };
2747 runOnMainThread(new Runnable() {
2748
2749 @Override
2750 public void run() {
2751 // Check isLoadingWorkspace on the UI thread, as it is updated on
2752 // the UI thread.
2753 if (mIsLoadingAndBindingWorkspace) {
2754 synchronized (mBindCompleteRunnables) {
2755 mBindCompleteRunnables.add(r);
2756 }
2757 } else {
2758 runOnWorkerThread(r);
2759 }
2760 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002761 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002762 }
Winson Chung64359a52013-07-08 17:17:08 -07002763 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002764 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002765 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2766 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002767
2768 // Post callback on main thread
2769 mHandler.post(new Runnable() {
2770 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002771
Winson Chung64359a52013-07-08 17:17:08 -07002772 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002773 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002774 if (callbacks != null) {
2775 callbacks.bindAllApplications(added);
2776 if (DEBUG_LOADERS) {
2777 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002778 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002779 }
2780 } else {
2781 Log.i(TAG, "not binding apps: no Launcher activity");
2782 }
2783 }
2784 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002785 // Cleanup any data stored for a deleted user.
2786 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002787 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002788 Log.d(TAG, "Icons processed in "
2789 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002790 }
2791 }
2792
2793 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002794 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002795 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002796 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2797 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2798 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2799 }
Joe Onorato36115782010-06-17 13:28:48 -04002800 }
2801 }
2802
Sunny Goyal75b0f552015-05-20 21:57:06 -07002803 /**
2804 * Called when the icons for packages have been updated in the icon cache.
2805 */
2806 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2807 final Callbacks callbacks = getCallback();
2808 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2809 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2810
2811 // If any package icon has changed (app was updated while launcher was dead),
2812 // update the corresponding shortcuts.
2813 synchronized (sBgLock) {
2814 for (ItemInfo info : sBgItemsIdMap) {
2815 if (info instanceof ShortcutInfo && user.equals(info.user)
2816 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2817 ShortcutInfo si = (ShortcutInfo) info;
2818 ComponentName cn = si.getTargetComponent();
2819 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2820 si.updateIcon(mIconCache);
2821 updatedShortcuts.add(si);
2822 }
2823 }
2824 }
2825 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2826 }
2827
2828 if (!updatedShortcuts.isEmpty()) {
2829 final UserHandleCompat userFinal = user;
2830 mHandler.post(new Runnable() {
2831
2832 public void run() {
2833 Callbacks cb = getCallback();
2834 if (cb != null && callbacks == cb) {
2835 cb.bindShortcutsChanged(updatedShortcuts,
2836 new ArrayList<ShortcutInfo>(), userFinal);
2837 }
2838 }
2839 });
2840 }
2841
2842 if (!updatedApps.isEmpty()) {
2843 mHandler.post(new Runnable() {
2844
2845 public void run() {
2846 Callbacks cb = getCallback();
2847 if (cb != null && callbacks == cb) {
2848 cb.bindAppsUpdated(updatedApps);
2849 }
2850 }
2851 });
2852 }
2853 }
2854
Joe Onorato36115782010-06-17 13:28:48 -04002855 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002856 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002857 }
2858
Adam Cohen091440a2015-03-18 14:16:05 -07002859 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002860
2861 @Override
2862 public void onReceive(Context context, Intent intent) {
2863 synchronized (sBgLock) {
2864 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2865 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002866 final PackageManager manager = context.getPackageManager();
2867 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2868 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002869 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2870 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002871 packagesRemoved.clear();
2872 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002873 for (String pkg : entry.getValue()) {
2874 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002875 boolean packageOnSdcard = launcherApps.isAppEnabled(
2876 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2877 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002878 packagesUnavailable.add(pkg);
2879 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002880 packagesRemoved.add(pkg);
2881 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002882 }
2883 }
2884 if (!packagesRemoved.isEmpty()) {
2885 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2886 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2887 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002888 if (!packagesUnavailable.isEmpty()) {
2889 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2890 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2891 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002892 }
Sunny Goyal34942622014-08-29 17:20:55 -07002893 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002894 }
2895 }
2896 }
2897
Joe Onorato36115782010-06-17 13:28:48 -04002898 private class PackageUpdatedTask implements Runnable {
2899 int mOp;
2900 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002901 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002902
2903 public static final int OP_NONE = 0;
2904 public static final int OP_ADD = 1;
2905 public static final int OP_UPDATE = 2;
2906 public static final int OP_REMOVE = 3; // uninstlled
2907 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002908 public static final int OP_SUSPEND = 5; // package suspended
2909 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002910 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002911
Kenny Guyed131872014-04-30 03:02:21 +01002912 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002913 mOp = op;
2914 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002915 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002916 }
2917
2918 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002919 if (!mHasLoaderCompletedOnce) {
2920 // Loader has not yet run.
2921 return;
2922 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002923 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002924
2925 final String[] packages = mPackages;
2926 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002927 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002928 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002929 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002930 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002931 for (int i=0; i<N; i++) {
2932 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002933 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002934 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002935 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002936
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002937 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2938 if (heuristic != null) {
2939 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002940 }
Joe Onorato36115782010-06-17 13:28:48 -04002941 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002942 }
Joe Onorato36115782010-06-17 13:28:48 -04002943 case OP_UPDATE:
2944 for (int i=0; i<N; i++) {
2945 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002946 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002947 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002948 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002949 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002950 // Since package was just updated, the target must be available now.
2951 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002952 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 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002970 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002971 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002972 case OP_SUSPEND:
2973 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002974 flagOp = mOp == OP_SUSPEND ?
2975 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2976 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002977 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002978 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2979 break;
2980 case OP_USER_AVAILABILITY_CHANGE:
2981 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2982 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2983 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2984 // We want to update all packages for this user.
2985 pkgFilter = StringFilter.matchesAll();
2986 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002987 break;
Joe Onorato36115782010-06-17 13:28:48 -04002988 }
2989
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002990 ArrayList<AppInfo> added = null;
2991 ArrayList<AppInfo> modified = null;
2992 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002993
Adam Cohen487f7dd2012-06-28 18:12:10 -07002994 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002995 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002996 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002997 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002998 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002999 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003000 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003001 }
Winson Chung5d55f332012-07-16 20:45:03 -07003002 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003003 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003004 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003005 }
3006
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003007 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003008
Joe Onorato36115782010-06-17 13:28:48 -04003009 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003010 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003011 for (AppInfo ai : added) {
3012 addedOrUpdatedApps.put(ai.componentName, ai);
3013 }
Joe Onorato36115782010-06-17 13:28:48 -04003014 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003015
Joe Onorato36115782010-06-17 13:28:48 -04003016 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003017 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003018 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003019 for (AppInfo ai : modified) {
3020 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003021 }
3022
Joe Onorato36115782010-06-17 13:28:48 -04003023 mHandler.post(new Runnable() {
3024 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003025 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003026 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003027 callbacks.bindAppsUpdated(modifiedFinal);
3028 }
3029 }
3030 });
3031 }
Winson Chung83892cc2013-05-01 16:53:33 -07003032
Sunny Goyal4390ace2014-10-13 11:33:11 -07003033 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003034 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003035 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3036 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3037 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3038
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003039 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003040 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003041 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3042 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003043 boolean infoUpdated = false;
3044 boolean shortcutUpdated = false;
3045
3046 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003047 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003048 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003049 Bitmap icon = Utilities.createIconBitmap(
3050 si.iconResource.packageName,
3051 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003052 if (icon != null) {
3053 si.setIcon(icon);
3054 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003055 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003056 }
3057 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003058
3059 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003060 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3062
3063 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003064 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3065 // Auto install icon
3066 PackageManager pm = context.getPackageManager();
3067 ResolveInfo matched = pm.resolveActivity(
3068 new Intent(Intent.ACTION_MAIN)
3069 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3070 PackageManager.MATCH_DEFAULT_ONLY);
3071 if (matched == null) {
3072 // Try to find the best match activity.
3073 Intent intent = pm.getLaunchIntentForPackage(
3074 cn.getPackageName());
3075 if (intent != null) {
3076 cn = intent.getComponent();
3077 appInfo = addedOrUpdatedApps.get(cn);
3078 }
3079
3080 if ((intent == null) || (appInfo == null)) {
3081 removedShortcuts.add(si);
3082 continue;
3083 }
3084 si.promisedIntent = intent;
3085 }
3086 }
3087
3088 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003089 if (appInfo != null) {
3090 si.flags = appInfo.flags;
3091 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003092
Sunny Goyal756adbc2015-04-16 15:20:51 -07003093 si.intent = si.promisedIntent;
3094 si.promisedIntent = null;
3095 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003096 infoUpdated = true;
3097 si.updateIcon(mIconCache);
3098 }
3099
3100 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3101 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3102 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003103 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003104 si.contentDescription = appInfo.contentDescription;
3105 infoUpdated = true;
3106 }
3107
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003108 int oldDisabledFlags = si.isDisabled;
3109 si.isDisabled = flagOp.apply(si.isDisabled);
3110 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003111 shortcutUpdated = true;
3112 }
3113 }
3114
3115 if (infoUpdated || shortcutUpdated) {
3116 updatedShortcuts.add(si);
3117 }
3118 if (infoUpdated) {
3119 updateItemInDatabase(context, si);
3120 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003121 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3123 if (mUser.equals(widgetInfo.user)
3124 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003125 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003126 widgetInfo.restoreStatus &=
3127 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3128 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003129
3130 // adding this flag ensures that launcher shows 'click to setup'
3131 // if the widget has a config activity. In case there is no config
3132 // activity, it will be marked as 'restored' during bind.
3133 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3134
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 widgets.add(widgetInfo);
3136 updateItemInDatabase(context, widgetInfo);
3137 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003138 }
3139 }
3140 }
3141
Sunny Goyal4390ace2014-10-13 11:33:11 -07003142 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003143 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003144 mHandler.post(new Runnable() {
3145
3146 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003147 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003148 if (callbacks == cb && cb != null) {
3149 callbacks.bindShortcutsChanged(
3150 updatedShortcuts, removedShortcuts, mUser);
3151 }
3152 }
3153 });
3154 if (!removedShortcuts.isEmpty()) {
3155 deleteItemsFromDatabase(context, removedShortcuts);
3156 }
3157 }
3158 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003159 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003160 mHandler.post(new Runnable() {
3161 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003162 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003163 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003164 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003165 }
3166 }
3167 });
3168 }
3169 }
3170
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003171 final HashSet<String> removedPackages = new HashSet<>();
3172 final HashSet<ComponentName> removedComponents = new HashSet<>();
3173 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003174 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003175 Collections.addAll(removedPackages, packages);
3176
3177 // No need to update the removedComponents as
3178 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003179 } else if (mOp == OP_UPDATE) {
3180 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003181 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003182 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003183 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003184 }
3185 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003186
3187 // Update removedComponents as some components can get removed during package update
3188 for (AppInfo info : removedApps) {
3189 removedComponents.add(info.componentName);
3190 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003191 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003192
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003193 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3194 for (String pn : removedPackages) {
3195 deletePackageFromDatabase(context, pn, mUser);
3196 }
3197 for (ComponentName cn : removedComponents) {
3198 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003199 }
3200
Winson Chungdf95eb12013-10-16 14:57:07 -07003201 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003202 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3203
Winson Chungdf95eb12013-10-16 14:57:07 -07003204 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003205 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003206 mHandler.post(new Runnable() {
3207 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003208 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003209 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003210 callbacks.bindWorkspaceComponentsRemoved(
3211 removedPackages, removedComponents, mUser);
3212 }
3213 }
3214 });
3215 }
3216
3217 if (!removedApps.isEmpty()) {
3218 // Remove corresponding apps from All-Apps
3219 final Callbacks callbacks = getCallback();
3220 mHandler.post(new Runnable() {
3221 public void run() {
3222 Callbacks cb = getCallback();
3223 if (callbacks == cb && cb != null) {
3224 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003225 }
3226 }
3227 });
Joe Onoratobe386092009-11-17 17:32:16 -08003228 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003229
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003230 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3231 // get widget update signals.
3232 if (!Utilities.ATLEAST_MARSHMALLOW &&
3233 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003234 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003235 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003236 public void run() {
3237 Callbacks cb = getCallback();
3238 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003239 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003240 }
3241 }
3242 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003243 }
3244 }
3245 }
3246
3247 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3248 mHandler.post(new Runnable() {
3249 @Override
3250 public void run() {
3251 Callbacks cb = getCallback();
3252 if (callbacks == cb && cb != null) {
3253 callbacks.bindWidgetsModel(model);
3254 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003255 }
3256 });
3257 }
3258
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003259 public void refreshAndBindWidgetsAndShortcuts(
3260 final Callbacks callbacks, final boolean bindFirst) {
3261 runOnWorkerThread(new Runnable() {
3262 @Override
3263 public void run() {
3264 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3265 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003266 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003267 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3268 bindWidgetsModel(callbacks, model);
3269 // update the Widget entries inside DB on the worker thread.
3270 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3271 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003272 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003273 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003274 }
3275
Adam Cohen091440a2015-03-18 14:16:05 -07003276 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003277 UserHandleCompat user) {
3278 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3279 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003280 }
Adam Cohen556f6132014-01-15 15:18:08 -08003281
Kenny Guyed131872014-04-30 03:02:21 +01003282 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3283 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003284 if (cn == null) {
3285 return false;
3286 }
Kenny Guyed131872014-04-30 03:02:21 +01003287 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3288 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003289 return false;
3290 }
Kenny Guyed131872014-04-30 03:02:21 +01003291 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003292 }
3293
Adam Cohena28b78e2014-05-20 17:03:04 -07003294 public static boolean isValidPackage(Context context, String packageName,
3295 UserHandleCompat user) {
3296 if (packageName == null) {
3297 return false;
3298 }
3299 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3300 return launcherApps.isPackageEnabledForProfile(packageName, user);
3301 }
3302
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003304 * Make an ShortcutInfo object for a restored application or shortcut item that points
3305 * to a package that is not yet installed on the system.
3306 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003307 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003308 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003309 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003310 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003311
3312 Bitmap icon = iconInfo.loadIcon(c, info, context);
3313 // the fallback icon
3314 if (icon == null) {
3315 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3316 } else {
3317 info.setIcon(icon);
3318 }
Sunny Goyal34942622014-08-29 17:20:55 -07003319
3320 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003321 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003322 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003323 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003324 }
Sunny Goyal34942622014-08-29 17:20:55 -07003325 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3326 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003327 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003328 }
Sunny Goyal34942622014-08-29 17:20:55 -07003329 } else {
3330 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3331 }
3332
Winson Chung82b016c2015-05-08 17:00:10 -07003333 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003334 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003335 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003336 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003337 return info;
3338 }
3339
3340 /**
3341 * Make an Intent object for a restored application or shortcut item that points
3342 * to the market page for the item.
3343 */
Adam Cohen091440a2015-03-18 14:16:05 -07003344 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003345 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003346 return getMarketIntent(componentName.getPackageName());
3347 }
3348
3349 static Intent getMarketIntent(String packageName) {
3350 return new Intent(Intent.ACTION_VIEW)
3351 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003352 .scheme("market")
3353 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003354 .appendQueryParameter("id", packageName)
3355 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003356 }
3357
3358 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003359 * Make an ShortcutInfo object for a shortcut that is an application.
3360 *
3361 * If c is not null, then it will be used to fill in missing data like the title and icon.
3362 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003363 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003364 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003365 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003366 if (user == null) {
3367 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003368 return null;
3369 }
3370
Kenny Guyed131872014-04-30 03:02:21 +01003371 ComponentName componentName = intent.getComponent();
3372 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003373 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003374 return null;
3375 }
3376
3377 Intent newIntent = new Intent(intent.getAction(), null);
3378 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3379 newIntent.setComponent(componentName);
3380 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003381 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003382 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3383 return null;
3384 }
3385
3386 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003387 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003388 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3389 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3390 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003391 }
3392
Joe Onorato56d82912010-03-07 14:32:10 -05003393 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003394 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003395 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003396 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003397
Joe Onorato56d82912010-03-07 14:32:10 -05003398 // fall back to the class name of the activity
3399 if (info.title == null) {
3400 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003401 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003402
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003404 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003405 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003406 if (lai != null) {
3407 info.flags = AppInfo.initFlags(lai);
3408 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003410 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003411
Sunny Goyale2df0622015-04-24 11:27:00 -07003412 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003413 ItemInfoFilter f) {
3414 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3415 for (ItemInfo i : infos) {
3416 if (i instanceof ShortcutInfo) {
3417 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003418 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003419 if (cn != null && f.filterItem(null, info, cn)) {
3420 filtered.add(info);
3421 }
3422 } else if (i instanceof FolderInfo) {
3423 FolderInfo info = (FolderInfo) i;
3424 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003425 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003426 if (cn != null && f.filterItem(info, s, cn)) {
3427 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003428 }
3429 }
Winson Chung64359a52013-07-08 17:17:08 -07003430 } else if (i instanceof LauncherAppWidgetInfo) {
3431 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3432 ComponentName cn = info.providerName;
3433 if (cn != null && f.filterItem(null, info, cn)) {
3434 filtered.add(info);
3435 }
Winson Chung8a435102012-08-30 17:16:53 -07003436 }
3437 }
Winson Chung64359a52013-07-08 17:17:08 -07003438 return new ArrayList<ItemInfo>(filtered);
3439 }
3440
Adam Cohen091440a2015-03-18 14:16:05 -07003441 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003442 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003443 ItemInfoFilter filter = new ItemInfoFilter() {
3444 @Override
3445 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003446 if (info.user == null) {
3447 return cn.equals(cname);
3448 } else {
3449 return cn.equals(cname) && info.user.equals(user);
3450 }
Winson Chung64359a52013-07-08 17:17:08 -07003451 }
3452 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003453 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003454 }
3455
Sunny Goyal1a745e82014-10-02 15:58:31 -07003456 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003457 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003458 */
Adam Cohen091440a2015-03-18 14:16:05 -07003459 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003460 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003461 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003462 // Non-app shortcuts are only supported for current user.
3463 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003465
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003466 // TODO: If there's an explicit component and we can't install that, delete it.
3467
Winson Chung82b016c2015-05-08 17:00:10 -07003468 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003469
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003470 Bitmap icon = iconInfo.loadIcon(c, info, context);
3471 // the fallback icon
3472 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003473 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003474 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003475 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003476 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 return info;
3478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003479
Sunny Goyal2350bc92014-10-14 16:42:54 -07003480 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003481 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3482 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3483 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3484
Adam Cohend9198822011-11-22 16:42:47 -08003485 if (intent == null) {
3486 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3487 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3488 return null;
3489 }
3490
Joe Onorato0589f0f2010-02-08 13:44:00 -08003491 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003492 boolean customIcon = false;
3493 ShortcutIconResource iconResource = null;
3494
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003495 if (bitmap instanceof Bitmap) {
3496 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003497 customIcon = true;
3498 } else {
3499 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003500 if (extra instanceof ShortcutIconResource) {
3501 iconResource = (ShortcutIconResource) extra;
3502 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003503 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003504 }
3505 }
3506
Michael Jurkac9d95c52011-08-29 14:03:34 -07003507 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003508
Kenny Guyed131872014-04-30 03:02:21 +01003509 // Only support intents for current user for now. Intents sent from other
3510 // users wouldn't get here without intent forwarding anyway.
3511 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003512 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003513 icon = mIconCache.getDefaultIcon(info.user);
3514 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003515 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003516 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003517
Winson Chung82b016c2015-05-08 17:00:10 -07003518 info.title = Utilities.trim(name);
3519 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003520 info.intent = intent;
3521 info.customIcon = customIcon;
3522 info.iconResource = iconResource;
3523
3524 return info;
3525 }
3526
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003528 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529 * or make a new one.
3530 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003531 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 // See if a placeholder was created for us already
3533 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003534 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003536 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 folders.put(id, folderInfo);
3538 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003539 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 }
3541
Joe Onoratobe386092009-11-17 17:32:16 -08003542
Sunny Goyal651077b2014-06-30 14:15:31 -07003543 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3544 return (provider != null) && (provider.provider != null)
3545 && (provider.provider.getPackageName() != null);
3546 }
3547
Joe Onoratobe386092009-11-17 17:32:16 -08003548 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003549 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003550 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3551 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3552 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3553 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003554 if (mLoaderTask != null) {
3555 mLoaderTask.dumpState();
3556 } else {
3557 Log.d(TAG, "mLoaderTask=null");
3558 }
Joe Onoratobe386092009-11-17 17:32:16 -08003559 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003560
3561 public Callbacks getCallback() {
3562 return mCallbacks != null ? mCallbacks.get() : null;
3563 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003564
3565 /**
3566 * @return {@link FolderInfo} if its already loaded.
3567 */
3568 public FolderInfo findFolderById(Long folderId) {
3569 synchronized (sBgLock) {
3570 return sBgFolders.get(folderId);
3571 }
3572 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003573
Sunny Goyal527c7d32015-08-28 15:19:36 -07003574 @Thunk class DeferredMainThreadExecutor implements Executor {
3575
3576 @Override
3577 public void execute(Runnable command) {
3578 runOnMainThread(command);
3579 }
3580 }
3581
Sunny Goyal756adbc2015-04-16 15:20:51 -07003582 /**
3583 * @return the looper for the worker thread which can be used to start background tasks.
3584 */
3585 public static Looper getWorkerLooper() {
3586 return sWorkerThread.getLooper();
3587 }
Sunny Goyala1365452015-10-01 15:46:24 -07003588
3589 @Thunk static final void addDumpLog(String log) {
3590 Launcher.addDumpLog(TAG, log);
3591 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003592}