blob: a132e919ae571183fa0621e0a95230ef71bd768b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.ContentProviderClient;
24import 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;
Reena Lee93f824a2011-09-23 17:20:28 -070034import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Resources;
36import android.database.Cursor;
37import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.net.Uri;
Hyunyoung Songd4af1482015-04-20 20:40:03 -070039import android.os.Build;
Joe Onorato17a89222011-02-08 17:26:11 -080040import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040041import android.os.Handler;
42import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070043import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070048import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040049import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080050import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080052import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070053import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010054
Sunny Goyalffe83f12014-08-14 17:39:34 -070055import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.LauncherActivityInfoCompat;
57import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070058import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070059import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010060import com.android.launcher3.compat.UserHandleCompat;
61import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000063import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -070064import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070065import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070066import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080067
Michael Jurkac2f801e2011-07-12 14:19:46 -070068import java.lang.ref.WeakReference;
69import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070070import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070072import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070073import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.util.Collections;
75import java.util.Comparator;
76import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070077import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070078import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070080import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083/**
84 * Maintains in-memory state of the Launcher. It is expected that there should be only one
85 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070086 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 */
Kenny Guyed131872014-04-30 03:02:21 +010088public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010089 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080090 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040091 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070092 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040093
Joe Onorato9c1289c2009-08-17 11:03:03 -040094 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070095
Dan Sandlerd5024042014-01-09 15:01:33 -050096 public static final int LOADER_FLAG_NONE = 0;
97 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
98 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
99
Joe Onorato36115782010-06-17 13:28:48 -0400100 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500101 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800104 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final LauncherAppState mApp;
107 @Thunk final Object mLock = new Object();
108 @Thunk DeferredHandler mHandler = new DeferredHandler();
109 @Thunk LoaderTask mLoaderTask;
110 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700111 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Jason Monkbbe1e242014-05-16 17:37:34 -0400113 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700114
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700116 static {
117 sWorkerThread.start();
118 }
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 // We start off with everything not loaded. After that, we assume that
122 // our monitoring of the package manager provides all updates and we never
123 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk boolean mWorkspaceLoaded;
125 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700126
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700127 // When we are loading pages synchronously, we can't just post the binding of items on the side
128 // pages as this delays the rotation process. Instead, we wait for a callback from the first
129 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
130 // a normal load, we also clear this set of Runnables.
131 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700142 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
144 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700146 // The lock that must be acquired before referencing any static bg data structures. Unlike
147 // other locks, this one can generally be held long-term because we never expect any of these
148 // static data structures to be referenced outside of the worker thread except on the first
149 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700150 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700154 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
157 // created by LauncherModel that are directly on the home screen (however, no widgets or
158 // shortcuts within folders).
159 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
162 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163 new ArrayList<LauncherAppWidgetInfo>();
164
Adam Cohen487f7dd2012-06-28 18:12:10 -0700165 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700166 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700167
Adam Cohendcd297f2013-06-18 13:13:40 -0700168 // sBgWorkspaceScreens is the ordered set of workspace screens.
169 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
170
Adam Cohen59400422014-03-05 18:07:04 -0800171 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000172 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800173
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700175 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
176 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700177
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700178 // </ only access in worker thread >
179
Adam Cohen091440a2015-03-18 14:16:05 -0700180 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Reena Lee99a73f32011-10-24 17:27:37 -0700182 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700183
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);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100211 public void bindSearchablesChanged();
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);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 mIconCache = iconCache;
246
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400247 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700248 Configuration config = res.getConfiguration();
249 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100250 mLauncherApps = LauncherAppsCompat.getInstance(context);
251 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 }
253
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700254 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
255 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700256 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700257 if (sWorkerThread.getThreadId() == Process.myTid()) {
258 // If we are on the worker thread, post onto the main handler
259 mHandler.post(r);
260 } else {
261 r.run();
262 }
263 }
264
265 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
266 * posted on the worker thread handler. */
267 private static void runOnWorkerThread(Runnable r) {
268 if (sWorkerThread.getThreadId() == Process.myTid()) {
269 r.run();
270 } else {
271 // If we are not on the worker thread, then post to the worker handler
272 sWorker.post(r);
273 }
274 }
275
Sunny Goyal756a28a2015-04-23 17:07:55 -0700276 /**
277 * Runs the specified runnable after the loader is complete
278 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700279 @Thunk void runAfterBindCompletes(Runnable r) {
Sunny Goyal756a28a2015-04-23 17:07:55 -0700280 if (isLoadingWorkspace() || !mHasLoaderCompletedOnce) {
281 synchronized (mBindCompleteRunnables) {
282 mBindCompleteRunnables.add(r);
283 }
284 } else {
285 runOnWorkerThread(r);
286 }
287 }
288
Winson Chunge43a1e72014-01-15 10:33:02 -0800289 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
290 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800291 }
292
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 public void setPackageState(final PackageInstallInfo installInfo) {
294 Runnable updateRunnable = new Runnable() {
295
296 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500297 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700298 synchronized (sBgLock) {
299 final HashSet<ItemInfo> updates = new HashSet<>();
300
301 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
302 // Ignore install success events as they are handled by Package add events.
303 return;
304 }
305
Sunny Goyale2df0622015-04-24 11:27:00 -0700306 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700307 if (info instanceof ShortcutInfo) {
308 ShortcutInfo si = (ShortcutInfo) info;
309 ComponentName cn = si.getTargetComponent();
310 if (si.isPromise() && (cn != null)
311 && installInfo.packageName.equals(cn.getPackageName())) {
312 si.setInstallProgress(installInfo.progress);
313
314 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
315 // Mark this info as broken.
316 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
317 }
318 updates.add(si);
319 }
320 }
321 }
322
323 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
324 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
325 widget.installProgress = installInfo.progress;
326 updates.add(widget);
327 }
328 }
329
330 if (!updates.isEmpty()) {
331 // Push changes to the callback.
332 Runnable r = new Runnable() {
333 public void run() {
334 Callbacks callbacks = getCallback();
335 if (callbacks != null) {
336 callbacks.bindRestoreItemsChange(updates);
337 }
338 }
339 };
340 mHandler.post(r);
341 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500342 }
343 }
344 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700345 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500346 }
347
Sunny Goyal756adbc2015-04-16 15:20:51 -0700348 /**
349 * Updates the icons and label of all pending icons for the provided package name.
350 */
351 public void updateSessionDisplayInfo(final String packageName) {
352 Runnable updateRunnable = new Runnable() {
353
354 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700355 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700356 synchronized (sBgLock) {
357 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
358 final UserHandleCompat user = UserHandleCompat.myUserHandle();
359
Sunny Goyale2df0622015-04-24 11:27:00 -0700360 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700361 if (info instanceof ShortcutInfo) {
362 ShortcutInfo si = (ShortcutInfo) info;
363 ComponentName cn = si.getTargetComponent();
364 if (si.isPromise() && (cn != null)
365 && packageName.equals(cn.getPackageName())) {
366 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
367 // For auto install apps update the icon as well as label.
368 mIconCache.getTitleAndIcon(si,
369 si.promisedIntent, user,
370 si.shouldUseLowResIcon());
371 } else {
372 // Only update the icon for restored apps.
373 si.updateIcon(mIconCache);
374 }
375 updates.add(si);
376 }
377 }
378 }
379
380 if (!updates.isEmpty()) {
381 // Push changes to the callback.
382 Runnable r = new Runnable() {
383 public void run() {
384 Callbacks callbacks = getCallback();
385 if (callbacks != null) {
386 callbacks.bindShortcutsChanged(updates,
387 new ArrayList<ShortcutInfo>(), user);
388 }
389 }
390 };
391 mHandler.post(r);
392 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700393 }
394 }
395 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700396 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700397 }
398
Adam Cohen76a47a12014-02-05 11:47:43 -0800399 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800400 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800401
402 if (allAppsApps == null) {
403 throw new RuntimeException("allAppsApps must not be null");
404 }
405 if (allAppsApps.isEmpty()) {
406 return;
407 }
408
409 // Process the newly added applications and add them to the database first
410 Runnable r = new Runnable() {
411 public void run() {
412 runOnMainThread(new Runnable() {
413 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800414 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800415 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500416 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800417 }
418 }
419 });
420 }
421 };
422 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700423 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800424
Sunny Goyala9116722015-04-29 13:55:58 -0700425 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426 int[] xy, int spanX, int spanY) {
427 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700428 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
429 final int xCount = (int) profile.numColumns;
430 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 boolean[][] occupied = new boolean[xCount][yCount];
432 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700433 for (ItemInfo r : occupiedPos) {
434 int right = r.cellX + r.spanX;
435 int bottom = r.cellY + r.spanY;
436 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
437 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 occupied[x][y] = true;
439 }
440 }
441 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800442 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700443 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444 }
445
446 /**
447 * Find a position on the screen for the given size or adds a new screen.
448 * @return screenId and the coordinates for the item.
449 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700450 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 ArrayList<Long> workspaceScreens,
453 ArrayList<Long> addedWorkspaceScreensFinal,
454 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700455 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800456
Sunny Goyala9116722015-04-29 13:55:58 -0700457 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700458 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700459 synchronized (sBgLock) {
460 for (ItemInfo info : sBgItemsIdMap) {
461 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
462 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
463 if (items == null) {
464 items = new ArrayList<>();
465 screenItems.put(info.screenId, items);
466 }
467 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800469 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800470 }
471
472 // Find appropriate space for the item.
473 long screenId = 0;
474 int[] cordinates = new int[2];
475 boolean found = false;
476
477 int screenCount = workspaceScreens.size();
478 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700479 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480 if (preferredScreenIndex < screenCount) {
481 screenId = workspaceScreens.get(preferredScreenIndex);
482 found = findNextAvailableIconSpaceInScreen(
483 screenItems.get(screenId), cordinates, spanX, spanY);
484 }
485
486 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700487 // Search on any of the screens starting from the first screen.
488 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800489 screenId = workspaceScreens.get(screen);
490 if (findNextAvailableIconSpaceInScreen(
491 screenItems.get(screenId), cordinates, spanX, spanY)) {
492 // We found a space for it
493 found = true;
494 break;
495 }
496 }
497 }
498
499 if (!found) {
500 // Still no position found. Add a new screen to the end.
501 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
502
503 // Save the screen id for binding in the workspace
504 workspaceScreens.add(screenId);
505 addedWorkspaceScreensFinal.add(screenId);
506
507 // If we still can't find an empty space, then God help us all!!!
508 if (!findNextAvailableIconSpaceInScreen(
509 screenItems.get(screenId), cordinates, spanX, spanY)) {
510 throw new RuntimeException("Can't find space to add the item");
511 }
512 }
513 return Pair.create(screenId, cordinates);
514 }
515
516 /**
517 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700519 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700520 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800521 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800522 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700523 return;
Winson Chung997a9232013-07-24 15:33:46 -0700524 }
Winson Chung64359a52013-07-08 17:17:08 -0700525 // Process the newly added applications and add them to the database first
526 Runnable r = new Runnable() {
527 public void run() {
528 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
529 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
530
Winson Chung76828c82013-08-19 15:43:29 -0700531 // Get the list of workspace screens. We need to append to this list and
532 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
533 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800534 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700535 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800536 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700537 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800538 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700539 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800540 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700541 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800542 }
Winson Chung76828c82013-08-19 15:43:29 -0700543
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800544 // Find appropriate space for the item.
Sunny Goyala9116722015-04-29 13:55:58 -0700545 Pair<Long, int[]> coords = findSpaceForItem(context,
546 workspaceScreens, addedWorkspaceScreensFinal,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800547 1, 1);
548 long screenId = coords.first;
549 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700550
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700551 ItemInfo itemInfo;
552 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
553 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800554 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700555 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700556 } else {
557 throw new RuntimeException("Unexpected info type");
558 }
Winson Chung94d67682013-09-25 16:29:40 -0700559
Winson Chung64359a52013-07-08 17:17:08 -0700560 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700561 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700562 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700563 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700564 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700565 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700566 }
567 }
568
Winson Chung76828c82013-08-19 15:43:29 -0700569 // Update the workspace screens
570 updateWorkspaceScreenOrder(context, workspaceScreens);
571
Adam Cohen76a47a12014-02-05 11:47:43 -0800572 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700573 runOnMainThread(new Runnable() {
574 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800575 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700576 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700577 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
578 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700579 if (!addedShortcutsFinal.isEmpty()) {
580 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
581 long lastScreenId = info.screenId;
582 for (ItemInfo i : addedShortcutsFinal) {
583 if (i.screenId == lastScreenId) {
584 addAnimated.add(i);
585 } else {
586 addNotAnimated.add(i);
587 }
Winson Chung997a9232013-07-24 15:33:46 -0700588 }
589 }
Winson Chungd64d1762013-08-20 14:37:16 -0700590 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800591 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700592 }
Winson Chung64359a52013-07-08 17:17:08 -0700593 }
Winson Chung997a9232013-07-24 15:33:46 -0700594 });
595 }
Winson Chung64359a52013-07-08 17:17:08 -0700596 }
597 };
598 runOnWorkerThread(r);
599 }
600
Sunny Goyald33860f2015-04-23 16:02:20 -0700601 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700602 if (sWorkerThread.getThreadId() == Process.myTid()) {
603 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
604 "main thread");
605 }
606
607 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400608 synchronized (mDeferredBindRunnables) {
609 mDeferredBindRunnables.clear();
610 }
Sunny Goyald33860f2015-04-23 16:02:20 -0700611
612 // Remove any queued UI runnables
613 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700614 // Unbind all the workspace items
615 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700616 }
617
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700618 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700619 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700620 // Ensure that we don't use the same workspace items data structure on the main thread
621 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700622 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700623 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700624 tmpItems.addAll(sBgWorkspaceItems);
625 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700626 }
627 Runnable r = new Runnable() {
628 @Override
629 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700630 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700631 item.unbind();
632 }
633 }
634 };
635 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700636 }
637
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 /**
639 * Adds an item to the DB if it was not created previously, or move it to a new
640 * <container, screen, cellX, cellY>
641 */
642 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 if (item.container == ItemInfo.NO_ID) {
645 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700646 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647 } else {
648 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
651 }
652
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700653 static void checkItemInfoLocked(
654 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
655 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
656 if (modelItem != null && item != modelItem) {
657 // check all the data is consistent
658 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
659 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
660 ShortcutInfo shortcut = (ShortcutInfo) item;
661 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
662 modelShortcut.intent.filterEquals(shortcut.intent) &&
663 modelShortcut.id == shortcut.id &&
664 modelShortcut.itemType == shortcut.itemType &&
665 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700666 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700667 modelShortcut.cellX == shortcut.cellX &&
668 modelShortcut.cellY == shortcut.cellY &&
669 modelShortcut.spanX == shortcut.spanX &&
670 modelShortcut.spanY == shortcut.spanY &&
671 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
672 (modelShortcut.dropPos != null &&
673 shortcut.dropPos != null &&
674 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
675 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
676 // For all intents and purposes, this is the same object
677 return;
678 }
679 }
680
681 // the modelItem needs to match up perfectly with item if our model is
682 // to be consistent with the database-- for now, just require
683 // modelItem == item or the equality check above
684 String msg = "item: " + ((item != null) ? item.toString() : "null") +
685 "modelItem: " +
686 ((modelItem != null) ? modelItem.toString() : "null") +
687 "Error: ItemInfo passed to checkItemInfo doesn't match original";
688 RuntimeException e = new RuntimeException(msg);
689 if (stackTrace != null) {
690 e.setStackTrace(stackTrace);
691 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800692 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700693 }
694 }
695
Michael Jurka816474f2012-06-25 14:49:02 -0700696 static void checkItemInfo(final ItemInfo item) {
697 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
698 final long itemId = item.id;
699 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700700 public void run() {
701 synchronized (sBgLock) {
702 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700703 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700704 }
705 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700706 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700707 }
708
Michael Jurkac9d95c52011-08-29 14:03:34 -0700709 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
710 final ItemInfo item, final String callingFunction) {
711 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700712 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700713 final ContentResolver cr = context.getContentResolver();
714
Adam Cohen487f7dd2012-06-28 18:12:10 -0700715 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700716 Runnable r = new Runnable() {
717 public void run() {
718 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700719 updateItemArrays(item, itemId, stackTrace);
720 }
721 };
722 runOnWorkerThread(r);
723 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
726 final ArrayList<ItemInfo> items, final String callingFunction) {
727 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700728
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
730 Runnable r = new Runnable() {
731 public void run() {
732 ArrayList<ContentProviderOperation> ops =
733 new ArrayList<ContentProviderOperation>();
734 int count = items.size();
735 for (int i = 0; i < count; i++) {
736 ItemInfo item = items.get(i);
737 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700738 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700739 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700740
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700741 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
742 updateItemArrays(item, itemId, stackTrace);
743
744 }
745 try {
746 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
747 } catch (Exception e) {
748 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700749 }
750 }
751 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700752 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700753 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700754
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700755 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
756 // Lock on mBgLock *after* the db operation
757 synchronized (sBgLock) {
758 checkItemInfoLocked(itemId, item, stackTrace);
759
760 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
761 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
762 // Item is in a folder, make sure this folder exists
763 if (!sBgFolders.containsKey(item.container)) {
764 // An items container is being set to a that of an item which is not in
765 // the list of Folders.
766 String msg = "item: " + item + " container being set to: " +
767 item.container + ", not in the list of folders";
768 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700769 }
770 }
771
772 // Items are added/removed from the corresponding FolderInfo elsewhere, such
773 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
774 // that are on the desktop, as appropriate
775 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800776 if (modelItem != null &&
777 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
778 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700779 switch (modelItem.itemType) {
780 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
781 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
782 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
783 if (!sBgWorkspaceItems.contains(modelItem)) {
784 sBgWorkspaceItems.add(modelItem);
785 }
786 break;
787 default:
788 break;
789 }
790 } else {
791 sBgWorkspaceItems.remove(modelItem);
792 }
793 }
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700798 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700799 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 item.cellX = cellX;
803 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700804
Winson Chung3d503fb2011-07-13 17:25:49 -0700805 // We store hotseat items in canonical form which is this orientation invariant position
806 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700808 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700809 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700811 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813
814 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700816 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
817 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800818 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820
Michael Jurkac9d95c52011-08-29 14:03:34 -0700821 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700822 }
823
824 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
826 * cellX, cellY have already been updated on the ItemInfos.
827 */
828 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
829 final long container, final int screen) {
830
831 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
832 int count = items.size();
833
834 for (int i = 0; i < count; i++) {
835 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836 item.container = container;
837
838 // We store hotseat items in canonical form which is this orientation invariant position
839 // in the hotseat
840 if (context instanceof Launcher && screen < 0 &&
841 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700843 item.cellY);
844 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700846 }
847
848 final ContentValues values = new ContentValues();
849 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
850 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
851 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800852 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700854
855 contentValues.add(values);
856 }
857 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
858 }
859
860 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700861 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800862 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700865 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800866 item.cellX = cellX;
867 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700868 item.spanX = spanX;
869 item.spanY = spanY;
870
871 // We store hotseat items in canonical form which is this orientation invariant position
872 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700874 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700876 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700878 }
Adam Cohend4844c32011-02-18 19:25:06 -0800879
Adam Cohend4844c32011-02-18 19:25:06 -0800880 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800881 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700882 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
883 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800884 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700885 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
886 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700887 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800888
Michael Jurka816474f2012-06-25 14:49:02 -0700889 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700890 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700891
892 /**
893 * Update an item to the database in a specified container.
894 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700895 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700896 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100897 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700898 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800899 }
900
Sunny Goyal756a28a2015-04-23 17:07:55 -0700901 private void assertWorkspaceLoaded() {
902 if (LauncherAppState.isDogfoodBuild() && (isLoadingWorkspace() || !mHasLoaderCompletedOnce)) {
903 throw new RuntimeException("Trying to add shortcut while loader is running");
904 }
905 }
906
Adam Cohend4844c32011-02-18 19:25:06 -0800907 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700908 * Returns true if the shortcuts already exists on the workspace. This must be called after
909 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700911 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
912 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700913 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700914 final String packageName;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700915 if (intent.getComponent() != null) {
916 // If component is not null, an intent with null package will produce
917 // the same result and should also be a match.
Sunny Goyal756adbc2015-04-16 15:20:51 -0700918 packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700919 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700920 intentWithPkg = intent.toUri(0);
921 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700922 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700923 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
924 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700925 }
926 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700927 intentWithPkg = intent.toUri(0);
928 intentWithoutPkg = intent.toUri(0);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700929 packageName = intent.getPackage();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700930 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700931
932 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700933 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700934 if (item instanceof ShortcutInfo) {
935 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyaldfde9992015-05-01 12:31:32 -0700936 if (info.getIntent() != null && info.user.equals(user)) {
937 String s = info.getIntent().toUri(0);
938 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
939 return true;
940 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700941 }
942 }
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700945 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700946 }
947
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 /**
949 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
950 */
Sunny Goyale2df0622015-04-24 11:27:00 -0700951 FolderInfo getFolderById(Context context, LongArrayMap<FolderInfo> folderList, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 final ContentResolver cr = context.getContentResolver();
953 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
954 "_id=? and (itemType=? or itemType=?)",
955 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700956 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700957
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 try {
959 if (c.moveToFirst()) {
960 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
961 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
962 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
963 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
964 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
965 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700966 final int optionsIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.OPTIONS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 FolderInfo folderInfo = null;
969 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700970 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
971 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700973 }
974
Sunny Goyala508e4f2015-05-21 09:33:57 -0700975 // Do not trim the folder label, as is was set by the user.
976 folderInfo.title = c.getString(titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 folderInfo.id = id;
978 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700980 folderInfo.cellX = c.getInt(cellXIndex);
981 folderInfo.cellY = c.getInt(cellYIndex);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700982 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983
984 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 } finally {
987 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700988 }
989
990 return null;
991 }
992
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 /**
994 * Add an item to the database in a specified container. Sets the container, screen, cellX and
995 * cellY fields of the item. Also assigns an ID to the item.
996 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700997 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700998 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 item.cellX = cellX;
1001 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 // We store hotseat items in canonical form which is this orientation invariant position
1003 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010
1011 final ContentValues values = new ContentValues();
1012 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001013 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014
Michael Jurka414300a2013-08-27 15:42:35 +02001015 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001016 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001017
Jason Monk8e19cf22014-03-20 15:06:57 -04001018 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001019 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001020 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001021 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001024 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001025 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 sBgItemsIdMap.put(item.id, item);
1027 switch (item.itemType) {
1028 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1029 sBgFolders.put(item.id, (FolderInfo) item);
1030 // Fall through
1031 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1032 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1033 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1034 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1035 sBgWorkspaceItems.add(item);
1036 } else {
1037 if (!sBgFolders.containsKey(item.container)) {
1038 // Adding an item to a folder that doesn't exist.
1039 String msg = "adding item: " + item + " to a folder that " +
1040 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001041 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001042 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001043 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001044 break;
1045 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1046 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1047 break;
1048 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001049 }
1050 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001051 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001053 }
1054
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001056 * Creates a new unique child id, for a given cell span across all layouts.
1057 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001058 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001061 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001062 }
1063
Sunny Goyal34942622014-08-29 17:20:55 -07001064 private static ArrayList<ItemInfo> getItemsByPackageName(
1065 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001066 ItemInfoFilter filter = new ItemInfoFilter() {
1067 @Override
1068 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1069 return cn.getPackageName().equals(pn) && info.user.equals(user);
1070 }
1071 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001072 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001073 }
1074
1075 /**
1076 * Removes all the items from the database corresponding to the specified package.
1077 */
1078 static void deletePackageFromDatabase(Context context, final String pn,
1079 final UserHandleCompat user) {
1080 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001081 }
1082
1083 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001084 * Removes the specified item from the database
1085 * @param context
1086 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001088 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001089 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1090 items.add(item);
1091 deleteItemsFromDatabase(context, items);
1092 }
1093
1094 /**
1095 * Removes the specified items from the database
1096 * @param context
1097 * @param item
1098 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001099 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001100 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001101 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001102 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001103 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001104 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001105 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001107 // Lock on mBgLock *after* the db operation
1108 synchronized (sBgLock) {
1109 switch (item.itemType) {
1110 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1111 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001112 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001113 if (info.container == item.id) {
1114 // We are deleting a folder which still contains items that
1115 // think they are contained by that folder.
1116 String msg = "deleting a folder (" + item + ") which still " +
1117 "contains items (" + info + ")";
1118 Log.e(TAG, msg);
1119 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001121 sBgWorkspaceItems.remove(item);
1122 break;
1123 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1124 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1125 sBgWorkspaceItems.remove(item);
1126 break;
1127 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1128 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1129 break;
1130 }
1131 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001133 }
1134 }
Michael Jurka83df1882011-08-31 20:59:26 -07001135 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001136 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 }
1138
1139 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001140 * Update the order of the workspace screens in the database. The array list contains
1141 * a list of screen ids in the order that they should appear.
1142 */
Winson Chungc9168342013-06-26 14:54:55 -07001143 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001144 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 final ContentResolver cr = context.getContentResolver();
1146 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1147
1148 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001149 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 while (iter.hasNext()) {
1151 long id = iter.next();
1152 if (id < 0) {
1153 iter.remove();
1154 }
1155 }
1156
1157 Runnable r = new Runnable() {
1158 @Override
1159 public void run() {
Yura085c8532014-02-11 15:15:29 +00001160 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001161 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001162 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001163 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 for (int i = 0; i < count; i++) {
1165 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001166 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1168 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001169 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001170 }
Yura085c8532014-02-11 15:15:29 +00001171
1172 try {
1173 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1174 } catch (Exception ex) {
1175 throw new RuntimeException(ex);
1176 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001177
Winson Chungba9c37f2013-08-30 14:11:37 -07001178 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001179 sBgWorkspaceScreens.clear();
1180 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001181 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001182 }
1183 };
1184 runOnWorkerThread(r);
1185 }
1186
1187 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 * Remove the contents of the specified folder from the database
1189 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001190 public static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 final ContentResolver cr = context.getContentResolver();
1192
Michael Jurkac9d95c52011-08-29 14:03:34 -07001193 Runnable r = new Runnable() {
1194 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001195 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001197 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 sBgItemsIdMap.remove(info.id);
1199 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 sBgWorkspaceItems.remove(info);
1201 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001202
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001203 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001204 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001206 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 for (ItemInfo childInfo : info.contents) {
1208 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001209 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001210 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001211 }
1212 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001213 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 }
1215
1216 /**
1217 * Set this as the current Launcher activity object for the loader.
1218 */
1219 public void initialize(Callbacks callbacks) {
1220 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001221 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1222 // workspace to prevent leaking Launcher activities on orientation change.
1223 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 mCallbacks = new WeakReference<Callbacks>(callbacks);
1225 }
1226 }
1227
Kenny Guyed131872014-04-30 03:02:21 +01001228 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001229 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001230 int op = PackageUpdatedTask.OP_UPDATE;
1231 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1232 user));
1233 }
1234
1235 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001236 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001237 int op = PackageUpdatedTask.OP_REMOVE;
1238 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1239 user));
1240 }
1241
1242 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001243 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001244 int op = PackageUpdatedTask.OP_ADD;
1245 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1246 user));
1247 }
1248
1249 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001250 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001251 boolean replacing) {
1252 if (!replacing) {
1253 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1254 user));
1255 if (mAppsCanBeOnRemoveableStorage) {
1256 // Only rebind if we support removable storage. It catches the
1257 // case where
1258 // apps on the external sd card need to be reloaded
1259 startLoaderFromBackground();
1260 }
1261 } else {
1262 // If we are replacing then just update the packages in the list
1263 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1264 packageNames, user));
1265 }
1266 }
1267
1268 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001269 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001270 boolean replacing) {
1271 if (!replacing) {
1272 enqueuePackageUpdated(new PackageUpdatedTask(
1273 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1274 user));
1275 }
Kenny Guyed131872014-04-30 03:02:21 +01001276 }
1277
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001278 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1280 * ACTION_PACKAGE_CHANGED.
1281 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001282 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001283 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001284 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
Joe Onorato36115782010-06-17 13:28:48 -04001286 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001287 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001288 // If we have changed locale we need to clear out the labels in all apps/workspace.
1289 forceReload();
1290 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1291 // Check if configuration change was an mcc/mnc change which would affect app resources
1292 // and we would need to clear out the labels in all apps/workspace. Same handling as
1293 // above for ACTION_LOCALE_CHANGED
1294 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001295 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001296 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001297 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001298 forceReload();
1299 }
1300 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001301 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001302 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1303 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001304 Callbacks callbacks = getCallback();
1305 if (callbacks != null) {
1306 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001307 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001308 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1309 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1310 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001311 }
1312 }
1313
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001314 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001315 resetLoadedState(true, true);
1316
Reena Lee93f824a2011-09-23 17:20:28 -07001317 // Do this here because if the launcher activity is running it will be restarted.
1318 // If it's not running startLoaderFromBackground will merely tell it that it needs
1319 // to reload.
1320 startLoaderFromBackground();
1321 }
1322
Winson Chungf0c6ae02012-03-21 16:10:31 -07001323 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1324 synchronized (mLock) {
1325 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1326 // mWorkspaceLoaded to true later
1327 stopLoaderLocked();
1328 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1329 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1330 }
1331 }
1332
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001333 /**
1334 * When the launcher is in the background, it's possible for it to miss paired
1335 * configuration changes. So whenever we trigger the loader from the background
1336 * tell the launcher that it needs to re-run the loader when it comes back instead
1337 * of doing it now.
1338 */
1339 public void startLoaderFromBackground() {
1340 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001341 Callbacks callbacks = getCallback();
1342 if (callbacks != null) {
1343 // Only actually run the loader if they're not paused.
1344 if (!callbacks.setLoadOnResume()) {
1345 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001346 }
1347 }
1348 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001349 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001350 }
Joe Onorato36115782010-06-17 13:28:48 -04001351 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001352
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001353 /**
1354 * If there is already a loader task running, tell it to stop.
1355 */
1356 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001357 LoaderTask oldTask = mLoaderTask;
1358 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001359 oldTask.stopLocked();
1360 }
Reena Lee93f824a2011-09-23 17:20:28 -07001361 }
1362
Adam Cohen1a85c582014-09-30 09:48:49 -07001363 public boolean isCurrentCallbacks(Callbacks callbacks) {
1364 return (mCallbacks != null && mCallbacks.get() == callbacks);
1365 }
1366
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001367 public void startLoader(int synchronousBindPage) {
1368 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001369 }
1370
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001371 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001372 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1373 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001374 synchronized (mLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 // Clear any deferred bind-runnables from the synchronized load process
1376 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001377 synchronized (mDeferredBindRunnables) {
1378 mDeferredBindRunnables.clear();
1379 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380
Joe Onorato36115782010-06-17 13:28:48 -04001381 // Don't bother to start the thread if we know it's not going to do anything
1382 if (mCallbacks != null && mCallbacks.get() != null) {
1383 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001384 stopLoaderLocked();
1385 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001386 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001387 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001388 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1389 } else {
1390 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1391 sWorker.post(mLoaderTask);
1392 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 }
1395 }
1396
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 void bindRemainingSynchronousPages() {
1398 // Post the remaining side pages to be loaded
1399 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001400 Runnable[] deferredBindRunnables = null;
1401 synchronized (mDeferredBindRunnables) {
1402 deferredBindRunnables = mDeferredBindRunnables.toArray(
1403 new Runnable[mDeferredBindRunnables.size()]);
1404 mDeferredBindRunnables.clear();
1405 }
1406 for (final Runnable r : deferredBindRunnables) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001407 mHandler.post(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001410
1411 // Run all the bind complete runnables after workspace is bound.
1412 if (!mBindCompleteRunnables.isEmpty()) {
1413 synchronized (mBindCompleteRunnables) {
1414 for (final Runnable r : mBindCompleteRunnables) {
1415 runOnWorkerThread(r);
1416 }
1417 mBindCompleteRunnables.clear();
1418 }
1419 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001420 }
1421
Joe Onorato36115782010-06-17 13:28:48 -04001422 public void stopLoader() {
1423 synchronized (mLock) {
1424 if (mLoaderTask != null) {
1425 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 }
1427 }
Joe Onorato36115782010-06-17 13:28:48 -04001428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001430 /**
1431 * Loads the workspace screen ids in an ordered list.
1432 */
Adam Cohen091440a2015-03-18 14:16:05 -07001433 @Thunk static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001434 final ContentResolver contentResolver = context.getContentResolver();
1435 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001436
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001437 // Get screens ordered by rank.
1438 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1439 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1440 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001441 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001442 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001443 while (sc.moveToNext()) {
1444 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001445 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001446 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001447 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1448 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001449 }
1450 }
1451 } finally {
1452 sc.close();
1453 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001454 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001455 }
1456
Michael Jurkac57b7a82011-08-09 22:02:20 -07001457 public boolean isAllAppsLoaded() {
1458 return mAllAppsLoaded;
1459 }
1460
Winson Chung36a62fe2012-05-06 18:04:42 -07001461 boolean isLoadingWorkspace() {
1462 synchronized (mLock) {
1463 if (mLoaderTask != null) {
1464 return mLoaderTask.isLoadingWorkspace();
1465 }
1466 }
1467 return false;
1468 }
1469
Joe Onorato36115782010-06-17 13:28:48 -04001470 /**
1471 * Runnable for the thread that loads the contents of the launcher:
1472 * - workspace icons
1473 * - widgets
1474 * - all apps icons
1475 */
1476 private class LoaderTask implements Runnable {
1477 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001478 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001479 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001480 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001481 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001482
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001483 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001484 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001485 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486 }
1487
Winson Chung36a62fe2012-05-06 18:04:42 -07001488 boolean isLoadingWorkspace() {
1489 return mIsLoadingAndBindingWorkspace;
1490 }
1491
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001492 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001493 mIsLoadingAndBindingWorkspace = true;
1494
Joe Onorato36115782010-06-17 13:28:48 -04001495 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001496 if (DEBUG_LOADERS) {
1497 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001499
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001500 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001501 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001502 synchronized (LoaderTask.this) {
1503 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001504 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001505 }
1506 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 }
1508 }
1509
Joe Onorato36115782010-06-17 13:28:48 -04001510 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001511 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001513
Joe Onorato36115782010-06-17 13:28:48 -04001514 private void waitForIdle() {
1515 // Wait until the either we're stopped or the other threads are done.
1516 // This way we don't start loading all apps until the workspace has settled
1517 // down.
1518 synchronized (LoaderTask.this) {
1519 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001520
Joe Onorato36115782010-06-17 13:28:48 -04001521 mHandler.postIdle(new Runnable() {
1522 public void run() {
1523 synchronized (LoaderTask.this) {
1524 mLoadAndBindStepFinished = true;
1525 if (DEBUG_LOADERS) {
1526 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001527 }
Joe Onorato36115782010-06-17 13:28:48 -04001528 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001529 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531 });
1532
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001533 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001534 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001535 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1536 // wait no longer than 1sec at a time
1537 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001538 } catch (InterruptedException ex) {
1539 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001540 }
1541 }
Joe Onorato36115782010-06-17 13:28:48 -04001542 if (DEBUG_LOADERS) {
1543 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001544 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001545 + "ms for previous step to finish binding");
1546 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001547 }
Joe Onorato36115782010-06-17 13:28:48 -04001548 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001549
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001551 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001552 // Ensure that we have a valid page index to load synchronously
1553 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1554 "valid page index");
1555 }
1556 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1557 // Ensure that we don't try and bind a specified page when the pages have not been
1558 // loaded already (we should load everything asynchronously in that case)
1559 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1560 }
1561 synchronized (mLock) {
1562 if (mIsLoaderTaskRunning) {
1563 // Ensure that we are never running the background loading at this point since
1564 // we also touch the background collections
1565 throw new RuntimeException("Error! Background loading is already running");
1566 }
1567 }
1568
1569 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1570 // data structures, we can't allow any other thread to touch that data, but because
1571 // this call is synchronous, we can get away with not locking).
1572
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001573 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001574 // operations from the previous activity. We need to ensure that all queued operations
1575 // are executed before any synchronous binding work is done.
1576 mHandler.flush();
1577
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578 // Divide the set of loaded items into those that we are binding synchronously, and
1579 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001580 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001581 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1582 // arise from that.
1583 onlyBindAllApps();
1584 }
1585
Joe Onorato36115782010-06-17 13:28:48 -04001586 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001587 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001588 if (mStopped) {
1589 return;
1590 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001591 mIsLoaderTaskRunning = true;
1592 }
Joe Onorato36115782010-06-17 13:28:48 -04001593 // Optimize for end-user experience: if the Launcher is up and // running with the
1594 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1595 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001596 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001597 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001598 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001599
Joe Onorato36115782010-06-17 13:28:48 -04001600 if (mStopped) {
1601 break keep_running;
1602 }
1603
Joe Onorato36115782010-06-17 13:28:48 -04001604 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001605
1606 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001607 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1608 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001609 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001610
Joe Onorato36115782010-06-17 13:28:48 -04001611 // Clear out this reference, otherwise we end up holding it until all of the
1612 // callback runnables are done.
1613 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001614
Joe Onorato36115782010-06-17 13:28:48 -04001615 synchronized (mLock) {
1616 // If we are still the last one to be scheduled, remove ourselves.
1617 if (mLoaderTask == this) {
1618 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001619 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001621 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
Joe Onorato36115782010-06-17 13:28:48 -04001623 }
1624
1625 public void stopLocked() {
1626 synchronized (LoaderTask.this) {
1627 mStopped = true;
1628 this.notify();
1629 }
1630 }
1631
1632 /**
1633 * Gets the callbacks object. If we've been stopped, or if the launcher object
1634 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1635 * object that was around when the deferred message was scheduled, and if there's
1636 * a new Callbacks object around then also return null. This will save us from
1637 * calling onto it with data that will be ignored.
1638 */
1639 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1640 synchronized (mLock) {
1641 if (mStopped) {
1642 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001643 }
Joe Onorato36115782010-06-17 13:28:48 -04001644
1645 if (mCallbacks == null) {
1646 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001647 }
Joe Onorato36115782010-06-17 13:28:48 -04001648
1649 final Callbacks callbacks = mCallbacks.get();
1650 if (callbacks != oldCallbacks) {
1651 return null;
1652 }
1653 if (callbacks == null) {
1654 Log.w(TAG, "no mCallbacks");
1655 return null;
1656 }
1657
1658 return callbacks;
1659 }
1660 }
1661
1662 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyale2df0622015-04-24 11:27:00 -07001663 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001664 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001665 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1666 final int countX = (int) profile.numColumns;
1667 final int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001668
Adam Cohendcd297f2013-06-18 13:13:40 -07001669 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001670 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001671 // Return early if we detect that an item is under the hotseat button
1672 if (mCallbacks == null ||
1673 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001674 Log.e(TAG, "Error loading shortcut into hotseat " + item
1675 + " into position (" + item.screenId + ":" + item.cellX + ","
1676 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001677 return false;
1678 }
1679
Dan Sandler295ae182013-12-10 16:05:47 -05001680 final ItemInfo[][] hotseatItems =
1681 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1682
Adam Cohen2e6da152015-05-06 11:42:25 -07001683 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001684 Log.e(TAG, "Error loading shortcut " + item
1685 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001686 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001687 + ")");
1688 return false;
1689 }
1690
Dan Sandler295ae182013-12-10 16:05:47 -05001691 if (hotseatItems != null) {
1692 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 Log.e(TAG, "Error loading shortcut into hotseat " + item
1694 + " into position (" + item.screenId + ":" + item.cellX + ","
1695 + item.cellY + ") occupied by "
1696 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1697 [(int) item.screenId][0]);
1698 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001699 } else {
1700 hotseatItems[(int) item.screenId][0] = item;
1701 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001702 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001703 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001704 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001705 items[(int) item.screenId][0] = item;
1706 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001707 return true;
1708 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001709 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1710 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001711 return true;
1712 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001713
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001715 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 occupied.put(item.screenId, items);
1717 }
1718
Dan Sandler295ae182013-12-10 16:05:47 -05001719 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001720 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1721 item.cellX < 0 || item.cellY < 0 ||
1722 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1723 Log.e(TAG, "Error loading shortcut " + item
1724 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1725 + item.cellX + "," + item.cellY
1726 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1727 return false;
1728 }
1729
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001730 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001731 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1732 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001733 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001734 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001735 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001736 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001737 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001738 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001739 return false;
1740 }
1741 }
1742 }
1743 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1744 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001745 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001746 }
1747 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001748
Joe Onorato36115782010-06-17 13:28:48 -04001749 return true;
1750 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751
Winson Chungba9c37f2013-08-30 14:11:37 -07001752 /** Clears all the sBg data structures */
1753 private void clearSBgDataStructures() {
1754 synchronized (sBgLock) {
1755 sBgWorkspaceItems.clear();
1756 sBgAppWidgets.clear();
1757 sBgFolders.clear();
1758 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001759 sBgWorkspaceScreens.clear();
1760 }
1761 }
1762
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001763 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001764 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001765
Joe Onorato36115782010-06-17 13:28:48 -04001766 final Context context = mContext;
1767 final ContentResolver contentResolver = context.getContentResolver();
1768 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001769 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001770 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1771 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001772 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001773
Winson Chung892c74d2013-08-22 16:15:50 -07001774 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001775 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
1776 int countX = (int) profile.numColumns;
1777 int countY = (int) profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001778
Dan Sandlerd5024042014-01-09 15:01:33 -05001779 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1780 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1781 LauncherAppState.getLauncherProvider().deleteDatabase();
1782 }
1783
1784 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1785 // append the user's Launcher2 shortcuts
1786 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1787 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1788 } else {
1789 // Make sure the default workspace is loaded
1790 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001791 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001792 }
Adam Cohene25af792013-06-06 23:08:25 -07001793
Winson Chung2abf94d2012-07-18 18:16:38 -07001794 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001795 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001796 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001797 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001798
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001800 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001801 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001802 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001803 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001804
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001805 // +1 for the hotseat (it can be larger than the workspace)
1806 // Load workspace in reverse order to ensure that latest items are loaded first (and
1807 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001808 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001810 try {
1811 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1812 final int intentIndex = c.getColumnIndexOrThrow
1813 (LauncherSettings.Favorites.INTENT);
1814 final int titleIndex = c.getColumnIndexOrThrow
1815 (LauncherSettings.Favorites.TITLE);
1816 final int iconTypeIndex = c.getColumnIndexOrThrow(
1817 LauncherSettings.Favorites.ICON_TYPE);
1818 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1819 final int iconPackageIndex = c.getColumnIndexOrThrow(
1820 LauncherSettings.Favorites.ICON_PACKAGE);
1821 final int iconResourceIndex = c.getColumnIndexOrThrow(
1822 LauncherSettings.Favorites.ICON_RESOURCE);
1823 final int containerIndex = c.getColumnIndexOrThrow(
1824 LauncherSettings.Favorites.CONTAINER);
1825 final int itemTypeIndex = c.getColumnIndexOrThrow(
1826 LauncherSettings.Favorites.ITEM_TYPE);
1827 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1828 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001829 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1830 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 final int screenIndex = c.getColumnIndexOrThrow(
1832 LauncherSettings.Favorites.SCREEN);
1833 final int cellXIndex = c.getColumnIndexOrThrow
1834 (LauncherSettings.Favorites.CELLX);
1835 final int cellYIndex = c.getColumnIndexOrThrow
1836 (LauncherSettings.Favorites.CELLY);
1837 final int spanXIndex = c.getColumnIndexOrThrow
1838 (LauncherSettings.Favorites.SPANX);
1839 final int spanYIndex = c.getColumnIndexOrThrow(
1840 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001841 final int rankIndex = c.getColumnIndexOrThrow(
1842 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001843 final int restoredIndex = c.getColumnIndexOrThrow(
1844 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001845 final int profileIdIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001847 final int optionsIndex = c.getColumnIndexOrThrow(
1848 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849
Sunny Goyal7f834d22015-04-21 10:10:23 -07001850 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1851 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1852 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1853 }
1854
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855 ShortcutInfo info;
1856 String intentDescription;
1857 LauncherAppWidgetInfo appWidgetInfo;
1858 int container;
1859 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001860 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001862 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 while (!mStopped && c.moveToNext()) {
1865 try {
1866 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001867 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001868 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 switch (itemType) {
1871 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1872 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001873 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001875 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001876 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001877 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001878 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001879 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001880 if (user == null) {
1881 // User has been deleted remove the item.
1882 itemsToRemove.add(id);
1883 continue;
1884 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 try {
1886 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001887 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001888 if (cn != null && cn.getPackageName() != null) {
1889 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1890 cn.getPackageName(), user);
1891 boolean validComponent = validPkg &&
1892 launcherApps.isActivityEnabledForProfile(cn, user);
1893
1894 if (validComponent) {
1895 if (restored) {
1896 // no special handling necessary for this item
1897 restoredRows.add(id);
1898 restored = false;
1899 }
1900 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001901 intent = null;
1902 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1903 // We allow auto install apps to have their intent
1904 // updated after an install.
1905 intent = manager.getLaunchIntentForPackage(
1906 cn.getPackageName());
1907 if (intent != null) {
1908 ContentValues values = new ContentValues();
1909 values.put(LauncherSettings.Favorites.INTENT,
1910 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001911 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001912 }
1913 }
1914
1915 if (intent == null) {
1916 // The app is installed but the component is no
1917 // longer available.
1918 Launcher.addDumpLog(TAG,
1919 "Invalid component removed: " + cn, true);
1920 itemsToRemove.add(id);
1921 continue;
1922 } else {
1923 // no special handling necessary for this item
1924 restoredRows.add(id);
1925 restored = false;
1926 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001927 } else if (restored) {
1928 // Package is not yet available but might be
1929 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001930 Launcher.addDumpLog(TAG,
1931 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001932
1933 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1934 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001935 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001936 // App restore has started. Update the flag
1937 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1938 ContentValues values = new ContentValues();
1939 values.put(LauncherSettings.Favorites.RESTORED,
1940 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001941 updateItem(id, values);
1942 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1943 // This is a common app. Try to replace this.
1944 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1945 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1946 if (parser.findDefaultApp()) {
1947 // Default app found. Replace it.
1948 intent = parser.parsedIntent;
1949 cn = intent.getComponent();
1950 ContentValues values = parser.parsedValues;
1951 values.put(LauncherSettings.Favorites.RESTORED, 0);
1952 updateItem(id, values);
1953 restored = false;
1954 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001955
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001956 } else if (REMOVE_UNRESTORED_ICONS) {
1957 Launcher.addDumpLog(TAG,
1958 "Unrestored package removed: " + cn, true);
1959 itemsToRemove.add(id);
1960 continue;
1961 }
Sunny Goyal94485362014-09-18 16:13:58 -07001962 } else if (REMOVE_UNRESTORED_ICONS) {
1963 Launcher.addDumpLog(TAG,
1964 "Unrestored package removed: " + cn, true);
1965 itemsToRemove.add(id);
1966 continue;
1967 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001968 } else if (launcherApps.isAppEnabled(
1969 manager, cn.getPackageName(),
1970 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1971 // Package is present but not available.
1972 allowMissingTarget = true;
1973 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1974 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001975 // SdCard is not ready yet. Package might get available,
1976 // once it is ready.
1977 Launcher.addDumpLog(TAG, "Invalid package: " + cn
1978 + " (check again later)", true);
1979 HashSet<String> pkgs = sPendingPackages.get(user);
1980 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001981 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001982 sPendingPackages.put(user, pkgs);
1983 }
1984 pkgs.add(cn.getPackageName());
1985 allowMissingTarget = true;
1986 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001987
1988 } else {
1989 // Do not wait for external media load anymore.
1990 // Log the invalid package, and remove it
1991 Launcher.addDumpLog(TAG,
1992 "Invalid package removed: " + cn, true);
1993 itemsToRemove.add(id);
1994 continue;
Winson Chungee055712013-07-30 14:46:24 -07001995 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001996 } else if (cn == null) {
1997 // For shortcuts with no component, keep them as they are
1998 restoredRows.add(id);
1999 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002000 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002002 Launcher.addDumpLog(TAG,
2003 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 continue;
2005 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006
Sunny Goyal34b65272015-03-11 16:56:52 -07002007 container = c.getInt(containerIndex);
2008 boolean useLowResIcon = container >= 0 &&
2009 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
2010
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002011 if (itemReplaced) {
2012 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002013 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal34b65272015-03-11 16:56:52 -07002014 iconIndex, titleIndex, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002015 } else {
2016 // Don't replace items for other profiles.
2017 itemsToRemove.add(id);
2018 continue;
2019 }
2020 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002021 if (user.equals(UserHandleCompat.myUserHandle())) {
2022 Launcher.addDumpLog(TAG,
2023 "constructing info for partially restored package",
2024 true);
Sunny Goyal34b65272015-03-11 16:56:52 -07002025 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07002026 promiseType);
Kenny Guyed131872014-04-30 03:02:21 +01002027 intent = getRestoredItemIntent(c, context, intent);
2028 } else {
2029 // Don't restore items for other profiles.
2030 itemsToRemove.add(id);
2031 continue;
2032 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002033 } else if (itemType ==
2034 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002035 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal34b65272015-03-11 16:56:52 -07002036 iconIndex, titleIndex, allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002037 } else {
2038 info = getShortcutInfo(c, context, iconTypeIndex,
2039 iconPackageIndex, iconResourceIndex, iconIndex,
2040 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 // App shortcuts that used to be automatically added to Launcher
2043 // didn't always have the correct intent flags set, so do that
2044 // here
2045 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002046 intent.getCategories() != null &&
2047 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002048 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 intent.addFlags(
2050 Intent.FLAG_ACTIVITY_NEW_TASK |
2051 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2052 }
Michael Jurka96879562012-03-22 05:54:33 -07002053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002054
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002056 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002059 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 info.cellX = c.getInt(cellXIndex);
2061 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002062 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002063 info.spanX = 1;
2064 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002065 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002066 info.isDisabled = disabledState;
2067 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2068 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2069 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002072 if (!checkItemPlacement(occupied, info)) {
2073 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 break;
2075 }
2076
Sunny Goyal756adbc2015-04-16 15:20:51 -07002077 if (restored) {
2078 ComponentName cn = info.getTargetComponent();
2079 if (cn != null) {
2080 Integer progress = installingPkgs.get(cn.getPackageName());
2081 if (progress != null) {
2082 info.setInstallProgress(progress);
2083 } else {
2084 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2085 }
2086 }
2087 }
2088
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 switch (container) {
2090 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2091 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2092 sBgWorkspaceItems.add(info);
2093 break;
2094 default:
2095 // Item is in a user folder
2096 FolderInfo folderInfo =
2097 findOrMakeFolder(sBgFolders, container);
2098 folderInfo.add(info);
2099 break;
2100 }
2101 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002102 } else {
2103 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002104 }
2105 break;
2106
2107 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2108 id = c.getLong(idIndex);
2109 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2110
Sunny Goyala508e4f2015-05-21 09:33:57 -07002111 // Do not trim the folder label, as is was set by the user.
2112 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002114 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002116 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 folderInfo.cellX = c.getInt(cellXIndex);
2118 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002119 folderInfo.spanX = 1;
2120 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002121 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122
Daniel Sandler8802e962010-05-26 16:28:16 -04002123 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002124 if (!checkItemPlacement(occupied, folderInfo)) {
2125 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002126 break;
2127 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002128
Joe Onorato9c1289c2009-08-17 11:03:03 -04002129 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2131 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2132 sBgWorkspaceItems.add(folderInfo);
2133 break;
Joe Onorato36115782010-06-17 13:28:48 -04002134 }
Joe Onorato17a89222011-02-08 17:26:11 -08002135
Chris Wrenf4d08112014-01-16 18:13:56 -05002136 if (restored) {
2137 // no special handling required for restored folders
2138 restoredRows.add(id);
2139 }
2140
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002141 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2142 sBgFolders.put(folderInfo.id, folderInfo);
2143 break;
2144
2145 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002146 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002148 boolean customWidget = itemType ==
2149 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2150
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002152 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002153 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002154 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002155 user = allUsers.get(serialNumber);
2156 if (user == null) {
2157 itemsToRemove.add(id);
2158 continue;
2159 }
2160
Sunny Goyalff572272014-07-23 13:58:07 -07002161 final ComponentName component =
2162 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002163
Sunny Goyal651077b2014-06-30 14:15:31 -07002164 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002165 final boolean isIdValid = (restoreStatus &
2166 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002167 final boolean wasProviderReady = (restoreStatus &
2168 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002169
Adam Cohen59400422014-03-05 18:07:04 -08002170 final LauncherAppWidgetProviderInfo provider =
2171 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002172 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002173 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002174
2175 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002176 if (!isSafeMode && !customWidget &&
2177 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002178 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002179 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002180
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002182 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 itemsToRemove.add(id);
2184 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002185 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002186 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2187 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002188
Sunny Goyalff572272014-07-23 13:58:07 -07002189 int status = restoreStatus;
2190 if (!wasProviderReady) {
2191 // If provider was not previously ready, update the
2192 // status and UI flag.
2193
2194 // Id would be valid only if the widget restore broadcast was received.
2195 if (isIdValid) {
2196 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2197 } else {
2198 status &= ~LauncherAppWidgetInfo
2199 .FLAG_PROVIDER_NOT_READY;
2200 }
2201 }
2202 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002203 } else {
2204 Log.v(TAG, "Widget restore pending id=" + id
2205 + " appWidgetId=" + appWidgetId
2206 + " status =" + restoreStatus);
2207 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002208 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002209 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002210 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002211
2212 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2213 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002214 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002215 // App restore has started. Update the flag
2216 appWidgetInfo.restoreStatus |=
2217 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002218 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002219 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002220 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002221 itemsToRemove.add(id);
2222 continue;
2223 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002224
2225 appWidgetInfo.installProgress =
2226 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002227 }
Sunny Goyalff572272014-07-23 13:58:07 -07002228
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 appWidgetInfo.cellX = c.getInt(cellXIndex);
2232 appWidgetInfo.cellY = c.getInt(cellYIndex);
2233 appWidgetInfo.spanX = c.getInt(spanXIndex);
2234 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002235
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 container = c.getInt(containerIndex);
2237 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2238 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2239 Log.e(TAG, "Widget found where container != " +
2240 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2241 continue;
2242 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243
Adam Cohene25af792013-06-06 23:08:25 -07002244 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002246 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2247 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 break;
2249 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002250
Adam Cohen59400422014-03-05 18:07:04 -08002251 if (!customWidget) {
2252 String providerName =
2253 appWidgetInfo.providerName.flattenToString();
2254 if (!providerName.equals(savedProvider) ||
2255 (appWidgetInfo.restoreStatus != restoreStatus)) {
2256 ContentValues values = new ContentValues();
2257 values.put(
2258 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2259 providerName);
2260 values.put(LauncherSettings.Favorites.RESTORED,
2261 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002262 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002263 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002264 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2266 sBgAppWidgets.add(appWidgetInfo);
2267 }
Joe Onorato36115782010-06-17 13:28:48 -04002268 break;
2269 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002270 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002271 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002272 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 }
2274 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002275 if (c != null) {
2276 c.close();
2277 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002278 }
2279
Winson Chungba9c37f2013-08-30 14:11:37 -07002280 // Break early if we've stopped loading
2281 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002282 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002283 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002284 }
2285
Sunny Goyal1936ec02015-05-12 11:03:04 -07002286 // Remove any empty folder
2287 LongArrayMap<FolderInfo> emptyFolders = sBgFolders.clone();
2288 for (ItemInfo item: sBgItemsIdMap) {
2289 long container = item.container;
2290 if (emptyFolders.containsKey(container)) {
2291 emptyFolders.remove(container);
2292 }
2293 }
2294 for (FolderInfo folder : emptyFolders) {
2295 long folderId = folder.id;
2296 sBgFolders.remove(folderId);
2297 sBgItemsIdMap.remove(folderId);
2298 sBgWorkspaceItems.remove(folder);
2299 itemsToRemove.add(folderId);
2300 }
2301
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 if (itemsToRemove.size() > 0) {
2303 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002304 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305 // Remove dead items
2306 for (long id : itemsToRemove) {
2307 if (DEBUG_LOADERS) {
2308 Log.d(TAG, "Removed id = " + id);
2309 }
2310 // Don't notify content observers
2311 try {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002312 client.delete(LauncherSettings.Favorites.getContentUri(id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 } catch (RemoteException e) {
2314 Log.w(TAG, "Could not remove id = " + id);
2315 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002316 }
2317 }
2318
Chris Wrenf4d08112014-01-16 18:13:56 -05002319 if (restoredRows.size() > 0) {
2320 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002321 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002322 // Update restored items that no longer require special handling
2323 try {
2324 StringBuilder selectionBuilder = new StringBuilder();
2325 selectionBuilder.append(LauncherSettings.Favorites._ID);
2326 selectionBuilder.append(" IN (");
2327 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2328 selectionBuilder.append(")");
2329 ContentValues values = new ContentValues();
2330 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002331 updater.update(LauncherSettings.Favorites.CONTENT_URI,
Chris Wrenf4d08112014-01-16 18:13:56 -05002332 values, selectionBuilder.toString(), null);
2333 } catch (RemoteException e) {
2334 Log.w(TAG, "Could not update restored rows");
2335 }
2336 }
2337
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002338 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2339 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002340 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002341 null, sWorker);
2342 }
2343
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002344 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2345 // Log to disk
2346 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2347 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002348
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002349 // Remove any empty screens
2350 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002351 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002352 long screenId = item.screenId;
2353 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2354 unusedScreens.contains(screenId)) {
2355 unusedScreens.remove(screenId);
2356 }
2357 }
2358
2359 // If there are any empty screens remove them, and update.
2360 if (unusedScreens.size() != 0) {
2361 // Log to disk
2362 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2363 TextUtils.join(", ", unusedScreens), true);
2364
2365 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002366 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002367 }
2368
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 if (DEBUG_LOADERS) {
2370 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2371 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002372 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002373 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002375
Sunny Goyale2df0622015-04-24 11:27:00 -07002376 for (int i = 0; i < nScreens; i++) {
2377 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002378 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 line += " | ";
2380 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002381 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002382 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002383 if (x < screen.length && y < screen[x].length) {
2384 line += (screen[x][y] != null) ? "#" : ".";
2385 } else {
2386 line += "!";
2387 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 }
Joe Onorato36115782010-06-17 13:28:48 -04002389 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002392 }
Joe Onorato36115782010-06-17 13:28:48 -04002393 }
Adam Cohene25af792013-06-06 23:08:25 -07002394 }
2395
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002396 /**
2397 * Partially updates the item without any notification. Must be called on the worker thread.
2398 */
2399 private void updateItem(long itemId, ContentValues update) {
2400 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002401 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002402 update,
2403 BaseColumns._ID + "= ?",
2404 new String[]{Long.toString(itemId)});
2405 }
2406
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 /** Filters the set of items who are directly or indirectly (via another container) on the
2408 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002409 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410 ArrayList<ItemInfo> allWorkspaceItems,
2411 ArrayList<ItemInfo> currentScreenItems,
2412 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002413 // Purge any null ItemInfos
2414 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2415 while (iter.hasNext()) {
2416 ItemInfo i = iter.next();
2417 if (i == null) {
2418 iter.remove();
2419 }
2420 }
2421
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422 // Order the set of items by their containers first, this allows use to walk through the
2423 // list sequentially, build up a list of containers that are in the specified screen,
2424 // as well as all items in those containers.
2425 Set<Long> itemsOnScreen = new HashSet<Long>();
2426 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2427 @Override
2428 public int compare(ItemInfo lhs, ItemInfo rhs) {
2429 return (int) (lhs.container - rhs.container);
2430 }
2431 });
2432 for (ItemInfo info : allWorkspaceItems) {
2433 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002434 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002435 currentScreenItems.add(info);
2436 itemsOnScreen.add(info.id);
2437 } else {
2438 otherScreenItems.add(info);
2439 }
2440 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2441 currentScreenItems.add(info);
2442 itemsOnScreen.add(info.id);
2443 } else {
2444 if (itemsOnScreen.contains(info.container)) {
2445 currentScreenItems.add(info);
2446 itemsOnScreen.add(info.id);
2447 } else {
2448 otherScreenItems.add(info);
2449 }
2450 }
2451 }
2452 }
2453
2454 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002455 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 ArrayList<LauncherAppWidgetInfo> appWidgets,
2457 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2458 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459
2460 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002461 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002463 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 currentScreenWidgets.add(widget);
2465 } else {
2466 otherScreenWidgets.add(widget);
2467 }
2468 }
2469 }
2470
2471 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002472 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002473 LongArrayMap<ItemInfo> itemsIdMap,
2474 LongArrayMap<FolderInfo> folders,
2475 LongArrayMap<FolderInfo> currentScreenFolders,
2476 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477
Sunny Goyale2df0622015-04-24 11:27:00 -07002478 int total = folders.size();
2479 for (int i = 0; i < total; i++) {
2480 long id = folders.keyAt(i);
2481 FolderInfo folder = folders.valueAt(i);
2482
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002484 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002486 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 currentScreenFolders.put(id, folder);
2488 } else {
2489 otherScreenFolders.put(id, folder);
2490 }
2491 }
2492 }
2493
2494 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2495 * right) */
2496 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002497 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002498 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 // XXX: review this
2500 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002501 @Override
2502 public int compare(ItemInfo lhs, ItemInfo rhs) {
Adam Cohen2e6da152015-05-06 11:42:25 -07002503 int cellCountX = (int) profile.numColumns;
2504 int cellCountY = (int) profile.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002505 int screenOffset = cellCountX * cellCountY;
2506 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002507 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002508 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002509 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002510 rhs.cellY * cellCountX + rhs.cellX);
2511 return (int) (lr - rr);
2512 }
2513 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002515
Adam Cohendcd297f2013-06-18 13:13:40 -07002516 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2517 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002518 final Runnable r = new Runnable() {
2519 @Override
2520 public void run() {
2521 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2522 if (callbacks != null) {
2523 callbacks.bindScreens(orderedScreens);
2524 }
2525 }
2526 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002527 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002528 }
2529
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2531 final ArrayList<ItemInfo> workspaceItems,
2532 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002533 final LongArrayMap<FolderInfo> folders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002535
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 final boolean postOnMainThread = (deferredBindRunnables != null);
2537
2538 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002539 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002541 final int start = i;
2542 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 final Runnable r = new Runnable() {
2544 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002545 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002546 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002548 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2549 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002550 }
2551 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552 };
2553 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002554 synchronized (deferredBindRunnables) {
2555 deferredBindRunnables.add(r);
2556 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002558 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 }
Joe Onorato36115782010-06-17 13:28:48 -04002560 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561
2562 // Bind the folders
2563 if (!folders.isEmpty()) {
2564 final Runnable r = new Runnable() {
2565 public void run() {
2566 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2567 if (callbacks != null) {
2568 callbacks.bindFolders(folders);
2569 }
2570 }
2571 };
2572 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002573 synchronized (deferredBindRunnables) {
2574 deferredBindRunnables.add(r);
2575 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002577 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002578 }
2579 }
2580
2581 // Bind the widgets, one at a time
2582 N = appWidgets.size();
2583 for (int i = 0; i < N; i++) {
2584 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2585 final Runnable r = new Runnable() {
2586 public void run() {
2587 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2588 if (callbacks != null) {
2589 callbacks.bindAppWidget(widget);
2590 }
2591 }
2592 };
2593 if (postOnMainThread) {
2594 deferredBindRunnables.add(r);
2595 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002596 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002597 }
2598 }
2599 }
2600
2601 /**
2602 * Binds all loaded data to actual views on the main thread.
2603 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002604 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002605 final long t = SystemClock.uptimeMillis();
2606 Runnable r;
2607
2608 // Don't use these two variables in any of the callback runnables.
2609 // Otherwise we hold a reference to them.
2610 final Callbacks oldCallbacks = mCallbacks.get();
2611 if (oldCallbacks == null) {
2612 // This launcher has exited and nobody bothered to tell us. Just bail.
2613 Log.w(TAG, "LoaderTask running with no launcher");
2614 return;
2615 }
2616
Winson Chung9b9fb962013-11-15 15:39:34 -08002617 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002618 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2619 ArrayList<LauncherAppWidgetInfo> appWidgets =
2620 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002621 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002622
2623 final LongArrayMap<FolderInfo> folders;
2624 final LongArrayMap<ItemInfo> itemsIdMap;
2625
Winson Chung2abf94d2012-07-18 18:16:38 -07002626 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627 workspaceItems.addAll(sBgWorkspaceItems);
2628 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002629 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002630
2631 folders = sBgFolders.clone();
2632 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633 }
2634
Derek Prothro7aff3992013-12-10 14:00:37 -05002635 final boolean isLoadingSynchronously =
2636 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002637 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002638 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002639 if (currScreen >= orderedScreenIds.size()) {
2640 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002641 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002642 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002643 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002644 final long currentScreenId = currentScreen < 0
2645 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002646
2647 // Load all the items that are on the current page first (and in the process, unbind
2648 // all the existing workspace items before we call startBinding() below.
2649 unbindWorkspaceItemsOnMainThread();
2650
2651 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002652 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2653 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2654 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2655 new ArrayList<LauncherAppWidgetInfo>();
2656 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2657 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002658 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2659 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002660
Winson Chung9b9fb962013-11-15 15:39:34 -08002661 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002663 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002664 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002665 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002666 otherFolders);
2667 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2668 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2669
2670 // Tell the workspace that we're about to start binding items
2671 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002672 public void run() {
2673 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2674 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002675 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002676 }
2677 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002678 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002679 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002680
Adam Cohendcd297f2013-06-18 13:13:40 -07002681 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2682
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002683 // Load items on the current page
2684 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2685 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002686 if (isLoadingSynchronously) {
2687 r = new Runnable() {
2688 public void run() {
2689 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002690 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002691 callbacks.onPageBoundSynchronously(currentScreen);
2692 }
2693 }
2694 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002695 runOnMainThread(r);
Adam Cohen1462de32012-07-24 22:34:36 -07002696 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002697
Winson Chung4a2afa32012-07-19 14:53:05 -07002698 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2699 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002700 synchronized (mDeferredBindRunnables) {
2701 mDeferredBindRunnables.clear();
2702 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002703 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002704 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002705
2706 // Tell the workspace that we're done binding items
2707 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002708 public void run() {
2709 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2710 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002711 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002712 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002713
Winson Chung98e030b2012-05-07 16:01:11 -07002714 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002715 if (DEBUG_LOADERS) {
2716 Log.d(TAG, "bound workspace in "
2717 + (SystemClock.uptimeMillis()-t) + "ms");
2718 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002719
2720 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002721 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002722 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002723 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002724 synchronized (mDeferredBindRunnables) {
2725 mDeferredBindRunnables.add(r);
2726 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002727 } else {
Sunny Goyald33860f2015-04-23 16:02:20 -07002728 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002729 }
Joe Onorato36115782010-06-17 13:28:48 -04002730 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002731
Joe Onorato36115782010-06-17 13:28:48 -04002732 private void loadAndBindAllApps() {
2733 if (DEBUG_LOADERS) {
2734 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2735 }
2736 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002737 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002738 synchronized (LoaderTask.this) {
2739 if (mStopped) {
2740 return;
2741 }
2742 }
Sunny Goyal75b0f552015-05-20 21:57:06 -07002743 mIconCache.updateDbIcons();
Reena Lee93f824a2011-09-23 17:20:28 -07002744 synchronized (LoaderTask.this) {
2745 if (mStopped) {
2746 return;
2747 }
2748 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002749 }
Joe Onorato36115782010-06-17 13:28:48 -04002750 } else {
2751 onlyBindAllApps();
2752 }
2753 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002754
Joe Onorato36115782010-06-17 13:28:48 -04002755 private void onlyBindAllApps() {
2756 final Callbacks oldCallbacks = mCallbacks.get();
2757 if (oldCallbacks == null) {
2758 // This launcher has exited and nobody bothered to tell us. Just bail.
2759 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2760 return;
2761 }
2762
2763 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002764 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002765 final ArrayList<AppInfo> list
2766 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002767 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002768 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002769 public void run() {
2770 final long t = SystemClock.uptimeMillis();
2771 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2772 if (callbacks != null) {
2773 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002774 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002775 }
2776 if (DEBUG_LOADERS) {
2777 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2778 + (SystemClock.uptimeMillis()-t) + "ms");
2779 }
2780 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002781 };
2782 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002783 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002784 r.run();
2785 } else {
2786 mHandler.post(r);
2787 }
Joe Onorato36115782010-06-17 13:28:48 -04002788 }
2789
Winson Chung64359a52013-07-08 17:17:08 -07002790 private void loadAllApps() {
2791 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002792
Joe Onorato36115782010-06-17 13:28:48 -04002793 final Callbacks oldCallbacks = mCallbacks.get();
2794 if (oldCallbacks == null) {
2795 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002796 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002797 return;
2798 }
2799
Kenny Guyed131872014-04-30 03:02:21 +01002800 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2801
Winson Chung64359a52013-07-08 17:17:08 -07002802 // Clear the list of apps
2803 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002804 for (UserHandleCompat user : profiles) {
2805 // Query for the set of apps
2806 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002807 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002808 if (DEBUG_LOADERS) {
2809 Log.d(TAG, "getActivityList took "
2810 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2811 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2812 }
2813 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002814 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002815 if (apps == null || apps.isEmpty()) {
2816 return;
2817 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002818
Kenny Guyed131872014-04-30 03:02:21 +01002819 // Create the ApplicationInfos
2820 for (int i = 0; i < apps.size(); i++) {
2821 LauncherActivityInfoCompat app = apps.get(i);
2822 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002823 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002824 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002825
Sunny Goyal756a28a2015-04-23 17:07:55 -07002826 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2827 if (heuristic != null) {
2828 runAfterBindCompletes(new Runnable() {
2829
2830 @Override
2831 public void run() {
2832 heuristic.processUserApps(apps);
2833 }
2834 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002835 }
Winson Chung64359a52013-07-08 17:17:08 -07002836 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002837 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002838 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2839 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002840
2841 // Post callback on main thread
2842 mHandler.post(new Runnable() {
2843 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002844
Winson Chung64359a52013-07-08 17:17:08 -07002845 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002846 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002847 if (callbacks != null) {
2848 callbacks.bindAllApplications(added);
2849 if (DEBUG_LOADERS) {
2850 Log.d(TAG, "bound " + added.size() + " apps in "
2851 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2852 }
2853 } else {
2854 Log.i(TAG, "not binding apps: no Launcher activity");
2855 }
2856 }
2857 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002858 // Cleanup any data stored for a deleted user.
2859 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002860
Hyunyoung Song219d0482015-05-07 12:51:42 -07002861 loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
2862 true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002863 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002864 Log.d(TAG, "Icons processed in "
2865 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002866 }
2867 }
2868
2869 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002870 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002871 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002872 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2873 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2874 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2875 }
Joe Onorato36115782010-06-17 13:28:48 -04002876 }
2877 }
2878
Sunny Goyal75b0f552015-05-20 21:57:06 -07002879 /**
2880 * Called when the icons for packages have been updated in the icon cache.
2881 */
2882 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2883 final Callbacks callbacks = getCallback();
2884 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2885 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2886
2887 // If any package icon has changed (app was updated while launcher was dead),
2888 // update the corresponding shortcuts.
2889 synchronized (sBgLock) {
2890 for (ItemInfo info : sBgItemsIdMap) {
2891 if (info instanceof ShortcutInfo && user.equals(info.user)
2892 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2893 ShortcutInfo si = (ShortcutInfo) info;
2894 ComponentName cn = si.getTargetComponent();
2895 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2896 si.updateIcon(mIconCache);
2897 updatedShortcuts.add(si);
2898 }
2899 }
2900 }
2901 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2902 }
2903
2904 if (!updatedShortcuts.isEmpty()) {
2905 final UserHandleCompat userFinal = user;
2906 mHandler.post(new Runnable() {
2907
2908 public void run() {
2909 Callbacks cb = getCallback();
2910 if (cb != null && callbacks == cb) {
2911 cb.bindShortcutsChanged(updatedShortcuts,
2912 new ArrayList<ShortcutInfo>(), userFinal);
2913 }
2914 }
2915 });
2916 }
2917
2918 if (!updatedApps.isEmpty()) {
2919 mHandler.post(new Runnable() {
2920
2921 public void run() {
2922 Callbacks cb = getCallback();
2923 if (cb != null && callbacks == cb) {
2924 cb.bindAppsUpdated(updatedApps);
2925 }
2926 }
2927 });
2928 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002929
2930 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
2931 loadAndBindWidgetsAndShortcuts(mApp.getContext(), callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002932 }
2933
Joe Onorato36115782010-06-17 13:28:48 -04002934 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002935 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002936 }
2937
Adam Cohen091440a2015-03-18 14:16:05 -07002938 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002939
2940 @Override
2941 public void onReceive(Context context, Intent intent) {
2942 synchronized (sBgLock) {
2943 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2944 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002945 final PackageManager manager = context.getPackageManager();
2946 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2947 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002948 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2949 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002950 packagesRemoved.clear();
2951 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002952 for (String pkg : entry.getValue()) {
2953 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002954 boolean packageOnSdcard = launcherApps.isAppEnabled(
2955 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2956 if (packageOnSdcard) {
2957 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2958 packagesUnavailable.add(pkg);
2959 } else {
2960 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2961 packagesRemoved.add(pkg);
2962 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002963 }
2964 }
2965 if (!packagesRemoved.isEmpty()) {
2966 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2967 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2968 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002969 if (!packagesUnavailable.isEmpty()) {
2970 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2971 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2972 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002973 }
Sunny Goyal34942622014-08-29 17:20:55 -07002974 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002975 }
2976 }
2977 }
2978
Joe Onorato36115782010-06-17 13:28:48 -04002979 private class PackageUpdatedTask implements Runnable {
2980 int mOp;
2981 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002982 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002983
2984 public static final int OP_NONE = 0;
2985 public static final int OP_ADD = 1;
2986 public static final int OP_UPDATE = 2;
2987 public static final int OP_REMOVE = 3; // uninstlled
2988 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2989
2990
Kenny Guyed131872014-04-30 03:02:21 +01002991 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002992 mOp = op;
2993 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002994 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002995 }
2996
2997 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002998 if (!mHasLoaderCompletedOnce) {
2999 // Loader has not yet run.
3000 return;
3001 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003002 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003003
3004 final String[] packages = mPackages;
3005 final int N = packages.length;
3006 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003007 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003008 for (int i=0; i<N; i++) {
3009 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003010 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003011 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003012 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003013
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003014 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3015 if (heuristic != null) {
3016 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003017 }
Joe Onorato36115782010-06-17 13:28:48 -04003018 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003019 }
Joe Onorato36115782010-06-17 13:28:48 -04003020 case OP_UPDATE:
3021 for (int i=0; i<N; i++) {
3022 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003023 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003024 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003025 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003026 }
3027 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003028 case OP_REMOVE: {
3029 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3030 if (heuristic != null) {
3031 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003032 }
Joe Onorato36115782010-06-17 13:28:48 -04003033 for (int i=0; i<N; i++) {
3034 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003035 mIconCache.removeIconsForPkg(packages[i], mUser);
3036 }
3037 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003038 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003039 case OP_UNAVAILABLE:
3040 for (int i=0; i<N; i++) {
3041 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3042 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003043 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003044 }
3045 break;
3046 }
3047
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003048 ArrayList<AppInfo> added = null;
3049 ArrayList<AppInfo> modified = null;
3050 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003051
Adam Cohen487f7dd2012-06-28 18:12:10 -07003052 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003053 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003054 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003055 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003056 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003057 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003058 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003059 }
Winson Chung5d55f332012-07-16 20:45:03 -07003060 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003061 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003062 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003063 }
3064
Sunny Goyale0f58d72014-11-10 18:05:31 -08003065 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003066 if (callbacks == null) {
3067 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3068 return;
3069 }
3070
Sunny Goyal4390ace2014-10-13 11:33:11 -07003071 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3072 new HashMap<ComponentName, AppInfo>();
3073
Joe Onorato36115782010-06-17 13:28:48 -04003074 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003075 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003076 for (AppInfo ai : added) {
3077 addedOrUpdatedApps.put(ai.componentName, ai);
3078 }
Joe Onorato36115782010-06-17 13:28:48 -04003079 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003080
Joe Onorato36115782010-06-17 13:28:48 -04003081 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003082 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003083 for (AppInfo ai : modified) {
3084 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003085 }
3086
Joe Onorato36115782010-06-17 13:28:48 -04003087 mHandler.post(new Runnable() {
3088 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003089 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003090 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003091 callbacks.bindAppsUpdated(modifiedFinal);
3092 }
3093 }
3094 });
3095 }
Winson Chung83892cc2013-05-01 16:53:33 -07003096
Sunny Goyal4390ace2014-10-13 11:33:11 -07003097 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003098 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003099 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3100 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3101 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3102
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003103 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003104 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003105 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003106 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3107 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 boolean infoUpdated = false;
3109 boolean shortcutUpdated = false;
3110
3111 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003112 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003113 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003114 Bitmap icon = Utilities.createIconBitmap(
3115 si.iconResource.packageName,
3116 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003117 if (icon != null) {
3118 si.setIcon(icon);
3119 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003121 }
3122 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003123
3124 ComponentName cn = si.getTargetComponent();
3125 if (cn != null && packageSet.contains(cn.getPackageName())) {
3126 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3127
3128 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003129 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3130 // Auto install icon
3131 PackageManager pm = context.getPackageManager();
3132 ResolveInfo matched = pm.resolveActivity(
3133 new Intent(Intent.ACTION_MAIN)
3134 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3135 PackageManager.MATCH_DEFAULT_ONLY);
3136 if (matched == null) {
3137 // Try to find the best match activity.
3138 Intent intent = pm.getLaunchIntentForPackage(
3139 cn.getPackageName());
3140 if (intent != null) {
3141 cn = intent.getComponent();
3142 appInfo = addedOrUpdatedApps.get(cn);
3143 }
3144
3145 if ((intent == null) || (appInfo == null)) {
3146 removedShortcuts.add(si);
3147 continue;
3148 }
3149 si.promisedIntent = intent;
3150 }
3151 }
3152
3153 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003154 if (appInfo != null) {
3155 si.flags = appInfo.flags;
3156 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157
Sunny Goyal756adbc2015-04-16 15:20:51 -07003158 si.intent = si.promisedIntent;
3159 si.promisedIntent = null;
3160 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003161 infoUpdated = true;
3162 si.updateIcon(mIconCache);
3163 }
3164
3165 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3166 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3167 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003168 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169 si.contentDescription = appInfo.contentDescription;
3170 infoUpdated = true;
3171 }
3172
3173 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3174 // Since package was just updated, the target must be available now.
3175 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3176 shortcutUpdated = true;
3177 }
3178 }
3179
3180 if (infoUpdated || shortcutUpdated) {
3181 updatedShortcuts.add(si);
3182 }
3183 if (infoUpdated) {
3184 updateItemInDatabase(context, si);
3185 }
3186 } else if (info instanceof LauncherAppWidgetInfo) {
3187 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3188 if (mUser.equals(widgetInfo.user)
3189 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3190 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3191 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3192 widgets.add(widgetInfo);
3193 updateItemInDatabase(context, widgetInfo);
3194 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003195 }
3196 }
3197 }
3198
Sunny Goyal4390ace2014-10-13 11:33:11 -07003199 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3200 mHandler.post(new Runnable() {
3201
3202 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003203 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003204 if (callbacks == cb && cb != null) {
3205 callbacks.bindShortcutsChanged(
3206 updatedShortcuts, removedShortcuts, mUser);
3207 }
3208 }
3209 });
3210 if (!removedShortcuts.isEmpty()) {
3211 deleteItemsFromDatabase(context, removedShortcuts);
3212 }
3213 }
3214 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003215 mHandler.post(new Runnable() {
3216 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003217 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003218 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003219 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003220 }
3221 }
3222 });
3223 }
3224 }
3225
Winson Chungdf95eb12013-10-16 14:57:07 -07003226 final ArrayList<String> removedPackageNames =
3227 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003228 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003229 // Mark all packages in the broadcast to be removed
3230 removedPackageNames.addAll(Arrays.asList(packages));
3231 } else if (mOp == OP_UPDATE) {
3232 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003233 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003234 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003235 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003236 }
3237 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003238 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003239
Winson Chungdf95eb12013-10-16 14:57:07 -07003240 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003241 final int removeReason;
3242 if (mOp == OP_UNAVAILABLE) {
3243 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3244 } else {
3245 // Remove all the components associated with this package
3246 for (String pn : removedPackageNames) {
3247 deletePackageFromDatabase(context, pn, mUser);
3248 }
3249 // Remove all the specific components
3250 for (AppInfo a : removedApps) {
3251 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3252 deleteItemsFromDatabase(context, infos);
3253 }
3254 removeReason = 0;
3255 }
3256
Winson Chungdf95eb12013-10-16 14:57:07 -07003257 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003258 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003259 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003260 mHandler.post(new Runnable() {
3261 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003262 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003263 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003264 callbacks.bindComponentsRemoved(
3265 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003266 }
3267 }
3268 });
Joe Onoratobe386092009-11-17 17:32:16 -08003269 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003270
3271 // onProvidersChanged method (API >= 17) already refreshed the widget list
3272 loadAndBindWidgetsAndShortcuts(context, callbacks, Build.VERSION.SDK_INT < 17);
3273
Adam Cohen4caf2982013-08-20 18:54:31 -07003274 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003275 mHandler.post(new Runnable() {
3276 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003277 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003278 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003279 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003280 }
3281 }
3282 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003283 }
3284 }
3285
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003286 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3287 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003288 ArrayList<LauncherAppWidgetProviderInfo> results =
3289 new ArrayList<LauncherAppWidgetProviderInfo>();
3290 try {
3291 synchronized (sBgLock) {
3292 if (sBgWidgetProviders == null || refresh) {
3293 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3294 = new HashMap<>();
3295 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3296 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003297
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003298 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3299 for (AppWidgetProviderInfo pInfo : widgets) {
3300 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3301 UserHandleCompat user = wm.getUser(info);
3302 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3303 }
Robin Lee26ace122015-03-16 19:41:43 +00003304
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003305 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3306 for (CustomAppWidget widget : customWidgets) {
3307 info = new LauncherAppWidgetProviderInfo(context, widget);
3308 UserHandleCompat user = wm.getUser(info);
3309 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3310 }
3311 // Replace the global list at the very end, so that if there is an exception,
3312 // previously loaded provider list is used.
3313 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003314 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003315 results.addAll(sBgWidgetProviders.values());
3316 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003317 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003318 } catch (Exception e) {
3319 if (e.getCause() instanceof TransactionTooLargeException) {
3320 // the returned value may be incomplete and will not be refreshed until the next
3321 // time Launcher starts.
3322 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3323 // onResume is called to refresh the widget provider list.
3324 synchronized (sBgLock) {
3325 if (sBgWidgetProviders != null) {
3326 results.addAll(sBgWidgetProviders.values());
3327 }
3328 return results;
3329 }
3330 } else {
3331 throw e;
3332 }
Adam Cohen59400422014-03-05 18:07:04 -08003333 }
3334 }
3335
Robin Lee26ace122015-03-16 19:41:43 +00003336 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3337 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003338 synchronized (sBgLock) {
3339 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003340 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003341 }
Robin Lee26ace122015-03-16 19:41:43 +00003342 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003343 }
3344 }
3345
Hyunyoung Song227239e2015-05-04 18:17:35 -07003346 public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
3347 final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003348
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003349 runOnWorkerThread(new Runnable(){
3350 @Override
3351 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003352 final WidgetsModel model = createWidgetsModel(context, refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003353 mHandler.post(new Runnable() {
3354 @Override
3355 public void run() {
3356 Callbacks cb = getCallback();
3357 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003358 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003359 }
3360 }
3361 });
Hyunyoung Song9110d482015-05-22 14:49:23 -07003362 mBgWidgetsModel = model;
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003363 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003364 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3365 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003366 }
3367 });
3368 }
3369
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003370 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003371 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003372 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003373 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003374 */
Sunny Goyal316490e2015-06-02 09:38:28 -07003375 @Thunk WidgetsModel createWidgetsModel(Context context, boolean refresh) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003376 PackageManager packageManager = context.getPackageManager();
3377 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003378 widgetsAndShortcuts.addAll(getWidgetProviders(context, refresh));
Michael Jurkac402cd92013-05-20 15:49:32 +02003379 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3380 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003381 WidgetsModel model = new WidgetsModel(context);
3382 model.addWidgetsAndShortcuts(widgetsAndShortcuts);
3383 return model;
Michael Jurkac402cd92013-05-20 15:49:32 +02003384 }
3385
Adam Cohen091440a2015-03-18 14:16:05 -07003386 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003387 UserHandleCompat user) {
3388 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3389 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003390 }
Adam Cohen556f6132014-01-15 15:18:08 -08003391
Kenny Guyed131872014-04-30 03:02:21 +01003392 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3393 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003394 if (cn == null) {
3395 return false;
3396 }
Kenny Guyed131872014-04-30 03:02:21 +01003397 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3398 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003399 return false;
3400 }
Kenny Guyed131872014-04-30 03:02:21 +01003401 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003402 }
3403
Adam Cohena28b78e2014-05-20 17:03:04 -07003404 public static boolean isValidPackage(Context context, String packageName,
3405 UserHandleCompat user) {
3406 if (packageName == null) {
3407 return false;
3408 }
3409 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3410 return launcherApps.isPackageEnabledForProfile(packageName, user);
3411 }
3412
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003414 * Make an ShortcutInfo object for a restored application or shortcut item that points
3415 * to a package that is not yet installed on the system.
3416 */
Sunny Goyal34942622014-08-29 17:20:55 -07003417 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
Sunny Goyal5c97f512015-05-19 16:03:28 -07003418 int promiseType) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003419 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003420 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal5c97f512015-05-19 16:03:28 -07003421 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
Sunny Goyal34942622014-08-29 17:20:55 -07003422
3423 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3424 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3425 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003426 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003427 }
Sunny Goyal34942622014-08-29 17:20:55 -07003428 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3429 if (TextUtils.isEmpty(info.title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003430 info.title = (cursor != null) ? Utilities.trim(cursor.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003431 }
Sunny Goyal34942622014-08-29 17:20:55 -07003432 } else {
3433 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3434 }
3435
Winson Chung82b016c2015-05-08 17:00:10 -07003436 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003437 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003438 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003439 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003440 return info;
3441 }
3442
3443 /**
3444 * Make an Intent object for a restored application or shortcut item that points
3445 * to the market page for the item.
3446 */
Adam Cohen091440a2015-03-18 14:16:05 -07003447 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003448 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003449 return getMarketIntent(componentName.getPackageName());
3450 }
3451
3452 static Intent getMarketIntent(String packageName) {
3453 return new Intent(Intent.ACTION_VIEW)
3454 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003455 .scheme("market")
3456 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003457 .appendQueryParameter("id", packageName)
3458 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003459 }
3460
3461 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003462 * Make an ShortcutInfo object for a shortcut that is an application.
3463 *
3464 * If c is not null, then it will be used to fill in missing data like the title and icon.
3465 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003466 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003467 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003468 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003469 if (user == null) {
3470 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003471 return null;
3472 }
3473
Kenny Guyed131872014-04-30 03:02:21 +01003474 ComponentName componentName = intent.getComponent();
3475 if (componentName == null) {
3476 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3477 return null;
3478 }
3479
3480 Intent newIntent = new Intent(intent.getAction(), null);
3481 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3482 newIntent.setComponent(componentName);
3483 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003484 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003485 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3486 return null;
3487 }
3488
3489 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003490 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003491 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3492 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3493 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003494 }
3495
Joe Onorato56d82912010-03-07 14:32:10 -05003496 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003497 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003498 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003499 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003500
Joe Onorato56d82912010-03-07 14:32:10 -05003501 // fall back to the class name of the activity
3502 if (info.title == null) {
3503 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003504 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003505
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003507 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003508 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003509 if (lai != null) {
3510 info.flags = AppInfo.initFlags(lai);
3511 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003513 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003514
Sunny Goyale2df0622015-04-24 11:27:00 -07003515 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003516 ItemInfoFilter f) {
3517 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3518 for (ItemInfo i : infos) {
3519 if (i instanceof ShortcutInfo) {
3520 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003521 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003522 if (cn != null && f.filterItem(null, info, cn)) {
3523 filtered.add(info);
3524 }
3525 } else if (i instanceof FolderInfo) {
3526 FolderInfo info = (FolderInfo) i;
3527 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003528 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003529 if (cn != null && f.filterItem(info, s, cn)) {
3530 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003531 }
3532 }
Winson Chung64359a52013-07-08 17:17:08 -07003533 } else if (i instanceof LauncherAppWidgetInfo) {
3534 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3535 ComponentName cn = info.providerName;
3536 if (cn != null && f.filterItem(null, info, cn)) {
3537 filtered.add(info);
3538 }
Winson Chung8a435102012-08-30 17:16:53 -07003539 }
3540 }
Winson Chung64359a52013-07-08 17:17:08 -07003541 return new ArrayList<ItemInfo>(filtered);
3542 }
3543
Adam Cohen091440a2015-03-18 14:16:05 -07003544 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003545 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003546 ItemInfoFilter filter = new ItemInfoFilter() {
3547 @Override
3548 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003549 if (info.user == null) {
3550 return cn.equals(cname);
3551 } else {
3552 return cn.equals(cname) && info.user.equals(user);
3553 }
Winson Chung64359a52013-07-08 17:17:08 -07003554 }
3555 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003556 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003557 }
3558
Sunny Goyal1a745e82014-10-02 15:58:31 -07003559 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003560 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003561 */
Adam Cohen091440a2015-03-18 14:16:05 -07003562 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003563 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3564 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003565
Joe Onorato56d82912010-03-07 14:32:10 -05003566 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003567 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003568 // Non-app shortcuts are only supported for current user.
3569 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003571
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003572 // TODO: If there's an explicit component and we can't install that, delete it.
3573
Winson Chung82b016c2015-05-08 17:00:10 -07003574 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003575
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 int iconType = c.getInt(iconTypeIndex);
3577 switch (iconType) {
3578 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3579 String packageName = c.getString(iconPackageIndex);
3580 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003581 info.customIcon = false;
3582 // the resource
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003583 icon = Utilities.createIconBitmap(packageName, resourceName, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003584 // the db
3585 if (icon == null) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003586 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003587 }
3588 // the fallback icon
3589 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003590 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003591 info.usingFallbackIcon = true;
3592 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 break;
3594 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003595 icon = Utilities.createIconBitmap(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003596 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003597 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003598 info.customIcon = false;
3599 info.usingFallbackIcon = true;
3600 } else {
3601 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 break;
3604 default:
Kenny Guyed131872014-04-30 03:02:21 +01003605 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003606 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 info.customIcon = false;
3608 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003609 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003610 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 return info;
3612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003613
Sunny Goyal2350bc92014-10-14 16:42:54 -07003614 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003615 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3616 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3617 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3618
Adam Cohend9198822011-11-22 16:42:47 -08003619 if (intent == null) {
3620 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3621 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3622 return null;
3623 }
3624
Joe Onorato0589f0f2010-02-08 13:44:00 -08003625 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003626 boolean customIcon = false;
3627 ShortcutIconResource iconResource = null;
3628
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003629 if (bitmap instanceof Bitmap) {
3630 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003631 customIcon = true;
3632 } else {
3633 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003634 if (extra instanceof ShortcutIconResource) {
3635 iconResource = (ShortcutIconResource) extra;
3636 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003637 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003638 }
3639 }
3640
Michael Jurkac9d95c52011-08-29 14:03:34 -07003641 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003642
Kenny Guyed131872014-04-30 03:02:21 +01003643 // Only support intents for current user for now. Intents sent from other
3644 // users wouldn't get here without intent forwarding anyway.
3645 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003646 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003647 icon = mIconCache.getDefaultIcon(info.user);
3648 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003649 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003650 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003651
Winson Chung82b016c2015-05-08 17:00:10 -07003652 info.title = Utilities.trim(name);
3653 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003654 info.intent = intent;
3655 info.customIcon = customIcon;
3656 info.iconResource = iconResource;
3657
3658 return info;
3659 }
3660
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003662 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 * or make a new one.
3664 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003665 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 // See if a placeholder was created for us already
3667 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003668 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003670 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 folders.put(id, folderInfo);
3672 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003673 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003674 }
3675
Joe Onoratobe386092009-11-17 17:32:16 -08003676
Sunny Goyal651077b2014-06-30 14:15:31 -07003677 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3678 return (provider != null) && (provider.provider != null)
3679 && (provider.provider.getPackageName() != null);
3680 }
3681
Joe Onoratobe386092009-11-17 17:32:16 -08003682 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003683 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003684 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3685 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3686 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3687 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003688 if (mLoaderTask != null) {
3689 mLoaderTask.dumpState();
3690 } else {
3691 Log.d(TAG, "mLoaderTask=null");
3692 }
Joe Onoratobe386092009-11-17 17:32:16 -08003693 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003694
3695 public Callbacks getCallback() {
3696 return mCallbacks != null ? mCallbacks.get() : null;
3697 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003698
3699 /**
3700 * @return {@link FolderInfo} if its already loaded.
3701 */
3702 public FolderInfo findFolderById(Long folderId) {
3703 synchronized (sBgLock) {
3704 return sBgFolders.get(folderId);
3705 }
3706 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003707
3708 /**
3709 * @return the looper for the worker thread which can be used to start background tasks.
3710 */
3711 public static Looper getWorkerLooper() {
3712 return sWorkerThread.getLooper();
3713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003714}