blob: 6e1aa2a4dbe6caa3bd8e5dab44afed0c04e6048f [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 Goyale5bb7052015-07-27 14:36:07 -070059import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070063import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070064import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070065import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080066
Michael Jurkac2f801e2011-07-12 14:19:46 -070067import java.lang.ref.WeakReference;
68import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070069import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070071import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070072import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070073import java.util.Collections;
74import java.util.Comparator;
75import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070077import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070079import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082/**
83 * Maintains in-memory state of the Launcher. It is expected that there should be only one
84 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070085 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 */
Kenny Guyed131872014-04-30 03:02:21 +010087public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010088 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040090 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070091 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040092
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070094
Dan Sandlerd5024042014-01-09 15:01:33 -050095 public static final int LOADER_FLAG_NONE = 0;
96 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
97 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
98
Joe Onorato36115782010-06-17 13:28:48 -040099 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500100 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800101
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800103 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final LauncherAppState mApp;
106 @Thunk final Object mLock = new Object();
107 @Thunk DeferredHandler mHandler = new DeferredHandler();
108 @Thunk LoaderTask mLoaderTask;
109 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700110 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Jason Monkbbe1e242014-05-16 17:37:34 -0400112 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115 static {
116 sWorkerThread.start();
117 }
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 // We start off with everything not loaded. After that, we assume that
121 // our monitoring of the package manager provides all updates and we never
122 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk boolean mWorkspaceLoaded;
124 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700125
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700126 // When we are loading pages synchronously, we can't just post the binding of items on the side
127 // pages as this delays the rotation process. Instead, we wait for a callback from the first
128 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
129 // a normal load, we also clear this set of Runnables.
130 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700141 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
143 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700145 // The lock that must be acquired before referencing any static bg data structures. Unlike
146 // other locks, this one can generally be held long-term because we never expect any of these
147 // static data structures to be referenced outside of the worker thread except on the first
148 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700149 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700153 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
156 // created by LauncherModel that are directly on the home screen (however, no widgets or
157 // shortcuts within folders).
158 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159
Adam Cohen487f7dd2012-06-28 18:12:10 -0700160 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
161 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700162 new ArrayList<LauncherAppWidgetInfo>();
163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700165 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700166
Adam Cohendcd297f2013-06-18 13:13:40 -0700167 // sBgWorkspaceScreens is the ordered set of workspace screens.
168 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
169
Adam Cohen59400422014-03-05 18:07:04 -0800170 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000171 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800172
Sunny Goyal31860582015-09-18 08:38:57 -0700173 // sBgShortcutProviders is the set of custom shortcut providers
174 public static List<ResolveInfo> sBgShortcutProviders;
175
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700177 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
178 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700179
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700180 // </ only access in worker thread >
181
Adam Cohen091440a2015-03-18 14:16:05 -0700182 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
Adam Cohen091440a2015-03-18 14:16:05 -0700184 @Thunk final LauncherAppsCompat mLauncherApps;
185 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100186
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700188 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public int getCurrentWorkspaceScreen();
190 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700191 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
192 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700193 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700194 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700195 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800196 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700199 public void bindAppsAdded(ArrayList<Long> newScreens,
200 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700201 ArrayList<ItemInfo> addAnimated,
202 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200203 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700204 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
205 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
206 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700207 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700208 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700209 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700210 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700211 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700212 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700213 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700214 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chung64359a52013-07-08 17:17:08 -0700217 public interface ItemInfoFilter {
218 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
219 }
220
Bjorn Bringert1307f632013-10-03 22:31:03 +0100221 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800222 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400223
Winson Chungee055712013-07-30 14:46:24 -0700224 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700225 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400226 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
227 // resource string.
228 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
229 ProviderInfo providerInfo =
230 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
231 ProviderInfo redirectProvider =
232 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700233
234 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400235 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700236
237 if (mOldContentProviderExists) {
238 Log.d(TAG, "Old launcher provider exists.");
239 } else {
240 Log.d(TAG, "Old launcher provider does not exist.");
241 }
242
Daniel Sandlere4f98912013-06-25 15:13:26 -0400243 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100244 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700245 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 mIconCache = iconCache;
247
Kenny Guyed131872014-04-30 03:02:21 +0100248 mLauncherApps = LauncherAppsCompat.getInstance(context);
249 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 }
251
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
253 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700255 if (sWorkerThread.getThreadId() == Process.myTid()) {
256 // If we are on the worker thread, post onto the main handler
257 mHandler.post(r);
258 } else {
259 r.run();
260 }
261 }
262
263 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
264 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700265 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700266 if (sWorkerThread.getThreadId() == Process.myTid()) {
267 r.run();
268 } else {
269 // If we are not on the worker thread, then post to the worker handler
270 sWorker.post(r);
271 }
272 }
273
Winson Chunge43a1e72014-01-15 10:33:02 -0800274 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
275 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800276 }
277
Sunny Goyal756adbc2015-04-16 15:20:51 -0700278 public void setPackageState(final PackageInstallInfo installInfo) {
279 Runnable updateRunnable = new Runnable() {
280
281 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500282 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700283 synchronized (sBgLock) {
284 final HashSet<ItemInfo> updates = new HashSet<>();
285
286 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
287 // Ignore install success events as they are handled by Package add events.
288 return;
289 }
290
Sunny Goyale2df0622015-04-24 11:27:00 -0700291 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700292 if (info instanceof ShortcutInfo) {
293 ShortcutInfo si = (ShortcutInfo) info;
294 ComponentName cn = si.getTargetComponent();
295 if (si.isPromise() && (cn != null)
296 && installInfo.packageName.equals(cn.getPackageName())) {
297 si.setInstallProgress(installInfo.progress);
298
299 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
300 // Mark this info as broken.
301 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
302 }
303 updates.add(si);
304 }
305 }
306 }
307
308 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
309 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
310 widget.installProgress = installInfo.progress;
311 updates.add(widget);
312 }
313 }
314
315 if (!updates.isEmpty()) {
316 // Push changes to the callback.
317 Runnable r = new Runnable() {
318 public void run() {
319 Callbacks callbacks = getCallback();
320 if (callbacks != null) {
321 callbacks.bindRestoreItemsChange(updates);
322 }
323 }
324 };
325 mHandler.post(r);
326 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500327 }
328 }
329 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700330 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500331 }
332
Sunny Goyal756adbc2015-04-16 15:20:51 -0700333 /**
334 * Updates the icons and label of all pending icons for the provided package name.
335 */
336 public void updateSessionDisplayInfo(final String packageName) {
337 Runnable updateRunnable = new Runnable() {
338
339 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700340 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341 synchronized (sBgLock) {
342 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
343 final UserHandleCompat user = UserHandleCompat.myUserHandle();
344
Sunny Goyale2df0622015-04-24 11:27:00 -0700345 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700346 if (info instanceof ShortcutInfo) {
347 ShortcutInfo si = (ShortcutInfo) info;
348 ComponentName cn = si.getTargetComponent();
349 if (si.isPromise() && (cn != null)
350 && packageName.equals(cn.getPackageName())) {
351 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
352 // For auto install apps update the icon as well as label.
353 mIconCache.getTitleAndIcon(si,
354 si.promisedIntent, user,
355 si.shouldUseLowResIcon());
356 } else {
357 // Only update the icon for restored apps.
358 si.updateIcon(mIconCache);
359 }
360 updates.add(si);
361 }
362 }
363 }
364
365 if (!updates.isEmpty()) {
366 // Push changes to the callback.
367 Runnable r = new Runnable() {
368 public void run() {
369 Callbacks callbacks = getCallback();
370 if (callbacks != null) {
371 callbacks.bindShortcutsChanged(updates,
372 new ArrayList<ShortcutInfo>(), user);
373 }
374 }
375 };
376 mHandler.post(r);
377 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700378 }
379 }
380 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700381 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700382 }
383
Adam Cohen76a47a12014-02-05 11:47:43 -0800384 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800385 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800386
387 if (allAppsApps == null) {
388 throw new RuntimeException("allAppsApps must not be null");
389 }
390 if (allAppsApps.isEmpty()) {
391 return;
392 }
393
394 // Process the newly added applications and add them to the database first
395 Runnable r = new Runnable() {
396 public void run() {
397 runOnMainThread(new Runnable() {
398 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800399 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800400 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500401 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800402 }
403 }
404 });
405 }
406 };
407 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700408 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800409
Sunny Goyala9116722015-04-29 13:55:58 -0700410 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 int[] xy, int spanX, int spanY) {
412 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700413 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
414 final int xCount = (int) profile.numColumns;
415 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800416 boolean[][] occupied = new boolean[xCount][yCount];
417 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700418 for (ItemInfo r : occupiedPos) {
419 int right = r.cellX + r.spanX;
420 int bottom = r.cellY + r.spanY;
421 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
422 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800423 occupied[x][y] = true;
424 }
425 }
426 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800427 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700428 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 }
430
431 /**
432 * Find a position on the screen for the given size or adds a new screen.
433 * @return screenId and the coordinates for the item.
434 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700435 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800436 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437 ArrayList<Long> workspaceScreens,
438 ArrayList<Long> addedWorkspaceScreensFinal,
439 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700440 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800441
Sunny Goyala9116722015-04-29 13:55:58 -0700442 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700443 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700444 synchronized (sBgLock) {
445 for (ItemInfo info : sBgItemsIdMap) {
446 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
447 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
448 if (items == null) {
449 items = new ArrayList<>();
450 screenItems.put(info.screenId, items);
451 }
452 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800454 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800455 }
456
457 // Find appropriate space for the item.
458 long screenId = 0;
459 int[] cordinates = new int[2];
460 boolean found = false;
461
462 int screenCount = workspaceScreens.size();
463 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700464 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465 if (preferredScreenIndex < screenCount) {
466 screenId = workspaceScreens.get(preferredScreenIndex);
467 found = findNextAvailableIconSpaceInScreen(
468 screenItems.get(screenId), cordinates, spanX, spanY);
469 }
470
471 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700472 // Search on any of the screens starting from the first screen.
473 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800474 screenId = workspaceScreens.get(screen);
475 if (findNextAvailableIconSpaceInScreen(
476 screenItems.get(screenId), cordinates, spanX, spanY)) {
477 // We found a space for it
478 found = true;
479 break;
480 }
481 }
482 }
483
484 if (!found) {
485 // Still no position found. Add a new screen to the end.
486 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
487
488 // Save the screen id for binding in the workspace
489 workspaceScreens.add(screenId);
490 addedWorkspaceScreensFinal.add(screenId);
491
492 // If we still can't find an empty space, then God help us all!!!
493 if (!findNextAvailableIconSpaceInScreen(
494 screenItems.get(screenId), cordinates, spanX, spanY)) {
495 throw new RuntimeException("Can't find space to add the item");
496 }
497 }
498 return Pair.create(screenId, cordinates);
499 }
500
501 /**
502 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700504 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700505 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800507 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700508 return;
Winson Chung997a9232013-07-24 15:33:46 -0700509 }
Winson Chung64359a52013-07-08 17:17:08 -0700510 // Process the newly added applications and add them to the database first
511 Runnable r = new Runnable() {
512 public void run() {
513 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
514 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
515
Winson Chung76828c82013-08-19 15:43:29 -0700516 // Get the list of workspace screens. We need to append to this list and
517 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
518 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800519 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700520 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700522 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800523 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700524 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800525 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700526 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800527 }
Winson Chung76828c82013-08-19 15:43:29 -0700528
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800529 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700530 Pair<Long, int[]> coords = findSpaceForItem(context,
531 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 1, 1);
533 long screenId = coords.first;
534 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700535
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700536 ItemInfo itemInfo;
537 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
538 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800539 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700540 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700541 } else {
542 throw new RuntimeException("Unexpected info type");
543 }
Winson Chung94d67682013-09-25 16:29:40 -0700544
Winson Chung64359a52013-07-08 17:17:08 -0700545 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700546 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700547 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700548 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700549 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700550 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700551 }
552 }
553
Winson Chung76828c82013-08-19 15:43:29 -0700554 // Update the workspace screens
555 updateWorkspaceScreenOrder(context, workspaceScreens);
556
Adam Cohen76a47a12014-02-05 11:47:43 -0800557 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700558 runOnMainThread(new Runnable() {
559 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800560 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700561 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700562 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
563 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700564 if (!addedShortcutsFinal.isEmpty()) {
565 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
566 long lastScreenId = info.screenId;
567 for (ItemInfo i : addedShortcutsFinal) {
568 if (i.screenId == lastScreenId) {
569 addAnimated.add(i);
570 } else {
571 addNotAnimated.add(i);
572 }
Winson Chung997a9232013-07-24 15:33:46 -0700573 }
574 }
Winson Chungd64d1762013-08-20 14:37:16 -0700575 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800576 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700577 }
Winson Chung64359a52013-07-08 17:17:08 -0700578 }
Winson Chung997a9232013-07-24 15:33:46 -0700579 });
580 }
Winson Chung64359a52013-07-08 17:17:08 -0700581 }
582 };
583 runOnWorkerThread(r);
584 }
585
Sunny Goyald33860f2015-04-23 16:02:20 -0700586 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700587 if (sWorkerThread.getThreadId() == Process.myTid()) {
588 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
589 "main thread");
590 }
591
592 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400593 synchronized (mDeferredBindRunnables) {
594 mDeferredBindRunnables.clear();
595 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700596
597 // Remove any queued UI runnables
598 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700599 // Unbind all the workspace items
600 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700601 }
602
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700603 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700604 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700605 // Ensure that we don't use the same workspace items data structure on the main thread
606 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700607 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700608 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700609 tmpItems.addAll(sBgWorkspaceItems);
610 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611 }
612 Runnable r = new Runnable() {
613 @Override
614 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700615 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700616 item.unbind();
617 }
618 }
619 };
620 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700621 }
622
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 /**
624 * Adds an item to the DB if it was not created previously, or move it to a new
625 * <container, screen, cellX, cellY>
626 */
627 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700628 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 if (item.container == ItemInfo.NO_ID) {
630 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700631 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 } else {
633 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700634 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
636 }
637
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700638 static void checkItemInfoLocked(
639 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
640 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
641 if (modelItem != null && item != modelItem) {
642 // check all the data is consistent
643 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
644 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
645 ShortcutInfo shortcut = (ShortcutInfo) item;
646 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
647 modelShortcut.intent.filterEquals(shortcut.intent) &&
648 modelShortcut.id == shortcut.id &&
649 modelShortcut.itemType == shortcut.itemType &&
650 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700651 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700652 modelShortcut.cellX == shortcut.cellX &&
653 modelShortcut.cellY == shortcut.cellY &&
654 modelShortcut.spanX == shortcut.spanX &&
655 modelShortcut.spanY == shortcut.spanY &&
656 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
657 (modelShortcut.dropPos != null &&
658 shortcut.dropPos != null &&
659 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
660 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
661 // For all intents and purposes, this is the same object
662 return;
663 }
664 }
665
666 // the modelItem needs to match up perfectly with item if our model is
667 // to be consistent with the database-- for now, just require
668 // modelItem == item or the equality check above
669 String msg = "item: " + ((item != null) ? item.toString() : "null") +
670 "modelItem: " +
671 ((modelItem != null) ? modelItem.toString() : "null") +
672 "Error: ItemInfo passed to checkItemInfo doesn't match original";
673 RuntimeException e = new RuntimeException(msg);
674 if (stackTrace != null) {
675 e.setStackTrace(stackTrace);
676 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800677 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700678 }
679 }
680
Michael Jurka816474f2012-06-25 14:49:02 -0700681 static void checkItemInfo(final ItemInfo item) {
682 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
683 final long itemId = item.id;
684 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700685 public void run() {
686 synchronized (sBgLock) {
687 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700688 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700689 }
690 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700691 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700692 }
693
Michael Jurkac9d95c52011-08-29 14:03:34 -0700694 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
695 final ItemInfo item, final String callingFunction) {
696 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700697 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700698 final ContentResolver cr = context.getContentResolver();
699
Adam Cohen487f7dd2012-06-28 18:12:10 -0700700 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700701 Runnable r = new Runnable() {
702 public void run() {
703 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700704 updateItemArrays(item, itemId, stackTrace);
705 }
706 };
707 runOnWorkerThread(r);
708 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700709
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
711 final ArrayList<ItemInfo> items, final String callingFunction) {
712 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700713
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700714 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
715 Runnable r = new Runnable() {
716 public void run() {
717 ArrayList<ContentProviderOperation> ops =
718 new ArrayList<ContentProviderOperation>();
719 int count = items.size();
720 for (int i = 0; i < count; i++) {
721 ItemInfo item = items.get(i);
722 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700723 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700724 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700725
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700726 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
727 updateItemArrays(item, itemId, stackTrace);
728
729 }
730 try {
731 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
732 } catch (Exception e) {
733 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700734 }
735 }
736 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700737 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700738 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700739
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
741 // Lock on mBgLock *after* the db operation
742 synchronized (sBgLock) {
743 checkItemInfoLocked(itemId, item, stackTrace);
744
745 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
746 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
747 // Item is in a folder, make sure this folder exists
748 if (!sBgFolders.containsKey(item.container)) {
749 // An items container is being set to a that of an item which is not in
750 // the list of Folders.
751 String msg = "item: " + item + " container being set to: " +
752 item.container + ", not in the list of folders";
753 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754 }
755 }
756
757 // Items are added/removed from the corresponding FolderInfo elsewhere, such
758 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
759 // that are on the desktop, as appropriate
760 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800761 if (modelItem != null &&
762 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
763 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700764 switch (modelItem.itemType) {
765 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
766 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
767 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
768 if (!sBgWorkspaceItems.contains(modelItem)) {
769 sBgWorkspaceItems.add(modelItem);
770 }
771 break;
772 default:
773 break;
774 }
775 } else {
776 sBgWorkspaceItems.remove(modelItem);
777 }
778 }
779 }
780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700783 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700784 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787 item.cellX = cellX;
788 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700789
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 // We store hotseat items in canonical form which is this orientation invariant position
791 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700795 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700797 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798
799 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700801 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
802 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800803 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700804 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400805
Michael Jurkac9d95c52011-08-29 14:03:34 -0700806 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700807 }
808
809 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700810 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
811 * cellX, cellY have already been updated on the ItemInfos.
812 */
813 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
814 final long container, final int screen) {
815
816 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
817 int count = items.size();
818
819 for (int i = 0; i < count; i++) {
820 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821 item.container = container;
822
823 // We store hotseat items in canonical form which is this orientation invariant position
824 // in the hotseat
825 if (context instanceof Launcher && screen < 0 &&
826 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700827 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 item.cellY);
829 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831 }
832
833 final ContentValues values = new ContentValues();
834 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
835 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
836 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800837 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700838 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839
840 contentValues.add(values);
841 }
842 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
843 }
844
845 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700846 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800847 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700848 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700850 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800851 item.cellX = cellX;
852 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 item.spanX = spanX;
854 item.spanY = spanY;
855
856 // We store hotseat items in canonical form which is this orientation invariant position
857 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700861 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 }
Adam Cohend4844c32011-02-18 19:25:06 -0800864
Adam Cohend4844c32011-02-18 19:25:06 -0800865 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800866 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
868 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800869 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700870 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
871 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700872 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800873
Michael Jurka816474f2012-06-25 14:49:02 -0700874 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700875 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700876
877 /**
878 * Update an item to the database in a specified container.
879 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700880 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700881 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100882 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700883 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800884 }
885
Sunny Goyal756a28a2015-04-23 17:07:55 -0700886 private void assertWorkspaceLoaded() {
Sunny Goyal639e9062015-08-19 19:17:06 -0700887 if (LauncherAppState.isDogfoodBuild()) {
888 synchronized (mLock) {
889 if (!mHasLoaderCompletedOnce ||
890 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
891 throw new RuntimeException("Trying to add shortcut while loader is running");
892 }
893 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700894 }
895 }
896
Adam Cohend4844c32011-02-18 19:25:06 -0800897 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700898 * Returns true if the shortcuts already exists on the workspace. This must be called after
899 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700901 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
902 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700903 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700904 if (intent.getComponent() != null) {
905 // If component is not null, an intent with null package will produce
906 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700907 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700908 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700909 intentWithPkg = intent.toUri(0);
910 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700911 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700912 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
913 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700914 }
915 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700916 intentWithPkg = intent.toUri(0);
917 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700918 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700919
920 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700921 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700922 if (item instanceof ShortcutInfo) {
923 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700924 Intent targetIntent = info.promisedIntent == null
925 ? info.intent : info.promisedIntent;
926 if (targetIntent != null && info.user.equals(user)) {
927 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700928 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
929 return true;
930 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700931 }
932 }
933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700935 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700936 }
937
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 /**
939 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
940 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700941 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 final ContentResolver cr = context.getContentResolver();
943 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
944 "_id=? and (itemType=? or itemType=?)",
945 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700946 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700947
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 try {
949 if (c.moveToFirst()) {
950 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
951 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
952 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
953 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
954 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
955 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700956 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 FolderInfo folderInfo = null;
959 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700960 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
961 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700963 }
964
Sunny Goyala508e4f2015-05-21 09:33:57 -0700965 // Do not trim the folder label, as is was set by the user.
966 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 folderInfo.id = id;
968 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700969 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700970 folderInfo.cellX = c.getInt(cellXIndex);
971 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700972 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973
974 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700975 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 } finally {
977 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700978 }
979
980 return null;
981 }
982
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 /**
984 * Add an item to the database in a specified container. Sets the container, screen, cellX and
985 * cellY fields of the item. Also assigns an ID to the item.
986 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700987 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700988 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 item.cellX = cellX;
991 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 // We store hotseat items in canonical form which is this orientation invariant position
993 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700998 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000
1001 final ContentValues values = new ContentValues();
1002 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001003 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004
Michael Jurka414300a2013-08-27 15:42:35 +02001005 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001007
Jason Monk8e19cf22014-03-20 15:06:57 -04001008 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001009 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001011 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001014 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001015 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 sBgItemsIdMap.put(item.id, item);
1017 switch (item.itemType) {
1018 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1019 sBgFolders.put(item.id, (FolderInfo) item);
1020 // Fall through
1021 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1022 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1023 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1024 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1025 sBgWorkspaceItems.add(item);
1026 } else {
1027 if (!sBgFolders.containsKey(item.container)) {
1028 // Adding an item to a folder that doesn't exist.
1029 String msg = "adding item: " + item + " to a folder that " +
1030 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001031 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001033 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034 break;
1035 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1036 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1037 break;
1038 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001039 }
1040 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001041 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001042 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001043 }
1044
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001046 * Creates a new unique child id, for a given cell span across all layouts.
1047 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001048 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001049 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001051 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001052 }
1053
Sunny Goyal34942622014-08-29 17:20:55 -07001054 private static ArrayList<ItemInfo> getItemsByPackageName(
1055 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001056 ItemInfoFilter filter = new ItemInfoFilter() {
1057 @Override
1058 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1059 return cn.getPackageName().equals(pn) && info.user.equals(user);
1060 }
1061 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001062 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001063 }
1064
1065 /**
1066 * Removes all the items from the database corresponding to the specified package.
1067 */
1068 static void deletePackageFromDatabase(Context context, final String pn,
1069 final UserHandleCompat user) {
1070 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001071 }
1072
1073 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001074 * Removes the specified item from the database
1075 * @param context
1076 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001077 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001078 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001079 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1080 items.add(item);
1081 deleteItemsFromDatabase(context, items);
1082 }
1083
1084 /**
1085 * Removes the specified items from the database
1086 * @param context
1087 * @param item
1088 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001089 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001091 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001092 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001093 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001094 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001095 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001096
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001097 // Lock on mBgLock *after* the db operation
1098 synchronized (sBgLock) {
1099 switch (item.itemType) {
1100 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1101 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001102 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 if (info.container == item.id) {
1104 // We are deleting a folder which still contains items that
1105 // think they are contained by that folder.
1106 String msg = "deleting a folder (" + item + ") which still " +
1107 "contains items (" + info + ")";
1108 Log.e(TAG, msg);
1109 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001111 sBgWorkspaceItems.remove(item);
1112 break;
1113 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1114 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1115 sBgWorkspaceItems.remove(item);
1116 break;
1117 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1118 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1119 break;
1120 }
1121 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001123 }
1124 }
Michael Jurka83df1882011-08-31 20:59:26 -07001125 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 }
1128
1129 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001130 * Update the order of the workspace screens in the database. The array list contains
1131 * a list of screen ids in the order that they should appear.
1132 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001133 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001134 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001135 final ContentResolver cr = context.getContentResolver();
1136 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1137
1138 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001139 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001140 while (iter.hasNext()) {
1141 long id = iter.next();
1142 if (id < 0) {
1143 iter.remove();
1144 }
1145 }
1146
1147 Runnable r = new Runnable() {
1148 @Override
1149 public void run() {
Yura085c8532014-02-11 15:15:29 +00001150 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001151 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001152 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001153 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001154 for (int i = 0; i < count; i++) {
1155 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001156 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001157 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1158 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001159 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001160 }
Yura085c8532014-02-11 15:15:29 +00001161
1162 try {
1163 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1164 } catch (Exception ex) {
1165 throw new RuntimeException(ex);
1166 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001167
Winson Chungba9c37f2013-08-30 14:11:37 -07001168 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001169 sBgWorkspaceScreens.clear();
1170 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001171 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001172 }
1173 };
1174 runOnWorkerThread(r);
1175 }
1176
1177 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001178 * Remove the contents of the specified folder from the database
1179 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001180 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 final ContentResolver cr = context.getContentResolver();
1182
Michael Jurkac9d95c52011-08-29 14:03:34 -07001183 Runnable r = new Runnable() {
1184 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001185 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001187 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 sBgItemsIdMap.remove(info.id);
1189 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001190 sBgWorkspaceItems.remove(info);
1191 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001192
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001193 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001194 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001196 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 for (ItemInfo childInfo : info.contents) {
1198 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001200 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001201 }
1202 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 }
1205
1206 /**
1207 * Set this as the current Launcher activity object for the loader.
1208 */
1209 public void initialize(Callbacks callbacks) {
1210 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001211 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1212 // workspace to prevent leaking Launcher activities on orientation change.
1213 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 mCallbacks = new WeakReference<Callbacks>(callbacks);
1215 }
1216 }
1217
Kenny Guyed131872014-04-30 03:02:21 +01001218 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001219 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001220 int op = PackageUpdatedTask.OP_UPDATE;
1221 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1222 user));
1223 }
1224
1225 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001226 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001227 int op = PackageUpdatedTask.OP_REMOVE;
1228 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1229 user));
1230 }
1231
1232 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001233 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001234 int op = PackageUpdatedTask.OP_ADD;
1235 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1236 user));
1237 }
1238
1239 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001240 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001241 boolean replacing) {
1242 if (!replacing) {
1243 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1244 user));
1245 if (mAppsCanBeOnRemoveableStorage) {
1246 // Only rebind if we support removable storage. It catches the
1247 // case where
1248 // apps on the external sd card need to be reloaded
1249 startLoaderFromBackground();
1250 }
1251 } else {
1252 // If we are replacing then just update the packages in the list
1253 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1254 packageNames, user));
1255 }
1256 }
1257
1258 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001259 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001260 boolean replacing) {
1261 if (!replacing) {
1262 enqueuePackageUpdated(new PackageUpdatedTask(
1263 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1264 user));
1265 }
Kenny Guyed131872014-04-30 03:02:21 +01001266 }
1267
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001268 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001269 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1270 * ACTION_PACKAGE_CHANGED.
1271 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001272 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001273 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001274 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001275
Joe Onorato36115782010-06-17 13:28:48 -04001276 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001277 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001278 // If we have changed locale we need to clear out the labels in all apps/workspace.
1279 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001280 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001281 Callbacks callbacks = getCallback();
1282 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001283 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001284 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001285 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1286 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001287 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001288 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001289 }
1290 }
1291
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001292 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001293 resetLoadedState(true, true);
1294
Reena Lee93f824a2011-09-23 17:20:28 -07001295 // Do this here because if the launcher activity is running it will be restarted.
1296 // If it's not running startLoaderFromBackground will merely tell it that it needs
1297 // to reload.
1298 startLoaderFromBackground();
1299 }
1300
Winson Chungf0c6ae02012-03-21 16:10:31 -07001301 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1302 synchronized (mLock) {
1303 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1304 // mWorkspaceLoaded to true later
1305 stopLoaderLocked();
1306 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1307 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1308 }
1309 }
1310
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001311 /**
1312 * When the launcher is in the background, it's possible for it to miss paired
1313 * configuration changes. So whenever we trigger the loader from the background
1314 * tell the launcher that it needs to re-run the loader when it comes back instead
1315 * of doing it now.
1316 */
1317 public void startLoaderFromBackground() {
1318 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001319 Callbacks callbacks = getCallback();
1320 if (callbacks != null) {
1321 // Only actually run the loader if they're not paused.
1322 if (!callbacks.setLoadOnResume()) {
1323 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001324 }
1325 }
1326 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001327 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001328 }
Joe Onorato36115782010-06-17 13:28:48 -04001329 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001330
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001331 /**
1332 * If there is already a loader task running, tell it to stop.
1333 */
1334 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001335 LoaderTask oldTask = mLoaderTask;
1336 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001337 oldTask.stopLocked();
1338 }
Reena Lee93f824a2011-09-23 17:20:28 -07001339 }
1340
Adam Cohen1a85c582014-09-30 09:48:49 -07001341 public boolean isCurrentCallbacks(Callbacks callbacks) {
1342 return (mCallbacks != null && mCallbacks.get() == callbacks);
1343 }
1344
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001345 public void startLoader(int synchronousBindPage) {
1346 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001347 }
1348
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001349 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001350 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1351 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001352 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353 // Clear any deferred bind-runnables from the synchronized load process
1354 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001355 synchronized (mDeferredBindRunnables) {
1356 mDeferredBindRunnables.clear();
1357 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358
Joe Onorato36115782010-06-17 13:28:48 -04001359 // Don't bother to start the thread if we know it's not going to do anything
1360 if (mCallbacks != null && mCallbacks.get() != null) {
1361 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001362 stopLoaderLocked();
1363 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001364 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001365 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1367 } else {
1368 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1369 sWorker.post(mLoaderTask);
1370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
1373 }
1374
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 void bindRemainingSynchronousPages() {
1376 // Post the remaining side pages to be loaded
1377 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001378 Runnable[] deferredBindRunnables = null;
1379 synchronized (mDeferredBindRunnables) {
1380 deferredBindRunnables = mDeferredBindRunnables.toArray(
1381 new Runnable[mDeferredBindRunnables.size()]);
1382 mDeferredBindRunnables.clear();
1383 }
1384 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001385 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001387 }
1388 }
1389
Joe Onorato36115782010-06-17 13:28:48 -04001390 public void stopLoader() {
1391 synchronized (mLock) {
1392 if (mLoaderTask != null) {
1393 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 }
1395 }
Joe Onorato36115782010-06-17 13:28:48 -04001396 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001397
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001398 /**
1399 * Loads the workspace screen ids in an ordered list.
1400 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001401 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001402 final ContentResolver contentResolver = context.getContentResolver();
1403 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001404
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001405 // Get screens ordered by rank.
1406 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1407 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1408 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001409 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001410 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001411 while (sc.moveToNext()) {
1412 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001413 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001414 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001415 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1416 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001417 }
1418 }
1419 } finally {
1420 sc.close();
1421 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001422 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001423 }
1424
Michael Jurkac57b7a82011-08-09 22:02:20 -07001425 public boolean isAllAppsLoaded() {
1426 return mAllAppsLoaded;
1427 }
1428
Joe Onorato36115782010-06-17 13:28:48 -04001429 /**
1430 * Runnable for the thread that loads the contents of the launcher:
1431 * - workspace icons
1432 * - widgets
1433 * - all apps icons
1434 */
1435 private class LoaderTask implements Runnable {
1436 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001437 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001438 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001439 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001440 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001441
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001442 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001443 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001444 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 }
1446
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001447 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001448 mIsLoadingAndBindingWorkspace = true;
1449
Joe Onorato36115782010-06-17 13:28:48 -04001450 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001451 if (DEBUG_LOADERS) {
1452 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001454
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001455 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001456 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001457 synchronized (LoaderTask.this) {
1458 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001459 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001460 }
1461 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 }
1463 }
1464
Joe Onorato36115782010-06-17 13:28:48 -04001465 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001466 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001467 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001468
Joe Onorato36115782010-06-17 13:28:48 -04001469 private void waitForIdle() {
1470 // Wait until the either we're stopped or the other threads are done.
1471 // This way we don't start loading all apps until the workspace has settled
1472 // down.
1473 synchronized (LoaderTask.this) {
1474 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001475
Joe Onorato36115782010-06-17 13:28:48 -04001476 mHandler.postIdle(new Runnable() {
1477 public void run() {
1478 synchronized (LoaderTask.this) {
1479 mLoadAndBindStepFinished = true;
1480 if (DEBUG_LOADERS) {
1481 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001482 }
Joe Onorato36115782010-06-17 13:28:48 -04001483 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001484 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001485 }
Joe Onorato36115782010-06-17 13:28:48 -04001486 });
1487
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001488 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001489 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001490 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1491 // wait no longer than 1sec at a time
1492 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001493 } catch (InterruptedException ex) {
1494 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001495 }
1496 }
Joe Onorato36115782010-06-17 13:28:48 -04001497 if (DEBUG_LOADERS) {
1498 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001499 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001500 + "ms for previous step to finish binding");
1501 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001502 }
Joe Onorato36115782010-06-17 13:28:48 -04001503 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001506 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 // Ensure that we have a valid page index to load synchronously
1508 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1509 "valid page index");
1510 }
1511 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1512 // Ensure that we don't try and bind a specified page when the pages have not been
1513 // loaded already (we should load everything asynchronously in that case)
1514 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1515 }
1516 synchronized (mLock) {
1517 if (mIsLoaderTaskRunning) {
1518 // Ensure that we are never running the background loading at this point since
1519 // we also touch the background collections
1520 throw new RuntimeException("Error! Background loading is already running");
1521 }
1522 }
1523
1524 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1525 // data structures, we can't allow any other thread to touch that data, but because
1526 // this call is synchronous, we can get away with not locking).
1527
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001528 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001529 // operations from the previous activity. We need to ensure that all queued operations
1530 // are executed before any synchronous binding work is done.
1531 mHandler.flush();
1532
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 // Divide the set of loaded items into those that we are binding synchronously, and
1534 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001535 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001536 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1537 // arise from that.
1538 onlyBindAllApps();
1539 }
1540
Joe Onorato36115782010-06-17 13:28:48 -04001541 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001542 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001543 if (mStopped) {
1544 return;
1545 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001546 mIsLoaderTaskRunning = true;
1547 }
Joe Onorato36115782010-06-17 13:28:48 -04001548 // Optimize for end-user experience: if the Launcher is up and // running with the
1549 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1550 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001551 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001552 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001553 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001554
Joe Onorato36115782010-06-17 13:28:48 -04001555 if (mStopped) {
1556 break keep_running;
1557 }
1558
Joe Onorato36115782010-06-17 13:28:48 -04001559 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001560
1561 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001562 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1563 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565
Joe Onorato36115782010-06-17 13:28:48 -04001566 // Clear out this reference, otherwise we end up holding it until all of the
1567 // callback runnables are done.
1568 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569
Joe Onorato36115782010-06-17 13:28:48 -04001570 synchronized (mLock) {
1571 // If we are still the last one to be scheduled, remove ourselves.
1572 if (mLoaderTask == this) {
1573 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001575 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001576 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001577 }
Joe Onorato36115782010-06-17 13:28:48 -04001578 }
1579
1580 public void stopLocked() {
1581 synchronized (LoaderTask.this) {
1582 mStopped = true;
1583 this.notify();
1584 }
1585 }
1586
1587 /**
1588 * Gets the callbacks object. If we've been stopped, or if the launcher object
1589 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1590 * object that was around when the deferred message was scheduled, and if there's
1591 * a new Callbacks object around then also return null. This will save us from
1592 * calling onto it with data that will be ignored.
1593 */
1594 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1595 synchronized (mLock) {
1596 if (mStopped) {
1597 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001598 }
Joe Onorato36115782010-06-17 13:28:48 -04001599
1600 if (mCallbacks == null) {
1601 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001602 }
Joe Onorato36115782010-06-17 13:28:48 -04001603
1604 final Callbacks callbacks = mCallbacks.get();
1605 if (callbacks != oldCallbacks) {
1606 return null;
1607 }
1608 if (callbacks == null) {
1609 Log.w(TAG, "no mCallbacks");
1610 return null;
1611 }
1612
1613 return callbacks;
1614 }
1615 }
1616
1617 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001618 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1619 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001620 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001621 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001622 final int countX = profile.numColumns;
1623 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001624
Adam Cohendcd297f2013-06-18 13:13:40 -07001625 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001626 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001627 // Return early if we detect that an item is under the hotseat button
1628 if (mCallbacks == null ||
1629 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001630 Log.e(TAG, "Error loading shortcut into hotseat " + item
1631 + " into position (" + item.screenId + ":" + item.cellX + ","
1632 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001633 return false;
1634 }
1635
Dan Sandler295ae182013-12-10 16:05:47 -05001636 final ItemInfo[][] hotseatItems =
1637 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1638
Adam Cohen2e6da152015-05-06 11:42:25 -07001639 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001640 Log.e(TAG, "Error loading shortcut " + item
1641 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001642 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001643 + ")");
1644 return false;
1645 }
1646
Dan Sandler295ae182013-12-10 16:05:47 -05001647 if (hotseatItems != null) {
1648 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001649 Log.e(TAG, "Error loading shortcut into hotseat " + item
1650 + " into position (" + item.screenId + ":" + item.cellX + ","
1651 + item.cellY + ") occupied by "
1652 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1653 [(int) item.screenId][0]);
1654 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001655 } else {
1656 hotseatItems[(int) item.screenId][0] = item;
1657 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001658 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001659 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001660 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001661 items[(int) item.screenId][0] = item;
1662 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001663 return true;
1664 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001665 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1666 if (!workspaceScreens.contains((Long) item.screenId)) {
1667 // The item has an invalid screen id.
1668 return false;
1669 }
1670 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001671 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001672 return true;
1673 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001674
Adam Cohendcd297f2013-06-18 13:13:40 -07001675 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001676 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001677 occupied.put(item.screenId, items);
1678 }
1679
Dan Sandler295ae182013-12-10 16:05:47 -05001680 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001681 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1682 item.cellX < 0 || item.cellY < 0 ||
1683 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1684 Log.e(TAG, "Error loading shortcut " + item
1685 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1686 + item.cellX + "," + item.cellY
1687 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1688 return false;
1689 }
1690
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001691 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001692 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1693 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001694 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001695 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001697 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001698 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001699 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001700 return false;
1701 }
1702 }
1703 }
1704 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1705 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001706 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001707 }
1708 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001709
Joe Onorato36115782010-06-17 13:28:48 -04001710 return true;
1711 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712
Winson Chungba9c37f2013-08-30 14:11:37 -07001713 /** Clears all the sBg data structures */
1714 private void clearSBgDataStructures() {
1715 synchronized (sBgLock) {
1716 sBgWorkspaceItems.clear();
1717 sBgAppWidgets.clear();
1718 sBgFolders.clear();
1719 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001720 sBgWorkspaceScreens.clear();
1721 }
1722 }
1723
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001724 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001725 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001726
Joe Onorato36115782010-06-17 13:28:48 -04001727 final Context context = mContext;
1728 final ContentResolver contentResolver = context.getContentResolver();
1729 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001730 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001731 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1732 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001733 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001734
Winson Chung892c74d2013-08-22 16:15:50 -07001735 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001736 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001737 int countX = profile.numColumns;
1738 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001739
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001740 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001741 long migrationStartTime = System.currentTimeMillis();
1742 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001743 try {
1744 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1745 // Clear the flags before starting the task, so that we do not run the task
1746 // again, in case there was an uncaught error.
1747 MigrateFromRestoreTask.clearFlags(mContext);
1748 task.execute();
1749 } catch (Exception e) {
1750 Log.e(TAG, "Error during grid migration", e);
1751
1752 // Clear workspace.
1753 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1754 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001755 Log.v(TAG, "Workspace migration completed in "
1756 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001757 }
1758
Dan Sandlerd5024042014-01-09 15:01:33 -05001759 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1760 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1761 LauncherAppState.getLauncherProvider().deleteDatabase();
1762 }
1763
1764 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1765 // append the user's Launcher2 shortcuts
1766 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1767 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1768 } else {
1769 // Make sure the default workspace is loaded
1770 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001771 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001772 }
Adam Cohene25af792013-06-06 23:08:25 -07001773
Winson Chung2abf94d2012-07-18 18:16:38 -07001774 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001775 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001776 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001777 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001778 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001779
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001781 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001782 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001783 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001784 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001785
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 // +1 for the hotseat (it can be larger than the workspace)
1787 // Load workspace in reverse order to ensure that latest items are loaded first (and
1788 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001789 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 try {
1792 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1793 final int intentIndex = c.getColumnIndexOrThrow
1794 (LauncherSettings.Favorites.INTENT);
1795 final int titleIndex = c.getColumnIndexOrThrow
1796 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001797 final int containerIndex = c.getColumnIndexOrThrow(
1798 LauncherSettings.Favorites.CONTAINER);
1799 final int itemTypeIndex = c.getColumnIndexOrThrow(
1800 LauncherSettings.Favorites.ITEM_TYPE);
1801 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1802 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001803 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1804 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001805 final int screenIndex = c.getColumnIndexOrThrow(
1806 LauncherSettings.Favorites.SCREEN);
1807 final int cellXIndex = c.getColumnIndexOrThrow
1808 (LauncherSettings.Favorites.CELLX);
1809 final int cellYIndex = c.getColumnIndexOrThrow
1810 (LauncherSettings.Favorites.CELLY);
1811 final int spanXIndex = c.getColumnIndexOrThrow
1812 (LauncherSettings.Favorites.SPANX);
1813 final int spanYIndex = c.getColumnIndexOrThrow(
1814 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001815 final int rankIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001817 final int restoredIndex = c.getColumnIndexOrThrow(
1818 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001819 final int profileIdIndex = c.getColumnIndexOrThrow(
1820 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001821 final int optionsIndex = c.getColumnIndexOrThrow(
1822 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001823 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824
Sunny Goyal7f834d22015-04-21 10:10:23 -07001825 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1826 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1827 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1828 }
1829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 ShortcutInfo info;
1831 String intentDescription;
1832 LauncherAppWidgetInfo appWidgetInfo;
1833 int container;
1834 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001835 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001837 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 while (!mStopped && c.moveToNext()) {
1840 try {
1841 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001842 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001843 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001844 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 switch (itemType) {
1847 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1848 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001849 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001851 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001852 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001853 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001854 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001855 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001856 if (user == null) {
1857 // User has been deleted remove the item.
1858 itemsToRemove.add(id);
1859 continue;
1860 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 try {
1862 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001863 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001864 if (cn != null && cn.getPackageName() != null) {
1865 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1866 cn.getPackageName(), user);
1867 boolean validComponent = validPkg &&
1868 launcherApps.isActivityEnabledForProfile(cn, user);
1869
1870 if (validComponent) {
1871 if (restored) {
1872 // no special handling necessary for this item
1873 restoredRows.add(id);
1874 restored = false;
1875 }
1876 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001877 intent = null;
1878 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1879 // We allow auto install apps to have their intent
1880 // updated after an install.
1881 intent = manager.getLaunchIntentForPackage(
1882 cn.getPackageName());
1883 if (intent != null) {
1884 ContentValues values = new ContentValues();
1885 values.put(LauncherSettings.Favorites.INTENT,
1886 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001887 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001888 }
1889 }
1890
1891 if (intent == null) {
1892 // The app is installed but the component is no
1893 // longer available.
1894 Launcher.addDumpLog(TAG,
1895 "Invalid component removed: " + cn, true);
1896 itemsToRemove.add(id);
1897 continue;
1898 } else {
1899 // no special handling necessary for this item
1900 restoredRows.add(id);
1901 restored = false;
1902 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001903 } else if (restored) {
1904 // Package is not yet available but might be
1905 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001906 Launcher.addDumpLog(TAG,
1907 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001908
1909 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1910 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001911 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001912 // App restore has started. Update the flag
1913 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1914 ContentValues values = new ContentValues();
1915 values.put(LauncherSettings.Favorites.RESTORED,
1916 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001917 updateItem(id, values);
1918 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1919 // This is a common app. Try to replace this.
1920 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1921 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1922 if (parser.findDefaultApp()) {
1923 // Default app found. Replace it.
1924 intent = parser.parsedIntent;
1925 cn = intent.getComponent();
1926 ContentValues values = parser.parsedValues;
1927 values.put(LauncherSettings.Favorites.RESTORED, 0);
1928 updateItem(id, values);
1929 restored = false;
1930 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001931
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001932 } else if (REMOVE_UNRESTORED_ICONS) {
1933 Launcher.addDumpLog(TAG,
1934 "Unrestored package removed: " + cn, true);
1935 itemsToRemove.add(id);
1936 continue;
1937 }
Sunny Goyal94485362014-09-18 16:13:58 -07001938 } else if (REMOVE_UNRESTORED_ICONS) {
1939 Launcher.addDumpLog(TAG,
1940 "Unrestored package removed: " + cn, true);
1941 itemsToRemove.add(id);
1942 continue;
1943 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001944 } else if (launcherApps.isAppEnabled(
1945 manager, cn.getPackageName(),
1946 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1947 // Package is present but not available.
1948 allowMissingTarget = true;
1949 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1950 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001951 // SdCard is not ready yet. Package might get available,
1952 // once it is ready.
1953 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1954 + " (check again later)", true);
1955 HashSet<String> pkgs = sPendingPackages.get(user);
1956 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001957 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001958 sPendingPackages.put(user, pkgs);
1959 }
1960 pkgs.add(cn.getPackageName());
1961 allowMissingTarget = true;
1962 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001963
1964 } else {
1965 // Do not wait for external media load anymore.
1966 // Log the invalid package, and remove it
1967 Launcher.addDumpLog(TAG,
1968 "Invalid package removed: " + cn, true);
1969 itemsToRemove.add(id);
1970 continue;
Winson Chungee055712013-07-30 14:46:24 -07001971 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001972 } else if (cn == null) {
1973 // For shortcuts with no component, keep them as they are
1974 restoredRows.add(id);
1975 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001976 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001978 Launcher.addDumpLog(TAG,
1979 "Invalid uri: " + intentDescription, true);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001980 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 continue;
1982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983
Sunny Goyal34b65272015-03-11 16:56:52 -07001984 boolean useLowResIcon = container >= 0 &&
1985 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1986
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001987 if (itemReplaced) {
1988 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001989 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001990 cursorIconInfo.iconIndex, titleIndex,
1991 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001992 } else {
1993 // Don't replace items for other profiles.
1994 itemsToRemove.add(id);
1995 continue;
1996 }
1997 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001998 if (user.equals(UserHandleCompat.myUserHandle())) {
1999 Launcher.addDumpLog(TAG,
2000 "constructing info for partially restored package",
2001 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002002 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07002003 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01002004 intent = getRestoredItemIntent(c, context, intent);
2005 } else {
2006 // Don't restore items for other profiles.
2007 itemsToRemove.add(id);
2008 continue;
2009 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002010 } else if (itemType ==
2011 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002012 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002013 cursorIconInfo.iconIndex, titleIndex,
2014 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002016 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07002017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 // App shortcuts that used to be automatically added to Launcher
2019 // didn't always have the correct intent flags set, so do that
2020 // here
2021 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002022 intent.getCategories() != null &&
2023 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002024 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 intent.addFlags(
2026 Intent.FLAG_ACTIVITY_NEW_TASK |
2027 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2028 }
Michael Jurka96879562012-03-22 05:54:33 -07002029 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002030
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002032 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002035 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 info.cellX = c.getInt(cellXIndex);
2037 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002038 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002039 info.spanX = 1;
2040 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002041 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002042 if (info.promisedIntent != null) {
2043 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2044 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002045 info.isDisabled = disabledState;
2046 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2047 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2048 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002049
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002051 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002052 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 break;
2054 }
2055
Sunny Goyal756adbc2015-04-16 15:20:51 -07002056 if (restored) {
2057 ComponentName cn = info.getTargetComponent();
2058 if (cn != null) {
2059 Integer progress = installingPkgs.get(cn.getPackageName());
2060 if (progress != null) {
2061 info.setInstallProgress(progress);
2062 } else {
2063 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2064 }
2065 }
2066 }
2067
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 switch (container) {
2069 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2070 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2071 sBgWorkspaceItems.add(info);
2072 break;
2073 default:
2074 // Item is in a user folder
2075 FolderInfo folderInfo =
2076 findOrMakeFolder(sBgFolders, container);
2077 folderInfo.add(info);
2078 break;
2079 }
2080 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002081 } else {
2082 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083 }
2084 break;
2085
2086 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2087 id = c.getLong(idIndex);
2088 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2089
Sunny Goyala508e4f2015-05-21 09:33:57 -07002090 // Do not trim the folder label, as is was set by the user.
2091 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002094 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 folderInfo.cellX = c.getInt(cellXIndex);
2096 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002097 folderInfo.spanX = 1;
2098 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002099 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002100
Daniel Sandler8802e962010-05-26 16:28:16 -04002101 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002102 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002103 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002104 break;
2105 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002106
Joe Onorato9c1289c2009-08-17 11:03:03 -04002107 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2109 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2110 sBgWorkspaceItems.add(folderInfo);
2111 break;
Joe Onorato36115782010-06-17 13:28:48 -04002112 }
Joe Onorato17a89222011-02-08 17:26:11 -08002113
Chris Wrenf4d08112014-01-16 18:13:56 -05002114 if (restored) {
2115 // no special handling required for restored folders
2116 restoredRows.add(id);
2117 }
2118
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2120 sBgFolders.put(folderInfo.id, folderInfo);
2121 break;
2122
2123 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002124 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002126 boolean customWidget = itemType ==
2127 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2128
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002130 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002131 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002132 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002133 user = allUsers.get(serialNumber);
2134 if (user == null) {
2135 itemsToRemove.add(id);
2136 continue;
2137 }
2138
Sunny Goyalff572272014-07-23 13:58:07 -07002139 final ComponentName component =
2140 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002141
Sunny Goyal651077b2014-06-30 14:15:31 -07002142 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002143 final boolean isIdValid = (restoreStatus &
2144 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002145 final boolean wasProviderReady = (restoreStatus &
2146 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002147
Adam Cohen59400422014-03-05 18:07:04 -08002148 final LauncherAppWidgetProviderInfo provider =
2149 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002150 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002151 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002152
2153 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002154 if (!isSafeMode && !customWidget &&
2155 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002156 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002157 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002158
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002160 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161 itemsToRemove.add(id);
2162 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002163 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002164 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2165 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002166
Sunny Goyal53f96722015-07-13 19:54:53 -07002167 // The provider is available. So the widget is either
2168 // available or not available. We do not need to track
2169 // any future restore updates.
2170 int status = restoreStatus &
2171 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002172 if (!wasProviderReady) {
2173 // If provider was not previously ready, update the
2174 // status and UI flag.
2175
2176 // Id would be valid only if the widget restore broadcast was received.
2177 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002178 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002179 } else {
2180 status &= ~LauncherAppWidgetInfo
2181 .FLAG_PROVIDER_NOT_READY;
2182 }
2183 }
2184 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002185 } else {
2186 Log.v(TAG, "Widget restore pending id=" + id
2187 + " appWidgetId=" + appWidgetId
2188 + " status =" + restoreStatus);
2189 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002190 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002191 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002192 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002193
2194 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2195 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002196 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002197 // App restore has started. Update the flag
2198 appWidgetInfo.restoreStatus |=
2199 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002200 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002201 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002202 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002203 itemsToRemove.add(id);
2204 continue;
2205 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002206
2207 appWidgetInfo.installProgress =
2208 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002209 }
Sunny Goyalff572272014-07-23 13:58:07 -07002210
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002212 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 appWidgetInfo.cellX = c.getInt(cellXIndex);
2214 appWidgetInfo.cellY = c.getInt(cellYIndex);
2215 appWidgetInfo.spanX = c.getInt(spanXIndex);
2216 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002217 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002218
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2220 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2221 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002222 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2223 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 continue;
2225 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002227 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002229 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002230 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 break;
2232 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002233
Adam Cohen59400422014-03-05 18:07:04 -08002234 if (!customWidget) {
2235 String providerName =
2236 appWidgetInfo.providerName.flattenToString();
2237 if (!providerName.equals(savedProvider) ||
2238 (appWidgetInfo.restoreStatus != restoreStatus)) {
2239 ContentValues values = new ContentValues();
2240 values.put(
2241 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2242 providerName);
2243 values.put(LauncherSettings.Favorites.RESTORED,
2244 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002245 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002246 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002247 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2249 sBgAppWidgets.add(appWidgetInfo);
2250 }
Joe Onorato36115782010-06-17 13:28:48 -04002251 break;
2252 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002253 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002254 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002255 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256 }
2257 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002258 if (c != null) {
2259 c.close();
2260 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 }
2262
Winson Chungba9c37f2013-08-30 14:11:37 -07002263 // Break early if we've stopped loading
2264 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002265 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002266 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002267 }
2268
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002270 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002271 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2272 Utilities.createDbSelectionQuery(
2273 LauncherSettings.Favorites._ID, itemsToRemove), null);
2274 if (DEBUG_LOADERS) {
2275 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2276 LauncherSettings.Favorites._ID, itemsToRemove));
2277 }
2278
2279 // Remove any empty folder
2280 for (long folderId : LauncherAppState.getLauncherProvider()
2281 .deleteEmptyFolders()) {
2282 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2283 sBgFolders.remove(folderId);
2284 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002285 }
2286 }
2287
Sunny Goyal317698b2015-07-29 11:45:41 -07002288 // Sort all the folder items and make sure the first 3 items are high resolution.
2289 for (FolderInfo folder : sBgFolders) {
2290 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2291 int pos = 0;
2292 for (ShortcutInfo info : folder.contents) {
2293 if (info.usingLowResIcon) {
2294 info.updateIcon(mIconCache, false);
2295 }
2296 pos ++;
2297 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2298 break;
2299 }
2300 }
2301 }
2302
Chris Wrenf4d08112014-01-16 18:13:56 -05002303 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002304 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002305 ContentValues values = new ContentValues();
2306 values.put(LauncherSettings.Favorites.RESTORED, 0);
2307 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2308 Utilities.createDbSelectionQuery(
2309 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002310 }
2311
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002312 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2313 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002314 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002315 null, sWorker);
2316 }
2317
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002318 // Remove any empty screens
2319 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002320 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002321 long screenId = item.screenId;
2322 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2323 unusedScreens.contains(screenId)) {
2324 unusedScreens.remove(screenId);
2325 }
2326 }
2327
2328 // If there are any empty screens remove them, and update.
2329 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002330 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 }
2333
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 if (DEBUG_LOADERS) {
2335 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2336 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002338 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002340
Sunny Goyale2df0622015-04-24 11:27:00 -07002341 for (int i = 0; i < nScreens; i++) {
2342 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002343 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 line += " | ";
2345 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002346 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002347 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002348 if (x < screen.length && y < screen[x].length) {
2349 line += (screen[x][y] != null) ? "#" : ".";
2350 } else {
2351 line += "!";
2352 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 }
Joe Onorato36115782010-06-17 13:28:48 -04002354 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002356 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002357 }
Joe Onorato36115782010-06-17 13:28:48 -04002358 }
Adam Cohene25af792013-06-06 23:08:25 -07002359 }
2360
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002361 /**
2362 * Partially updates the item without any notification. Must be called on the worker thread.
2363 */
2364 private void updateItem(long itemId, ContentValues update) {
2365 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002366 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002367 update,
2368 BaseColumns._ID + "= ?",
2369 new String[]{Long.toString(itemId)});
2370 }
2371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 /** Filters the set of items who are directly or indirectly (via another container) on the
2373 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002374 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 ArrayList<ItemInfo> allWorkspaceItems,
2376 ArrayList<ItemInfo> currentScreenItems,
2377 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002378 // Purge any null ItemInfos
2379 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2380 while (iter.hasNext()) {
2381 ItemInfo i = iter.next();
2382 if (i == null) {
2383 iter.remove();
2384 }
2385 }
2386
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387 // Order the set of items by their containers first, this allows use to walk through the
2388 // list sequentially, build up a list of containers that are in the specified screen,
2389 // as well as all items in those containers.
2390 Set<Long> itemsOnScreen = new HashSet<Long>();
2391 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2392 @Override
2393 public int compare(ItemInfo lhs, ItemInfo rhs) {
2394 return (int) (lhs.container - rhs.container);
2395 }
2396 });
2397 for (ItemInfo info : allWorkspaceItems) {
2398 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002399 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 currentScreenItems.add(info);
2401 itemsOnScreen.add(info.id);
2402 } else {
2403 otherScreenItems.add(info);
2404 }
2405 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2406 currentScreenItems.add(info);
2407 itemsOnScreen.add(info.id);
2408 } else {
2409 if (itemsOnScreen.contains(info.container)) {
2410 currentScreenItems.add(info);
2411 itemsOnScreen.add(info.id);
2412 } else {
2413 otherScreenItems.add(info);
2414 }
2415 }
2416 }
2417 }
2418
2419 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002420 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421 ArrayList<LauncherAppWidgetInfo> appWidgets,
2422 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2423 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424
2425 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002426 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002428 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002429 currentScreenWidgets.add(widget);
2430 } else {
2431 otherScreenWidgets.add(widget);
2432 }
2433 }
2434 }
2435
2436 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002437 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002438 LongArrayMap<ItemInfo> itemsIdMap,
2439 LongArrayMap<FolderInfo> folders,
2440 LongArrayMap<FolderInfo> currentScreenFolders,
2441 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442
Sunny Goyale2df0622015-04-24 11:27:00 -07002443 int total = folders.size();
2444 for (int i = 0; i < total; i++) {
2445 long id = folders.keyAt(i);
2446 FolderInfo folder = folders.valueAt(i);
2447
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002449 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002451 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452 currentScreenFolders.put(id, folder);
2453 } else {
2454 otherScreenFolders.put(id, folder);
2455 }
2456 }
2457 }
2458
2459 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2460 * right) */
2461 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002462 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002463 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 // XXX: review this
2465 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002466 @Override
2467 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002468 int cellCountX = (int) profile.numColumns;
2469 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002470 int screenOffset = cellCountX * cellCountY;
2471 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002472 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002473 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002475 rhs.cellY * cellCountX + rhs.cellX);
2476 return (int) (lr - rr);
2477 }
2478 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002480
Adam Cohendcd297f2013-06-18 13:13:40 -07002481 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2482 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002483 final Runnable r = new Runnable() {
2484 @Override
2485 public void run() {
2486 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2487 if (callbacks != null) {
2488 callbacks.bindScreens(orderedScreens);
2489 }
2490 }
2491 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002492 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002493 }
2494
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2496 final ArrayList<ItemInfo> workspaceItems,
2497 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002498 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002500
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 final boolean postOnMainThread = (deferredBindRunnables != null);
2502
2503 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002504 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002506 final int start = i;
2507 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 final Runnable r = new Runnable() {
2509 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002511 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002512 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002513 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2514 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002515 }
2516 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002517 };
2518 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002519 synchronized (deferredBindRunnables) {
2520 deferredBindRunnables.add(r);
2521 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002522 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002523 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002524 }
Joe Onorato36115782010-06-17 13:28:48 -04002525 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002526
2527 // Bind the folders
2528 if (!folders.isEmpty()) {
2529 final Runnable r = new Runnable() {
2530 public void run() {
2531 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2532 if (callbacks != null) {
2533 callbacks.bindFolders(folders);
2534 }
2535 }
2536 };
2537 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002538 synchronized (deferredBindRunnables) {
2539 deferredBindRunnables.add(r);
2540 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002541 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002542 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 }
2544 }
2545
2546 // Bind the widgets, one at a time
2547 N = appWidgets.size();
2548 for (int i = 0; i < N; i++) {
2549 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2550 final Runnable r = new Runnable() {
2551 public void run() {
2552 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2553 if (callbacks != null) {
2554 callbacks.bindAppWidget(widget);
2555 }
2556 }
2557 };
2558 if (postOnMainThread) {
2559 deferredBindRunnables.add(r);
2560 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002561 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 }
2563 }
2564 }
2565
2566 /**
2567 * Binds all loaded data to actual views on the main thread.
2568 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002569 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002570 final long t = SystemClock.uptimeMillis();
2571 Runnable r;
2572
2573 // Don't use these two variables in any of the callback runnables.
2574 // Otherwise we hold a reference to them.
2575 final Callbacks oldCallbacks = mCallbacks.get();
2576 if (oldCallbacks == null) {
2577 // This launcher has exited and nobody bothered to tell us. Just bail.
2578 Log.w(TAG, "LoaderTask running with no launcher");
2579 return;
2580 }
2581
Winson Chung9b9fb962013-11-15 15:39:34 -08002582 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2584 ArrayList<LauncherAppWidgetInfo> appWidgets =
2585 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002586 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002587
2588 final LongArrayMap<FolderInfo> folders;
2589 final LongArrayMap<ItemInfo> itemsIdMap;
2590
Winson Chung2abf94d2012-07-18 18:16:38 -07002591 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 workspaceItems.addAll(sBgWorkspaceItems);
2593 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002594 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002595
2596 folders = sBgFolders.clone();
2597 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002598 }
2599
Derek Prothro7aff3992013-12-10 14:00:37 -05002600 final boolean isLoadingSynchronously =
2601 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002602 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002603 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002604 if (currScreen >= orderedScreenIds.size()) {
2605 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002606 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002607 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002608 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002609 final long currentScreenId = currentScreen < 0
2610 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002611
2612 // Load all the items that are on the current page first (and in the process, unbind
2613 // all the existing workspace items before we call startBinding() below.
2614 unbindWorkspaceItemsOnMainThread();
2615
2616 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002617 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2618 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2619 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2620 new ArrayList<LauncherAppWidgetInfo>();
2621 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2622 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002623 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2624 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002625
Winson Chung9b9fb962013-11-15 15:39:34 -08002626 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002628 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002630 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002631 otherFolders);
2632 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2633 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2634
2635 // Tell the workspace that we're about to start binding items
2636 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002637 public void run() {
2638 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2639 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002640 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002641 }
2642 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002644 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002645
Adam Cohendcd297f2013-06-18 13:13:40 -07002646 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2647
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648 // Load items on the current page
2649 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2650 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002651 if (isLoadingSynchronously) {
2652 r = new Runnable() {
2653 public void run() {
2654 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002655 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002656 callbacks.onPageBoundSynchronously(currentScreen);
2657 }
2658 }
2659 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002660 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002661 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662
Winson Chung4a2afa32012-07-19 14:53:05 -07002663 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2664 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002665 synchronized (mDeferredBindRunnables) {
2666 mDeferredBindRunnables.clear();
2667 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002669 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002670
2671 // Tell the workspace that we're done binding items
2672 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002673 public void run() {
2674 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2675 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002676 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002677 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678
Sunny Goyal639e9062015-08-19 19:17:06 -07002679 mIsLoadingAndBindingWorkspace = false;
2680
2681 // Run all the bind complete runnables after workspace is bound.
2682 if (!mBindCompleteRunnables.isEmpty()) {
2683 synchronized (mBindCompleteRunnables) {
2684 for (final Runnable r : mBindCompleteRunnables) {
2685 runOnWorkerThread(r);
2686 }
2687 mBindCompleteRunnables.clear();
2688 }
2689 }
2690
Winson Chung98e030b2012-05-07 16:01:11 -07002691 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002692 if (DEBUG_LOADERS) {
2693 Log.d(TAG, "bound workspace in "
2694 + (SystemClock.uptimeMillis()-t) + "ms");
2695 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002696
Joe Onorato36115782010-06-17 13:28:48 -04002697 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002698 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002699 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002700 synchronized (mDeferredBindRunnables) {
2701 mDeferredBindRunnables.add(r);
2702 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002703 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002704 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002705 }
Joe Onorato36115782010-06-17 13:28:48 -04002706 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002707
Joe Onorato36115782010-06-17 13:28:48 -04002708 private void loadAndBindAllApps() {
2709 if (DEBUG_LOADERS) {
2710 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2711 }
2712 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002713 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002714 synchronized (LoaderTask.this) {
2715 if (mStopped) {
2716 return;
2717 }
2718 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002719 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002720 synchronized (LoaderTask.this) {
2721 if (mStopped) {
2722 return;
2723 }
2724 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002725 }
Joe Onorato36115782010-06-17 13:28:48 -04002726 } else {
2727 onlyBindAllApps();
2728 }
2729 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002730
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002731 private void updateIconCache() {
2732 // Ignore packages which have a promise icon.
2733 HashSet<String> packagesToIgnore = new HashSet<>();
2734 synchronized (sBgLock) {
2735 for (ItemInfo info : sBgItemsIdMap) {
2736 if (info instanceof ShortcutInfo) {
2737 ShortcutInfo si = (ShortcutInfo) info;
2738 if (si.isPromise() && si.getTargetComponent() != null) {
2739 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2740 }
2741 } else if (info instanceof LauncherAppWidgetInfo) {
2742 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2743 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2744 packagesToIgnore.add(lawi.providerName.getPackageName());
2745 }
2746 }
2747 }
2748 }
2749 mIconCache.updateDbIcons(packagesToIgnore);
2750 }
2751
Joe Onorato36115782010-06-17 13:28:48 -04002752 private void onlyBindAllApps() {
2753 final Callbacks oldCallbacks = mCallbacks.get();
2754 if (oldCallbacks == null) {
2755 // This launcher has exited and nobody bothered to tell us. Just bail.
2756 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2757 return;
2758 }
2759
2760 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002761 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002762 final ArrayList<AppInfo> list
2763 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002764 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002765 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002766 public void run() {
2767 final long t = SystemClock.uptimeMillis();
2768 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2769 if (callbacks != null) {
2770 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002771 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002772 }
2773 if (DEBUG_LOADERS) {
2774 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2775 + (SystemClock.uptimeMillis()-t) + "ms");
2776 }
2777 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002778 };
2779 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002780 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002781 r.run();
2782 } else {
2783 mHandler.post(r);
2784 }
Joe Onorato36115782010-06-17 13:28:48 -04002785 }
2786
Winson Chung64359a52013-07-08 17:17:08 -07002787 private void loadAllApps() {
2788 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002789
Joe Onorato36115782010-06-17 13:28:48 -04002790 final Callbacks oldCallbacks = mCallbacks.get();
2791 if (oldCallbacks == null) {
2792 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002793 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002794 return;
2795 }
2796
Kenny Guyed131872014-04-30 03:02:21 +01002797 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2798
Winson Chung64359a52013-07-08 17:17:08 -07002799 // Clear the list of apps
2800 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002801 for (UserHandleCompat user : profiles) {
2802 // Query for the set of apps
2803 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002804 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002805 if (DEBUG_LOADERS) {
2806 Log.d(TAG, "getActivityList took "
2807 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2808 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2809 }
2810 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002811 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002812 if (apps == null || apps.isEmpty()) {
2813 return;
2814 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002815
Kenny Guyed131872014-04-30 03:02:21 +01002816 // Create the ApplicationInfos
2817 for (int i = 0; i < apps.size(); i++) {
2818 LauncherActivityInfoCompat app = apps.get(i);
2819 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002820 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002821 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002822
Sunny Goyal756a28a2015-04-23 17:07:55 -07002823 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2824 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002825 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002826
2827 @Override
2828 public void run() {
2829 heuristic.processUserApps(apps);
2830 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002831 };
2832 runOnMainThread(new Runnable() {
2833
2834 @Override
2835 public void run() {
2836 // Check isLoadingWorkspace on the UI thread, as it is updated on
2837 // the UI thread.
2838 if (mIsLoadingAndBindingWorkspace) {
2839 synchronized (mBindCompleteRunnables) {
2840 mBindCompleteRunnables.add(r);
2841 }
2842 } else {
2843 runOnWorkerThread(r);
2844 }
2845 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002846 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002847 }
Winson Chung64359a52013-07-08 17:17:08 -07002848 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002849 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002850 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2851 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002852
2853 // Post callback on main thread
2854 mHandler.post(new Runnable() {
2855 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002856
Winson Chung64359a52013-07-08 17:17:08 -07002857 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002858 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002859 if (callbacks != null) {
2860 callbacks.bindAllApplications(added);
2861 if (DEBUG_LOADERS) {
2862 Log.d(TAG, "bound " + added.size() + " apps in "
2863 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2864 }
2865 } else {
2866 Log.i(TAG, "not binding apps: no Launcher activity");
2867 }
2868 }
2869 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002870 // Cleanup any data stored for a deleted user.
2871 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002872
Sunny Goyal2d648b02015-08-11 13:56:28 -07002873 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002874 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002875 Log.d(TAG, "Icons processed in "
2876 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002877 }
2878 }
2879
2880 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002881 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002882 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002883 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2884 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2885 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2886 }
Joe Onorato36115782010-06-17 13:28:48 -04002887 }
2888 }
2889
Sunny Goyal75b0f552015-05-20 21:57:06 -07002890 /**
2891 * Called when the icons for packages have been updated in the icon cache.
2892 */
2893 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2894 final Callbacks callbacks = getCallback();
2895 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2896 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2897
2898 // If any package icon has changed (app was updated while launcher was dead),
2899 // update the corresponding shortcuts.
2900 synchronized (sBgLock) {
2901 for (ItemInfo info : sBgItemsIdMap) {
2902 if (info instanceof ShortcutInfo && user.equals(info.user)
2903 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2904 ShortcutInfo si = (ShortcutInfo) info;
2905 ComponentName cn = si.getTargetComponent();
2906 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2907 si.updateIcon(mIconCache);
2908 updatedShortcuts.add(si);
2909 }
2910 }
2911 }
2912 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2913 }
2914
2915 if (!updatedShortcuts.isEmpty()) {
2916 final UserHandleCompat userFinal = user;
2917 mHandler.post(new Runnable() {
2918
2919 public void run() {
2920 Callbacks cb = getCallback();
2921 if (cb != null && callbacks == cb) {
2922 cb.bindShortcutsChanged(updatedShortcuts,
2923 new ArrayList<ShortcutInfo>(), userFinal);
2924 }
2925 }
2926 });
2927 }
2928
2929 if (!updatedApps.isEmpty()) {
2930 mHandler.post(new Runnable() {
2931
2932 public void run() {
2933 Callbacks cb = getCallback();
2934 if (cb != null && callbacks == cb) {
2935 cb.bindAppsUpdated(updatedApps);
2936 }
2937 }
2938 });
2939 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002940
2941 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002942 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002943 }
2944
Joe Onorato36115782010-06-17 13:28:48 -04002945 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002946 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002947 }
2948
Adam Cohen091440a2015-03-18 14:16:05 -07002949 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002950
2951 @Override
2952 public void onReceive(Context context, Intent intent) {
2953 synchronized (sBgLock) {
2954 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2955 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002956 final PackageManager manager = context.getPackageManager();
2957 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2958 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002959 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2960 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002961 packagesRemoved.clear();
2962 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002963 for (String pkg : entry.getValue()) {
2964 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002965 boolean packageOnSdcard = launcherApps.isAppEnabled(
2966 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2967 if (packageOnSdcard) {
2968 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2969 packagesUnavailable.add(pkg);
2970 } else {
2971 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2972 packagesRemoved.add(pkg);
2973 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002974 }
2975 }
2976 if (!packagesRemoved.isEmpty()) {
2977 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2978 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2979 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002980 if (!packagesUnavailable.isEmpty()) {
2981 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2982 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2983 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002984 }
Sunny Goyal34942622014-08-29 17:20:55 -07002985 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002986 }
2987 }
2988 }
2989
Joe Onorato36115782010-06-17 13:28:48 -04002990 private class PackageUpdatedTask implements Runnable {
2991 int mOp;
2992 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002993 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002994
2995 public static final int OP_NONE = 0;
2996 public static final int OP_ADD = 1;
2997 public static final int OP_UPDATE = 2;
2998 public static final int OP_REMOVE = 3; // uninstlled
2999 public static final int OP_UNAVAILABLE = 4; // external media unmounted
3000
3001
Kenny Guyed131872014-04-30 03:02:21 +01003002 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003003 mOp = op;
3004 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003005 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003006 }
3007
3008 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003009 if (!mHasLoaderCompletedOnce) {
3010 // Loader has not yet run.
3011 return;
3012 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003013 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003014
3015 final String[] packages = mPackages;
3016 final int N = packages.length;
3017 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003018 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003019 for (int i=0; i<N; i++) {
3020 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003021 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003022 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003023 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003024
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003025 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3026 if (heuristic != null) {
3027 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003028 }
Joe Onorato36115782010-06-17 13:28:48 -04003029 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003030 }
Joe Onorato36115782010-06-17 13:28:48 -04003031 case OP_UPDATE:
3032 for (int i=0; i<N; i++) {
3033 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003034 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003035 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003036 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003037 }
3038 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003039 case OP_REMOVE: {
3040 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3041 if (heuristic != null) {
3042 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003043 }
Joe Onorato36115782010-06-17 13:28:48 -04003044 for (int i=0; i<N; i++) {
3045 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003046 mIconCache.removeIconsForPkg(packages[i], mUser);
3047 }
3048 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003049 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003050 case OP_UNAVAILABLE:
3051 for (int i=0; i<N; i++) {
3052 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3053 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003054 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003055 }
3056 break;
3057 }
3058
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003059 ArrayList<AppInfo> added = null;
3060 ArrayList<AppInfo> modified = null;
3061 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003062
Adam Cohen487f7dd2012-06-28 18:12:10 -07003063 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003064 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003065 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003066 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003067 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003068 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003069 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003070 }
Winson Chung5d55f332012-07-16 20:45:03 -07003071 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003072 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003073 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003074 }
3075
Sunny Goyale0f58d72014-11-10 18:05:31 -08003076 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003077 if (callbacks == null) {
3078 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3079 return;
3080 }
3081
Sunny Goyal4390ace2014-10-13 11:33:11 -07003082 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3083 new HashMap<ComponentName, AppInfo>();
3084
Joe Onorato36115782010-06-17 13:28:48 -04003085 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003086 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003087 for (AppInfo ai : added) {
3088 addedOrUpdatedApps.put(ai.componentName, ai);
3089 }
Joe Onorato36115782010-06-17 13:28:48 -04003090 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003091
Joe Onorato36115782010-06-17 13:28:48 -04003092 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003093 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094 for (AppInfo ai : modified) {
3095 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003096 }
3097
Joe Onorato36115782010-06-17 13:28:48 -04003098 mHandler.post(new Runnable() {
3099 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003100 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003101 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003102 callbacks.bindAppsUpdated(modifiedFinal);
3103 }
3104 }
3105 });
3106 }
Winson Chung83892cc2013-05-01 16:53:33 -07003107
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003109 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3111 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3112 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3113
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003115 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003116 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003117 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3118 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003119 boolean infoUpdated = false;
3120 boolean shortcutUpdated = false;
3121
3122 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003125 Bitmap icon = Utilities.createIconBitmap(
3126 si.iconResource.packageName,
3127 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003128 if (icon != null) {
3129 si.setIcon(icon);
3130 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003132 }
3133 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003134
3135 ComponentName cn = si.getTargetComponent();
3136 if (cn != null && packageSet.contains(cn.getPackageName())) {
3137 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3138
3139 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003140 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3141 // Auto install icon
3142 PackageManager pm = context.getPackageManager();
3143 ResolveInfo matched = pm.resolveActivity(
3144 new Intent(Intent.ACTION_MAIN)
3145 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3146 PackageManager.MATCH_DEFAULT_ONLY);
3147 if (matched == null) {
3148 // Try to find the best match activity.
3149 Intent intent = pm.getLaunchIntentForPackage(
3150 cn.getPackageName());
3151 if (intent != null) {
3152 cn = intent.getComponent();
3153 appInfo = addedOrUpdatedApps.get(cn);
3154 }
3155
3156 if ((intent == null) || (appInfo == null)) {
3157 removedShortcuts.add(si);
3158 continue;
3159 }
3160 si.promisedIntent = intent;
3161 }
3162 }
3163
3164 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003165 if (appInfo != null) {
3166 si.flags = appInfo.flags;
3167 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168
Sunny Goyal756adbc2015-04-16 15:20:51 -07003169 si.intent = si.promisedIntent;
3170 si.promisedIntent = null;
3171 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172 infoUpdated = true;
3173 si.updateIcon(mIconCache);
3174 }
3175
3176 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3177 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3178 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003179 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003180 si.contentDescription = appInfo.contentDescription;
3181 infoUpdated = true;
3182 }
3183
3184 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3185 // Since package was just updated, the target must be available now.
3186 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3187 shortcutUpdated = true;
3188 }
3189 }
3190
3191 if (infoUpdated || shortcutUpdated) {
3192 updatedShortcuts.add(si);
3193 }
3194 if (infoUpdated) {
3195 updateItemInDatabase(context, si);
3196 }
3197 } else if (info instanceof LauncherAppWidgetInfo) {
3198 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3199 if (mUser.equals(widgetInfo.user)
3200 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3201 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003202 widgetInfo.restoreStatus &=
3203 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3204 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003205
3206 // adding this flag ensures that launcher shows 'click to setup'
3207 // if the widget has a config activity. In case there is no config
3208 // activity, it will be marked as 'restored' during bind.
3209 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3210
Sunny Goyal4390ace2014-10-13 11:33:11 -07003211 widgets.add(widgetInfo);
3212 updateItemInDatabase(context, widgetInfo);
3213 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003214 }
3215 }
3216 }
3217
Sunny Goyal4390ace2014-10-13 11:33:11 -07003218 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3219 mHandler.post(new Runnable() {
3220
3221 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003222 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003223 if (callbacks == cb && cb != null) {
3224 callbacks.bindShortcutsChanged(
3225 updatedShortcuts, removedShortcuts, mUser);
3226 }
3227 }
3228 });
3229 if (!removedShortcuts.isEmpty()) {
3230 deleteItemsFromDatabase(context, removedShortcuts);
3231 }
3232 }
3233 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003234 mHandler.post(new Runnable() {
3235 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003236 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003237 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003238 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003239 }
3240 }
3241 });
3242 }
3243 }
3244
Winson Chungdf95eb12013-10-16 14:57:07 -07003245 final ArrayList<String> removedPackageNames =
3246 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003247 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 // Mark all packages in the broadcast to be removed
3249 removedPackageNames.addAll(Arrays.asList(packages));
3250 } else if (mOp == OP_UPDATE) {
3251 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003252 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003253 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003254 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003255 }
3256 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003257 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003258
Winson Chungdf95eb12013-10-16 14:57:07 -07003259 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003260 final int removeReason;
3261 if (mOp == OP_UNAVAILABLE) {
3262 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3263 } else {
3264 // Remove all the components associated with this package
3265 for (String pn : removedPackageNames) {
3266 deletePackageFromDatabase(context, pn, mUser);
3267 }
3268 // Remove all the specific components
3269 for (AppInfo a : removedApps) {
3270 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3271 deleteItemsFromDatabase(context, infos);
3272 }
3273 removeReason = 0;
3274 }
3275
Winson Chungdf95eb12013-10-16 14:57:07 -07003276 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003277 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003278 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003279 mHandler.post(new Runnable() {
3280 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003281 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003282 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003283 callbacks.bindComponentsRemoved(
3284 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003285 }
3286 }
3287 });
Joe Onoratobe386092009-11-17 17:32:16 -08003288 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003289
Sunny Goyal2d648b02015-08-11 13:56:28 -07003290 // Update widgets
3291 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3292 // Always refresh for a package event on secondary user
3293 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3294
3295 // Refresh widget list, if the package already had a widget.
3296 synchronized (sBgLock) {
3297 if (sBgWidgetProviders != null) {
3298 HashSet<String> pkgSet = new HashSet<>();
3299 Collections.addAll(pkgSet, mPackages);
3300
3301 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3302 needToRefresh |= key.user.equals(mUser) &&
3303 pkgSet.contains(key.componentName.getPackageName());
3304 }
3305 }
3306 }
3307
3308 if (!needToRefresh && mOp != OP_REMOVE) {
3309 // Refresh widget list, if there is any newly added widget
3310 PackageManager pm = context.getPackageManager();
3311 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003312 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003313 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003314 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003315 .setPackage(pkg), 0);
3316 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003317 } catch (RuntimeException e) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003318 if (LauncherAppState.isDogfoodBuild()) {
3319 throw e;
3320 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003321 // Ignore the crash. We can live with a state widget list.
3322 Log.e(TAG, "PM call failed for " + pkg, e);
3323 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003324 }
3325 }
3326
3327 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3328 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003329
Adam Cohen4caf2982013-08-20 18:54:31 -07003330 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003331 mHandler.post(new Runnable() {
3332 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003333 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003334 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003335 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003336 }
3337 }
3338 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 }
3340 }
3341
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003342 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3343 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003344 ArrayList<LauncherAppWidgetProviderInfo> results =
3345 new ArrayList<LauncherAppWidgetProviderInfo>();
3346 try {
3347 synchronized (sBgLock) {
3348 if (sBgWidgetProviders == null || refresh) {
3349 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3350 = new HashMap<>();
3351 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3352 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003353
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003354 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3355 for (AppWidgetProviderInfo pInfo : widgets) {
3356 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3357 UserHandleCompat user = wm.getUser(info);
3358 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3359 }
Robin Lee26ace122015-03-16 19:41:43 +00003360
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003361 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3362 for (CustomAppWidget widget : customWidgets) {
3363 info = new LauncherAppWidgetProviderInfo(context, widget);
3364 UserHandleCompat user = wm.getUser(info);
3365 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3366 }
3367 // Replace the global list at the very end, so that if there is an exception,
3368 // previously loaded provider list is used.
3369 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003370 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003371 results.addAll(sBgWidgetProviders.values());
3372 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003373 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003374 } catch (Exception e) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003375 if (!LauncherAppState.isDogfoodBuild() &&
3376 (e.getCause() instanceof TransactionTooLargeException ||
3377 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003378 // the returned value may be incomplete and will not be refreshed until the next
3379 // time Launcher starts.
3380 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3381 // onResume is called to refresh the widget provider list.
3382 synchronized (sBgLock) {
3383 if (sBgWidgetProviders != null) {
3384 results.addAll(sBgWidgetProviders.values());
3385 }
3386 return results;
3387 }
3388 } else {
3389 throw e;
3390 }
Adam Cohen59400422014-03-05 18:07:04 -08003391 }
3392 }
3393
Robin Lee26ace122015-03-16 19:41:43 +00003394 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3395 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003396 synchronized (sBgLock) {
3397 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003398 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003399 }
Robin Lee26ace122015-03-16 19:41:43 +00003400 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003401 }
3402 }
3403
Sunny Goyal2d648b02015-08-11 13:56:28 -07003404 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003405
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003406 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003407 @Override
3408 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003409 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003410 final WidgetsModel model = mBgWidgetsModel.clone();
3411
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003412 mHandler.post(new Runnable() {
3413 @Override
3414 public void run() {
3415 Callbacks cb = getCallback();
3416 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003417 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003418 }
3419 }
3420 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003421 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003422 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3423 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003424 }
3425 });
3426 }
3427
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003428 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003429 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003430 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003431 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003432 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003433 @Thunk void updateWidgetsModel(boolean refresh) {
3434 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003435 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003436 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003437
3438 // Update shortcut providers
3439 synchronized (sBgLock) {
3440 try {
3441 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3442 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3443 sBgShortcutProviders = providers;
3444 } catch (RuntimeException e) {
3445 if (!LauncherAppState.isDogfoodBuild() &&
3446 (e.getCause() instanceof TransactionTooLargeException ||
3447 e.getCause() instanceof DeadObjectException)) {
3448 /**
3449 * Ignore exception and use the cached list if available.
3450 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3451 */
3452 } else {
3453 throw e;
3454 }
3455 }
3456 if (sBgShortcutProviders != null) {
3457 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3458 }
3459 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003460 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003461 }
3462
Adam Cohen091440a2015-03-18 14:16:05 -07003463 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003464 UserHandleCompat user) {
3465 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3466 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003467 }
Adam Cohen556f6132014-01-15 15:18:08 -08003468
Kenny Guyed131872014-04-30 03:02:21 +01003469 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3470 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003471 if (cn == null) {
3472 return false;
3473 }
Kenny Guyed131872014-04-30 03:02:21 +01003474 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3475 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003476 return false;
3477 }
Kenny Guyed131872014-04-30 03:02:21 +01003478 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003479 }
3480
Adam Cohena28b78e2014-05-20 17:03:04 -07003481 public static boolean isValidPackage(Context context, String packageName,
3482 UserHandleCompat user) {
3483 if (packageName == null) {
3484 return false;
3485 }
3486 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3487 return launcherApps.isPackageEnabledForProfile(packageName, user);
3488 }
3489
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003491 * Make an ShortcutInfo object for a restored application or shortcut item that points
3492 * to a package that is not yet installed on the system.
3493 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003494 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003495 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003496 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003497 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003498
3499 Bitmap icon = iconInfo.loadIcon(c, info, context);
3500 // the fallback icon
3501 if (icon == null) {
3502 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3503 } else {
3504 info.setIcon(icon);
3505 }
Sunny Goyal34942622014-08-29 17:20:55 -07003506
3507 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003508 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003509 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003510 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003511 }
Sunny Goyal34942622014-08-29 17:20:55 -07003512 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3513 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003514 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003515 }
Sunny Goyal34942622014-08-29 17:20:55 -07003516 } else {
3517 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3518 }
3519
Winson Chung82b016c2015-05-08 17:00:10 -07003520 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003521 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003522 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003523 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003524 return info;
3525 }
3526
3527 /**
3528 * Make an Intent object for a restored application or shortcut item that points
3529 * to the market page for the item.
3530 */
Adam Cohen091440a2015-03-18 14:16:05 -07003531 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003532 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003533 return getMarketIntent(componentName.getPackageName());
3534 }
3535
3536 static Intent getMarketIntent(String packageName) {
3537 return new Intent(Intent.ACTION_VIEW)
3538 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003539 .scheme("market")
3540 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003541 .appendQueryParameter("id", packageName)
3542 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003543 }
3544
3545 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003546 * Make an ShortcutInfo object for a shortcut that is an application.
3547 *
3548 * If c is not null, then it will be used to fill in missing data like the title and icon.
3549 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003550 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003551 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003552 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003553 if (user == null) {
3554 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003555 return null;
3556 }
3557
Kenny Guyed131872014-04-30 03:02:21 +01003558 ComponentName componentName = intent.getComponent();
3559 if (componentName == null) {
3560 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3561 return null;
3562 }
3563
3564 Intent newIntent = new Intent(intent.getAction(), null);
3565 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3566 newIntent.setComponent(componentName);
3567 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003568 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003569 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3570 return null;
3571 }
3572
3573 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003574 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003575 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3576 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3577 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003578 }
3579
Joe Onorato56d82912010-03-07 14:32:10 -05003580 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003581 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003582 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003583 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003584
Joe Onorato56d82912010-03-07 14:32:10 -05003585 // fall back to the class name of the activity
3586 if (info.title == null) {
3587 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003588 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003589
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003591 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003592 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003593 if (lai != null) {
3594 info.flags = AppInfo.initFlags(lai);
3595 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003597 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003598
Sunny Goyale2df0622015-04-24 11:27:00 -07003599 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003600 ItemInfoFilter f) {
3601 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3602 for (ItemInfo i : infos) {
3603 if (i instanceof ShortcutInfo) {
3604 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003605 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003606 if (cn != null && f.filterItem(null, info, cn)) {
3607 filtered.add(info);
3608 }
3609 } else if (i instanceof FolderInfo) {
3610 FolderInfo info = (FolderInfo) i;
3611 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003612 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003613 if (cn != null && f.filterItem(info, s, cn)) {
3614 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003615 }
3616 }
Winson Chung64359a52013-07-08 17:17:08 -07003617 } else if (i instanceof LauncherAppWidgetInfo) {
3618 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3619 ComponentName cn = info.providerName;
3620 if (cn != null && f.filterItem(null, info, cn)) {
3621 filtered.add(info);
3622 }
Winson Chung8a435102012-08-30 17:16:53 -07003623 }
3624 }
Winson Chung64359a52013-07-08 17:17:08 -07003625 return new ArrayList<ItemInfo>(filtered);
3626 }
3627
Adam Cohen091440a2015-03-18 14:16:05 -07003628 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003629 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003630 ItemInfoFilter filter = new ItemInfoFilter() {
3631 @Override
3632 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003633 if (info.user == null) {
3634 return cn.equals(cname);
3635 } else {
3636 return cn.equals(cname) && info.user.equals(user);
3637 }
Winson Chung64359a52013-07-08 17:17:08 -07003638 }
3639 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003640 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003641 }
3642
Sunny Goyal1a745e82014-10-02 15:58:31 -07003643 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003644 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003645 */
Adam Cohen091440a2015-03-18 14:16:05 -07003646 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003647 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003648 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003649 // Non-app shortcuts are only supported for current user.
3650 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003652
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003653 // TODO: If there's an explicit component and we can't install that, delete it.
3654
Winson Chung82b016c2015-05-08 17:00:10 -07003655 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003656
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003657 Bitmap icon = iconInfo.loadIcon(c, info, context);
3658 // the fallback icon
3659 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003660 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003661 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003662 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003663 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 return info;
3665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666
Sunny Goyal2350bc92014-10-14 16:42:54 -07003667 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003668 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3669 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3670 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3671
Adam Cohend9198822011-11-22 16:42:47 -08003672 if (intent == null) {
3673 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3674 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3675 return null;
3676 }
3677
Joe Onorato0589f0f2010-02-08 13:44:00 -08003678 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003679 boolean customIcon = false;
3680 ShortcutIconResource iconResource = null;
3681
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003682 if (bitmap instanceof Bitmap) {
3683 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003684 customIcon = true;
3685 } else {
3686 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003687 if (extra instanceof ShortcutIconResource) {
3688 iconResource = (ShortcutIconResource) extra;
3689 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003690 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003691 }
3692 }
3693
Michael Jurkac9d95c52011-08-29 14:03:34 -07003694 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003695
Kenny Guyed131872014-04-30 03:02:21 +01003696 // Only support intents for current user for now. Intents sent from other
3697 // users wouldn't get here without intent forwarding anyway.
3698 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003699 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003700 icon = mIconCache.getDefaultIcon(info.user);
3701 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003702 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003703 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003704
Winson Chung82b016c2015-05-08 17:00:10 -07003705 info.title = Utilities.trim(name);
3706 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003707 info.intent = intent;
3708 info.customIcon = customIcon;
3709 info.iconResource = iconResource;
3710
3711 return info;
3712 }
3713
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003715 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003716 * or make a new one.
3717 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003718 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 // See if a placeholder was created for us already
3720 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003721 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003723 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 folders.put(id, folderInfo);
3725 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003726 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727 }
3728
Joe Onoratobe386092009-11-17 17:32:16 -08003729
Sunny Goyal651077b2014-06-30 14:15:31 -07003730 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3731 return (provider != null) && (provider.provider != null)
3732 && (provider.provider.getPackageName() != null);
3733 }
3734
Joe Onoratobe386092009-11-17 17:32:16 -08003735 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003736 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003737 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3738 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3739 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3740 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003741 if (mLoaderTask != null) {
3742 mLoaderTask.dumpState();
3743 } else {
3744 Log.d(TAG, "mLoaderTask=null");
3745 }
Joe Onoratobe386092009-11-17 17:32:16 -08003746 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003747
3748 public Callbacks getCallback() {
3749 return mCallbacks != null ? mCallbacks.get() : null;
3750 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003751
3752 /**
3753 * @return {@link FolderInfo} if its already loaded.
3754 */
3755 public FolderInfo findFolderById(Long folderId) {
3756 synchronized (sBgLock) {
3757 return sBgFolders.get(folderId);
3758 }
3759 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003760
3761 /**
3762 * @return the looper for the worker thread which can be used to start background tasks.
3763 */
3764 public static Looper getWorkerLooper() {
3765 return sWorkerThread.getLooper();
3766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003767}