blob: 4ad4695f3b4d97f37dcf96f11d93b61b709802ea [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Sunny Goyal2d648b02015-08-11 13:56:28 -070020import android.appwidget.AppWidgetManager;
Romain Guy629de3e2010-01-13 12:20:59 -080021import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070024import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.net.Uri;
Sunny Goyal3dc7bee2015-09-15 11:32:58 -070037import android.os.DeadObjectException;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070041import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070045import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040046import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080047import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070050import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010051
Sunny Goyalffe83f12014-08-14 17:39:34 -070052import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.LauncherActivityInfoCompat;
54import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070055import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070056import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010057import com.android.launcher3.compat.UserHandleCompat;
58import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070059import com.android.launcher3.config.ProviderConfig;
Sunny Goyale5bb7052015-07-27 14:36:07 -070060import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070063import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070064import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070065import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070066import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070067import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080068
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.lang.ref.WeakReference;
70import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070071import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070073import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070074import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070075import java.util.Collections;
76import java.util.Comparator;
77import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070079import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070081import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070083import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085/**
86 * Maintains in-memory state of the Launcher. It is expected that there should be only one
87 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070088 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 */
Kenny Guyed131872014-04-30 03:02:21 +010090public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010091 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080092 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040093 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070094 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070097
Dan Sandlerd5024042014-01-09 15:01:33 -050098 public static final int LOADER_FLAG_NONE = 0;
99 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
100 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
101
Joe Onorato36115782010-06-17 13:28:48 -0400102 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500103 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800106 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400107
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk final LauncherAppState mApp;
109 @Thunk final Object mLock = new Object();
110 @Thunk DeferredHandler mHandler = new DeferredHandler();
111 @Thunk LoaderTask mLoaderTask;
112 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700113 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Jason Monkbbe1e242014-05-16 17:37:34 -0400115 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700116
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118 static {
119 sWorkerThread.start();
120 }
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 // We start off with everything not loaded. After that, we assume that
124 // our monitoring of the package manager provides all updates and we never
125 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk boolean mWorkspaceLoaded;
127 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700128
Sunny Goyal756a28a2015-04-23 17:07:55 -0700129 /**
130 * Set of runnables to be called on the background thread after the workspace binding
131 * is complete.
132 */
133 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
134
Adam Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700137 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700138 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700139 // Entire list of widgets.
140 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142 // The lock that must be acquired before referencing any static bg data structures. Unlike
143 // other locks, this one can generally be held long-term because we never expect any of these
144 // static data structures to be referenced outside of the worker thread except on the first
145 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700146 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700150 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
153 // created by LauncherModel that are directly on the home screen (however, no widgets or
154 // shortcuts within folders).
155 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
158 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 new ArrayList<LauncherAppWidgetInfo>();
160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700162 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700163
Adam Cohendcd297f2013-06-18 13:13:40 -0700164 // sBgWorkspaceScreens is the ordered set of workspace screens.
165 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
166
Adam Cohen59400422014-03-05 18:07:04 -0800167 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000168 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800169
Sunny Goyal31860582015-09-18 08:38:57 -0700170 // sBgShortcutProviders is the set of custom shortcut providers
171 public static List<ResolveInfo> sBgShortcutProviders;
172
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700174 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
175 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 // </ only access in worker thread >
178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Adam Cohen091440a2015-03-18 14:16:05 -0700181 @Thunk final LauncherAppsCompat mLauncherApps;
182 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700185 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700187 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
190 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700191 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700192 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700193 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800194 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200196 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700197 public void bindAppsAdded(ArrayList<Long> newScreens,
198 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700199 ArrayList<ItemInfo> addAnimated,
200 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700202 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
203 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
204 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700205 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700206 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700207 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700208 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700209 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700210 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700211 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700212 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung64359a52013-07-08 17:17:08 -0700215 public interface ItemInfoFilter {
216 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
217 }
218
Bjorn Bringert1307f632013-10-03 22:31:03 +0100219 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800220 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221
Winson Chungee055712013-07-30 14:46:24 -0700222 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700223 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400224 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
225 // resource string.
226 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
227 ProviderInfo providerInfo =
228 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
229 ProviderInfo redirectProvider =
230 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400233 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700234
235 if (mOldContentProviderExists) {
236 Log.d(TAG, "Old launcher provider exists.");
237 } else {
238 Log.d(TAG, "Old launcher provider does not exist.");
239 }
240
Daniel Sandlere4f98912013-06-25 15:13:26 -0400241 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100242 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700243 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 mIconCache = iconCache;
245
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700263 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Winson Chunge43a1e72014-01-15 10:33:02 -0800272 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
273 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800274 }
275
Sunny Goyal756adbc2015-04-16 15:20:51 -0700276 public void setPackageState(final PackageInstallInfo installInfo) {
277 Runnable updateRunnable = new Runnable() {
278
279 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500280 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700281 synchronized (sBgLock) {
282 final HashSet<ItemInfo> updates = new HashSet<>();
283
284 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
285 // Ignore install success events as they are handled by Package add events.
286 return;
287 }
288
Sunny Goyale2df0622015-04-24 11:27:00 -0700289 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700290 if (info instanceof ShortcutInfo) {
291 ShortcutInfo si = (ShortcutInfo) info;
292 ComponentName cn = si.getTargetComponent();
293 if (si.isPromise() && (cn != null)
294 && installInfo.packageName.equals(cn.getPackageName())) {
295 si.setInstallProgress(installInfo.progress);
296
297 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
298 // Mark this info as broken.
299 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
300 }
301 updates.add(si);
302 }
303 }
304 }
305
306 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
307 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
308 widget.installProgress = installInfo.progress;
309 updates.add(widget);
310 }
311 }
312
313 if (!updates.isEmpty()) {
314 // Push changes to the callback.
315 Runnable r = new Runnable() {
316 public void run() {
317 Callbacks callbacks = getCallback();
318 if (callbacks != null) {
319 callbacks.bindRestoreItemsChange(updates);
320 }
321 }
322 };
323 mHandler.post(r);
324 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500325 }
326 }
327 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500329 }
330
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 /**
332 * Updates the icons and label of all pending icons for the provided package name.
333 */
334 public void updateSessionDisplayInfo(final String packageName) {
335 Runnable updateRunnable = new Runnable() {
336
337 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700338 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 synchronized (sBgLock) {
340 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
341 final UserHandleCompat user = UserHandleCompat.myUserHandle();
342
Sunny Goyale2df0622015-04-24 11:27:00 -0700343 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 if (info instanceof ShortcutInfo) {
345 ShortcutInfo si = (ShortcutInfo) info;
346 ComponentName cn = si.getTargetComponent();
347 if (si.isPromise() && (cn != null)
348 && packageName.equals(cn.getPackageName())) {
349 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
350 // For auto install apps update the icon as well as label.
351 mIconCache.getTitleAndIcon(si,
352 si.promisedIntent, user,
353 si.shouldUseLowResIcon());
354 } else {
355 // Only update the icon for restored apps.
356 si.updateIcon(mIconCache);
357 }
358 updates.add(si);
359 }
360 }
361 }
362
363 if (!updates.isEmpty()) {
364 // Push changes to the callback.
365 Runnable r = new Runnable() {
366 public void run() {
367 Callbacks callbacks = getCallback();
368 if (callbacks != null) {
369 callbacks.bindShortcutsChanged(updates,
370 new ArrayList<ShortcutInfo>(), user);
371 }
372 }
373 };
374 mHandler.post(r);
375 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700376 }
377 }
378 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700379 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700380 }
381
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800383 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800384
385 if (allAppsApps == null) {
386 throw new RuntimeException("allAppsApps must not be null");
387 }
388 if (allAppsApps.isEmpty()) {
389 return;
390 }
391
392 // Process the newly added applications and add them to the database first
393 Runnable r = new Runnable() {
394 public void run() {
395 runOnMainThread(new Runnable() {
396 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800397 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800398 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500399 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800400 }
401 }
402 });
403 }
404 };
405 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700406 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800407
Sunny Goyala9116722015-04-29 13:55:58 -0700408 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 int[] xy, int spanX, int spanY) {
410 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
412 final int xCount = (int) profile.numColumns;
413 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 boolean[][] occupied = new boolean[xCount][yCount];
415 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700416 for (ItemInfo r : occupiedPos) {
417 int right = r.cellX + r.spanX;
418 int bottom = r.cellY + r.spanY;
419 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
420 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 occupied[x][y] = true;
422 }
423 }
424 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800425 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700426 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800427 }
428
429 /**
430 * Find a position on the screen for the given size or adds a new screen.
431 * @return screenId and the coordinates for the item.
432 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700433 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800435 ArrayList<Long> workspaceScreens,
436 ArrayList<Long> addedWorkspaceScreensFinal,
437 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700438 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800439
Sunny Goyala9116722015-04-29 13:55:58 -0700440 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700441 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700442 synchronized (sBgLock) {
443 for (ItemInfo info : sBgItemsIdMap) {
444 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
445 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
446 if (items == null) {
447 items = new ArrayList<>();
448 screenItems.put(info.screenId, items);
449 }
450 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453 }
454
455 // Find appropriate space for the item.
456 long screenId = 0;
457 int[] cordinates = new int[2];
458 boolean found = false;
459
460 int screenCount = workspaceScreens.size();
461 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700462 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 if (preferredScreenIndex < screenCount) {
464 screenId = workspaceScreens.get(preferredScreenIndex);
465 found = findNextAvailableIconSpaceInScreen(
466 screenItems.get(screenId), cordinates, spanX, spanY);
467 }
468
469 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700470 // Search on any of the screens starting from the first screen.
471 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800472 screenId = workspaceScreens.get(screen);
473 if (findNextAvailableIconSpaceInScreen(
474 screenItems.get(screenId), cordinates, spanX, spanY)) {
475 // We found a space for it
476 found = true;
477 break;
478 }
479 }
480 }
481
482 if (!found) {
483 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700484 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
485 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
486 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800487
488 // Save the screen id for binding in the workspace
489 workspaceScreens.add(screenId);
490 addedWorkspaceScreensFinal.add(screenId);
491
492 // If we still can't find an empty space, then God help us all!!!
493 if (!findNextAvailableIconSpaceInScreen(
494 screenItems.get(screenId), cordinates, spanX, spanY)) {
495 throw new RuntimeException("Can't find space to add the item");
496 }
497 }
498 return Pair.create(screenId, cordinates);
499 }
500
501 /**
502 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700504 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700505 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800507 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700508 return;
Winson Chung997a9232013-07-24 15:33:46 -0700509 }
Winson Chung64359a52013-07-08 17:17:08 -0700510 // Process the newly added applications and add them to the database first
511 Runnable r = new Runnable() {
512 public void run() {
513 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
514 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
515
Winson Chung76828c82013-08-19 15:43:29 -0700516 // Get the list of workspace screens. We need to append to this list and
517 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
518 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800519 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700520 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700522 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800523 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700524 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800525 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700526 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800527 }
Winson Chung76828c82013-08-19 15:43:29 -0700528
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700530 Pair<Long, int[]> coords = findSpaceForItem(context,
531 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 1, 1);
533 long screenId = coords.first;
534 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700535
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700536 ItemInfo itemInfo;
537 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
538 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800539 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700540 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700541 } else {
542 throw new RuntimeException("Unexpected info type");
543 }
Winson Chung94d67682013-09-25 16:29:40 -0700544
Winson Chung64359a52013-07-08 17:17:08 -0700545 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700546 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700547 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700548 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700549 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700550 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700551 }
552 }
553
Winson Chung76828c82013-08-19 15:43:29 -0700554 // Update the workspace screens
555 updateWorkspaceScreenOrder(context, workspaceScreens);
556
Adam Cohen76a47a12014-02-05 11:47:43 -0800557 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700558 runOnMainThread(new Runnable() {
559 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800560 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700561 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700562 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
563 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700564 if (!addedShortcutsFinal.isEmpty()) {
565 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
566 long lastScreenId = info.screenId;
567 for (ItemInfo i : addedShortcutsFinal) {
568 if (i.screenId == lastScreenId) {
569 addAnimated.add(i);
570 } else {
571 addNotAnimated.add(i);
572 }
Winson Chung997a9232013-07-24 15:33:46 -0700573 }
574 }
Winson Chungd64d1762013-08-20 14:37:16 -0700575 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800576 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700577 }
Winson Chung64359a52013-07-08 17:17:08 -0700578 }
Winson Chung997a9232013-07-24 15:33:46 -0700579 });
580 }
Winson Chung64359a52013-07-08 17:17:08 -0700581 }
582 };
583 runOnWorkerThread(r);
584 }
585
Sunny Goyald33860f2015-04-23 16:02:20 -0700586 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700587 if (sWorkerThread.getThreadId() == Process.myTid()) {
588 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
589 "main thread");
590 }
591
Sunny Goyald33860f2015-04-23 16:02:20 -0700592 // Remove any queued UI runnables
593 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700594 // Unbind all the workspace items
595 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700596 }
597
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700598 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700599 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700600 // Ensure that we don't use the same workspace items data structure on the main thread
601 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700602 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700603 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700604 tmpItems.addAll(sBgWorkspaceItems);
605 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700606 }
607 Runnable r = new Runnable() {
608 @Override
609 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700610 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611 item.unbind();
612 }
613 }
614 };
615 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700616 }
617
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618 /**
619 * Adds an item to the DB if it was not created previously, or move it to a new
620 * <container, screen, cellX, cellY>
621 */
622 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700623 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624 if (item.container == ItemInfo.NO_ID) {
625 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700626 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400627 } else {
628 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700629 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 }
631 }
632
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700633 static void checkItemInfoLocked(
634 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
635 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
636 if (modelItem != null && item != modelItem) {
637 // check all the data is consistent
638 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
639 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
640 ShortcutInfo shortcut = (ShortcutInfo) item;
641 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
642 modelShortcut.intent.filterEquals(shortcut.intent) &&
643 modelShortcut.id == shortcut.id &&
644 modelShortcut.itemType == shortcut.itemType &&
645 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700647 modelShortcut.cellX == shortcut.cellX &&
648 modelShortcut.cellY == shortcut.cellY &&
649 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700650 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700651 // For all intents and purposes, this is the same object
652 return;
653 }
654 }
655
656 // the modelItem needs to match up perfectly with item if our model is
657 // to be consistent with the database-- for now, just require
658 // modelItem == item or the equality check above
659 String msg = "item: " + ((item != null) ? item.toString() : "null") +
660 "modelItem: " +
661 ((modelItem != null) ? modelItem.toString() : "null") +
662 "Error: ItemInfo passed to checkItemInfo doesn't match original";
663 RuntimeException e = new RuntimeException(msg);
664 if (stackTrace != null) {
665 e.setStackTrace(stackTrace);
666 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800667 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700668 }
669 }
670
Michael Jurka816474f2012-06-25 14:49:02 -0700671 static void checkItemInfo(final ItemInfo item) {
672 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
673 final long itemId = item.id;
674 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700675 public void run() {
676 synchronized (sBgLock) {
677 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700678 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700679 }
680 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700681 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700682 }
683
Michael Jurkac9d95c52011-08-29 14:03:34 -0700684 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
685 final ItemInfo item, final String callingFunction) {
686 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700687 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700688 final ContentResolver cr = context.getContentResolver();
689
Adam Cohen487f7dd2012-06-28 18:12:10 -0700690 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700691 Runnable r = new Runnable() {
692 public void run() {
693 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694 updateItemArrays(item, itemId, stackTrace);
695 }
696 };
697 runOnWorkerThread(r);
698 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700699
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
701 final ArrayList<ItemInfo> items, final String callingFunction) {
702 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700703
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700704 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
705 Runnable r = new Runnable() {
706 public void run() {
707 ArrayList<ContentProviderOperation> ops =
708 new ArrayList<ContentProviderOperation>();
709 int count = items.size();
710 for (int i = 0; i < count; i++) {
711 ItemInfo item = items.get(i);
712 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700713 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700714 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700715
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700716 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
717 updateItemArrays(item, itemId, stackTrace);
718
719 }
720 try {
721 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
722 } catch (Exception e) {
723 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724 }
725 }
726 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700727 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700728 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700729
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700730 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
731 // Lock on mBgLock *after* the db operation
732 synchronized (sBgLock) {
733 checkItemInfoLocked(itemId, item, stackTrace);
734
735 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
736 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
737 // Item is in a folder, make sure this folder exists
738 if (!sBgFolders.containsKey(item.container)) {
739 // An items container is being set to a that of an item which is not in
740 // the list of Folders.
741 String msg = "item: " + item + " container being set to: " +
742 item.container + ", not in the list of folders";
743 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 }
745 }
746
747 // Items are added/removed from the corresponding FolderInfo elsewhere, such
748 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
749 // that are on the desktop, as appropriate
750 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800751 if (modelItem != null &&
752 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
753 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754 switch (modelItem.itemType) {
755 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
756 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
757 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
758 if (!sBgWorkspaceItems.contains(modelItem)) {
759 sBgWorkspaceItems.add(modelItem);
760 }
761 break;
762 default:
763 break;
764 }
765 } else {
766 sBgWorkspaceItems.remove(modelItem);
767 }
768 }
769 }
770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700773 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700774 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400777 item.cellX = cellX;
778 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700779
Winson Chung3d503fb2011-07-13 17:25:49 -0700780 // We store hotseat items in canonical form which is this orientation invariant position
781 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788
789 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700791 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
792 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800793 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795
Michael Jurkac9d95c52011-08-29 14:03:34 -0700796 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700797 }
798
799 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700800 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
801 * cellX, cellY have already been updated on the ItemInfos.
802 */
803 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
804 final long container, final int screen) {
805
806 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
807 int count = items.size();
808
809 for (int i = 0; i < count; i++) {
810 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700811 item.container = container;
812
813 // We store hotseat items in canonical form which is this orientation invariant position
814 // in the hotseat
815 if (context instanceof Launcher && screen < 0 &&
816 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700817 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700818 item.cellY);
819 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700820 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821 }
822
823 final ContentValues values = new ContentValues();
824 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
825 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
826 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800827 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700829
830 contentValues.add(values);
831 }
832 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
833 }
834
835 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700836 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800837 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700838 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700840 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800841 item.cellX = cellX;
842 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700843 item.spanX = spanX;
844 item.spanY = spanY;
845
846 // We store hotseat items in canonical form which is this orientation invariant position
847 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 }
Adam Cohend4844c32011-02-18 19:25:06 -0800854
Adam Cohend4844c32011-02-18 19:25:06 -0800855 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800856 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
858 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800859 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700860 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
861 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800863
Michael Jurka816474f2012-06-25 14:49:02 -0700864 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700865 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700866
867 /**
868 * Update an item to the database in a specified container.
869 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700870 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700871 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100872 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700873 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800874 }
875
Sunny Goyal756a28a2015-04-23 17:07:55 -0700876 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700877 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700878 synchronized (mLock) {
879 if (!mHasLoaderCompletedOnce ||
880 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
881 throw new RuntimeException("Trying to add shortcut while loader is running");
882 }
883 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700884 }
885 }
886
Adam Cohend4844c32011-02-18 19:25:06 -0800887 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700888 * Returns true if the shortcuts already exists on the workspace. This must be called after
889 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700891 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
892 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700893 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700894 if (intent.getComponent() != null) {
895 // If component is not null, an intent with null package will produce
896 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700897 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700898 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700899 intentWithPkg = intent.toUri(0);
900 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700901 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700902 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
903 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 }
905 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700906 intentWithPkg = intent.toUri(0);
907 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700908 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700909
910 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700911 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700912 if (item instanceof ShortcutInfo) {
913 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700914 Intent targetIntent = info.promisedIntent == null
915 ? info.intent : info.promisedIntent;
916 if (targetIntent != null && info.user.equals(user)) {
917 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700918 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
919 return true;
920 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700921 }
922 }
923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700925 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700926 }
927
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 /**
929 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
930 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700931 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 final ContentResolver cr = context.getContentResolver();
933 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
934 "_id=? and (itemType=? or itemType=?)",
935 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700936 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700937
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 try {
939 if (c.moveToFirst()) {
940 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
941 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
942 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
943 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
944 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
945 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700946 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 FolderInfo folderInfo = null;
949 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700950 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
951 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700953 }
954
Sunny Goyala508e4f2015-05-21 09:33:57 -0700955 // Do not trim the folder label, as is was set by the user.
956 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 folderInfo.id = id;
958 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700960 folderInfo.cellX = c.getInt(cellXIndex);
961 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700962 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963
964 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 } finally {
967 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700968 }
969
970 return null;
971 }
972
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 /**
974 * Add an item to the database in a specified container. Sets the container, screen, cellX and
975 * cellY fields of the item. Also assigns an ID to the item.
976 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700977 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700978 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 item.cellX = cellX;
981 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700982 // We store hotseat items in canonical form which is this orientation invariant position
983 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700985 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700987 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700989 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990
991 final ContentValues values = new ContentValues();
992 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100993 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994
Sunny Goyald2497482015-09-22 18:24:19 -0700995 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
996 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
997
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700998 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700999
Jason Monk8e19cf22014-03-20 15:06:57 -04001000 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001001 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001002 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001003 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001006 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001007 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008 sBgItemsIdMap.put(item.id, item);
1009 switch (item.itemType) {
1010 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1011 sBgFolders.put(item.id, (FolderInfo) item);
1012 // Fall through
1013 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1014 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1015 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1016 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1017 sBgWorkspaceItems.add(item);
1018 } else {
1019 if (!sBgFolders.containsKey(item.container)) {
1020 // Adding an item to a folder that doesn't exist.
1021 String msg = "adding item: " + item + " to a folder that " +
1022 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001023 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001025 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 break;
1027 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1028 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1029 break;
1030 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001031 }
1032 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001033 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001035 }
1036
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001038 * Creates a new unique child id, for a given cell span across all layouts.
1039 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001040 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001041 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001042 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001043 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001044 }
1045
Sunny Goyal34942622014-08-29 17:20:55 -07001046 private static ArrayList<ItemInfo> getItemsByPackageName(
1047 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001048 ItemInfoFilter filter = new ItemInfoFilter() {
1049 @Override
1050 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1051 return cn.getPackageName().equals(pn) && info.user.equals(user);
1052 }
1053 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001054 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001055 }
1056
1057 /**
1058 * Removes all the items from the database corresponding to the specified package.
1059 */
1060 static void deletePackageFromDatabase(Context context, final String pn,
1061 final UserHandleCompat user) {
1062 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001063 }
1064
1065 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001066 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001068 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001069 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1070 items.add(item);
1071 deleteItemsFromDatabase(context, items);
1072 }
1073
1074 /**
1075 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001076 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001077 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001078 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001079 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001080 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001081 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001082 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001083 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001084
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001085 // Lock on mBgLock *after* the db operation
1086 synchronized (sBgLock) {
1087 switch (item.itemType) {
1088 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1089 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001090 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001091 if (info.container == item.id) {
1092 // We are deleting a folder which still contains items that
1093 // think they are contained by that folder.
1094 String msg = "deleting a folder (" + item + ") which still " +
1095 "contains items (" + info + ")";
1096 Log.e(TAG, msg);
1097 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001098 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001099 sBgWorkspaceItems.remove(item);
1100 break;
1101 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1102 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1103 sBgWorkspaceItems.remove(item);
1104 break;
1105 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1106 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1107 break;
1108 }
1109 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001111 }
1112 }
Michael Jurka83df1882011-08-31 20:59:26 -07001113 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 }
1116
1117 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001118 * Update the order of the workspace screens in the database. The array list contains
1119 * a list of screen ids in the order that they should appear.
1120 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001121 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001122 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 final ContentResolver cr = context.getContentResolver();
1124 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1125
1126 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001127 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 while (iter.hasNext()) {
1129 long id = iter.next();
1130 if (id < 0) {
1131 iter.remove();
1132 }
1133 }
1134
1135 Runnable r = new Runnable() {
1136 @Override
1137 public void run() {
Yura085c8532014-02-11 15:15:29 +00001138 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001139 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001140 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001141 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001142 for (int i = 0; i < count; i++) {
1143 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001144 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1146 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001147 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 }
Yura085c8532014-02-11 15:15:29 +00001149
1150 try {
1151 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1152 } catch (Exception ex) {
1153 throw new RuntimeException(ex);
1154 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001155
Winson Chungba9c37f2013-08-30 14:11:37 -07001156 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001157 sBgWorkspaceScreens.clear();
1158 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001159 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001160 }
1161 };
1162 runOnWorkerThread(r);
1163 }
1164
1165 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001166 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 */
Winsonc0b52fe2015-09-09 16:38:15 -07001168 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001169 final ContentResolver cr = context.getContentResolver();
1170
Michael Jurkac9d95c52011-08-29 14:03:34 -07001171 Runnable r = new Runnable() {
1172 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001173 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001175 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001176 sBgItemsIdMap.remove(info.id);
1177 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001178 sBgWorkspaceItems.remove(info);
1179 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001180
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001181 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001182 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001183 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001184 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001185 for (ItemInfo childInfo : info.contents) {
1186 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001188 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001189 }
1190 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 }
1193
1194 /**
1195 * Set this as the current Launcher activity object for the loader.
1196 */
1197 public void initialize(Callbacks callbacks) {
1198 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001199 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1200 // workspace to prevent leaking Launcher activities on orientation change.
1201 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 mCallbacks = new WeakReference<Callbacks>(callbacks);
1203 }
1204 }
1205
Kenny Guyed131872014-04-30 03:02:21 +01001206 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001207 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001208 int op = PackageUpdatedTask.OP_UPDATE;
1209 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1210 user));
1211 }
1212
1213 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001214 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001215 int op = PackageUpdatedTask.OP_REMOVE;
1216 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1217 user));
1218 }
1219
1220 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001221 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001222 int op = PackageUpdatedTask.OP_ADD;
1223 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1224 user));
1225 }
1226
1227 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001228 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001229 boolean replacing) {
1230 if (!replacing) {
1231 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1232 user));
1233 if (mAppsCanBeOnRemoveableStorage) {
1234 // Only rebind if we support removable storage. It catches the
1235 // case where
1236 // apps on the external sd card need to be reloaded
1237 startLoaderFromBackground();
1238 }
1239 } else {
1240 // If we are replacing then just update the packages in the list
1241 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1242 packageNames, user));
1243 }
1244 }
1245
1246 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001247 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001248 boolean replacing) {
1249 if (!replacing) {
1250 enqueuePackageUpdated(new PackageUpdatedTask(
1251 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1252 user));
1253 }
Kenny Guyed131872014-04-30 03:02:21 +01001254 }
1255
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001256 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1258 * ACTION_PACKAGE_CHANGED.
1259 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001260 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001261 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001262 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001263
Joe Onorato36115782010-06-17 13:28:48 -04001264 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001265 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001266 // If we have changed locale we need to clear out the labels in all apps/workspace.
1267 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001268 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001269 Callbacks callbacks = getCallback();
1270 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001271 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001272 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001273 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1274 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001275 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001276 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001277 }
1278 }
1279
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001280 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001281 resetLoadedState(true, true);
1282
Reena Lee93f824a2011-09-23 17:20:28 -07001283 // Do this here because if the launcher activity is running it will be restarted.
1284 // If it's not running startLoaderFromBackground will merely tell it that it needs
1285 // to reload.
1286 startLoaderFromBackground();
1287 }
1288
Winson Chungf0c6ae02012-03-21 16:10:31 -07001289 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1290 synchronized (mLock) {
1291 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1292 // mWorkspaceLoaded to true later
1293 stopLoaderLocked();
1294 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1295 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1296 }
1297 }
1298
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001299 /**
1300 * When the launcher is in the background, it's possible for it to miss paired
1301 * configuration changes. So whenever we trigger the loader from the background
1302 * tell the launcher that it needs to re-run the loader when it comes back instead
1303 * of doing it now.
1304 */
1305 public void startLoaderFromBackground() {
1306 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001307 Callbacks callbacks = getCallback();
1308 if (callbacks != null) {
1309 // Only actually run the loader if they're not paused.
1310 if (!callbacks.setLoadOnResume()) {
1311 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001312 }
1313 }
1314 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001315 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001316 }
Joe Onorato36115782010-06-17 13:28:48 -04001317 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001318
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001319 /**
1320 * If there is already a loader task running, tell it to stop.
1321 */
1322 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001323 LoaderTask oldTask = mLoaderTask;
1324 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001325 oldTask.stopLocked();
1326 }
Reena Lee93f824a2011-09-23 17:20:28 -07001327 }
1328
Adam Cohen1a85c582014-09-30 09:48:49 -07001329 public boolean isCurrentCallbacks(Callbacks callbacks) {
1330 return (mCallbacks != null && mCallbacks.get() == callbacks);
1331 }
1332
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001333 public void startLoader(int synchronousBindPage) {
1334 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001335 }
1336
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001337 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001338 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1339 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001340 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001341 // Don't bother to start the thread if we know it's not going to do anything
1342 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001343 final Callbacks oldCallbacks = mCallbacks.get();
1344 // Clear any pending bind-runnables from the synchronized load process.
1345 runOnMainThread(new Runnable() {
1346 public void run() {
1347 oldCallbacks.clearPendingBinds();
1348 }
1349 });
1350
Joe Onorato36115782010-06-17 13:28:48 -04001351 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001352 stopLoaderLocked();
1353 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001354 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001355 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1357 } else {
1358 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1359 sWorker.post(mLoaderTask);
1360 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 }
1363 }
1364
Joe Onorato36115782010-06-17 13:28:48 -04001365 public void stopLoader() {
1366 synchronized (mLock) {
1367 if (mLoaderTask != null) {
1368 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 }
1370 }
Joe Onorato36115782010-06-17 13:28:48 -04001371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001373 /**
1374 * Loads the workspace screen ids in an ordered list.
1375 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001376 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001377 final ContentResolver contentResolver = context.getContentResolver();
1378 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001379
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001380 // Get screens ordered by rank.
1381 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1382 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1383 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001384 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001385 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001386 while (sc.moveToNext()) {
1387 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001388 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001389 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001390 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001391 }
1392 }
1393 } finally {
1394 sc.close();
1395 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001396 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001397 }
1398
Michael Jurkac57b7a82011-08-09 22:02:20 -07001399 public boolean isAllAppsLoaded() {
1400 return mAllAppsLoaded;
1401 }
1402
Joe Onorato36115782010-06-17 13:28:48 -04001403 /**
1404 * Runnable for the thread that loads the contents of the launcher:
1405 * - workspace icons
1406 * - widgets
1407 * - all apps icons
1408 */
1409 private class LoaderTask implements Runnable {
1410 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001411 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001412 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001413 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001414 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001415
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001416 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001417 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001418 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001419 }
1420
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001421 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001422 mIsLoadingAndBindingWorkspace = true;
1423
Joe Onorato36115782010-06-17 13:28:48 -04001424 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001425 if (DEBUG_LOADERS) {
1426 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001428
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001429 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001430 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001431 synchronized (LoaderTask.this) {
1432 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001433 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001434 }
1435 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001436 }
1437 }
1438
Joe Onorato36115782010-06-17 13:28:48 -04001439 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001440 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001441 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001442
Joe Onorato36115782010-06-17 13:28:48 -04001443 private void waitForIdle() {
1444 // Wait until the either we're stopped or the other threads are done.
1445 // This way we don't start loading all apps until the workspace has settled
1446 // down.
1447 synchronized (LoaderTask.this) {
1448 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001449
Joe Onorato36115782010-06-17 13:28:48 -04001450 mHandler.postIdle(new Runnable() {
1451 public void run() {
1452 synchronized (LoaderTask.this) {
1453 mLoadAndBindStepFinished = true;
1454 if (DEBUG_LOADERS) {
1455 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001456 }
Joe Onorato36115782010-06-17 13:28:48 -04001457 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001458 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001459 }
Joe Onorato36115782010-06-17 13:28:48 -04001460 });
1461
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001462 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001463 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001464 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1465 // wait no longer than 1sec at a time
1466 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001467 } catch (InterruptedException ex) {
1468 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001469 }
1470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471 if (DEBUG_LOADERS) {
1472 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001473 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001474 + "ms for previous step to finish binding");
1475 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001476 }
Joe Onorato36115782010-06-17 13:28:48 -04001477 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001478
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001479 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001480 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001481 // Ensure that we have a valid page index to load synchronously
1482 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1483 "valid page index");
1484 }
1485 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1486 // Ensure that we don't try and bind a specified page when the pages have not been
1487 // loaded already (we should load everything asynchronously in that case)
1488 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1489 }
1490 synchronized (mLock) {
1491 if (mIsLoaderTaskRunning) {
1492 // Ensure that we are never running the background loading at this point since
1493 // we also touch the background collections
1494 throw new RuntimeException("Error! Background loading is already running");
1495 }
1496 }
1497
1498 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1499 // data structures, we can't allow any other thread to touch that data, but because
1500 // this call is synchronous, we can get away with not locking).
1501
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001502 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001503 // operations from the previous activity. We need to ensure that all queued operations
1504 // are executed before any synchronous binding work is done.
1505 mHandler.flush();
1506
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 // Divide the set of loaded items into those that we are binding synchronously, and
1508 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001509 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1511 // arise from that.
1512 onlyBindAllApps();
1513 }
1514
Joe Onorato36115782010-06-17 13:28:48 -04001515 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001516 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001517 if (mStopped) {
1518 return;
1519 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001520 mIsLoaderTaskRunning = true;
1521 }
Joe Onorato36115782010-06-17 13:28:48 -04001522 // Optimize for end-user experience: if the Launcher is up and // running with the
1523 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1524 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001525 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001526 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001527 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001528
Joe Onorato36115782010-06-17 13:28:48 -04001529 if (mStopped) {
1530 break keep_running;
1531 }
1532
Joe Onorato36115782010-06-17 13:28:48 -04001533 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001534
1535 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001536 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1537 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001538 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539
Joe Onorato36115782010-06-17 13:28:48 -04001540 // Clear out this reference, otherwise we end up holding it until all of the
1541 // callback runnables are done.
1542 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001543
Joe Onorato36115782010-06-17 13:28:48 -04001544 synchronized (mLock) {
1545 // If we are still the last one to be scheduled, remove ourselves.
1546 if (mLoaderTask == this) {
1547 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001548 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001549 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001550 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001551 }
Joe Onorato36115782010-06-17 13:28:48 -04001552 }
1553
1554 public void stopLocked() {
1555 synchronized (LoaderTask.this) {
1556 mStopped = true;
1557 this.notify();
1558 }
1559 }
1560
1561 /**
1562 * Gets the callbacks object. If we've been stopped, or if the launcher object
1563 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1564 * object that was around when the deferred message was scheduled, and if there's
1565 * a new Callbacks object around then also return null. This will save us from
1566 * calling onto it with data that will be ignored.
1567 */
1568 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1569 synchronized (mLock) {
1570 if (mStopped) {
1571 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001572 }
Joe Onorato36115782010-06-17 13:28:48 -04001573
1574 if (mCallbacks == null) {
1575 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001576 }
Joe Onorato36115782010-06-17 13:28:48 -04001577
1578 final Callbacks callbacks = mCallbacks.get();
1579 if (callbacks != oldCallbacks) {
1580 return null;
1581 }
1582 if (callbacks == null) {
1583 Log.w(TAG, "no mCallbacks");
1584 return null;
1585 }
1586
1587 return callbacks;
1588 }
1589 }
1590
1591 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001592 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1593 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001594 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001595 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001596 final int countX = profile.numColumns;
1597 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001598
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001600 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001601 // Return early if we detect that an item is under the hotseat button
1602 if (mCallbacks == null ||
1603 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001604 Log.e(TAG, "Error loading shortcut into hotseat " + item
1605 + " into position (" + item.screenId + ":" + item.cellX + ","
1606 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001607 return false;
1608 }
1609
Dan Sandler295ae182013-12-10 16:05:47 -05001610 final ItemInfo[][] hotseatItems =
1611 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1612
Adam Cohen2e6da152015-05-06 11:42:25 -07001613 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001614 Log.e(TAG, "Error loading shortcut " + item
1615 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001616 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001617 + ")");
1618 return false;
1619 }
1620
Dan Sandler295ae182013-12-10 16:05:47 -05001621 if (hotseatItems != null) {
1622 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001623 Log.e(TAG, "Error loading shortcut into hotseat " + item
1624 + " into position (" + item.screenId + ":" + item.cellX + ","
1625 + item.cellY + ") occupied by "
1626 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1627 [(int) item.screenId][0]);
1628 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001629 } else {
1630 hotseatItems[(int) item.screenId][0] = item;
1631 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001632 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001633 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001634 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001635 items[(int) item.screenId][0] = item;
1636 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001637 return true;
1638 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001639 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1640 if (!workspaceScreens.contains((Long) item.screenId)) {
1641 // The item has an invalid screen id.
1642 return false;
1643 }
1644 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001645 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001646 return true;
1647 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001648
Adam Cohendcd297f2013-06-18 13:13:40 -07001649 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001650 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001651 occupied.put(item.screenId, items);
1652 }
1653
Dan Sandler295ae182013-12-10 16:05:47 -05001654 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001655 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1656 item.cellX < 0 || item.cellY < 0 ||
1657 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1658 Log.e(TAG, "Error loading shortcut " + item
1659 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1660 + item.cellX + "," + item.cellY
1661 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1662 return false;
1663 }
1664
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001665 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001666 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1667 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001668 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001669 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001670 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001671 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001672 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001673 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001674 return false;
1675 }
1676 }
1677 }
1678 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1679 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001680 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001681 }
1682 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001683
Joe Onorato36115782010-06-17 13:28:48 -04001684 return true;
1685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686
Winson Chungba9c37f2013-08-30 14:11:37 -07001687 /** Clears all the sBg data structures */
1688 private void clearSBgDataStructures() {
1689 synchronized (sBgLock) {
1690 sBgWorkspaceItems.clear();
1691 sBgAppWidgets.clear();
1692 sBgFolders.clear();
1693 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001694 sBgWorkspaceScreens.clear();
1695 }
1696 }
1697
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001698 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001699 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700
Joe Onorato36115782010-06-17 13:28:48 -04001701 final Context context = mContext;
1702 final ContentResolver contentResolver = context.getContentResolver();
1703 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001704 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001705 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001706 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001707
Winson Chung892c74d2013-08-22 16:15:50 -07001708 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001709 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001710 int countX = profile.numColumns;
1711 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001712
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001713 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001714 long migrationStartTime = System.currentTimeMillis();
1715 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001716 try {
1717 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1718 // Clear the flags before starting the task, so that we do not run the task
1719 // again, in case there was an uncaught error.
1720 MigrateFromRestoreTask.clearFlags(mContext);
1721 task.execute();
1722 } catch (Exception e) {
1723 Log.e(TAG, "Error during grid migration", e);
1724
1725 // Clear workspace.
1726 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1727 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001728 Log.v(TAG, "Workspace migration completed in "
1729 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001730 }
1731
Dan Sandlerd5024042014-01-09 15:01:33 -05001732 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001733 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001734 LauncherSettings.Settings.call(contentResolver,
1735 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001736 }
1737
1738 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1739 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001740 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001741 LauncherSettings.Settings.call(contentResolver,
1742 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001743 } else {
1744 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001745 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001746 LauncherSettings.Settings.call(contentResolver,
1747 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001748 }
Adam Cohene25af792013-06-06 23:08:25 -07001749
Winson Chung2abf94d2012-07-18 18:16:38 -07001750 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001751 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001752 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001753 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001754 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001755
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001757 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001758 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001759 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001760 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 // +1 for the hotseat (it can be larger than the workspace)
1763 // Load workspace in reverse order to ensure that latest items are loaded first (and
1764 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001765 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 try {
1768 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1769 final int intentIndex = c.getColumnIndexOrThrow
1770 (LauncherSettings.Favorites.INTENT);
1771 final int titleIndex = c.getColumnIndexOrThrow
1772 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 final int containerIndex = c.getColumnIndexOrThrow(
1774 LauncherSettings.Favorites.CONTAINER);
1775 final int itemTypeIndex = c.getColumnIndexOrThrow(
1776 LauncherSettings.Favorites.ITEM_TYPE);
1777 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1778 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001779 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1780 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 final int screenIndex = c.getColumnIndexOrThrow(
1782 LauncherSettings.Favorites.SCREEN);
1783 final int cellXIndex = c.getColumnIndexOrThrow
1784 (LauncherSettings.Favorites.CELLX);
1785 final int cellYIndex = c.getColumnIndexOrThrow
1786 (LauncherSettings.Favorites.CELLY);
1787 final int spanXIndex = c.getColumnIndexOrThrow
1788 (LauncherSettings.Favorites.SPANX);
1789 final int spanYIndex = c.getColumnIndexOrThrow(
1790 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001791 final int rankIndex = c.getColumnIndexOrThrow(
1792 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001793 final int restoredIndex = c.getColumnIndexOrThrow(
1794 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001795 final int profileIdIndex = c.getColumnIndexOrThrow(
1796 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001797 final int optionsIndex = c.getColumnIndexOrThrow(
1798 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001799 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001800
Sunny Goyal7f834d22015-04-21 10:10:23 -07001801 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1802 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1803 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1804 }
1805
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001806 ShortcutInfo info;
1807 String intentDescription;
1808 LauncherAppWidgetInfo appWidgetInfo;
1809 int container;
1810 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001811 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001813 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001815 while (!mStopped && c.moveToNext()) {
1816 try {
1817 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001818 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001819 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001820 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001822 switch (itemType) {
1823 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1824 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001825 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001827 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001828 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001829 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001830 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001831 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001832 if (user == null) {
1833 // User has been deleted remove the item.
1834 itemsToRemove.add(id);
1835 continue;
1836 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 try {
1838 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001839 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001840 if (cn != null && cn.getPackageName() != null) {
1841 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1842 cn.getPackageName(), user);
1843 boolean validComponent = validPkg &&
1844 launcherApps.isActivityEnabledForProfile(cn, user);
1845
1846 if (validComponent) {
1847 if (restored) {
1848 // no special handling necessary for this item
1849 restoredRows.add(id);
1850 restored = false;
1851 }
1852 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001853 intent = null;
1854 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1855 // We allow auto install apps to have their intent
1856 // updated after an install.
1857 intent = manager.getLaunchIntentForPackage(
1858 cn.getPackageName());
1859 if (intent != null) {
1860 ContentValues values = new ContentValues();
1861 values.put(LauncherSettings.Favorites.INTENT,
1862 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001863 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001864 }
1865 }
1866
1867 if (intent == null) {
1868 // The app is installed but the component is no
1869 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001870 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001871 itemsToRemove.add(id);
1872 continue;
1873 } else {
1874 // no special handling necessary for this item
1875 restoredRows.add(id);
1876 restored = false;
1877 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001878 } else if (restored) {
1879 // Package is not yet available but might be
1880 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001881 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001882
1883 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1884 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001885 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001886 // App restore has started. Update the flag
1887 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1888 ContentValues values = new ContentValues();
1889 values.put(LauncherSettings.Favorites.RESTORED,
1890 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001891 updateItem(id, values);
1892 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1893 // This is a common app. Try to replace this.
1894 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1895 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1896 if (parser.findDefaultApp()) {
1897 // Default app found. Replace it.
1898 intent = parser.parsedIntent;
1899 cn = intent.getComponent();
1900 ContentValues values = parser.parsedValues;
1901 values.put(LauncherSettings.Favorites.RESTORED, 0);
1902 updateItem(id, values);
1903 restored = false;
1904 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001905
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001906 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001907 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001908 itemsToRemove.add(id);
1909 continue;
1910 }
Sunny Goyal94485362014-09-18 16:13:58 -07001911 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001912 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001913 itemsToRemove.add(id);
1914 continue;
1915 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001916 } else if (launcherApps.isAppEnabled(
1917 manager, cn.getPackageName(),
1918 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1919 // Package is present but not available.
1920 allowMissingTarget = true;
1921 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1922 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001923 // SdCard is not ready yet. Package might get available,
1924 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001925 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001926 HashSet<String> pkgs = sPendingPackages.get(user);
1927 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001928 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001929 sPendingPackages.put(user, pkgs);
1930 }
1931 pkgs.add(cn.getPackageName());
1932 allowMissingTarget = true;
1933 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001934
1935 } else {
1936 // Do not wait for external media load anymore.
1937 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001938 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001939 itemsToRemove.add(id);
1940 continue;
Winson Chungee055712013-07-30 14:46:24 -07001941 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001942 } else if (cn == null) {
1943 // For shortcuts with no component, keep them as they are
1944 restoredRows.add(id);
1945 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001946 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001948 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001949 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 continue;
1951 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952
Sunny Goyal34b65272015-03-11 16:56:52 -07001953 boolean useLowResIcon = container >= 0 &&
1954 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1955
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001956 if (itemReplaced) {
1957 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001958 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001959 cursorIconInfo.iconIndex, titleIndex,
1960 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001961 } else {
1962 // Don't replace items for other profiles.
1963 itemsToRemove.add(id);
1964 continue;
1965 }
1966 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001967 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001968 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001969 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001970 intent = getRestoredItemIntent(c, context, intent);
1971 } else {
1972 // Don't restore items for other profiles.
1973 itemsToRemove.add(id);
1974 continue;
1975 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001976 } else if (itemType ==
1977 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001978 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001979 cursorIconInfo.iconIndex, titleIndex,
1980 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001982 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001983
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 // App shortcuts that used to be automatically added to Launcher
1985 // didn't always have the correct intent flags set, so do that
1986 // here
1987 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001988 intent.getCategories() != null &&
1989 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001990 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 intent.addFlags(
1992 Intent.FLAG_ACTIVITY_NEW_TASK |
1993 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1994 }
Michael Jurka96879562012-03-22 05:54:33 -07001995 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001996
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001998 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 info.cellX = c.getInt(cellXIndex);
2003 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002004 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002005 info.spanX = 1;
2006 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002007 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002008 if (info.promisedIntent != null) {
2009 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2010 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002011 info.isDisabled = disabledState;
2012 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2013 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2014 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002015
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002017 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002018 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 break;
2020 }
2021
Sunny Goyal756adbc2015-04-16 15:20:51 -07002022 if (restored) {
2023 ComponentName cn = info.getTargetComponent();
2024 if (cn != null) {
2025 Integer progress = installingPkgs.get(cn.getPackageName());
2026 if (progress != null) {
2027 info.setInstallProgress(progress);
2028 } else {
2029 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2030 }
2031 }
2032 }
2033
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034 switch (container) {
2035 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2036 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2037 sBgWorkspaceItems.add(info);
2038 break;
2039 default:
2040 // Item is in a user folder
2041 FolderInfo folderInfo =
2042 findOrMakeFolder(sBgFolders, container);
2043 folderInfo.add(info);
2044 break;
2045 }
2046 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002047 } else {
2048 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 }
2050 break;
2051
2052 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2053 id = c.getLong(idIndex);
2054 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2055
Sunny Goyala508e4f2015-05-21 09:33:57 -07002056 // Do not trim the folder label, as is was set by the user.
2057 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 folderInfo.cellX = c.getInt(cellXIndex);
2062 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002063 folderInfo.spanX = 1;
2064 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002065 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002066
Daniel Sandler8802e962010-05-26 16:28:16 -04002067 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002068 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002069 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002070 break;
2071 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002072
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2075 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2076 sBgWorkspaceItems.add(folderInfo);
2077 break;
Joe Onorato36115782010-06-17 13:28:48 -04002078 }
Joe Onorato17a89222011-02-08 17:26:11 -08002079
Chris Wrenf4d08112014-01-16 18:13:56 -05002080 if (restored) {
2081 // no special handling required for restored folders
2082 restoredRows.add(id);
2083 }
2084
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2086 sBgFolders.put(folderInfo.id, folderInfo);
2087 break;
2088
2089 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002090 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002092 boolean customWidget = itemType ==
2093 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2094
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002096 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002097 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002098 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002099 user = allUsers.get(serialNumber);
2100 if (user == null) {
2101 itemsToRemove.add(id);
2102 continue;
2103 }
2104
Sunny Goyalff572272014-07-23 13:58:07 -07002105 final ComponentName component =
2106 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002107
Sunny Goyal651077b2014-06-30 14:15:31 -07002108 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002109 final boolean isIdValid = (restoreStatus &
2110 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002111 final boolean wasProviderReady = (restoreStatus &
2112 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002113
Adam Cohen59400422014-03-05 18:07:04 -08002114 final LauncherAppWidgetProviderInfo provider =
2115 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002116 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002117 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002118
2119 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002120 if (!isSafeMode && !customWidget &&
2121 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002122 addDumpLog("Deleting widget that isn't installed anymore: "
2123 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002124 itemsToRemove.add(id);
2125 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002126 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002127 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2128 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002129
Sunny Goyal53f96722015-07-13 19:54:53 -07002130 // The provider is available. So the widget is either
2131 // available or not available. We do not need to track
2132 // any future restore updates.
2133 int status = restoreStatus &
2134 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002135 if (!wasProviderReady) {
2136 // If provider was not previously ready, update the
2137 // status and UI flag.
2138
2139 // Id would be valid only if the widget restore broadcast was received.
2140 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002141 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002142 } else {
2143 status &= ~LauncherAppWidgetInfo
2144 .FLAG_PROVIDER_NOT_READY;
2145 }
2146 }
2147 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002148 } else {
2149 Log.v(TAG, "Widget restore pending id=" + id
2150 + " appWidgetId=" + appWidgetId
2151 + " status =" + restoreStatus);
2152 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002153 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002154 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002155 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002156
2157 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2158 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002159 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002160 // App restore has started. Update the flag
2161 appWidgetInfo.restoreStatus |=
2162 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002163 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002164 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002165 itemsToRemove.add(id);
2166 continue;
2167 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002168
2169 appWidgetInfo.installProgress =
2170 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002171 }
Sunny Goyalff572272014-07-23 13:58:07 -07002172
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002174 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 appWidgetInfo.cellX = c.getInt(cellXIndex);
2176 appWidgetInfo.cellY = c.getInt(cellYIndex);
2177 appWidgetInfo.spanX = c.getInt(spanXIndex);
2178 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002179 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002180
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2182 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2183 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002184 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2185 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186 continue;
2187 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002189 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002191 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002192 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 break;
2194 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002195
Adam Cohen59400422014-03-05 18:07:04 -08002196 if (!customWidget) {
2197 String providerName =
2198 appWidgetInfo.providerName.flattenToString();
2199 if (!providerName.equals(savedProvider) ||
2200 (appWidgetInfo.restoreStatus != restoreStatus)) {
2201 ContentValues values = new ContentValues();
2202 values.put(
2203 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2204 providerName);
2205 values.put(LauncherSettings.Favorites.RESTORED,
2206 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002207 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002208 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002209 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2211 sBgAppWidgets.add(appWidgetInfo);
2212 }
Joe Onorato36115782010-06-17 13:28:48 -04002213 break;
2214 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002216 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002217 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 }
2219 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002220 if (c != null) {
2221 c.close();
2222 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223 }
2224
Winson Chungba9c37f2013-08-30 14:11:37 -07002225 // Break early if we've stopped loading
2226 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002227 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002228 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002229 }
2230
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002233 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2234 Utilities.createDbSelectionQuery(
2235 LauncherSettings.Favorites._ID, itemsToRemove), null);
2236 if (DEBUG_LOADERS) {
2237 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2238 LauncherSettings.Favorites._ID, itemsToRemove));
2239 }
2240
2241 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002242 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2243 .call(contentResolver,
2244 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2245 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2246 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002247 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2248 sBgFolders.remove(folderId);
2249 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002250 }
2251 }
2252
Sunny Goyal317698b2015-07-29 11:45:41 -07002253 // Sort all the folder items and make sure the first 3 items are high resolution.
2254 for (FolderInfo folder : sBgFolders) {
2255 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2256 int pos = 0;
2257 for (ShortcutInfo info : folder.contents) {
2258 if (info.usingLowResIcon) {
2259 info.updateIcon(mIconCache, false);
2260 }
2261 pos ++;
2262 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2263 break;
2264 }
2265 }
2266 }
2267
Chris Wrenf4d08112014-01-16 18:13:56 -05002268 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002269 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002270 ContentValues values = new ContentValues();
2271 values.put(LauncherSettings.Favorites.RESTORED, 0);
2272 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2273 Utilities.createDbSelectionQuery(
2274 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002275 }
2276
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002277 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2278 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002279 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002280 null, sWorker);
2281 }
2282
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002283 // Remove any empty screens
2284 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002285 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002286 long screenId = item.screenId;
2287 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2288 unusedScreens.contains(screenId)) {
2289 unusedScreens.remove(screenId);
2290 }
2291 }
2292
2293 // If there are any empty screens remove them, and update.
2294 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002295 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 }
2298
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002299 if (DEBUG_LOADERS) {
2300 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2301 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002303 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002305
Sunny Goyale2df0622015-04-24 11:27:00 -07002306 for (int i = 0; i < nScreens; i++) {
2307 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002308 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 line += " | ";
2310 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002311 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002312 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002313 if (x < screen.length && y < screen[x].length) {
2314 line += (screen[x][y] != null) ? "#" : ".";
2315 } else {
2316 line += "!";
2317 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002318 }
Joe Onorato36115782010-06-17 13:28:48 -04002319 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002321 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002322 }
Joe Onorato36115782010-06-17 13:28:48 -04002323 }
Adam Cohene25af792013-06-06 23:08:25 -07002324 }
2325
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002326 /**
2327 * Partially updates the item without any notification. Must be called on the worker thread.
2328 */
2329 private void updateItem(long itemId, ContentValues update) {
2330 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002331 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002332 update,
2333 BaseColumns._ID + "= ?",
2334 new String[]{Long.toString(itemId)});
2335 }
2336
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 /** Filters the set of items who are directly or indirectly (via another container) on the
2338 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002339 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 ArrayList<ItemInfo> allWorkspaceItems,
2341 ArrayList<ItemInfo> currentScreenItems,
2342 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002343 // Purge any null ItemInfos
2344 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2345 while (iter.hasNext()) {
2346 ItemInfo i = iter.next();
2347 if (i == null) {
2348 iter.remove();
2349 }
2350 }
2351
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 // Order the set of items by their containers first, this allows use to walk through the
2353 // list sequentially, build up a list of containers that are in the specified screen,
2354 // as well as all items in those containers.
2355 Set<Long> itemsOnScreen = new HashSet<Long>();
2356 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2357 @Override
2358 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002359 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360 }
2361 });
2362 for (ItemInfo info : allWorkspaceItems) {
2363 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002364 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002365 currentScreenItems.add(info);
2366 itemsOnScreen.add(info.id);
2367 } else {
2368 otherScreenItems.add(info);
2369 }
2370 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2371 currentScreenItems.add(info);
2372 itemsOnScreen.add(info.id);
2373 } else {
2374 if (itemsOnScreen.contains(info.container)) {
2375 currentScreenItems.add(info);
2376 itemsOnScreen.add(info.id);
2377 } else {
2378 otherScreenItems.add(info);
2379 }
2380 }
2381 }
2382 }
2383
2384 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002385 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386 ArrayList<LauncherAppWidgetInfo> appWidgets,
2387 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2388 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002389
2390 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002391 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002393 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002394 currentScreenWidgets.add(widget);
2395 } else {
2396 otherScreenWidgets.add(widget);
2397 }
2398 }
2399 }
2400
2401 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002402 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002403 LongArrayMap<ItemInfo> itemsIdMap,
2404 LongArrayMap<FolderInfo> folders,
2405 LongArrayMap<FolderInfo> currentScreenFolders,
2406 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407
Sunny Goyale2df0622015-04-24 11:27:00 -07002408 int total = folders.size();
2409 for (int i = 0; i < total; i++) {
2410 long id = folders.keyAt(i);
2411 FolderInfo folder = folders.valueAt(i);
2412
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002414 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002416 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 currentScreenFolders.put(id, folder);
2418 } else {
2419 otherScreenFolders.put(id, folder);
2420 }
2421 }
2422 }
2423
2424 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2425 * right) */
2426 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002427 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002428 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002429 final int screenCols = profile.numColumns;
2430 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002432 @Override
2433 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002434 if (lhs.container == rhs.container) {
2435 // Within containers, order by their spatial position in that container
2436 switch ((int) lhs.container) {
2437 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2438 long lr = (lhs.screenId * screenCellCount +
2439 lhs.cellY * screenCols + lhs.cellX);
2440 long rr = (rhs.screenId * screenCellCount +
2441 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002442 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002443 }
2444 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2445 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002446 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002447 }
2448 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002449 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002450 throw new RuntimeException("Unexpected container type when " +
2451 "sorting workspace items.");
2452 }
2453 return 0;
2454 }
2455 } else {
2456 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002457 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002458 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002459 }
2460 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002461 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002462
Adam Cohendcd297f2013-06-18 13:13:40 -07002463 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2464 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002465 final Runnable r = new Runnable() {
2466 @Override
2467 public void run() {
2468 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2469 if (callbacks != null) {
2470 callbacks.bindScreens(orderedScreens);
2471 }
2472 }
2473 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002474 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002475 }
2476
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2478 final ArrayList<ItemInfo> workspaceItems,
2479 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002480 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002481 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482
2483 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002484 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002486 final int start = i;
2487 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 final Runnable r = new Runnable() {
2489 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002491 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002492 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002493 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2494 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 }
2496 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002498 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002499 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500
2501 // Bind the folders
2502 if (!folders.isEmpty()) {
2503 final Runnable r = new Runnable() {
2504 public void run() {
2505 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2506 if (callbacks != null) {
2507 callbacks.bindFolders(folders);
2508 }
2509 }
2510 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002511 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002512 }
2513
2514 // Bind the widgets, one at a time
2515 N = appWidgets.size();
2516 for (int i = 0; i < N; i++) {
2517 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2518 final Runnable r = new Runnable() {
2519 public void run() {
2520 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2521 if (callbacks != null) {
2522 callbacks.bindAppWidget(widget);
2523 }
2524 }
2525 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002526 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527 }
2528 }
2529
2530 /**
2531 * Binds all loaded data to actual views on the main thread.
2532 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002533 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 final long t = SystemClock.uptimeMillis();
2535 Runnable r;
2536
2537 // Don't use these two variables in any of the callback runnables.
2538 // Otherwise we hold a reference to them.
2539 final Callbacks oldCallbacks = mCallbacks.get();
2540 if (oldCallbacks == null) {
2541 // This launcher has exited and nobody bothered to tell us. Just bail.
2542 Log.w(TAG, "LoaderTask running with no launcher");
2543 return;
2544 }
2545
Winson Chung9b9fb962013-11-15 15:39:34 -08002546 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2548 ArrayList<LauncherAppWidgetInfo> appWidgets =
2549 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002550 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002551
2552 final LongArrayMap<FolderInfo> folders;
2553 final LongArrayMap<ItemInfo> itemsIdMap;
2554
Winson Chung2abf94d2012-07-18 18:16:38 -07002555 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 workspaceItems.addAll(sBgWorkspaceItems);
2557 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002558 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002559
2560 folders = sBgFolders.clone();
2561 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 }
2563
Derek Prothro7aff3992013-12-10 14:00:37 -05002564 final boolean isLoadingSynchronously =
2565 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002566 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002567 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002568 if (currScreen >= orderedScreenIds.size()) {
2569 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002570 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002571 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002572 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002573 final long currentScreenId = currentScreen < 0
2574 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002575
2576 // Load all the items that are on the current page first (and in the process, unbind
2577 // all the existing workspace items before we call startBinding() below.
2578 unbindWorkspaceItemsOnMainThread();
2579
2580 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002581 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2582 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2583 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2584 new ArrayList<LauncherAppWidgetInfo>();
2585 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2586 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002587 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2588 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002589
Winson Chung9b9fb962013-11-15 15:39:34 -08002590 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002591 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002592 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002593 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002594 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002595 otherFolders);
2596 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2597 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2598
2599 // Tell the workspace that we're about to start binding items
2600 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002601 public void run() {
2602 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2603 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002604 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002605 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002606 }
2607 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002608 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002609 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002610
Adam Cohendcd297f2013-06-18 13:13:40 -07002611 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2612
Sunny Goyal527c7d32015-08-28 15:19:36 -07002613 Executor mainExecutor = new DeferredMainThreadExecutor();
2614 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002615 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002616 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002617
Sunny Goyal527c7d32015-08-28 15:19:36 -07002618 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2619 // remaining screens after first onDraw is called. This ensures that the first screen
2620 // is immediately visible (eg. during rotation)
2621 // In case of !isLoadingSynchronously, bind all pages one after other.
2622 final Executor deferredExecutor = isLoadingSynchronously ?
2623 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2624
2625 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2626 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627
2628 // Tell the workspace that we're done binding items
2629 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002630 public void run() {
2631 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2632 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002633 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002634 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635
Sunny Goyal639e9062015-08-19 19:17:06 -07002636 mIsLoadingAndBindingWorkspace = false;
2637
2638 // Run all the bind complete runnables after workspace is bound.
2639 if (!mBindCompleteRunnables.isEmpty()) {
2640 synchronized (mBindCompleteRunnables) {
2641 for (final Runnable r : mBindCompleteRunnables) {
2642 runOnWorkerThread(r);
2643 }
2644 mBindCompleteRunnables.clear();
2645 }
2646 }
2647
Winson Chung98e030b2012-05-07 16:01:11 -07002648 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002649 if (DEBUG_LOADERS) {
2650 Log.d(TAG, "bound workspace in "
2651 + (SystemClock.uptimeMillis()-t) + "ms");
2652 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002653
Joe Onorato36115782010-06-17 13:28:48 -04002654 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002655 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002656 deferredExecutor.execute(r);
2657
Winson Chung4a2afa32012-07-19 14:53:05 -07002658 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002659 r = new Runnable() {
2660 public void run() {
2661 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2662 if (callbacks != null) {
2663 // We are loading synchronously, which means, some of the pages will be
2664 // bound after first draw. Inform the callbacks that page binding is
2665 // not complete, and schedule the remaining pages.
2666 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2667 callbacks.onPageBoundSynchronously(currentScreen);
2668 }
2669 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2670 }
2671 }
2672 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002673 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002674 }
Joe Onorato36115782010-06-17 13:28:48 -04002675 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002676
Joe Onorato36115782010-06-17 13:28:48 -04002677 private void loadAndBindAllApps() {
2678 if (DEBUG_LOADERS) {
2679 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2680 }
2681 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002682 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002683 synchronized (LoaderTask.this) {
2684 if (mStopped) {
2685 return;
2686 }
2687 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002688 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002689 synchronized (LoaderTask.this) {
2690 if (mStopped) {
2691 return;
2692 }
2693 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002694 }
Joe Onorato36115782010-06-17 13:28:48 -04002695 } else {
2696 onlyBindAllApps();
2697 }
2698 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002699
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002700 private void updateIconCache() {
2701 // Ignore packages which have a promise icon.
2702 HashSet<String> packagesToIgnore = new HashSet<>();
2703 synchronized (sBgLock) {
2704 for (ItemInfo info : sBgItemsIdMap) {
2705 if (info instanceof ShortcutInfo) {
2706 ShortcutInfo si = (ShortcutInfo) info;
2707 if (si.isPromise() && si.getTargetComponent() != null) {
2708 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2709 }
2710 } else if (info instanceof LauncherAppWidgetInfo) {
2711 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2712 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2713 packagesToIgnore.add(lawi.providerName.getPackageName());
2714 }
2715 }
2716 }
2717 }
2718 mIconCache.updateDbIcons(packagesToIgnore);
2719 }
2720
Joe Onorato36115782010-06-17 13:28:48 -04002721 private void onlyBindAllApps() {
2722 final Callbacks oldCallbacks = mCallbacks.get();
2723 if (oldCallbacks == null) {
2724 // This launcher has exited and nobody bothered to tell us. Just bail.
2725 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2726 return;
2727 }
2728
2729 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002730 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002731 final ArrayList<AppInfo> list
2732 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002733 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002734 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002735 public void run() {
2736 final long t = SystemClock.uptimeMillis();
2737 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2738 if (callbacks != null) {
2739 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002740 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002741 }
2742 if (DEBUG_LOADERS) {
2743 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2744 + (SystemClock.uptimeMillis()-t) + "ms");
2745 }
2746 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002747 };
2748 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002749 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002750 r.run();
2751 } else {
2752 mHandler.post(r);
2753 }
Joe Onorato36115782010-06-17 13:28:48 -04002754 }
2755
Winson Chung64359a52013-07-08 17:17:08 -07002756 private void loadAllApps() {
2757 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002758
Joe Onorato36115782010-06-17 13:28:48 -04002759 final Callbacks oldCallbacks = mCallbacks.get();
2760 if (oldCallbacks == null) {
2761 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002762 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002763 return;
2764 }
2765
Kenny Guyed131872014-04-30 03:02:21 +01002766 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2767
Winson Chung64359a52013-07-08 17:17:08 -07002768 // Clear the list of apps
2769 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002770 for (UserHandleCompat user : profiles) {
2771 // Query for the set of apps
2772 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002773 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002774 if (DEBUG_LOADERS) {
2775 Log.d(TAG, "getActivityList took "
2776 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2777 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2778 }
2779 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002780 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002781 if (apps == null || apps.isEmpty()) {
2782 return;
2783 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002784
Kenny Guyed131872014-04-30 03:02:21 +01002785 // Create the ApplicationInfos
2786 for (int i = 0; i < apps.size(); i++) {
2787 LauncherActivityInfoCompat app = apps.get(i);
2788 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002789 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002790 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002791
Sunny Goyal756a28a2015-04-23 17:07:55 -07002792 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2793 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002794 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002795
2796 @Override
2797 public void run() {
2798 heuristic.processUserApps(apps);
2799 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002800 };
2801 runOnMainThread(new Runnable() {
2802
2803 @Override
2804 public void run() {
2805 // Check isLoadingWorkspace on the UI thread, as it is updated on
2806 // the UI thread.
2807 if (mIsLoadingAndBindingWorkspace) {
2808 synchronized (mBindCompleteRunnables) {
2809 mBindCompleteRunnables.add(r);
2810 }
2811 } else {
2812 runOnWorkerThread(r);
2813 }
2814 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002815 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002816 }
Winson Chung64359a52013-07-08 17:17:08 -07002817 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002818 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002819 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2820 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002821
2822 // Post callback on main thread
2823 mHandler.post(new Runnable() {
2824 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002825
Winson Chung64359a52013-07-08 17:17:08 -07002826 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002827 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002828 if (callbacks != null) {
2829 callbacks.bindAllApplications(added);
2830 if (DEBUG_LOADERS) {
2831 Log.d(TAG, "bound " + added.size() + " apps in "
2832 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2833 }
2834 } else {
2835 Log.i(TAG, "not binding apps: no Launcher activity");
2836 }
2837 }
2838 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002839 // Cleanup any data stored for a deleted user.
2840 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002841
Sunny Goyal2d648b02015-08-11 13:56:28 -07002842 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002843 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002844 Log.d(TAG, "Icons processed in "
2845 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002846 }
2847 }
2848
2849 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002850 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002851 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002852 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2853 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2854 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2855 }
Joe Onorato36115782010-06-17 13:28:48 -04002856 }
2857 }
2858
Sunny Goyal75b0f552015-05-20 21:57:06 -07002859 /**
2860 * Called when the icons for packages have been updated in the icon cache.
2861 */
2862 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2863 final Callbacks callbacks = getCallback();
2864 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2865 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2866
2867 // If any package icon has changed (app was updated while launcher was dead),
2868 // update the corresponding shortcuts.
2869 synchronized (sBgLock) {
2870 for (ItemInfo info : sBgItemsIdMap) {
2871 if (info instanceof ShortcutInfo && user.equals(info.user)
2872 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2873 ShortcutInfo si = (ShortcutInfo) info;
2874 ComponentName cn = si.getTargetComponent();
2875 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2876 si.updateIcon(mIconCache);
2877 updatedShortcuts.add(si);
2878 }
2879 }
2880 }
2881 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2882 }
2883
2884 if (!updatedShortcuts.isEmpty()) {
2885 final UserHandleCompat userFinal = user;
2886 mHandler.post(new Runnable() {
2887
2888 public void run() {
2889 Callbacks cb = getCallback();
2890 if (cb != null && callbacks == cb) {
2891 cb.bindShortcutsChanged(updatedShortcuts,
2892 new ArrayList<ShortcutInfo>(), userFinal);
2893 }
2894 }
2895 });
2896 }
2897
2898 if (!updatedApps.isEmpty()) {
2899 mHandler.post(new Runnable() {
2900
2901 public void run() {
2902 Callbacks cb = getCallback();
2903 if (cb != null && callbacks == cb) {
2904 cb.bindAppsUpdated(updatedApps);
2905 }
2906 }
2907 });
2908 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002909
2910 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002911 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002912 }
2913
Joe Onorato36115782010-06-17 13:28:48 -04002914 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002915 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002916 }
2917
Adam Cohen091440a2015-03-18 14:16:05 -07002918 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002919
2920 @Override
2921 public void onReceive(Context context, Intent intent) {
2922 synchronized (sBgLock) {
2923 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2924 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002925 final PackageManager manager = context.getPackageManager();
2926 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2927 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002928 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2929 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002930 packagesRemoved.clear();
2931 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002932 for (String pkg : entry.getValue()) {
2933 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002934 boolean packageOnSdcard = launcherApps.isAppEnabled(
2935 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2936 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002937 packagesUnavailable.add(pkg);
2938 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002939 packagesRemoved.add(pkg);
2940 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002941 }
2942 }
2943 if (!packagesRemoved.isEmpty()) {
2944 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2945 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2946 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002947 if (!packagesUnavailable.isEmpty()) {
2948 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2949 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2950 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002951 }
Sunny Goyal34942622014-08-29 17:20:55 -07002952 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002953 }
2954 }
2955 }
2956
Joe Onorato36115782010-06-17 13:28:48 -04002957 private class PackageUpdatedTask implements Runnable {
2958 int mOp;
2959 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002960 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002961
2962 public static final int OP_NONE = 0;
2963 public static final int OP_ADD = 1;
2964 public static final int OP_UPDATE = 2;
2965 public static final int OP_REMOVE = 3; // uninstlled
2966 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2967
2968
Kenny Guyed131872014-04-30 03:02:21 +01002969 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002970 mOp = op;
2971 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002972 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002973 }
2974
2975 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002976 if (!mHasLoaderCompletedOnce) {
2977 // Loader has not yet run.
2978 return;
2979 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002980 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002981
2982 final String[] packages = mPackages;
2983 final int N = packages.length;
2984 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002985 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002986 for (int i=0; i<N; i++) {
2987 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002988 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002989 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002990 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002991
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002992 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2993 if (heuristic != null) {
2994 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002995 }
Joe Onorato36115782010-06-17 13:28:48 -04002996 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002997 }
Joe Onorato36115782010-06-17 13:28:48 -04002998 case OP_UPDATE:
2999 for (int i=0; i<N; i++) {
3000 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003001 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003002 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003003 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003004 }
3005 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003006 case OP_REMOVE: {
3007 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3008 if (heuristic != null) {
3009 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003010 }
Joe Onorato36115782010-06-17 13:28:48 -04003011 for (int i=0; i<N; i++) {
3012 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003013 mIconCache.removeIconsForPkg(packages[i], mUser);
3014 }
3015 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003016 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003017 case OP_UNAVAILABLE:
3018 for (int i=0; i<N; i++) {
3019 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3020 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003021 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003022 }
3023 break;
3024 }
3025
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003026 ArrayList<AppInfo> added = null;
3027 ArrayList<AppInfo> modified = null;
3028 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003029
Adam Cohen487f7dd2012-06-28 18:12:10 -07003030 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003031 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003032 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003033 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003034 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003035 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003036 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003037 }
Winson Chung5d55f332012-07-16 20:45:03 -07003038 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003039 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003040 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003041 }
3042
Sunny Goyale0f58d72014-11-10 18:05:31 -08003043 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003044 if (callbacks == null) {
3045 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3046 return;
3047 }
3048
Sunny Goyal4390ace2014-10-13 11:33:11 -07003049 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3050 new HashMap<ComponentName, AppInfo>();
3051
Joe Onorato36115782010-06-17 13:28:48 -04003052 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003053 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003054 for (AppInfo ai : added) {
3055 addedOrUpdatedApps.put(ai.componentName, ai);
3056 }
Joe Onorato36115782010-06-17 13:28:48 -04003057 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003058
Joe Onorato36115782010-06-17 13:28:48 -04003059 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003060 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061 for (AppInfo ai : modified) {
3062 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003063 }
3064
Joe Onorato36115782010-06-17 13:28:48 -04003065 mHandler.post(new Runnable() {
3066 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003067 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003068 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003069 callbacks.bindAppsUpdated(modifiedFinal);
3070 }
3071 }
3072 });
3073 }
Winson Chung83892cc2013-05-01 16:53:33 -07003074
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003076 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3078 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3079 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3080
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003081 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003082 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003083 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003084 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3085 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003086 boolean infoUpdated = false;
3087 boolean shortcutUpdated = false;
3088
3089 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003090 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003091 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003092 Bitmap icon = Utilities.createIconBitmap(
3093 si.iconResource.packageName,
3094 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003095 if (icon != null) {
3096 si.setIcon(icon);
3097 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003098 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003099 }
3100 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003101
3102 ComponentName cn = si.getTargetComponent();
3103 if (cn != null && packageSet.contains(cn.getPackageName())) {
3104 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3105
3106 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003107 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3108 // Auto install icon
3109 PackageManager pm = context.getPackageManager();
3110 ResolveInfo matched = pm.resolveActivity(
3111 new Intent(Intent.ACTION_MAIN)
3112 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3113 PackageManager.MATCH_DEFAULT_ONLY);
3114 if (matched == null) {
3115 // Try to find the best match activity.
3116 Intent intent = pm.getLaunchIntentForPackage(
3117 cn.getPackageName());
3118 if (intent != null) {
3119 cn = intent.getComponent();
3120 appInfo = addedOrUpdatedApps.get(cn);
3121 }
3122
3123 if ((intent == null) || (appInfo == null)) {
3124 removedShortcuts.add(si);
3125 continue;
3126 }
3127 si.promisedIntent = intent;
3128 }
3129 }
3130
3131 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003132 if (appInfo != null) {
3133 si.flags = appInfo.flags;
3134 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135
Sunny Goyal756adbc2015-04-16 15:20:51 -07003136 si.intent = si.promisedIntent;
3137 si.promisedIntent = null;
3138 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139 infoUpdated = true;
3140 si.updateIcon(mIconCache);
3141 }
3142
3143 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3144 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3145 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003146 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003147 si.contentDescription = appInfo.contentDescription;
3148 infoUpdated = true;
3149 }
3150
3151 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3152 // Since package was just updated, the target must be available now.
3153 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3154 shortcutUpdated = true;
3155 }
3156 }
3157
3158 if (infoUpdated || shortcutUpdated) {
3159 updatedShortcuts.add(si);
3160 }
3161 if (infoUpdated) {
3162 updateItemInDatabase(context, si);
3163 }
3164 } else if (info instanceof LauncherAppWidgetInfo) {
3165 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3166 if (mUser.equals(widgetInfo.user)
3167 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3168 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003169 widgetInfo.restoreStatus &=
3170 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3171 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003172
3173 // adding this flag ensures that launcher shows 'click to setup'
3174 // if the widget has a config activity. In case there is no config
3175 // activity, it will be marked as 'restored' during bind.
3176 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3177
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 widgets.add(widgetInfo);
3179 updateItemInDatabase(context, widgetInfo);
3180 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003181 }
3182 }
3183 }
3184
Sunny Goyal4390ace2014-10-13 11:33:11 -07003185 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3186 mHandler.post(new Runnable() {
3187
3188 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003189 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003190 if (callbacks == cb && cb != null) {
3191 callbacks.bindShortcutsChanged(
3192 updatedShortcuts, removedShortcuts, mUser);
3193 }
3194 }
3195 });
3196 if (!removedShortcuts.isEmpty()) {
3197 deleteItemsFromDatabase(context, removedShortcuts);
3198 }
3199 }
3200 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003201 mHandler.post(new Runnable() {
3202 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003203 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003204 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003205 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003206 }
3207 }
3208 });
3209 }
3210 }
3211
Winson Chungdf95eb12013-10-16 14:57:07 -07003212 final ArrayList<String> removedPackageNames =
3213 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003214 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003215 // Mark all packages in the broadcast to be removed
3216 removedPackageNames.addAll(Arrays.asList(packages));
3217 } else if (mOp == OP_UPDATE) {
3218 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003219 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003220 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003221 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003222 }
3223 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003224 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003225
Winson Chungdf95eb12013-10-16 14:57:07 -07003226 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003227 final int removeReason;
3228 if (mOp == OP_UNAVAILABLE) {
3229 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3230 } else {
3231 // Remove all the components associated with this package
3232 for (String pn : removedPackageNames) {
3233 deletePackageFromDatabase(context, pn, mUser);
3234 }
3235 // Remove all the specific components
3236 for (AppInfo a : removedApps) {
3237 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3238 deleteItemsFromDatabase(context, infos);
3239 }
3240 removeReason = 0;
3241 }
3242
Winson Chungdf95eb12013-10-16 14:57:07 -07003243 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003244 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003245 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003246 mHandler.post(new Runnable() {
3247 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003248 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003249 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003250 callbacks.bindComponentsRemoved(
3251 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003252 }
3253 }
3254 });
Joe Onoratobe386092009-11-17 17:32:16 -08003255 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003256
Sunny Goyal2d648b02015-08-11 13:56:28 -07003257 // Update widgets
3258 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3259 // Always refresh for a package event on secondary user
3260 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3261
3262 // Refresh widget list, if the package already had a widget.
3263 synchronized (sBgLock) {
3264 if (sBgWidgetProviders != null) {
3265 HashSet<String> pkgSet = new HashSet<>();
3266 Collections.addAll(pkgSet, mPackages);
3267
3268 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3269 needToRefresh |= key.user.equals(mUser) &&
3270 pkgSet.contains(key.componentName.getPackageName());
3271 }
3272 }
3273 }
3274
3275 if (!needToRefresh && mOp != OP_REMOVE) {
3276 // Refresh widget list, if there is any newly added widget
3277 PackageManager pm = context.getPackageManager();
3278 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003279 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003280 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003281 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003282 .setPackage(pkg), 0);
3283 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003284 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003285 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003286 throw e;
3287 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003288 // Ignore the crash. We can live with a state widget list.
3289 Log.e(TAG, "PM call failed for " + pkg, e);
3290 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003291 }
3292 }
3293
3294 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 }
3297 }
3298
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003299 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3300 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003301 ArrayList<LauncherAppWidgetProviderInfo> results =
3302 new ArrayList<LauncherAppWidgetProviderInfo>();
3303 try {
3304 synchronized (sBgLock) {
3305 if (sBgWidgetProviders == null || refresh) {
3306 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3307 = new HashMap<>();
3308 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3309 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003310
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003311 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3312 for (AppWidgetProviderInfo pInfo : widgets) {
3313 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3314 UserHandleCompat user = wm.getUser(info);
3315 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3316 }
Robin Lee26ace122015-03-16 19:41:43 +00003317
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003318 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3319 for (CustomAppWidget widget : customWidgets) {
3320 info = new LauncherAppWidgetProviderInfo(context, widget);
3321 UserHandleCompat user = wm.getUser(info);
3322 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3323 }
3324 // Replace the global list at the very end, so that if there is an exception,
3325 // previously loaded provider list is used.
3326 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003327 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003328 results.addAll(sBgWidgetProviders.values());
3329 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003330 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003331 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003332 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003333 (e.getCause() instanceof TransactionTooLargeException ||
3334 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003335 // the returned value may be incomplete and will not be refreshed until the next
3336 // time Launcher starts.
3337 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3338 // onResume is called to refresh the widget provider list.
3339 synchronized (sBgLock) {
3340 if (sBgWidgetProviders != null) {
3341 results.addAll(sBgWidgetProviders.values());
3342 }
3343 return results;
3344 }
3345 } else {
3346 throw e;
3347 }
Adam Cohen59400422014-03-05 18:07:04 -08003348 }
3349 }
3350
Robin Lee26ace122015-03-16 19:41:43 +00003351 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3352 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003353 synchronized (sBgLock) {
3354 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003355 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003356 }
Robin Lee26ace122015-03-16 19:41:43 +00003357 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003358 }
3359 }
3360
Sunny Goyal2d648b02015-08-11 13:56:28 -07003361 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003362
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003363 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003364 @Override
3365 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003366 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003367 final WidgetsModel model = mBgWidgetsModel.clone();
3368
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003369 mHandler.post(new Runnable() {
3370 @Override
3371 public void run() {
3372 Callbacks cb = getCallback();
3373 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003374 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003375 }
3376 }
3377 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003378 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003379 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3380 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003381 }
3382 });
3383 }
3384
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003385 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003386 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003387 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003388 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003389 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003390 @Thunk void updateWidgetsModel(boolean refresh) {
3391 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003392 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003393 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003394
3395 // Update shortcut providers
3396 synchronized (sBgLock) {
3397 try {
3398 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3399 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3400 sBgShortcutProviders = providers;
3401 } catch (RuntimeException e) {
Sunny Goyal877f9282015-09-18 10:18:18 -07003402 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal31860582015-09-18 08:38:57 -07003403 (e.getCause() instanceof TransactionTooLargeException ||
3404 e.getCause() instanceof DeadObjectException)) {
3405 /**
3406 * Ignore exception and use the cached list if available.
3407 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3408 */
3409 } else {
3410 throw e;
3411 }
3412 }
3413 if (sBgShortcutProviders != null) {
3414 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3415 }
3416 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003417 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003418 }
3419
Adam Cohen091440a2015-03-18 14:16:05 -07003420 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003421 UserHandleCompat user) {
3422 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3423 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003424 }
Adam Cohen556f6132014-01-15 15:18:08 -08003425
Kenny Guyed131872014-04-30 03:02:21 +01003426 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3427 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003428 if (cn == null) {
3429 return false;
3430 }
Kenny Guyed131872014-04-30 03:02:21 +01003431 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3432 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003433 return false;
3434 }
Kenny Guyed131872014-04-30 03:02:21 +01003435 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003436 }
3437
Adam Cohena28b78e2014-05-20 17:03:04 -07003438 public static boolean isValidPackage(Context context, String packageName,
3439 UserHandleCompat user) {
3440 if (packageName == null) {
3441 return false;
3442 }
3443 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3444 return launcherApps.isPackageEnabledForProfile(packageName, user);
3445 }
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003448 * Make an ShortcutInfo object for a restored application or shortcut item that points
3449 * to a package that is not yet installed on the system.
3450 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003451 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003452 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003453 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003454 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003455
3456 Bitmap icon = iconInfo.loadIcon(c, info, context);
3457 // the fallback icon
3458 if (icon == null) {
3459 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3460 } else {
3461 info.setIcon(icon);
3462 }
Sunny Goyal34942622014-08-29 17:20:55 -07003463
3464 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003465 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003466 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003467 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003468 }
Sunny Goyal34942622014-08-29 17:20:55 -07003469 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3470 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003471 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003472 }
Sunny Goyal34942622014-08-29 17:20:55 -07003473 } else {
3474 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3475 }
3476
Winson Chung82b016c2015-05-08 17:00:10 -07003477 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003478 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003479 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003480 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003481 return info;
3482 }
3483
3484 /**
3485 * Make an Intent object for a restored application or shortcut item that points
3486 * to the market page for the item.
3487 */
Adam Cohen091440a2015-03-18 14:16:05 -07003488 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003489 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003490 return getMarketIntent(componentName.getPackageName());
3491 }
3492
3493 static Intent getMarketIntent(String packageName) {
3494 return new Intent(Intent.ACTION_VIEW)
3495 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003496 .scheme("market")
3497 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003498 .appendQueryParameter("id", packageName)
3499 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003500 }
3501
3502 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003503 * Make an ShortcutInfo object for a shortcut that is an application.
3504 *
3505 * If c is not null, then it will be used to fill in missing data like the title and icon.
3506 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003507 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003508 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003509 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003510 if (user == null) {
3511 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003512 return null;
3513 }
3514
Kenny Guyed131872014-04-30 03:02:21 +01003515 ComponentName componentName = intent.getComponent();
3516 if (componentName == null) {
3517 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3518 return null;
3519 }
3520
3521 Intent newIntent = new Intent(intent.getAction(), null);
3522 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3523 newIntent.setComponent(componentName);
3524 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003525 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003526 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3527 return null;
3528 }
3529
3530 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003531 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003532 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3533 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3534 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003535 }
3536
Joe Onorato56d82912010-03-07 14:32:10 -05003537 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003538 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003539 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003540 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003541
Joe Onorato56d82912010-03-07 14:32:10 -05003542 // fall back to the class name of the activity
3543 if (info.title == null) {
3544 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003545 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003546
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003548 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003549 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003550 if (lai != null) {
3551 info.flags = AppInfo.initFlags(lai);
3552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003554 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003555
Sunny Goyale2df0622015-04-24 11:27:00 -07003556 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003557 ItemInfoFilter f) {
3558 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3559 for (ItemInfo i : infos) {
3560 if (i instanceof ShortcutInfo) {
3561 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003562 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003563 if (cn != null && f.filterItem(null, info, cn)) {
3564 filtered.add(info);
3565 }
3566 } else if (i instanceof FolderInfo) {
3567 FolderInfo info = (FolderInfo) i;
3568 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003569 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003570 if (cn != null && f.filterItem(info, s, cn)) {
3571 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003572 }
3573 }
Winson Chung64359a52013-07-08 17:17:08 -07003574 } else if (i instanceof LauncherAppWidgetInfo) {
3575 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3576 ComponentName cn = info.providerName;
3577 if (cn != null && f.filterItem(null, info, cn)) {
3578 filtered.add(info);
3579 }
Winson Chung8a435102012-08-30 17:16:53 -07003580 }
3581 }
Winson Chung64359a52013-07-08 17:17:08 -07003582 return new ArrayList<ItemInfo>(filtered);
3583 }
3584
Adam Cohen091440a2015-03-18 14:16:05 -07003585 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003586 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003587 ItemInfoFilter filter = new ItemInfoFilter() {
3588 @Override
3589 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003590 if (info.user == null) {
3591 return cn.equals(cname);
3592 } else {
3593 return cn.equals(cname) && info.user.equals(user);
3594 }
Winson Chung64359a52013-07-08 17:17:08 -07003595 }
3596 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003597 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003598 }
3599
Sunny Goyal1a745e82014-10-02 15:58:31 -07003600 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003601 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003602 */
Adam Cohen091440a2015-03-18 14:16:05 -07003603 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003604 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003605 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003606 // Non-app shortcuts are only supported for current user.
3607 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003609
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003610 // TODO: If there's an explicit component and we can't install that, delete it.
3611
Winson Chung82b016c2015-05-08 17:00:10 -07003612 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003613
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003614 Bitmap icon = iconInfo.loadIcon(c, info, context);
3615 // the fallback icon
3616 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003617 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003618 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003619 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003620 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 return info;
3622 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003623
Sunny Goyal2350bc92014-10-14 16:42:54 -07003624 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003625 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3626 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3627 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3628
Adam Cohend9198822011-11-22 16:42:47 -08003629 if (intent == null) {
3630 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3631 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3632 return null;
3633 }
3634
Joe Onorato0589f0f2010-02-08 13:44:00 -08003635 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003636 boolean customIcon = false;
3637 ShortcutIconResource iconResource = null;
3638
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003639 if (bitmap instanceof Bitmap) {
3640 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003641 customIcon = true;
3642 } else {
3643 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003644 if (extra instanceof ShortcutIconResource) {
3645 iconResource = (ShortcutIconResource) extra;
3646 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003647 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003648 }
3649 }
3650
Michael Jurkac9d95c52011-08-29 14:03:34 -07003651 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003652
Kenny Guyed131872014-04-30 03:02:21 +01003653 // Only support intents for current user for now. Intents sent from other
3654 // users wouldn't get here without intent forwarding anyway.
3655 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003656 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003657 icon = mIconCache.getDefaultIcon(info.user);
3658 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003659 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003660 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003661
Winson Chung82b016c2015-05-08 17:00:10 -07003662 info.title = Utilities.trim(name);
3663 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003664 info.intent = intent;
3665 info.customIcon = customIcon;
3666 info.iconResource = iconResource;
3667
3668 return info;
3669 }
3670
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003672 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003673 * or make a new one.
3674 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003675 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676 // See if a placeholder was created for us already
3677 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003678 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003680 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 folders.put(id, folderInfo);
3682 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003683 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003684 }
3685
Joe Onoratobe386092009-11-17 17:32:16 -08003686
Sunny Goyal651077b2014-06-30 14:15:31 -07003687 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3688 return (provider != null) && (provider.provider != null)
3689 && (provider.provider.getPackageName() != null);
3690 }
3691
Joe Onoratobe386092009-11-17 17:32:16 -08003692 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003693 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003694 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3695 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3696 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3697 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003698 if (mLoaderTask != null) {
3699 mLoaderTask.dumpState();
3700 } else {
3701 Log.d(TAG, "mLoaderTask=null");
3702 }
Joe Onoratobe386092009-11-17 17:32:16 -08003703 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003704
3705 public Callbacks getCallback() {
3706 return mCallbacks != null ? mCallbacks.get() : null;
3707 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003708
3709 /**
3710 * @return {@link FolderInfo} if its already loaded.
3711 */
3712 public FolderInfo findFolderById(Long folderId) {
3713 synchronized (sBgLock) {
3714 return sBgFolders.get(folderId);
3715 }
3716 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003717
Sunny Goyal527c7d32015-08-28 15:19:36 -07003718 @Thunk class DeferredMainThreadExecutor implements Executor {
3719
3720 @Override
3721 public void execute(Runnable command) {
3722 runOnMainThread(command);
3723 }
3724 }
3725
Sunny Goyal756adbc2015-04-16 15:20:51 -07003726 /**
3727 * @return the looper for the worker thread which can be used to start background tasks.
3728 */
3729 public static Looper getWorkerLooper() {
3730 return sWorkerThread.getLooper();
3731 }
Sunny Goyala1365452015-10-01 15:46:24 -07003732
3733 @Thunk static final void addDumpLog(String log) {
3734 Launcher.addDumpLog(TAG, log);
3735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003736}