blob: c4c1946fc8b482e3e5ea3de68746cee4ddf30e17 [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;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyalf862a262015-12-14 14:27:38 -080060import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070063import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070064import com.android.launcher3.util.FlagOp;
Sunny Goyale2df0622015-04-24 11:27:00 -070065import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070066import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyalda891c12016-03-18 18:29:24 -070067import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070068import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070069import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080070
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.lang.ref.WeakReference;
72import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070073import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070075import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070076import java.util.Collections;
77import java.util.Comparator;
78import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070079import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070080import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070082import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070083import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070084import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086/**
87 * Maintains in-memory state of the Launcher. It is expected that there should be only one
88 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070089 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 */
Kenny Guyed131872014-04-30 03:02:21 +010091public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010092 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080093 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040094 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070095 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040096
Joe Onorato9c1289c2009-08-17 11:03:03 -040097 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070098
Dan Sandlerd5024042014-01-09 15:01:33 -050099 public static final int LOADER_FLAG_NONE = 0;
100 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
101 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
102
Joe Onorato36115782010-06-17 13:28:48 -0400103 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500104 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800105
Winson Chunga6945242014-01-08 14:04:34 -0800106 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400107
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk final LauncherAppState mApp;
109 @Thunk final Object mLock = new Object();
110 @Thunk DeferredHandler mHandler = new DeferredHandler();
111 @Thunk LoaderTask mLoaderTask;
112 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700113 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Jason Monkbbe1e242014-05-16 17:37:34 -0400115 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700116
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118 static {
119 sWorkerThread.start();
120 }
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 // We start off with everything not loaded. After that, we assume that
124 // our monitoring of the package manager provides all updates and we never
125 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk boolean mWorkspaceLoaded;
127 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700128
Sunny Goyal756a28a2015-04-23 17:07:55 -0700129 /**
130 * Set of runnables to be called on the background thread after the workspace binding
131 * is complete.
132 */
133 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
134
Adam Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700137 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800138 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700139 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800140 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142 // The lock that must be acquired before referencing any static bg data structures. Unlike
143 // other locks, this one can generally be held long-term because we never expect any of these
144 // static data structures to be referenced outside of the worker thread except on the first
145 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700146 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700150 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
153 // created by LauncherModel that are directly on the home screen (however, no widgets or
154 // shortcuts within folders).
155 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
158 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 new ArrayList<LauncherAppWidgetInfo>();
160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700162 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700163
Adam Cohendcd297f2013-06-18 13:13:40 -0700164 // sBgWorkspaceScreens is the ordered set of workspace screens.
165 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
166
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700167 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700168 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
169 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700170
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171 // </ only access in worker thread >
172
Adam Cohen091440a2015-03-18 14:16:05 -0700173 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Adam Cohen091440a2015-03-18 14:16:05 -0700175 @Thunk final LauncherAppsCompat mLauncherApps;
176 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700179 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700181 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700183 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
184 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700185 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700186 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700187 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800188 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200190 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700191 public void bindAppsAdded(ArrayList<Long> newScreens,
192 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700193 ArrayList<ItemInfo> addAnimated,
194 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700196 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
197 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
198 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700199 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700200 public void bindWorkspaceComponentsRemoved(
201 HashSet<String> packageNames, HashSet<ComponentName> components,
202 UserHandleCompat user);
203 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800204 public void notifyWidgetProvidersChanged();
205 public void bindWidgetsModel(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700206 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700207 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700208 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700209 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chung64359a52013-07-08 17:17:08 -0700212 public interface ItemInfoFilter {
213 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
214 }
215
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800217 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400218
Adam Cohen71483f42014-05-15 14:04:01 -0700219 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400220 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
221 // resource string.
222 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
223 ProviderInfo providerInfo =
224 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
225 ProviderInfo redirectProvider =
226 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700227
228 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400229 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700230
231 if (mOldContentProviderExists) {
232 Log.d(TAG, "Old launcher provider exists.");
233 } else {
234 Log.d(TAG, "Old launcher provider does not exist.");
235 }
236
Daniel Sandlere4f98912013-06-25 15:13:26 -0400237 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100238 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700239 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800240 mIconCache = iconCache;
241
Kenny Guyed131872014-04-30 03:02:21 +0100242 mLauncherApps = LauncherAppsCompat.getInstance(context);
243 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 }
245
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700246 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
247 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700249 if (sWorkerThread.getThreadId() == Process.myTid()) {
250 // If we are on the worker thread, post onto the main handler
251 mHandler.post(r);
252 } else {
253 r.run();
254 }
255 }
256
257 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
258 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700259 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700260 if (sWorkerThread.getThreadId() == Process.myTid()) {
261 r.run();
262 } else {
263 // If we are not on the worker thread, then post to the worker handler
264 sWorker.post(r);
265 }
266 }
267
Winson Chunge43a1e72014-01-15 10:33:02 -0800268 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
269 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800270 }
271
Sunny Goyal756adbc2015-04-16 15:20:51 -0700272 public void setPackageState(final PackageInstallInfo installInfo) {
273 Runnable updateRunnable = new Runnable() {
274
275 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500276 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700277 synchronized (sBgLock) {
278 final HashSet<ItemInfo> updates = new HashSet<>();
279
280 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
281 // Ignore install success events as they are handled by Package add events.
282 return;
283 }
284
Sunny Goyale2df0622015-04-24 11:27:00 -0700285 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700286 if (info instanceof ShortcutInfo) {
287 ShortcutInfo si = (ShortcutInfo) info;
288 ComponentName cn = si.getTargetComponent();
289 if (si.isPromise() && (cn != null)
290 && installInfo.packageName.equals(cn.getPackageName())) {
291 si.setInstallProgress(installInfo.progress);
292
293 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
294 // Mark this info as broken.
295 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
296 }
297 updates.add(si);
298 }
299 }
300 }
301
302 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
303 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
304 widget.installProgress = installInfo.progress;
305 updates.add(widget);
306 }
307 }
308
309 if (!updates.isEmpty()) {
310 // Push changes to the callback.
311 Runnable r = new Runnable() {
312 public void run() {
313 Callbacks callbacks = getCallback();
314 if (callbacks != null) {
315 callbacks.bindRestoreItemsChange(updates);
316 }
317 }
318 };
319 mHandler.post(r);
320 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500321 }
322 }
323 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700324 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500325 }
326
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327 /**
328 * Updates the icons and label of all pending icons for the provided package name.
329 */
330 public void updateSessionDisplayInfo(final String packageName) {
331 Runnable updateRunnable = new Runnable() {
332
333 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700334 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700335 synchronized (sBgLock) {
336 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
337 final UserHandleCompat user = UserHandleCompat.myUserHandle();
338
Sunny Goyale2df0622015-04-24 11:27:00 -0700339 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340 if (info instanceof ShortcutInfo) {
341 ShortcutInfo si = (ShortcutInfo) info;
342 ComponentName cn = si.getTargetComponent();
343 if (si.isPromise() && (cn != null)
344 && packageName.equals(cn.getPackageName())) {
345 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
346 // For auto install apps update the icon as well as label.
347 mIconCache.getTitleAndIcon(si,
348 si.promisedIntent, user,
349 si.shouldUseLowResIcon());
350 } else {
351 // Only update the icon for restored apps.
352 si.updateIcon(mIconCache);
353 }
354 updates.add(si);
355 }
356 }
357 }
358
359 if (!updates.isEmpty()) {
360 // Push changes to the callback.
361 Runnable r = new Runnable() {
362 public void run() {
363 Callbacks callbacks = getCallback();
364 if (callbacks != null) {
365 callbacks.bindShortcutsChanged(updates,
366 new ArrayList<ShortcutInfo>(), user);
367 }
368 }
369 };
370 mHandler.post(r);
371 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700372 }
373 }
374 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700375 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700376 }
377
Adam Cohen76a47a12014-02-05 11:47:43 -0800378 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800379 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800380
381 if (allAppsApps == null) {
382 throw new RuntimeException("allAppsApps must not be null");
383 }
384 if (allAppsApps.isEmpty()) {
385 return;
386 }
387
388 // Process the newly added applications and add them to the database first
389 Runnable r = new Runnable() {
390 public void run() {
391 runOnMainThread(new Runnable() {
392 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800393 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800394 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500395 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800396 }
397 }
398 });
399 }
400 };
401 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700402 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800403
Sunny Goyala9116722015-04-29 13:55:58 -0700404 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800405 int[] xy, int spanX, int spanY) {
406 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700407 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
408 final int xCount = (int) profile.numColumns;
409 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800410 boolean[][] occupied = new boolean[xCount][yCount];
411 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700412 for (ItemInfo r : occupiedPos) {
413 int right = r.cellX + r.spanX;
414 int bottom = r.cellY + r.spanY;
415 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
416 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800417 occupied[x][y] = true;
418 }
419 }
420 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800421 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700422 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800423 }
424
425 /**
426 * Find a position on the screen for the given size or adds a new screen.
427 * @return screenId and the coordinates for the item.
428 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700429 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700430 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 ArrayList<Long> workspaceScreens,
432 ArrayList<Long> addedWorkspaceScreensFinal,
433 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700434 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800435
Sunny Goyala9116722015-04-29 13:55:58 -0700436 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700437 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700438 synchronized (sBgLock) {
439 for (ItemInfo info : sBgItemsIdMap) {
440 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
441 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
442 if (items == null) {
443 items = new ArrayList<>();
444 screenItems.put(info.screenId, items);
445 }
446 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 }
450
451 // Find appropriate space for the item.
452 long screenId = 0;
453 int[] cordinates = new int[2];
454 boolean found = false;
455
456 int screenCount = workspaceScreens.size();
457 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700458 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800459 if (preferredScreenIndex < screenCount) {
460 screenId = workspaceScreens.get(preferredScreenIndex);
461 found = findNextAvailableIconSpaceInScreen(
462 screenItems.get(screenId), cordinates, spanX, spanY);
463 }
464
465 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700466 // Search on any of the screens starting from the first screen.
467 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 screenId = workspaceScreens.get(screen);
469 if (findNextAvailableIconSpaceInScreen(
470 screenItems.get(screenId), cordinates, spanX, spanY)) {
471 // We found a space for it
472 found = true;
473 break;
474 }
475 }
476 }
477
478 if (!found) {
479 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700480 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
481 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
482 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483
484 // Save the screen id for binding in the workspace
485 workspaceScreens.add(screenId);
486 addedWorkspaceScreensFinal.add(screenId);
487
488 // If we still can't find an empty space, then God help us all!!!
489 if (!findNextAvailableIconSpaceInScreen(
490 screenItems.get(screenId), cordinates, spanX, spanY)) {
491 throw new RuntimeException("Can't find space to add the item");
492 }
493 }
494 return Pair.create(screenId, cordinates);
495 }
496
497 /**
498 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800499 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700500 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700501 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800503 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700504 return;
Winson Chung997a9232013-07-24 15:33:46 -0700505 }
Winson Chung64359a52013-07-08 17:17:08 -0700506 // Process the newly added applications and add them to the database first
507 Runnable r = new Runnable() {
508 public void run() {
509 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
510 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
511
Winson Chung76828c82013-08-19 15:43:29 -0700512 // Get the list of workspace screens. We need to append to this list and
513 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
514 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800515 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700516 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700518 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800519 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700520 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700522 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800523 }
Winson Chung76828c82013-08-19 15:43:29 -0700524
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800525 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700526 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700527 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800528 long screenId = coords.first;
529 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700530
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700531 ItemInfo itemInfo;
532 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
533 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700535 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700536 } else {
537 throw new RuntimeException("Unexpected info type");
538 }
Winson Chung94d67682013-09-25 16:29:40 -0700539
Winson Chung64359a52013-07-08 17:17:08 -0700540 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700541 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700542 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700543 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700544 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700545 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700546 }
547 }
548
Winson Chung76828c82013-08-19 15:43:29 -0700549 // Update the workspace screens
550 updateWorkspaceScreenOrder(context, workspaceScreens);
551
Adam Cohen76a47a12014-02-05 11:47:43 -0800552 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700553 runOnMainThread(new Runnable() {
554 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800555 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700556 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700557 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
558 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700559 if (!addedShortcutsFinal.isEmpty()) {
560 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
561 long lastScreenId = info.screenId;
562 for (ItemInfo i : addedShortcutsFinal) {
563 if (i.screenId == lastScreenId) {
564 addAnimated.add(i);
565 } else {
566 addNotAnimated.add(i);
567 }
Winson Chung997a9232013-07-24 15:33:46 -0700568 }
569 }
Winson Chungd64d1762013-08-20 14:37:16 -0700570 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800571 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700572 }
Winson Chung64359a52013-07-08 17:17:08 -0700573 }
Winson Chung997a9232013-07-24 15:33:46 -0700574 });
575 }
Winson Chung64359a52013-07-08 17:17:08 -0700576 }
577 };
578 runOnWorkerThread(r);
579 }
580
Sunny Goyald33860f2015-04-23 16:02:20 -0700581 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700582 if (sWorkerThread.getThreadId() == Process.myTid()) {
583 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
584 "main thread");
585 }
586
Sunny Goyald33860f2015-04-23 16:02:20 -0700587 // Remove any queued UI runnables
588 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700589 // Unbind all the workspace items
590 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700591 }
592
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700593 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700594 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700595 // Ensure that we don't use the same workspace items data structure on the main thread
596 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700597 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700598 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700599 tmpItems.addAll(sBgWorkspaceItems);
600 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700601 }
602 Runnable r = new Runnable() {
603 @Override
604 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700605 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700606 item.unbind();
607 }
608 }
609 };
610 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700611 }
612
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613 /**
614 * Adds an item to the DB if it was not created previously, or move it to a new
615 * <container, screen, cellX, cellY>
616 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800617 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700618 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 if (item.container == ItemInfo.NO_ID) {
620 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700621 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622 } else {
623 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700624 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 }
626 }
627
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700628 static void checkItemInfoLocked(
629 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
630 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
631 if (modelItem != null && item != modelItem) {
632 // check all the data is consistent
633 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
634 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
635 ShortcutInfo shortcut = (ShortcutInfo) item;
636 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
637 modelShortcut.intent.filterEquals(shortcut.intent) &&
638 modelShortcut.id == shortcut.id &&
639 modelShortcut.itemType == shortcut.itemType &&
640 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700642 modelShortcut.cellX == shortcut.cellX &&
643 modelShortcut.cellY == shortcut.cellY &&
644 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700645 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700646 // For all intents and purposes, this is the same object
647 return;
648 }
649 }
650
651 // the modelItem needs to match up perfectly with item if our model is
652 // to be consistent with the database-- for now, just require
653 // modelItem == item or the equality check above
654 String msg = "item: " + ((item != null) ? item.toString() : "null") +
655 "modelItem: " +
656 ((modelItem != null) ? modelItem.toString() : "null") +
657 "Error: ItemInfo passed to checkItemInfo doesn't match original";
658 RuntimeException e = new RuntimeException(msg);
659 if (stackTrace != null) {
660 e.setStackTrace(stackTrace);
661 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800662 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700663 }
664 }
665
Michael Jurka816474f2012-06-25 14:49:02 -0700666 static void checkItemInfo(final ItemInfo item) {
667 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
668 final long itemId = item.id;
669 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700670 public void run() {
671 synchronized (sBgLock) {
672 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700673 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700674 }
675 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700676 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700677 }
678
Michael Jurkac9d95c52011-08-29 14:03:34 -0700679 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
680 final ItemInfo item, final String callingFunction) {
681 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700682 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700683 final ContentResolver cr = context.getContentResolver();
684
Adam Cohen487f7dd2012-06-28 18:12:10 -0700685 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700686 Runnable r = new Runnable() {
687 public void run() {
688 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700689 updateItemArrays(item, itemId, stackTrace);
690 }
691 };
692 runOnWorkerThread(r);
693 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700694
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700695 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
696 final ArrayList<ItemInfo> items, final String callingFunction) {
697 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700698
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700699 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
700 Runnable r = new Runnable() {
701 public void run() {
702 ArrayList<ContentProviderOperation> ops =
703 new ArrayList<ContentProviderOperation>();
704 int count = items.size();
705 for (int i = 0; i < count; i++) {
706 ItemInfo item = items.get(i);
707 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700708 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700709 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700710
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700711 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
712 updateItemArrays(item, itemId, stackTrace);
713
714 }
715 try {
716 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
717 } catch (Exception e) {
718 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700719 }
720 }
721 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700722 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700724
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
726 // Lock on mBgLock *after* the db operation
727 synchronized (sBgLock) {
728 checkItemInfoLocked(itemId, item, stackTrace);
729
730 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
731 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
732 // Item is in a folder, make sure this folder exists
733 if (!sBgFolders.containsKey(item.container)) {
734 // An items container is being set to a that of an item which is not in
735 // the list of Folders.
736 String msg = "item: " + item + " container being set to: " +
737 item.container + ", not in the list of folders";
738 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700739 }
740 }
741
742 // Items are added/removed from the corresponding FolderInfo elsewhere, such
743 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
744 // that are on the desktop, as appropriate
745 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800746 if (modelItem != null &&
747 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
748 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 switch (modelItem.itemType) {
750 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
751 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
752 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
753 if (!sBgWorkspaceItems.contains(modelItem)) {
754 sBgWorkspaceItems.add(modelItem);
755 }
756 break;
757 default:
758 break;
759 }
760 } else {
761 sBgWorkspaceItems.remove(modelItem);
762 }
763 }
764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400767 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700768 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700769 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700770 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 item.cellX = cellX;
773 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700774
Winson Chung3d503fb2011-07-13 17:25:49 -0700775 // We store hotseat items in canonical form which is this orientation invariant position
776 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700779 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700780 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783
784 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
787 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800788 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790
Michael Jurkac9d95c52011-08-29 14:03:34 -0700791 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700792 }
793
794 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700795 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
796 * cellX, cellY have already been updated on the ItemInfos.
797 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800798 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700799 final long container, final int screen) {
800
801 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
802 int count = items.size();
803
804 for (int i = 0; i < count; i++) {
805 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700806 item.container = container;
807
808 // We store hotseat items in canonical form which is this orientation invariant position
809 // in the hotseat
810 if (context instanceof Launcher && screen < 0 &&
811 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 item.cellY);
814 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700816 }
817
818 final ContentValues values = new ContentValues();
819 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
820 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
821 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800822 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824
825 contentValues.add(values);
826 }
827 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
828 }
829
830 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700831 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800832 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700833 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700834 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700835 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800836 item.cellX = cellX;
837 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700838 item.spanX = spanX;
839 item.spanY = spanY;
840
841 // We store hotseat items in canonical form which is this orientation invariant position
842 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700843 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700844 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700846 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700848 }
Adam Cohend4844c32011-02-18 19:25:06 -0800849
Adam Cohend4844c32011-02-18 19:25:06 -0800850 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800851 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700852 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
853 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800854 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
856 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800858
Michael Jurka816474f2012-06-25 14:49:02 -0700859 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700861
862 /**
863 * Update an item to the database in a specified container.
864 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700865 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700866 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100867 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800869 }
870
Sunny Goyal756a28a2015-04-23 17:07:55 -0700871 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700872 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700873 synchronized (mLock) {
874 if (!mHasLoaderCompletedOnce ||
875 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
876 throw new RuntimeException("Trying to add shortcut while loader is running");
877 }
878 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700879 }
880 }
881
Adam Cohend4844c32011-02-18 19:25:06 -0800882 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700883 * Returns true if the shortcuts already exists on the workspace. This must be called after
884 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700886 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
887 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700888 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700889 if (intent.getComponent() != null) {
890 // If component is not null, an intent with null package will produce
891 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700892 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700893 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700894 intentWithPkg = intent.toUri(0);
895 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700896 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700897 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
898 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700899 }
900 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 intentWithPkg = intent.toUri(0);
902 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700903 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700904
905 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700906 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700907 if (item instanceof ShortcutInfo) {
908 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700909 Intent targetIntent = info.promisedIntent == null
910 ? info.intent : info.promisedIntent;
911 if (targetIntent != null && info.user.equals(user)) {
912 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700913 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
914 return true;
915 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700916 }
917 }
918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700920 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700921 }
922
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 * Add an item to the database in a specified container. Sets the container, screen, cellX and
925 * cellY fields of the item. Also assigns an ID to the item.
926 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700927 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700928 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 item.cellX = cellX;
931 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700932 // We store hotseat items in canonical form which is this orientation invariant position
933 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700935 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700937 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940
941 final ContentValues values = new ContentValues();
942 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100943 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944
Sunny Goyald2497482015-09-22 18:24:19 -0700945 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
946 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
947
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700948 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700949
Jason Monk8e19cf22014-03-20 15:06:57 -0400950 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700951 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700952 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700953 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700956 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400957 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700958 sBgItemsIdMap.put(item.id, item);
959 switch (item.itemType) {
960 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
961 sBgFolders.put(item.id, (FolderInfo) item);
962 // Fall through
963 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
964 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
965 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
966 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
967 sBgWorkspaceItems.add(item);
968 } else {
969 if (!sBgFolders.containsKey(item.container)) {
970 // Adding an item to a folder that doesn't exist.
971 String msg = "adding item: " + item + " to a folder that " +
972 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700973 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700975 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700976 break;
977 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
978 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
979 break;
980 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 }
982 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700983 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700985 }
986
Sunny Goyal34942622014-08-29 17:20:55 -0700987 private static ArrayList<ItemInfo> getItemsByPackageName(
988 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700989 ItemInfoFilter filter = new ItemInfoFilter() {
990 @Override
991 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
992 return cn.getPackageName().equals(pn) && info.user.equals(user);
993 }
994 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700995 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700996 }
997
998 /**
999 * Removes all the items from the database corresponding to the specified package.
1000 */
1001 static void deletePackageFromDatabase(Context context, final String pn,
1002 final UserHandleCompat user) {
1003 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001004 }
1005
1006 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001009 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001010 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1011 items.add(item);
1012 deleteItemsFromDatabase(context, items);
1013 }
1014
1015 /**
1016 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001017 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001018 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001020 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001021 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001022 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001023 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001024 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001025
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001026 // Lock on mBgLock *after* the db operation
1027 synchronized (sBgLock) {
1028 switch (item.itemType) {
1029 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1030 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001031 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001032 if (info.container == item.id) {
1033 // We are deleting a folder which still contains items that
1034 // think they are contained by that folder.
1035 String msg = "deleting a folder (" + item + ") which still " +
1036 "contains items (" + info + ")";
1037 Log.e(TAG, msg);
1038 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001040 sBgWorkspaceItems.remove(item);
1041 break;
1042 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1043 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1044 sBgWorkspaceItems.remove(item);
1045 break;
1046 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1047 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1048 break;
1049 }
1050 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001052 }
1053 }
Michael Jurka83df1882011-08-31 20:59:26 -07001054 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001055 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 }
1057
1058 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 * Update the order of the workspace screens in the database. The array list contains
1060 * a list of screen ids in the order that they should appear.
1061 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001062 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001063 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 final ContentResolver cr = context.getContentResolver();
1065 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1066
1067 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001068 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 while (iter.hasNext()) {
1070 long id = iter.next();
1071 if (id < 0) {
1072 iter.remove();
1073 }
1074 }
1075
1076 Runnable r = new Runnable() {
1077 @Override
1078 public void run() {
Yura085c8532014-02-11 15:15:29 +00001079 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001081 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001082 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 for (int i = 0; i < count; i++) {
1084 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001085 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1087 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001088 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 }
Yura085c8532014-02-11 15:15:29 +00001090
1091 try {
1092 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1093 } catch (Exception ex) {
1094 throw new RuntimeException(ex);
1095 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001096
Winson Chungba9c37f2013-08-30 14:11:37 -07001097 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001098 sBgWorkspaceScreens.clear();
1099 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001100 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 }
1102 };
1103 runOnWorkerThread(r);
1104 }
1105
1106 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001107 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 */
Winsonc0b52fe2015-09-09 16:38:15 -07001109 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 final ContentResolver cr = context.getContentResolver();
1111
Michael Jurkac9d95c52011-08-29 14:03:34 -07001112 Runnable r = new Runnable() {
1113 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001114 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001116 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 sBgItemsIdMap.remove(info.id);
1118 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 sBgWorkspaceItems.remove(info);
1120 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001122 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001123 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001125 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 for (ItemInfo childInfo : info.contents) {
1127 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001129 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001130 }
1131 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 }
1134
1135 /**
1136 * Set this as the current Launcher activity object for the loader.
1137 */
1138 public void initialize(Callbacks callbacks) {
1139 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001140 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1141 // workspace to prevent leaking Launcher activities on orientation change.
1142 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 mCallbacks = new WeakReference<Callbacks>(callbacks);
1144 }
1145 }
1146
Kenny Guyed131872014-04-30 03:02:21 +01001147 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001148 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001149 int op = PackageUpdatedTask.OP_UPDATE;
1150 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1151 user));
1152 }
1153
1154 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001155 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001156 int op = PackageUpdatedTask.OP_REMOVE;
1157 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1158 user));
1159 }
1160
1161 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001162 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001163 int op = PackageUpdatedTask.OP_ADD;
1164 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1165 user));
1166 }
1167
1168 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001169 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001170 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001171 enqueuePackageUpdated(
1172 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001173 }
1174
1175 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001176 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001177 boolean replacing) {
1178 if (!replacing) {
1179 enqueuePackageUpdated(new PackageUpdatedTask(
1180 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1181 user));
1182 }
Kenny Guyed131872014-04-30 03:02:21 +01001183 }
1184
Kenny Guy44cba692016-01-21 19:50:02 +00001185 @Override
1186 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1187 enqueuePackageUpdated(new PackageUpdatedTask(
1188 PackageUpdatedTask.OP_SUSPEND, packageNames,
1189 user));
1190 }
1191
1192 @Override
1193 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1194 enqueuePackageUpdated(new PackageUpdatedTask(
1195 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1196 user));
1197 }
1198
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001199 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1201 * ACTION_PACKAGE_CHANGED.
1202 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001203 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001204 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001205 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001206
Joe Onorato36115782010-06-17 13:28:48 -04001207 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001208 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001209 // If we have changed locale we need to clear out the labels in all apps/workspace.
1210 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001211 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001212 Callbacks callbacks = getCallback();
1213 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001214 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001215 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001216 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001217 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001218 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001219 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001220 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1221 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001222 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1223 if (user != null) {
1224 enqueuePackageUpdated(new PackageUpdatedTask(
1225 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1226 new String[0], user));
1227 }
Tony Wickham827cef22016-03-17 15:39:39 -07001228 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1229 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001230 }
1231 }
1232
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001233 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001234 resetLoadedState(true, true);
1235
Reena Lee93f824a2011-09-23 17:20:28 -07001236 // Do this here because if the launcher activity is running it will be restarted.
1237 // If it's not running startLoaderFromBackground will merely tell it that it needs
1238 // to reload.
1239 startLoaderFromBackground();
1240 }
1241
Winson Chungf0c6ae02012-03-21 16:10:31 -07001242 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1243 synchronized (mLock) {
1244 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1245 // mWorkspaceLoaded to true later
1246 stopLoaderLocked();
1247 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1248 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1249 }
1250 }
1251
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001252 /**
1253 * When the launcher is in the background, it's possible for it to miss paired
1254 * configuration changes. So whenever we trigger the loader from the background
1255 * tell the launcher that it needs to re-run the loader when it comes back instead
1256 * of doing it now.
1257 */
1258 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001259 Callbacks callbacks = getCallback();
1260 if (callbacks != null) {
1261 // Only actually run the loader if they're not paused.
1262 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001263 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001264 }
1265 }
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();
Sunny Goyal93f878c2016-03-30 17:31:24 -07001302 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags, synchronousBindPage);
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;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001356 private int mPageToBindFirst;
1357
Adam Cohen091440a2015-03-18 14:16:05 -07001358 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001359 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001360 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001361 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362
Sunny Goyal93f878c2016-03-30 17:31:24 -07001363 LoaderTask(Context context, int flags, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001364 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001365 mFlags = flags;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001366 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367 }
1368
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001369 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001370 mIsLoadingAndBindingWorkspace = true;
1371
Joe Onorato36115782010-06-17 13:28:48 -04001372 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001373 if (DEBUG_LOADERS) {
1374 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001376
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001377 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001378 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001379 synchronized (LoaderTask.this) {
1380 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001381 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001382 }
1383 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001384 }
1385 }
1386
Joe Onorato36115782010-06-17 13:28:48 -04001387 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001388 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001389 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001390
Joe Onorato36115782010-06-17 13:28:48 -04001391 private void waitForIdle() {
1392 // Wait until the either we're stopped or the other threads are done.
1393 // This way we don't start loading all apps until the workspace has settled
1394 // down.
1395 synchronized (LoaderTask.this) {
1396 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001397
Joe Onorato36115782010-06-17 13:28:48 -04001398 mHandler.postIdle(new Runnable() {
1399 public void run() {
1400 synchronized (LoaderTask.this) {
1401 mLoadAndBindStepFinished = true;
1402 if (DEBUG_LOADERS) {
1403 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001404 }
Joe Onorato36115782010-06-17 13:28:48 -04001405 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001406 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 }
Joe Onorato36115782010-06-17 13:28:48 -04001408 });
1409
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001410 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001411 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001412 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1413 // wait no longer than 1sec at a time
1414 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001415 } catch (InterruptedException ex) {
1416 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001417 }
1418 }
Joe Onorato36115782010-06-17 13:28:48 -04001419 if (DEBUG_LOADERS) {
1420 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001421 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001422 + "ms for previous step to finish binding");
1423 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001426
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001427 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001428 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001429 // Ensure that we have a valid page index to load synchronously
1430 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1431 "valid page index");
1432 }
1433 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1434 // Ensure that we don't try and bind a specified page when the pages have not been
1435 // loaded already (we should load everything asynchronously in that case)
1436 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1437 }
1438 synchronized (mLock) {
1439 if (mIsLoaderTaskRunning) {
1440 // Ensure that we are never running the background loading at this point since
1441 // we also touch the background collections
1442 throw new RuntimeException("Error! Background loading is already running");
1443 }
1444 }
1445
1446 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1447 // data structures, we can't allow any other thread to touch that data, but because
1448 // this call is synchronous, we can get away with not locking).
1449
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001450 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001451 // operations from the previous activity. We need to ensure that all queued operations
1452 // are executed before any synchronous binding work is done.
1453 mHandler.flush();
1454
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001455 // Divide the set of loaded items into those that we are binding synchronously, and
1456 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001457 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001458 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1459 // arise from that.
1460 onlyBindAllApps();
1461 }
1462
Joe Onorato36115782010-06-17 13:28:48 -04001463 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001464 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001465 if (mStopped) {
1466 return;
1467 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 mIsLoaderTaskRunning = true;
1469 }
Joe Onorato36115782010-06-17 13:28:48 -04001470 // Optimize for end-user experience: if the Launcher is up and // running with the
1471 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1472 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001473 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001474 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001475 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001476
Joe Onorato36115782010-06-17 13:28:48 -04001477 if (mStopped) {
1478 break keep_running;
1479 }
1480
Joe Onorato36115782010-06-17 13:28:48 -04001481 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001482
1483 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001484 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1485 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001486 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487
Joe Onorato36115782010-06-17 13:28:48 -04001488 // Clear out this reference, otherwise we end up holding it until all of the
1489 // callback runnables are done.
1490 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491
Joe Onorato36115782010-06-17 13:28:48 -04001492 synchronized (mLock) {
1493 // If we are still the last one to be scheduled, remove ourselves.
1494 if (mLoaderTask == this) {
1495 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001497 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001498 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001499 }
Joe Onorato36115782010-06-17 13:28:48 -04001500 }
1501
1502 public void stopLocked() {
1503 synchronized (LoaderTask.this) {
1504 mStopped = true;
1505 this.notify();
1506 }
1507 }
1508
1509 /**
1510 * Gets the callbacks object. If we've been stopped, or if the launcher object
1511 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1512 * object that was around when the deferred message was scheduled, and if there's
1513 * a new Callbacks object around then also return null. This will save us from
1514 * calling onto it with data that will be ignored.
1515 */
1516 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1517 synchronized (mLock) {
1518 if (mStopped) {
1519 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001520 }
Joe Onorato36115782010-06-17 13:28:48 -04001521
1522 if (mCallbacks == null) {
1523 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001524 }
Joe Onorato36115782010-06-17 13:28:48 -04001525
1526 final Callbacks callbacks = mCallbacks.get();
1527 if (callbacks != oldCallbacks) {
1528 return null;
1529 }
1530 if (callbacks == null) {
1531 Log.w(TAG, "no mCallbacks");
1532 return null;
1533 }
1534
1535 return callbacks;
1536 }
1537 }
1538
1539 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001540 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1541 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001542 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001543 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001544 final int countX = profile.numColumns;
1545 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001546
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001548 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001549 // Return early if we detect that an item is under the hotseat button
1550 if (mCallbacks == null ||
1551 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001552 Log.e(TAG, "Error loading shortcut into hotseat " + item
1553 + " into position (" + item.screenId + ":" + item.cellX + ","
1554 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001555 return false;
1556 }
1557
Dan Sandler295ae182013-12-10 16:05:47 -05001558 final ItemInfo[][] hotseatItems =
1559 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1560
Adam Cohen2e6da152015-05-06 11:42:25 -07001561 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001562 Log.e(TAG, "Error loading shortcut " + item
1563 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001564 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001565 + ")");
1566 return false;
1567 }
1568
Dan Sandler295ae182013-12-10 16:05:47 -05001569 if (hotseatItems != null) {
1570 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 Log.e(TAG, "Error loading shortcut into hotseat " + item
1572 + " into position (" + item.screenId + ":" + item.cellX + ","
1573 + item.cellY + ") occupied by "
1574 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1575 [(int) item.screenId][0]);
1576 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001577 } else {
1578 hotseatItems[(int) item.screenId][0] = item;
1579 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001581 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001582 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 items[(int) item.screenId][0] = item;
1584 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001585 return true;
1586 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001587 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1588 if (!workspaceScreens.contains((Long) item.screenId)) {
1589 // The item has an invalid screen id.
1590 return false;
1591 }
1592 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001593 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001594 return true;
1595 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001596
Adam Cohendcd297f2013-06-18 13:13:40 -07001597 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001598 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 occupied.put(item.screenId, items);
1600 }
1601
Dan Sandler295ae182013-12-10 16:05:47 -05001602 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001603 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1604 item.cellX < 0 || item.cellY < 0 ||
1605 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1606 Log.e(TAG, "Error loading shortcut " + item
1607 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1608 + item.cellX + "," + item.cellY
1609 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1610 return false;
1611 }
1612
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001613 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001614 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1615 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001616 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001617 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001618 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001619 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001620 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001621 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001622 return false;
1623 }
1624 }
1625 }
1626 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1627 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001628 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001629 }
1630 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001631
Joe Onorato36115782010-06-17 13:28:48 -04001632 return true;
1633 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634
Winson Chungba9c37f2013-08-30 14:11:37 -07001635 /** Clears all the sBg data structures */
1636 private void clearSBgDataStructures() {
1637 synchronized (sBgLock) {
1638 sBgWorkspaceItems.clear();
1639 sBgAppWidgets.clear();
1640 sBgFolders.clear();
1641 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001642 sBgWorkspaceScreens.clear();
1643 }
1644 }
1645
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001646 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001647 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001648
Joe Onorato36115782010-06-17 13:28:48 -04001649 final Context context = mContext;
1650 final ContentResolver contentResolver = context.getContentResolver();
1651 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001652 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001653 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001654 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001655
Winson Chung892c74d2013-08-22 16:15:50 -07001656 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001657 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001658 int countX = profile.numColumns;
1659 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001660
Sunny Goyalf076eae2016-01-11 12:25:10 -08001661 if (GridSizeMigrationTask.ENABLED &&
1662 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1663 // Migration failed. Clear workspace.
1664 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001665 }
1666
Dan Sandlerd5024042014-01-09 15:01:33 -05001667 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001668 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001669 LauncherSettings.Settings.call(contentResolver,
1670 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 }
1672
1673 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1674 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001675 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001676 LauncherSettings.Settings.call(contentResolver,
1677 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001678 } else {
1679 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001680 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001681 LauncherSettings.Settings.call(contentResolver,
1682 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001683 }
Adam Cohene25af792013-06-06 23:08:25 -07001684
Winson Chung2abf94d2012-07-18 18:16:38 -07001685 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001686 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001687 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001688 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001689 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001690
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001691 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1692 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001693 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001694 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001695 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001696
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001697 // +1 for the hotseat (it can be larger than the workspace)
1698 // Load workspace in reverse order to ensure that latest items are loaded first (and
1699 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001700 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001701 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 try {
1704 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1705 final int intentIndex = c.getColumnIndexOrThrow
1706 (LauncherSettings.Favorites.INTENT);
1707 final int titleIndex = c.getColumnIndexOrThrow
1708 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 final int containerIndex = c.getColumnIndexOrThrow(
1710 LauncherSettings.Favorites.CONTAINER);
1711 final int itemTypeIndex = c.getColumnIndexOrThrow(
1712 LauncherSettings.Favorites.ITEM_TYPE);
1713 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1714 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001715 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1716 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001717 final int screenIndex = c.getColumnIndexOrThrow(
1718 LauncherSettings.Favorites.SCREEN);
1719 final int cellXIndex = c.getColumnIndexOrThrow
1720 (LauncherSettings.Favorites.CELLX);
1721 final int cellYIndex = c.getColumnIndexOrThrow
1722 (LauncherSettings.Favorites.CELLY);
1723 final int spanXIndex = c.getColumnIndexOrThrow
1724 (LauncherSettings.Favorites.SPANX);
1725 final int spanYIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001727 final int rankIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001729 final int restoredIndex = c.getColumnIndexOrThrow(
1730 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001731 final int profileIdIndex = c.getColumnIndexOrThrow(
1732 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001733 final int optionsIndex = c.getColumnIndexOrThrow(
1734 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001735 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736
Sunny Goyal7f834d22015-04-21 10:10:23 -07001737 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001738 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001739 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001740 long serialNo = mUserManager.getSerialNumberForUser(user);
1741 allUsers.put(serialNo, user);
1742 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001743 }
1744
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 ShortcutInfo info;
1746 String intentDescription;
1747 LauncherAppWidgetInfo appWidgetInfo;
1748 int container;
1749 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001750 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001752 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001754 while (!mStopped && c.moveToNext()) {
1755 try {
1756 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001757 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001758 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001759 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001760
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 switch (itemType) {
1762 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1763 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001764 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001765 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001766 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001767 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001768 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001769 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001770 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001771 if (user == null) {
1772 // User has been deleted remove the item.
1773 itemsToRemove.add(id);
1774 continue;
1775 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 try {
1777 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001778 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001779 if (cn != null && cn.getPackageName() != null) {
1780 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1781 cn.getPackageName(), user);
1782 boolean validComponent = validPkg &&
1783 launcherApps.isActivityEnabledForProfile(cn, user);
1784
1785 if (validComponent) {
1786 if (restored) {
1787 // no special handling necessary for this item
1788 restoredRows.add(id);
1789 restored = false;
1790 }
Kenny Guy44cba692016-01-21 19:50:02 +00001791 boolean isSuspended = launcherApps.isPackageSuspendedForProfile(
1792 cn.getPackageName(), user);
1793 if (isSuspended) {
1794 disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1795 }
Kenny Guyff05f432016-01-22 17:48:29 +00001796 if (quietMode.get(serialNumber)) {
1797 disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER;
1798 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001799 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001800 intent = null;
1801 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1802 // We allow auto install apps to have their intent
1803 // updated after an install.
1804 intent = manager.getLaunchIntentForPackage(
1805 cn.getPackageName());
1806 if (intent != null) {
1807 ContentValues values = new ContentValues();
1808 values.put(LauncherSettings.Favorites.INTENT,
1809 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001810 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001811 }
1812 }
1813
1814 if (intent == null) {
1815 // The app is installed but the component is no
1816 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001817 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001818 itemsToRemove.add(id);
1819 continue;
1820 } else {
1821 // no special handling necessary for this item
1822 restoredRows.add(id);
1823 restored = false;
1824 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001825 } else if (restored) {
1826 // Package is not yet available but might be
1827 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001828 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001829
1830 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1831 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001832 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001833 // App restore has started. Update the flag
1834 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1835 ContentValues values = new ContentValues();
1836 values.put(LauncherSettings.Favorites.RESTORED,
1837 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001838 updateItem(id, values);
1839 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1840 // This is a common app. Try to replace this.
1841 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1842 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1843 if (parser.findDefaultApp()) {
1844 // Default app found. Replace it.
1845 intent = parser.parsedIntent;
1846 cn = intent.getComponent();
1847 ContentValues values = parser.parsedValues;
1848 values.put(LauncherSettings.Favorites.RESTORED, 0);
1849 updateItem(id, values);
1850 restored = false;
1851 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001852
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001853 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001854 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001855 itemsToRemove.add(id);
1856 continue;
1857 }
Sunny Goyal94485362014-09-18 16:13:58 -07001858 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001859 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001860 itemsToRemove.add(id);
1861 continue;
1862 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001863 } else if (launcherApps.isAppEnabled(
1864 manager, cn.getPackageName(),
1865 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1866 // Package is present but not available.
1867 allowMissingTarget = true;
1868 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1869 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001870 // SdCard is not ready yet. Package might get available,
1871 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001872 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001873 HashSet<String> pkgs = sPendingPackages.get(user);
1874 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001875 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001876 sPendingPackages.put(user, pkgs);
1877 }
1878 pkgs.add(cn.getPackageName());
1879 allowMissingTarget = true;
1880 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001881
1882 } else {
1883 // Do not wait for external media load anymore.
1884 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001885 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001886 itemsToRemove.add(id);
1887 continue;
Winson Chungee055712013-07-30 14:46:24 -07001888 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001889 } else if (cn == null) {
1890 // For shortcuts with no component, keep them as they are
1891 restoredRows.add(id);
1892 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001893 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001895 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001896 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 continue;
1898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899
Sunny Goyal34b65272015-03-11 16:56:52 -07001900 boolean useLowResIcon = container >= 0 &&
1901 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1902
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001903 if (itemReplaced) {
1904 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001905 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001906 cursorIconInfo.iconIndex, titleIndex,
1907 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001908 } else {
1909 // Don't replace items for other profiles.
1910 itemsToRemove.add(id);
1911 continue;
1912 }
1913 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001914 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001915 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001916 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001917 intent = getRestoredItemIntent(c, context, intent);
1918 } else {
1919 // Don't restore items for other profiles.
1920 itemsToRemove.add(id);
1921 continue;
1922 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001923 } else if (itemType ==
1924 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001925 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001926 cursorIconInfo.iconIndex, titleIndex,
1927 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001929 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001930
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 // App shortcuts that used to be automatically added to Launcher
1932 // didn't always have the correct intent flags set, so do that
1933 // here
1934 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001935 intent.getCategories() != null &&
1936 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001937 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001938 intent.addFlags(
1939 Intent.FLAG_ACTIVITY_NEW_TASK |
1940 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1941 }
Michael Jurka96879562012-03-22 05:54:33 -07001942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001943
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001945 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001946 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001949 info.cellX = c.getInt(cellXIndex);
1950 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001951 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001952 info.spanX = 1;
1953 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001954 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001955 if (info.promisedIntent != null) {
1956 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1957 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001958 info.isDisabled = disabledState;
1959 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1960 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1961 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001962
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001964 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001965 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 break;
1967 }
1968
Sunny Goyal756adbc2015-04-16 15:20:51 -07001969 if (restored) {
1970 ComponentName cn = info.getTargetComponent();
1971 if (cn != null) {
1972 Integer progress = installingPkgs.get(cn.getPackageName());
1973 if (progress != null) {
1974 info.setInstallProgress(progress);
1975 } else {
1976 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1977 }
1978 }
1979 }
1980
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 switch (container) {
1982 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1983 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1984 sBgWorkspaceItems.add(info);
1985 break;
1986 default:
1987 // Item is in a user folder
1988 FolderInfo folderInfo =
1989 findOrMakeFolder(sBgFolders, container);
1990 folderInfo.add(info);
1991 break;
1992 }
1993 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001994 } else {
1995 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 }
1997 break;
1998
1999 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2000 id = c.getLong(idIndex);
2001 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2002
Sunny Goyala508e4f2015-05-21 09:33:57 -07002003 // Do not trim the folder label, as is was set by the user.
2004 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002007 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 folderInfo.cellX = c.getInt(cellXIndex);
2009 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002010 folderInfo.spanX = 1;
2011 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002012 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013
Daniel Sandler8802e962010-05-26 16:28:16 -04002014 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002015 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002016 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002017 break;
2018 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002019
Joe Onorato9c1289c2009-08-17 11:03:03 -04002020 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2022 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2023 sBgWorkspaceItems.add(folderInfo);
2024 break;
Joe Onorato36115782010-06-17 13:28:48 -04002025 }
Joe Onorato17a89222011-02-08 17:26:11 -08002026
Chris Wrenf4d08112014-01-16 18:13:56 -05002027 if (restored) {
2028 // no special handling required for restored folders
2029 restoredRows.add(id);
2030 }
2031
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2033 sBgFolders.put(folderInfo.id, folderInfo);
2034 break;
2035
2036 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002037 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002039 boolean customWidget = itemType ==
2040 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002043 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002044 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002045 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002046 user = allUsers.get(serialNumber);
2047 if (user == null) {
2048 itemsToRemove.add(id);
2049 continue;
2050 }
2051
Sunny Goyalff572272014-07-23 13:58:07 -07002052 final ComponentName component =
2053 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002054
Sunny Goyal651077b2014-06-30 14:15:31 -07002055 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002056 final boolean isIdValid = (restoreStatus &
2057 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002058 final boolean wasProviderReady = (restoreStatus &
2059 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002060
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002061 if (widgetProvidersMap == null) {
2062 widgetProvidersMap = AppWidgetManagerCompat
2063 .getInstance(mContext).getAllProvidersMap();
2064 }
2065 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2066 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002067 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002068 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002069
2070 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002071 if (!isSafeMode && !customWidget &&
2072 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002073 addDumpLog("Deleting widget that isn't installed anymore: "
2074 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002075 itemsToRemove.add(id);
2076 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002077 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002078 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2079 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002080
Sunny Goyal53f96722015-07-13 19:54:53 -07002081 // The provider is available. So the widget is either
2082 // available or not available. We do not need to track
2083 // any future restore updates.
2084 int status = restoreStatus &
2085 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002086 if (!wasProviderReady) {
2087 // If provider was not previously ready, update the
2088 // status and UI flag.
2089
2090 // Id would be valid only if the widget restore broadcast was received.
2091 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002092 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002093 } else {
2094 status &= ~LauncherAppWidgetInfo
2095 .FLAG_PROVIDER_NOT_READY;
2096 }
2097 }
2098 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002099 } else {
2100 Log.v(TAG, "Widget restore pending id=" + id
2101 + " appWidgetId=" + appWidgetId
2102 + " status =" + restoreStatus);
2103 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002104 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002105 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002106 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002107
2108 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2109 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002110 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002111 // App restore has started. Update the flag
2112 appWidgetInfo.restoreStatus |=
2113 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002114 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002115 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002116 itemsToRemove.add(id);
2117 continue;
2118 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002119
2120 appWidgetInfo.installProgress =
2121 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002122 }
Sunny Goyalff572272014-07-23 13:58:07 -07002123
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002124 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002125 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 appWidgetInfo.cellX = c.getInt(cellXIndex);
2127 appWidgetInfo.cellY = c.getInt(cellYIndex);
2128 appWidgetInfo.spanX = c.getInt(spanXIndex);
2129 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002130 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002131
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2133 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2134 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002135 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2136 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002137 continue;
2138 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002139
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002140 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002141 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002142 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002143 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 break;
2145 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002146
Adam Cohen59400422014-03-05 18:07:04 -08002147 if (!customWidget) {
2148 String providerName =
2149 appWidgetInfo.providerName.flattenToString();
2150 if (!providerName.equals(savedProvider) ||
2151 (appWidgetInfo.restoreStatus != restoreStatus)) {
2152 ContentValues values = new ContentValues();
2153 values.put(
2154 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2155 providerName);
2156 values.put(LauncherSettings.Favorites.RESTORED,
2157 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002158 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002159 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002160 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2162 sBgAppWidgets.add(appWidgetInfo);
2163 }
Joe Onorato36115782010-06-17 13:28:48 -04002164 break;
2165 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002167 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002168 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002169 }
2170 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002171 if (c != null) {
2172 c.close();
2173 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002174 }
2175
Winson Chungba9c37f2013-08-30 14:11:37 -07002176 // Break early if we've stopped loading
2177 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002178 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002179 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002180 }
2181
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002184 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2185 Utilities.createDbSelectionQuery(
2186 LauncherSettings.Favorites._ID, itemsToRemove), null);
2187 if (DEBUG_LOADERS) {
2188 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2189 LauncherSettings.Favorites._ID, itemsToRemove));
2190 }
2191
2192 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002193 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2194 .call(contentResolver,
2195 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2196 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2197 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002198 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2199 sBgFolders.remove(folderId);
2200 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002201 }
2202 }
2203
Sunny Goyal317698b2015-07-29 11:45:41 -07002204 // Sort all the folder items and make sure the first 3 items are high resolution.
2205 for (FolderInfo folder : sBgFolders) {
2206 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2207 int pos = 0;
2208 for (ShortcutInfo info : folder.contents) {
2209 if (info.usingLowResIcon) {
2210 info.updateIcon(mIconCache, false);
2211 }
2212 pos ++;
2213 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2214 break;
2215 }
2216 }
2217 }
2218
Chris Wrenf4d08112014-01-16 18:13:56 -05002219 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002220 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002221 ContentValues values = new ContentValues();
2222 values.put(LauncherSettings.Favorites.RESTORED, 0);
2223 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2224 Utilities.createDbSelectionQuery(
2225 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002226 }
2227
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002228 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2229 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002230 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002231 null, sWorker);
2232 }
2233
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002234 // Remove any empty screens
2235 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002236 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002237 long screenId = item.screenId;
2238 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2239 unusedScreens.contains(screenId)) {
2240 unusedScreens.remove(screenId);
2241 }
2242 }
2243
2244 // If there are any empty screens remove them, and update.
2245 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002246 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 }
2249
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 if (DEBUG_LOADERS) {
2251 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2252 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002254 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002256
Sunny Goyale2df0622015-04-24 11:27:00 -07002257 for (int i = 0; i < nScreens; i++) {
2258 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002259 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 line += " | ";
2261 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002262 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002263 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002264 if (x < screen.length && y < screen[x].length) {
2265 line += (screen[x][y] != null) ? "#" : ".";
2266 } else {
2267 line += "!";
2268 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 }
Joe Onorato36115782010-06-17 13:28:48 -04002270 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002271 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002272 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 }
Joe Onorato36115782010-06-17 13:28:48 -04002274 }
Adam Cohene25af792013-06-06 23:08:25 -07002275 }
2276
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002277 /**
2278 * Partially updates the item without any notification. Must be called on the worker thread.
2279 */
2280 private void updateItem(long itemId, ContentValues update) {
2281 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002282 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002283 update,
2284 BaseColumns._ID + "= ?",
2285 new String[]{Long.toString(itemId)});
2286 }
2287
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288 /** Filters the set of items who are directly or indirectly (via another container) on the
2289 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002290 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 ArrayList<ItemInfo> allWorkspaceItems,
2292 ArrayList<ItemInfo> currentScreenItems,
2293 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002294 // Purge any null ItemInfos
2295 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2296 while (iter.hasNext()) {
2297 ItemInfo i = iter.next();
2298 if (i == null) {
2299 iter.remove();
2300 }
2301 }
2302
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002303 // Order the set of items by their containers first, this allows use to walk through the
2304 // list sequentially, build up a list of containers that are in the specified screen,
2305 // as well as all items in those containers.
2306 Set<Long> itemsOnScreen = new HashSet<Long>();
2307 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2308 @Override
2309 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002310 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 }
2312 });
2313 for (ItemInfo info : allWorkspaceItems) {
2314 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002315 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 currentScreenItems.add(info);
2317 itemsOnScreen.add(info.id);
2318 } else {
2319 otherScreenItems.add(info);
2320 }
2321 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2322 currentScreenItems.add(info);
2323 itemsOnScreen.add(info.id);
2324 } else {
2325 if (itemsOnScreen.contains(info.container)) {
2326 currentScreenItems.add(info);
2327 itemsOnScreen.add(info.id);
2328 } else {
2329 otherScreenItems.add(info);
2330 }
2331 }
2332 }
2333 }
2334
2335 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002336 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 ArrayList<LauncherAppWidgetInfo> appWidgets,
2338 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2339 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340
2341 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002342 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002344 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 currentScreenWidgets.add(widget);
2346 } else {
2347 otherScreenWidgets.add(widget);
2348 }
2349 }
2350 }
2351
2352 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002353 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002354 LongArrayMap<ItemInfo> itemsIdMap,
2355 LongArrayMap<FolderInfo> folders,
2356 LongArrayMap<FolderInfo> currentScreenFolders,
2357 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358
Sunny Goyale2df0622015-04-24 11:27:00 -07002359 int total = folders.size();
2360 for (int i = 0; i < total; i++) {
2361 long id = folders.keyAt(i);
2362 FolderInfo folder = folders.valueAt(i);
2363
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002365 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002367 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 currentScreenFolders.put(id, folder);
2369 } else {
2370 otherScreenFolders.put(id, folder);
2371 }
2372 }
2373 }
2374
2375 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2376 * right) */
2377 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002378 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002379 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002380 final int screenCols = profile.numColumns;
2381 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002383 @Override
2384 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002385 if (lhs.container == rhs.container) {
2386 // Within containers, order by their spatial position in that container
2387 switch ((int) lhs.container) {
2388 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2389 long lr = (lhs.screenId * screenCellCount +
2390 lhs.cellY * screenCols + lhs.cellX);
2391 long rr = (rhs.screenId * screenCellCount +
2392 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002393 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002394 }
2395 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2396 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002397 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002398 }
2399 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002400 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002401 throw new RuntimeException("Unexpected container type when " +
2402 "sorting workspace items.");
2403 }
2404 return 0;
2405 }
2406 } else {
2407 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002408 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002409 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002410 }
2411 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002413
Adam Cohendcd297f2013-06-18 13:13:40 -07002414 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2415 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002416 final Runnable r = new Runnable() {
2417 @Override
2418 public void run() {
2419 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2420 if (callbacks != null) {
2421 callbacks.bindScreens(orderedScreens);
2422 }
2423 }
2424 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002425 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002426 }
2427
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2429 final ArrayList<ItemInfo> workspaceItems,
2430 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002431 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002432 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433
2434 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002435 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002437 final int start = i;
2438 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439 final Runnable r = new Runnable() {
2440 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002441 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002442 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002443 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002444 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2445 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002446 }
2447 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002449 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451
2452 // Bind the folders
2453 if (!folders.isEmpty()) {
2454 final Runnable r = new Runnable() {
2455 public void run() {
2456 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2457 if (callbacks != null) {
2458 callbacks.bindFolders(folders);
2459 }
2460 }
2461 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002462 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002463 }
2464
2465 // Bind the widgets, one at a time
2466 N = appWidgets.size();
2467 for (int i = 0; i < N; i++) {
2468 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2469 final Runnable r = new Runnable() {
2470 public void run() {
2471 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2472 if (callbacks != null) {
2473 callbacks.bindAppWidget(widget);
2474 }
2475 }
2476 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002477 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002478 }
2479 }
2480
2481 /**
2482 * Binds all loaded data to actual views on the main thread.
2483 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002484 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 final long t = SystemClock.uptimeMillis();
2486 Runnable r;
2487
2488 // Don't use these two variables in any of the callback runnables.
2489 // Otherwise we hold a reference to them.
2490 final Callbacks oldCallbacks = mCallbacks.get();
2491 if (oldCallbacks == null) {
2492 // This launcher has exited and nobody bothered to tell us. Just bail.
2493 Log.w(TAG, "LoaderTask running with no launcher");
2494 return;
2495 }
2496
Winson Chung9b9fb962013-11-15 15:39:34 -08002497 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2499 ArrayList<LauncherAppWidgetInfo> appWidgets =
2500 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002501 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002502
2503 final LongArrayMap<FolderInfo> folders;
2504 final LongArrayMap<ItemInfo> itemsIdMap;
2505
Winson Chung2abf94d2012-07-18 18:16:38 -07002506 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 workspaceItems.addAll(sBgWorkspaceItems);
2508 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002509 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002510
2511 folders = sBgFolders.clone();
2512 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 }
2514
Derek Prothro7aff3992013-12-10 14:00:37 -05002515 final boolean isLoadingSynchronously =
2516 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002517 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002518 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002519 if (currScreen >= orderedScreenIds.size()) {
2520 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002521 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002522 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002523 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002524 final long currentScreenId = currentScreen < 0
2525 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002526
2527 // Load all the items that are on the current page first (and in the process, unbind
2528 // all the existing workspace items before we call startBinding() below.
2529 unbindWorkspaceItemsOnMainThread();
2530
2531 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002532 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2533 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2534 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2535 new ArrayList<LauncherAppWidgetInfo>();
2536 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2537 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002538 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2539 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540
Winson Chung9b9fb962013-11-15 15:39:34 -08002541 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002543 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002545 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546 otherFolders);
2547 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2548 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2549
2550 // Tell the workspace that we're about to start binding items
2551 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002552 public void run() {
2553 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2554 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002555 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002557 }
2558 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002560 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561
Adam Cohendcd297f2013-06-18 13:13:40 -07002562 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2563
Sunny Goyal527c7d32015-08-28 15:19:36 -07002564 Executor mainExecutor = new DeferredMainThreadExecutor();
2565 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002567 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002568
Sunny Goyal527c7d32015-08-28 15:19:36 -07002569 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2570 // remaining screens after first onDraw is called. This ensures that the first screen
2571 // is immediately visible (eg. during rotation)
2572 // In case of !isLoadingSynchronously, bind all pages one after other.
2573 final Executor deferredExecutor = isLoadingSynchronously ?
2574 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2575
2576 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2577 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002578
2579 // Tell the workspace that we're done binding items
2580 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002581 public void run() {
2582 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2583 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002584 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002585 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002586
Sunny Goyal639e9062015-08-19 19:17:06 -07002587 mIsLoadingAndBindingWorkspace = false;
2588
2589 // Run all the bind complete runnables after workspace is bound.
2590 if (!mBindCompleteRunnables.isEmpty()) {
2591 synchronized (mBindCompleteRunnables) {
2592 for (final Runnable r : mBindCompleteRunnables) {
2593 runOnWorkerThread(r);
2594 }
2595 mBindCompleteRunnables.clear();
2596 }
2597 }
2598
Winson Chung98e030b2012-05-07 16:01:11 -07002599 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002600 if (DEBUG_LOADERS) {
2601 Log.d(TAG, "bound workspace in "
2602 + (SystemClock.uptimeMillis()-t) + "ms");
2603 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002604
Joe Onorato36115782010-06-17 13:28:48 -04002605 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002606 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002607 deferredExecutor.execute(r);
2608
Winson Chung4a2afa32012-07-19 14:53:05 -07002609 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002610 r = new Runnable() {
2611 public void run() {
2612 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2613 if (callbacks != null) {
2614 // We are loading synchronously, which means, some of the pages will be
2615 // bound after first draw. Inform the callbacks that page binding is
2616 // not complete, and schedule the remaining pages.
2617 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2618 callbacks.onPageBoundSynchronously(currentScreen);
2619 }
2620 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2621 }
2622 }
2623 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002624 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002625 }
Joe Onorato36115782010-06-17 13:28:48 -04002626 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002627
Joe Onorato36115782010-06-17 13:28:48 -04002628 private void loadAndBindAllApps() {
2629 if (DEBUG_LOADERS) {
2630 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2631 }
2632 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002633 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002634 synchronized (LoaderTask.this) {
2635 if (mStopped) {
2636 return;
2637 }
2638 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002639 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002640 synchronized (LoaderTask.this) {
2641 if (mStopped) {
2642 return;
2643 }
2644 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002645 }
Joe Onorato36115782010-06-17 13:28:48 -04002646 } else {
2647 onlyBindAllApps();
2648 }
2649 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002650
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002651 private void updateIconCache() {
2652 // Ignore packages which have a promise icon.
2653 HashSet<String> packagesToIgnore = new HashSet<>();
2654 synchronized (sBgLock) {
2655 for (ItemInfo info : sBgItemsIdMap) {
2656 if (info instanceof ShortcutInfo) {
2657 ShortcutInfo si = (ShortcutInfo) info;
2658 if (si.isPromise() && si.getTargetComponent() != null) {
2659 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2660 }
2661 } else if (info instanceof LauncherAppWidgetInfo) {
2662 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2663 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2664 packagesToIgnore.add(lawi.providerName.getPackageName());
2665 }
2666 }
2667 }
2668 }
2669 mIconCache.updateDbIcons(packagesToIgnore);
2670 }
2671
Joe Onorato36115782010-06-17 13:28:48 -04002672 private void onlyBindAllApps() {
2673 final Callbacks oldCallbacks = mCallbacks.get();
2674 if (oldCallbacks == null) {
2675 // This launcher has exited and nobody bothered to tell us. Just bail.
2676 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2677 return;
2678 }
2679
2680 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002681 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002682 final ArrayList<AppInfo> list
2683 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002684 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002685 public void run() {
2686 final long t = SystemClock.uptimeMillis();
2687 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2688 if (callbacks != null) {
2689 callbacks.bindAllApplications(list);
2690 }
2691 if (DEBUG_LOADERS) {
2692 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002693 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002694 }
2695 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002696 };
2697 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002698 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002699 r.run();
2700 } else {
2701 mHandler.post(r);
2702 }
Joe Onorato36115782010-06-17 13:28:48 -04002703 }
2704
Winson Chung64359a52013-07-08 17:17:08 -07002705 private void loadAllApps() {
2706 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002707
Joe Onorato36115782010-06-17 13:28:48 -04002708 final Callbacks oldCallbacks = mCallbacks.get();
2709 if (oldCallbacks == null) {
2710 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002711 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002712 return;
2713 }
2714
Kenny Guyed131872014-04-30 03:02:21 +01002715 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2716
Winson Chung64359a52013-07-08 17:17:08 -07002717 // Clear the list of apps
2718 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002719 for (UserHandleCompat user : profiles) {
2720 // Query for the set of apps
2721 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002722 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002723 if (DEBUG_LOADERS) {
2724 Log.d(TAG, "getActivityList took "
2725 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2726 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2727 }
2728 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002729 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002730 if (apps == null || apps.isEmpty()) {
2731 return;
2732 }
Kenny Guyff05f432016-01-22 17:48:29 +00002733 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002734 // Create the ApplicationInfos
2735 for (int i = 0; i < apps.size(); i++) {
2736 LauncherActivityInfoCompat app = apps.get(i);
2737 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002738 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002739 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002740
Sunny Goyal756a28a2015-04-23 17:07:55 -07002741 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2742 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002743 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002744
2745 @Override
2746 public void run() {
2747 heuristic.processUserApps(apps);
2748 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002749 };
2750 runOnMainThread(new Runnable() {
2751
2752 @Override
2753 public void run() {
2754 // Check isLoadingWorkspace on the UI thread, as it is updated on
2755 // the UI thread.
2756 if (mIsLoadingAndBindingWorkspace) {
2757 synchronized (mBindCompleteRunnables) {
2758 mBindCompleteRunnables.add(r);
2759 }
2760 } else {
2761 runOnWorkerThread(r);
2762 }
2763 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002764 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002765 }
Winson Chung64359a52013-07-08 17:17:08 -07002766 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002767 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002768 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2769 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002770
2771 // Post callback on main thread
2772 mHandler.post(new Runnable() {
2773 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002774
Winson Chung64359a52013-07-08 17:17:08 -07002775 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002776 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002777 if (callbacks != null) {
2778 callbacks.bindAllApplications(added);
2779 if (DEBUG_LOADERS) {
2780 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002781 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002782 }
2783 } else {
2784 Log.i(TAG, "not binding apps: no Launcher activity");
2785 }
2786 }
2787 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002788 // Cleanup any data stored for a deleted user.
2789 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002790 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002791 Log.d(TAG, "Icons processed in "
2792 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002793 }
2794 }
2795
2796 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002797 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002798 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002799 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2800 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2801 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2802 }
Joe Onorato36115782010-06-17 13:28:48 -04002803 }
2804 }
2805
Sunny Goyal75b0f552015-05-20 21:57:06 -07002806 /**
2807 * Called when the icons for packages have been updated in the icon cache.
2808 */
2809 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2810 final Callbacks callbacks = getCallback();
2811 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2812 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2813
2814 // If any package icon has changed (app was updated while launcher was dead),
2815 // update the corresponding shortcuts.
2816 synchronized (sBgLock) {
2817 for (ItemInfo info : sBgItemsIdMap) {
2818 if (info instanceof ShortcutInfo && user.equals(info.user)
2819 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2820 ShortcutInfo si = (ShortcutInfo) info;
2821 ComponentName cn = si.getTargetComponent();
2822 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2823 si.updateIcon(mIconCache);
2824 updatedShortcuts.add(si);
2825 }
2826 }
2827 }
2828 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2829 }
2830
2831 if (!updatedShortcuts.isEmpty()) {
2832 final UserHandleCompat userFinal = user;
2833 mHandler.post(new Runnable() {
2834
2835 public void run() {
2836 Callbacks cb = getCallback();
2837 if (cb != null && callbacks == cb) {
2838 cb.bindShortcutsChanged(updatedShortcuts,
2839 new ArrayList<ShortcutInfo>(), userFinal);
2840 }
2841 }
2842 });
2843 }
2844
2845 if (!updatedApps.isEmpty()) {
2846 mHandler.post(new Runnable() {
2847
2848 public void run() {
2849 Callbacks cb = getCallback();
2850 if (cb != null && callbacks == cb) {
2851 cb.bindAppsUpdated(updatedApps);
2852 }
2853 }
2854 });
2855 }
2856 }
2857
Joe Onorato36115782010-06-17 13:28:48 -04002858 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002859 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002860 }
2861
Adam Cohen091440a2015-03-18 14:16:05 -07002862 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002863
2864 @Override
2865 public void onReceive(Context context, Intent intent) {
2866 synchronized (sBgLock) {
2867 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2868 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002869 final PackageManager manager = context.getPackageManager();
2870 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2871 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002872 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2873 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002874 packagesRemoved.clear();
2875 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002876 for (String pkg : entry.getValue()) {
2877 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002878 boolean packageOnSdcard = launcherApps.isAppEnabled(
2879 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2880 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002881 packagesUnavailable.add(pkg);
2882 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002883 packagesRemoved.add(pkg);
2884 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002885 }
2886 }
2887 if (!packagesRemoved.isEmpty()) {
2888 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2889 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2890 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002891 if (!packagesUnavailable.isEmpty()) {
2892 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2893 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2894 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002895 }
Sunny Goyal34942622014-08-29 17:20:55 -07002896 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002897 }
2898 }
2899 }
2900
Joe Onorato36115782010-06-17 13:28:48 -04002901 private class PackageUpdatedTask implements Runnable {
2902 int mOp;
2903 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002904 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002905
2906 public static final int OP_NONE = 0;
2907 public static final int OP_ADD = 1;
2908 public static final int OP_UPDATE = 2;
2909 public static final int OP_REMOVE = 3; // uninstlled
2910 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002911 public static final int OP_SUSPEND = 5; // package suspended
2912 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002913 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002914
Kenny Guyed131872014-04-30 03:02:21 +01002915 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002916 mOp = op;
2917 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002918 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002919 }
2920
2921 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002922 if (!mHasLoaderCompletedOnce) {
2923 // Loader has not yet run.
2924 return;
2925 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002926 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002927
2928 final String[] packages = mPackages;
2929 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002930 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002931 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002932 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002933 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002934 for (int i=0; i<N; i++) {
2935 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002936 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002937 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002938 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002939
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002940 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2941 if (heuristic != null) {
2942 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002943 }
Joe Onorato36115782010-06-17 13:28:48 -04002944 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002945 }
Joe Onorato36115782010-06-17 13:28:48 -04002946 case OP_UPDATE:
2947 for (int i=0; i<N; i++) {
2948 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002949 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002950 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002951 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002952 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002953 // Since package was just updated, the target must be available now.
2954 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002955 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002956 case OP_REMOVE: {
2957 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2958 if (heuristic != null) {
2959 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002960 }
Joe Onorato36115782010-06-17 13:28:48 -04002961 for (int i=0; i<N; i++) {
2962 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002963 mIconCache.removeIconsForPkg(packages[i], mUser);
2964 }
2965 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002966 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002967 case OP_UNAVAILABLE:
2968 for (int i=0; i<N; i++) {
2969 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2970 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002971 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002972 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002973 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002974 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002975 case OP_SUSPEND:
2976 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002977 flagOp = mOp == OP_SUSPEND ?
2978 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2979 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002980 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002981 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2982 break;
2983 case OP_USER_AVAILABILITY_CHANGE:
2984 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2985 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2986 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2987 // We want to update all packages for this user.
2988 pkgFilter = StringFilter.matchesAll();
2989 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002990 break;
Joe Onorato36115782010-06-17 13:28:48 -04002991 }
2992
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002993 ArrayList<AppInfo> added = null;
2994 ArrayList<AppInfo> modified = null;
2995 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002996
Adam Cohen487f7dd2012-06-28 18:12:10 -07002997 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002998 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002999 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003000 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003001 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003002 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003003 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003004 }
Winson Chung5d55f332012-07-16 20:45:03 -07003005 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003006 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003007 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003008 }
3009
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003010 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003011
Joe Onorato36115782010-06-17 13:28:48 -04003012 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003013 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003014 for (AppInfo ai : added) {
3015 addedOrUpdatedApps.put(ai.componentName, ai);
3016 }
Joe Onorato36115782010-06-17 13:28:48 -04003017 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003018
Joe Onorato36115782010-06-17 13:28:48 -04003019 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003020 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003021 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003022 for (AppInfo ai : modified) {
3023 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003024 }
3025
Joe Onorato36115782010-06-17 13:28:48 -04003026 mHandler.post(new Runnable() {
3027 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003028 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003029 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003030 callbacks.bindAppsUpdated(modifiedFinal);
3031 }
3032 }
3033 });
3034 }
Winson Chung83892cc2013-05-01 16:53:33 -07003035
Sunny Goyal4390ace2014-10-13 11:33:11 -07003036 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003037 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003038 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3039 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3040 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3041
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003042 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003043 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003044 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3045 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003046 boolean infoUpdated = false;
3047 boolean shortcutUpdated = false;
3048
3049 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003050 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003051 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003052 Bitmap icon = Utilities.createIconBitmap(
3053 si.iconResource.packageName,
3054 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003055 if (icon != null) {
3056 si.setIcon(icon);
3057 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003058 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003059 }
3060 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061
3062 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003063 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003064 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3065
3066 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003067 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3068 // Auto install icon
3069 PackageManager pm = context.getPackageManager();
3070 ResolveInfo matched = pm.resolveActivity(
3071 new Intent(Intent.ACTION_MAIN)
3072 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3073 PackageManager.MATCH_DEFAULT_ONLY);
3074 if (matched == null) {
3075 // Try to find the best match activity.
3076 Intent intent = pm.getLaunchIntentForPackage(
3077 cn.getPackageName());
3078 if (intent != null) {
3079 cn = intent.getComponent();
3080 appInfo = addedOrUpdatedApps.get(cn);
3081 }
3082
3083 if ((intent == null) || (appInfo == null)) {
3084 removedShortcuts.add(si);
3085 continue;
3086 }
3087 si.promisedIntent = intent;
3088 }
3089 }
3090
3091 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003092 if (appInfo != null) {
3093 si.flags = appInfo.flags;
3094 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095
Sunny Goyal756adbc2015-04-16 15:20:51 -07003096 si.intent = si.promisedIntent;
3097 si.promisedIntent = null;
3098 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003099 infoUpdated = true;
3100 si.updateIcon(mIconCache);
3101 }
3102
3103 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3104 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3105 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003106 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003107 si.contentDescription = appInfo.contentDescription;
3108 infoUpdated = true;
3109 }
3110
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003111 int oldDisabledFlags = si.isDisabled;
3112 si.isDisabled = flagOp.apply(si.isDisabled);
3113 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003114 shortcutUpdated = true;
3115 }
3116 }
3117
3118 if (infoUpdated || shortcutUpdated) {
3119 updatedShortcuts.add(si);
3120 }
3121 if (infoUpdated) {
3122 updateItemInDatabase(context, si);
3123 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003124 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3126 if (mUser.equals(widgetInfo.user)
3127 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003128 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003129 widgetInfo.restoreStatus &=
3130 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3131 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003132
3133 // adding this flag ensures that launcher shows 'click to setup'
3134 // if the widget has a config activity. In case there is no config
3135 // activity, it will be marked as 'restored' during bind.
3136 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3137
Sunny Goyal4390ace2014-10-13 11:33:11 -07003138 widgets.add(widgetInfo);
3139 updateItemInDatabase(context, widgetInfo);
3140 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003141 }
3142 }
3143 }
3144
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003146 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003147 mHandler.post(new Runnable() {
3148
3149 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003150 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003151 if (callbacks == cb && cb != null) {
3152 callbacks.bindShortcutsChanged(
3153 updatedShortcuts, removedShortcuts, mUser);
3154 }
3155 }
3156 });
3157 if (!removedShortcuts.isEmpty()) {
3158 deleteItemsFromDatabase(context, removedShortcuts);
3159 }
3160 }
3161 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003162 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003163 mHandler.post(new Runnable() {
3164 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003165 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003166 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003167 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003168 }
3169 }
3170 });
3171 }
3172 }
3173
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003174 final HashSet<String> removedPackages = new HashSet<>();
3175 final HashSet<ComponentName> removedComponents = new HashSet<>();
3176 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003177 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003178 Collections.addAll(removedPackages, packages);
3179
3180 // No need to update the removedComponents as
3181 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003182 } else if (mOp == OP_UPDATE) {
3183 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003184 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003185 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003186 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003187 }
3188 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003189
3190 // Update removedComponents as some components can get removed during package update
3191 for (AppInfo info : removedApps) {
3192 removedComponents.add(info.componentName);
3193 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003194 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003195
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003196 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3197 for (String pn : removedPackages) {
3198 deletePackageFromDatabase(context, pn, mUser);
3199 }
3200 for (ComponentName cn : removedComponents) {
3201 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003202 }
3203
Winson Chungdf95eb12013-10-16 14:57:07 -07003204 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003205 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3206
Winson Chungdf95eb12013-10-16 14:57:07 -07003207 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003208 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003209 mHandler.post(new Runnable() {
3210 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003211 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003212 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003213 callbacks.bindWorkspaceComponentsRemoved(
3214 removedPackages, removedComponents, mUser);
3215 }
3216 }
3217 });
3218 }
3219
3220 if (!removedApps.isEmpty()) {
3221 // Remove corresponding apps from All-Apps
3222 final Callbacks callbacks = getCallback();
3223 mHandler.post(new Runnable() {
3224 public void run() {
3225 Callbacks cb = getCallback();
3226 if (callbacks == cb && cb != null) {
3227 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003228 }
3229 }
3230 });
Joe Onoratobe386092009-11-17 17:32:16 -08003231 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003232
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003233 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3234 // get widget update signals.
3235 if (!Utilities.ATLEAST_MARSHMALLOW &&
3236 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003237 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003238 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003239 public void run() {
3240 Callbacks cb = getCallback();
3241 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003242 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003243 }
3244 }
3245 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003246 }
3247 }
3248 }
3249
3250 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3251 mHandler.post(new Runnable() {
3252 @Override
3253 public void run() {
3254 Callbacks cb = getCallback();
3255 if (callbacks == cb && cb != null) {
3256 callbacks.bindWidgetsModel(model);
3257 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003258 }
3259 });
3260 }
3261
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003262 public void refreshAndBindWidgetsAndShortcuts(
3263 final Callbacks callbacks, final boolean bindFirst) {
3264 runOnWorkerThread(new Runnable() {
3265 @Override
3266 public void run() {
3267 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3268 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003269 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003270 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3271 bindWidgetsModel(callbacks, model);
3272 // update the Widget entries inside DB on the worker thread.
3273 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3274 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003275 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003276 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003277 }
3278
Adam Cohen091440a2015-03-18 14:16:05 -07003279 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003280 UserHandleCompat user) {
3281 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3282 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003283 }
Adam Cohen556f6132014-01-15 15:18:08 -08003284
Kenny Guyed131872014-04-30 03:02:21 +01003285 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3286 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003287 if (cn == null) {
3288 return false;
3289 }
Kenny Guyed131872014-04-30 03:02:21 +01003290 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3291 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003292 return false;
3293 }
Kenny Guyed131872014-04-30 03:02:21 +01003294 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003295 }
3296
Adam Cohena28b78e2014-05-20 17:03:04 -07003297 public static boolean isValidPackage(Context context, String packageName,
3298 UserHandleCompat user) {
3299 if (packageName == null) {
3300 return false;
3301 }
3302 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3303 return launcherApps.isPackageEnabledForProfile(packageName, user);
3304 }
3305
Joe Onorato9c1289c2009-08-17 11:03:03 -04003306 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003307 * Make an ShortcutInfo object for a restored application or shortcut item that points
3308 * to a package that is not yet installed on the system.
3309 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003310 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003311 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003312 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003313 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003314
3315 Bitmap icon = iconInfo.loadIcon(c, info, context);
3316 // the fallback icon
3317 if (icon == null) {
3318 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3319 } else {
3320 info.setIcon(icon);
3321 }
Sunny Goyal34942622014-08-29 17:20:55 -07003322
3323 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003324 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003325 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003326 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003327 }
Sunny Goyal34942622014-08-29 17:20:55 -07003328 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3329 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003330 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003331 }
Sunny Goyal34942622014-08-29 17:20:55 -07003332 } else {
3333 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3334 }
3335
Winson Chung82b016c2015-05-08 17:00:10 -07003336 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003337 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003338 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003339 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003340 return info;
3341 }
3342
3343 /**
3344 * Make an Intent object for a restored application or shortcut item that points
3345 * to the market page for the item.
3346 */
Adam Cohen091440a2015-03-18 14:16:05 -07003347 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003348 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003349 return getMarketIntent(componentName.getPackageName());
3350 }
3351
3352 static Intent getMarketIntent(String packageName) {
3353 return new Intent(Intent.ACTION_VIEW)
3354 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003355 .scheme("market")
3356 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003357 .appendQueryParameter("id", packageName)
3358 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003359 }
3360
3361 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003362 * Make an ShortcutInfo object for a shortcut that is an application.
3363 *
3364 * If c is not null, then it will be used to fill in missing data like the title and icon.
3365 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003366 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003367 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003368 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003369 if (user == null) {
3370 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003371 return null;
3372 }
3373
Kenny Guyed131872014-04-30 03:02:21 +01003374 ComponentName componentName = intent.getComponent();
3375 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003376 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003377 return null;
3378 }
3379
3380 Intent newIntent = new Intent(intent.getAction(), null);
3381 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3382 newIntent.setComponent(componentName);
3383 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003384 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003385 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3386 return null;
3387 }
3388
3389 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003390 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003391 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3392 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3393 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003394 }
3395
Joe Onorato56d82912010-03-07 14:32:10 -05003396 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003397 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003398 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003399 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003400
Joe Onorato56d82912010-03-07 14:32:10 -05003401 // fall back to the class name of the activity
3402 if (info.title == null) {
3403 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003404 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003405
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003407 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003408 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003409 if (lai != null) {
3410 info.flags = AppInfo.initFlags(lai);
3411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003413 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003414
Sunny Goyale2df0622015-04-24 11:27:00 -07003415 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003416 ItemInfoFilter f) {
3417 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3418 for (ItemInfo i : infos) {
3419 if (i instanceof ShortcutInfo) {
3420 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003421 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003422 if (cn != null && f.filterItem(null, info, cn)) {
3423 filtered.add(info);
3424 }
3425 } else if (i instanceof FolderInfo) {
3426 FolderInfo info = (FolderInfo) i;
3427 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003428 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003429 if (cn != null && f.filterItem(info, s, cn)) {
3430 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003431 }
3432 }
Winson Chung64359a52013-07-08 17:17:08 -07003433 } else if (i instanceof LauncherAppWidgetInfo) {
3434 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3435 ComponentName cn = info.providerName;
3436 if (cn != null && f.filterItem(null, info, cn)) {
3437 filtered.add(info);
3438 }
Winson Chung8a435102012-08-30 17:16:53 -07003439 }
3440 }
Winson Chung64359a52013-07-08 17:17:08 -07003441 return new ArrayList<ItemInfo>(filtered);
3442 }
3443
Adam Cohen091440a2015-03-18 14:16:05 -07003444 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003445 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003446 ItemInfoFilter filter = new ItemInfoFilter() {
3447 @Override
3448 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003449 if (info.user == null) {
3450 return cn.equals(cname);
3451 } else {
3452 return cn.equals(cname) && info.user.equals(user);
3453 }
Winson Chung64359a52013-07-08 17:17:08 -07003454 }
3455 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003456 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003457 }
3458
Sunny Goyal1a745e82014-10-02 15:58:31 -07003459 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003460 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003461 */
Adam Cohen091440a2015-03-18 14:16:05 -07003462 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003463 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003464 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003465 // Non-app shortcuts are only supported for current user.
3466 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003468
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003469 // TODO: If there's an explicit component and we can't install that, delete it.
3470
Winson Chung82b016c2015-05-08 17:00:10 -07003471 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003472
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003473 Bitmap icon = iconInfo.loadIcon(c, info, context);
3474 // the fallback icon
3475 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003476 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003477 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003478 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003479 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 return info;
3481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003482
Sunny Goyal2350bc92014-10-14 16:42:54 -07003483 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003484 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3485 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3486 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3487
Adam Cohend9198822011-11-22 16:42:47 -08003488 if (intent == null) {
3489 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3490 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3491 return null;
3492 }
3493
Joe Onorato0589f0f2010-02-08 13:44:00 -08003494 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003495 boolean customIcon = false;
3496 ShortcutIconResource iconResource = null;
3497
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003498 if (bitmap instanceof Bitmap) {
3499 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003500 customIcon = true;
3501 } else {
3502 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003503 if (extra instanceof ShortcutIconResource) {
3504 iconResource = (ShortcutIconResource) extra;
3505 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003506 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003507 }
3508 }
3509
Michael Jurkac9d95c52011-08-29 14:03:34 -07003510 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003511
Kenny Guyed131872014-04-30 03:02:21 +01003512 // Only support intents for current user for now. Intents sent from other
3513 // users wouldn't get here without intent forwarding anyway.
3514 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003515 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003516 icon = mIconCache.getDefaultIcon(info.user);
3517 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003518 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003519 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003520
Winson Chung82b016c2015-05-08 17:00:10 -07003521 info.title = Utilities.trim(name);
3522 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003523 info.intent = intent;
3524 info.customIcon = customIcon;
3525 info.iconResource = iconResource;
3526
3527 return info;
3528 }
3529
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003531 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 * or make a new one.
3533 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003534 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 // See if a placeholder was created for us already
3536 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003537 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003539 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 folders.put(id, folderInfo);
3541 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003542 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 }
3544
Joe Onoratobe386092009-11-17 17:32:16 -08003545
Sunny Goyal651077b2014-06-30 14:15:31 -07003546 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3547 return (provider != null) && (provider.provider != null)
3548 && (provider.provider.getPackageName() != null);
3549 }
3550
Joe Onoratobe386092009-11-17 17:32:16 -08003551 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003552 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003553 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3554 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3555 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3556 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003557 if (mLoaderTask != null) {
3558 mLoaderTask.dumpState();
3559 } else {
3560 Log.d(TAG, "mLoaderTask=null");
3561 }
Joe Onoratobe386092009-11-17 17:32:16 -08003562 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003563
3564 public Callbacks getCallback() {
3565 return mCallbacks != null ? mCallbacks.get() : null;
3566 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003567
3568 /**
3569 * @return {@link FolderInfo} if its already loaded.
3570 */
3571 public FolderInfo findFolderById(Long folderId) {
3572 synchronized (sBgLock) {
3573 return sBgFolders.get(folderId);
3574 }
3575 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003576
Sunny Goyal527c7d32015-08-28 15:19:36 -07003577 @Thunk class DeferredMainThreadExecutor implements Executor {
3578
3579 @Override
3580 public void execute(Runnable command) {
3581 runOnMainThread(command);
3582 }
3583 }
3584
Sunny Goyal756adbc2015-04-16 15:20:51 -07003585 /**
3586 * @return the looper for the worker thread which can be used to start background tasks.
3587 */
3588 public static Looper getWorkerLooper() {
3589 return sWorkerThread.getLooper();
3590 }
Sunny Goyala1365452015-10-01 15:46:24 -07003591
3592 @Thunk static final void addDumpLog(String log) {
3593 Launcher.addDumpLog(TAG, log);
3594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003595}