blob: 676ee2a5b44e621467702f2c5945002d1cde52da [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Sunny Goyal2d648b02015-08-11 13:56:28 -070020import android.appwidget.AppWidgetManager;
Romain Guy629de3e2010-01-13 12:20:59 -080021import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070024import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040032import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.net.Uri;
Sunny Goyal3dc7bee2015-09-15 11:32:58 -070037import android.os.DeadObjectException;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070041import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Hyunyoung Song3abd5482015-06-08 18:41:04 -070045import android.os.TransactionTooLargeException;
Chris Wrenc3919c02013-09-18 09:48:33 -040046import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080047import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070050import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010051
Sunny Goyalffe83f12014-08-14 17:39:34 -070052import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.LauncherActivityInfoCompat;
54import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070055import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070056import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010057import com.android.launcher3.compat.UserHandleCompat;
58import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070059import com.android.launcher3.config.ProviderConfig;
Sunny Goyale5bb7052015-07-27 14:36:07 -070060import com.android.launcher3.model.MigrateFromRestoreTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070063import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070064import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070065import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070066import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070067import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080068
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.lang.ref.WeakReference;
70import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070071import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070073import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070074import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070075import java.util.Collections;
76import java.util.Comparator;
77import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070079import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070081import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070083import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085/**
86 * Maintains in-memory state of the Launcher. It is expected that there should be only one
87 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070088 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 */
Kenny Guyed131872014-04-30 03:02:21 +010090public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010091 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080092 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040093 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070094 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070097
Dan Sandlerd5024042014-01-09 15:01:33 -050098 public static final int LOADER_FLAG_NONE = 0;
99 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
100 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
101
Joe Onorato36115782010-06-17 13:28:48 -0400102 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500103 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800106 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400107
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk final LauncherAppState mApp;
109 @Thunk final Object mLock = new Object();
110 @Thunk DeferredHandler mHandler = new DeferredHandler();
111 @Thunk LoaderTask mLoaderTask;
112 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700113 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Jason Monkbbe1e242014-05-16 17:37:34 -0400115 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700116
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118 static {
119 sWorkerThread.start();
120 }
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 // We start off with everything not loaded. After that, we assume that
124 // our monitoring of the package manager provides all updates and we never
125 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk boolean mWorkspaceLoaded;
127 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700128
Sunny Goyal756a28a2015-04-23 17:07:55 -0700129 /**
130 * Set of runnables to be called on the background thread after the workspace binding
131 * is complete.
132 */
133 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
134
Adam Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700137 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700138 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700139 // Entire list of widgets.
140 WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142 // The lock that must be acquired before referencing any static bg data structures. Unlike
143 // other locks, this one can generally be held long-term because we never expect any of these
144 // static data structures to be referenced outside of the worker thread except on the first
145 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700146 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700150 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
153 // created by LauncherModel that are directly on the home screen (however, no widgets or
154 // shortcuts within folders).
155 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
158 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 new ArrayList<LauncherAppWidgetInfo>();
160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700162 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700163
Adam Cohendcd297f2013-06-18 13:13:40 -0700164 // sBgWorkspaceScreens is the ordered set of workspace screens.
165 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
166
Adam Cohen59400422014-03-05 18:07:04 -0800167 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000168 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800169
Sunny Goyal31860582015-09-18 08:38:57 -0700170 // sBgShortcutProviders is the set of custom shortcut providers
171 public static List<ResolveInfo> sBgShortcutProviders;
172
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700173 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700174 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
175 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 // </ only access in worker thread >
178
Adam Cohen091440a2015-03-18 14:16:05 -0700179 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Adam Cohen091440a2015-03-18 14:16:05 -0700181 @Thunk final LauncherAppsCompat mLauncherApps;
182 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700185 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700187 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700189 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
190 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700191 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700192 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700193 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800194 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200196 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700197 public void bindAppsAdded(ArrayList<Long> newScreens,
198 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700199 ArrayList<ItemInfo> addAnimated,
200 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700202 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
203 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
204 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700205 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700206 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700207 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700208 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700209 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700210 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700211 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700212 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung64359a52013-07-08 17:17:08 -0700215 public interface ItemInfoFilter {
216 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
217 }
218
Bjorn Bringert1307f632013-10-03 22:31:03 +0100219 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800220 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221
Winson Chungee055712013-07-30 14:46:24 -0700222 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700223 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400224 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
225 // resource string.
226 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
227 ProviderInfo providerInfo =
228 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
229 ProviderInfo redirectProvider =
230 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400233 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700234
235 if (mOldContentProviderExists) {
236 Log.d(TAG, "Old launcher provider exists.");
237 } else {
238 Log.d(TAG, "Old launcher provider does not exist.");
239 }
240
Daniel Sandlere4f98912013-06-25 15:13:26 -0400241 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100242 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700243 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 mIconCache = iconCache;
245
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700263 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Winson Chunge43a1e72014-01-15 10:33:02 -0800272 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
273 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800274 }
275
Sunny Goyal756adbc2015-04-16 15:20:51 -0700276 public void setPackageState(final PackageInstallInfo installInfo) {
277 Runnable updateRunnable = new Runnable() {
278
279 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500280 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700281 synchronized (sBgLock) {
282 final HashSet<ItemInfo> updates = new HashSet<>();
283
284 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
285 // Ignore install success events as they are handled by Package add events.
286 return;
287 }
288
Sunny Goyale2df0622015-04-24 11:27:00 -0700289 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700290 if (info instanceof ShortcutInfo) {
291 ShortcutInfo si = (ShortcutInfo) info;
292 ComponentName cn = si.getTargetComponent();
293 if (si.isPromise() && (cn != null)
294 && installInfo.packageName.equals(cn.getPackageName())) {
295 si.setInstallProgress(installInfo.progress);
296
297 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
298 // Mark this info as broken.
299 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
300 }
301 updates.add(si);
302 }
303 }
304 }
305
306 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
307 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
308 widget.installProgress = installInfo.progress;
309 updates.add(widget);
310 }
311 }
312
313 if (!updates.isEmpty()) {
314 // Push changes to the callback.
315 Runnable r = new Runnable() {
316 public void run() {
317 Callbacks callbacks = getCallback();
318 if (callbacks != null) {
319 callbacks.bindRestoreItemsChange(updates);
320 }
321 }
322 };
323 mHandler.post(r);
324 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500325 }
326 }
327 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500329 }
330
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 /**
332 * Updates the icons and label of all pending icons for the provided package name.
333 */
334 public void updateSessionDisplayInfo(final String packageName) {
335 Runnable updateRunnable = new Runnable() {
336
337 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700338 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 synchronized (sBgLock) {
340 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
341 final UserHandleCompat user = UserHandleCompat.myUserHandle();
342
Sunny Goyale2df0622015-04-24 11:27:00 -0700343 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 if (info instanceof ShortcutInfo) {
345 ShortcutInfo si = (ShortcutInfo) info;
346 ComponentName cn = si.getTargetComponent();
347 if (si.isPromise() && (cn != null)
348 && packageName.equals(cn.getPackageName())) {
349 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
350 // For auto install apps update the icon as well as label.
351 mIconCache.getTitleAndIcon(si,
352 si.promisedIntent, user,
353 si.shouldUseLowResIcon());
354 } else {
355 // Only update the icon for restored apps.
356 si.updateIcon(mIconCache);
357 }
358 updates.add(si);
359 }
360 }
361 }
362
363 if (!updates.isEmpty()) {
364 // Push changes to the callback.
365 Runnable r = new Runnable() {
366 public void run() {
367 Callbacks callbacks = getCallback();
368 if (callbacks != null) {
369 callbacks.bindShortcutsChanged(updates,
370 new ArrayList<ShortcutInfo>(), user);
371 }
372 }
373 };
374 mHandler.post(r);
375 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700376 }
377 }
378 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700379 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700380 }
381
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800383 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800384
385 if (allAppsApps == null) {
386 throw new RuntimeException("allAppsApps must not be null");
387 }
388 if (allAppsApps.isEmpty()) {
389 return;
390 }
391
392 // Process the newly added applications and add them to the database first
393 Runnable r = new Runnable() {
394 public void run() {
395 runOnMainThread(new Runnable() {
396 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800397 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800398 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500399 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800400 }
401 }
402 });
403 }
404 };
405 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700406 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800407
Sunny Goyala9116722015-04-29 13:55:58 -0700408 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 int[] xy, int spanX, int spanY) {
410 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
412 final int xCount = (int) profile.numColumns;
413 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 boolean[][] occupied = new boolean[xCount][yCount];
415 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700416 for (ItemInfo r : occupiedPos) {
417 int right = r.cellX + r.spanX;
418 int bottom = r.cellY + r.spanY;
419 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
420 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 occupied[x][y] = true;
422 }
423 }
424 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800425 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700426 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800427 }
428
429 /**
430 * Find a position on the screen for the given size or adds a new screen.
431 * @return screenId and the coordinates for the item.
432 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700433 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800434 ArrayList<Long> workspaceScreens,
435 ArrayList<Long> addedWorkspaceScreensFinal,
436 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700437 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438
Sunny Goyala9116722015-04-29 13:55:58 -0700439 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700440 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700441 synchronized (sBgLock) {
442 for (ItemInfo info : sBgItemsIdMap) {
443 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
444 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
445 if (items == null) {
446 items = new ArrayList<>();
447 screenItems.put(info.screenId, items);
448 }
449 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 }
453
454 // Find appropriate space for the item.
455 long screenId = 0;
456 int[] cordinates = new int[2];
457 boolean found = false;
458
459 int screenCount = workspaceScreens.size();
460 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700461 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 if (preferredScreenIndex < screenCount) {
463 screenId = workspaceScreens.get(preferredScreenIndex);
464 found = findNextAvailableIconSpaceInScreen(
465 screenItems.get(screenId), cordinates, spanX, spanY);
466 }
467
468 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700469 // Search on any of the screens starting from the first screen.
470 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800471 screenId = workspaceScreens.get(screen);
472 if (findNextAvailableIconSpaceInScreen(
473 screenItems.get(screenId), cordinates, spanX, spanY)) {
474 // We found a space for it
475 found = true;
476 break;
477 }
478 }
479 }
480
481 if (!found) {
482 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700483 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
484 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
485 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486
487 // Save the screen id for binding in the workspace
488 workspaceScreens.add(screenId);
489 addedWorkspaceScreensFinal.add(screenId);
490
491 // If we still can't find an empty space, then God help us all!!!
492 if (!findNextAvailableIconSpaceInScreen(
493 screenItems.get(screenId), cordinates, spanX, spanY)) {
494 throw new RuntimeException("Can't find space to add the item");
495 }
496 }
497 return Pair.create(screenId, cordinates);
498 }
499
500 /**
501 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700503 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700504 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800506 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700507 return;
Winson Chung997a9232013-07-24 15:33:46 -0700508 }
Winson Chung64359a52013-07-08 17:17:08 -0700509 // Process the newly added applications and add them to the database first
510 Runnable r = new Runnable() {
511 public void run() {
512 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
513 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
514
Winson Chung76828c82013-08-19 15:43:29 -0700515 // Get the list of workspace screens. We need to append to this list and
516 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
517 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800518 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700519 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800520 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700521 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800522 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700523 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700525 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 }
Winson Chung76828c82013-08-19 15:43:29 -0700527
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800528 // Find appropriate space for the item.
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700529 Pair<Long, int[]> coords = findSpaceForItem(
530 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800531 long screenId = coords.first;
532 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700533
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700534 ItemInfo itemInfo;
535 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
536 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700538 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700539 } else {
540 throw new RuntimeException("Unexpected info type");
541 }
Winson Chung94d67682013-09-25 16:29:40 -0700542
Winson Chung64359a52013-07-08 17:17:08 -0700543 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700544 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700545 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700546 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700547 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700549 }
550 }
551
Winson Chung76828c82013-08-19 15:43:29 -0700552 // Update the workspace screens
553 updateWorkspaceScreenOrder(context, workspaceScreens);
554
Adam Cohen76a47a12014-02-05 11:47:43 -0800555 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700556 runOnMainThread(new Runnable() {
557 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800558 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700559 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700560 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
561 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700562 if (!addedShortcutsFinal.isEmpty()) {
563 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
564 long lastScreenId = info.screenId;
565 for (ItemInfo i : addedShortcutsFinal) {
566 if (i.screenId == lastScreenId) {
567 addAnimated.add(i);
568 } else {
569 addNotAnimated.add(i);
570 }
Winson Chung997a9232013-07-24 15:33:46 -0700571 }
572 }
Winson Chungd64d1762013-08-20 14:37:16 -0700573 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800574 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700575 }
Winson Chung64359a52013-07-08 17:17:08 -0700576 }
Winson Chung997a9232013-07-24 15:33:46 -0700577 });
578 }
Winson Chung64359a52013-07-08 17:17:08 -0700579 }
580 };
581 runOnWorkerThread(r);
582 }
583
Sunny Goyald33860f2015-04-23 16:02:20 -0700584 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700585 if (sWorkerThread.getThreadId() == Process.myTid()) {
586 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
587 "main thread");
588 }
589
Sunny Goyald33860f2015-04-23 16:02:20 -0700590 // Remove any queued UI runnables
591 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700592 // Unbind all the workspace items
593 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700594 }
595
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700596 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700597 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700598 // Ensure that we don't use the same workspace items data structure on the main thread
599 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700600 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700601 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700602 tmpItems.addAll(sBgWorkspaceItems);
603 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700604 }
605 Runnable r = new Runnable() {
606 @Override
607 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700608 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700609 item.unbind();
610 }
611 }
612 };
613 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700614 }
615
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 /**
617 * Adds an item to the DB if it was not created previously, or move it to a new
618 * <container, screen, cellX, cellY>
619 */
620 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700621 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622 if (item.container == ItemInfo.NO_ID) {
623 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700624 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 } else {
626 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700627 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 }
629 }
630
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700631 static void checkItemInfoLocked(
632 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
633 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
634 if (modelItem != null && item != modelItem) {
635 // check all the data is consistent
636 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
637 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
638 ShortcutInfo shortcut = (ShortcutInfo) item;
639 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
640 modelShortcut.intent.filterEquals(shortcut.intent) &&
641 modelShortcut.id == shortcut.id &&
642 modelShortcut.itemType == shortcut.itemType &&
643 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700645 modelShortcut.cellX == shortcut.cellX &&
646 modelShortcut.cellY == shortcut.cellY &&
647 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700648 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700649 // For all intents and purposes, this is the same object
650 return;
651 }
652 }
653
654 // the modelItem needs to match up perfectly with item if our model is
655 // to be consistent with the database-- for now, just require
656 // modelItem == item or the equality check above
657 String msg = "item: " + ((item != null) ? item.toString() : "null") +
658 "modelItem: " +
659 ((modelItem != null) ? modelItem.toString() : "null") +
660 "Error: ItemInfo passed to checkItemInfo doesn't match original";
661 RuntimeException e = new RuntimeException(msg);
662 if (stackTrace != null) {
663 e.setStackTrace(stackTrace);
664 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800665 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700666 }
667 }
668
Michael Jurka816474f2012-06-25 14:49:02 -0700669 static void checkItemInfo(final ItemInfo item) {
670 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
671 final long itemId = item.id;
672 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700673 public void run() {
674 synchronized (sBgLock) {
675 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700676 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700677 }
678 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700679 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700680 }
681
Michael Jurkac9d95c52011-08-29 14:03:34 -0700682 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
683 final ItemInfo item, final String callingFunction) {
684 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700685 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700686 final ContentResolver cr = context.getContentResolver();
687
Adam Cohen487f7dd2012-06-28 18:12:10 -0700688 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700689 Runnable r = new Runnable() {
690 public void run() {
691 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700692 updateItemArrays(item, itemId, stackTrace);
693 }
694 };
695 runOnWorkerThread(r);
696 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700697
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
699 final ArrayList<ItemInfo> items, final String callingFunction) {
700 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700701
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
703 Runnable r = new Runnable() {
704 public void run() {
705 ArrayList<ContentProviderOperation> ops =
706 new ArrayList<ContentProviderOperation>();
707 int count = items.size();
708 for (int i = 0; i < count; i++) {
709 ItemInfo item = items.get(i);
710 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700711 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700712 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700713
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700714 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
715 updateItemArrays(item, itemId, stackTrace);
716
717 }
718 try {
719 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
720 } catch (Exception e) {
721 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700722 }
723 }
724 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700725 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700726 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700727
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700728 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
729 // Lock on mBgLock *after* the db operation
730 synchronized (sBgLock) {
731 checkItemInfoLocked(itemId, item, stackTrace);
732
733 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
734 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
735 // Item is in a folder, make sure this folder exists
736 if (!sBgFolders.containsKey(item.container)) {
737 // An items container is being set to a that of an item which is not in
738 // the list of Folders.
739 String msg = "item: " + item + " container being set to: " +
740 item.container + ", not in the list of folders";
741 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700742 }
743 }
744
745 // Items are added/removed from the corresponding FolderInfo elsewhere, such
746 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
747 // that are on the desktop, as appropriate
748 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800749 if (modelItem != null &&
750 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
751 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 switch (modelItem.itemType) {
753 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
754 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
755 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
756 if (!sBgWorkspaceItems.contains(modelItem)) {
757 sBgWorkspaceItems.add(modelItem);
758 }
759 break;
760 default:
761 break;
762 }
763 } else {
764 sBgWorkspaceItems.remove(modelItem);
765 }
766 }
767 }
768
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700771 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700772 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775 item.cellX = cellX;
776 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700777
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 // We store hotseat items in canonical form which is this orientation invariant position
779 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700781 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786
787 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
790 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800791 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793
Michael Jurkac9d95c52011-08-29 14:03:34 -0700794 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700795 }
796
797 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700798 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
799 * cellX, cellY have already been updated on the ItemInfos.
800 */
801 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
802 final long container, final int screen) {
803
804 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
805 int count = items.size();
806
807 for (int i = 0; i < count; i++) {
808 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700809 item.container = container;
810
811 // We store hotseat items in canonical form which is this orientation invariant position
812 // in the hotseat
813 if (context instanceof Launcher && screen < 0 &&
814 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700816 item.cellY);
817 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700818 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700819 }
820
821 final ContentValues values = new ContentValues();
822 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
823 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
824 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800825 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700826 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700827
828 contentValues.add(values);
829 }
830 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
831 }
832
833 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700834 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800835 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700836 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700837 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700838 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800839 item.cellX = cellX;
840 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700841 item.spanX = spanX;
842 item.spanY = spanY;
843
844 // We store hotseat items in canonical form which is this orientation invariant position
845 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700847 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700849 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 }
Adam Cohend4844c32011-02-18 19:25:06 -0800852
Adam Cohend4844c32011-02-18 19:25:06 -0800853 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800854 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
856 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800857 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700858 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
859 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800861
Michael Jurka816474f2012-06-25 14:49:02 -0700862 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700863 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700864
865 /**
866 * Update an item to the database in a specified container.
867 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700868 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700869 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100870 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700871 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800872 }
873
Sunny Goyal756a28a2015-04-23 17:07:55 -0700874 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700875 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700876 synchronized (mLock) {
877 if (!mHasLoaderCompletedOnce ||
878 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
879 throw new RuntimeException("Trying to add shortcut while loader is running");
880 }
881 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700882 }
883 }
884
Adam Cohend4844c32011-02-18 19:25:06 -0800885 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700886 * Returns true if the shortcuts already exists on the workspace. This must be called after
887 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700889 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
890 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700891 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700892 if (intent.getComponent() != null) {
893 // If component is not null, an intent with null package will produce
894 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700895 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700896 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700897 intentWithPkg = intent.toUri(0);
898 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700899 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700900 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
901 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700902 }
903 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700904 intentWithPkg = intent.toUri(0);
905 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700906 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700907
908 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700909 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700910 if (item instanceof ShortcutInfo) {
911 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700912 Intent targetIntent = info.promisedIntent == null
913 ? info.intent : info.promisedIntent;
914 if (targetIntent != null && info.user.equals(user)) {
915 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700916 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
917 return true;
918 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700919 }
920 }
921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700923 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700924 }
925
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 * Add an item to the database in a specified container. Sets the container, screen, cellX and
928 * cellY fields of the item. Also assigns an ID to the item.
929 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700930 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700931 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 item.cellX = cellX;
934 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700935 // We store hotseat items in canonical form which is this orientation invariant position
936 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943
944 final ContentValues values = new ContentValues();
945 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100946 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947
Sunny Goyald2497482015-09-22 18:24:19 -0700948 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
949 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
950
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700952
Jason Monk8e19cf22014-03-20 15:06:57 -0400953 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700954 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700955 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700956 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700958 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700959 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400960 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700961 sBgItemsIdMap.put(item.id, item);
962 switch (item.itemType) {
963 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
964 sBgFolders.put(item.id, (FolderInfo) item);
965 // Fall through
966 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
967 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
968 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
969 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
970 sBgWorkspaceItems.add(item);
971 } else {
972 if (!sBgFolders.containsKey(item.container)) {
973 // Adding an item to a folder that doesn't exist.
974 String msg = "adding item: " + item + " to a folder that " +
975 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700976 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700977 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700978 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700979 break;
980 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
981 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
982 break;
983 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700984 }
985 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700986 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700987 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700988 }
989
Sunny Goyal34942622014-08-29 17:20:55 -0700990 private static ArrayList<ItemInfo> getItemsByPackageName(
991 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700992 ItemInfoFilter filter = new ItemInfoFilter() {
993 @Override
994 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
995 return cn.getPackageName().equals(pn) && info.user.equals(user);
996 }
997 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700998 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700999 }
1000
1001 /**
1002 * Removes all the items from the database corresponding to the specified package.
1003 */
1004 static void deletePackageFromDatabase(Context context, final String pn,
1005 final UserHandleCompat user) {
1006 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001007 }
1008
1009 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001010 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001012 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001013 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1014 items.add(item);
1015 deleteItemsFromDatabase(context, items);
1016 }
1017
1018 /**
1019 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001020 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001021 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001023 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001024 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001025 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001026 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001027 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001028
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001029 // Lock on mBgLock *after* the db operation
1030 synchronized (sBgLock) {
1031 switch (item.itemType) {
1032 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1033 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001034 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001035 if (info.container == item.id) {
1036 // We are deleting a folder which still contains items that
1037 // think they are contained by that folder.
1038 String msg = "deleting a folder (" + item + ") which still " +
1039 "contains items (" + info + ")";
1040 Log.e(TAG, msg);
1041 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001042 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001043 sBgWorkspaceItems.remove(item);
1044 break;
1045 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1046 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1047 sBgWorkspaceItems.remove(item);
1048 break;
1049 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1050 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1051 break;
1052 }
1053 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001055 }
1056 }
Michael Jurka83df1882011-08-31 20:59:26 -07001057 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 }
1060
1061 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 * Update the order of the workspace screens in the database. The array list contains
1063 * a list of screen ids in the order that they should appear.
1064 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001065 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001066 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 final ContentResolver cr = context.getContentResolver();
1068 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1069
1070 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001071 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001072 while (iter.hasNext()) {
1073 long id = iter.next();
1074 if (id < 0) {
1075 iter.remove();
1076 }
1077 }
1078
1079 Runnable r = new Runnable() {
1080 @Override
1081 public void run() {
Yura085c8532014-02-11 15:15:29 +00001082 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001084 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001085 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 for (int i = 0; i < count; i++) {
1087 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001088 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1090 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001091 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001092 }
Yura085c8532014-02-11 15:15:29 +00001093
1094 try {
1095 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1096 } catch (Exception ex) {
1097 throw new RuntimeException(ex);
1098 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001099
Winson Chungba9c37f2013-08-30 14:11:37 -07001100 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001101 sBgWorkspaceScreens.clear();
1102 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001103 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 }
1105 };
1106 runOnWorkerThread(r);
1107 }
1108
1109 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001110 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 */
Winsonc0b52fe2015-09-09 16:38:15 -07001112 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 final ContentResolver cr = context.getContentResolver();
1114
Michael Jurkac9d95c52011-08-29 14:03:34 -07001115 Runnable r = new Runnable() {
1116 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001117 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001119 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 sBgItemsIdMap.remove(info.id);
1121 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 sBgWorkspaceItems.remove(info);
1123 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001124
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001125 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001126 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001128 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 for (ItemInfo childInfo : info.contents) {
1130 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001132 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001133 }
1134 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001135 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 }
1137
1138 /**
1139 * Set this as the current Launcher activity object for the loader.
1140 */
1141 public void initialize(Callbacks callbacks) {
1142 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001143 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1144 // workspace to prevent leaking Launcher activities on orientation change.
1145 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001146 mCallbacks = new WeakReference<Callbacks>(callbacks);
1147 }
1148 }
1149
Kenny Guyed131872014-04-30 03:02:21 +01001150 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001151 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001152 int op = PackageUpdatedTask.OP_UPDATE;
1153 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1154 user));
1155 }
1156
1157 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001158 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001159 int op = PackageUpdatedTask.OP_REMOVE;
1160 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1161 user));
1162 }
1163
1164 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001165 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001166 int op = PackageUpdatedTask.OP_ADD;
1167 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1168 user));
1169 }
1170
1171 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001172 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001173 boolean replacing) {
1174 if (!replacing) {
1175 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1176 user));
1177 if (mAppsCanBeOnRemoveableStorage) {
1178 // Only rebind if we support removable storage. It catches the
1179 // case where
1180 // apps on the external sd card need to be reloaded
1181 startLoaderFromBackground();
1182 }
1183 } else {
1184 // If we are replacing then just update the packages in the list
1185 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1186 packageNames, user));
1187 }
1188 }
1189
1190 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001191 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001192 boolean replacing) {
1193 if (!replacing) {
1194 enqueuePackageUpdated(new PackageUpdatedTask(
1195 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1196 user));
1197 }
Kenny Guyed131872014-04-30 03:02:21 +01001198 }
1199
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001200 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1202 * ACTION_PACKAGE_CHANGED.
1203 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001204 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001205 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001206 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001207
Joe Onorato36115782010-06-17 13:28:48 -04001208 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001209 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001210 // If we have changed locale we need to clear out the labels in all apps/workspace.
1211 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001212 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001213 Callbacks callbacks = getCallback();
1214 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001215 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001216 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001217 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1218 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001219 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001220 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001221 }
1222 }
1223
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001224 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001225 resetLoadedState(true, true);
1226
Reena Lee93f824a2011-09-23 17:20:28 -07001227 // Do this here because if the launcher activity is running it will be restarted.
1228 // If it's not running startLoaderFromBackground will merely tell it that it needs
1229 // to reload.
1230 startLoaderFromBackground();
1231 }
1232
Winson Chungf0c6ae02012-03-21 16:10:31 -07001233 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1234 synchronized (mLock) {
1235 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1236 // mWorkspaceLoaded to true later
1237 stopLoaderLocked();
1238 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1239 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1240 }
1241 }
1242
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001243 /**
1244 * When the launcher is in the background, it's possible for it to miss paired
1245 * configuration changes. So whenever we trigger the loader from the background
1246 * tell the launcher that it needs to re-run the loader when it comes back instead
1247 * of doing it now.
1248 */
1249 public void startLoaderFromBackground() {
1250 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001251 Callbacks callbacks = getCallback();
1252 if (callbacks != null) {
1253 // Only actually run the loader if they're not paused.
1254 if (!callbacks.setLoadOnResume()) {
1255 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001256 }
1257 }
1258 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001259 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001260 }
Joe Onorato36115782010-06-17 13:28:48 -04001261 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001262
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001263 /**
1264 * If there is already a loader task running, tell it to stop.
1265 */
1266 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001267 LoaderTask oldTask = mLoaderTask;
1268 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001269 oldTask.stopLocked();
1270 }
Reena Lee93f824a2011-09-23 17:20:28 -07001271 }
1272
Adam Cohen1a85c582014-09-30 09:48:49 -07001273 public boolean isCurrentCallbacks(Callbacks callbacks) {
1274 return (mCallbacks != null && mCallbacks.get() == callbacks);
1275 }
1276
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001277 public void startLoader(int synchronousBindPage) {
1278 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001279 }
1280
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001281 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001282 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1283 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001284 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001285 // Don't bother to start the thread if we know it's not going to do anything
1286 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001287 final Callbacks oldCallbacks = mCallbacks.get();
1288 // Clear any pending bind-runnables from the synchronized load process.
1289 runOnMainThread(new Runnable() {
1290 public void run() {
1291 oldCallbacks.clearPendingBinds();
1292 }
1293 });
1294
Joe Onorato36115782010-06-17 13:28:48 -04001295 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001296 stopLoaderLocked();
1297 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001298 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001299 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001300 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1301 } else {
1302 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1303 sWorker.post(mLoaderTask);
1304 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001306 }
1307 }
1308
Joe Onorato36115782010-06-17 13:28:48 -04001309 public void stopLoader() {
1310 synchronized (mLock) {
1311 if (mLoaderTask != null) {
1312 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001313 }
1314 }
Joe Onorato36115782010-06-17 13:28:48 -04001315 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001317 /**
1318 * Loads the workspace screen ids in an ordered list.
1319 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001320 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001321 final ContentResolver contentResolver = context.getContentResolver();
1322 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001323
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001324 // Get screens ordered by rank.
1325 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1326 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1327 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001328 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001329 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001330 while (sc.moveToNext()) {
1331 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001332 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001333 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001334 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001335 }
1336 }
1337 } finally {
1338 sc.close();
1339 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001340 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001341 }
1342
Joe Onorato36115782010-06-17 13:28:48 -04001343 /**
1344 * Runnable for the thread that loads the contents of the launcher:
1345 * - workspace icons
1346 * - widgets
1347 * - all apps icons
1348 */
1349 private class LoaderTask implements Runnable {
1350 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001351 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001352 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001353 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001354 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001355
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001356 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001357 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001358 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 }
1360
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001361 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001362 mIsLoadingAndBindingWorkspace = true;
1363
Joe Onorato36115782010-06-17 13:28:48 -04001364 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001365 if (DEBUG_LOADERS) {
1366 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001368
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001369 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001370 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001371 synchronized (LoaderTask.this) {
1372 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001373 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001374 }
1375 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 }
1377 }
1378
Joe Onorato36115782010-06-17 13:28:48 -04001379 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001380 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001381 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001382
Joe Onorato36115782010-06-17 13:28:48 -04001383 private void waitForIdle() {
1384 // Wait until the either we're stopped or the other threads are done.
1385 // This way we don't start loading all apps until the workspace has settled
1386 // down.
1387 synchronized (LoaderTask.this) {
1388 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001389
Joe Onorato36115782010-06-17 13:28:48 -04001390 mHandler.postIdle(new Runnable() {
1391 public void run() {
1392 synchronized (LoaderTask.this) {
1393 mLoadAndBindStepFinished = true;
1394 if (DEBUG_LOADERS) {
1395 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001396 }
Joe Onorato36115782010-06-17 13:28:48 -04001397 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001398 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001399 }
Joe Onorato36115782010-06-17 13:28:48 -04001400 });
1401
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001402 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001403 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001404 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1405 // wait no longer than 1sec at a time
1406 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001407 } catch (InterruptedException ex) {
1408 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 }
1410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 if (DEBUG_LOADERS) {
1412 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001413 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001414 + "ms for previous step to finish binding");
1415 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001416 }
Joe Onorato36115782010-06-17 13:28:48 -04001417 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001418
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001419 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001420 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 // Ensure that we have a valid page index to load synchronously
1422 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1423 "valid page index");
1424 }
1425 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1426 // Ensure that we don't try and bind a specified page when the pages have not been
1427 // loaded already (we should load everything asynchronously in that case)
1428 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1429 }
1430 synchronized (mLock) {
1431 if (mIsLoaderTaskRunning) {
1432 // Ensure that we are never running the background loading at this point since
1433 // we also touch the background collections
1434 throw new RuntimeException("Error! Background loading is already running");
1435 }
1436 }
1437
1438 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1439 // data structures, we can't allow any other thread to touch that data, but because
1440 // this call is synchronous, we can get away with not locking).
1441
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001442 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001443 // operations from the previous activity. We need to ensure that all queued operations
1444 // are executed before any synchronous binding work is done.
1445 mHandler.flush();
1446
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 // Divide the set of loaded items into those that we are binding synchronously, and
1448 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001449 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1451 // arise from that.
1452 onlyBindAllApps();
1453 }
1454
Joe Onorato36115782010-06-17 13:28:48 -04001455 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001457 if (mStopped) {
1458 return;
1459 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001460 mIsLoaderTaskRunning = true;
1461 }
Joe Onorato36115782010-06-17 13:28:48 -04001462 // Optimize for end-user experience: if the Launcher is up and // running with the
1463 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1464 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001465 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001466 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001467 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001468
Joe Onorato36115782010-06-17 13:28:48 -04001469 if (mStopped) {
1470 break keep_running;
1471 }
1472
Joe Onorato36115782010-06-17 13:28:48 -04001473 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001474
1475 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001476 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1477 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479
Joe Onorato36115782010-06-17 13:28:48 -04001480 // Clear out this reference, otherwise we end up holding it until all of the
1481 // callback runnables are done.
1482 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483
Joe Onorato36115782010-06-17 13:28:48 -04001484 synchronized (mLock) {
1485 // If we are still the last one to be scheduled, remove ourselves.
1486 if (mLoaderTask == this) {
1487 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001490 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
1493
1494 public void stopLocked() {
1495 synchronized (LoaderTask.this) {
1496 mStopped = true;
1497 this.notify();
1498 }
1499 }
1500
1501 /**
1502 * Gets the callbacks object. If we've been stopped, or if the launcher object
1503 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1504 * object that was around when the deferred message was scheduled, and if there's
1505 * a new Callbacks object around then also return null. This will save us from
1506 * calling onto it with data that will be ignored.
1507 */
1508 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1509 synchronized (mLock) {
1510 if (mStopped) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 if (mCallbacks == null) {
1515 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001516 }
Joe Onorato36115782010-06-17 13:28:48 -04001517
1518 final Callbacks callbacks = mCallbacks.get();
1519 if (callbacks != oldCallbacks) {
1520 return null;
1521 }
1522 if (callbacks == null) {
1523 Log.w(TAG, "no mCallbacks");
1524 return null;
1525 }
1526
1527 return callbacks;
1528 }
1529 }
1530
1531 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001532 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1533 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001534 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001535 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001536 final int countX = profile.numColumns;
1537 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001538
Adam Cohendcd297f2013-06-18 13:13:40 -07001539 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001540 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001541 // Return early if we detect that an item is under the hotseat button
1542 if (mCallbacks == null ||
1543 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001544 Log.e(TAG, "Error loading shortcut into hotseat " + item
1545 + " into position (" + item.screenId + ":" + item.cellX + ","
1546 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001547 return false;
1548 }
1549
Dan Sandler295ae182013-12-10 16:05:47 -05001550 final ItemInfo[][] hotseatItems =
1551 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1552
Adam Cohen2e6da152015-05-06 11:42:25 -07001553 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001554 Log.e(TAG, "Error loading shortcut " + item
1555 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001557 + ")");
1558 return false;
1559 }
1560
Dan Sandler295ae182013-12-10 16:05:47 -05001561 if (hotseatItems != null) {
1562 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 Log.e(TAG, "Error loading shortcut into hotseat " + item
1564 + " into position (" + item.screenId + ":" + item.cellX + ","
1565 + item.cellY + ") occupied by "
1566 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1567 [(int) item.screenId][0]);
1568 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001569 } else {
1570 hotseatItems[(int) item.screenId][0] = item;
1571 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001572 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001573 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001574 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001575 items[(int) item.screenId][0] = item;
1576 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001577 return true;
1578 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001579 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1580 if (!workspaceScreens.contains((Long) item.screenId)) {
1581 // The item has an invalid screen id.
1582 return false;
1583 }
1584 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001585 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001586 return true;
1587 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001588
Adam Cohendcd297f2013-06-18 13:13:40 -07001589 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001590 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001591 occupied.put(item.screenId, items);
1592 }
1593
Dan Sandler295ae182013-12-10 16:05:47 -05001594 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001595 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1596 item.cellX < 0 || item.cellY < 0 ||
1597 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1598 Log.e(TAG, "Error loading shortcut " + item
1599 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1600 + item.cellX + "," + item.cellY
1601 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1602 return false;
1603 }
1604
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001605 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001606 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1607 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001608 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001609 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001611 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001612 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001613 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001614 return false;
1615 }
1616 }
1617 }
1618 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1619 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001620 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001621 }
1622 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001623
Joe Onorato36115782010-06-17 13:28:48 -04001624 return true;
1625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626
Winson Chungba9c37f2013-08-30 14:11:37 -07001627 /** Clears all the sBg data structures */
1628 private void clearSBgDataStructures() {
1629 synchronized (sBgLock) {
1630 sBgWorkspaceItems.clear();
1631 sBgAppWidgets.clear();
1632 sBgFolders.clear();
1633 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001634 sBgWorkspaceScreens.clear();
1635 }
1636 }
1637
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001638 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001639 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640
Joe Onorato36115782010-06-17 13:28:48 -04001641 final Context context = mContext;
1642 final ContentResolver contentResolver = context.getContentResolver();
1643 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001644 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001645 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001646 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001647
Winson Chung892c74d2013-08-22 16:15:50 -07001648 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001649 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001650 int countX = profile.numColumns;
1651 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001652
Sunny Goyal6579e1e2015-08-11 12:10:34 -07001653 if (MigrateFromRestoreTask.ENABLED && MigrateFromRestoreTask.shouldRunTask(mContext)) {
Sunny Goyald934e0b2015-07-31 12:40:57 -07001654 long migrationStartTime = System.currentTimeMillis();
1655 Log.v(TAG, "Starting workspace migration after restore");
Sunny Goyale5bb7052015-07-27 14:36:07 -07001656 try {
1657 MigrateFromRestoreTask task = new MigrateFromRestoreTask(mContext);
1658 // Clear the flags before starting the task, so that we do not run the task
1659 // again, in case there was an uncaught error.
1660 MigrateFromRestoreTask.clearFlags(mContext);
1661 task.execute();
1662 } catch (Exception e) {
1663 Log.e(TAG, "Error during grid migration", e);
1664
1665 // Clear workspace.
1666 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
1667 }
Sunny Goyald934e0b2015-07-31 12:40:57 -07001668 Log.v(TAG, "Workspace migration completed in "
1669 + (System.currentTimeMillis() - migrationStartTime));
Sunny Goyale5bb7052015-07-27 14:36:07 -07001670 }
1671
Dan Sandlerd5024042014-01-09 15:01:33 -05001672 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001673 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001674 LauncherSettings.Settings.call(contentResolver,
1675 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001676 }
1677
1678 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1679 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001680 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001681 LauncherSettings.Settings.call(contentResolver,
1682 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001683 } else {
1684 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001685 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001686 LauncherSettings.Settings.call(contentResolver,
1687 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001688 }
Adam Cohene25af792013-06-06 23:08:25 -07001689
Winson Chung2abf94d2012-07-18 18:16:38 -07001690 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001691 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001692 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001693 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001694 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001695
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001696 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001697 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001698 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001699 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001700 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001701
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 // +1 for the hotseat (it can be larger than the workspace)
1703 // Load workspace in reverse order to ensure that latest items are loaded first (and
1704 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001705 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001706
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 try {
1708 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1709 final int intentIndex = c.getColumnIndexOrThrow
1710 (LauncherSettings.Favorites.INTENT);
1711 final int titleIndex = c.getColumnIndexOrThrow
1712 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 final int containerIndex = c.getColumnIndexOrThrow(
1714 LauncherSettings.Favorites.CONTAINER);
1715 final int itemTypeIndex = c.getColumnIndexOrThrow(
1716 LauncherSettings.Favorites.ITEM_TYPE);
1717 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1718 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001719 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001721 final int screenIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.SCREEN);
1723 final int cellXIndex = c.getColumnIndexOrThrow
1724 (LauncherSettings.Favorites.CELLX);
1725 final int cellYIndex = c.getColumnIndexOrThrow
1726 (LauncherSettings.Favorites.CELLY);
1727 final int spanXIndex = c.getColumnIndexOrThrow
1728 (LauncherSettings.Favorites.SPANX);
1729 final int spanYIndex = c.getColumnIndexOrThrow(
1730 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001731 final int rankIndex = c.getColumnIndexOrThrow(
1732 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001733 final int restoredIndex = c.getColumnIndexOrThrow(
1734 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001735 final int profileIdIndex = c.getColumnIndexOrThrow(
1736 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001737 final int optionsIndex = c.getColumnIndexOrThrow(
1738 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001739 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001740
Sunny Goyal7f834d22015-04-21 10:10:23 -07001741 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
1742 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
1743 allUsers.put(mUserManager.getSerialNumberForUser(user), user);
1744 }
1745
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 ShortcutInfo info;
1747 String intentDescription;
1748 LauncherAppWidgetInfo appWidgetInfo;
1749 int container;
1750 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001751 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001753 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 while (!mStopped && c.moveToNext()) {
1756 try {
1757 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001758 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001759 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001760 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 switch (itemType) {
1763 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1764 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001765 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001767 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001768 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001769 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001770 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001771 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001772 if (user == null) {
1773 // User has been deleted remove the item.
1774 itemsToRemove.add(id);
1775 continue;
1776 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 try {
1778 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001779 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001780 if (cn != null && cn.getPackageName() != null) {
1781 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1782 cn.getPackageName(), user);
1783 boolean validComponent = validPkg &&
1784 launcherApps.isActivityEnabledForProfile(cn, user);
1785
1786 if (validComponent) {
1787 if (restored) {
1788 // no special handling necessary for this item
1789 restoredRows.add(id);
1790 restored = false;
1791 }
1792 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001793 intent = null;
1794 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1795 // We allow auto install apps to have their intent
1796 // updated after an install.
1797 intent = manager.getLaunchIntentForPackage(
1798 cn.getPackageName());
1799 if (intent != null) {
1800 ContentValues values = new ContentValues();
1801 values.put(LauncherSettings.Favorites.INTENT,
1802 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001803 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001804 }
1805 }
1806
1807 if (intent == null) {
1808 // The app is installed but the component is no
1809 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001810 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001811 itemsToRemove.add(id);
1812 continue;
1813 } else {
1814 // no special handling necessary for this item
1815 restoredRows.add(id);
1816 restored = false;
1817 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001818 } else if (restored) {
1819 // Package is not yet available but might be
1820 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001821 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001822
1823 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1824 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001825 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001826 // App restore has started. Update the flag
1827 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1828 ContentValues values = new ContentValues();
1829 values.put(LauncherSettings.Favorites.RESTORED,
1830 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001831 updateItem(id, values);
1832 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1833 // This is a common app. Try to replace this.
1834 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1835 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1836 if (parser.findDefaultApp()) {
1837 // Default app found. Replace it.
1838 intent = parser.parsedIntent;
1839 cn = intent.getComponent();
1840 ContentValues values = parser.parsedValues;
1841 values.put(LauncherSettings.Favorites.RESTORED, 0);
1842 updateItem(id, values);
1843 restored = false;
1844 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001845
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001846 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001847 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001848 itemsToRemove.add(id);
1849 continue;
1850 }
Sunny Goyal94485362014-09-18 16:13:58 -07001851 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001852 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001853 itemsToRemove.add(id);
1854 continue;
1855 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001856 } else if (launcherApps.isAppEnabled(
1857 manager, cn.getPackageName(),
1858 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1859 // Package is present but not available.
1860 allowMissingTarget = true;
1861 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1862 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001863 // SdCard is not ready yet. Package might get available,
1864 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001865 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001866 HashSet<String> pkgs = sPendingPackages.get(user);
1867 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001868 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001869 sPendingPackages.put(user, pkgs);
1870 }
1871 pkgs.add(cn.getPackageName());
1872 allowMissingTarget = true;
1873 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001874
1875 } else {
1876 // Do not wait for external media load anymore.
1877 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001878 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001879 itemsToRemove.add(id);
1880 continue;
Winson Chungee055712013-07-30 14:46:24 -07001881 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001882 } else if (cn == null) {
1883 // For shortcuts with no component, keep them as they are
1884 restoredRows.add(id);
1885 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001886 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001888 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001889 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 continue;
1891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001892
Sunny Goyal34b65272015-03-11 16:56:52 -07001893 boolean useLowResIcon = container >= 0 &&
1894 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1895
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001896 if (itemReplaced) {
1897 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001898 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001899 cursorIconInfo.iconIndex, titleIndex,
1900 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001901 } else {
1902 // Don't replace items for other profiles.
1903 itemsToRemove.add(id);
1904 continue;
1905 }
1906 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001907 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001908 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001909 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001910 intent = getRestoredItemIntent(c, context, intent);
1911 } else {
1912 // Don't restore items for other profiles.
1913 itemsToRemove.add(id);
1914 continue;
1915 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001916 } else if (itemType ==
1917 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001918 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001919 cursorIconInfo.iconIndex, titleIndex,
1920 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001921 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001922 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001923
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 // App shortcuts that used to be automatically added to Launcher
1925 // didn't always have the correct intent flags set, so do that
1926 // here
1927 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001928 intent.getCategories() != null &&
1929 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001930 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 intent.addFlags(
1932 Intent.FLAG_ACTIVITY_NEW_TASK |
1933 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1934 }
Michael Jurka96879562012-03-22 05:54:33 -07001935 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001938 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001939 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001940 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001942 info.cellX = c.getInt(cellXIndex);
1943 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001944 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001945 info.spanX = 1;
1946 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001947 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001948 if (info.promisedIntent != null) {
1949 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1950 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001951 info.isDisabled = disabledState;
1952 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1953 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1954 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001955
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001956 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001957 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001958 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 break;
1960 }
1961
Sunny Goyal756adbc2015-04-16 15:20:51 -07001962 if (restored) {
1963 ComponentName cn = info.getTargetComponent();
1964 if (cn != null) {
1965 Integer progress = installingPkgs.get(cn.getPackageName());
1966 if (progress != null) {
1967 info.setInstallProgress(progress);
1968 } else {
1969 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1970 }
1971 }
1972 }
1973
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 switch (container) {
1975 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1976 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1977 sBgWorkspaceItems.add(info);
1978 break;
1979 default:
1980 // Item is in a user folder
1981 FolderInfo folderInfo =
1982 findOrMakeFolder(sBgFolders, container);
1983 folderInfo.add(info);
1984 break;
1985 }
1986 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001987 } else {
1988 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 }
1990 break;
1991
1992 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1993 id = c.getLong(idIndex);
1994 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1995
Sunny Goyala508e4f2015-05-21 09:33:57 -07001996 // Do not trim the folder label, as is was set by the user.
1997 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002000 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 folderInfo.cellX = c.getInt(cellXIndex);
2002 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002003 folderInfo.spanX = 1;
2004 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002005 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006
Daniel Sandler8802e962010-05-26 16:28:16 -04002007 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002008 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002009 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002010 break;
2011 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002012
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2015 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2016 sBgWorkspaceItems.add(folderInfo);
2017 break;
Joe Onorato36115782010-06-17 13:28:48 -04002018 }
Joe Onorato17a89222011-02-08 17:26:11 -08002019
Chris Wrenf4d08112014-01-16 18:13:56 -05002020 if (restored) {
2021 // no special handling required for restored folders
2022 restoredRows.add(id);
2023 }
2024
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2026 sBgFolders.put(folderInfo.id, folderInfo);
2027 break;
2028
2029 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002030 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002032 boolean customWidget = itemType ==
2033 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2034
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002036 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002037 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002038 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002039 user = allUsers.get(serialNumber);
2040 if (user == null) {
2041 itemsToRemove.add(id);
2042 continue;
2043 }
2044
Sunny Goyalff572272014-07-23 13:58:07 -07002045 final ComponentName component =
2046 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002047
Sunny Goyal651077b2014-06-30 14:15:31 -07002048 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002049 final boolean isIdValid = (restoreStatus &
2050 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002051 final boolean wasProviderReady = (restoreStatus &
2052 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002053
Adam Cohen59400422014-03-05 18:07:04 -08002054 final LauncherAppWidgetProviderInfo provider =
2055 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002056 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002057 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002058
2059 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002060 if (!isSafeMode && !customWidget &&
2061 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002062 addDumpLog("Deleting widget that isn't installed anymore: "
2063 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 itemsToRemove.add(id);
2065 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002066 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002067 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2068 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002069
Sunny Goyal53f96722015-07-13 19:54:53 -07002070 // The provider is available. So the widget is either
2071 // available or not available. We do not need to track
2072 // any future restore updates.
2073 int status = restoreStatus &
2074 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002075 if (!wasProviderReady) {
2076 // If provider was not previously ready, update the
2077 // status and UI flag.
2078
2079 // Id would be valid only if the widget restore broadcast was received.
2080 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002081 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002082 } else {
2083 status &= ~LauncherAppWidgetInfo
2084 .FLAG_PROVIDER_NOT_READY;
2085 }
2086 }
2087 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002088 } else {
2089 Log.v(TAG, "Widget restore pending id=" + id
2090 + " appWidgetId=" + appWidgetId
2091 + " status =" + restoreStatus);
2092 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002093 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002094 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002095 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002096
2097 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2098 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002099 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002100 // App restore has started. Update the flag
2101 appWidgetInfo.restoreStatus |=
2102 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002103 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002104 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002105 itemsToRemove.add(id);
2106 continue;
2107 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002108
2109 appWidgetInfo.installProgress =
2110 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002111 }
Sunny Goyalff572272014-07-23 13:58:07 -07002112
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002114 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 appWidgetInfo.cellX = c.getInt(cellXIndex);
2116 appWidgetInfo.cellY = c.getInt(cellYIndex);
2117 appWidgetInfo.spanX = c.getInt(spanXIndex);
2118 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002119 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002120
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2122 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2123 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002124 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2125 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 continue;
2127 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002129 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002131 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002132 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 break;
2134 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002135
Adam Cohen59400422014-03-05 18:07:04 -08002136 if (!customWidget) {
2137 String providerName =
2138 appWidgetInfo.providerName.flattenToString();
2139 if (!providerName.equals(savedProvider) ||
2140 (appWidgetInfo.restoreStatus != restoreStatus)) {
2141 ContentValues values = new ContentValues();
2142 values.put(
2143 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2144 providerName);
2145 values.put(LauncherSettings.Favorites.RESTORED,
2146 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002147 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002148 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002149 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2151 sBgAppWidgets.add(appWidgetInfo);
2152 }
Joe Onorato36115782010-06-17 13:28:48 -04002153 break;
2154 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002156 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002157 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 }
2159 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002160 if (c != null) {
2161 c.close();
2162 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 }
2164
Winson Chungba9c37f2013-08-30 14:11:37 -07002165 // Break early if we've stopped loading
2166 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002167 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002168 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002169 }
2170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002173 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2174 Utilities.createDbSelectionQuery(
2175 LauncherSettings.Favorites._ID, itemsToRemove), null);
2176 if (DEBUG_LOADERS) {
2177 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2178 LauncherSettings.Favorites._ID, itemsToRemove));
2179 }
2180
2181 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002182 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2183 .call(contentResolver,
2184 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2185 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2186 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002187 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2188 sBgFolders.remove(folderId);
2189 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002190 }
2191 }
2192
Sunny Goyal317698b2015-07-29 11:45:41 -07002193 // Sort all the folder items and make sure the first 3 items are high resolution.
2194 for (FolderInfo folder : sBgFolders) {
2195 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2196 int pos = 0;
2197 for (ShortcutInfo info : folder.contents) {
2198 if (info.usingLowResIcon) {
2199 info.updateIcon(mIconCache, false);
2200 }
2201 pos ++;
2202 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2203 break;
2204 }
2205 }
2206 }
2207
Chris Wrenf4d08112014-01-16 18:13:56 -05002208 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002209 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002210 ContentValues values = new ContentValues();
2211 values.put(LauncherSettings.Favorites.RESTORED, 0);
2212 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2213 Utilities.createDbSelectionQuery(
2214 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002215 }
2216
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002217 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2218 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002219 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002220 null, sWorker);
2221 }
2222
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002223 // Remove any empty screens
2224 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002225 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002226 long screenId = item.screenId;
2227 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2228 unusedScreens.contains(screenId)) {
2229 unusedScreens.remove(screenId);
2230 }
2231 }
2232
2233 // If there are any empty screens remove them, and update.
2234 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002235 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 }
2238
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 if (DEBUG_LOADERS) {
2240 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2241 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002242 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002243 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002245
Sunny Goyale2df0622015-04-24 11:27:00 -07002246 for (int i = 0; i < nScreens; i++) {
2247 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002248 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 line += " | ";
2250 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002251 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002252 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002253 if (x < screen.length && y < screen[x].length) {
2254 line += (screen[x][y] != null) ? "#" : ".";
2255 } else {
2256 line += "!";
2257 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 }
Joe Onorato36115782010-06-17 13:28:48 -04002259 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002261 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002262 }
Joe Onorato36115782010-06-17 13:28:48 -04002263 }
Adam Cohene25af792013-06-06 23:08:25 -07002264 }
2265
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002266 /**
2267 * Partially updates the item without any notification. Must be called on the worker thread.
2268 */
2269 private void updateItem(long itemId, ContentValues update) {
2270 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002271 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002272 update,
2273 BaseColumns._ID + "= ?",
2274 new String[]{Long.toString(itemId)});
2275 }
2276
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 /** Filters the set of items who are directly or indirectly (via another container) on the
2278 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002279 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 ArrayList<ItemInfo> allWorkspaceItems,
2281 ArrayList<ItemInfo> currentScreenItems,
2282 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002283 // Purge any null ItemInfos
2284 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2285 while (iter.hasNext()) {
2286 ItemInfo i = iter.next();
2287 if (i == null) {
2288 iter.remove();
2289 }
2290 }
2291
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 // Order the set of items by their containers first, this allows use to walk through the
2293 // list sequentially, build up a list of containers that are in the specified screen,
2294 // as well as all items in those containers.
2295 Set<Long> itemsOnScreen = new HashSet<Long>();
2296 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2297 @Override
2298 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002299 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 }
2301 });
2302 for (ItemInfo info : allWorkspaceItems) {
2303 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002304 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305 currentScreenItems.add(info);
2306 itemsOnScreen.add(info.id);
2307 } else {
2308 otherScreenItems.add(info);
2309 }
2310 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2311 currentScreenItems.add(info);
2312 itemsOnScreen.add(info.id);
2313 } else {
2314 if (itemsOnScreen.contains(info.container)) {
2315 currentScreenItems.add(info);
2316 itemsOnScreen.add(info.id);
2317 } else {
2318 otherScreenItems.add(info);
2319 }
2320 }
2321 }
2322 }
2323
2324 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002325 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 ArrayList<LauncherAppWidgetInfo> appWidgets,
2327 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2328 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329
2330 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002331 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002333 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 currentScreenWidgets.add(widget);
2335 } else {
2336 otherScreenWidgets.add(widget);
2337 }
2338 }
2339 }
2340
2341 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002342 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002343 LongArrayMap<ItemInfo> itemsIdMap,
2344 LongArrayMap<FolderInfo> folders,
2345 LongArrayMap<FolderInfo> currentScreenFolders,
2346 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347
Sunny Goyale2df0622015-04-24 11:27:00 -07002348 int total = folders.size();
2349 for (int i = 0; i < total; i++) {
2350 long id = folders.keyAt(i);
2351 FolderInfo folder = folders.valueAt(i);
2352
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002354 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002356 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002357 currentScreenFolders.put(id, folder);
2358 } else {
2359 otherScreenFolders.put(id, folder);
2360 }
2361 }
2362 }
2363
2364 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2365 * right) */
2366 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002367 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002368 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002369 final int screenCols = profile.numColumns;
2370 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002371 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002372 @Override
2373 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002374 if (lhs.container == rhs.container) {
2375 // Within containers, order by their spatial position in that container
2376 switch ((int) lhs.container) {
2377 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2378 long lr = (lhs.screenId * screenCellCount +
2379 lhs.cellY * screenCols + lhs.cellX);
2380 long rr = (rhs.screenId * screenCellCount +
2381 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002382 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002383 }
2384 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2385 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002386 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002387 }
2388 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002389 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002390 throw new RuntimeException("Unexpected container type when " +
2391 "sorting workspace items.");
2392 }
2393 return 0;
2394 }
2395 } else {
2396 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002397 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002398 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002399 }
2400 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002401 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002402
Adam Cohendcd297f2013-06-18 13:13:40 -07002403 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2404 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002405 final Runnable r = new Runnable() {
2406 @Override
2407 public void run() {
2408 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2409 if (callbacks != null) {
2410 callbacks.bindScreens(orderedScreens);
2411 }
2412 }
2413 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002414 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002415 }
2416
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2418 final ArrayList<ItemInfo> workspaceItems,
2419 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002420 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002421 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422
2423 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002424 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002426 final int start = i;
2427 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 final Runnable r = new Runnable() {
2429 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002430 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002431 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002432 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002433 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2434 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002435 }
2436 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002438 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002439 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002440
2441 // Bind the folders
2442 if (!folders.isEmpty()) {
2443 final Runnable r = new Runnable() {
2444 public void run() {
2445 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2446 if (callbacks != null) {
2447 callbacks.bindFolders(folders);
2448 }
2449 }
2450 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002451 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452 }
2453
2454 // Bind the widgets, one at a time
2455 N = appWidgets.size();
2456 for (int i = 0; i < N; i++) {
2457 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2458 final Runnable r = new Runnable() {
2459 public void run() {
2460 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2461 if (callbacks != null) {
2462 callbacks.bindAppWidget(widget);
2463 }
2464 }
2465 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002466 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467 }
2468 }
2469
2470 /**
2471 * Binds all loaded data to actual views on the main thread.
2472 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002473 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474 final long t = SystemClock.uptimeMillis();
2475 Runnable r;
2476
2477 // Don't use these two variables in any of the callback runnables.
2478 // Otherwise we hold a reference to them.
2479 final Callbacks oldCallbacks = mCallbacks.get();
2480 if (oldCallbacks == null) {
2481 // This launcher has exited and nobody bothered to tell us. Just bail.
2482 Log.w(TAG, "LoaderTask running with no launcher");
2483 return;
2484 }
2485
Winson Chung9b9fb962013-11-15 15:39:34 -08002486 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2488 ArrayList<LauncherAppWidgetInfo> appWidgets =
2489 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002490 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002491
2492 final LongArrayMap<FolderInfo> folders;
2493 final LongArrayMap<ItemInfo> itemsIdMap;
2494
Winson Chung2abf94d2012-07-18 18:16:38 -07002495 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496 workspaceItems.addAll(sBgWorkspaceItems);
2497 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002498 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002499
2500 folders = sBgFolders.clone();
2501 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 }
2503
Derek Prothro7aff3992013-12-10 14:00:37 -05002504 final boolean isLoadingSynchronously =
2505 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002506 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002507 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002508 if (currScreen >= orderedScreenIds.size()) {
2509 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002510 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002511 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002512 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002513 final long currentScreenId = currentScreen < 0
2514 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002515
2516 // Load all the items that are on the current page first (and in the process, unbind
2517 // all the existing workspace items before we call startBinding() below.
2518 unbindWorkspaceItemsOnMainThread();
2519
2520 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2522 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2523 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2524 new ArrayList<LauncherAppWidgetInfo>();
2525 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2526 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002527 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2528 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529
Winson Chung9b9fb962013-11-15 15:39:34 -08002530 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002532 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002534 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 otherFolders);
2536 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2537 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2538
2539 // Tell the workspace that we're about to start binding items
2540 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002541 public void run() {
2542 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2543 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002544 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002546 }
2547 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002549 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550
Adam Cohendcd297f2013-06-18 13:13:40 -07002551 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2552
Sunny Goyal527c7d32015-08-28 15:19:36 -07002553 Executor mainExecutor = new DeferredMainThreadExecutor();
2554 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002555 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002556 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557
Sunny Goyal527c7d32015-08-28 15:19:36 -07002558 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2559 // remaining screens after first onDraw is called. This ensures that the first screen
2560 // is immediately visible (eg. during rotation)
2561 // In case of !isLoadingSynchronously, bind all pages one after other.
2562 final Executor deferredExecutor = isLoadingSynchronously ?
2563 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2564
2565 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2566 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567
2568 // Tell the workspace that we're done binding items
2569 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002570 public void run() {
2571 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2572 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002573 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002574 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575
Sunny Goyal639e9062015-08-19 19:17:06 -07002576 mIsLoadingAndBindingWorkspace = false;
2577
2578 // Run all the bind complete runnables after workspace is bound.
2579 if (!mBindCompleteRunnables.isEmpty()) {
2580 synchronized (mBindCompleteRunnables) {
2581 for (final Runnable r : mBindCompleteRunnables) {
2582 runOnWorkerThread(r);
2583 }
2584 mBindCompleteRunnables.clear();
2585 }
2586 }
2587
Winson Chung98e030b2012-05-07 16:01:11 -07002588 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002589 if (DEBUG_LOADERS) {
2590 Log.d(TAG, "bound workspace in "
2591 + (SystemClock.uptimeMillis()-t) + "ms");
2592 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002593
Joe Onorato36115782010-06-17 13:28:48 -04002594 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002595 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002596 deferredExecutor.execute(r);
2597
Winson Chung4a2afa32012-07-19 14:53:05 -07002598 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002599 r = new Runnable() {
2600 public void run() {
2601 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2602 if (callbacks != null) {
2603 // We are loading synchronously, which means, some of the pages will be
2604 // bound after first draw. Inform the callbacks that page binding is
2605 // not complete, and schedule the remaining pages.
2606 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2607 callbacks.onPageBoundSynchronously(currentScreen);
2608 }
2609 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2610 }
2611 }
2612 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002613 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002614 }
Joe Onorato36115782010-06-17 13:28:48 -04002615 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002616
Joe Onorato36115782010-06-17 13:28:48 -04002617 private void loadAndBindAllApps() {
2618 if (DEBUG_LOADERS) {
2619 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2620 }
2621 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002622 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002623 synchronized (LoaderTask.this) {
2624 if (mStopped) {
2625 return;
2626 }
2627 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002628 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002629 synchronized (LoaderTask.this) {
2630 if (mStopped) {
2631 return;
2632 }
2633 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002634 }
Joe Onorato36115782010-06-17 13:28:48 -04002635 } else {
2636 onlyBindAllApps();
2637 }
2638 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002639
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002640 private void updateIconCache() {
2641 // Ignore packages which have a promise icon.
2642 HashSet<String> packagesToIgnore = new HashSet<>();
2643 synchronized (sBgLock) {
2644 for (ItemInfo info : sBgItemsIdMap) {
2645 if (info instanceof ShortcutInfo) {
2646 ShortcutInfo si = (ShortcutInfo) info;
2647 if (si.isPromise() && si.getTargetComponent() != null) {
2648 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2649 }
2650 } else if (info instanceof LauncherAppWidgetInfo) {
2651 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2652 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2653 packagesToIgnore.add(lawi.providerName.getPackageName());
2654 }
2655 }
2656 }
2657 }
2658 mIconCache.updateDbIcons(packagesToIgnore);
2659 }
2660
Joe Onorato36115782010-06-17 13:28:48 -04002661 private void onlyBindAllApps() {
2662 final Callbacks oldCallbacks = mCallbacks.get();
2663 if (oldCallbacks == null) {
2664 // This launcher has exited and nobody bothered to tell us. Just bail.
2665 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2666 return;
2667 }
2668
2669 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002670 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002671 final ArrayList<AppInfo> list
2672 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Hyunyoung Song9110d482015-05-22 14:49:23 -07002673 final WidgetsModel widgetList = mBgWidgetsModel.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002674 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002675 public void run() {
2676 final long t = SystemClock.uptimeMillis();
2677 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2678 if (callbacks != null) {
2679 callbacks.bindAllApplications(list);
Hyunyoung Song9110d482015-05-22 14:49:23 -07002680 callbacks.bindAllPackages(widgetList);
Joe Onorato36115782010-06-17 13:28:48 -04002681 }
2682 if (DEBUG_LOADERS) {
2683 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2684 + (SystemClock.uptimeMillis()-t) + "ms");
2685 }
2686 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002687 };
2688 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002689 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002690 r.run();
2691 } else {
2692 mHandler.post(r);
2693 }
Joe Onorato36115782010-06-17 13:28:48 -04002694 }
2695
Winson Chung64359a52013-07-08 17:17:08 -07002696 private void loadAllApps() {
2697 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002698
Joe Onorato36115782010-06-17 13:28:48 -04002699 final Callbacks oldCallbacks = mCallbacks.get();
2700 if (oldCallbacks == null) {
2701 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002702 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002703 return;
2704 }
2705
Kenny Guyed131872014-04-30 03:02:21 +01002706 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2707
Winson Chung64359a52013-07-08 17:17:08 -07002708 // Clear the list of apps
2709 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002710 for (UserHandleCompat user : profiles) {
2711 // Query for the set of apps
2712 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002713 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002714 if (DEBUG_LOADERS) {
2715 Log.d(TAG, "getActivityList took "
2716 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2717 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2718 }
2719 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002720 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002721 if (apps == null || apps.isEmpty()) {
2722 return;
2723 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002724
Kenny Guyed131872014-04-30 03:02:21 +01002725 // Create the ApplicationInfos
2726 for (int i = 0; i < apps.size(); i++) {
2727 LauncherActivityInfoCompat app = apps.get(i);
2728 // This builds the icon bitmaps.
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002729 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));
Kenny Guyed131872014-04-30 03:02:21 +01002730 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002731
Sunny Goyal756a28a2015-04-23 17:07:55 -07002732 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2733 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002734 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002735
2736 @Override
2737 public void run() {
2738 heuristic.processUserApps(apps);
2739 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002740 };
2741 runOnMainThread(new Runnable() {
2742
2743 @Override
2744 public void run() {
2745 // Check isLoadingWorkspace on the UI thread, as it is updated on
2746 // the UI thread.
2747 if (mIsLoadingAndBindingWorkspace) {
2748 synchronized (mBindCompleteRunnables) {
2749 mBindCompleteRunnables.add(r);
2750 }
2751 } else {
2752 runOnWorkerThread(r);
2753 }
2754 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002755 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002756 }
Winson Chung64359a52013-07-08 17:17:08 -07002757 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002758 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002759 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2760 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002761
2762 // Post callback on main thread
2763 mHandler.post(new Runnable() {
2764 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002765
Winson Chung64359a52013-07-08 17:17:08 -07002766 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002767 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002768 if (callbacks != null) {
2769 callbacks.bindAllApplications(added);
2770 if (DEBUG_LOADERS) {
2771 Log.d(TAG, "bound " + added.size() + " apps in "
2772 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2773 }
2774 } else {
2775 Log.i(TAG, "not binding apps: no Launcher activity");
2776 }
2777 }
2778 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002779 // Cleanup any data stored for a deleted user.
2780 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002781
Sunny Goyal2d648b02015-08-11 13:56:28 -07002782 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002783 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002784 Log.d(TAG, "Icons processed in "
2785 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002786 }
2787 }
2788
2789 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002790 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002791 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002792 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2793 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2794 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2795 }
Joe Onorato36115782010-06-17 13:28:48 -04002796 }
2797 }
2798
Sunny Goyal75b0f552015-05-20 21:57:06 -07002799 /**
2800 * Called when the icons for packages have been updated in the icon cache.
2801 */
2802 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2803 final Callbacks callbacks = getCallback();
2804 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2805 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2806
2807 // If any package icon has changed (app was updated while launcher was dead),
2808 // update the corresponding shortcuts.
2809 synchronized (sBgLock) {
2810 for (ItemInfo info : sBgItemsIdMap) {
2811 if (info instanceof ShortcutInfo && user.equals(info.user)
2812 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2813 ShortcutInfo si = (ShortcutInfo) info;
2814 ComponentName cn = si.getTargetComponent();
2815 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2816 si.updateIcon(mIconCache);
2817 updatedShortcuts.add(si);
2818 }
2819 }
2820 }
2821 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2822 }
2823
2824 if (!updatedShortcuts.isEmpty()) {
2825 final UserHandleCompat userFinal = user;
2826 mHandler.post(new Runnable() {
2827
2828 public void run() {
2829 Callbacks cb = getCallback();
2830 if (cb != null && callbacks == cb) {
2831 cb.bindShortcutsChanged(updatedShortcuts,
2832 new ArrayList<ShortcutInfo>(), userFinal);
2833 }
2834 }
2835 });
2836 }
2837
2838 if (!updatedApps.isEmpty()) {
2839 mHandler.post(new Runnable() {
2840
2841 public void run() {
2842 Callbacks cb = getCallback();
2843 if (cb != null && callbacks == cb) {
2844 cb.bindAppsUpdated(updatedApps);
2845 }
2846 }
2847 });
2848 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002849
2850 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002851 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002852 }
2853
Joe Onorato36115782010-06-17 13:28:48 -04002854 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002855 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002856 }
2857
Adam Cohen091440a2015-03-18 14:16:05 -07002858 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002859
2860 @Override
2861 public void onReceive(Context context, Intent intent) {
2862 synchronized (sBgLock) {
2863 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2864 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002865 final PackageManager manager = context.getPackageManager();
2866 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2867 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002868 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2869 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002870 packagesRemoved.clear();
2871 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002872 for (String pkg : entry.getValue()) {
2873 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002874 boolean packageOnSdcard = launcherApps.isAppEnabled(
2875 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2876 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002877 packagesUnavailable.add(pkg);
2878 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002879 packagesRemoved.add(pkg);
2880 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002881 }
2882 }
2883 if (!packagesRemoved.isEmpty()) {
2884 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2885 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2886 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002887 if (!packagesUnavailable.isEmpty()) {
2888 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2889 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2890 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002891 }
Sunny Goyal34942622014-08-29 17:20:55 -07002892 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002893 }
2894 }
2895 }
2896
Joe Onorato36115782010-06-17 13:28:48 -04002897 private class PackageUpdatedTask implements Runnable {
2898 int mOp;
2899 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002900 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002901
2902 public static final int OP_NONE = 0;
2903 public static final int OP_ADD = 1;
2904 public static final int OP_UPDATE = 2;
2905 public static final int OP_REMOVE = 3; // uninstlled
2906 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2907
2908
Kenny Guyed131872014-04-30 03:02:21 +01002909 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002910 mOp = op;
2911 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002912 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002913 }
2914
2915 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002916 if (!mHasLoaderCompletedOnce) {
2917 // Loader has not yet run.
2918 return;
2919 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002920 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002921
2922 final String[] packages = mPackages;
2923 final int N = packages.length;
2924 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002925 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002926 for (int i=0; i<N; i++) {
2927 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002928 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002929 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002930 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002931
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002932 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2933 if (heuristic != null) {
2934 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002935 }
Joe Onorato36115782010-06-17 13:28:48 -04002936 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002937 }
Joe Onorato36115782010-06-17 13:28:48 -04002938 case OP_UPDATE:
2939 for (int i=0; i<N; i++) {
2940 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002941 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002942 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002943 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002944 }
2945 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002946 case OP_REMOVE: {
2947 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2948 if (heuristic != null) {
2949 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002950 }
Joe Onorato36115782010-06-17 13:28:48 -04002951 for (int i=0; i<N; i++) {
2952 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002953 mIconCache.removeIconsForPkg(packages[i], mUser);
2954 }
2955 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002956 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002957 case OP_UNAVAILABLE:
2958 for (int i=0; i<N; i++) {
2959 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2960 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002961 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002962 }
2963 break;
2964 }
2965
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002966 ArrayList<AppInfo> added = null;
2967 ArrayList<AppInfo> modified = null;
2968 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002969
Adam Cohen487f7dd2012-06-28 18:12:10 -07002970 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002971 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002972 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002973 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002974 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002975 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002976 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002977 }
Winson Chung5d55f332012-07-16 20:45:03 -07002978 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002979 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002980 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002981 }
2982
Sunny Goyale0f58d72014-11-10 18:05:31 -08002983 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04002984 if (callbacks == null) {
2985 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2986 return;
2987 }
2988
Sunny Goyal4390ace2014-10-13 11:33:11 -07002989 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
2990 new HashMap<ComponentName, AppInfo>();
2991
Joe Onorato36115782010-06-17 13:28:48 -04002992 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002993 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002994 for (AppInfo ai : added) {
2995 addedOrUpdatedApps.put(ai.componentName, ai);
2996 }
Joe Onorato36115782010-06-17 13:28:48 -04002997 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002998
Joe Onorato36115782010-06-17 13:28:48 -04002999 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003000 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003001 for (AppInfo ai : modified) {
3002 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003003 }
3004
Joe Onorato36115782010-06-17 13:28:48 -04003005 mHandler.post(new Runnable() {
3006 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003007 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003008 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003009 callbacks.bindAppsUpdated(modifiedFinal);
3010 }
3011 }
3012 });
3013 }
Winson Chung83892cc2013-05-01 16:53:33 -07003014
Sunny Goyal4390ace2014-10-13 11:33:11 -07003015 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003016 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003017 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3018 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3019 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3020
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003021 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003022 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003023 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003024 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3025 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003026 boolean infoUpdated = false;
3027 boolean shortcutUpdated = false;
3028
3029 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003030 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003031 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003032 Bitmap icon = Utilities.createIconBitmap(
3033 si.iconResource.packageName,
3034 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003035 if (icon != null) {
3036 si.setIcon(icon);
3037 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003038 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003039 }
3040 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003041
3042 ComponentName cn = si.getTargetComponent();
3043 if (cn != null && packageSet.contains(cn.getPackageName())) {
3044 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3045
3046 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003047 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3048 // Auto install icon
3049 PackageManager pm = context.getPackageManager();
3050 ResolveInfo matched = pm.resolveActivity(
3051 new Intent(Intent.ACTION_MAIN)
3052 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3053 PackageManager.MATCH_DEFAULT_ONLY);
3054 if (matched == null) {
3055 // Try to find the best match activity.
3056 Intent intent = pm.getLaunchIntentForPackage(
3057 cn.getPackageName());
3058 if (intent != null) {
3059 cn = intent.getComponent();
3060 appInfo = addedOrUpdatedApps.get(cn);
3061 }
3062
3063 if ((intent == null) || (appInfo == null)) {
3064 removedShortcuts.add(si);
3065 continue;
3066 }
3067 si.promisedIntent = intent;
3068 }
3069 }
3070
3071 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003072 if (appInfo != null) {
3073 si.flags = appInfo.flags;
3074 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075
Sunny Goyal756adbc2015-04-16 15:20:51 -07003076 si.intent = si.promisedIntent;
3077 si.promisedIntent = null;
3078 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 infoUpdated = true;
3080 si.updateIcon(mIconCache);
3081 }
3082
3083 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3084 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3085 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003086 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003087 si.contentDescription = appInfo.contentDescription;
3088 infoUpdated = true;
3089 }
3090
3091 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3092 // Since package was just updated, the target must be available now.
3093 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3094 shortcutUpdated = true;
3095 }
3096 }
3097
3098 if (infoUpdated || shortcutUpdated) {
3099 updatedShortcuts.add(si);
3100 }
3101 if (infoUpdated) {
3102 updateItemInDatabase(context, si);
3103 }
3104 } else if (info instanceof LauncherAppWidgetInfo) {
3105 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3106 if (mUser.equals(widgetInfo.user)
3107 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3108 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003109 widgetInfo.restoreStatus &=
3110 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3111 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003112
3113 // adding this flag ensures that launcher shows 'click to setup'
3114 // if the widget has a config activity. In case there is no config
3115 // activity, it will be marked as 'restored' during bind.
3116 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3117
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118 widgets.add(widgetInfo);
3119 updateItemInDatabase(context, widgetInfo);
3120 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003121 }
3122 }
3123 }
3124
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3126 mHandler.post(new Runnable() {
3127
3128 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003129 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 if (callbacks == cb && cb != null) {
3131 callbacks.bindShortcutsChanged(
3132 updatedShortcuts, removedShortcuts, mUser);
3133 }
3134 }
3135 });
3136 if (!removedShortcuts.isEmpty()) {
3137 deleteItemsFromDatabase(context, removedShortcuts);
3138 }
3139 }
3140 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003141 mHandler.post(new Runnable() {
3142 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003143 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003144 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003146 }
3147 }
3148 });
3149 }
3150 }
3151
Winson Chungdf95eb12013-10-16 14:57:07 -07003152 final ArrayList<String> removedPackageNames =
3153 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003154 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003155 // Mark all packages in the broadcast to be removed
3156 removedPackageNames.addAll(Arrays.asList(packages));
3157 } else if (mOp == OP_UPDATE) {
3158 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003159 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003160 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003161 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003162 }
3163 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003164 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003165
Winson Chungdf95eb12013-10-16 14:57:07 -07003166 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003167 final int removeReason;
3168 if (mOp == OP_UNAVAILABLE) {
3169 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3170 } else {
3171 // Remove all the components associated with this package
3172 for (String pn : removedPackageNames) {
3173 deletePackageFromDatabase(context, pn, mUser);
3174 }
3175 // Remove all the specific components
3176 for (AppInfo a : removedApps) {
3177 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3178 deleteItemsFromDatabase(context, infos);
3179 }
3180 removeReason = 0;
3181 }
3182
Winson Chungdf95eb12013-10-16 14:57:07 -07003183 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003184 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003185 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003186 mHandler.post(new Runnable() {
3187 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003188 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003189 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003190 callbacks.bindComponentsRemoved(
3191 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003192 }
3193 }
3194 });
Joe Onoratobe386092009-11-17 17:32:16 -08003195 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003196
Sunny Goyal2d648b02015-08-11 13:56:28 -07003197 // Update widgets
3198 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3199 // Always refresh for a package event on secondary user
3200 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3201
3202 // Refresh widget list, if the package already had a widget.
3203 synchronized (sBgLock) {
3204 if (sBgWidgetProviders != null) {
3205 HashSet<String> pkgSet = new HashSet<>();
3206 Collections.addAll(pkgSet, mPackages);
3207
3208 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3209 needToRefresh |= key.user.equals(mUser) &&
3210 pkgSet.contains(key.componentName.getPackageName());
3211 }
3212 }
3213 }
3214
3215 if (!needToRefresh && mOp != OP_REMOVE) {
3216 // Refresh widget list, if there is any newly added widget
3217 PackageManager pm = context.getPackageManager();
3218 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003219 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003220 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003221 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003222 .setPackage(pkg), 0);
3223 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003224 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003225 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003226 throw e;
3227 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003228 // Ignore the crash. We can live with a state widget list.
3229 Log.e(TAG, "PM call failed for " + pkg, e);
3230 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003231 }
3232 }
3233
3234 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3235 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003236 }
3237 }
3238
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003239 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3240 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003241 ArrayList<LauncherAppWidgetProviderInfo> results =
3242 new ArrayList<LauncherAppWidgetProviderInfo>();
3243 try {
3244 synchronized (sBgLock) {
3245 if (sBgWidgetProviders == null || refresh) {
3246 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3247 = new HashMap<>();
3248 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3249 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003250
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003251 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3252 for (AppWidgetProviderInfo pInfo : widgets) {
3253 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3254 UserHandleCompat user = wm.getUser(info);
3255 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3256 }
Robin Lee26ace122015-03-16 19:41:43 +00003257
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003258 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3259 for (CustomAppWidget widget : customWidgets) {
3260 info = new LauncherAppWidgetProviderInfo(context, widget);
3261 UserHandleCompat user = wm.getUser(info);
3262 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3263 }
3264 // Replace the global list at the very end, so that if there is an exception,
3265 // previously loaded provider list is used.
3266 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003267 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003268 results.addAll(sBgWidgetProviders.values());
3269 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003270 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003271 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003272 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003273 (e.getCause() instanceof TransactionTooLargeException ||
3274 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003275 // the returned value may be incomplete and will not be refreshed until the next
3276 // time Launcher starts.
3277 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3278 // onResume is called to refresh the widget provider list.
3279 synchronized (sBgLock) {
3280 if (sBgWidgetProviders != null) {
3281 results.addAll(sBgWidgetProviders.values());
3282 }
3283 return results;
3284 }
3285 } else {
3286 throw e;
3287 }
Adam Cohen59400422014-03-05 18:07:04 -08003288 }
3289 }
3290
Robin Lee26ace122015-03-16 19:41:43 +00003291 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3292 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003293 synchronized (sBgLock) {
3294 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003295 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003296 }
Robin Lee26ace122015-03-16 19:41:43 +00003297 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003298 }
3299 }
3300
Sunny Goyal2d648b02015-08-11 13:56:28 -07003301 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003302
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003303 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003304 @Override
3305 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003306 updateWidgetsModel(refresh);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003307 final WidgetsModel model = mBgWidgetsModel.clone();
3308
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003309 mHandler.post(new Runnable() {
3310 @Override
3311 public void run() {
3312 Callbacks cb = getCallback();
3313 if (callbacks == cb && cb != null) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003314 callbacks.bindAllPackages(model);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003315 }
3316 }
3317 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003318 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003319 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3320 model.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003321 }
3322 });
3323 }
3324
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003325 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003326 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003327 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003328 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003329 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003330 @Thunk void updateWidgetsModel(boolean refresh) {
3331 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003332 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003333 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003334
3335 // Update shortcut providers
3336 synchronized (sBgLock) {
3337 try {
3338 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3339 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3340 sBgShortcutProviders = providers;
3341 } catch (RuntimeException e) {
Sunny Goyal877f9282015-09-18 10:18:18 -07003342 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal31860582015-09-18 08:38:57 -07003343 (e.getCause() instanceof TransactionTooLargeException ||
3344 e.getCause() instanceof DeadObjectException)) {
3345 /**
3346 * Ignore exception and use the cached list if available.
3347 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3348 */
3349 } else {
3350 throw e;
3351 }
3352 }
3353 if (sBgShortcutProviders != null) {
3354 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3355 }
3356 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003357 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Michael Jurkac402cd92013-05-20 15:49:32 +02003358 }
3359
Adam Cohen091440a2015-03-18 14:16:05 -07003360 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003361 UserHandleCompat user) {
3362 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3363 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003364 }
Adam Cohen556f6132014-01-15 15:18:08 -08003365
Kenny Guyed131872014-04-30 03:02:21 +01003366 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3367 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003368 if (cn == null) {
3369 return false;
3370 }
Kenny Guyed131872014-04-30 03:02:21 +01003371 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3372 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003373 return false;
3374 }
Kenny Guyed131872014-04-30 03:02:21 +01003375 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003376 }
3377
Adam Cohena28b78e2014-05-20 17:03:04 -07003378 public static boolean isValidPackage(Context context, String packageName,
3379 UserHandleCompat user) {
3380 if (packageName == null) {
3381 return false;
3382 }
3383 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3384 return launcherApps.isPackageEnabledForProfile(packageName, user);
3385 }
3386
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003388 * Make an ShortcutInfo object for a restored application or shortcut item that points
3389 * to a package that is not yet installed on the system.
3390 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003391 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003392 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003393 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003394 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003395
3396 Bitmap icon = iconInfo.loadIcon(c, info, context);
3397 // the fallback icon
3398 if (icon == null) {
3399 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3400 } else {
3401 info.setIcon(icon);
3402 }
Sunny Goyal34942622014-08-29 17:20:55 -07003403
3404 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003405 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003406 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003407 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003408 }
Sunny Goyal34942622014-08-29 17:20:55 -07003409 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3410 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003411 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003412 }
Sunny Goyal34942622014-08-29 17:20:55 -07003413 } else {
3414 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3415 }
3416
Winson Chung82b016c2015-05-08 17:00:10 -07003417 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003418 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003419 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003420 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003421 return info;
3422 }
3423
3424 /**
3425 * Make an Intent object for a restored application or shortcut item that points
3426 * to the market page for the item.
3427 */
Adam Cohen091440a2015-03-18 14:16:05 -07003428 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003429 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003430 return getMarketIntent(componentName.getPackageName());
3431 }
3432
3433 static Intent getMarketIntent(String packageName) {
3434 return new Intent(Intent.ACTION_VIEW)
3435 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003436 .scheme("market")
3437 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003438 .appendQueryParameter("id", packageName)
3439 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003440 }
3441
3442 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003443 * Make an ShortcutInfo object for a shortcut that is an application.
3444 *
3445 * If c is not null, then it will be used to fill in missing data like the title and icon.
3446 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003447 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003448 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003449 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003450 if (user == null) {
3451 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003452 return null;
3453 }
3454
Kenny Guyed131872014-04-30 03:02:21 +01003455 ComponentName componentName = intent.getComponent();
3456 if (componentName == null) {
3457 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3458 return null;
3459 }
3460
3461 Intent newIntent = new Intent(intent.getAction(), null);
3462 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3463 newIntent.setComponent(componentName);
3464 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003465 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003466 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3467 return null;
3468 }
3469
3470 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003471 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003472 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3473 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3474 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003475 }
3476
Joe Onorato56d82912010-03-07 14:32:10 -05003477 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003478 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003479 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003480 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003481
Joe Onorato56d82912010-03-07 14:32:10 -05003482 // fall back to the class name of the activity
3483 if (info.title == null) {
3484 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003485 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003486
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003488 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003489 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003490 if (lai != null) {
3491 info.flags = AppInfo.initFlags(lai);
3492 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003494 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003495
Sunny Goyale2df0622015-04-24 11:27:00 -07003496 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003497 ItemInfoFilter f) {
3498 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3499 for (ItemInfo i : infos) {
3500 if (i instanceof ShortcutInfo) {
3501 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003502 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003503 if (cn != null && f.filterItem(null, info, cn)) {
3504 filtered.add(info);
3505 }
3506 } else if (i instanceof FolderInfo) {
3507 FolderInfo info = (FolderInfo) i;
3508 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003509 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003510 if (cn != null && f.filterItem(info, s, cn)) {
3511 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003512 }
3513 }
Winson Chung64359a52013-07-08 17:17:08 -07003514 } else if (i instanceof LauncherAppWidgetInfo) {
3515 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3516 ComponentName cn = info.providerName;
3517 if (cn != null && f.filterItem(null, info, cn)) {
3518 filtered.add(info);
3519 }
Winson Chung8a435102012-08-30 17:16:53 -07003520 }
3521 }
Winson Chung64359a52013-07-08 17:17:08 -07003522 return new ArrayList<ItemInfo>(filtered);
3523 }
3524
Adam Cohen091440a2015-03-18 14:16:05 -07003525 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003526 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003527 ItemInfoFilter filter = new ItemInfoFilter() {
3528 @Override
3529 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003530 if (info.user == null) {
3531 return cn.equals(cname);
3532 } else {
3533 return cn.equals(cname) && info.user.equals(user);
3534 }
Winson Chung64359a52013-07-08 17:17:08 -07003535 }
3536 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003537 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003538 }
3539
Sunny Goyal1a745e82014-10-02 15:58:31 -07003540 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003541 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003542 */
Adam Cohen091440a2015-03-18 14:16:05 -07003543 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003544 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003545 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003546 // Non-app shortcuts are only supported for current user.
3547 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003549
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003550 // TODO: If there's an explicit component and we can't install that, delete it.
3551
Winson Chung82b016c2015-05-08 17:00:10 -07003552 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003553
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003554 Bitmap icon = iconInfo.loadIcon(c, info, context);
3555 // the fallback icon
3556 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003557 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003558 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003559 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003560 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 return info;
3562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003563
Sunny Goyal2350bc92014-10-14 16:42:54 -07003564 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003565 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3566 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3567 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3568
Adam Cohend9198822011-11-22 16:42:47 -08003569 if (intent == null) {
3570 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3571 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3572 return null;
3573 }
3574
Joe Onorato0589f0f2010-02-08 13:44:00 -08003575 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003576 boolean customIcon = false;
3577 ShortcutIconResource iconResource = null;
3578
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003579 if (bitmap instanceof Bitmap) {
3580 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003581 customIcon = true;
3582 } else {
3583 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003584 if (extra instanceof ShortcutIconResource) {
3585 iconResource = (ShortcutIconResource) extra;
3586 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003587 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003588 }
3589 }
3590
Michael Jurkac9d95c52011-08-29 14:03:34 -07003591 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003592
Kenny Guyed131872014-04-30 03:02:21 +01003593 // Only support intents for current user for now. Intents sent from other
3594 // users wouldn't get here without intent forwarding anyway.
3595 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003596 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003597 icon = mIconCache.getDefaultIcon(info.user);
3598 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003599 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003600 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003601
Winson Chung82b016c2015-05-08 17:00:10 -07003602 info.title = Utilities.trim(name);
3603 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003604 info.intent = intent;
3605 info.customIcon = customIcon;
3606 info.iconResource = iconResource;
3607
3608 return info;
3609 }
3610
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003612 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 * or make a new one.
3614 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003615 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 // See if a placeholder was created for us already
3617 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003618 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003620 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 folders.put(id, folderInfo);
3622 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003623 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003624 }
3625
Joe Onoratobe386092009-11-17 17:32:16 -08003626
Sunny Goyal651077b2014-06-30 14:15:31 -07003627 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3628 return (provider != null) && (provider.provider != null)
3629 && (provider.provider.getPackageName() != null);
3630 }
3631
Joe Onoratobe386092009-11-17 17:32:16 -08003632 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003633 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003634 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3635 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3636 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3637 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003638 if (mLoaderTask != null) {
3639 mLoaderTask.dumpState();
3640 } else {
3641 Log.d(TAG, "mLoaderTask=null");
3642 }
Joe Onoratobe386092009-11-17 17:32:16 -08003643 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003644
3645 public Callbacks getCallback() {
3646 return mCallbacks != null ? mCallbacks.get() : null;
3647 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003648
3649 /**
3650 * @return {@link FolderInfo} if its already loaded.
3651 */
3652 public FolderInfo findFolderById(Long folderId) {
3653 synchronized (sBgLock) {
3654 return sBgFolders.get(folderId);
3655 }
3656 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003657
Sunny Goyal527c7d32015-08-28 15:19:36 -07003658 @Thunk class DeferredMainThreadExecutor implements Executor {
3659
3660 @Override
3661 public void execute(Runnable command) {
3662 runOnMainThread(command);
3663 }
3664 }
3665
Sunny Goyal756adbc2015-04-16 15:20:51 -07003666 /**
3667 * @return the looper for the worker thread which can be used to start background tasks.
3668 */
3669 public static Looper getWorkerLooper() {
3670 return sWorkerThread.getLooper();
3671 }
Sunny Goyala1365452015-10-01 15:46:24 -07003672
3673 @Thunk static final void addDumpLog(String log) {
3674 Launcher.addDumpLog(TAG, log);
3675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003676}