blob: 1fd05246e38bdae1605f7f6630739af38e53a172 [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;
Romain Guyedcce092010-03-04 13:03:17 -080067
Michael Jurkac2f801e2011-07-12 14:19:46 -070068import java.lang.ref.WeakReference;
69import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070070import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070072import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070073import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.util.Collections;
75import java.util.Comparator;
76import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070077import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070078import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070080import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083/**
84 * Maintains in-memory state of the Launcher. It is expected that there should be only one
85 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070086 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 */
Kenny Guyed131872014-04-30 03:02:21 +010088public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010089 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080090 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040091 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070092 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040093
Joe Onorato9c1289c2009-08-17 11:03:03 -040094 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070095
Dan Sandlerd5024042014-01-09 15:01:33 -050096 public static final int LOADER_FLAG_NONE = 0;
97 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
98 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
99
Joe Onorato36115782010-06-17 13:28:48 -0400100 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500101 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800104 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final LauncherAppState mApp;
107 @Thunk final Object mLock = new Object();
108 @Thunk DeferredHandler mHandler = new DeferredHandler();
109 @Thunk LoaderTask mLoaderTask;
110 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700111 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Jason Monkbbe1e242014-05-16 17:37:34 -0400113 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700114
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700116 static {
117 sWorkerThread.start();
118 }
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 // We start off with everything not loaded. After that, we assume that
122 // our monitoring of the package manager provides all updates and we never
123 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk boolean mWorkspaceLoaded;
125 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700126
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700127 // When we are loading pages synchronously, we can't just post the binding of items on the side
128 // pages as this delays the rotation process. Instead, we wait for a callback from the first
129 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
130 // a normal load, we also clear this set of Runnables.
131 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700142 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
144 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700146 // The lock that must be acquired before referencing any static bg data structures. Unlike
147 // other locks, this one can generally be held long-term because we never expect any of these
148 // static data structures to be referenced outside of the worker thread except on the first
149 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700150 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700154 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
157 // created by LauncherModel that are directly on the home screen (however, no widgets or
158 // shortcuts within folders).
159 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
162 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163 new ArrayList<LauncherAppWidgetInfo>();
164
Adam Cohen487f7dd2012-06-28 18:12:10 -0700165 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700166 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700167
Adam Cohendcd297f2013-06-18 13:13:40 -0700168 // sBgWorkspaceScreens is the ordered set of workspace screens.
169 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
170
Adam Cohen59400422014-03-05 18:07:04 -0800171 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000172 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800173
Sunny Goyal31860582015-09-18 08:38:57 -0700174 // sBgShortcutProviders is the set of custom shortcut providers
175 public static List<ResolveInfo> sBgShortcutProviders;
176
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700177 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700178 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
179 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700180
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700181 // </ only access in worker thread >
182
Adam Cohen091440a2015-03-18 14:16:05 -0700183 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Adam Cohen091440a2015-03-18 14:16:05 -0700185 @Thunk final LauncherAppsCompat mLauncherApps;
186 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700189 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public int getCurrentWorkspaceScreen();
191 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700192 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
193 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700194 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700195 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700196 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800197 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200199 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700200 public void bindAppsAdded(ArrayList<Long> newScreens,
201 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700202 ArrayList<ItemInfo> addAnimated,
203 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200204 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700205 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
206 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
207 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700208 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700209 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700210 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700211 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700212 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700213 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700214 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700215 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Winson Chung64359a52013-07-08 17:17:08 -0700218 public interface ItemInfoFilter {
219 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
220 }
221
Bjorn Bringert1307f632013-10-03 22:31:03 +0100222 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800223 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400224
Winson Chungee055712013-07-30 14:46:24 -0700225 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700226 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400227 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
228 // resource string.
229 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
230 ProviderInfo providerInfo =
231 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
232 ProviderInfo redirectProvider =
233 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700234
235 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400236 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700237
238 if (mOldContentProviderExists) {
239 Log.d(TAG, "Old launcher provider exists.");
240 } else {
241 Log.d(TAG, "Old launcher provider does not exist.");
242 }
243
Daniel Sandlere4f98912013-06-25 15:13:26 -0400244 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100245 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700246 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 mIconCache = iconCache;
248
Kenny Guyed131872014-04-30 03:02:21 +0100249 mLauncherApps = LauncherAppsCompat.getInstance(context);
250 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 }
252
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
254 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700256 if (sWorkerThread.getThreadId() == Process.myTid()) {
257 // If we are on the worker thread, post onto the main handler
258 mHandler.post(r);
259 } else {
260 r.run();
261 }
262 }
263
264 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
265 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700266 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700267 if (sWorkerThread.getThreadId() == Process.myTid()) {
268 r.run();
269 } else {
270 // If we are not on the worker thread, then post to the worker handler
271 sWorker.post(r);
272 }
273 }
274
Winson Chunge43a1e72014-01-15 10:33:02 -0800275 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
276 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800277 }
278
Sunny Goyal756adbc2015-04-16 15:20:51 -0700279 public void setPackageState(final PackageInstallInfo installInfo) {
280 Runnable updateRunnable = new Runnable() {
281
282 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500283 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700284 synchronized (sBgLock) {
285 final HashSet<ItemInfo> updates = new HashSet<>();
286
287 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
288 // Ignore install success events as they are handled by Package add events.
289 return;
290 }
291
Sunny Goyale2df0622015-04-24 11:27:00 -0700292 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 if (info instanceof ShortcutInfo) {
294 ShortcutInfo si = (ShortcutInfo) info;
295 ComponentName cn = si.getTargetComponent();
296 if (si.isPromise() && (cn != null)
297 && installInfo.packageName.equals(cn.getPackageName())) {
298 si.setInstallProgress(installInfo.progress);
299
300 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
301 // Mark this info as broken.
302 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
303 }
304 updates.add(si);
305 }
306 }
307 }
308
309 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
310 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
311 widget.installProgress = installInfo.progress;
312 updates.add(widget);
313 }
314 }
315
316 if (!updates.isEmpty()) {
317 // Push changes to the callback.
318 Runnable r = new Runnable() {
319 public void run() {
320 Callbacks callbacks = getCallback();
321 if (callbacks != null) {
322 callbacks.bindRestoreItemsChange(updates);
323 }
324 }
325 };
326 mHandler.post(r);
327 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500328 }
329 }
330 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500332 }
333
Sunny Goyal756adbc2015-04-16 15:20:51 -0700334 /**
335 * Updates the icons and label of all pending icons for the provided package name.
336 */
337 public void updateSessionDisplayInfo(final String packageName) {
338 Runnable updateRunnable = new Runnable() {
339
340 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700341 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 synchronized (sBgLock) {
343 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
344 final UserHandleCompat user = UserHandleCompat.myUserHandle();
345
Sunny Goyale2df0622015-04-24 11:27:00 -0700346 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700347 if (info instanceof ShortcutInfo) {
348 ShortcutInfo si = (ShortcutInfo) info;
349 ComponentName cn = si.getTargetComponent();
350 if (si.isPromise() && (cn != null)
351 && packageName.equals(cn.getPackageName())) {
352 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
353 // For auto install apps update the icon as well as label.
354 mIconCache.getTitleAndIcon(si,
355 si.promisedIntent, user,
356 si.shouldUseLowResIcon());
357 } else {
358 // Only update the icon for restored apps.
359 si.updateIcon(mIconCache);
360 }
361 updates.add(si);
362 }
363 }
364 }
365
366 if (!updates.isEmpty()) {
367 // Push changes to the callback.
368 Runnable r = new Runnable() {
369 public void run() {
370 Callbacks callbacks = getCallback();
371 if (callbacks != null) {
372 callbacks.bindShortcutsChanged(updates,
373 new ArrayList<ShortcutInfo>(), user);
374 }
375 }
376 };
377 mHandler.post(r);
378 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700379 }
380 }
381 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700382 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700383 }
384
Adam Cohen76a47a12014-02-05 11:47:43 -0800385 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800386 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800387
388 if (allAppsApps == null) {
389 throw new RuntimeException("allAppsApps must not be null");
390 }
391 if (allAppsApps.isEmpty()) {
392 return;
393 }
394
395 // Process the newly added applications and add them to the database first
396 Runnable r = new Runnable() {
397 public void run() {
398 runOnMainThread(new Runnable() {
399 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800400 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800401 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500402 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800403 }
404 }
405 });
406 }
407 };
408 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700409 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800410
Sunny Goyala9116722015-04-29 13:55:58 -0700411 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 int[] xy, int spanX, int spanY) {
413 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700414 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
415 final int xCount = (int) profile.numColumns;
416 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800417 boolean[][] occupied = new boolean[xCount][yCount];
418 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700419 for (ItemInfo r : occupiedPos) {
420 int right = r.cellX + r.spanX;
421 int bottom = r.cellY + r.spanY;
422 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
423 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 occupied[x][y] = true;
425 }
426 }
427 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800428 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700429 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
431
432 /**
433 * Find a position on the screen for the given size or adds a new screen.
434 * @return screenId and the coordinates for the item.
435 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700436 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 ArrayList<Long> workspaceScreens,
439 ArrayList<Long> addedWorkspaceScreensFinal,
440 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700441 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800442
Sunny Goyala9116722015-04-29 13:55:58 -0700443 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700444 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700445 synchronized (sBgLock) {
446 for (ItemInfo info : sBgItemsIdMap) {
447 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
448 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
449 if (items == null) {
450 items = new ArrayList<>();
451 screenItems.put(info.screenId, items);
452 }
453 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800454 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800455 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800456 }
457
458 // Find appropriate space for the item.
459 long screenId = 0;
460 int[] cordinates = new int[2];
461 boolean found = false;
462
463 int screenCount = workspaceScreens.size();
464 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700465 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 if (preferredScreenIndex < screenCount) {
467 screenId = workspaceScreens.get(preferredScreenIndex);
468 found = findNextAvailableIconSpaceInScreen(
469 screenItems.get(screenId), cordinates, spanX, spanY);
470 }
471
472 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700473 // Search on any of the screens starting from the first screen.
474 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800475 screenId = workspaceScreens.get(screen);
476 if (findNextAvailableIconSpaceInScreen(
477 screenItems.get(screenId), cordinates, spanX, spanY)) {
478 // We found a space for it
479 found = true;
480 break;
481 }
482 }
483 }
484
485 if (!found) {
486 // Still no position found. Add a new screen to the end.
487 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
488
489 // Save the screen id for binding in the workspace
490 workspaceScreens.add(screenId);
491 addedWorkspaceScreensFinal.add(screenId);
492
493 // If we still can't find an empty space, then God help us all!!!
494 if (!findNextAvailableIconSpaceInScreen(
495 screenItems.get(screenId), cordinates, spanX, spanY)) {
496 throw new RuntimeException("Can't find space to add the item");
497 }
498 }
499 return Pair.create(screenId, cordinates);
500 }
501
502 /**
503 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700505 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700506 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800507 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800508 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700509 return;
Winson Chung997a9232013-07-24 15:33:46 -0700510 }
Winson Chung64359a52013-07-08 17:17:08 -0700511 // Process the newly added applications and add them to the database first
512 Runnable r = new Runnable() {
513 public void run() {
514 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
515 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
516
Winson Chung76828c82013-08-19 15:43:29 -0700517 // Get the list of workspace screens. We need to append to this list and
518 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
519 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800520 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700521 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700523 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700525 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700527 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800528 }
Winson Chung76828c82013-08-19 15:43:29 -0700529
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800530 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700531 Pair<Long, int[]> coords = findSpaceForItem(context,
532 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800533 1, 1);
534 long screenId = coords.first;
535 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700536
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700537 ItemInfo itemInfo;
538 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
539 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700541 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700542 } else {
543 throw new RuntimeException("Unexpected info type");
544 }
Winson Chung94d67682013-09-25 16:29:40 -0700545
Winson Chung64359a52013-07-08 17:17:08 -0700546 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700547 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700548 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700549 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700550 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700551 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700552 }
553 }
554
Winson Chung76828c82013-08-19 15:43:29 -0700555 // Update the workspace screens
556 updateWorkspaceScreenOrder(context, workspaceScreens);
557
Adam Cohen76a47a12014-02-05 11:47:43 -0800558 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700559 runOnMainThread(new Runnable() {
560 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800561 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700562 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700563 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
564 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700565 if (!addedShortcutsFinal.isEmpty()) {
566 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
567 long lastScreenId = info.screenId;
568 for (ItemInfo i : addedShortcutsFinal) {
569 if (i.screenId == lastScreenId) {
570 addAnimated.add(i);
571 } else {
572 addNotAnimated.add(i);
573 }
Winson Chung997a9232013-07-24 15:33:46 -0700574 }
575 }
Winson Chungd64d1762013-08-20 14:37:16 -0700576 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800577 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700578 }
Winson Chung64359a52013-07-08 17:17:08 -0700579 }
Winson Chung997a9232013-07-24 15:33:46 -0700580 });
581 }
Winson Chung64359a52013-07-08 17:17:08 -0700582 }
583 };
584 runOnWorkerThread(r);
585 }
586
Sunny Goyald33860f2015-04-23 16:02:20 -0700587 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700588 if (sWorkerThread.getThreadId() == Process.myTid()) {
589 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
590 "main thread");
591 }
592
593 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400594 synchronized (mDeferredBindRunnables) {
595 mDeferredBindRunnables.clear();
596 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700597
598 // Remove any queued UI runnables
599 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700600 // Unbind all the workspace items
601 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700602 }
603
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700604 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700605 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700606 // Ensure that we don't use the same workspace items data structure on the main thread
607 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700608 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700609 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700610 tmpItems.addAll(sBgWorkspaceItems);
611 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700612 }
613 Runnable r = new Runnable() {
614 @Override
615 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700616 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700617 item.unbind();
618 }
619 }
620 };
621 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700622 }
623
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624 /**
625 * Adds an item to the DB if it was not created previously, or move it to a new
626 * <container, screen, cellX, cellY>
627 */
628 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700629 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 if (item.container == ItemInfo.NO_ID) {
631 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700632 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 } else {
634 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
637 }
638
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700639 static void checkItemInfoLocked(
640 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
641 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
642 if (modelItem != null && item != modelItem) {
643 // check all the data is consistent
644 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
645 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
646 ShortcutInfo shortcut = (ShortcutInfo) item;
647 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
648 modelShortcut.intent.filterEquals(shortcut.intent) &&
649 modelShortcut.id == shortcut.id &&
650 modelShortcut.itemType == shortcut.itemType &&
651 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700652 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700653 modelShortcut.cellX == shortcut.cellX &&
654 modelShortcut.cellY == shortcut.cellY &&
655 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700656 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700657 // For all intents and purposes, this is the same object
658 return;
659 }
660 }
661
662 // the modelItem needs to match up perfectly with item if our model is
663 // to be consistent with the database-- for now, just require
664 // modelItem == item or the equality check above
665 String msg = "item: " + ((item != null) ? item.toString() : "null") +
666 "modelItem: " +
667 ((modelItem != null) ? modelItem.toString() : "null") +
668 "Error: ItemInfo passed to checkItemInfo doesn't match original";
669 RuntimeException e = new RuntimeException(msg);
670 if (stackTrace != null) {
671 e.setStackTrace(stackTrace);
672 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800673 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700674 }
675 }
676
Michael Jurka816474f2012-06-25 14:49:02 -0700677 static void checkItemInfo(final ItemInfo item) {
678 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
679 final long itemId = item.id;
680 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700681 public void run() {
682 synchronized (sBgLock) {
683 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700684 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700685 }
686 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700687 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700688 }
689
Michael Jurkac9d95c52011-08-29 14:03:34 -0700690 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
691 final ItemInfo item, final String callingFunction) {
692 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700693 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700694 final ContentResolver cr = context.getContentResolver();
695
Adam Cohen487f7dd2012-06-28 18:12:10 -0700696 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700697 Runnable r = new Runnable() {
698 public void run() {
699 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 updateItemArrays(item, itemId, stackTrace);
701 }
702 };
703 runOnWorkerThread(r);
704 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700705
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700706 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
707 final ArrayList<ItemInfo> items, final String callingFunction) {
708 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700709
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
711 Runnable r = new Runnable() {
712 public void run() {
713 ArrayList<ContentProviderOperation> ops =
714 new ArrayList<ContentProviderOperation>();
715 int count = items.size();
716 for (int i = 0; i < count; i++) {
717 ItemInfo item = items.get(i);
718 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700719 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700720 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700721
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700722 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
723 updateItemArrays(item, itemId, stackTrace);
724
725 }
726 try {
727 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
728 } catch (Exception e) {
729 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730 }
731 }
732 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700733 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700734 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700735
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
737 // Lock on mBgLock *after* the db operation
738 synchronized (sBgLock) {
739 checkItemInfoLocked(itemId, item, stackTrace);
740
741 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
742 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
743 // Item is in a folder, make sure this folder exists
744 if (!sBgFolders.containsKey(item.container)) {
745 // An items container is being set to a that of an item which is not in
746 // the list of Folders.
747 String msg = "item: " + item + " container being set to: " +
748 item.container + ", not in the list of folders";
749 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 }
751 }
752
753 // Items are added/removed from the corresponding FolderInfo elsewhere, such
754 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
755 // that are on the desktop, as appropriate
756 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800757 if (modelItem != null &&
758 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
759 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700760 switch (modelItem.itemType) {
761 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
762 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
763 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
764 if (!sBgWorkspaceItems.contains(modelItem)) {
765 sBgWorkspaceItems.add(modelItem);
766 }
767 break;
768 default:
769 break;
770 }
771 } else {
772 sBgWorkspaceItems.remove(modelItem);
773 }
774 }
775 }
776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700779 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700780 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783 item.cellX = cellX;
784 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700785
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 // We store hotseat items in canonical form which is this orientation invariant position
787 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700791 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794
795 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700797 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
798 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800799 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801
Michael Jurkac9d95c52011-08-29 14:03:34 -0700802 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700803 }
804
805 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700806 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
807 * cellX, cellY have already been updated on the ItemInfos.
808 */
809 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
810 final long container, final int screen) {
811
812 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
813 int count = items.size();
814
815 for (int i = 0; i < count; i++) {
816 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700817 item.container = container;
818
819 // We store hotseat items in canonical form which is this orientation invariant position
820 // in the hotseat
821 if (context instanceof Launcher && screen < 0 &&
822 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700824 item.cellY);
825 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700826 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700827 }
828
829 final ContentValues values = new ContentValues();
830 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
831 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
832 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800833 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700834 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700835
836 contentValues.add(values);
837 }
838 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
839 }
840
841 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700842 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800843 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700844 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700846 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800847 item.cellX = cellX;
848 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 item.spanX = spanX;
850 item.spanY = spanY;
851
852 // We store hotseat items in canonical form which is this orientation invariant position
853 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 }
Adam Cohend4844c32011-02-18 19:25:06 -0800860
Adam Cohend4844c32011-02-18 19:25:06 -0800861 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800862 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
864 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800865 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700866 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
867 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700868 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800869
Michael Jurka816474f2012-06-25 14:49:02 -0700870 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700871 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700872
873 /**
874 * Update an item to the database in a specified container.
875 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700876 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700877 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100878 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700879 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800880 }
881
Sunny Goyal756a28a2015-04-23 17:07:55 -0700882 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700883 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700884 synchronized (mLock) {
885 if (!mHasLoaderCompletedOnce ||
886 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
887 throw new RuntimeException("Trying to add shortcut while loader is running");
888 }
889 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700890 }
891 }
892
Adam Cohend4844c32011-02-18 19:25:06 -0800893 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700894 * Returns true if the shortcuts already exists on the workspace. This must be called after
895 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700897 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
898 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700899 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 if (intent.getComponent() != null) {
901 // If component is not null, an intent with null package will produce
902 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700903 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700905 intentWithPkg = intent.toUri(0);
906 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700907 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700908 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
909 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700910 }
911 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700912 intentWithPkg = intent.toUri(0);
913 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700914 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700915
916 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700917 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700918 if (item instanceof ShortcutInfo) {
919 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700920 Intent targetIntent = info.promisedIntent == null
921 ? info.intent : info.promisedIntent;
922 if (targetIntent != null && info.user.equals(user)) {
923 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700924 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
925 return true;
926 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927 }
928 }
929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700931 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700932 }
933
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 /**
935 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
936 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700937 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 final ContentResolver cr = context.getContentResolver();
939 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
940 "_id=? and (itemType=? or itemType=?)",
941 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700942 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700943
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 try {
945 if (c.moveToFirst()) {
946 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
947 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
948 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
949 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
950 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
951 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700952 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 FolderInfo folderInfo = null;
955 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700956 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
957 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700959 }
960
Sunny Goyala508e4f2015-05-21 09:33:57 -0700961 // Do not trim the folder label, as is was set by the user.
962 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 folderInfo.id = id;
964 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700966 folderInfo.cellX = c.getInt(cellXIndex);
967 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700968 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969
970 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 } finally {
973 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700974 }
975
976 return null;
977 }
978
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 /**
980 * Add an item to the database in a specified container. Sets the container, screen, cellX and
981 * cellY fields of the item. Also assigns an ID to the item.
982 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700983 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700984 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 item.cellX = cellX;
987 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700988 // We store hotseat items in canonical form which is this orientation invariant position
989 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996
997 final ContentValues values = new ContentValues();
998 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100999 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000
Michael Jurka414300a2013-08-27 15:42:35 +02001001 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001002 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001003
Jason Monk8e19cf22014-03-20 15:06:57 -04001004 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001005 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001007 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001010 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001011 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 sBgItemsIdMap.put(item.id, item);
1013 switch (item.itemType) {
1014 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1015 sBgFolders.put(item.id, (FolderInfo) item);
1016 // Fall through
1017 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1018 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1019 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1020 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1021 sBgWorkspaceItems.add(item);
1022 } else {
1023 if (!sBgFolders.containsKey(item.container)) {
1024 // Adding an item to a folder that doesn't exist.
1025 String msg = "adding item: " + item + " to a folder that " +
1026 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001027 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001028 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001029 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001030 break;
1031 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1032 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1033 break;
1034 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001035 }
1036 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001037 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001039 }
1040
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001042 * Creates a new unique child id, for a given cell span across all layouts.
1043 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001044 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001045 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001046 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001047 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001048 }
1049
Sunny Goyal34942622014-08-29 17:20:55 -07001050 private static ArrayList<ItemInfo> getItemsByPackageName(
1051 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001052 ItemInfoFilter filter = new ItemInfoFilter() {
1053 @Override
1054 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1055 return cn.getPackageName().equals(pn) && info.user.equals(user);
1056 }
1057 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001058 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001059 }
1060
1061 /**
1062 * Removes all the items from the database corresponding to the specified package.
1063 */
1064 static void deletePackageFromDatabase(Context context, final String pn,
1065 final UserHandleCompat user) {
1066 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001067 }
1068
1069 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001070 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001071 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001072 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001073 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1074 items.add(item);
1075 deleteItemsFromDatabase(context, items);
1076 }
1077
1078 /**
1079 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001080 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001081 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001083 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001084 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001085 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001086 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001087 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001088
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001089 // Lock on mBgLock *after* the db operation
1090 synchronized (sBgLock) {
1091 switch (item.itemType) {
1092 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1093 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001094 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001095 if (info.container == item.id) {
1096 // We are deleting a folder which still contains items that
1097 // think they are contained by that folder.
1098 String msg = "deleting a folder (" + item + ") which still " +
1099 "contains items (" + info + ")";
1100 Log.e(TAG, msg);
1101 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 sBgWorkspaceItems.remove(item);
1104 break;
1105 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1106 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1107 sBgWorkspaceItems.remove(item);
1108 break;
1109 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1110 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1111 break;
1112 }
1113 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001115 }
1116 }
Michael Jurka83df1882011-08-31 20:59:26 -07001117 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 }
1120
1121 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001122 * Update the order of the workspace screens in the database. The array list contains
1123 * a list of screen ids in the order that they should appear.
1124 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001125 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001126 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001127 final ContentResolver cr = context.getContentResolver();
1128 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1129
1130 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001131 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 while (iter.hasNext()) {
1133 long id = iter.next();
1134 if (id < 0) {
1135 iter.remove();
1136 }
1137 }
1138
1139 Runnable r = new Runnable() {
1140 @Override
1141 public void run() {
Yura085c8532014-02-11 15:15:29 +00001142 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001144 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001145 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001146 for (int i = 0; i < count; i++) {
1147 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001148 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1150 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001151 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001152 }
Yura085c8532014-02-11 15:15:29 +00001153
1154 try {
1155 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1156 } catch (Exception ex) {
1157 throw new RuntimeException(ex);
1158 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001159
Winson Chungba9c37f2013-08-30 14:11:37 -07001160 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001161 sBgWorkspaceScreens.clear();
1162 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001163 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 }
1165 };
1166 runOnWorkerThread(r);
1167 }
1168
1169 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001170 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 */
Winsonc0b52fe2015-09-09 16:38:15 -07001172 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 final ContentResolver cr = context.getContentResolver();
1174
Michael Jurkac9d95c52011-08-29 14:03:34 -07001175 Runnable r = new Runnable() {
1176 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001177 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001178 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001179 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001180 sBgItemsIdMap.remove(info.id);
1181 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001182 sBgWorkspaceItems.remove(info);
1183 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001184
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001185 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001186 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001188 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001189 for (ItemInfo childInfo : info.contents) {
1190 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001192 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001193 }
1194 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 }
1197
1198 /**
1199 * Set this as the current Launcher activity object for the loader.
1200 */
1201 public void initialize(Callbacks callbacks) {
1202 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001203 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1204 // workspace to prevent leaking Launcher activities on orientation change.
1205 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 mCallbacks = new WeakReference<Callbacks>(callbacks);
1207 }
1208 }
1209
Kenny Guyed131872014-04-30 03:02:21 +01001210 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001211 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001212 int op = PackageUpdatedTask.OP_UPDATE;
1213 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1214 user));
1215 }
1216
1217 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001218 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001219 int op = PackageUpdatedTask.OP_REMOVE;
1220 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1221 user));
1222 }
1223
1224 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001225 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001226 int op = PackageUpdatedTask.OP_ADD;
1227 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1228 user));
1229 }
1230
1231 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001232 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001233 boolean replacing) {
1234 if (!replacing) {
1235 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1236 user));
1237 if (mAppsCanBeOnRemoveableStorage) {
1238 // Only rebind if we support removable storage. It catches the
1239 // case where
1240 // apps on the external sd card need to be reloaded
1241 startLoaderFromBackground();
1242 }
1243 } else {
1244 // If we are replacing then just update the packages in the list
1245 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1246 packageNames, user));
1247 }
1248 }
1249
1250 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001251 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001252 boolean replacing) {
1253 if (!replacing) {
1254 enqueuePackageUpdated(new PackageUpdatedTask(
1255 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1256 user));
1257 }
Kenny Guyed131872014-04-30 03:02:21 +01001258 }
1259
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001260 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1262 * ACTION_PACKAGE_CHANGED.
1263 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001264 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001265 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001266 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001267
Joe Onorato36115782010-06-17 13:28:48 -04001268 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001269 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001270 // If we have changed locale we need to clear out the labels in all apps/workspace.
1271 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001272 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001273 Callbacks callbacks = getCallback();
1274 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001275 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001276 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001277 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1278 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001279 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001280 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001281 }
1282 }
1283
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001284 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001285 resetLoadedState(true, true);
1286
Reena Lee93f824a2011-09-23 17:20:28 -07001287 // Do this here because if the launcher activity is running it will be restarted.
1288 // If it's not running startLoaderFromBackground will merely tell it that it needs
1289 // to reload.
1290 startLoaderFromBackground();
1291 }
1292
Winson Chungf0c6ae02012-03-21 16:10:31 -07001293 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1294 synchronized (mLock) {
1295 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1296 // mWorkspaceLoaded to true later
1297 stopLoaderLocked();
1298 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1299 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1300 }
1301 }
1302
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001303 /**
1304 * When the launcher is in the background, it's possible for it to miss paired
1305 * configuration changes. So whenever we trigger the loader from the background
1306 * tell the launcher that it needs to re-run the loader when it comes back instead
1307 * of doing it now.
1308 */
1309 public void startLoaderFromBackground() {
1310 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001311 Callbacks callbacks = getCallback();
1312 if (callbacks != null) {
1313 // Only actually run the loader if they're not paused.
1314 if (!callbacks.setLoadOnResume()) {
1315 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001316 }
1317 }
1318 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001319 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001320 }
Joe Onorato36115782010-06-17 13:28:48 -04001321 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001322
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001323 /**
1324 * If there is already a loader task running, tell it to stop.
1325 */
1326 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001327 LoaderTask oldTask = mLoaderTask;
1328 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001329 oldTask.stopLocked();
1330 }
Reena Lee93f824a2011-09-23 17:20:28 -07001331 }
1332
Adam Cohen1a85c582014-09-30 09:48:49 -07001333 public boolean isCurrentCallbacks(Callbacks callbacks) {
1334 return (mCallbacks != null && mCallbacks.get() == callbacks);
1335 }
1336
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001337 public void startLoader(int synchronousBindPage) {
1338 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001339 }
1340
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001341 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001342 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1343 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001344 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001345 // Clear any deferred bind-runnables from the synchronized load process
1346 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001347 synchronized (mDeferredBindRunnables) {
1348 mDeferredBindRunnables.clear();
1349 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001350
Joe Onorato36115782010-06-17 13:28:48 -04001351 // Don't bother to start the thread if we know it's not going to do anything
1352 if (mCallbacks != null && mCallbacks.get() != null) {
1353 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001354 stopLoaderLocked();
1355 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001356 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001357 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1359 } else {
1360 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1361 sWorker.post(mLoaderTask);
1362 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001364 }
1365 }
1366
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001367 void bindRemainingSynchronousPages() {
1368 // Post the remaining side pages to be loaded
1369 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001370 Runnable[] deferredBindRunnables = null;
1371 synchronized (mDeferredBindRunnables) {
1372 deferredBindRunnables = mDeferredBindRunnables.toArray(
1373 new Runnable[mDeferredBindRunnables.size()]);
1374 mDeferredBindRunnables.clear();
1375 }
1376 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001377 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 }
1380 }
1381
Joe Onorato36115782010-06-17 13:28:48 -04001382 public void stopLoader() {
1383 synchronized (mLock) {
1384 if (mLoaderTask != null) {
1385 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 }
1387 }
Joe Onorato36115782010-06-17 13:28:48 -04001388 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001389
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001390 /**
1391 * Loads the workspace screen ids in an ordered list.
1392 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001393 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001394 final ContentResolver contentResolver = context.getContentResolver();
1395 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001396
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001397 // Get screens ordered by rank.
1398 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1399 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1400 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001401 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001402 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001403 while (sc.moveToNext()) {
1404 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001405 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001406 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001407 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1408 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001409 }
1410 }
1411 } finally {
1412 sc.close();
1413 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001414 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001415 }
1416
Michael Jurkac57b7a82011-08-09 22:02:20 -07001417 public boolean isAllAppsLoaded() {
1418 return mAllAppsLoaded;
1419 }
1420
Joe Onorato36115782010-06-17 13:28:48 -04001421 /**
1422 * Runnable for the thread that loads the contents of the launcher:
1423 * - workspace icons
1424 * - widgets
1425 * - all apps icons
1426 */
1427 private class LoaderTask implements Runnable {
1428 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001429 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001430 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001431 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001432 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001434 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001435 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001436 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
1438
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001439 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001440 mIsLoadingAndBindingWorkspace = true;
1441
Joe Onorato36115782010-06-17 13:28:48 -04001442 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001443 if (DEBUG_LOADERS) {
1444 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001446
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001447 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001448 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001449 synchronized (LoaderTask.this) {
1450 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001451 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001452 }
1453 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454 }
1455 }
1456
Joe Onorato36115782010-06-17 13:28:48 -04001457 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001458 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001459 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001460
Joe Onorato36115782010-06-17 13:28:48 -04001461 private void waitForIdle() {
1462 // Wait until the either we're stopped or the other threads are done.
1463 // This way we don't start loading all apps until the workspace has settled
1464 // down.
1465 synchronized (LoaderTask.this) {
1466 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001467
Joe Onorato36115782010-06-17 13:28:48 -04001468 mHandler.postIdle(new Runnable() {
1469 public void run() {
1470 synchronized (LoaderTask.this) {
1471 mLoadAndBindStepFinished = true;
1472 if (DEBUG_LOADERS) {
1473 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001476 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001477 }
Joe Onorato36115782010-06-17 13:28:48 -04001478 });
1479
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001480 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001481 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001482 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1483 // wait no longer than 1sec at a time
1484 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001485 } catch (InterruptedException ex) {
1486 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001487 }
1488 }
Joe Onorato36115782010-06-17 13:28:48 -04001489 if (DEBUG_LOADERS) {
1490 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001491 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001492 + "ms for previous step to finish binding");
1493 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001494 }
Joe Onorato36115782010-06-17 13:28:48 -04001495 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001496
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001497 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001498 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001499 // Ensure that we have a valid page index to load synchronously
1500 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1501 "valid page index");
1502 }
1503 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1504 // Ensure that we don't try and bind a specified page when the pages have not been
1505 // loaded already (we should load everything asynchronously in that case)
1506 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1507 }
1508 synchronized (mLock) {
1509 if (mIsLoaderTaskRunning) {
1510 // Ensure that we are never running the background loading at this point since
1511 // we also touch the background collections
1512 throw new RuntimeException("Error! Background loading is already running");
1513 }
1514 }
1515
1516 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1517 // data structures, we can't allow any other thread to touch that data, but because
1518 // this call is synchronous, we can get away with not locking).
1519
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001520 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001521 // operations from the previous activity. We need to ensure that all queued operations
1522 // are executed before any synchronous binding work is done.
1523 mHandler.flush();
1524
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525 // Divide the set of loaded items into those that we are binding synchronously, and
1526 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001527 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1529 // arise from that.
1530 onlyBindAllApps();
1531 }
1532
Joe Onorato36115782010-06-17 13:28:48 -04001533 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001535 if (mStopped) {
1536 return;
1537 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001538 mIsLoaderTaskRunning = true;
1539 }
Joe Onorato36115782010-06-17 13:28:48 -04001540 // Optimize for end-user experience: if the Launcher is up and // running with the
1541 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1542 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001543 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001544 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001545 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001546
Joe Onorato36115782010-06-17 13:28:48 -04001547 if (mStopped) {
1548 break keep_running;
1549 }
1550
Joe Onorato36115782010-06-17 13:28:48 -04001551 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001552
1553 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001554 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1555 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557
Joe Onorato36115782010-06-17 13:28:48 -04001558 // Clear out this reference, otherwise we end up holding it until all of the
1559 // callback runnables are done.
1560 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561
Joe Onorato36115782010-06-17 13:28:48 -04001562 synchronized (mLock) {
1563 // If we are still the last one to be scheduled, remove ourselves.
1564 if (mLoaderTask == this) {
1565 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001566 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001567 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001568 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001569 }
Joe Onorato36115782010-06-17 13:28:48 -04001570 }
1571
1572 public void stopLocked() {
1573 synchronized (LoaderTask.this) {
1574 mStopped = true;
1575 this.notify();
1576 }
1577 }
1578
1579 /**
1580 * Gets the callbacks object. If we've been stopped, or if the launcher object
1581 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1582 * object that was around when the deferred message was scheduled, and if there's
1583 * a new Callbacks object around then also return null. This will save us from
1584 * calling onto it with data that will be ignored.
1585 */
1586 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1587 synchronized (mLock) {
1588 if (mStopped) {
1589 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001590 }
Joe Onorato36115782010-06-17 13:28:48 -04001591
1592 if (mCallbacks == null) {
1593 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001594 }
Joe Onorato36115782010-06-17 13:28:48 -04001595
1596 final Callbacks callbacks = mCallbacks.get();
1597 if (callbacks != oldCallbacks) {
1598 return null;
1599 }
1600 if (callbacks == null) {
1601 Log.w(TAG, "no mCallbacks");
1602 return null;
1603 }
1604
1605 return callbacks;
1606 }
1607 }
1608
1609 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001610 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1611 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001612 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001613 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001614 final int countX = profile.numColumns;
1615 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001616
Adam Cohendcd297f2013-06-18 13:13:40 -07001617 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001618 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001619 // Return early if we detect that an item is under the hotseat button
1620 if (mCallbacks == null ||
1621 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001622 Log.e(TAG, "Error loading shortcut into hotseat " + item
1623 + " into position (" + item.screenId + ":" + item.cellX + ","
1624 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001625 return false;
1626 }
1627
Dan Sandler295ae182013-12-10 16:05:47 -05001628 final ItemInfo[][] hotseatItems =
1629 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1630
Adam Cohen2e6da152015-05-06 11:42:25 -07001631 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001632 Log.e(TAG, "Error loading shortcut " + item
1633 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001634 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001635 + ")");
1636 return false;
1637 }
1638
Dan Sandler295ae182013-12-10 16:05:47 -05001639 if (hotseatItems != null) {
1640 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001641 Log.e(TAG, "Error loading shortcut into hotseat " + item
1642 + " into position (" + item.screenId + ":" + item.cellX + ","
1643 + item.cellY + ") occupied by "
1644 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1645 [(int) item.screenId][0]);
1646 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001647 } else {
1648 hotseatItems[(int) item.screenId][0] = item;
1649 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001650 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001651 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001652 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001653 items[(int) item.screenId][0] = item;
1654 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001655 return true;
1656 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001657 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1658 if (!workspaceScreens.contains((Long) item.screenId)) {
1659 // The item has an invalid screen id.
1660 return false;
1661 }
1662 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001663 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001664 return true;
1665 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001666
Adam Cohendcd297f2013-06-18 13:13:40 -07001667 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001668 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001669 occupied.put(item.screenId, items);
1670 }
1671
Dan Sandler295ae182013-12-10 16:05:47 -05001672 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001673 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1674 item.cellX < 0 || item.cellY < 0 ||
1675 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1676 Log.e(TAG, "Error loading shortcut " + item
1677 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1678 + item.cellX + "," + item.cellY
1679 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1680 return false;
1681 }
1682
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001683 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001684 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1685 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001686 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001687 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001688 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001689 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001690 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001691 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001692 return false;
1693 }
1694 }
1695 }
1696 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1697 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001698 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001699 }
1700 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001701
Joe Onorato36115782010-06-17 13:28:48 -04001702 return true;
1703 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704
Winson Chungba9c37f2013-08-30 14:11:37 -07001705 /** Clears all the sBg data structures */
1706 private void clearSBgDataStructures() {
1707 synchronized (sBgLock) {
1708 sBgWorkspaceItems.clear();
1709 sBgAppWidgets.clear();
1710 sBgFolders.clear();
1711 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001712 sBgWorkspaceScreens.clear();
1713 }
1714 }
1715
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001716 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001717 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718
Joe Onorato36115782010-06-17 13:28:48 -04001719 final Context context = mContext;
1720 final ContentResolver contentResolver = context.getContentResolver();
1721 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001722 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001723 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001724 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001725
Winson Chung892c74d2013-08-22 16:15:50 -07001726 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001727 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001728 int countX = profile.numColumns;
1729 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001730
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001731 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001732 long migrationStartTime = System.currentTimeMillis();
1733 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001734 try {
1735 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1736 // Clear the flags before starting the task, so that we do not run the task
1737 // again, in case there was an uncaught error.
1738 MigrateFromRestoreTask.clearFlags(mContext);
1739 task.execute();
1740 } catch (Exception e) {
1741 Log.e(TAG, "Error during grid migration", e);
1742
1743 // Clear workspace.
1744 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1745 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001746 Log.v(TAG, "Workspace migration completed in "
1747 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001748 }
1749
Dan Sandlerd5024042014-01-09 15:01:33 -05001750 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1751 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1752 LauncherAppState.getLauncherProvider().deleteDatabase();
1753 }
1754
1755 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1756 // append the user's Launcher2 shortcuts
1757 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1758 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1759 } else {
1760 // Make sure the default workspace is loaded
1761 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001762 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001763 }
Adam Cohene25af792013-06-06 23:08:25 -07001764
Winson Chung2abf94d2012-07-18 18:16:38 -07001765 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001766 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001767 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001768 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001769 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001770
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001772 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001773 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001774 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001775 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001776
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 // +1 for the hotseat (it can be larger than the workspace)
1778 // Load workspace in reverse order to ensure that latest items are loaded first (and
1779 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001780 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001782 try {
1783 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1784 final int intentIndex = c.getColumnIndexOrThrow
1785 (LauncherSettings.Favorites.INTENT);
1786 final int titleIndex = c.getColumnIndexOrThrow
1787 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 final int containerIndex = c.getColumnIndexOrThrow(
1789 LauncherSettings.Favorites.CONTAINER);
1790 final int itemTypeIndex = c.getColumnIndexOrThrow(
1791 LauncherSettings.Favorites.ITEM_TYPE);
1792 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1793 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001794 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1795 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001796 final int screenIndex = c.getColumnIndexOrThrow(
1797 LauncherSettings.Favorites.SCREEN);
1798 final int cellXIndex = c.getColumnIndexOrThrow
1799 (LauncherSettings.Favorites.CELLX);
1800 final int cellYIndex = c.getColumnIndexOrThrow
1801 (LauncherSettings.Favorites.CELLY);
1802 final int spanXIndex = c.getColumnIndexOrThrow
1803 (LauncherSettings.Favorites.SPANX);
1804 final int spanYIndex = c.getColumnIndexOrThrow(
1805 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001806 final int rankIndex = c.getColumnIndexOrThrow(
1807 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001808 final int restoredIndex = c.getColumnIndexOrThrow(
1809 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001810 final int profileIdIndex = c.getColumnIndexOrThrow(
1811 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001812 final int optionsIndex = c.getColumnIndexOrThrow(
1813 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001814 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815
Sunny Goyal7f834d22015-04-21 10:10:23 -07001816 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1817 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1818 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1819 }
1820
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001821 ShortcutInfo info;
1822 String intentDescription;
1823 LauncherAppWidgetInfo appWidgetInfo;
1824 int container;
1825 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001826 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001828 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 while (!mStopped && c.moveToNext()) {
1831 try {
1832 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001833 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001834 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001835 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 switch (itemType) {
1838 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1839 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001840 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001842 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001843 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001844 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001845 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001846 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001847 if (user == null) {
1848 // User has been deleted remove the item.
1849 itemsToRemove.add(id);
1850 continue;
1851 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001852 try {
1853 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001854 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001855 if (cn != null && cn.getPackageName() != null) {
1856 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1857 cn.getPackageName(), user);
1858 boolean validComponent = validPkg &&
1859 launcherApps.isActivityEnabledForProfile(cn, user);
1860
1861 if (validComponent) {
1862 if (restored) {
1863 // no special handling necessary for this item
1864 restoredRows.add(id);
1865 restored = false;
1866 }
1867 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001868 intent = null;
1869 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1870 // We allow auto install apps to have their intent
1871 // updated after an install.
1872 intent = manager.getLaunchIntentForPackage(
1873 cn.getPackageName());
1874 if (intent != null) {
1875 ContentValues values = new ContentValues();
1876 values.put(LauncherSettings.Favorites.INTENT,
1877 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001878 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001879 }
1880 }
1881
1882 if (intent == null) {
1883 // The app is installed but the component is no
1884 // longer available.
1885 Launcher.addDumpLog(TAG,
1886 "Invalid component removed: " + cn, true);
1887 itemsToRemove.add(id);
1888 continue;
1889 } else {
1890 // no special handling necessary for this item
1891 restoredRows.add(id);
1892 restored = false;
1893 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001894 } else if (restored) {
1895 // Package is not yet available but might be
1896 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001897 Launcher.addDumpLog(TAG,
1898 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001899
1900 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1901 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001902 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001903 // App restore has started. Update the flag
1904 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1905 ContentValues values = new ContentValues();
1906 values.put(LauncherSettings.Favorites.RESTORED,
1907 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001908 updateItem(id, values);
1909 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1910 // This is a common app. Try to replace this.
1911 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1912 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1913 if (parser.findDefaultApp()) {
1914 // Default app found. Replace it.
1915 intent = parser.parsedIntent;
1916 cn = intent.getComponent();
1917 ContentValues values = parser.parsedValues;
1918 values.put(LauncherSettings.Favorites.RESTORED, 0);
1919 updateItem(id, values);
1920 restored = false;
1921 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001922
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001923 } else if (REMOVE_UNRESTORED_ICONS) {
1924 Launcher.addDumpLog(TAG,
1925 "Unrestored package removed: " + cn, true);
1926 itemsToRemove.add(id);
1927 continue;
1928 }
Sunny Goyal94485362014-09-18 16:13:58 -07001929 } else if (REMOVE_UNRESTORED_ICONS) {
1930 Launcher.addDumpLog(TAG,
1931 "Unrestored package removed: " + cn, true);
1932 itemsToRemove.add(id);
1933 continue;
1934 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001935 } else if (launcherApps.isAppEnabled(
1936 manager, cn.getPackageName(),
1937 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1938 // Package is present but not available.
1939 allowMissingTarget = true;
1940 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1941 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001942 // SdCard is not ready yet. Package might get available,
1943 // once it is ready.
1944 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1945 + " (check again later)", true);
1946 HashSet<String> pkgs = sPendingPackages.get(user);
1947 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001948 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001949 sPendingPackages.put(user, pkgs);
1950 }
1951 pkgs.add(cn.getPackageName());
1952 allowMissingTarget = true;
1953 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001954
1955 } else {
1956 // Do not wait for external media load anymore.
1957 // Log the invalid package, and remove it
1958 Launcher.addDumpLog(TAG,
1959 "Invalid package removed: " + cn, true);
1960 itemsToRemove.add(id);
1961 continue;
Winson Chungee055712013-07-30 14:46:24 -07001962 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001963 } else if (cn == null) {
1964 // For shortcuts with no component, keep them as they are
1965 restoredRows.add(id);
1966 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001967 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001969 Launcher.addDumpLog(TAG,
1970 "Invalid uri: " + intentDescription, true);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001971 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 continue;
1973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001974
Sunny Goyal34b65272015-03-11 16:56:52 -07001975 boolean useLowResIcon = container >= 0 &&
1976 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1977
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001978 if (itemReplaced) {
1979 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001980 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001981 cursorIconInfo.iconIndex, titleIndex,
1982 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001983 } else {
1984 // Don't replace items for other profiles.
1985 itemsToRemove.add(id);
1986 continue;
1987 }
1988 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001989 if (user.equals(UserHandleCompat.myUserHandle())) {
1990 Launcher.addDumpLog(TAG,
1991 "constructing info for partially restored package",
1992 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07001993 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001994 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001995 intent = getRestoredItemIntent(c, context, intent);
1996 } else {
1997 // Don't restore items for other profiles.
1998 itemsToRemove.add(id);
1999 continue;
2000 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002001 } else if (itemType ==
2002 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002003 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002004 cursorIconInfo.iconIndex, titleIndex,
2005 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002007 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 // App shortcuts that used to be automatically added to Launcher
2010 // didn't always have the correct intent flags set, so do that
2011 // here
2012 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002013 intent.getCategories() != null &&
2014 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002015 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 intent.addFlags(
2017 Intent.FLAG_ACTIVITY_NEW_TASK |
2018 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2019 }
Michael Jurka96879562012-03-22 05:54:33 -07002020 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002021
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002023 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 info.cellX = c.getInt(cellXIndex);
2028 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002029 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002030 info.spanX = 1;
2031 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002032 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002033 if (info.promisedIntent != null) {
2034 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2035 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002036 info.isDisabled = disabledState;
2037 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2038 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2039 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002040
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002042 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002043 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 break;
2045 }
2046
Sunny Goyal756adbc2015-04-16 15:20:51 -07002047 if (restored) {
2048 ComponentName cn = info.getTargetComponent();
2049 if (cn != null) {
2050 Integer progress = installingPkgs.get(cn.getPackageName());
2051 if (progress != null) {
2052 info.setInstallProgress(progress);
2053 } else {
2054 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2055 }
2056 }
2057 }
2058
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 switch (container) {
2060 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2061 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2062 sBgWorkspaceItems.add(info);
2063 break;
2064 default:
2065 // Item is in a user folder
2066 FolderInfo folderInfo =
2067 findOrMakeFolder(sBgFolders, container);
2068 folderInfo.add(info);
2069 break;
2070 }
2071 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002072 } else {
2073 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 }
2075 break;
2076
2077 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2078 id = c.getLong(idIndex);
2079 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2080
Sunny Goyala508e4f2015-05-21 09:33:57 -07002081 // Do not trim the folder label, as is was set by the user.
2082 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002085 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 folderInfo.cellX = c.getInt(cellXIndex);
2087 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002088 folderInfo.spanX = 1;
2089 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002090 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002091
Daniel Sandler8802e962010-05-26 16:28:16 -04002092 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002093 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002094 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002095 break;
2096 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002097
Joe Onorato9c1289c2009-08-17 11:03:03 -04002098 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2100 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2101 sBgWorkspaceItems.add(folderInfo);
2102 break;
Joe Onorato36115782010-06-17 13:28:48 -04002103 }
Joe Onorato17a89222011-02-08 17:26:11 -08002104
Chris Wrenf4d08112014-01-16 18:13:56 -05002105 if (restored) {
2106 // no special handling required for restored folders
2107 restoredRows.add(id);
2108 }
2109
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2111 sBgFolders.put(folderInfo.id, folderInfo);
2112 break;
2113
2114 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002115 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002117 boolean customWidget = itemType ==
2118 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2119
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002121 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002122 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002123 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002124 user = allUsers.get(serialNumber);
2125 if (user == null) {
2126 itemsToRemove.add(id);
2127 continue;
2128 }
2129
Sunny Goyalff572272014-07-23 13:58:07 -07002130 final ComponentName component =
2131 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002132
Sunny Goyal651077b2014-06-30 14:15:31 -07002133 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002134 final boolean isIdValid = (restoreStatus &
2135 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002136 final boolean wasProviderReady = (restoreStatus &
2137 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002138
Adam Cohen59400422014-03-05 18:07:04 -08002139 final LauncherAppWidgetProviderInfo provider =
2140 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002141 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002142 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002143
2144 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002145 if (!isSafeMode && !customWidget &&
2146 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002147 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002148 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002149
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002151 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 itemsToRemove.add(id);
2153 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002154 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002155 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2156 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002157
Sunny Goyal53f96722015-07-13 19:54:53 -07002158 // The provider is available. So the widget is either
2159 // available or not available. We do not need to track
2160 // any future restore updates.
2161 int status = restoreStatus &
2162 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002163 if (!wasProviderReady) {
2164 // If provider was not previously ready, update the
2165 // status and UI flag.
2166
2167 // Id would be valid only if the widget restore broadcast was received.
2168 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002169 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002170 } else {
2171 status &= ~LauncherAppWidgetInfo
2172 .FLAG_PROVIDER_NOT_READY;
2173 }
2174 }
2175 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002176 } else {
2177 Log.v(TAG, "Widget restore pending id=" + id
2178 + " appWidgetId=" + appWidgetId
2179 + " status =" + restoreStatus);
2180 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002181 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002182 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002183 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002184
2185 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2186 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002187 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002188 // App restore has started. Update the flag
2189 appWidgetInfo.restoreStatus |=
2190 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002191 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002192 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002193 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002194 itemsToRemove.add(id);
2195 continue;
2196 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002197
2198 appWidgetInfo.installProgress =
2199 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002200 }
Sunny Goyalff572272014-07-23 13:58:07 -07002201
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 appWidgetInfo.cellX = c.getInt(cellXIndex);
2205 appWidgetInfo.cellY = c.getInt(cellYIndex);
2206 appWidgetInfo.spanX = c.getInt(spanXIndex);
2207 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002208 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002209
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2211 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2212 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002213 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2214 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 continue;
2216 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002218 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002220 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002221 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222 break;
2223 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002224
Adam Cohen59400422014-03-05 18:07:04 -08002225 if (!customWidget) {
2226 String providerName =
2227 appWidgetInfo.providerName.flattenToString();
2228 if (!providerName.equals(savedProvider) ||
2229 (appWidgetInfo.restoreStatus != restoreStatus)) {
2230 ContentValues values = new ContentValues();
2231 values.put(
2232 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2233 providerName);
2234 values.put(LauncherSettings.Favorites.RESTORED,
2235 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002236 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002237 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002238 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2240 sBgAppWidgets.add(appWidgetInfo);
2241 }
Joe Onorato36115782010-06-17 13:28:48 -04002242 break;
2243 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002245 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002246 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 }
2248 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002249 if (c != null) {
2250 c.close();
2251 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 }
2253
Winson Chungba9c37f2013-08-30 14:11:37 -07002254 // Break early if we've stopped loading
2255 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002256 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002257 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002258 }
2259
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002262 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2263 Utilities.createDbSelectionQuery(
2264 LauncherSettings.Favorites._ID, itemsToRemove), null);
2265 if (DEBUG_LOADERS) {
2266 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2267 LauncherSettings.Favorites._ID, itemsToRemove));
2268 }
2269
2270 // Remove any empty folder
2271 for (long folderId : LauncherAppState.getLauncherProvider()
2272 .deleteEmptyFolders()) {
2273 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2274 sBgFolders.remove(folderId);
2275 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002276 }
2277 }
2278
Sunny Goyal317698b2015-07-29 11:45:41 -07002279 // Sort all the folder items and make sure the first 3 items are high resolution.
2280 for (FolderInfo folder : sBgFolders) {
2281 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2282 int pos = 0;
2283 for (ShortcutInfo info : folder.contents) {
2284 if (info.usingLowResIcon) {
2285 info.updateIcon(mIconCache, false);
2286 }
2287 pos ++;
2288 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2289 break;
2290 }
2291 }
2292 }
2293
Chris Wrenf4d08112014-01-16 18:13:56 -05002294 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002295 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002296 ContentValues values = new ContentValues();
2297 values.put(LauncherSettings.Favorites.RESTORED, 0);
2298 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2299 Utilities.createDbSelectionQuery(
2300 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002301 }
2302
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002303 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2304 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002305 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002306 null, sWorker);
2307 }
2308
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002309 // Remove any empty screens
2310 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002311 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002312 long screenId = item.screenId;
2313 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2314 unusedScreens.contains(screenId)) {
2315 unusedScreens.remove(screenId);
2316 }
2317 }
2318
2319 // If there are any empty screens remove them, and update.
2320 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002321 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 }
2324
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325 if (DEBUG_LOADERS) {
2326 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2327 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002329 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002331
Sunny Goyale2df0622015-04-24 11:27:00 -07002332 for (int i = 0; i < nScreens; i++) {
2333 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002334 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 line += " | ";
2336 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002337 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002338 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002339 if (x < screen.length && y < screen[x].length) {
2340 line += (screen[x][y] != null) ? "#" : ".";
2341 } else {
2342 line += "!";
2343 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 }
Joe Onorato36115782010-06-17 13:28:48 -04002345 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002346 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002347 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002348 }
Joe Onorato36115782010-06-17 13:28:48 -04002349 }
Adam Cohene25af792013-06-06 23:08:25 -07002350 }
2351
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002352 /**
2353 * Partially updates the item without any notification. Must be called on the worker thread.
2354 */
2355 private void updateItem(long itemId, ContentValues update) {
2356 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002357 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002358 update,
2359 BaseColumns._ID + "= ?",
2360 new String[]{Long.toString(itemId)});
2361 }
2362
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002363 /** Filters the set of items who are directly or indirectly (via another container) on the
2364 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002365 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 ArrayList<ItemInfo> allWorkspaceItems,
2367 ArrayList<ItemInfo> currentScreenItems,
2368 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002369 // Purge any null ItemInfos
2370 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2371 while (iter.hasNext()) {
2372 ItemInfo i = iter.next();
2373 if (i == null) {
2374 iter.remove();
2375 }
2376 }
2377
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 // Order the set of items by their containers first, this allows use to walk through the
2379 // list sequentially, build up a list of containers that are in the specified screen,
2380 // as well as all items in those containers.
2381 Set<Long> itemsOnScreen = new HashSet<Long>();
2382 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2383 @Override
2384 public int compare(ItemInfo lhs, ItemInfo rhs) {
2385 return (int) (lhs.container - rhs.container);
2386 }
2387 });
2388 for (ItemInfo info : allWorkspaceItems) {
2389 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002390 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 currentScreenItems.add(info);
2392 itemsOnScreen.add(info.id);
2393 } else {
2394 otherScreenItems.add(info);
2395 }
2396 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2397 currentScreenItems.add(info);
2398 itemsOnScreen.add(info.id);
2399 } else {
2400 if (itemsOnScreen.contains(info.container)) {
2401 currentScreenItems.add(info);
2402 itemsOnScreen.add(info.id);
2403 } else {
2404 otherScreenItems.add(info);
2405 }
2406 }
2407 }
2408 }
2409
2410 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002411 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412 ArrayList<LauncherAppWidgetInfo> appWidgets,
2413 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2414 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415
2416 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002417 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002419 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002420 currentScreenWidgets.add(widget);
2421 } else {
2422 otherScreenWidgets.add(widget);
2423 }
2424 }
2425 }
2426
2427 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002428 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002429 LongArrayMap<ItemInfo> itemsIdMap,
2430 LongArrayMap<FolderInfo> folders,
2431 LongArrayMap<FolderInfo> currentScreenFolders,
2432 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433
Sunny Goyale2df0622015-04-24 11:27:00 -07002434 int total = folders.size();
2435 for (int i = 0; i < total; i++) {
2436 long id = folders.keyAt(i);
2437 FolderInfo folder = folders.valueAt(i);
2438
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002440 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002442 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 currentScreenFolders.put(id, folder);
2444 } else {
2445 otherScreenFolders.put(id, folder);
2446 }
2447 }
2448 }
2449
2450 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2451 * right) */
2452 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002453 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002454 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002455 final int screenCols = profile.numColumns;
2456 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002458 @Override
2459 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002460 if (lhs.container == rhs.container) {
2461 // Within containers, order by their spatial position in that container
2462 switch ((int) lhs.container) {
2463 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2464 long lr = (lhs.screenId * screenCellCount +
2465 lhs.cellY * screenCols + lhs.cellX);
2466 long rr = (rhs.screenId * screenCellCount +
2467 rhs.cellY * screenCols + rhs.cellX);
2468 return (int) (lr - rr);
2469 }
2470 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2471 // We currently use the screen id as the rank
2472 return (int) (lhs.screenId - rhs.screenId);
2473 }
2474 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002475 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002476 throw new RuntimeException("Unexpected container type when " +
2477 "sorting workspace items.");
2478 }
2479 return 0;
2480 }
2481 } else {
2482 // Between containers, order by hotseat, desktop
2483 return (int) (lhs.container - rhs.container);
2484 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002485 }
2486 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002488
Adam Cohendcd297f2013-06-18 13:13:40 -07002489 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2490 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002491 final Runnable r = new Runnable() {
2492 @Override
2493 public void run() {
2494 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2495 if (callbacks != null) {
2496 callbacks.bindScreens(orderedScreens);
2497 }
2498 }
2499 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002500 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002501 }
2502
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002503 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2504 final ArrayList<ItemInfo> workspaceItems,
2505 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002506 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002508
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 final boolean postOnMainThread = (deferredBindRunnables != null);
2510
2511 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002512 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002514 final int start = i;
2515 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002516 final Runnable r = new Runnable() {
2517 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002519 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002521 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2522 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002523 }
2524 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 };
2526 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002527 synchronized (deferredBindRunnables) {
2528 deferredBindRunnables.add(r);
2529 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002531 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002532 }
Joe Onorato36115782010-06-17 13:28:48 -04002533 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534
2535 // Bind the folders
2536 if (!folders.isEmpty()) {
2537 final Runnable r = new Runnable() {
2538 public void run() {
2539 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2540 if (callbacks != null) {
2541 callbacks.bindFolders(folders);
2542 }
2543 }
2544 };
2545 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002546 synchronized (deferredBindRunnables) {
2547 deferredBindRunnables.add(r);
2548 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002549 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002550 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551 }
2552 }
2553
2554 // Bind the widgets, one at a time
2555 N = appWidgets.size();
2556 for (int i = 0; i < N; i++) {
2557 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2558 final Runnable r = new Runnable() {
2559 public void run() {
2560 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2561 if (callbacks != null) {
2562 callbacks.bindAppWidget(widget);
2563 }
2564 }
2565 };
2566 if (postOnMainThread) {
2567 deferredBindRunnables.add(r);
2568 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002569 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002570 }
2571 }
2572 }
2573
2574 /**
2575 * Binds all loaded data to actual views on the main thread.
2576 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002577 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002578 final long t = SystemClock.uptimeMillis();
2579 Runnable r;
2580
2581 // Don't use these two variables in any of the callback runnables.
2582 // Otherwise we hold a reference to them.
2583 final Callbacks oldCallbacks = mCallbacks.get();
2584 if (oldCallbacks == null) {
2585 // This launcher has exited and nobody bothered to tell us. Just bail.
2586 Log.w(TAG, "LoaderTask running with no launcher");
2587 return;
2588 }
2589
Winson Chung9b9fb962013-11-15 15:39:34 -08002590 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002591 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2592 ArrayList<LauncherAppWidgetInfo> appWidgets =
2593 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002594 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002595
2596 final LongArrayMap<FolderInfo> folders;
2597 final LongArrayMap<ItemInfo> itemsIdMap;
2598
Winson Chung2abf94d2012-07-18 18:16:38 -07002599 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002600 workspaceItems.addAll(sBgWorkspaceItems);
2601 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002602 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002603
2604 folders = sBgFolders.clone();
2605 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002606 }
2607
Derek Prothro7aff3992013-12-10 14:00:37 -05002608 final boolean isLoadingSynchronously =
2609 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002610 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002611 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002612 if (currScreen >= orderedScreenIds.size()) {
2613 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002614 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002615 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002616 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002617 final long currentScreenId = currentScreen < 0
2618 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002619
2620 // Load all the items that are on the current page first (and in the process, unbind
2621 // all the existing workspace items before we call startBinding() below.
2622 unbindWorkspaceItemsOnMainThread();
2623
2624 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2626 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2627 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2628 new ArrayList<LauncherAppWidgetInfo>();
2629 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2630 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002631 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2632 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633
Winson Chung9b9fb962013-11-15 15:39:34 -08002634 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002636 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002637 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002638 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002639 otherFolders);
2640 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2641 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2642
2643 // Tell the workspace that we're about to start binding items
2644 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002645 public void run() {
2646 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2647 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002649 }
2650 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002651 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002652 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002653
Adam Cohendcd297f2013-06-18 13:13:40 -07002654 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2655
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002656 // Load items on the current page
2657 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2658 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002659 if (isLoadingSynchronously) {
2660 r = new Runnable() {
2661 public void run() {
2662 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002663 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002664 callbacks.onPageBoundSynchronously(currentScreen);
2665 }
2666 }
2667 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002668 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002669 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002670
Winson Chung4a2afa32012-07-19 14:53:05 -07002671 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2672 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002673 synchronized (mDeferredBindRunnables) {
2674 mDeferredBindRunnables.clear();
2675 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002676 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002677 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678
2679 // Tell the workspace that we're done binding items
2680 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002681 public void run() {
2682 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2683 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002684 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002685 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002686
Sunny Goyal639e9062015-08-19 19:17:06 -07002687 mIsLoadingAndBindingWorkspace = false;
2688
2689 // Run all the bind complete runnables after workspace is bound.
2690 if (!mBindCompleteRunnables.isEmpty()) {
2691 synchronized (mBindCompleteRunnables) {
2692 for (final Runnable r : mBindCompleteRunnables) {
2693 runOnWorkerThread(r);
2694 }
2695 mBindCompleteRunnables.clear();
2696 }
2697 }
2698
Winson Chung98e030b2012-05-07 16:01:11 -07002699 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002700 if (DEBUG_LOADERS) {
2701 Log.d(TAG, "bound workspace in "
2702 + (SystemClock.uptimeMillis()-t) + "ms");
2703 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002704
Joe Onorato36115782010-06-17 13:28:48 -04002705 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002706 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002707 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002708 synchronized (mDeferredBindRunnables) {
2709 mDeferredBindRunnables.add(r);
2710 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002711 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002712 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002713 }
Joe Onorato36115782010-06-17 13:28:48 -04002714 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002715
Joe Onorato36115782010-06-17 13:28:48 -04002716 private void loadAndBindAllApps() {
2717 if (DEBUG_LOADERS) {
2718 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2719 }
2720 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002721 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002722 synchronized (LoaderTask.this) {
2723 if (mStopped) {
2724 return;
2725 }
2726 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002727 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002728 synchronized (LoaderTask.this) {
2729 if (mStopped) {
2730 return;
2731 }
2732 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002733 }
Joe Onorato36115782010-06-17 13:28:48 -04002734 } else {
2735 onlyBindAllApps();
2736 }
2737 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002738
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002739 private void updateIconCache() {
2740 // Ignore packages which have a promise icon.
2741 HashSet<String> packagesToIgnore = new HashSet<>();
2742 synchronized (sBgLock) {
2743 for (ItemInfo info : sBgItemsIdMap) {
2744 if (info instanceof ShortcutInfo) {
2745 ShortcutInfo si = (ShortcutInfo) info;
2746 if (si.isPromise() && si.getTargetComponent() != null) {
2747 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2748 }
2749 } else if (info instanceof LauncherAppWidgetInfo) {
2750 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2751 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2752 packagesToIgnore.add(lawi.providerName.getPackageName());
2753 }
2754 }
2755 }
2756 }
2757 mIconCache.updateDbIcons(packagesToIgnore);
2758 }
2759
Joe Onorato36115782010-06-17 13:28:48 -04002760 private void onlyBindAllApps() {
2761 final Callbacks oldCallbacks = mCallbacks.get();
2762 if (oldCallbacks == null) {
2763 // This launcher has exited and nobody bothered to tell us. Just bail.
2764 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2765 return;
2766 }
2767
2768 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002769 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002770 final ArrayList<AppInfo> list
2771 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002772 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002773 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002774 public void run() {
2775 final long t = SystemClock.uptimeMillis();
2776 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2777 if (callbacks != null) {
2778 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002779 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002780 }
2781 if (DEBUG_LOADERS) {
2782 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2783 + (SystemClock.uptimeMillis()-t) + "ms");
2784 }
2785 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002786 };
2787 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002788 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002789 r.run();
2790 } else {
2791 mHandler.post(r);
2792 }
Joe Onorato36115782010-06-17 13:28:48 -04002793 }
2794
Winson Chung64359a52013-07-08 17:17:08 -07002795 private void loadAllApps() {
2796 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002797
Joe Onorato36115782010-06-17 13:28:48 -04002798 final Callbacks oldCallbacks = mCallbacks.get();
2799 if (oldCallbacks == null) {
2800 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002801 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002802 return;
2803 }
2804
Kenny Guyed131872014-04-30 03:02:21 +01002805 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2806
Winson Chung64359a52013-07-08 17:17:08 -07002807 // Clear the list of apps
2808 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002809 for (UserHandleCompat user : profiles) {
2810 // Query for the set of apps
2811 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002812 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002813 if (DEBUG_LOADERS) {
2814 Log.d(TAG, "getActivityList took "
2815 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2816 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2817 }
2818 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002819 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002820 if (apps == null || apps.isEmpty()) {
2821 return;
2822 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002823
Kenny Guyed131872014-04-30 03:02:21 +01002824 // Create the ApplicationInfos
2825 for (int i = 0; i < apps.size(); i++) {
2826 LauncherActivityInfoCompat app = apps.get(i);
2827 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002828 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002829 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002830
Sunny Goyal756a28a2015-04-23 17:07:55 -07002831 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2832 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002833 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002834
2835 @Override
2836 public void run() {
2837 heuristic.processUserApps(apps);
2838 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002839 };
2840 runOnMainThread(new Runnable() {
2841
2842 @Override
2843 public void run() {
2844 // Check isLoadingWorkspace on the UI thread, as it is updated on
2845 // the UI thread.
2846 if (mIsLoadingAndBindingWorkspace) {
2847 synchronized (mBindCompleteRunnables) {
2848 mBindCompleteRunnables.add(r);
2849 }
2850 } else {
2851 runOnWorkerThread(r);
2852 }
2853 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002854 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002855 }
Winson Chung64359a52013-07-08 17:17:08 -07002856 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002857 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002858 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2859 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002860
2861 // Post callback on main thread
2862 mHandler.post(new Runnable() {
2863 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002864
Winson Chung64359a52013-07-08 17:17:08 -07002865 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002866 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002867 if (callbacks != null) {
2868 callbacks.bindAllApplications(added);
2869 if (DEBUG_LOADERS) {
2870 Log.d(TAG, "bound " + added.size() + " apps in "
2871 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2872 }
2873 } else {
2874 Log.i(TAG, "not binding apps: no Launcher activity");
2875 }
2876 }
2877 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002878 // Cleanup any data stored for a deleted user.
2879 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002880
Sunny Goyal2d648b02015-08-11 13:56:28 -07002881 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002882 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002883 Log.d(TAG, "Icons processed in "
2884 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002885 }
2886 }
2887
2888 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002889 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002890 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002891 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2892 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2893 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2894 }
Joe Onorato36115782010-06-17 13:28:48 -04002895 }
2896 }
2897
Sunny Goyal75b0f552015-05-20 21:57:06 -07002898 /**
2899 * Called when the icons for packages have been updated in the icon cache.
2900 */
2901 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2902 final Callbacks callbacks = getCallback();
2903 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2904 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2905
2906 // If any package icon has changed (app was updated while launcher was dead),
2907 // update the corresponding shortcuts.
2908 synchronized (sBgLock) {
2909 for (ItemInfo info : sBgItemsIdMap) {
2910 if (info instanceof ShortcutInfo && user.equals(info.user)
2911 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2912 ShortcutInfo si = (ShortcutInfo) info;
2913 ComponentName cn = si.getTargetComponent();
2914 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2915 si.updateIcon(mIconCache);
2916 updatedShortcuts.add(si);
2917 }
2918 }
2919 }
2920 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2921 }
2922
2923 if (!updatedShortcuts.isEmpty()) {
2924 final UserHandleCompat userFinal = user;
2925 mHandler.post(new Runnable() {
2926
2927 public void run() {
2928 Callbacks cb = getCallback();
2929 if (cb != null && callbacks == cb) {
2930 cb.bindShortcutsChanged(updatedShortcuts,
2931 new ArrayList<ShortcutInfo>(), userFinal);
2932 }
2933 }
2934 });
2935 }
2936
2937 if (!updatedApps.isEmpty()) {
2938 mHandler.post(new Runnable() {
2939
2940 public void run() {
2941 Callbacks cb = getCallback();
2942 if (cb != null && callbacks == cb) {
2943 cb.bindAppsUpdated(updatedApps);
2944 }
2945 }
2946 });
2947 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002948
2949 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002950 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002951 }
2952
Joe Onorato36115782010-06-17 13:28:48 -04002953 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002954 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002955 }
2956
Adam Cohen091440a2015-03-18 14:16:05 -07002957 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002958
2959 @Override
2960 public void onReceive(Context context, Intent intent) {
2961 synchronized (sBgLock) {
2962 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2963 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002964 final PackageManager manager = context.getPackageManager();
2965 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2966 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002967 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2968 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002969 packagesRemoved.clear();
2970 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002971 for (String pkg : entry.getValue()) {
2972 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002973 boolean packageOnSdcard = launcherApps.isAppEnabled(
2974 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2975 if (packageOnSdcard) {
2976 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2977 packagesUnavailable.add(pkg);
2978 } else {
2979 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2980 packagesRemoved.add(pkg);
2981 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002982 }
2983 }
2984 if (!packagesRemoved.isEmpty()) {
2985 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2986 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2987 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002988 if (!packagesUnavailable.isEmpty()) {
2989 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2990 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2991 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002992 }
Sunny Goyal34942622014-08-29 17:20:55 -07002993 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002994 }
2995 }
2996 }
2997
Joe Onorato36115782010-06-17 13:28:48 -04002998 private class PackageUpdatedTask implements Runnable {
2999 int mOp;
3000 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003001 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003002
3003 public static final int OP_NONE = 0;
3004 public static final int OP_ADD = 1;
3005 public static final int OP_UPDATE = 2;
3006 public static final int OP_REMOVE = 3; // uninstlled
3007 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3008
3009
Kenny Guyed131872014-04-30 03:02:21 +01003010 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003011 mOp = op;
3012 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003013 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003014 }
3015
3016 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003017 if (!mHasLoaderCompletedOnce) {
3018 // Loader has not yet run.
3019 return;
3020 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003021 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003022
3023 final String[] packages = mPackages;
3024 final int N = packages.length;
3025 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003026 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003027 for (int i=0; i<N; i++) {
3028 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003029 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003030 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003031 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003032
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003033 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3034 if (heuristic != null) {
3035 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003036 }
Joe Onorato36115782010-06-17 13:28:48 -04003037 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003038 }
Joe Onorato36115782010-06-17 13:28:48 -04003039 case OP_UPDATE:
3040 for (int i=0; i<N; i++) {
3041 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003042 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003043 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003044 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003045 }
3046 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003047 case OP_REMOVE: {
3048 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3049 if (heuristic != null) {
3050 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003051 }
Joe Onorato36115782010-06-17 13:28:48 -04003052 for (int i=0; i<N; i++) {
3053 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003054 mIconCache.removeIconsForPkg(packages[i], mUser);
3055 }
3056 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003057 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003058 case OP_UNAVAILABLE:
3059 for (int i=0; i<N; i++) {
3060 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3061 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003062 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003063 }
3064 break;
3065 }
3066
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003067 ArrayList<AppInfo> added = null;
3068 ArrayList<AppInfo> modified = null;
3069 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003070
Adam Cohen487f7dd2012-06-28 18:12:10 -07003071 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003072 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003073 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003074 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003075 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003076 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003077 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003078 }
Winson Chung5d55f332012-07-16 20:45:03 -07003079 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003080 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003081 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003082 }
3083
Sunny Goyale0f58d72014-11-10 18:05:31 -08003084 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003085 if (callbacks == null) {
3086 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3087 return;
3088 }
3089
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3091 new HashMap<ComponentName, AppInfo>();
3092
Joe Onorato36115782010-06-17 13:28:48 -04003093 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003094 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095 for (AppInfo ai : added) {
3096 addedOrUpdatedApps.put(ai.componentName, ai);
3097 }
Joe Onorato36115782010-06-17 13:28:48 -04003098 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003099
Joe Onorato36115782010-06-17 13:28:48 -04003100 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003101 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003102 for (AppInfo ai : modified) {
3103 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003104 }
3105
Joe Onorato36115782010-06-17 13:28:48 -04003106 mHandler.post(new Runnable() {
3107 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003108 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003109 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003110 callbacks.bindAppsUpdated(modifiedFinal);
3111 }
3112 }
3113 });
3114 }
Winson Chung83892cc2013-05-01 16:53:33 -07003115
Sunny Goyal4390ace2014-10-13 11:33:11 -07003116 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003117 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3119 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3120 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3121
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003122 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003124 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003125 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3126 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003127 boolean infoUpdated = false;
3128 boolean shortcutUpdated = false;
3129
3130 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003131 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003132 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003133 Bitmap icon = Utilities.createIconBitmap(
3134 si.iconResource.packageName,
3135 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003136 if (icon != null) {
3137 si.setIcon(icon);
3138 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003140 }
3141 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003142
3143 ComponentName cn = si.getTargetComponent();
3144 if (cn != null && packageSet.contains(cn.getPackageName())) {
3145 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3146
3147 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003148 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3149 // Auto install icon
3150 PackageManager pm = context.getPackageManager();
3151 ResolveInfo matched = pm.resolveActivity(
3152 new Intent(Intent.ACTION_MAIN)
3153 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3154 PackageManager.MATCH_DEFAULT_ONLY);
3155 if (matched == null) {
3156 // Try to find the best match activity.
3157 Intent intent = pm.getLaunchIntentForPackage(
3158 cn.getPackageName());
3159 if (intent != null) {
3160 cn = intent.getComponent();
3161 appInfo = addedOrUpdatedApps.get(cn);
3162 }
3163
3164 if ((intent == null) || (appInfo == null)) {
3165 removedShortcuts.add(si);
3166 continue;
3167 }
3168 si.promisedIntent = intent;
3169 }
3170 }
3171
3172 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003173 if (appInfo != null) {
3174 si.flags = appInfo.flags;
3175 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003176
Sunny Goyal756adbc2015-04-16 15:20:51 -07003177 si.intent = si.promisedIntent;
3178 si.promisedIntent = null;
3179 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003180 infoUpdated = true;
3181 si.updateIcon(mIconCache);
3182 }
3183
3184 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3185 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3186 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003187 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003188 si.contentDescription = appInfo.contentDescription;
3189 infoUpdated = true;
3190 }
3191
3192 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3193 // Since package was just updated, the target must be available now.
3194 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3195 shortcutUpdated = true;
3196 }
3197 }
3198
3199 if (infoUpdated || shortcutUpdated) {
3200 updatedShortcuts.add(si);
3201 }
3202 if (infoUpdated) {
3203 updateItemInDatabase(context, si);
3204 }
3205 } else if (info instanceof LauncherAppWidgetInfo) {
3206 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3207 if (mUser.equals(widgetInfo.user)
3208 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3209 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003210 widgetInfo.restoreStatus &=
3211 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3212 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003213
3214 // adding this flag ensures that launcher shows 'click to setup'
3215 // if the widget has a config activity. In case there is no config
3216 // activity, it will be marked as 'restored' during bind.
3217 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3218
Sunny Goyal4390ace2014-10-13 11:33:11 -07003219 widgets.add(widgetInfo);
3220 updateItemInDatabase(context, widgetInfo);
3221 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003222 }
3223 }
3224 }
3225
Sunny Goyal4390ace2014-10-13 11:33:11 -07003226 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3227 mHandler.post(new Runnable() {
3228
3229 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003230 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003231 if (callbacks == cb && cb != null) {
3232 callbacks.bindShortcutsChanged(
3233 updatedShortcuts, removedShortcuts, mUser);
3234 }
3235 }
3236 });
3237 if (!removedShortcuts.isEmpty()) {
3238 deleteItemsFromDatabase(context, removedShortcuts);
3239 }
3240 }
3241 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003242 mHandler.post(new Runnable() {
3243 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003244 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003245 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003246 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003247 }
3248 }
3249 });
3250 }
3251 }
3252
Winson Chungdf95eb12013-10-16 14:57:07 -07003253 final ArrayList<String> removedPackageNames =
3254 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003255 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003256 // Mark all packages in the broadcast to be removed
3257 removedPackageNames.addAll(Arrays.asList(packages));
3258 } else if (mOp == OP_UPDATE) {
3259 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003260 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003261 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003262 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003263 }
3264 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003265 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003266
Winson Chungdf95eb12013-10-16 14:57:07 -07003267 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003268 final int removeReason;
3269 if (mOp == OP_UNAVAILABLE) {
3270 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3271 } else {
3272 // Remove all the components associated with this package
3273 for (String pn : removedPackageNames) {
3274 deletePackageFromDatabase(context, pn, mUser);
3275 }
3276 // Remove all the specific components
3277 for (AppInfo a : removedApps) {
3278 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3279 deleteItemsFromDatabase(context, infos);
3280 }
3281 removeReason = 0;
3282 }
3283
Winson Chungdf95eb12013-10-16 14:57:07 -07003284 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003285 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003286 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003287 mHandler.post(new Runnable() {
3288 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003289 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003290 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003291 callbacks.bindComponentsRemoved(
3292 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003293 }
3294 }
3295 });
Joe Onoratobe386092009-11-17 17:32:16 -08003296 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003297
Sunny Goyal2d648b02015-08-11 13:56:28 -07003298 // Update widgets
3299 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3300 // Always refresh for a package event on secondary user
3301 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3302
3303 // Refresh widget list, if the package already had a widget.
3304 synchronized (sBgLock) {
3305 if (sBgWidgetProviders != null) {
3306 HashSet<String> pkgSet = new HashSet<>();
3307 Collections.addAll(pkgSet, mPackages);
3308
3309 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3310 needToRefresh |= key.user.equals(mUser) &&
3311 pkgSet.contains(key.componentName.getPackageName());
3312 }
3313 }
3314 }
3315
3316 if (!needToRefresh && mOp != OP_REMOVE) {
3317 // Refresh widget list, if there is any newly added widget
3318 PackageManager pm = context.getPackageManager();
3319 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003320 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003321 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003322 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003323 .setPackage(pkg), 0);
3324 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003325 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003326 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003327 throw e;
3328 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003329 // Ignore the crash. We can live with a state widget list.
3330 Log.e(TAG, "PM call failed for " + pkg, e);
3331 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003332 }
3333 }
3334
3335 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3336 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003337
Adam Cohen4caf2982013-08-20 18:54:31 -07003338 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003339 mHandler.post(new Runnable() {
3340 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003341 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003342 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003343 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003344 }
3345 }
3346 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 }
3348 }
3349
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003350 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3351 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003352 ArrayList<LauncherAppWidgetProviderInfo> results =
3353 new ArrayList<LauncherAppWidgetProviderInfo>();
3354 try {
3355 synchronized (sBgLock) {
3356 if (sBgWidgetProviders == null || refresh) {
3357 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3358 = new HashMap<>();
3359 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3360 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003361
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003362 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3363 for (AppWidgetProviderInfo pInfo : widgets) {
3364 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3365 UserHandleCompat user = wm.getUser(info);
3366 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3367 }
Robin Lee26ace122015-03-16 19:41:43 +00003368
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003369 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3370 for (CustomAppWidget widget : customWidgets) {
3371 info = new LauncherAppWidgetProviderInfo(context, widget);
3372 UserHandleCompat user = wm.getUser(info);
3373 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3374 }
3375 // Replace the global list at the very end, so that if there is an exception,
3376 // previously loaded provider list is used.
3377 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003378 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003379 results.addAll(sBgWidgetProviders.values());
3380 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003381 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003382 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003383 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003384 (e.getCause() instanceof TransactionTooLargeException ||
3385 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003386 // the returned value may be incomplete and will not be refreshed until the next
3387 // time Launcher starts.
3388 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3389 // onResume is called to refresh the widget provider list.
3390 synchronized (sBgLock) {
3391 if (sBgWidgetProviders != null) {
3392 results.addAll(sBgWidgetProviders.values());
3393 }
3394 return results;
3395 }
3396 } else {
3397 throw e;
3398 }
Adam Cohen59400422014-03-05 18:07:04 -08003399 }
3400 }
3401
Robin Lee26ace122015-03-16 19:41:43 +00003402 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3403 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003404 synchronized (sBgLock) {
3405 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003406 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003407 }
Robin Lee26ace122015-03-16 19:41:43 +00003408 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003409 }
3410 }
3411
Sunny Goyal2d648b02015-08-11 13:56:28 -07003412 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003413
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003414 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003415 @Override
3416 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003417 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003418 final WidgetsModel model = mBgWidgetsModel.clone();
3419
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003420 mHandler.post(new Runnable() {
3421 @Override
3422 public void run() {
3423 Callbacks cb = getCallback();
3424 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003425 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003426 }
3427 }
3428 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003429 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003430 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3431 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003432 }
3433 });
3434 }
3435
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003436 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003437 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003438 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003439 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003440 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003441 @Thunk void updateWidgetsModel(boolean refresh) {
3442 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003443 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003444 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003445
3446 // Update shortcut providers
3447 synchronized (sBgLock) {
3448 try {
3449 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3450 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3451 sBgShortcutProviders = providers;
3452 } catch (RuntimeException e) {
3453 if (!LauncherAppState.isDogfoodBuild() &&
3454 (e.getCause() instanceof TransactionTooLargeException ||
3455 e.getCause() instanceof DeadObjectException)) {
3456 /**
3457 * Ignore exception and use the cached list if available.
3458 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3459 */
3460 } else {
3461 throw e;
3462 }
3463 }
3464 if (sBgShortcutProviders != null) {
3465 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3466 }
3467 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003468 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003469 }
3470
Adam Cohen091440a2015-03-18 14:16:05 -07003471 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003472 UserHandleCompat user) {
3473 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3474 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003475 }
Adam Cohen556f6132014-01-15 15:18:08 -08003476
Kenny Guyed131872014-04-30 03:02:21 +01003477 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3478 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003479 if (cn == null) {
3480 return false;
3481 }
Kenny Guyed131872014-04-30 03:02:21 +01003482 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3483 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003484 return false;
3485 }
Kenny Guyed131872014-04-30 03:02:21 +01003486 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003487 }
3488
Adam Cohena28b78e2014-05-20 17:03:04 -07003489 public static boolean isValidPackage(Context context, String packageName,
3490 UserHandleCompat user) {
3491 if (packageName == null) {
3492 return false;
3493 }
3494 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3495 return launcherApps.isPackageEnabledForProfile(packageName, user);
3496 }
3497
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003499 * Make an ShortcutInfo object for a restored application or shortcut item that points
3500 * to a package that is not yet installed on the system.
3501 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003502 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003503 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003504 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003505 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003506
3507 Bitmap icon = iconInfo.loadIcon(c, info, context);
3508 // the fallback icon
3509 if (icon == null) {
3510 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3511 } else {
3512 info.setIcon(icon);
3513 }
Sunny Goyal34942622014-08-29 17:20:55 -07003514
3515 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003516 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003517 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003518 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003519 }
Sunny Goyal34942622014-08-29 17:20:55 -07003520 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3521 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003522 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003523 }
Sunny Goyal34942622014-08-29 17:20:55 -07003524 } else {
3525 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3526 }
3527
Winson Chung82b016c2015-05-08 17:00:10 -07003528 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003529 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003530 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003531 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003532 return info;
3533 }
3534
3535 /**
3536 * Make an Intent object for a restored application or shortcut item that points
3537 * to the market page for the item.
3538 */
Adam Cohen091440a2015-03-18 14:16:05 -07003539 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003540 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003541 return getMarketIntent(componentName.getPackageName());
3542 }
3543
3544 static Intent getMarketIntent(String packageName) {
3545 return new Intent(Intent.ACTION_VIEW)
3546 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003547 .scheme("market")
3548 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003549 .appendQueryParameter("id", packageName)
3550 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003551 }
3552
3553 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003554 * Make an ShortcutInfo object for a shortcut that is an application.
3555 *
3556 * If c is not null, then it will be used to fill in missing data like the title and icon.
3557 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003558 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003559 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003560 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003561 if (user == null) {
3562 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003563 return null;
3564 }
3565
Kenny Guyed131872014-04-30 03:02:21 +01003566 ComponentName componentName = intent.getComponent();
3567 if (componentName == null) {
3568 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3569 return null;
3570 }
3571
3572 Intent newIntent = new Intent(intent.getAction(), null);
3573 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3574 newIntent.setComponent(componentName);
3575 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003576 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003577 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3578 return null;
3579 }
3580
3581 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003582 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003583 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3584 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3585 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003586 }
3587
Joe Onorato56d82912010-03-07 14:32:10 -05003588 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003589 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003590 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003591 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003592
Joe Onorato56d82912010-03-07 14:32:10 -05003593 // fall back to the class name of the activity
3594 if (info.title == null) {
3595 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003596 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003597
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003599 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003600 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003601 if (lai != null) {
3602 info.flags = AppInfo.initFlags(lai);
3603 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003605 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003606
Sunny Goyale2df0622015-04-24 11:27:00 -07003607 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003608 ItemInfoFilter f) {
3609 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3610 for (ItemInfo i : infos) {
3611 if (i instanceof ShortcutInfo) {
3612 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003613 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003614 if (cn != null && f.filterItem(null, info, cn)) {
3615 filtered.add(info);
3616 }
3617 } else if (i instanceof FolderInfo) {
3618 FolderInfo info = (FolderInfo) i;
3619 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003620 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003621 if (cn != null && f.filterItem(info, s, cn)) {
3622 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003623 }
3624 }
Winson Chung64359a52013-07-08 17:17:08 -07003625 } else if (i instanceof LauncherAppWidgetInfo) {
3626 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3627 ComponentName cn = info.providerName;
3628 if (cn != null && f.filterItem(null, info, cn)) {
3629 filtered.add(info);
3630 }
Winson Chung8a435102012-08-30 17:16:53 -07003631 }
3632 }
Winson Chung64359a52013-07-08 17:17:08 -07003633 return new ArrayList<ItemInfo>(filtered);
3634 }
3635
Adam Cohen091440a2015-03-18 14:16:05 -07003636 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003637 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003638 ItemInfoFilter filter = new ItemInfoFilter() {
3639 @Override
3640 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003641 if (info.user == null) {
3642 return cn.equals(cname);
3643 } else {
3644 return cn.equals(cname) && info.user.equals(user);
3645 }
Winson Chung64359a52013-07-08 17:17:08 -07003646 }
3647 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003648 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003649 }
3650
Sunny Goyal1a745e82014-10-02 15:58:31 -07003651 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003652 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003653 */
Adam Cohen091440a2015-03-18 14:16:05 -07003654 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003655 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003656 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003657 // Non-app shortcuts are only supported for current user.
3658 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003660
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003661 // TODO: If there's an explicit component and we can't install that, delete it.
3662
Winson Chung82b016c2015-05-08 17:00:10 -07003663 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003664
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003665 Bitmap icon = iconInfo.loadIcon(c, info, context);
3666 // the fallback icon
3667 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003668 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003669 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003670 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003671 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 return info;
3673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003674
Sunny Goyal2350bc92014-10-14 16:42:54 -07003675 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003676 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3677 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3678 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3679
Adam Cohend9198822011-11-22 16:42:47 -08003680 if (intent == null) {
3681 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3682 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3683 return null;
3684 }
3685
Joe Onorato0589f0f2010-02-08 13:44:00 -08003686 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003687 boolean customIcon = false;
3688 ShortcutIconResource iconResource = null;
3689
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003690 if (bitmap instanceof Bitmap) {
3691 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003692 customIcon = true;
3693 } else {
3694 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003695 if (extra instanceof ShortcutIconResource) {
3696 iconResource = (ShortcutIconResource) extra;
3697 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003698 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003699 }
3700 }
3701
Michael Jurkac9d95c52011-08-29 14:03:34 -07003702 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003703
Kenny Guyed131872014-04-30 03:02:21 +01003704 // Only support intents for current user for now. Intents sent from other
3705 // users wouldn't get here without intent forwarding anyway.
3706 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003707 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003708 icon = mIconCache.getDefaultIcon(info.user);
3709 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003710 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003711 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003712
Winson Chung82b016c2015-05-08 17:00:10 -07003713 info.title = Utilities.trim(name);
3714 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003715 info.intent = intent;
3716 info.customIcon = customIcon;
3717 info.iconResource = iconResource;
3718
3719 return info;
3720 }
3721
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003723 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 * or make a new one.
3725 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003726 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 // See if a placeholder was created for us already
3728 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003729 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003731 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 folders.put(id, folderInfo);
3733 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003734 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003735 }
3736
Joe Onoratobe386092009-11-17 17:32:16 -08003737
Sunny Goyal651077b2014-06-30 14:15:31 -07003738 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3739 return (provider != null) && (provider.provider != null)
3740 && (provider.provider.getPackageName() != null);
3741 }
3742
Joe Onoratobe386092009-11-17 17:32:16 -08003743 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003744 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003745 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3746 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3747 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3748 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003749 if (mLoaderTask != null) {
3750 mLoaderTask.dumpState();
3751 } else {
3752 Log.d(TAG, "mLoaderTask=null");
3753 }
Joe Onoratobe386092009-11-17 17:32:16 -08003754 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003755
3756 public Callbacks getCallback() {
3757 return mCallbacks != null ? mCallbacks.get() : null;
3758 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003759
3760 /**
3761 * @return {@link FolderInfo} if its already loaded.
3762 */
3763 public FolderInfo findFolderById(Long folderId) {
3764 synchronized (sBgLock) {
3765 return sBgFolders.get(folderId);
3766 }
3767 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003768
3769 /**
3770 * @return the looper for the worker thread which can be used to start background tasks.
3771 */
3772 public static Looper getWorkerLooper() {
3773 return sWorkerThread.getLooper();
3774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003775}