blob: 97a55590b4b06a39611178a78ccbded47bdd414a [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);
Winson Chungede41292013-09-19 16:27:36 -0700213 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chung64359a52013-07-08 17:17:08 -0700216 public interface ItemInfoFilter {
217 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
218 }
219
Bjorn Bringert1307f632013-10-03 22:31:03 +0100220 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800221 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400222
Winson Chungee055712013-07-30 14:46:24 -0700223 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700224 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400225 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
226 // resource string.
227 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
228 ProviderInfo providerInfo =
229 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
230 ProviderInfo redirectProvider =
231 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700232
233 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400234 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700235
236 if (mOldContentProviderExists) {
237 Log.d(TAG, "Old launcher provider exists.");
238 } else {
239 Log.d(TAG, "Old launcher provider does not exist.");
240 }
241
Daniel Sandlere4f98912013-06-25 15:13:26 -0400242 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100243 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700244 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 mIconCache = iconCache;
246
Kenny Guyed131872014-04-30 03:02:21 +0100247 mLauncherApps = LauncherAppsCompat.getInstance(context);
248 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 }
250
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
252 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700253 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700254 if (sWorkerThread.getThreadId() == Process.myTid()) {
255 // If we are on the worker thread, post onto the main handler
256 mHandler.post(r);
257 } else {
258 r.run();
259 }
260 }
261
262 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
263 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700264 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700265 if (sWorkerThread.getThreadId() == Process.myTid()) {
266 r.run();
267 } else {
268 // If we are not on the worker thread, then post to the worker handler
269 sWorker.post(r);
270 }
271 }
272
Winson Chunge43a1e72014-01-15 10:33:02 -0800273 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
274 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800275 }
276
Sunny Goyal756adbc2015-04-16 15:20:51 -0700277 public void setPackageState(final PackageInstallInfo installInfo) {
278 Runnable updateRunnable = new Runnable() {
279
280 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500281 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700282 synchronized (sBgLock) {
283 final HashSet<ItemInfo> updates = new HashSet<>();
284
285 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
286 // Ignore install success events as they are handled by Package add events.
287 return;
288 }
289
Sunny Goyale2df0622015-04-24 11:27:00 -0700290 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700291 if (info instanceof ShortcutInfo) {
292 ShortcutInfo si = (ShortcutInfo) info;
293 ComponentName cn = si.getTargetComponent();
294 if (si.isPromise() && (cn != null)
295 && installInfo.packageName.equals(cn.getPackageName())) {
296 si.setInstallProgress(installInfo.progress);
297
298 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
299 // Mark this info as broken.
300 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
301 }
302 updates.add(si);
303 }
304 }
305 }
306
307 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
308 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
309 widget.installProgress = installInfo.progress;
310 updates.add(widget);
311 }
312 }
313
314 if (!updates.isEmpty()) {
315 // Push changes to the callback.
316 Runnable r = new Runnable() {
317 public void run() {
318 Callbacks callbacks = getCallback();
319 if (callbacks != null) {
320 callbacks.bindRestoreItemsChange(updates);
321 }
322 }
323 };
324 mHandler.post(r);
325 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500326 }
327 }
328 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700329 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500330 }
331
Sunny Goyal756adbc2015-04-16 15:20:51 -0700332 /**
333 * Updates the icons and label of all pending icons for the provided package name.
334 */
335 public void updateSessionDisplayInfo(final String packageName) {
336 Runnable updateRunnable = new Runnable() {
337
338 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700339 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340 synchronized (sBgLock) {
341 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
342 final UserHandleCompat user = UserHandleCompat.myUserHandle();
343
Sunny Goyale2df0622015-04-24 11:27:00 -0700344 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700345 if (info instanceof ShortcutInfo) {
346 ShortcutInfo si = (ShortcutInfo) info;
347 ComponentName cn = si.getTargetComponent();
348 if (si.isPromise() && (cn != null)
349 && packageName.equals(cn.getPackageName())) {
350 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
351 // For auto install apps update the icon as well as label.
352 mIconCache.getTitleAndIcon(si,
353 si.promisedIntent, user,
354 si.shouldUseLowResIcon());
355 } else {
356 // Only update the icon for restored apps.
357 si.updateIcon(mIconCache);
358 }
359 updates.add(si);
360 }
361 }
362 }
363
364 if (!updates.isEmpty()) {
365 // Push changes to the callback.
366 Runnable r = new Runnable() {
367 public void run() {
368 Callbacks callbacks = getCallback();
369 if (callbacks != null) {
370 callbacks.bindShortcutsChanged(updates,
371 new ArrayList<ShortcutInfo>(), user);
372 }
373 }
374 };
375 mHandler.post(r);
376 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700377 }
378 }
379 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700380 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700381 }
382
Adam Cohen76a47a12014-02-05 11:47:43 -0800383 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800384 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800385
386 if (allAppsApps == null) {
387 throw new RuntimeException("allAppsApps must not be null");
388 }
389 if (allAppsApps.isEmpty()) {
390 return;
391 }
392
393 // Process the newly added applications and add them to the database first
394 Runnable r = new Runnable() {
395 public void run() {
396 runOnMainThread(new Runnable() {
397 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800398 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800399 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500400 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800401 }
402 }
403 });
404 }
405 };
406 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700407 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800408
Sunny Goyala9116722015-04-29 13:55:58 -0700409 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800410 int[] xy, int spanX, int spanY) {
411 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
413 final int xCount = (int) profile.numColumns;
414 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800415 boolean[][] occupied = new boolean[xCount][yCount];
416 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700417 for (ItemInfo r : occupiedPos) {
418 int right = r.cellX + r.spanX;
419 int bottom = r.cellY + r.spanY;
420 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
421 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800422 occupied[x][y] = true;
423 }
424 }
425 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800426 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700427 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 }
429
430 /**
431 * Find a position on the screen for the given size or adds a new screen.
432 * @return screenId and the coordinates for the item.
433 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700434 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800435 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800436 ArrayList<Long> workspaceScreens,
437 ArrayList<Long> addedWorkspaceScreensFinal,
438 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700439 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440
Sunny Goyala9116722015-04-29 13:55:58 -0700441 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700442 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700443 synchronized (sBgLock) {
444 for (ItemInfo info : sBgItemsIdMap) {
445 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
446 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
447 if (items == null) {
448 items = new ArrayList<>();
449 screenItems.put(info.screenId, items);
450 }
451 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800454 }
455
456 // Find appropriate space for the item.
457 long screenId = 0;
458 int[] cordinates = new int[2];
459 boolean found = false;
460
461 int screenCount = workspaceScreens.size();
462 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700463 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 if (preferredScreenIndex < screenCount) {
465 screenId = workspaceScreens.get(preferredScreenIndex);
466 found = findNextAvailableIconSpaceInScreen(
467 screenItems.get(screenId), cordinates, spanX, spanY);
468 }
469
470 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700471 // Search on any of the screens starting from the first screen.
472 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800473 screenId = workspaceScreens.get(screen);
474 if (findNextAvailableIconSpaceInScreen(
475 screenItems.get(screenId), cordinates, spanX, spanY)) {
476 // We found a space for it
477 found = true;
478 break;
479 }
480 }
481 }
482
483 if (!found) {
484 // Still no position found. Add a new screen to the end.
485 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
486
487 // Save the screen id for binding in the workspace
488 workspaceScreens.add(screenId);
489 addedWorkspaceScreensFinal.add(screenId);
490
491 // If we still can't find an empty space, then God help us all!!!
492 if (!findNextAvailableIconSpaceInScreen(
493 screenItems.get(screenId), cordinates, spanX, spanY)) {
494 throw new RuntimeException("Can't find space to add the item");
495 }
496 }
497 return Pair.create(screenId, cordinates);
498 }
499
500 /**
501 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700503 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700504 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800506 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700507 return;
Winson Chung997a9232013-07-24 15:33:46 -0700508 }
Winson Chung64359a52013-07-08 17:17:08 -0700509 // Process the newly added applications and add them to the database first
510 Runnable r = new Runnable() {
511 public void run() {
512 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
513 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
514
Winson Chung76828c82013-08-19 15:43:29 -0700515 // Get the list of workspace screens. We need to append to this list and
516 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
517 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800518 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700519 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700521 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700523 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700525 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 }
Winson Chung76828c82013-08-19 15:43:29 -0700527
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800528 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700529 Pair<Long, int[]> coords = findSpaceForItem(context,
530 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 1, 1);
532 long screenId = coords.first;
533 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700534
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700535 ItemInfo itemInfo;
536 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
537 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700539 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700540 } else {
541 throw new RuntimeException("Unexpected info type");
542 }
Winson Chung94d67682013-09-25 16:29:40 -0700543
Winson Chung64359a52013-07-08 17:17:08 -0700544 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700545 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700546 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700547 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700548 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700549 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700550 }
551 }
552
Winson Chung76828c82013-08-19 15:43:29 -0700553 // Update the workspace screens
554 updateWorkspaceScreenOrder(context, workspaceScreens);
555
Adam Cohen76a47a12014-02-05 11:47:43 -0800556 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700557 runOnMainThread(new Runnable() {
558 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800559 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700560 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700561 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
562 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700563 if (!addedShortcutsFinal.isEmpty()) {
564 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
565 long lastScreenId = info.screenId;
566 for (ItemInfo i : addedShortcutsFinal) {
567 if (i.screenId == lastScreenId) {
568 addAnimated.add(i);
569 } else {
570 addNotAnimated.add(i);
571 }
Winson Chung997a9232013-07-24 15:33:46 -0700572 }
573 }
Winson Chungd64d1762013-08-20 14:37:16 -0700574 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800575 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700576 }
Winson Chung64359a52013-07-08 17:17:08 -0700577 }
Winson Chung997a9232013-07-24 15:33:46 -0700578 });
579 }
Winson Chung64359a52013-07-08 17:17:08 -0700580 }
581 };
582 runOnWorkerThread(r);
583 }
584
Sunny Goyald33860f2015-04-23 16:02:20 -0700585 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700586 if (sWorkerThread.getThreadId() == Process.myTid()) {
587 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
588 "main thread");
589 }
590
Sunny Goyald33860f2015-04-23 16:02:20 -0700591 // Remove any queued UI runnables
592 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700593 // Unbind all the workspace items
594 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700595 }
596
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700597 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700598 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700599 // Ensure that we don't use the same workspace items data structure on the main thread
600 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700601 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700602 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700603 tmpItems.addAll(sBgWorkspaceItems);
604 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700605 }
606 Runnable r = new Runnable() {
607 @Override
608 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700609 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700610 item.unbind();
611 }
612 }
613 };
614 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700615 }
616
Joe Onorato9c1289c2009-08-17 11:03:03 -0400617 /**
618 * Adds an item to the DB if it was not created previously, or move it to a new
619 * <container, screen, cellX, cellY>
620 */
621 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700622 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 if (item.container == ItemInfo.NO_ID) {
624 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700625 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 } else {
627 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700628 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 }
630 }
631
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700632 static void checkItemInfoLocked(
633 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
634 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
635 if (modelItem != null && item != modelItem) {
636 // check all the data is consistent
637 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
638 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
639 ShortcutInfo shortcut = (ShortcutInfo) item;
640 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
641 modelShortcut.intent.filterEquals(shortcut.intent) &&
642 modelShortcut.id == shortcut.id &&
643 modelShortcut.itemType == shortcut.itemType &&
644 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700646 modelShortcut.cellX == shortcut.cellX &&
647 modelShortcut.cellY == shortcut.cellY &&
648 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700649 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700650 // For all intents and purposes, this is the same object
651 return;
652 }
653 }
654
655 // the modelItem needs to match up perfectly with item if our model is
656 // to be consistent with the database-- for now, just require
657 // modelItem == item or the equality check above
658 String msg = "item: " + ((item != null) ? item.toString() : "null") +
659 "modelItem: " +
660 ((modelItem != null) ? modelItem.toString() : "null") +
661 "Error: ItemInfo passed to checkItemInfo doesn't match original";
662 RuntimeException e = new RuntimeException(msg);
663 if (stackTrace != null) {
664 e.setStackTrace(stackTrace);
665 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800666 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700667 }
668 }
669
Michael Jurka816474f2012-06-25 14:49:02 -0700670 static void checkItemInfo(final ItemInfo item) {
671 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
672 final long itemId = item.id;
673 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700674 public void run() {
675 synchronized (sBgLock) {
676 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700677 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700678 }
679 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700680 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700681 }
682
Michael Jurkac9d95c52011-08-29 14:03:34 -0700683 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
684 final ItemInfo item, final String callingFunction) {
685 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700686 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700687 final ContentResolver cr = context.getContentResolver();
688
Adam Cohen487f7dd2012-06-28 18:12:10 -0700689 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700690 Runnable r = new Runnable() {
691 public void run() {
692 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700693 updateItemArrays(item, itemId, stackTrace);
694 }
695 };
696 runOnWorkerThread(r);
697 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700698
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700699 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
700 final ArrayList<ItemInfo> items, final String callingFunction) {
701 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700702
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700703 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
704 Runnable r = new Runnable() {
705 public void run() {
706 ArrayList<ContentProviderOperation> ops =
707 new ArrayList<ContentProviderOperation>();
708 int count = items.size();
709 for (int i = 0; i < count; i++) {
710 ItemInfo item = items.get(i);
711 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700712 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700713 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700714
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700715 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
716 updateItemArrays(item, itemId, stackTrace);
717
718 }
719 try {
720 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
721 } catch (Exception e) {
722 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723 }
724 }
725 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700726 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700728
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
730 // Lock on mBgLock *after* the db operation
731 synchronized (sBgLock) {
732 checkItemInfoLocked(itemId, item, stackTrace);
733
734 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
735 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
736 // Item is in a folder, make sure this folder exists
737 if (!sBgFolders.containsKey(item.container)) {
738 // An items container is being set to a that of an item which is not in
739 // the list of Folders.
740 String msg = "item: " + item + " container being set to: " +
741 item.container + ", not in the list of folders";
742 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 }
744 }
745
746 // Items are added/removed from the corresponding FolderInfo elsewhere, such
747 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
748 // that are on the desktop, as appropriate
749 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800750 if (modelItem != null &&
751 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
752 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753 switch (modelItem.itemType) {
754 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
755 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
756 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
757 if (!sBgWorkspaceItems.contains(modelItem)) {
758 sBgWorkspaceItems.add(modelItem);
759 }
760 break;
761 default:
762 break;
763 }
764 } else {
765 sBgWorkspaceItems.remove(modelItem);
766 }
767 }
768 }
769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700772 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700773 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 item.cellX = cellX;
777 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700778
Winson Chung3d503fb2011-07-13 17:25:49 -0700779 // We store hotseat items in canonical form which is this orientation invariant position
780 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787
788 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
791 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800792 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794
Michael Jurkac9d95c52011-08-29 14:03:34 -0700795 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700796 }
797
798 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700799 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
800 * cellX, cellY have already been updated on the ItemInfos.
801 */
802 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
803 final long container, final int screen) {
804
805 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
806 int count = items.size();
807
808 for (int i = 0; i < count; i++) {
809 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700810 item.container = container;
811
812 // We store hotseat items in canonical form which is this orientation invariant position
813 // in the hotseat
814 if (context instanceof Launcher && screen < 0 &&
815 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700817 item.cellY);
818 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700820 }
821
822 final ContentValues values = new ContentValues();
823 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
824 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
825 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800826 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700827 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828
829 contentValues.add(values);
830 }
831 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
832 }
833
834 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700835 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800836 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700837 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700838 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700839 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800840 item.cellX = cellX;
841 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700842 item.spanX = spanX;
843 item.spanY = spanY;
844
845 // We store hotseat items in canonical form which is this orientation invariant position
846 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700848 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700850 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700852 }
Adam Cohend4844c32011-02-18 19:25:06 -0800853
Adam Cohend4844c32011-02-18 19:25:06 -0800854 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800855 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700856 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
857 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800858 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
860 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800862
Michael Jurka816474f2012-06-25 14:49:02 -0700863 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700865
866 /**
867 * Update an item to the database in a specified container.
868 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700869 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700870 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100871 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700872 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800873 }
874
Sunny Goyal756a28a2015-04-23 17:07:55 -0700875 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700876 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700877 synchronized (mLock) {
878 if (!mHasLoaderCompletedOnce ||
879 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
880 throw new RuntimeException("Trying to add shortcut while loader is running");
881 }
882 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700883 }
884 }
885
Adam Cohend4844c32011-02-18 19:25:06 -0800886 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700887 * Returns true if the shortcuts already exists on the workspace. This must be called after
888 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700890 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
891 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700892 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700893 if (intent.getComponent() != null) {
894 // If component is not null, an intent with null package will produce
895 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700896 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700897 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700898 intentWithPkg = intent.toUri(0);
899 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
902 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700903 }
904 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700905 intentWithPkg = intent.toUri(0);
906 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700907 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700908
909 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700910 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700911 if (item instanceof ShortcutInfo) {
912 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700913 Intent targetIntent = info.promisedIntent == null
914 ? info.intent : info.promisedIntent;
915 if (targetIntent != null && info.user.equals(user)) {
916 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700917 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
918 return true;
919 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700920 }
921 }
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700924 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700925 }
926
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 /**
928 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
929 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700930 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 final ContentResolver cr = context.getContentResolver();
932 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
933 "_id=? and (itemType=? or itemType=?)",
934 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700935 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700936
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 try {
938 if (c.moveToFirst()) {
939 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
940 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
941 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
942 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
943 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
944 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700945 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 FolderInfo folderInfo = null;
948 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700949 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
950 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700952 }
953
Sunny Goyala508e4f2015-05-21 09:33:57 -0700954 // Do not trim the folder label, as is was set by the user.
955 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 folderInfo.id = id;
957 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700958 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700959 folderInfo.cellX = c.getInt(cellXIndex);
960 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700961 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962
963 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 } finally {
966 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700967 }
968
969 return null;
970 }
971
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 /**
973 * Add an item to the database in a specified container. Sets the container, screen, cellX and
974 * cellY fields of the item. Also assigns an ID to the item.
975 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700976 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700977 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 item.cellX = cellX;
980 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700981 // We store hotseat items in canonical form which is this orientation invariant position
982 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700985 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700986 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700987 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989
990 final ContentValues values = new ContentValues();
991 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100992 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993
Michael Jurka414300a2013-08-27 15:42:35 +0200994 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700995 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700996
Jason Monk8e19cf22014-03-20 15:06:57 -0400997 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700998 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001000 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001003 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001004 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 sBgItemsIdMap.put(item.id, item);
1006 switch (item.itemType) {
1007 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1008 sBgFolders.put(item.id, (FolderInfo) item);
1009 // Fall through
1010 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1011 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1012 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1013 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1014 sBgWorkspaceItems.add(item);
1015 } else {
1016 if (!sBgFolders.containsKey(item.container)) {
1017 // Adding an item to a folder that doesn't exist.
1018 String msg = "adding item: " + item + " to a folder that " +
1019 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001020 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001022 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 break;
1024 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1025 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1026 break;
1027 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001028 }
1029 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001030 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001031 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001032 }
1033
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001035 * Creates a new unique child id, for a given cell span across all layouts.
1036 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001037 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001038 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001039 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001040 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001041 }
1042
Sunny Goyal34942622014-08-29 17:20:55 -07001043 private static ArrayList<ItemInfo> getItemsByPackageName(
1044 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001045 ItemInfoFilter filter = new ItemInfoFilter() {
1046 @Override
1047 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1048 return cn.getPackageName().equals(pn) && info.user.equals(user);
1049 }
1050 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001051 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001052 }
1053
1054 /**
1055 * Removes all the items from the database corresponding to the specified package.
1056 */
1057 static void deletePackageFromDatabase(Context context, final String pn,
1058 final UserHandleCompat user) {
1059 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001060 }
1061
1062 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001063 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001065 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001066 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1067 items.add(item);
1068 deleteItemsFromDatabase(context, items);
1069 }
1070
1071 /**
1072 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001073 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001074 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001076 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001077 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001078 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001079 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001080 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001081
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001082 // Lock on mBgLock *after* the db operation
1083 synchronized (sBgLock) {
1084 switch (item.itemType) {
1085 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1086 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001087 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001088 if (info.container == item.id) {
1089 // We are deleting a folder which still contains items that
1090 // think they are contained by that folder.
1091 String msg = "deleting a folder (" + item + ") which still " +
1092 "contains items (" + info + ")";
1093 Log.e(TAG, msg);
1094 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001095 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001096 sBgWorkspaceItems.remove(item);
1097 break;
1098 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1099 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1100 sBgWorkspaceItems.remove(item);
1101 break;
1102 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1103 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1104 break;
1105 }
1106 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001107 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001108 }
1109 }
Michael Jurka83df1882011-08-31 20:59:26 -07001110 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001111 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112 }
1113
1114 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 * Update the order of the workspace screens in the database. The array list contains
1116 * a list of screen ids in the order that they should appear.
1117 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001118 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001119 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001120 final ContentResolver cr = context.getContentResolver();
1121 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1122
1123 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001124 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001125 while (iter.hasNext()) {
1126 long id = iter.next();
1127 if (id < 0) {
1128 iter.remove();
1129 }
1130 }
1131
1132 Runnable r = new Runnable() {
1133 @Override
1134 public void run() {
Yura085c8532014-02-11 15:15:29 +00001135 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001136 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001137 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001138 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001139 for (int i = 0; i < count; i++) {
1140 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001141 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001142 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1143 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001144 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 }
Yura085c8532014-02-11 15:15:29 +00001146
1147 try {
1148 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1149 } catch (Exception ex) {
1150 throw new RuntimeException(ex);
1151 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001152
Winson Chungba9c37f2013-08-30 14:11:37 -07001153 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001154 sBgWorkspaceScreens.clear();
1155 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001156 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001157 }
1158 };
1159 runOnWorkerThread(r);
1160 }
1161
1162 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001163 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 */
Winsonc0b52fe2015-09-09 16:38:15 -07001165 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 final ContentResolver cr = context.getContentResolver();
1167
Michael Jurkac9d95c52011-08-29 14:03:34 -07001168 Runnable r = new Runnable() {
1169 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001170 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001171 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001172 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001173 sBgItemsIdMap.remove(info.id);
1174 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001175 sBgWorkspaceItems.remove(info);
1176 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001177
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001178 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001179 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001180 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001181 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001182 for (ItemInfo childInfo : info.contents) {
1183 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001185 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001186 }
1187 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 }
1190
1191 /**
1192 * Set this as the current Launcher activity object for the loader.
1193 */
1194 public void initialize(Callbacks callbacks) {
1195 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001196 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1197 // workspace to prevent leaking Launcher activities on orientation change.
1198 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 mCallbacks = new WeakReference<Callbacks>(callbacks);
1200 }
1201 }
1202
Kenny Guyed131872014-04-30 03:02:21 +01001203 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001204 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001205 int op = PackageUpdatedTask.OP_UPDATE;
1206 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1207 user));
1208 }
1209
1210 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001211 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001212 int op = PackageUpdatedTask.OP_REMOVE;
1213 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1214 user));
1215 }
1216
1217 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001218 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001219 int op = PackageUpdatedTask.OP_ADD;
1220 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1221 user));
1222 }
1223
1224 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001225 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001226 boolean replacing) {
1227 if (!replacing) {
1228 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1229 user));
1230 if (mAppsCanBeOnRemoveableStorage) {
1231 // Only rebind if we support removable storage. It catches the
1232 // case where
1233 // apps on the external sd card need to be reloaded
1234 startLoaderFromBackground();
1235 }
1236 } else {
1237 // If we are replacing then just update the packages in the list
1238 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1239 packageNames, user));
1240 }
1241 }
1242
1243 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001244 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001245 boolean replacing) {
1246 if (!replacing) {
1247 enqueuePackageUpdated(new PackageUpdatedTask(
1248 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1249 user));
1250 }
Kenny Guyed131872014-04-30 03:02:21 +01001251 }
1252
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001253 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1255 * ACTION_PACKAGE_CHANGED.
1256 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001257 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001258 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001259 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001260
Joe Onorato36115782010-06-17 13:28:48 -04001261 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001262 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001263 // If we have changed locale we need to clear out the labels in all apps/workspace.
1264 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001265 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001266 Callbacks callbacks = getCallback();
1267 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001268 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001269 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001270 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1271 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001272 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001273 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001274 }
1275 }
1276
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001277 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001278 resetLoadedState(true, true);
1279
Reena Lee93f824a2011-09-23 17:20:28 -07001280 // Do this here because if the launcher activity is running it will be restarted.
1281 // If it's not running startLoaderFromBackground will merely tell it that it needs
1282 // to reload.
1283 startLoaderFromBackground();
1284 }
1285
Winson Chungf0c6ae02012-03-21 16:10:31 -07001286 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1287 synchronized (mLock) {
1288 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1289 // mWorkspaceLoaded to true later
1290 stopLoaderLocked();
1291 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1292 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1293 }
1294 }
1295
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001296 /**
1297 * When the launcher is in the background, it's possible for it to miss paired
1298 * configuration changes. So whenever we trigger the loader from the background
1299 * tell the launcher that it needs to re-run the loader when it comes back instead
1300 * of doing it now.
1301 */
1302 public void startLoaderFromBackground() {
1303 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001304 Callbacks callbacks = getCallback();
1305 if (callbacks != null) {
1306 // Only actually run the loader if they're not paused.
1307 if (!callbacks.setLoadOnResume()) {
1308 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001309 }
1310 }
1311 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001312 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001313 }
Joe Onorato36115782010-06-17 13:28:48 -04001314 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001315
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001316 /**
1317 * If there is already a loader task running, tell it to stop.
1318 */
1319 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001320 LoaderTask oldTask = mLoaderTask;
1321 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001322 oldTask.stopLocked();
1323 }
Reena Lee93f824a2011-09-23 17:20:28 -07001324 }
1325
Adam Cohen1a85c582014-09-30 09:48:49 -07001326 public boolean isCurrentCallbacks(Callbacks callbacks) {
1327 return (mCallbacks != null && mCallbacks.get() == callbacks);
1328 }
1329
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001330 public void startLoader(int synchronousBindPage) {
1331 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001332 }
1333
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001334 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001335 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1336 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001337 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001338 // Don't bother to start the thread if we know it's not going to do anything
1339 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001340 final Callbacks oldCallbacks = mCallbacks.get();
1341 // Clear any pending bind-runnables from the synchronized load process.
1342 runOnMainThread(new Runnable() {
1343 public void run() {
1344 oldCallbacks.clearPendingBinds();
1345 }
1346 });
1347
Joe Onorato36115782010-06-17 13:28:48 -04001348 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001349 stopLoaderLocked();
1350 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001351 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001352 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1354 } else {
1355 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1356 sWorker.post(mLoaderTask);
1357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 }
1360 }
1361
Joe Onorato36115782010-06-17 13:28:48 -04001362 public void stopLoader() {
1363 synchronized (mLock) {
1364 if (mLoaderTask != null) {
1365 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 }
1367 }
Joe Onorato36115782010-06-17 13:28:48 -04001368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001370 /**
1371 * Loads the workspace screen ids in an ordered list.
1372 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001373 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001374 final ContentResolver contentResolver = context.getContentResolver();
1375 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001376
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001377 // Get screens ordered by rank.
1378 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1379 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1380 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001381 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001382 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001383 while (sc.moveToNext()) {
1384 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001385 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001386 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001387 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1388 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001389 }
1390 }
1391 } finally {
1392 sc.close();
1393 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001394 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001395 }
1396
Michael Jurkac57b7a82011-08-09 22:02:20 -07001397 public boolean isAllAppsLoaded() {
1398 return mAllAppsLoaded;
1399 }
1400
Joe Onorato36115782010-06-17 13:28:48 -04001401 /**
1402 * Runnable for the thread that loads the contents of the launcher:
1403 * - workspace icons
1404 * - widgets
1405 * - all apps icons
1406 */
1407 private class LoaderTask implements Runnable {
1408 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001409 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001410 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001411 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001412 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001413
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001414 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001415 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001416 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 }
1418
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001419 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001420 mIsLoadingAndBindingWorkspace = true;
1421
Joe Onorato36115782010-06-17 13:28:48 -04001422 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001423 if (DEBUG_LOADERS) {
1424 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001426
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001427 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001428 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001429 synchronized (LoaderTask.this) {
1430 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001431 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001432 }
1433 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 }
1435 }
1436
Joe Onorato36115782010-06-17 13:28:48 -04001437 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001438 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001439 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001440
Joe Onorato36115782010-06-17 13:28:48 -04001441 private void waitForIdle() {
1442 // Wait until the either we're stopped or the other threads are done.
1443 // This way we don't start loading all apps until the workspace has settled
1444 // down.
1445 synchronized (LoaderTask.this) {
1446 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001447
Joe Onorato36115782010-06-17 13:28:48 -04001448 mHandler.postIdle(new Runnable() {
1449 public void run() {
1450 synchronized (LoaderTask.this) {
1451 mLoadAndBindStepFinished = true;
1452 if (DEBUG_LOADERS) {
1453 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001454 }
Joe Onorato36115782010-06-17 13:28:48 -04001455 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001456 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001457 }
Joe Onorato36115782010-06-17 13:28:48 -04001458 });
1459
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001460 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001461 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001462 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1463 // wait no longer than 1sec at a time
1464 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001465 } catch (InterruptedException ex) {
1466 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001467 }
1468 }
Joe Onorato36115782010-06-17 13:28:48 -04001469 if (DEBUG_LOADERS) {
1470 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001471 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001472 + "ms for previous step to finish binding");
1473 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001476
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001477 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001478 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001479 // Ensure that we have a valid page index to load synchronously
1480 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1481 "valid page index");
1482 }
1483 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1484 // Ensure that we don't try and bind a specified page when the pages have not been
1485 // loaded already (we should load everything asynchronously in that case)
1486 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1487 }
1488 synchronized (mLock) {
1489 if (mIsLoaderTaskRunning) {
1490 // Ensure that we are never running the background loading at this point since
1491 // we also touch the background collections
1492 throw new RuntimeException("Error! Background loading is already running");
1493 }
1494 }
1495
1496 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1497 // data structures, we can't allow any other thread to touch that data, but because
1498 // this call is synchronous, we can get away with not locking).
1499
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001500 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001501 // operations from the previous activity. We need to ensure that all queued operations
1502 // are executed before any synchronous binding work is done.
1503 mHandler.flush();
1504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505 // Divide the set of loaded items into those that we are binding synchronously, and
1506 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001507 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1509 // arise from that.
1510 onlyBindAllApps();
1511 }
1512
Joe Onorato36115782010-06-17 13:28:48 -04001513 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001514 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001515 if (mStopped) {
1516 return;
1517 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001518 mIsLoaderTaskRunning = true;
1519 }
Joe Onorato36115782010-06-17 13:28:48 -04001520 // Optimize for end-user experience: if the Launcher is up and // running with the
1521 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1522 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001523 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001524 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001525 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001526
Joe Onorato36115782010-06-17 13:28:48 -04001527 if (mStopped) {
1528 break keep_running;
1529 }
1530
Joe Onorato36115782010-06-17 13:28:48 -04001531 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001532
1533 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001534 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1535 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001536 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537
Joe Onorato36115782010-06-17 13:28:48 -04001538 // Clear out this reference, otherwise we end up holding it until all of the
1539 // callback runnables are done.
1540 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541
Joe Onorato36115782010-06-17 13:28:48 -04001542 synchronized (mLock) {
1543 // If we are still the last one to be scheduled, remove ourselves.
1544 if (mLoaderTask == this) {
1545 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001547 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001548 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001549 }
Joe Onorato36115782010-06-17 13:28:48 -04001550 }
1551
1552 public void stopLocked() {
1553 synchronized (LoaderTask.this) {
1554 mStopped = true;
1555 this.notify();
1556 }
1557 }
1558
1559 /**
1560 * Gets the callbacks object. If we've been stopped, or if the launcher object
1561 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1562 * object that was around when the deferred message was scheduled, and if there's
1563 * a new Callbacks object around then also return null. This will save us from
1564 * calling onto it with data that will be ignored.
1565 */
1566 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1567 synchronized (mLock) {
1568 if (mStopped) {
1569 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001570 }
Joe Onorato36115782010-06-17 13:28:48 -04001571
1572 if (mCallbacks == null) {
1573 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001574 }
Joe Onorato36115782010-06-17 13:28:48 -04001575
1576 final Callbacks callbacks = mCallbacks.get();
1577 if (callbacks != oldCallbacks) {
1578 return null;
1579 }
1580 if (callbacks == null) {
1581 Log.w(TAG, "no mCallbacks");
1582 return null;
1583 }
1584
1585 return callbacks;
1586 }
1587 }
1588
1589 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001590 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1591 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001592 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001593 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001594 final int countX = profile.numColumns;
1595 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001596
Adam Cohendcd297f2013-06-18 13:13:40 -07001597 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001598 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001599 // Return early if we detect that an item is under the hotseat button
1600 if (mCallbacks == null ||
1601 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001602 Log.e(TAG, "Error loading shortcut into hotseat " + item
1603 + " into position (" + item.screenId + ":" + item.cellX + ","
1604 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001605 return false;
1606 }
1607
Dan Sandler295ae182013-12-10 16:05:47 -05001608 final ItemInfo[][] hotseatItems =
1609 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1610
Adam Cohen2e6da152015-05-06 11:42:25 -07001611 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001612 Log.e(TAG, "Error loading shortcut " + item
1613 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001614 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001615 + ")");
1616 return false;
1617 }
1618
Dan Sandler295ae182013-12-10 16:05:47 -05001619 if (hotseatItems != null) {
1620 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001621 Log.e(TAG, "Error loading shortcut into hotseat " + item
1622 + " into position (" + item.screenId + ":" + item.cellX + ","
1623 + item.cellY + ") occupied by "
1624 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1625 [(int) item.screenId][0]);
1626 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001627 } else {
1628 hotseatItems[(int) item.screenId][0] = item;
1629 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001630 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001631 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001632 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001633 items[(int) item.screenId][0] = item;
1634 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001635 return true;
1636 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001637 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1638 if (!workspaceScreens.contains((Long) item.screenId)) {
1639 // The item has an invalid screen id.
1640 return false;
1641 }
1642 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001643 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001644 return true;
1645 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001646
Adam Cohendcd297f2013-06-18 13:13:40 -07001647 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001648 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001649 occupied.put(item.screenId, items);
1650 }
1651
Dan Sandler295ae182013-12-10 16:05:47 -05001652 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001653 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1654 item.cellX < 0 || item.cellY < 0 ||
1655 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1656 Log.e(TAG, "Error loading shortcut " + item
1657 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1658 + item.cellX + "," + item.cellY
1659 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1660 return false;
1661 }
1662
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001663 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001664 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1665 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001666 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001667 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001668 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001669 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001670 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001671 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001672 return false;
1673 }
1674 }
1675 }
1676 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1677 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001678 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001679 }
1680 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001681
Joe Onorato36115782010-06-17 13:28:48 -04001682 return true;
1683 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001684
Winson Chungba9c37f2013-08-30 14:11:37 -07001685 /** Clears all the sBg data structures */
1686 private void clearSBgDataStructures() {
1687 synchronized (sBgLock) {
1688 sBgWorkspaceItems.clear();
1689 sBgAppWidgets.clear();
1690 sBgFolders.clear();
1691 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001692 sBgWorkspaceScreens.clear();
1693 }
1694 }
1695
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001696 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001697 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001698
Joe Onorato36115782010-06-17 13:28:48 -04001699 final Context context = mContext;
1700 final ContentResolver contentResolver = context.getContentResolver();
1701 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001702 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001703 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001704 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001705
Winson Chung892c74d2013-08-22 16:15:50 -07001706 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001707 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001708 int countX = profile.numColumns;
1709 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001710
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001711 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001712 long migrationStartTime = System.currentTimeMillis();
1713 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001714 try {
1715 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1716 // Clear the flags before starting the task, so that we do not run the task
1717 // again, in case there was an uncaught error.
1718 MigrateFromRestoreTask.clearFlags(mContext);
1719 task.execute();
1720 } catch (Exception e) {
1721 Log.e(TAG, "Error during grid migration", e);
1722
1723 // Clear workspace.
1724 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1725 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001726 Log.v(TAG, "Workspace migration completed in "
1727 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001728 }
1729
Dan Sandlerd5024042014-01-09 15:01:33 -05001730 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1731 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1732 LauncherAppState.getLauncherProvider().deleteDatabase();
1733 }
1734
1735 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1736 // append the user's Launcher2 shortcuts
1737 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1738 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1739 } else {
1740 // Make sure the default workspace is loaded
1741 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001742 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001743 }
Adam Cohene25af792013-06-06 23:08:25 -07001744
Winson Chung2abf94d2012-07-18 18:16:38 -07001745 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001746 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001747 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001748 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001749 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001750
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001752 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001753 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001754 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001755 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001756
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001757 // +1 for the hotseat (it can be larger than the workspace)
1758 // Load workspace in reverse order to ensure that latest items are loaded first (and
1759 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001760 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 try {
1763 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1764 final int intentIndex = c.getColumnIndexOrThrow
1765 (LauncherSettings.Favorites.INTENT);
1766 final int titleIndex = c.getColumnIndexOrThrow
1767 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001768 final int containerIndex = c.getColumnIndexOrThrow(
1769 LauncherSettings.Favorites.CONTAINER);
1770 final int itemTypeIndex = c.getColumnIndexOrThrow(
1771 LauncherSettings.Favorites.ITEM_TYPE);
1772 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1773 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001774 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1775 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 final int screenIndex = c.getColumnIndexOrThrow(
1777 LauncherSettings.Favorites.SCREEN);
1778 final int cellXIndex = c.getColumnIndexOrThrow
1779 (LauncherSettings.Favorites.CELLX);
1780 final int cellYIndex = c.getColumnIndexOrThrow
1781 (LauncherSettings.Favorites.CELLY);
1782 final int spanXIndex = c.getColumnIndexOrThrow
1783 (LauncherSettings.Favorites.SPANX);
1784 final int spanYIndex = c.getColumnIndexOrThrow(
1785 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001786 final int rankIndex = c.getColumnIndexOrThrow(
1787 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001788 final int restoredIndex = c.getColumnIndexOrThrow(
1789 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001790 final int profileIdIndex = c.getColumnIndexOrThrow(
1791 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001792 final int optionsIndex = c.getColumnIndexOrThrow(
1793 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001794 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795
Sunny Goyal7f834d22015-04-21 10:10:23 -07001796 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1797 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1798 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1799 }
1800
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 ShortcutInfo info;
1802 String intentDescription;
1803 LauncherAppWidgetInfo appWidgetInfo;
1804 int container;
1805 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001806 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001808 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001810 while (!mStopped && c.moveToNext()) {
1811 try {
1812 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001813 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001814 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001815 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 switch (itemType) {
1818 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1819 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001820 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001821 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001822 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001823 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001824 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001825 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001826 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001827 if (user == null) {
1828 // User has been deleted remove the item.
1829 itemsToRemove.add(id);
1830 continue;
1831 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 try {
1833 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001834 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001835 if (cn != null && cn.getPackageName() != null) {
1836 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1837 cn.getPackageName(), user);
1838 boolean validComponent = validPkg &&
1839 launcherApps.isActivityEnabledForProfile(cn, user);
1840
1841 if (validComponent) {
1842 if (restored) {
1843 // no special handling necessary for this item
1844 restoredRows.add(id);
1845 restored = false;
1846 }
1847 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001848 intent = null;
1849 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1850 // We allow auto install apps to have their intent
1851 // updated after an install.
1852 intent = manager.getLaunchIntentForPackage(
1853 cn.getPackageName());
1854 if (intent != null) {
1855 ContentValues values = new ContentValues();
1856 values.put(LauncherSettings.Favorites.INTENT,
1857 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001858 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001859 }
1860 }
1861
1862 if (intent == null) {
1863 // The app is installed but the component is no
1864 // longer available.
1865 Launcher.addDumpLog(TAG,
1866 "Invalid component removed: " + cn, true);
1867 itemsToRemove.add(id);
1868 continue;
1869 } else {
1870 // no special handling necessary for this item
1871 restoredRows.add(id);
1872 restored = false;
1873 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001874 } else if (restored) {
1875 // Package is not yet available but might be
1876 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001877 Launcher.addDumpLog(TAG,
1878 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001879
1880 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1881 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001882 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001883 // App restore has started. Update the flag
1884 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1885 ContentValues values = new ContentValues();
1886 values.put(LauncherSettings.Favorites.RESTORED,
1887 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001888 updateItem(id, values);
1889 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1890 // This is a common app. Try to replace this.
1891 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1892 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1893 if (parser.findDefaultApp()) {
1894 // Default app found. Replace it.
1895 intent = parser.parsedIntent;
1896 cn = intent.getComponent();
1897 ContentValues values = parser.parsedValues;
1898 values.put(LauncherSettings.Favorites.RESTORED, 0);
1899 updateItem(id, values);
1900 restored = false;
1901 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001902
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001903 } else if (REMOVE_UNRESTORED_ICONS) {
1904 Launcher.addDumpLog(TAG,
1905 "Unrestored package removed: " + cn, true);
1906 itemsToRemove.add(id);
1907 continue;
1908 }
Sunny Goyal94485362014-09-18 16:13:58 -07001909 } else if (REMOVE_UNRESTORED_ICONS) {
1910 Launcher.addDumpLog(TAG,
1911 "Unrestored package removed: " + cn, true);
1912 itemsToRemove.add(id);
1913 continue;
1914 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001915 } else if (launcherApps.isAppEnabled(
1916 manager, cn.getPackageName(),
1917 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1918 // Package is present but not available.
1919 allowMissingTarget = true;
1920 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1921 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001922 // SdCard is not ready yet. Package might get available,
1923 // once it is ready.
1924 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1925 + " (check again later)", true);
1926 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
1938 Launcher.addDumpLog(TAG,
1939 "Invalid package removed: " + cn, true);
1940 itemsToRemove.add(id);
1941 continue;
Winson Chungee055712013-07-30 14:46:24 -07001942 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001943 } else if (cn == null) {
1944 // For shortcuts with no component, keep them as they are
1945 restoredRows.add(id);
1946 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001947 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001949 Launcher.addDumpLog(TAG,
1950 "Invalid uri: " + intentDescription, true);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001951 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001952 continue;
1953 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954
Sunny Goyal34b65272015-03-11 16:56:52 -07001955 boolean useLowResIcon = container >= 0 &&
1956 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1957
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001958 if (itemReplaced) {
1959 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001960 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001961 cursorIconInfo.iconIndex, titleIndex,
1962 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001963 } else {
1964 // Don't replace items for other profiles.
1965 itemsToRemove.add(id);
1966 continue;
1967 }
1968 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001969 if (user.equals(UserHandleCompat.myUserHandle())) {
1970 Launcher.addDumpLog(TAG,
1971 "constructing info for partially restored package",
1972 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001973 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001974 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001975 intent = getRestoredItemIntent(c, context, intent);
1976 } else {
1977 // Don't restore items for other profiles.
1978 itemsToRemove.add(id);
1979 continue;
1980 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001981 } else if (itemType ==
1982 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001983 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001984 cursorIconInfo.iconIndex, titleIndex,
1985 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001987 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001988
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 // App shortcuts that used to be automatically added to Launcher
1990 // didn't always have the correct intent flags set, so do that
1991 // here
1992 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001993 intent.getCategories() != null &&
1994 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001995 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 intent.addFlags(
1997 Intent.FLAG_ACTIVITY_NEW_TASK |
1998 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1999 }
Michael Jurka96879562012-03-22 05:54:33 -07002000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002003 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002006 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 info.cellX = c.getInt(cellXIndex);
2008 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002009 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002010 info.spanX = 1;
2011 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002012 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002013 if (info.promisedIntent != null) {
2014 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2015 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002016 info.isDisabled = disabledState;
2017 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2018 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2019 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002020
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002022 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002023 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 break;
2025 }
2026
Sunny Goyal756adbc2015-04-16 15:20:51 -07002027 if (restored) {
2028 ComponentName cn = info.getTargetComponent();
2029 if (cn != null) {
2030 Integer progress = installingPkgs.get(cn.getPackageName());
2031 if (progress != null) {
2032 info.setInstallProgress(progress);
2033 } else {
2034 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2035 }
2036 }
2037 }
2038
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 switch (container) {
2040 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2041 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2042 sBgWorkspaceItems.add(info);
2043 break;
2044 default:
2045 // Item is in a user folder
2046 FolderInfo folderInfo =
2047 findOrMakeFolder(sBgFolders, container);
2048 folderInfo.add(info);
2049 break;
2050 }
2051 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002052 } else {
2053 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 }
2055 break;
2056
2057 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2058 id = c.getLong(idIndex);
2059 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2060
Sunny Goyala508e4f2015-05-21 09:33:57 -07002061 // Do not trim the folder label, as is was set by the user.
2062 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002065 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 folderInfo.cellX = c.getInt(cellXIndex);
2067 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002068 folderInfo.spanX = 1;
2069 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002070 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071
Daniel Sandler8802e962010-05-26 16:28:16 -04002072 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002073 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002074 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002075 break;
2076 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002077
Joe Onorato9c1289c2009-08-17 11:03:03 -04002078 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2080 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2081 sBgWorkspaceItems.add(folderInfo);
2082 break;
Joe Onorato36115782010-06-17 13:28:48 -04002083 }
Joe Onorato17a89222011-02-08 17:26:11 -08002084
Chris Wrenf4d08112014-01-16 18:13:56 -05002085 if (restored) {
2086 // no special handling required for restored folders
2087 restoredRows.add(id);
2088 }
2089
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2091 sBgFolders.put(folderInfo.id, folderInfo);
2092 break;
2093
2094 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002095 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002097 boolean customWidget = itemType ==
2098 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2099
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002101 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002102 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002103 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002104 user = allUsers.get(serialNumber);
2105 if (user == null) {
2106 itemsToRemove.add(id);
2107 continue;
2108 }
2109
Sunny Goyalff572272014-07-23 13:58:07 -07002110 final ComponentName component =
2111 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002112
Sunny Goyal651077b2014-06-30 14:15:31 -07002113 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002114 final boolean isIdValid = (restoreStatus &
2115 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002116 final boolean wasProviderReady = (restoreStatus &
2117 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002118
Adam Cohen59400422014-03-05 18:07:04 -08002119 final LauncherAppWidgetProviderInfo provider =
2120 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002121 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002122 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002123
2124 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002125 if (!isSafeMode && !customWidget &&
2126 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002127 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002128 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002129
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002131 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 itemsToRemove.add(id);
2133 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002134 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002135 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2136 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002137
Sunny Goyal53f96722015-07-13 19:54:53 -07002138 // The provider is available. So the widget is either
2139 // available or not available. We do not need to track
2140 // any future restore updates.
2141 int status = restoreStatus &
2142 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002143 if (!wasProviderReady) {
2144 // If provider was not previously ready, update the
2145 // status and UI flag.
2146
2147 // Id would be valid only if the widget restore broadcast was received.
2148 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002149 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002150 } else {
2151 status &= ~LauncherAppWidgetInfo
2152 .FLAG_PROVIDER_NOT_READY;
2153 }
2154 }
2155 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002156 } else {
2157 Log.v(TAG, "Widget restore pending id=" + id
2158 + " appWidgetId=" + appWidgetId
2159 + " status =" + restoreStatus);
2160 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002161 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002162 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002163 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002164
2165 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2166 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002167 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002168 // App restore has started. Update the flag
2169 appWidgetInfo.restoreStatus |=
2170 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002171 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002172 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002173 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002174 itemsToRemove.add(id);
2175 continue;
2176 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002177
2178 appWidgetInfo.installProgress =
2179 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002180 }
Sunny Goyalff572272014-07-23 13:58:07 -07002181
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002183 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 appWidgetInfo.cellX = c.getInt(cellXIndex);
2185 appWidgetInfo.cellY = c.getInt(cellYIndex);
2186 appWidgetInfo.spanX = c.getInt(spanXIndex);
2187 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002188 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002189
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2191 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2192 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002193 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2194 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 continue;
2196 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002198 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002200 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002201 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 break;
2203 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002204
Adam Cohen59400422014-03-05 18:07:04 -08002205 if (!customWidget) {
2206 String providerName =
2207 appWidgetInfo.providerName.flattenToString();
2208 if (!providerName.equals(savedProvider) ||
2209 (appWidgetInfo.restoreStatus != restoreStatus)) {
2210 ContentValues values = new ContentValues();
2211 values.put(
2212 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2213 providerName);
2214 values.put(LauncherSettings.Favorites.RESTORED,
2215 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002216 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002217 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002218 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2220 sBgAppWidgets.add(appWidgetInfo);
2221 }
Joe Onorato36115782010-06-17 13:28:48 -04002222 break;
2223 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002225 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002226 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 }
2228 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002229 if (c != null) {
2230 c.close();
2231 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 }
2233
Winson Chungba9c37f2013-08-30 14:11:37 -07002234 // Break early if we've stopped loading
2235 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002236 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002237 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002238 }
2239
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002242 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2243 Utilities.createDbSelectionQuery(
2244 LauncherSettings.Favorites._ID, itemsToRemove), null);
2245 if (DEBUG_LOADERS) {
2246 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2247 LauncherSettings.Favorites._ID, itemsToRemove));
2248 }
2249
2250 // Remove any empty folder
2251 for (long folderId : LauncherAppState.getLauncherProvider()
2252 .deleteEmptyFolders()) {
2253 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2254 sBgFolders.remove(folderId);
2255 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002256 }
2257 }
2258
Sunny Goyal317698b2015-07-29 11:45:41 -07002259 // Sort all the folder items and make sure the first 3 items are high resolution.
2260 for (FolderInfo folder : sBgFolders) {
2261 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2262 int pos = 0;
2263 for (ShortcutInfo info : folder.contents) {
2264 if (info.usingLowResIcon) {
2265 info.updateIcon(mIconCache, false);
2266 }
2267 pos ++;
2268 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2269 break;
2270 }
2271 }
2272 }
2273
Chris Wrenf4d08112014-01-16 18:13:56 -05002274 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002275 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002276 ContentValues values = new ContentValues();
2277 values.put(LauncherSettings.Favorites.RESTORED, 0);
2278 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2279 Utilities.createDbSelectionQuery(
2280 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002281 }
2282
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002283 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2284 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002285 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002286 null, sWorker);
2287 }
2288
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002289 // Remove any empty screens
2290 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002291 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002292 long screenId = item.screenId;
2293 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2294 unusedScreens.contains(screenId)) {
2295 unusedScreens.remove(screenId);
2296 }
2297 }
2298
2299 // If there are any empty screens remove them, and update.
2300 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002301 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002303 }
2304
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305 if (DEBUG_LOADERS) {
2306 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2307 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002308 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002309 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002311
Sunny Goyale2df0622015-04-24 11:27:00 -07002312 for (int i = 0; i < nScreens; i++) {
2313 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002314 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 line += " | ";
2316 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002317 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002318 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002319 if (x < screen.length && y < screen[x].length) {
2320 line += (screen[x][y] != null) ? "#" : ".";
2321 } else {
2322 line += "!";
2323 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 }
Joe Onorato36115782010-06-17 13:28:48 -04002325 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002328 }
Joe Onorato36115782010-06-17 13:28:48 -04002329 }
Adam Cohene25af792013-06-06 23:08:25 -07002330 }
2331
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002332 /**
2333 * Partially updates the item without any notification. Must be called on the worker thread.
2334 */
2335 private void updateItem(long itemId, ContentValues update) {
2336 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002337 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002338 update,
2339 BaseColumns._ID + "= ?",
2340 new String[]{Long.toString(itemId)});
2341 }
2342
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 /** Filters the set of items who are directly or indirectly (via another container) on the
2344 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002345 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002346 ArrayList<ItemInfo> allWorkspaceItems,
2347 ArrayList<ItemInfo> currentScreenItems,
2348 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002349 // Purge any null ItemInfos
2350 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2351 while (iter.hasNext()) {
2352 ItemInfo i = iter.next();
2353 if (i == null) {
2354 iter.remove();
2355 }
2356 }
2357
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 // Order the set of items by their containers first, this allows use to walk through the
2359 // list sequentially, build up a list of containers that are in the specified screen,
2360 // as well as all items in those containers.
2361 Set<Long> itemsOnScreen = new HashSet<Long>();
2362 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2363 @Override
2364 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson17e2d492015-09-25 16:01:24 -07002365 return Long.compare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 }
2367 });
2368 for (ItemInfo info : allWorkspaceItems) {
2369 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002370 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002371 currentScreenItems.add(info);
2372 itemsOnScreen.add(info.id);
2373 } else {
2374 otherScreenItems.add(info);
2375 }
2376 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2377 currentScreenItems.add(info);
2378 itemsOnScreen.add(info.id);
2379 } else {
2380 if (itemsOnScreen.contains(info.container)) {
2381 currentScreenItems.add(info);
2382 itemsOnScreen.add(info.id);
2383 } else {
2384 otherScreenItems.add(info);
2385 }
2386 }
2387 }
2388 }
2389
2390 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002391 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392 ArrayList<LauncherAppWidgetInfo> appWidgets,
2393 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2394 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002395
2396 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002397 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002398 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002399 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 currentScreenWidgets.add(widget);
2401 } else {
2402 otherScreenWidgets.add(widget);
2403 }
2404 }
2405 }
2406
2407 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002408 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002409 LongArrayMap<ItemInfo> itemsIdMap,
2410 LongArrayMap<FolderInfo> folders,
2411 LongArrayMap<FolderInfo> currentScreenFolders,
2412 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413
Sunny Goyale2df0622015-04-24 11:27:00 -07002414 int total = folders.size();
2415 for (int i = 0; i < total; i++) {
2416 long id = folders.keyAt(i);
2417 FolderInfo folder = folders.valueAt(i);
2418
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002419 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002420 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002422 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 currentScreenFolders.put(id, folder);
2424 } else {
2425 otherScreenFolders.put(id, folder);
2426 }
2427 }
2428 }
2429
2430 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2431 * right) */
2432 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002433 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002434 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002435 final int screenCols = profile.numColumns;
2436 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002438 @Override
2439 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002440 if (lhs.container == rhs.container) {
2441 // Within containers, order by their spatial position in that container
2442 switch ((int) lhs.container) {
2443 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2444 long lr = (lhs.screenId * screenCellCount +
2445 lhs.cellY * screenCols + lhs.cellX);
2446 long rr = (rhs.screenId * screenCellCount +
2447 rhs.cellY * screenCols + rhs.cellX);
Winsone993c4b2015-09-29 17:39:37 -07002448 return Long.compare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002449 }
2450 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2451 // We currently use the screen id as the rank
Winsone993c4b2015-09-29 17:39:37 -07002452 return Long.compare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002453 }
2454 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002455 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002456 throw new RuntimeException("Unexpected container type when " +
2457 "sorting workspace items.");
2458 }
2459 return 0;
2460 }
2461 } else {
2462 // Between containers, order by hotseat, desktop
Winsone993c4b2015-09-29 17:39:37 -07002463 return Long.compare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002464 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002465 }
2466 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002468
Adam Cohendcd297f2013-06-18 13:13:40 -07002469 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2470 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002471 final Runnable r = new Runnable() {
2472 @Override
2473 public void run() {
2474 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2475 if (callbacks != null) {
2476 callbacks.bindScreens(orderedScreens);
2477 }
2478 }
2479 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002480 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002481 }
2482
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2484 final ArrayList<ItemInfo> workspaceItems,
2485 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002486 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002487 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488
2489 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002490 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002492 final int start = i;
2493 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494 final Runnable r = new Runnable() {
2495 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002496 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002497 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002498 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002499 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2500 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002501 }
2502 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002503 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002504 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002505 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506
2507 // Bind the folders
2508 if (!folders.isEmpty()) {
2509 final Runnable r = new Runnable() {
2510 public void run() {
2511 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2512 if (callbacks != null) {
2513 callbacks.bindFolders(folders);
2514 }
2515 }
2516 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002517 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518 }
2519
2520 // Bind the widgets, one at a time
2521 N = appWidgets.size();
2522 for (int i = 0; i < N; i++) {
2523 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2524 final Runnable r = new Runnable() {
2525 public void run() {
2526 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2527 if (callbacks != null) {
2528 callbacks.bindAppWidget(widget);
2529 }
2530 }
2531 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002532 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 }
2534 }
2535
2536 /**
2537 * Binds all loaded data to actual views on the main thread.
2538 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002539 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 final long t = SystemClock.uptimeMillis();
2541 Runnable r;
2542
2543 // Don't use these two variables in any of the callback runnables.
2544 // Otherwise we hold a reference to them.
2545 final Callbacks oldCallbacks = mCallbacks.get();
2546 if (oldCallbacks == null) {
2547 // This launcher has exited and nobody bothered to tell us. Just bail.
2548 Log.w(TAG, "LoaderTask running with no launcher");
2549 return;
2550 }
2551
Winson Chung9b9fb962013-11-15 15:39:34 -08002552 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002553 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2554 ArrayList<LauncherAppWidgetInfo> appWidgets =
2555 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002556 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002557
2558 final LongArrayMap<FolderInfo> folders;
2559 final LongArrayMap<ItemInfo> itemsIdMap;
2560
Winson Chung2abf94d2012-07-18 18:16:38 -07002561 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 workspaceItems.addAll(sBgWorkspaceItems);
2563 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002564 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002565
2566 folders = sBgFolders.clone();
2567 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002568 }
2569
Derek Prothro7aff3992013-12-10 14:00:37 -05002570 final boolean isLoadingSynchronously =
2571 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002572 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002573 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002574 if (currScreen >= orderedScreenIds.size()) {
2575 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002576 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002577 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002578 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002579 final long currentScreenId = currentScreen < 0
2580 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002581
2582 // Load all the items that are on the current page first (and in the process, unbind
2583 // all the existing workspace items before we call startBinding() below.
2584 unbindWorkspaceItemsOnMainThread();
2585
2586 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002587 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2588 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2589 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2590 new ArrayList<LauncherAppWidgetInfo>();
2591 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2592 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002593 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2594 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002595
Winson Chung9b9fb962013-11-15 15:39:34 -08002596 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002597 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002598 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002599 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002600 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002601 otherFolders);
2602 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2603 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2604
2605 // Tell the workspace that we're about to start binding items
2606 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002607 public void run() {
2608 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2609 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002610 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002611 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002612 }
2613 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002614 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002615 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002616
Adam Cohendcd297f2013-06-18 13:13:40 -07002617 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2618
Sunny Goyal527c7d32015-08-28 15:19:36 -07002619 Executor mainExecutor = new DeferredMainThreadExecutor();
2620 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002621 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002622 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623
Sunny Goyal527c7d32015-08-28 15:19:36 -07002624 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2625 // remaining screens after first onDraw is called. This ensures that the first screen
2626 // is immediately visible (eg. during rotation)
2627 // In case of !isLoadingSynchronously, bind all pages one after other.
2628 final Executor deferredExecutor = isLoadingSynchronously ?
2629 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2630
2631 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2632 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633
2634 // Tell the workspace that we're done binding items
2635 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002636 public void run() {
2637 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2638 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002639 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002640 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002641
Sunny Goyal639e9062015-08-19 19:17:06 -07002642 mIsLoadingAndBindingWorkspace = false;
2643
2644 // Run all the bind complete runnables after workspace is bound.
2645 if (!mBindCompleteRunnables.isEmpty()) {
2646 synchronized (mBindCompleteRunnables) {
2647 for (final Runnable r : mBindCompleteRunnables) {
2648 runOnWorkerThread(r);
2649 }
2650 mBindCompleteRunnables.clear();
2651 }
2652 }
2653
Winson Chung98e030b2012-05-07 16:01:11 -07002654 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002655 if (DEBUG_LOADERS) {
2656 Log.d(TAG, "bound workspace in "
2657 + (SystemClock.uptimeMillis()-t) + "ms");
2658 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002659
Joe Onorato36115782010-06-17 13:28:48 -04002660 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002661 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002662 deferredExecutor.execute(r);
2663
Winson Chung4a2afa32012-07-19 14:53:05 -07002664 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002665 r = new Runnable() {
2666 public void run() {
2667 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2668 if (callbacks != null) {
2669 // We are loading synchronously, which means, some of the pages will be
2670 // bound after first draw. Inform the callbacks that page binding is
2671 // not complete, and schedule the remaining pages.
2672 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2673 callbacks.onPageBoundSynchronously(currentScreen);
2674 }
2675 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2676 }
2677 }
2678 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002679 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002680 }
Joe Onorato36115782010-06-17 13:28:48 -04002681 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002682
Joe Onorato36115782010-06-17 13:28:48 -04002683 private void loadAndBindAllApps() {
2684 if (DEBUG_LOADERS) {
2685 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2686 }
2687 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002688 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002689 synchronized (LoaderTask.this) {
2690 if (mStopped) {
2691 return;
2692 }
2693 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002694 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002695 synchronized (LoaderTask.this) {
2696 if (mStopped) {
2697 return;
2698 }
2699 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002700 }
Joe Onorato36115782010-06-17 13:28:48 -04002701 } else {
2702 onlyBindAllApps();
2703 }
2704 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002705
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002706 private void updateIconCache() {
2707 // Ignore packages which have a promise icon.
2708 HashSet<String> packagesToIgnore = new HashSet<>();
2709 synchronized (sBgLock) {
2710 for (ItemInfo info : sBgItemsIdMap) {
2711 if (info instanceof ShortcutInfo) {
2712 ShortcutInfo si = (ShortcutInfo) info;
2713 if (si.isPromise() && si.getTargetComponent() != null) {
2714 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2715 }
2716 } else if (info instanceof LauncherAppWidgetInfo) {
2717 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2718 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2719 packagesToIgnore.add(lawi.providerName.getPackageName());
2720 }
2721 }
2722 }
2723 }
2724 mIconCache.updateDbIcons(packagesToIgnore);
2725 }
2726
Joe Onorato36115782010-06-17 13:28:48 -04002727 private void onlyBindAllApps() {
2728 final Callbacks oldCallbacks = mCallbacks.get();
2729 if (oldCallbacks == null) {
2730 // This launcher has exited and nobody bothered to tell us. Just bail.
2731 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2732 return;
2733 }
2734
2735 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002736 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002737 final ArrayList<AppInfo> list
2738 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002739 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002740 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002741 public void run() {
2742 final long t = SystemClock.uptimeMillis();
2743 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2744 if (callbacks != null) {
2745 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002746 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002747 }
2748 if (DEBUG_LOADERS) {
2749 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2750 + (SystemClock.uptimeMillis()-t) + "ms");
2751 }
2752 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002753 };
2754 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002755 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002756 r.run();
2757 } else {
2758 mHandler.post(r);
2759 }
Joe Onorato36115782010-06-17 13:28:48 -04002760 }
2761
Winson Chung64359a52013-07-08 17:17:08 -07002762 private void loadAllApps() {
2763 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002764
Joe Onorato36115782010-06-17 13:28:48 -04002765 final Callbacks oldCallbacks = mCallbacks.get();
2766 if (oldCallbacks == null) {
2767 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002768 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002769 return;
2770 }
2771
Kenny Guyed131872014-04-30 03:02:21 +01002772 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2773
Winson Chung64359a52013-07-08 17:17:08 -07002774 // Clear the list of apps
2775 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002776 for (UserHandleCompat user : profiles) {
2777 // Query for the set of apps
2778 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002779 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002780 if (DEBUG_LOADERS) {
2781 Log.d(TAG, "getActivityList took "
2782 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2783 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2784 }
2785 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002786 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002787 if (apps == null || apps.isEmpty()) {
2788 return;
2789 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002790
Kenny Guyed131872014-04-30 03:02:21 +01002791 // Create the ApplicationInfos
2792 for (int i = 0; i < apps.size(); i++) {
2793 LauncherActivityInfoCompat app = apps.get(i);
2794 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002795 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002796 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002797
Sunny Goyal756a28a2015-04-23 17:07:55 -07002798 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2799 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002800 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002801
2802 @Override
2803 public void run() {
2804 heuristic.processUserApps(apps);
2805 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002806 };
2807 runOnMainThread(new Runnable() {
2808
2809 @Override
2810 public void run() {
2811 // Check isLoadingWorkspace on the UI thread, as it is updated on
2812 // the UI thread.
2813 if (mIsLoadingAndBindingWorkspace) {
2814 synchronized (mBindCompleteRunnables) {
2815 mBindCompleteRunnables.add(r);
2816 }
2817 } else {
2818 runOnWorkerThread(r);
2819 }
2820 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002821 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002822 }
Winson Chung64359a52013-07-08 17:17:08 -07002823 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002824 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002825 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2826 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002827
2828 // Post callback on main thread
2829 mHandler.post(new Runnable() {
2830 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002831
Winson Chung64359a52013-07-08 17:17:08 -07002832 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002833 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002834 if (callbacks != null) {
2835 callbacks.bindAllApplications(added);
2836 if (DEBUG_LOADERS) {
2837 Log.d(TAG, "bound " + added.size() + " apps in "
2838 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2839 }
2840 } else {
2841 Log.i(TAG, "not binding apps: no Launcher activity");
2842 }
2843 }
2844 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002845 // Cleanup any data stored for a deleted user.
2846 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002847
Sunny Goyal2d648b02015-08-11 13:56:28 -07002848 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002849 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002850 Log.d(TAG, "Icons processed in "
2851 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002852 }
2853 }
2854
2855 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002856 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002857 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002858 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2859 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2860 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2861 }
Joe Onorato36115782010-06-17 13:28:48 -04002862 }
2863 }
2864
Sunny Goyal75b0f552015-05-20 21:57:06 -07002865 /**
2866 * Called when the icons for packages have been updated in the icon cache.
2867 */
2868 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2869 final Callbacks callbacks = getCallback();
2870 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2871 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2872
2873 // If any package icon has changed (app was updated while launcher was dead),
2874 // update the corresponding shortcuts.
2875 synchronized (sBgLock) {
2876 for (ItemInfo info : sBgItemsIdMap) {
2877 if (info instanceof ShortcutInfo && user.equals(info.user)
2878 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2879 ShortcutInfo si = (ShortcutInfo) info;
2880 ComponentName cn = si.getTargetComponent();
2881 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2882 si.updateIcon(mIconCache);
2883 updatedShortcuts.add(si);
2884 }
2885 }
2886 }
2887 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2888 }
2889
2890 if (!updatedShortcuts.isEmpty()) {
2891 final UserHandleCompat userFinal = user;
2892 mHandler.post(new Runnable() {
2893
2894 public void run() {
2895 Callbacks cb = getCallback();
2896 if (cb != null && callbacks == cb) {
2897 cb.bindShortcutsChanged(updatedShortcuts,
2898 new ArrayList<ShortcutInfo>(), userFinal);
2899 }
2900 }
2901 });
2902 }
2903
2904 if (!updatedApps.isEmpty()) {
2905 mHandler.post(new Runnable() {
2906
2907 public void run() {
2908 Callbacks cb = getCallback();
2909 if (cb != null && callbacks == cb) {
2910 cb.bindAppsUpdated(updatedApps);
2911 }
2912 }
2913 });
2914 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002915
2916 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002917 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002918 }
2919
Joe Onorato36115782010-06-17 13:28:48 -04002920 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002921 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002922 }
2923
Adam Cohen091440a2015-03-18 14:16:05 -07002924 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002925
2926 @Override
2927 public void onReceive(Context context, Intent intent) {
2928 synchronized (sBgLock) {
2929 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2930 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002931 final PackageManager manager = context.getPackageManager();
2932 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2933 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002934 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2935 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002936 packagesRemoved.clear();
2937 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002938 for (String pkg : entry.getValue()) {
2939 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002940 boolean packageOnSdcard = launcherApps.isAppEnabled(
2941 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2942 if (packageOnSdcard) {
2943 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2944 packagesUnavailable.add(pkg);
2945 } else {
2946 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2947 packagesRemoved.add(pkg);
2948 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002949 }
2950 }
2951 if (!packagesRemoved.isEmpty()) {
2952 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2953 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2954 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002955 if (!packagesUnavailable.isEmpty()) {
2956 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2957 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2958 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002959 }
Sunny Goyal34942622014-08-29 17:20:55 -07002960 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002961 }
2962 }
2963 }
2964
Joe Onorato36115782010-06-17 13:28:48 -04002965 private class PackageUpdatedTask implements Runnable {
2966 int mOp;
2967 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002968 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002969
2970 public static final int OP_NONE = 0;
2971 public static final int OP_ADD = 1;
2972 public static final int OP_UPDATE = 2;
2973 public static final int OP_REMOVE = 3; // uninstlled
2974 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2975
2976
Kenny Guyed131872014-04-30 03:02:21 +01002977 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002978 mOp = op;
2979 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002980 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002981 }
2982
2983 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002984 if (!mHasLoaderCompletedOnce) {
2985 // Loader has not yet run.
2986 return;
2987 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002988 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002989
2990 final String[] packages = mPackages;
2991 final int N = packages.length;
2992 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002993 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002994 for (int i=0; i<N; i++) {
2995 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002996 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002997 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002998 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002999
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003000 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3001 if (heuristic != null) {
3002 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003003 }
Joe Onorato36115782010-06-17 13:28:48 -04003004 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003005 }
Joe Onorato36115782010-06-17 13:28:48 -04003006 case OP_UPDATE:
3007 for (int i=0; i<N; i++) {
3008 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003009 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003010 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003011 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003012 }
3013 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003014 case OP_REMOVE: {
3015 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3016 if (heuristic != null) {
3017 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003018 }
Joe Onorato36115782010-06-17 13:28:48 -04003019 for (int i=0; i<N; i++) {
3020 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003021 mIconCache.removeIconsForPkg(packages[i], mUser);
3022 }
3023 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003024 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003025 case OP_UNAVAILABLE:
3026 for (int i=0; i<N; i++) {
3027 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3028 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003029 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003030 }
3031 break;
3032 }
3033
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003034 ArrayList<AppInfo> added = null;
3035 ArrayList<AppInfo> modified = null;
3036 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003037
Adam Cohen487f7dd2012-06-28 18:12:10 -07003038 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003039 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003040 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003041 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003042 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003043 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003044 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003045 }
Winson Chung5d55f332012-07-16 20:45:03 -07003046 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003047 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003048 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003049 }
3050
Sunny Goyale0f58d72014-11-10 18:05:31 -08003051 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003052 if (callbacks == null) {
3053 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3054 return;
3055 }
3056
Sunny Goyal4390ace2014-10-13 11:33:11 -07003057 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3058 new HashMap<ComponentName, AppInfo>();
3059
Joe Onorato36115782010-06-17 13:28:48 -04003060 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003061 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003062 for (AppInfo ai : added) {
3063 addedOrUpdatedApps.put(ai.componentName, ai);
3064 }
Joe Onorato36115782010-06-17 13:28:48 -04003065 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003066
Joe Onorato36115782010-06-17 13:28:48 -04003067 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003068 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003069 for (AppInfo ai : modified) {
3070 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003071 }
3072
Joe Onorato36115782010-06-17 13:28:48 -04003073 mHandler.post(new Runnable() {
3074 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003075 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003076 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003077 callbacks.bindAppsUpdated(modifiedFinal);
3078 }
3079 }
3080 });
3081 }
Winson Chung83892cc2013-05-01 16:53:33 -07003082
Sunny Goyal4390ace2014-10-13 11:33:11 -07003083 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003084 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003085 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3086 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3087 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3088
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003089 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003090 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003091 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003092 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3093 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094 boolean infoUpdated = false;
3095 boolean shortcutUpdated = false;
3096
3097 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003098 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003099 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003100 Bitmap icon = Utilities.createIconBitmap(
3101 si.iconResource.packageName,
3102 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003103 if (icon != null) {
3104 si.setIcon(icon);
3105 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003106 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003107 }
3108 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003109
3110 ComponentName cn = si.getTargetComponent();
3111 if (cn != null && packageSet.contains(cn.getPackageName())) {
3112 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3113
3114 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3116 // Auto install icon
3117 PackageManager pm = context.getPackageManager();
3118 ResolveInfo matched = pm.resolveActivity(
3119 new Intent(Intent.ACTION_MAIN)
3120 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3121 PackageManager.MATCH_DEFAULT_ONLY);
3122 if (matched == null) {
3123 // Try to find the best match activity.
3124 Intent intent = pm.getLaunchIntentForPackage(
3125 cn.getPackageName());
3126 if (intent != null) {
3127 cn = intent.getComponent();
3128 appInfo = addedOrUpdatedApps.get(cn);
3129 }
3130
3131 if ((intent == null) || (appInfo == null)) {
3132 removedShortcuts.add(si);
3133 continue;
3134 }
3135 si.promisedIntent = intent;
3136 }
3137 }
3138
3139 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003140 if (appInfo != null) {
3141 si.flags = appInfo.flags;
3142 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003143
Sunny Goyal756adbc2015-04-16 15:20:51 -07003144 si.intent = si.promisedIntent;
3145 si.promisedIntent = null;
3146 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003147 infoUpdated = true;
3148 si.updateIcon(mIconCache);
3149 }
3150
3151 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3152 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3153 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003154 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003155 si.contentDescription = appInfo.contentDescription;
3156 infoUpdated = true;
3157 }
3158
3159 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3160 // Since package was just updated, the target must be available now.
3161 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3162 shortcutUpdated = true;
3163 }
3164 }
3165
3166 if (infoUpdated || shortcutUpdated) {
3167 updatedShortcuts.add(si);
3168 }
3169 if (infoUpdated) {
3170 updateItemInDatabase(context, si);
3171 }
3172 } else if (info instanceof LauncherAppWidgetInfo) {
3173 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3174 if (mUser.equals(widgetInfo.user)
3175 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3176 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003177 widgetInfo.restoreStatus &=
3178 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3179 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003180
3181 // adding this flag ensures that launcher shows 'click to setup'
3182 // if the widget has a config activity. In case there is no config
3183 // activity, it will be marked as 'restored' during bind.
3184 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3185
Sunny Goyal4390ace2014-10-13 11:33:11 -07003186 widgets.add(widgetInfo);
3187 updateItemInDatabase(context, widgetInfo);
3188 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003189 }
3190 }
3191 }
3192
Sunny Goyal4390ace2014-10-13 11:33:11 -07003193 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3194 mHandler.post(new Runnable() {
3195
3196 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003197 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003198 if (callbacks == cb && cb != null) {
3199 callbacks.bindShortcutsChanged(
3200 updatedShortcuts, removedShortcuts, mUser);
3201 }
3202 }
3203 });
3204 if (!removedShortcuts.isEmpty()) {
3205 deleteItemsFromDatabase(context, removedShortcuts);
3206 }
3207 }
3208 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003209 mHandler.post(new Runnable() {
3210 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003211 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003212 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003213 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003214 }
3215 }
3216 });
3217 }
3218 }
3219
Winson Chungdf95eb12013-10-16 14:57:07 -07003220 final ArrayList<String> removedPackageNames =
3221 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003222 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003223 // Mark all packages in the broadcast to be removed
3224 removedPackageNames.addAll(Arrays.asList(packages));
3225 } else if (mOp == OP_UPDATE) {
3226 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003227 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003228 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003229 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003230 }
3231 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003232 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003233
Winson Chungdf95eb12013-10-16 14:57:07 -07003234 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003235 final int removeReason;
3236 if (mOp == OP_UNAVAILABLE) {
3237 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3238 } else {
3239 // Remove all the components associated with this package
3240 for (String pn : removedPackageNames) {
3241 deletePackageFromDatabase(context, pn, mUser);
3242 }
3243 // Remove all the specific components
3244 for (AppInfo a : removedApps) {
3245 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3246 deleteItemsFromDatabase(context, infos);
3247 }
3248 removeReason = 0;
3249 }
3250
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003252 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003254 mHandler.post(new Runnable() {
3255 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003256 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003257 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003258 callbacks.bindComponentsRemoved(
3259 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003260 }
3261 }
3262 });
Joe Onoratobe386092009-11-17 17:32:16 -08003263 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003264
Sunny Goyal2d648b02015-08-11 13:56:28 -07003265 // Update widgets
3266 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3267 // Always refresh for a package event on secondary user
3268 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3269
3270 // Refresh widget list, if the package already had a widget.
3271 synchronized (sBgLock) {
3272 if (sBgWidgetProviders != null) {
3273 HashSet<String> pkgSet = new HashSet<>();
3274 Collections.addAll(pkgSet, mPackages);
3275
3276 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3277 needToRefresh |= key.user.equals(mUser) &&
3278 pkgSet.contains(key.componentName.getPackageName());
3279 }
3280 }
3281 }
3282
3283 if (!needToRefresh && mOp != OP_REMOVE) {
3284 // Refresh widget list, if there is any newly added widget
3285 PackageManager pm = context.getPackageManager();
3286 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003287 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003288 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003289 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003290 .setPackage(pkg), 0);
3291 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003292 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003293 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003294 throw e;
3295 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003296 // Ignore the crash. We can live with a state widget list.
3297 Log.e(TAG, "PM call failed for " + pkg, e);
3298 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003299 }
3300 }
3301
3302 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3303 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003304
Adam Cohen4caf2982013-08-20 18:54:31 -07003305 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003306 mHandler.post(new Runnable() {
3307 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003308 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003309 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003310 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003311 }
3312 }
3313 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 }
3315 }
3316
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003317 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3318 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003319 ArrayList<LauncherAppWidgetProviderInfo> results =
3320 new ArrayList<LauncherAppWidgetProviderInfo>();
3321 try {
3322 synchronized (sBgLock) {
3323 if (sBgWidgetProviders == null || refresh) {
3324 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3325 = new HashMap<>();
3326 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3327 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003328
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003329 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3330 for (AppWidgetProviderInfo pInfo : widgets) {
3331 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3332 UserHandleCompat user = wm.getUser(info);
3333 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3334 }
Robin Lee26ace122015-03-16 19:41:43 +00003335
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003336 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3337 for (CustomAppWidget widget : customWidgets) {
3338 info = new LauncherAppWidgetProviderInfo(context, widget);
3339 UserHandleCompat user = wm.getUser(info);
3340 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3341 }
3342 // Replace the global list at the very end, so that if there is an exception,
3343 // previously loaded provider list is used.
3344 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003345 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003346 results.addAll(sBgWidgetProviders.values());
3347 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003348 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003349 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003350 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003351 (e.getCause() instanceof TransactionTooLargeException ||
3352 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003353 // the returned value may be incomplete and will not be refreshed until the next
3354 // time Launcher starts.
3355 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3356 // onResume is called to refresh the widget provider list.
3357 synchronized (sBgLock) {
3358 if (sBgWidgetProviders != null) {
3359 results.addAll(sBgWidgetProviders.values());
3360 }
3361 return results;
3362 }
3363 } else {
3364 throw e;
3365 }
Adam Cohen59400422014-03-05 18:07:04 -08003366 }
3367 }
3368
Robin Lee26ace122015-03-16 19:41:43 +00003369 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3370 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003371 synchronized (sBgLock) {
3372 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003373 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003374 }
Robin Lee26ace122015-03-16 19:41:43 +00003375 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003376 }
3377 }
3378
Sunny Goyal2d648b02015-08-11 13:56:28 -07003379 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003380
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003381 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003382 @Override
3383 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003384 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003385 final WidgetsModel model = mBgWidgetsModel.clone();
3386
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003387 mHandler.post(new Runnable() {
3388 @Override
3389 public void run() {
3390 Callbacks cb = getCallback();
3391 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003392 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003393 }
3394 }
3395 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003396 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003397 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3398 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003399 }
3400 });
3401 }
3402
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003403 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003404 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003405 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003406 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003407 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003408 @Thunk void updateWidgetsModel(boolean refresh) {
3409 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003410 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003411 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003412
3413 // Update shortcut providers
3414 synchronized (sBgLock) {
3415 try {
3416 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3417 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3418 sBgShortcutProviders = providers;
3419 } catch (RuntimeException e) {
Sunny Goyal877f9282015-09-18 10:18:18 -07003420 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal31860582015-09-18 08:38:57 -07003421 (e.getCause() instanceof TransactionTooLargeException ||
3422 e.getCause() instanceof DeadObjectException)) {
3423 /**
3424 * Ignore exception and use the cached list if available.
3425 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3426 */
3427 } else {
3428 throw e;
3429 }
3430 }
3431 if (sBgShortcutProviders != null) {
3432 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3433 }
3434 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003435 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003436 }
3437
Adam Cohen091440a2015-03-18 14:16:05 -07003438 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003439 UserHandleCompat user) {
3440 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3441 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003442 }
Adam Cohen556f6132014-01-15 15:18:08 -08003443
Kenny Guyed131872014-04-30 03:02:21 +01003444 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3445 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003446 if (cn == null) {
3447 return false;
3448 }
Kenny Guyed131872014-04-30 03:02:21 +01003449 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3450 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003451 return false;
3452 }
Kenny Guyed131872014-04-30 03:02:21 +01003453 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003454 }
3455
Adam Cohena28b78e2014-05-20 17:03:04 -07003456 public static boolean isValidPackage(Context context, String packageName,
3457 UserHandleCompat user) {
3458 if (packageName == null) {
3459 return false;
3460 }
3461 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3462 return launcherApps.isPackageEnabledForProfile(packageName, user);
3463 }
3464
Joe Onorato9c1289c2009-08-17 11:03:03 -04003465 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003466 * Make an ShortcutInfo object for a restored application or shortcut item that points
3467 * to a package that is not yet installed on the system.
3468 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003469 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003470 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003471 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003472 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003473
3474 Bitmap icon = iconInfo.loadIcon(c, info, context);
3475 // the fallback icon
3476 if (icon == null) {
3477 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3478 } else {
3479 info.setIcon(icon);
3480 }
Sunny Goyal34942622014-08-29 17:20:55 -07003481
3482 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003483 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003484 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003485 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003486 }
Sunny Goyal34942622014-08-29 17:20:55 -07003487 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3488 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003489 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003490 }
Sunny Goyal34942622014-08-29 17:20:55 -07003491 } else {
3492 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3493 }
3494
Winson Chung82b016c2015-05-08 17:00:10 -07003495 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003496 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003497 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003498 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003499 return info;
3500 }
3501
3502 /**
3503 * Make an Intent object for a restored application or shortcut item that points
3504 * to the market page for the item.
3505 */
Adam Cohen091440a2015-03-18 14:16:05 -07003506 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003507 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003508 return getMarketIntent(componentName.getPackageName());
3509 }
3510
3511 static Intent getMarketIntent(String packageName) {
3512 return new Intent(Intent.ACTION_VIEW)
3513 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003514 .scheme("market")
3515 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003516 .appendQueryParameter("id", packageName)
3517 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003518 }
3519
3520 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003521 * Make an ShortcutInfo object for a shortcut that is an application.
3522 *
3523 * If c is not null, then it will be used to fill in missing data like the title and icon.
3524 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003525 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003526 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003527 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003528 if (user == null) {
3529 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003530 return null;
3531 }
3532
Kenny Guyed131872014-04-30 03:02:21 +01003533 ComponentName componentName = intent.getComponent();
3534 if (componentName == null) {
3535 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3536 return null;
3537 }
3538
3539 Intent newIntent = new Intent(intent.getAction(), null);
3540 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3541 newIntent.setComponent(componentName);
3542 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003543 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003544 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3545 return null;
3546 }
3547
3548 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003549 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003550 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3551 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3552 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003553 }
3554
Joe Onorato56d82912010-03-07 14:32:10 -05003555 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003556 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003557 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003558 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003559
Joe Onorato56d82912010-03-07 14:32:10 -05003560 // fall back to the class name of the activity
3561 if (info.title == null) {
3562 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003563 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003564
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003566 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003567 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003568 if (lai != null) {
3569 info.flags = AppInfo.initFlags(lai);
3570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003572 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003573
Sunny Goyale2df0622015-04-24 11:27:00 -07003574 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003575 ItemInfoFilter f) {
3576 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3577 for (ItemInfo i : infos) {
3578 if (i instanceof ShortcutInfo) {
3579 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003580 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003581 if (cn != null && f.filterItem(null, info, cn)) {
3582 filtered.add(info);
3583 }
3584 } else if (i instanceof FolderInfo) {
3585 FolderInfo info = (FolderInfo) i;
3586 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003587 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003588 if (cn != null && f.filterItem(info, s, cn)) {
3589 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003590 }
3591 }
Winson Chung64359a52013-07-08 17:17:08 -07003592 } else if (i instanceof LauncherAppWidgetInfo) {
3593 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3594 ComponentName cn = info.providerName;
3595 if (cn != null && f.filterItem(null, info, cn)) {
3596 filtered.add(info);
3597 }
Winson Chung8a435102012-08-30 17:16:53 -07003598 }
3599 }
Winson Chung64359a52013-07-08 17:17:08 -07003600 return new ArrayList<ItemInfo>(filtered);
3601 }
3602
Adam Cohen091440a2015-03-18 14:16:05 -07003603 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003604 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003605 ItemInfoFilter filter = new ItemInfoFilter() {
3606 @Override
3607 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003608 if (info.user == null) {
3609 return cn.equals(cname);
3610 } else {
3611 return cn.equals(cname) && info.user.equals(user);
3612 }
Winson Chung64359a52013-07-08 17:17:08 -07003613 }
3614 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003615 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003616 }
3617
Sunny Goyal1a745e82014-10-02 15:58:31 -07003618 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003619 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003620 */
Adam Cohen091440a2015-03-18 14:16:05 -07003621 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003622 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003623 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003624 // Non-app shortcuts are only supported for current user.
3625 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003627
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003628 // TODO: If there's an explicit component and we can't install that, delete it.
3629
Winson Chung82b016c2015-05-08 17:00:10 -07003630 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003631
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003632 Bitmap icon = iconInfo.loadIcon(c, info, context);
3633 // the fallback icon
3634 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003635 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003636 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003637 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003638 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 return info;
3640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003641
Sunny Goyal2350bc92014-10-14 16:42:54 -07003642 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003643 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3644 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3645 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3646
Adam Cohend9198822011-11-22 16:42:47 -08003647 if (intent == null) {
3648 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3649 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3650 return null;
3651 }
3652
Joe Onorato0589f0f2010-02-08 13:44:00 -08003653 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003654 boolean customIcon = false;
3655 ShortcutIconResource iconResource = null;
3656
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003657 if (bitmap instanceof Bitmap) {
3658 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003659 customIcon = true;
3660 } else {
3661 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003662 if (extra instanceof ShortcutIconResource) {
3663 iconResource = (ShortcutIconResource) extra;
3664 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003665 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003666 }
3667 }
3668
Michael Jurkac9d95c52011-08-29 14:03:34 -07003669 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003670
Kenny Guyed131872014-04-30 03:02:21 +01003671 // Only support intents for current user for now. Intents sent from other
3672 // users wouldn't get here without intent forwarding anyway.
3673 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003674 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003675 icon = mIconCache.getDefaultIcon(info.user);
3676 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003677 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003678 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003679
Winson Chung82b016c2015-05-08 17:00:10 -07003680 info.title = Utilities.trim(name);
3681 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003682 info.intent = intent;
3683 info.customIcon = customIcon;
3684 info.iconResource = iconResource;
3685
3686 return info;
3687 }
3688
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003690 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 * or make a new one.
3692 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003693 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 // See if a placeholder was created for us already
3695 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003696 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003698 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 folders.put(id, folderInfo);
3700 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003701 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003702 }
3703
Joe Onoratobe386092009-11-17 17:32:16 -08003704
Sunny Goyal651077b2014-06-30 14:15:31 -07003705 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3706 return (provider != null) && (provider.provider != null)
3707 && (provider.provider.getPackageName() != null);
3708 }
3709
Joe Onoratobe386092009-11-17 17:32:16 -08003710 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003711 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003712 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3713 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3714 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3715 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003716 if (mLoaderTask != null) {
3717 mLoaderTask.dumpState();
3718 } else {
3719 Log.d(TAG, "mLoaderTask=null");
3720 }
Joe Onoratobe386092009-11-17 17:32:16 -08003721 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003722
3723 public Callbacks getCallback() {
3724 return mCallbacks != null ? mCallbacks.get() : null;
3725 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003726
3727 /**
3728 * @return {@link FolderInfo} if its already loaded.
3729 */
3730 public FolderInfo findFolderById(Long folderId) {
3731 synchronized (sBgLock) {
3732 return sBgFolders.get(folderId);
3733 }
3734 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003735
Sunny Goyal527c7d32015-08-28 15:19:36 -07003736 @Thunk class DeferredMainThreadExecutor implements Executor {
3737
3738 @Override
3739 public void execute(Runnable command) {
3740 runOnMainThread(command);
3741 }
3742 }
3743
Sunny Goyal756adbc2015-04-16 15:20:51 -07003744 /**
3745 * @return the looper for the worker thread which can be used to start background tasks.
3746 */
3747 public static Looper getWorkerLooper() {
3748 return sWorkerThread.getLooper();
3749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003750}