blob: d8bc6317684248bde74b48ab52d2be31a743dd08 [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 Goyalf862a262015-12-14 14:27:38 -080060import com.android.launcher3.model.GridSizeMigrationTask;
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;
Hyunyoung Song747a5bc2016-02-08 11:31:33 -0800141 // Keep a clone of widgets that can be accessed from non-worker thread.
142 WidgetsModel mFgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700144 // The lock that must be acquired before referencing any static bg data structures. Unlike
145 // other locks, this one can generally be held long-term because we never expect any of these
146 // static data structures to be referenced outside of the worker thread except on the first
147 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700148 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700152 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
155 // created by LauncherModel that are directly on the home screen (however, no widgets or
156 // shortcuts within folders).
157 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
160 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700161 new ArrayList<LauncherAppWidgetInfo>();
162
Adam Cohen487f7dd2012-06-28 18:12:10 -0700163 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700164 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700165
Adam Cohendcd297f2013-06-18 13:13:40 -0700166 // sBgWorkspaceScreens is the ordered set of workspace screens.
167 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
168
Adam Cohen59400422014-03-05 18:07:04 -0800169 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000170 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800171
Sunny Goyal31860582015-09-18 08:38:57 -0700172 // sBgShortcutProviders is the set of custom shortcut providers
173 public static List<ResolveInfo> sBgShortcutProviders;
174
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700175 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700176 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
177 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700178
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700179 // </ only access in worker thread >
180
Adam Cohen091440a2015-03-18 14:16:05 -0700181 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Adam Cohen091440a2015-03-18 14:16:05 -0700183 @Thunk final LauncherAppsCompat mLauncherApps;
184 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700187 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700189 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700191 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
192 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700193 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700194 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700195 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800196 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700199 public void bindAppsAdded(ArrayList<Long> newScreens,
200 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700201 ArrayList<ItemInfo> addAnimated,
202 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200203 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700204 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
205 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
206 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700207 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700208 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700209 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700210 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700211 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700212 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700213 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700214 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chung64359a52013-07-08 17:17:08 -0700217 public interface ItemInfoFilter {
218 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
219 }
220
Bjorn Bringert1307f632013-10-03 22:31:03 +0100221 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800222 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400223
Winson Chungee055712013-07-30 14:46:24 -0700224 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700225 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400226 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
227 // resource string.
228 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
229 ProviderInfo providerInfo =
230 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
231 ProviderInfo redirectProvider =
232 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700233
234 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400235 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700236
237 if (mOldContentProviderExists) {
238 Log.d(TAG, "Old launcher provider exists.");
239 } else {
240 Log.d(TAG, "Old launcher provider does not exist.");
241 }
242
Daniel Sandlere4f98912013-06-25 15:13:26 -0400243 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100244 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700245 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Hyunyoung Song747a5bc2016-02-08 11:31:33 -0800246 mFgWidgetsModel = mBgWidgetsModel.clone();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 mIconCache = iconCache;
248
Kenny Guyed131872014-04-30 03:02:21 +0100249 mLauncherApps = LauncherAppsCompat.getInstance(context);
250 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 }
252
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
254 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700256 if (sWorkerThread.getThreadId() == Process.myTid()) {
257 // If we are on the worker thread, post onto the main handler
258 mHandler.post(r);
259 } else {
260 r.run();
261 }
262 }
263
264 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
265 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700266 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700267 if (sWorkerThread.getThreadId() == Process.myTid()) {
268 r.run();
269 } else {
270 // If we are not on the worker thread, then post to the worker handler
271 sWorker.post(r);
272 }
273 }
274
Winson Chunge43a1e72014-01-15 10:33:02 -0800275 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
276 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800277 }
278
Sunny Goyal756adbc2015-04-16 15:20:51 -0700279 public void setPackageState(final PackageInstallInfo installInfo) {
280 Runnable updateRunnable = new Runnable() {
281
282 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500283 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700284 synchronized (sBgLock) {
285 final HashSet<ItemInfo> updates = new HashSet<>();
286
287 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
288 // Ignore install success events as they are handled by Package add events.
289 return;
290 }
291
Sunny Goyale2df0622015-04-24 11:27:00 -0700292 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 if (info instanceof ShortcutInfo) {
294 ShortcutInfo si = (ShortcutInfo) info;
295 ComponentName cn = si.getTargetComponent();
296 if (si.isPromise() && (cn != null)
297 && installInfo.packageName.equals(cn.getPackageName())) {
298 si.setInstallProgress(installInfo.progress);
299
300 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
301 // Mark this info as broken.
302 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
303 }
304 updates.add(si);
305 }
306 }
307 }
308
309 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
310 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
311 widget.installProgress = installInfo.progress;
312 updates.add(widget);
313 }
314 }
315
316 if (!updates.isEmpty()) {
317 // Push changes to the callback.
318 Runnable r = new Runnable() {
319 public void run() {
320 Callbacks callbacks = getCallback();
321 if (callbacks != null) {
322 callbacks.bindRestoreItemsChange(updates);
323 }
324 }
325 };
326 mHandler.post(r);
327 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500328 }
329 }
330 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500332 }
333
Sunny Goyal756adbc2015-04-16 15:20:51 -0700334 /**
335 * Updates the icons and label of all pending icons for the provided package name.
336 */
337 public void updateSessionDisplayInfo(final String packageName) {
338 Runnable updateRunnable = new Runnable() {
339
340 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700341 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 synchronized (sBgLock) {
343 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
344 final UserHandleCompat user = UserHandleCompat.myUserHandle();
345
Sunny Goyale2df0622015-04-24 11:27:00 -0700346 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700347 if (info instanceof ShortcutInfo) {
348 ShortcutInfo si = (ShortcutInfo) info;
349 ComponentName cn = si.getTargetComponent();
350 if (si.isPromise() && (cn != null)
351 && packageName.equals(cn.getPackageName())) {
352 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
353 // For auto install apps update the icon as well as label.
354 mIconCache.getTitleAndIcon(si,
355 si.promisedIntent, user,
356 si.shouldUseLowResIcon());
357 } else {
358 // Only update the icon for restored apps.
359 si.updateIcon(mIconCache);
360 }
361 updates.add(si);
362 }
363 }
364 }
365
366 if (!updates.isEmpty()) {
367 // Push changes to the callback.
368 Runnable r = new Runnable() {
369 public void run() {
370 Callbacks callbacks = getCallback();
371 if (callbacks != null) {
372 callbacks.bindShortcutsChanged(updates,
373 new ArrayList<ShortcutInfo>(), user);
374 }
375 }
376 };
377 mHandler.post(r);
378 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700379 }
380 }
381 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700382 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700383 }
384
Adam Cohen76a47a12014-02-05 11:47:43 -0800385 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800386 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800387
388 if (allAppsApps == null) {
389 throw new RuntimeException("allAppsApps must not be null");
390 }
391 if (allAppsApps.isEmpty()) {
392 return;
393 }
394
395 // Process the newly added applications and add them to the database first
396 Runnable r = new Runnable() {
397 public void run() {
398 runOnMainThread(new Runnable() {
399 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800400 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800401 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500402 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800403 }
404 }
405 });
406 }
407 };
408 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700409 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800410
Sunny Goyala9116722015-04-29 13:55:58 -0700411 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 int[] xy, int spanX, int spanY) {
413 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700414 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
415 final int xCount = (int) profile.numColumns;
416 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800417 boolean[][] occupied = new boolean[xCount][yCount];
418 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700419 for (ItemInfo r : occupiedPos) {
420 int right = r.cellX + r.spanX;
421 int bottom = r.cellY + r.spanY;
422 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
423 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 occupied[x][y] = true;
425 }
426 }
427 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800428 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700429 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
431
432 /**
433 * Find a position on the screen for the given size or adds a new screen.
434 * @return screenId and the coordinates for the item.
435 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700436 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700437 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 ArrayList<Long> workspaceScreens,
439 ArrayList<Long> addedWorkspaceScreensFinal,
440 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700441 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800442
Sunny Goyala9116722015-04-29 13:55:58 -0700443 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700444 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700445 synchronized (sBgLock) {
446 for (ItemInfo info : sBgItemsIdMap) {
447 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
448 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
449 if (items == null) {
450 items = new ArrayList<>();
451 screenItems.put(info.screenId, items);
452 }
453 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800454 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800455 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800456 }
457
458 // Find appropriate space for the item.
459 long screenId = 0;
460 int[] cordinates = new int[2];
461 boolean found = false;
462
463 int screenCount = workspaceScreens.size();
464 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700465 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 if (preferredScreenIndex < screenCount) {
467 screenId = workspaceScreens.get(preferredScreenIndex);
468 found = findNextAvailableIconSpaceInScreen(
469 screenItems.get(screenId), cordinates, spanX, spanY);
470 }
471
472 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700473 // Search on any of the screens starting from the first screen.
474 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800475 screenId = workspaceScreens.get(screen);
476 if (findNextAvailableIconSpaceInScreen(
477 screenItems.get(screenId), cordinates, spanX, spanY)) {
478 // We found a space for it
479 found = true;
480 break;
481 }
482 }
483 }
484
485 if (!found) {
486 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700487 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
488 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
489 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800490
491 // Save the screen id for binding in the workspace
492 workspaceScreens.add(screenId);
493 addedWorkspaceScreensFinal.add(screenId);
494
495 // If we still can't find an empty space, then God help us all!!!
496 if (!findNextAvailableIconSpaceInScreen(
497 screenItems.get(screenId), cordinates, spanX, spanY)) {
498 throw new RuntimeException("Can't find space to add the item");
499 }
500 }
501 return Pair.create(screenId, cordinates);
502 }
503
504 /**
505 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700507 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700508 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800509 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800510 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700511 return;
Winson Chung997a9232013-07-24 15:33:46 -0700512 }
Winson Chung64359a52013-07-08 17:17:08 -0700513 // Process the newly added applications and add them to the database first
514 Runnable r = new Runnable() {
515 public void run() {
516 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
517 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
518
Winson Chung76828c82013-08-19 15:43:29 -0700519 // Get the list of workspace screens. We need to append to this list and
520 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
521 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800522 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700523 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800524 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700525 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700527 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800528 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700529 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800530 }
Winson Chung76828c82013-08-19 15:43:29 -0700531
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700533 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700534 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800535 long screenId = coords.first;
536 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700537
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700538 ItemInfo itemInfo;
539 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
540 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800541 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700542 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700543 } else {
544 throw new RuntimeException("Unexpected info type");
545 }
Winson Chung94d67682013-09-25 16:29:40 -0700546
Winson Chung64359a52013-07-08 17:17:08 -0700547 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700548 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700549 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700550 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700551 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700552 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700553 }
554 }
555
Winson Chung76828c82013-08-19 15:43:29 -0700556 // Update the workspace screens
557 updateWorkspaceScreenOrder(context, workspaceScreens);
558
Adam Cohen76a47a12014-02-05 11:47:43 -0800559 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700560 runOnMainThread(new Runnable() {
561 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800562 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700563 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700564 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
565 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700566 if (!addedShortcutsFinal.isEmpty()) {
567 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
568 long lastScreenId = info.screenId;
569 for (ItemInfo i : addedShortcutsFinal) {
570 if (i.screenId == lastScreenId) {
571 addAnimated.add(i);
572 } else {
573 addNotAnimated.add(i);
574 }
Winson Chung997a9232013-07-24 15:33:46 -0700575 }
576 }
Winson Chungd64d1762013-08-20 14:37:16 -0700577 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800578 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700579 }
Winson Chung64359a52013-07-08 17:17:08 -0700580 }
Winson Chung997a9232013-07-24 15:33:46 -0700581 });
582 }
Winson Chung64359a52013-07-08 17:17:08 -0700583 }
584 };
585 runOnWorkerThread(r);
586 }
587
Sunny Goyald33860f2015-04-23 16:02:20 -0700588 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700589 if (sWorkerThread.getThreadId() == Process.myTid()) {
590 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
591 "main thread");
592 }
593
Sunny Goyald33860f2015-04-23 16:02:20 -0700594 // Remove any queued UI runnables
595 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700596 // Unbind all the workspace items
597 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700598 }
599
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700600 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700601 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700602 // Ensure that we don't use the same workspace items data structure on the main thread
603 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700604 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700605 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700606 tmpItems.addAll(sBgWorkspaceItems);
607 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700608 }
609 Runnable r = new Runnable() {
610 @Override
611 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700612 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700613 item.unbind();
614 }
615 }
616 };
617 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700618 }
619
Joe Onorato9c1289c2009-08-17 11:03:03 -0400620 /**
621 * Adds an item to the DB if it was not created previously, or move it to a new
622 * <container, screen, cellX, cellY>
623 */
624 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700625 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 if (item.container == ItemInfo.NO_ID) {
627 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700628 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 } else {
630 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700631 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
633 }
634
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700635 static void checkItemInfoLocked(
636 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
637 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
638 if (modelItem != null && item != modelItem) {
639 // check all the data is consistent
640 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
641 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
642 ShortcutInfo shortcut = (ShortcutInfo) item;
643 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
644 modelShortcut.intent.filterEquals(shortcut.intent) &&
645 modelShortcut.id == shortcut.id &&
646 modelShortcut.itemType == shortcut.itemType &&
647 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700649 modelShortcut.cellX == shortcut.cellX &&
650 modelShortcut.cellY == shortcut.cellY &&
651 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700652 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700653 // For all intents and purposes, this is the same object
654 return;
655 }
656 }
657
658 // the modelItem needs to match up perfectly with item if our model is
659 // to be consistent with the database-- for now, just require
660 // modelItem == item or the equality check above
661 String msg = "item: " + ((item != null) ? item.toString() : "null") +
662 "modelItem: " +
663 ((modelItem != null) ? modelItem.toString() : "null") +
664 "Error: ItemInfo passed to checkItemInfo doesn't match original";
665 RuntimeException e = new RuntimeException(msg);
666 if (stackTrace != null) {
667 e.setStackTrace(stackTrace);
668 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800669 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700670 }
671 }
672
Michael Jurka816474f2012-06-25 14:49:02 -0700673 static void checkItemInfo(final ItemInfo item) {
674 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
675 final long itemId = item.id;
676 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700677 public void run() {
678 synchronized (sBgLock) {
679 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700680 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700681 }
682 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700683 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700684 }
685
Michael Jurkac9d95c52011-08-29 14:03:34 -0700686 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
687 final ItemInfo item, final String callingFunction) {
688 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700689 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700690 final ContentResolver cr = context.getContentResolver();
691
Adam Cohen487f7dd2012-06-28 18:12:10 -0700692 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700693 Runnable r = new Runnable() {
694 public void run() {
695 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 updateItemArrays(item, itemId, stackTrace);
697 }
698 };
699 runOnWorkerThread(r);
700 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700701
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
703 final ArrayList<ItemInfo> items, final String callingFunction) {
704 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700705
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700706 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
707 Runnable r = new Runnable() {
708 public void run() {
709 ArrayList<ContentProviderOperation> ops =
710 new ArrayList<ContentProviderOperation>();
711 int count = items.size();
712 for (int i = 0; i < count; i++) {
713 ItemInfo item = items.get(i);
714 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700715 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700716 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700717
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
719 updateItemArrays(item, itemId, stackTrace);
720
721 }
722 try {
723 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
724 } catch (Exception e) {
725 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700726 }
727 }
728 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700729 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700731
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700732 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
733 // Lock on mBgLock *after* the db operation
734 synchronized (sBgLock) {
735 checkItemInfoLocked(itemId, item, stackTrace);
736
737 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
738 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
739 // Item is in a folder, make sure this folder exists
740 if (!sBgFolders.containsKey(item.container)) {
741 // An items container is being set to a that of an item which is not in
742 // the list of Folders.
743 String msg = "item: " + item + " container being set to: " +
744 item.container + ", not in the list of folders";
745 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700746 }
747 }
748
749 // Items are added/removed from the corresponding FolderInfo elsewhere, such
750 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
751 // that are on the desktop, as appropriate
752 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800753 if (modelItem != null &&
754 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
755 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700756 switch (modelItem.itemType) {
757 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
758 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
759 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
760 if (!sBgWorkspaceItems.contains(modelItem)) {
761 sBgWorkspaceItems.add(modelItem);
762 }
763 break;
764 default:
765 break;
766 }
767 } else {
768 sBgWorkspaceItems.remove(modelItem);
769 }
770 }
771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700775 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700776 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 item.cellX = cellX;
780 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700781
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 // We store hotseat items in canonical form which is this orientation invariant position
783 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790
791 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
794 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800795 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797
Michael Jurkac9d95c52011-08-29 14:03:34 -0700798 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700799 }
800
801 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700802 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
803 * cellX, cellY have already been updated on the ItemInfos.
804 */
805 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
806 final long container, final int screen) {
807
808 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
809 int count = items.size();
810
811 for (int i = 0; i < count; i++) {
812 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 item.container = container;
814
815 // We store hotseat items in canonical form which is this orientation invariant position
816 // in the hotseat
817 if (context instanceof Launcher && screen < 0 &&
818 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700820 item.cellY);
821 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700822 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 }
824
825 final ContentValues values = new ContentValues();
826 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
827 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
828 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800829 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831
832 contentValues.add(values);
833 }
834 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
835 }
836
837 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700838 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800839 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700840 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700842 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800843 item.cellX = cellX;
844 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700845 item.spanX = spanX;
846 item.spanY = spanY;
847
848 // We store hotseat items in canonical form which is this orientation invariant position
849 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700851 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 }
Adam Cohend4844c32011-02-18 19:25:06 -0800856
Adam Cohend4844c32011-02-18 19:25:06 -0800857 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800858 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700859 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
860 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800861 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700862 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
863 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800865
Michael Jurka816474f2012-06-25 14:49:02 -0700866 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700867 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868
869 /**
870 * Update an item to the database in a specified container.
871 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700872 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700873 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100874 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700875 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800876 }
877
Sunny Goyal756a28a2015-04-23 17:07:55 -0700878 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700879 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700880 synchronized (mLock) {
881 if (!mHasLoaderCompletedOnce ||
882 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
883 throw new RuntimeException("Trying to add shortcut while loader is running");
884 }
885 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700886 }
887 }
888
Adam Cohend4844c32011-02-18 19:25:06 -0800889 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700890 * Returns true if the shortcuts already exists on the workspace. This must be called after
891 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700893 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
894 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700895 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700896 if (intent.getComponent() != null) {
897 // If component is not null, an intent with null package will produce
898 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700899 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700900 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700901 intentWithPkg = intent.toUri(0);
902 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700903 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700904 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
905 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700906 }
907 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700908 intentWithPkg = intent.toUri(0);
909 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700910 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700911
912 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700913 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700914 if (item instanceof ShortcutInfo) {
915 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700916 Intent targetIntent = info.promisedIntent == null
917 ? info.intent : info.promisedIntent;
918 if (targetIntent != null && info.user.equals(user)) {
919 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700920 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
921 return true;
922 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700923 }
924 }
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700927 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700928 }
929
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 * Add an item to the database in a specified container. Sets the container, screen, cellX and
932 * cellY fields of the item. Also assigns an ID to the item.
933 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700934 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700935 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 item.cellX = cellX;
938 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 // We store hotseat items in canonical form which is this orientation invariant position
940 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947
948 final ContentValues values = new ContentValues();
949 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100950 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951
Sunny Goyald2497482015-09-22 18:24:19 -0700952 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
953 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
954
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700955 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700956
Jason Monk8e19cf22014-03-20 15:06:57 -0400957 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700958 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700959 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700960 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700962 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700963 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400964 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700965 sBgItemsIdMap.put(item.id, item);
966 switch (item.itemType) {
967 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
968 sBgFolders.put(item.id, (FolderInfo) item);
969 // Fall through
970 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
971 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
972 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
973 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
974 sBgWorkspaceItems.add(item);
975 } else {
976 if (!sBgFolders.containsKey(item.container)) {
977 // Adding an item to a folder that doesn't exist.
978 String msg = "adding item: " + item + " to a folder that " +
979 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700980 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700981 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700982 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 break;
984 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
985 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
986 break;
987 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700988 }
989 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700990 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700991 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700992 }
993
Sunny Goyal34942622014-08-29 17:20:55 -0700994 private static ArrayList<ItemInfo> getItemsByPackageName(
995 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700996 ItemInfoFilter filter = new ItemInfoFilter() {
997 @Override
998 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
999 return cn.getPackageName().equals(pn) && info.user.equals(user);
1000 }
1001 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001002 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001003 }
1004
1005 /**
1006 * Removes all the items from the database corresponding to the specified package.
1007 */
1008 static void deletePackageFromDatabase(Context context, final String pn,
1009 final UserHandleCompat user) {
1010 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001011 }
1012
1013 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001014 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001016 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001017 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1018 items.add(item);
1019 deleteItemsFromDatabase(context, items);
1020 }
1021
1022 /**
1023 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001024 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001025 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001027 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001028 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001029 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001030 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001031 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001033 // Lock on mBgLock *after* the db operation
1034 synchronized (sBgLock) {
1035 switch (item.itemType) {
1036 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1037 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001038 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001039 if (info.container == item.id) {
1040 // We are deleting a folder which still contains items that
1041 // think they are contained by that folder.
1042 String msg = "deleting a folder (" + item + ") which still " +
1043 "contains items (" + info + ")";
1044 Log.e(TAG, msg);
1045 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001046 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001047 sBgWorkspaceItems.remove(item);
1048 break;
1049 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1050 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1051 sBgWorkspaceItems.remove(item);
1052 break;
1053 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1054 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1055 break;
1056 }
1057 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001059 }
1060 }
Michael Jurka83df1882011-08-31 20:59:26 -07001061 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001062 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 }
1064
1065 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 * Update the order of the workspace screens in the database. The array list contains
1067 * a list of screen ids in the order that they should appear.
1068 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001069 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001070 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001071 final ContentResolver cr = context.getContentResolver();
1072 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1073
1074 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001075 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 while (iter.hasNext()) {
1077 long id = iter.next();
1078 if (id < 0) {
1079 iter.remove();
1080 }
1081 }
1082
1083 Runnable r = new Runnable() {
1084 @Override
1085 public void run() {
Yura085c8532014-02-11 15:15:29 +00001086 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001088 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001089 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 for (int i = 0; i < count; i++) {
1091 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001092 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1094 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001095 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 }
Yura085c8532014-02-11 15:15:29 +00001097
1098 try {
1099 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1100 } catch (Exception ex) {
1101 throw new RuntimeException(ex);
1102 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001103
Winson Chungba9c37f2013-08-30 14:11:37 -07001104 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001105 sBgWorkspaceScreens.clear();
1106 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001107 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001108 }
1109 };
1110 runOnWorkerThread(r);
1111 }
1112
1113 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001114 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 */
Winsonc0b52fe2015-09-09 16:38:15 -07001116 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 final ContentResolver cr = context.getContentResolver();
1118
Michael Jurkac9d95c52011-08-29 14:03:34 -07001119 Runnable r = new Runnable() {
1120 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001121 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001123 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 sBgItemsIdMap.remove(info.id);
1125 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 sBgWorkspaceItems.remove(info);
1127 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001128
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001129 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001130 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001132 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001133 for (ItemInfo childInfo : info.contents) {
1134 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001135 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001136 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001137 }
1138 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001139 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 }
1141
1142 /**
1143 * Set this as the current Launcher activity object for the loader.
1144 */
1145 public void initialize(Callbacks callbacks) {
1146 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001147 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1148 // workspace to prevent leaking Launcher activities on orientation change.
1149 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 mCallbacks = new WeakReference<Callbacks>(callbacks);
1151 }
1152 }
1153
Kenny Guyed131872014-04-30 03:02:21 +01001154 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001155 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001156 int op = PackageUpdatedTask.OP_UPDATE;
1157 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1158 user));
1159 }
1160
1161 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001162 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001163 int op = PackageUpdatedTask.OP_REMOVE;
1164 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1165 user));
1166 }
1167
1168 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001169 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001170 int op = PackageUpdatedTask.OP_ADD;
1171 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1172 user));
1173 }
1174
1175 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001176 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001177 boolean replacing) {
1178 if (!replacing) {
1179 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1180 user));
1181 if (mAppsCanBeOnRemoveableStorage) {
1182 // Only rebind if we support removable storage. It catches the
1183 // case where
1184 // apps on the external sd card need to be reloaded
1185 startLoaderFromBackground();
1186 }
1187 } else {
1188 // If we are replacing then just update the packages in the list
1189 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1190 packageNames, user));
1191 }
1192 }
1193
1194 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001195 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001196 boolean replacing) {
1197 if (!replacing) {
1198 enqueuePackageUpdated(new PackageUpdatedTask(
1199 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1200 user));
1201 }
Kenny Guyed131872014-04-30 03:02:21 +01001202 }
1203
Kenny Guy44cba692016-01-21 19:50:02 +00001204 @Override
1205 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1206 enqueuePackageUpdated(new PackageUpdatedTask(
1207 PackageUpdatedTask.OP_SUSPEND, packageNames,
1208 user));
1209 }
1210
1211 @Override
1212 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1213 enqueuePackageUpdated(new PackageUpdatedTask(
1214 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1215 user));
1216 }
1217
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001218 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1220 * ACTION_PACKAGE_CHANGED.
1221 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001222 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001223 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001224 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001225
Joe Onorato36115782010-06-17 13:28:48 -04001226 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001227 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001228 // If we have changed locale we need to clear out the labels in all apps/workspace.
1229 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001230 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001231 Callbacks callbacks = getCallback();
1232 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001233 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001234 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001235 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Kenny Guyff05f432016-01-22 17:48:29 +00001236 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)
1237 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001238 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001239 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001240 }
1241 }
1242
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001243 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001244 resetLoadedState(true, true);
1245
Reena Lee93f824a2011-09-23 17:20:28 -07001246 // Do this here because if the launcher activity is running it will be restarted.
1247 // If it's not running startLoaderFromBackground will merely tell it that it needs
1248 // to reload.
1249 startLoaderFromBackground();
1250 }
1251
Winson Chungf0c6ae02012-03-21 16:10:31 -07001252 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1253 synchronized (mLock) {
1254 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1255 // mWorkspaceLoaded to true later
1256 stopLoaderLocked();
1257 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1258 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1259 }
1260 }
1261
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001262 /**
1263 * When the launcher is in the background, it's possible for it to miss paired
1264 * configuration changes. So whenever we trigger the loader from the background
1265 * tell the launcher that it needs to re-run the loader when it comes back instead
1266 * of doing it now.
1267 */
1268 public void startLoaderFromBackground() {
1269 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001270 Callbacks callbacks = getCallback();
1271 if (callbacks != null) {
1272 // Only actually run the loader if they're not paused.
1273 if (!callbacks.setLoadOnResume()) {
1274 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001275 }
1276 }
1277 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001278 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001279 }
Joe Onorato36115782010-06-17 13:28:48 -04001280 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001281
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001282 /**
1283 * If there is already a loader task running, tell it to stop.
1284 */
1285 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001286 LoaderTask oldTask = mLoaderTask;
1287 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001288 oldTask.stopLocked();
1289 }
Reena Lee93f824a2011-09-23 17:20:28 -07001290 }
1291
Adam Cohen1a85c582014-09-30 09:48:49 -07001292 public boolean isCurrentCallbacks(Callbacks callbacks) {
1293 return (mCallbacks != null && mCallbacks.get() == callbacks);
1294 }
1295
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001296 public void startLoader(int synchronousBindPage) {
1297 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001298 }
1299
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001300 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001301 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1302 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001303 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001304 // Don't bother to start the thread if we know it's not going to do anything
1305 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001306 final Callbacks oldCallbacks = mCallbacks.get();
1307 // Clear any pending bind-runnables from the synchronized load process.
1308 runOnMainThread(new Runnable() {
1309 public void run() {
1310 oldCallbacks.clearPendingBinds();
1311 }
1312 });
1313
Joe Onorato36115782010-06-17 13:28:48 -04001314 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001315 stopLoaderLocked();
1316 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001317 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001318 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1320 } else {
1321 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1322 sWorker.post(mLoaderTask);
1323 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 }
1326 }
1327
Joe Onorato36115782010-06-17 13:28:48 -04001328 public void stopLoader() {
1329 synchronized (mLock) {
1330 if (mLoaderTask != null) {
1331 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001332 }
1333 }
Joe Onorato36115782010-06-17 13:28:48 -04001334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001336 /**
1337 * Loads the workspace screen ids in an ordered list.
1338 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001339 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001340 final ContentResolver contentResolver = context.getContentResolver();
1341 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001342
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001343 // Get screens ordered by rank.
1344 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1345 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1346 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001347 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001348 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001349 while (sc.moveToNext()) {
1350 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001351 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001352 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001353 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001354 }
1355 }
1356 } finally {
1357 sc.close();
1358 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001359 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001360 }
1361
Joe Onorato36115782010-06-17 13:28:48 -04001362 /**
1363 * Runnable for the thread that loads the contents of the launcher:
1364 * - workspace icons
1365 * - widgets
1366 * - all apps icons
1367 */
1368 private class LoaderTask implements Runnable {
1369 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001370 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001371 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001372 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001373 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001375 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001376 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001377 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001378 }
1379
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001380 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001381 mIsLoadingAndBindingWorkspace = true;
1382
Joe Onorato36115782010-06-17 13:28:48 -04001383 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001384 if (DEBUG_LOADERS) {
1385 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001387
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001388 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001389 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001390 synchronized (LoaderTask.this) {
1391 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001392 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001393 }
1394 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 }
1396 }
1397
Joe Onorato36115782010-06-17 13:28:48 -04001398 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001399 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001400 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001401
Joe Onorato36115782010-06-17 13:28:48 -04001402 private void waitForIdle() {
1403 // Wait until the either we're stopped or the other threads are done.
1404 // This way we don't start loading all apps until the workspace has settled
1405 // down.
1406 synchronized (LoaderTask.this) {
1407 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001408
Joe Onorato36115782010-06-17 13:28:48 -04001409 mHandler.postIdle(new Runnable() {
1410 public void run() {
1411 synchronized (LoaderTask.this) {
1412 mLoadAndBindStepFinished = true;
1413 if (DEBUG_LOADERS) {
1414 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001415 }
Joe Onorato36115782010-06-17 13:28:48 -04001416 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001417 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001418 }
Joe Onorato36115782010-06-17 13:28:48 -04001419 });
1420
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001421 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001422 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001423 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1424 // wait no longer than 1sec at a time
1425 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001426 } catch (InterruptedException ex) {
1427 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001428 }
1429 }
Joe Onorato36115782010-06-17 13:28:48 -04001430 if (DEBUG_LOADERS) {
1431 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001432 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001433 + "ms for previous step to finish binding");
1434 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001435 }
Joe Onorato36115782010-06-17 13:28:48 -04001436 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001437
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001438 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001439 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001440 // Ensure that we have a valid page index to load synchronously
1441 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1442 "valid page index");
1443 }
1444 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1445 // Ensure that we don't try and bind a specified page when the pages have not been
1446 // loaded already (we should load everything asynchronously in that case)
1447 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1448 }
1449 synchronized (mLock) {
1450 if (mIsLoaderTaskRunning) {
1451 // Ensure that we are never running the background loading at this point since
1452 // we also touch the background collections
1453 throw new RuntimeException("Error! Background loading is already running");
1454 }
1455 }
1456
1457 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1458 // data structures, we can't allow any other thread to touch that data, but because
1459 // this call is synchronous, we can get away with not locking).
1460
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001461 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001462 // operations from the previous activity. We need to ensure that all queued operations
1463 // are executed before any synchronous binding work is done.
1464 mHandler.flush();
1465
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 // Divide the set of loaded items into those that we are binding synchronously, and
1467 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001468 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1470 // arise from that.
1471 onlyBindAllApps();
1472 }
1473
Joe Onorato36115782010-06-17 13:28:48 -04001474 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001476 if (mStopped) {
1477 return;
1478 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001479 mIsLoaderTaskRunning = true;
1480 }
Joe Onorato36115782010-06-17 13:28:48 -04001481 // Optimize for end-user experience: if the Launcher is up and // running with the
1482 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1483 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001484 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001485 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001486 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001487
Joe Onorato36115782010-06-17 13:28:48 -04001488 if (mStopped) {
1489 break keep_running;
1490 }
1491
Joe Onorato36115782010-06-17 13:28:48 -04001492 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001493
1494 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001495 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1496 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001497 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498
Joe Onorato36115782010-06-17 13:28:48 -04001499 // Clear out this reference, otherwise we end up holding it until all of the
1500 // callback runnables are done.
1501 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502
Joe Onorato36115782010-06-17 13:28:48 -04001503 synchronized (mLock) {
1504 // If we are still the last one to be scheduled, remove ourselves.
1505 if (mLoaderTask == this) {
1506 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001509 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001510 }
Joe Onorato36115782010-06-17 13:28:48 -04001511 }
1512
1513 public void stopLocked() {
1514 synchronized (LoaderTask.this) {
1515 mStopped = true;
1516 this.notify();
1517 }
1518 }
1519
1520 /**
1521 * Gets the callbacks object. If we've been stopped, or if the launcher object
1522 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1523 * object that was around when the deferred message was scheduled, and if there's
1524 * a new Callbacks object around then also return null. This will save us from
1525 * calling onto it with data that will be ignored.
1526 */
1527 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1528 synchronized (mLock) {
1529 if (mStopped) {
1530 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001531 }
Joe Onorato36115782010-06-17 13:28:48 -04001532
1533 if (mCallbacks == null) {
1534 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001535 }
Joe Onorato36115782010-06-17 13:28:48 -04001536
1537 final Callbacks callbacks = mCallbacks.get();
1538 if (callbacks != oldCallbacks) {
1539 return null;
1540 }
1541 if (callbacks == null) {
1542 Log.w(TAG, "no mCallbacks");
1543 return null;
1544 }
1545
1546 return callbacks;
1547 }
1548 }
1549
1550 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001551 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1552 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001553 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001554 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001555 final int countX = profile.numColumns;
1556 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001559 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001560 // Return early if we detect that an item is under the hotseat button
1561 if (mCallbacks == null ||
1562 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001563 Log.e(TAG, "Error loading shortcut into hotseat " + item
1564 + " into position (" + item.screenId + ":" + item.cellX + ","
1565 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001566 return false;
1567 }
1568
Dan Sandler295ae182013-12-10 16:05:47 -05001569 final ItemInfo[][] hotseatItems =
1570 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1571
Adam Cohen2e6da152015-05-06 11:42:25 -07001572 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001573 Log.e(TAG, "Error loading shortcut " + item
1574 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001575 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001576 + ")");
1577 return false;
1578 }
1579
Dan Sandler295ae182013-12-10 16:05:47 -05001580 if (hotseatItems != null) {
1581 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 Log.e(TAG, "Error loading shortcut into hotseat " + item
1583 + " into position (" + item.screenId + ":" + item.cellX + ","
1584 + item.cellY + ") occupied by "
1585 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1586 [(int) item.screenId][0]);
1587 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001588 } else {
1589 hotseatItems[(int) item.screenId][0] = item;
1590 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001591 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001592 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001593 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001594 items[(int) item.screenId][0] = item;
1595 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001596 return true;
1597 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001598 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1599 if (!workspaceScreens.contains((Long) item.screenId)) {
1600 // The item has an invalid screen id.
1601 return false;
1602 }
1603 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001604 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001605 return true;
1606 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001607
Adam Cohendcd297f2013-06-18 13:13:40 -07001608 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001609 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 occupied.put(item.screenId, items);
1611 }
1612
Dan Sandler295ae182013-12-10 16:05:47 -05001613 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001614 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1615 item.cellX < 0 || item.cellY < 0 ||
1616 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1617 Log.e(TAG, "Error loading shortcut " + item
1618 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1619 + item.cellX + "," + item.cellY
1620 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1621 return false;
1622 }
1623
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001624 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001625 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1626 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001627 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001628 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001630 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001631 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001632 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001633 return false;
1634 }
1635 }
1636 }
1637 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1638 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001639 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001640 }
1641 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001642
Joe Onorato36115782010-06-17 13:28:48 -04001643 return true;
1644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645
Winson Chungba9c37f2013-08-30 14:11:37 -07001646 /** Clears all the sBg data structures */
1647 private void clearSBgDataStructures() {
1648 synchronized (sBgLock) {
1649 sBgWorkspaceItems.clear();
1650 sBgAppWidgets.clear();
1651 sBgFolders.clear();
1652 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001653 sBgWorkspaceScreens.clear();
1654 }
1655 }
1656
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001657 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001658 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659
Joe Onorato36115782010-06-17 13:28:48 -04001660 final Context context = mContext;
1661 final ContentResolver contentResolver = context.getContentResolver();
1662 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001663 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001664 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001665 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001666
Winson Chung892c74d2013-08-22 16:15:50 -07001667 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001668 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001669 int countX = profile.numColumns;
1670 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001671
Sunny Goyalf076eae2016-01-11 12:25:10 -08001672 if (GridSizeMigrationTask.ENABLED &&
1673 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1674 // Migration failed. Clear workspace.
1675 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001676 }
1677
Dan Sandlerd5024042014-01-09 15:01:33 -05001678 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001679 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001680 LauncherSettings.Settings.call(contentResolver,
1681 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001682 }
1683
1684 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1685 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001686 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001687 LauncherSettings.Settings.call(contentResolver,
1688 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001689 } else {
1690 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001691 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001692 LauncherSettings.Settings.call(contentResolver,
1693 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001694 }
Adam Cohene25af792013-06-06 23:08:25 -07001695
Winson Chung2abf94d2012-07-18 18:16:38 -07001696 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001697 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001698 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001699 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001700 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001701
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001703 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001704 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001705 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001706 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001707
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 // +1 for the hotseat (it can be larger than the workspace)
1709 // Load workspace in reverse order to ensure that latest items are loaded first (and
1710 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001711 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 try {
1714 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1715 final int intentIndex = c.getColumnIndexOrThrow
1716 (LauncherSettings.Favorites.INTENT);
1717 final int titleIndex = c.getColumnIndexOrThrow
1718 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 final int containerIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.CONTAINER);
1721 final int itemTypeIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.ITEM_TYPE);
1723 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001725 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 final int screenIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.SCREEN);
1729 final int cellXIndex = c.getColumnIndexOrThrow
1730 (LauncherSettings.Favorites.CELLX);
1731 final int cellYIndex = c.getColumnIndexOrThrow
1732 (LauncherSettings.Favorites.CELLY);
1733 final int spanXIndex = c.getColumnIndexOrThrow
1734 (LauncherSettings.Favorites.SPANX);
1735 final int spanYIndex = c.getColumnIndexOrThrow(
1736 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001737 final int rankIndex = c.getColumnIndexOrThrow(
1738 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001739 final int restoredIndex = c.getColumnIndexOrThrow(
1740 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001741 final int profileIdIndex = c.getColumnIndexOrThrow(
1742 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001743 final int optionsIndex = c.getColumnIndexOrThrow(
1744 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001745 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746
Sunny Goyal7f834d22015-04-21 10:10:23 -07001747 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001748 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001749 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001750 long serialNo = mUserManager.getSerialNumberForUser(user);
1751 allUsers.put(serialNo, user);
1752 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001753 }
1754
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 ShortcutInfo info;
1756 String intentDescription;
1757 LauncherAppWidgetInfo appWidgetInfo;
1758 int container;
1759 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001760 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001762 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 while (!mStopped && c.moveToNext()) {
1765 try {
1766 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001767 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001768 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001769 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 switch (itemType) {
1772 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1773 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001774 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001776 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001777 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001778 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001779 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001780 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001781 if (user == null) {
1782 // User has been deleted remove the item.
1783 itemsToRemove.add(id);
1784 continue;
1785 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 try {
1787 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001788 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001789 if (cn != null && cn.getPackageName() != null) {
1790 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1791 cn.getPackageName(), user);
1792 boolean validComponent = validPkg &&
1793 launcherApps.isActivityEnabledForProfile(cn, user);
1794
1795 if (validComponent) {
1796 if (restored) {
1797 // no special handling necessary for this item
1798 restoredRows.add(id);
1799 restored = false;
1800 }
Kenny Guy44cba692016-01-21 19:50:02 +00001801 boolean isSuspended = launcherApps.isPackageSuspendedForProfile(
1802 cn.getPackageName(), user);
1803 if (isSuspended) {
1804 disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1805 }
Kenny Guyff05f432016-01-22 17:48:29 +00001806 if (quietMode.get(serialNumber)) {
1807 disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER;
1808 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001809 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001810 intent = null;
1811 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1812 // We allow auto install apps to have their intent
1813 // updated after an install.
1814 intent = manager.getLaunchIntentForPackage(
1815 cn.getPackageName());
1816 if (intent != null) {
1817 ContentValues values = new ContentValues();
1818 values.put(LauncherSettings.Favorites.INTENT,
1819 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001820 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001821 }
1822 }
1823
1824 if (intent == null) {
1825 // The app is installed but the component is no
1826 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001827 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001828 itemsToRemove.add(id);
1829 continue;
1830 } else {
1831 // no special handling necessary for this item
1832 restoredRows.add(id);
1833 restored = false;
1834 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001835 } else if (restored) {
1836 // Package is not yet available but might be
1837 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001838 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001839
1840 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1841 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001842 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001843 // App restore has started. Update the flag
1844 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1845 ContentValues values = new ContentValues();
1846 values.put(LauncherSettings.Favorites.RESTORED,
1847 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001848 updateItem(id, values);
1849 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1850 // This is a common app. Try to replace this.
1851 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1852 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1853 if (parser.findDefaultApp()) {
1854 // Default app found. Replace it.
1855 intent = parser.parsedIntent;
1856 cn = intent.getComponent();
1857 ContentValues values = parser.parsedValues;
1858 values.put(LauncherSettings.Favorites.RESTORED, 0);
1859 updateItem(id, values);
1860 restored = false;
1861 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001862
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001863 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001864 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001865 itemsToRemove.add(id);
1866 continue;
1867 }
Sunny Goyal94485362014-09-18 16:13:58 -07001868 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001869 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001870 itemsToRemove.add(id);
1871 continue;
1872 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001873 } else if (launcherApps.isAppEnabled(
1874 manager, cn.getPackageName(),
1875 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1876 // Package is present but not available.
1877 allowMissingTarget = true;
1878 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1879 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001880 // SdCard is not ready yet. Package might get available,
1881 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001882 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001883 HashSet<String> pkgs = sPendingPackages.get(user);
1884 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001885 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001886 sPendingPackages.put(user, pkgs);
1887 }
1888 pkgs.add(cn.getPackageName());
1889 allowMissingTarget = true;
1890 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001891
1892 } else {
1893 // Do not wait for external media load anymore.
1894 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001895 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001896 itemsToRemove.add(id);
1897 continue;
Winson Chungee055712013-07-30 14:46:24 -07001898 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001899 } else if (cn == null) {
1900 // For shortcuts with no component, keep them as they are
1901 restoredRows.add(id);
1902 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001903 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001905 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001906 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 continue;
1908 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001909
Sunny Goyal34b65272015-03-11 16:56:52 -07001910 boolean useLowResIcon = container >= 0 &&
1911 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1912
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001913 if (itemReplaced) {
1914 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001915 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001916 cursorIconInfo.iconIndex, titleIndex,
1917 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001918 } else {
1919 // Don't replace items for other profiles.
1920 itemsToRemove.add(id);
1921 continue;
1922 }
1923 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001924 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001925 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001926 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001927 intent = getRestoredItemIntent(c, context, intent);
1928 } else {
1929 // Don't restore items for other profiles.
1930 itemsToRemove.add(id);
1931 continue;
1932 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001933 } else if (itemType ==
1934 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001935 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001936 cursorIconInfo.iconIndex, titleIndex,
1937 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001938 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001939 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001940
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001941 // App shortcuts that used to be automatically added to Launcher
1942 // didn't always have the correct intent flags set, so do that
1943 // here
1944 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001945 intent.getCategories() != null &&
1946 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001947 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 intent.addFlags(
1949 Intent.FLAG_ACTIVITY_NEW_TASK |
1950 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1951 }
Michael Jurka96879562012-03-22 05:54:33 -07001952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001954 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001955 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001956 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 info.cellX = c.getInt(cellXIndex);
1960 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001961 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001962 info.spanX = 1;
1963 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001964 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001965 if (info.promisedIntent != null) {
1966 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1967 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001968 info.isDisabled = disabledState;
1969 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1970 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1971 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001972
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001974 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001975 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 break;
1977 }
1978
Sunny Goyal756adbc2015-04-16 15:20:51 -07001979 if (restored) {
1980 ComponentName cn = info.getTargetComponent();
1981 if (cn != null) {
1982 Integer progress = installingPkgs.get(cn.getPackageName());
1983 if (progress != null) {
1984 info.setInstallProgress(progress);
1985 } else {
1986 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1987 }
1988 }
1989 }
1990
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 switch (container) {
1992 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1993 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1994 sBgWorkspaceItems.add(info);
1995 break;
1996 default:
1997 // Item is in a user folder
1998 FolderInfo folderInfo =
1999 findOrMakeFolder(sBgFolders, container);
2000 folderInfo.add(info);
2001 break;
2002 }
2003 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002004 } else {
2005 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 }
2007 break;
2008
2009 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2010 id = c.getLong(idIndex);
2011 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2012
Sunny Goyala508e4f2015-05-21 09:33:57 -07002013 // Do not trim the folder label, as is was set by the user.
2014 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 folderInfo.cellX = c.getInt(cellXIndex);
2019 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002020 folderInfo.spanX = 1;
2021 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002022 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023
Daniel Sandler8802e962010-05-26 16:28:16 -04002024 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002025 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002026 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002027 break;
2028 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002029
Joe Onorato9c1289c2009-08-17 11:03:03 -04002030 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2032 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2033 sBgWorkspaceItems.add(folderInfo);
2034 break;
Joe Onorato36115782010-06-17 13:28:48 -04002035 }
Joe Onorato17a89222011-02-08 17:26:11 -08002036
Chris Wrenf4d08112014-01-16 18:13:56 -05002037 if (restored) {
2038 // no special handling required for restored folders
2039 restoredRows.add(id);
2040 }
2041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2043 sBgFolders.put(folderInfo.id, folderInfo);
2044 break;
2045
2046 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002047 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002049 boolean customWidget = itemType ==
2050 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2051
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002053 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002054 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002055 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002056 user = allUsers.get(serialNumber);
2057 if (user == null) {
2058 itemsToRemove.add(id);
2059 continue;
2060 }
2061
Sunny Goyalff572272014-07-23 13:58:07 -07002062 final ComponentName component =
2063 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002064
Sunny Goyal651077b2014-06-30 14:15:31 -07002065 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002066 final boolean isIdValid = (restoreStatus &
2067 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002068 final boolean wasProviderReady = (restoreStatus &
2069 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002070
Adam Cohen59400422014-03-05 18:07:04 -08002071 final LauncherAppWidgetProviderInfo provider =
2072 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002073 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002074 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002075
2076 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002077 if (!isSafeMode && !customWidget &&
2078 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002079 addDumpLog("Deleting widget that isn't installed anymore: "
2080 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 itemsToRemove.add(id);
2082 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002083 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002084 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2085 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002086
Sunny Goyal53f96722015-07-13 19:54:53 -07002087 // The provider is available. So the widget is either
2088 // available or not available. We do not need to track
2089 // any future restore updates.
2090 int status = restoreStatus &
2091 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002092 if (!wasProviderReady) {
2093 // If provider was not previously ready, update the
2094 // status and UI flag.
2095
2096 // Id would be valid only if the widget restore broadcast was received.
2097 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002098 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002099 } else {
2100 status &= ~LauncherAppWidgetInfo
2101 .FLAG_PROVIDER_NOT_READY;
2102 }
2103 }
2104 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002105 } else {
2106 Log.v(TAG, "Widget restore pending id=" + id
2107 + " appWidgetId=" + appWidgetId
2108 + " status =" + restoreStatus);
2109 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002110 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002111 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002112 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002113
2114 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2115 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002116 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002117 // App restore has started. Update the flag
2118 appWidgetInfo.restoreStatus |=
2119 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002120 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002121 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002122 itemsToRemove.add(id);
2123 continue;
2124 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002125
2126 appWidgetInfo.installProgress =
2127 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002128 }
Sunny Goyalff572272014-07-23 13:58:07 -07002129
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002131 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 appWidgetInfo.cellX = c.getInt(cellXIndex);
2133 appWidgetInfo.cellY = c.getInt(cellYIndex);
2134 appWidgetInfo.spanX = c.getInt(spanXIndex);
2135 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002136 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002137
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2139 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2140 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002141 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2142 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 continue;
2144 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002146 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002148 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002149 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 break;
2151 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002152
Adam Cohen59400422014-03-05 18:07:04 -08002153 if (!customWidget) {
2154 String providerName =
2155 appWidgetInfo.providerName.flattenToString();
2156 if (!providerName.equals(savedProvider) ||
2157 (appWidgetInfo.restoreStatus != restoreStatus)) {
2158 ContentValues values = new ContentValues();
2159 values.put(
2160 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2161 providerName);
2162 values.put(LauncherSettings.Favorites.RESTORED,
2163 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002164 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002165 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002166 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2168 sBgAppWidgets.add(appWidgetInfo);
2169 }
Joe Onorato36115782010-06-17 13:28:48 -04002170 break;
2171 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002173 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002174 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 }
2176 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002177 if (c != null) {
2178 c.close();
2179 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 }
2181
Winson Chungba9c37f2013-08-30 14:11:37 -07002182 // Break early if we've stopped loading
2183 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002184 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002185 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002186 }
2187
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002190 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2191 Utilities.createDbSelectionQuery(
2192 LauncherSettings.Favorites._ID, itemsToRemove), null);
2193 if (DEBUG_LOADERS) {
2194 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2195 LauncherSettings.Favorites._ID, itemsToRemove));
2196 }
2197
2198 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002199 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2200 .call(contentResolver,
2201 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2202 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2203 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002204 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2205 sBgFolders.remove(folderId);
2206 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002207 }
2208 }
2209
Sunny Goyal317698b2015-07-29 11:45:41 -07002210 // Sort all the folder items and make sure the first 3 items are high resolution.
2211 for (FolderInfo folder : sBgFolders) {
2212 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2213 int pos = 0;
2214 for (ShortcutInfo info : folder.contents) {
2215 if (info.usingLowResIcon) {
2216 info.updateIcon(mIconCache, false);
2217 }
2218 pos ++;
2219 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2220 break;
2221 }
2222 }
2223 }
2224
Chris Wrenf4d08112014-01-16 18:13:56 -05002225 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002226 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002227 ContentValues values = new ContentValues();
2228 values.put(LauncherSettings.Favorites.RESTORED, 0);
2229 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2230 Utilities.createDbSelectionQuery(
2231 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002232 }
2233
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002234 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2235 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002236 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002237 null, sWorker);
2238 }
2239
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002240 // Remove any empty screens
2241 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002242 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002243 long screenId = item.screenId;
2244 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2245 unusedScreens.contains(screenId)) {
2246 unusedScreens.remove(screenId);
2247 }
2248 }
2249
2250 // If there are any empty screens remove them, and update.
2251 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002252 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002254 }
2255
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256 if (DEBUG_LOADERS) {
2257 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2258 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002260 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002262
Sunny Goyale2df0622015-04-24 11:27:00 -07002263 for (int i = 0; i < nScreens; i++) {
2264 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002265 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 line += " | ";
2267 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002268 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002269 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002270 if (x < screen.length && y < screen[x].length) {
2271 line += (screen[x][y] != null) ? "#" : ".";
2272 } else {
2273 line += "!";
2274 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 }
Joe Onorato36115782010-06-17 13:28:48 -04002276 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002278 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 }
Joe Onorato36115782010-06-17 13:28:48 -04002280 }
Adam Cohene25af792013-06-06 23:08:25 -07002281 }
2282
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002283 /**
2284 * Partially updates the item without any notification. Must be called on the worker thread.
2285 */
2286 private void updateItem(long itemId, ContentValues update) {
2287 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002288 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002289 update,
2290 BaseColumns._ID + "= ?",
2291 new String[]{Long.toString(itemId)});
2292 }
2293
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294 /** Filters the set of items who are directly or indirectly (via another container) on the
2295 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002296 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 ArrayList<ItemInfo> allWorkspaceItems,
2298 ArrayList<ItemInfo> currentScreenItems,
2299 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002300 // Purge any null ItemInfos
2301 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2302 while (iter.hasNext()) {
2303 ItemInfo i = iter.next();
2304 if (i == null) {
2305 iter.remove();
2306 }
2307 }
2308
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 // Order the set of items by their containers first, this allows use to walk through the
2310 // list sequentially, build up a list of containers that are in the specified screen,
2311 // as well as all items in those containers.
2312 Set<Long> itemsOnScreen = new HashSet<Long>();
2313 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2314 @Override
2315 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002316 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 }
2318 });
2319 for (ItemInfo info : allWorkspaceItems) {
2320 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002321 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 currentScreenItems.add(info);
2323 itemsOnScreen.add(info.id);
2324 } else {
2325 otherScreenItems.add(info);
2326 }
2327 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2328 currentScreenItems.add(info);
2329 itemsOnScreen.add(info.id);
2330 } else {
2331 if (itemsOnScreen.contains(info.container)) {
2332 currentScreenItems.add(info);
2333 itemsOnScreen.add(info.id);
2334 } else {
2335 otherScreenItems.add(info);
2336 }
2337 }
2338 }
2339 }
2340
2341 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002342 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 ArrayList<LauncherAppWidgetInfo> appWidgets,
2344 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2345 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002346
2347 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002348 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002350 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 currentScreenWidgets.add(widget);
2352 } else {
2353 otherScreenWidgets.add(widget);
2354 }
2355 }
2356 }
2357
2358 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002359 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002360 LongArrayMap<ItemInfo> itemsIdMap,
2361 LongArrayMap<FolderInfo> folders,
2362 LongArrayMap<FolderInfo> currentScreenFolders,
2363 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364
Sunny Goyale2df0622015-04-24 11:27:00 -07002365 int total = folders.size();
2366 for (int i = 0; i < total; i++) {
2367 long id = folders.keyAt(i);
2368 FolderInfo folder = folders.valueAt(i);
2369
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002371 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002373 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 currentScreenFolders.put(id, folder);
2375 } else {
2376 otherScreenFolders.put(id, folder);
2377 }
2378 }
2379 }
2380
2381 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2382 * right) */
2383 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002384 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002385 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002386 final int screenCols = profile.numColumns;
2387 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002389 @Override
2390 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002391 if (lhs.container == rhs.container) {
2392 // Within containers, order by their spatial position in that container
2393 switch ((int) lhs.container) {
2394 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2395 long lr = (lhs.screenId * screenCellCount +
2396 lhs.cellY * screenCols + lhs.cellX);
2397 long rr = (rhs.screenId * screenCellCount +
2398 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002399 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002400 }
2401 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2402 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002403 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002404 }
2405 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002406 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002407 throw new RuntimeException("Unexpected container type when " +
2408 "sorting workspace items.");
2409 }
2410 return 0;
2411 }
2412 } else {
2413 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002414 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002415 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002416 }
2417 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002419
Adam Cohendcd297f2013-06-18 13:13:40 -07002420 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2421 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002422 final Runnable r = new Runnable() {
2423 @Override
2424 public void run() {
2425 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2426 if (callbacks != null) {
2427 callbacks.bindScreens(orderedScreens);
2428 }
2429 }
2430 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002431 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002432 }
2433
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002434 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2435 final ArrayList<ItemInfo> workspaceItems,
2436 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002437 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002438 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439
2440 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002441 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002443 final int start = i;
2444 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445 final Runnable r = new Runnable() {
2446 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002447 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002448 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002449 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002450 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2451 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002452 }
2453 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002455 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002456 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457
2458 // Bind the folders
2459 if (!folders.isEmpty()) {
2460 final Runnable r = new Runnable() {
2461 public void run() {
2462 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2463 if (callbacks != null) {
2464 callbacks.bindFolders(folders);
2465 }
2466 }
2467 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002468 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002469 }
2470
2471 // Bind the widgets, one at a time
2472 N = appWidgets.size();
2473 for (int i = 0; i < N; i++) {
2474 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2475 final Runnable r = new Runnable() {
2476 public void run() {
2477 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2478 if (callbacks != null) {
2479 callbacks.bindAppWidget(widget);
2480 }
2481 }
2482 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002483 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 }
2485 }
2486
2487 /**
2488 * Binds all loaded data to actual views on the main thread.
2489 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002490 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 final long t = SystemClock.uptimeMillis();
2492 Runnable r;
2493
2494 // Don't use these two variables in any of the callback runnables.
2495 // Otherwise we hold a reference to them.
2496 final Callbacks oldCallbacks = mCallbacks.get();
2497 if (oldCallbacks == null) {
2498 // This launcher has exited and nobody bothered to tell us. Just bail.
2499 Log.w(TAG, "LoaderTask running with no launcher");
2500 return;
2501 }
2502
Winson Chung9b9fb962013-11-15 15:39:34 -08002503 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2505 ArrayList<LauncherAppWidgetInfo> appWidgets =
2506 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002507 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002508
2509 final LongArrayMap<FolderInfo> folders;
2510 final LongArrayMap<ItemInfo> itemsIdMap;
2511
Winson Chung2abf94d2012-07-18 18:16:38 -07002512 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 workspaceItems.addAll(sBgWorkspaceItems);
2514 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002515 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002516
2517 folders = sBgFolders.clone();
2518 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519 }
2520
Derek Prothro7aff3992013-12-10 14:00:37 -05002521 final boolean isLoadingSynchronously =
2522 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002523 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002524 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002525 if (currScreen >= orderedScreenIds.size()) {
2526 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002527 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002528 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002529 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002530 final long currentScreenId = currentScreen < 0
2531 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002532
2533 // Load all the items that are on the current page first (and in the process, unbind
2534 // all the existing workspace items before we call startBinding() below.
2535 unbindWorkspaceItemsOnMainThread();
2536
2537 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2539 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2540 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2541 new ArrayList<LauncherAppWidgetInfo>();
2542 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2543 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002544 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2545 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546
Winson Chung9b9fb962013-11-15 15:39:34 -08002547 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002549 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002551 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552 otherFolders);
2553 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2554 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2555
2556 // Tell the workspace that we're about to start binding items
2557 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002558 public void run() {
2559 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2560 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002561 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002563 }
2564 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002566 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567
Adam Cohendcd297f2013-06-18 13:13:40 -07002568 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2569
Sunny Goyal527c7d32015-08-28 15:19:36 -07002570 Executor mainExecutor = new DeferredMainThreadExecutor();
2571 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002572 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002573 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002574
Sunny Goyal527c7d32015-08-28 15:19:36 -07002575 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2576 // remaining screens after first onDraw is called. This ensures that the first screen
2577 // is immediately visible (eg. during rotation)
2578 // In case of !isLoadingSynchronously, bind all pages one after other.
2579 final Executor deferredExecutor = isLoadingSynchronously ?
2580 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2581
2582 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2583 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002584
2585 // Tell the workspace that we're done binding items
2586 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002587 public void run() {
2588 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2589 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002590 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002591 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592
Sunny Goyal639e9062015-08-19 19:17:06 -07002593 mIsLoadingAndBindingWorkspace = false;
2594
2595 // Run all the bind complete runnables after workspace is bound.
2596 if (!mBindCompleteRunnables.isEmpty()) {
2597 synchronized (mBindCompleteRunnables) {
2598 for (final Runnable r : mBindCompleteRunnables) {
2599 runOnWorkerThread(r);
2600 }
2601 mBindCompleteRunnables.clear();
2602 }
2603 }
2604
Winson Chung98e030b2012-05-07 16:01:11 -07002605 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002606 if (DEBUG_LOADERS) {
2607 Log.d(TAG, "bound workspace in "
2608 + (SystemClock.uptimeMillis()-t) + "ms");
2609 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002610
Joe Onorato36115782010-06-17 13:28:48 -04002611 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002612 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002613 deferredExecutor.execute(r);
2614
Winson Chung4a2afa32012-07-19 14:53:05 -07002615 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002616 r = new Runnable() {
2617 public void run() {
2618 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2619 if (callbacks != null) {
2620 // We are loading synchronously, which means, some of the pages will be
2621 // bound after first draw. Inform the callbacks that page binding is
2622 // not complete, and schedule the remaining pages.
2623 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2624 callbacks.onPageBoundSynchronously(currentScreen);
2625 }
2626 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2627 }
2628 }
2629 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002630 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002631 }
Joe Onorato36115782010-06-17 13:28:48 -04002632 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002633
Joe Onorato36115782010-06-17 13:28:48 -04002634 private void loadAndBindAllApps() {
2635 if (DEBUG_LOADERS) {
2636 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2637 }
2638 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002639 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002640 synchronized (LoaderTask.this) {
2641 if (mStopped) {
2642 return;
2643 }
2644 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002645 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002646 synchronized (LoaderTask.this) {
2647 if (mStopped) {
2648 return;
2649 }
2650 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002651 }
Joe Onorato36115782010-06-17 13:28:48 -04002652 } else {
2653 onlyBindAllApps();
2654 }
2655 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002656
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002657 private void updateIconCache() {
2658 // Ignore packages which have a promise icon.
2659 HashSet<String> packagesToIgnore = new HashSet<>();
2660 synchronized (sBgLock) {
2661 for (ItemInfo info : sBgItemsIdMap) {
2662 if (info instanceof ShortcutInfo) {
2663 ShortcutInfo si = (ShortcutInfo) info;
2664 if (si.isPromise() && si.getTargetComponent() != null) {
2665 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2666 }
2667 } else if (info instanceof LauncherAppWidgetInfo) {
2668 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2669 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2670 packagesToIgnore.add(lawi.providerName.getPackageName());
2671 }
2672 }
2673 }
2674 }
2675 mIconCache.updateDbIcons(packagesToIgnore);
2676 }
2677
Joe Onorato36115782010-06-17 13:28:48 -04002678 private void onlyBindAllApps() {
2679 final Callbacks oldCallbacks = mCallbacks.get();
2680 if (oldCallbacks == null) {
2681 // This launcher has exited and nobody bothered to tell us. Just bail.
2682 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2683 return;
2684 }
2685
2686 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002687 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002688 final ArrayList<AppInfo> list
2689 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002690 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002691 public void run() {
2692 final long t = SystemClock.uptimeMillis();
2693 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2694 if (callbacks != null) {
2695 callbacks.bindAllApplications(list);
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002696 callbacks.bindAllPackages(mFgWidgetsModel);
Joe Onorato36115782010-06-17 13:28:48 -04002697 }
2698 if (DEBUG_LOADERS) {
2699 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002700 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002701 }
2702 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002703 };
2704 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002705 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002706 r.run();
2707 } else {
2708 mHandler.post(r);
2709 }
Joe Onorato36115782010-06-17 13:28:48 -04002710 }
2711
Winson Chung64359a52013-07-08 17:17:08 -07002712 private void loadAllApps() {
2713 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002714
Joe Onorato36115782010-06-17 13:28:48 -04002715 final Callbacks oldCallbacks = mCallbacks.get();
2716 if (oldCallbacks == null) {
2717 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002718 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002719 return;
2720 }
2721
Kenny Guyed131872014-04-30 03:02:21 +01002722 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2723
Winson Chung64359a52013-07-08 17:17:08 -07002724 // Clear the list of apps
2725 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002726 for (UserHandleCompat user : profiles) {
2727 // Query for the set of apps
2728 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002729 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002730 if (DEBUG_LOADERS) {
2731 Log.d(TAG, "getActivityList took "
2732 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2733 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2734 }
2735 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002736 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002737 if (apps == null || apps.isEmpty()) {
2738 return;
2739 }
Kenny Guyff05f432016-01-22 17:48:29 +00002740 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002741 // Create the ApplicationInfos
2742 for (int i = 0; i < apps.size(); i++) {
2743 LauncherActivityInfoCompat app = apps.get(i);
2744 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002745 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002746 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002747
Sunny Goyal756a28a2015-04-23 17:07:55 -07002748 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2749 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002750 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002751
2752 @Override
2753 public void run() {
2754 heuristic.processUserApps(apps);
2755 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002756 };
2757 runOnMainThread(new Runnable() {
2758
2759 @Override
2760 public void run() {
2761 // Check isLoadingWorkspace on the UI thread, as it is updated on
2762 // the UI thread.
2763 if (mIsLoadingAndBindingWorkspace) {
2764 synchronized (mBindCompleteRunnables) {
2765 mBindCompleteRunnables.add(r);
2766 }
2767 } else {
2768 runOnWorkerThread(r);
2769 }
2770 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002771 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002772 }
Winson Chung64359a52013-07-08 17:17:08 -07002773 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002774 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002775 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2776 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002777
2778 // Post callback on main thread
2779 mHandler.post(new Runnable() {
2780 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002781
Winson Chung64359a52013-07-08 17:17:08 -07002782 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002783 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002784 if (callbacks != null) {
2785 callbacks.bindAllApplications(added);
2786 if (DEBUG_LOADERS) {
2787 Log.d(TAG, "bound " + added.size() + " apps in "
2788 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2789 }
2790 } else {
2791 Log.i(TAG, "not binding apps: no Launcher activity");
2792 }
2793 }
2794 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002795 // Cleanup any data stored for a deleted user.
2796 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002797
Sunny Goyal2d648b02015-08-11 13:56:28 -07002798 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002799 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002800 Log.d(TAG, "Icons processed in "
2801 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002802 }
2803 }
2804
2805 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002806 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002807 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002808 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2809 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2810 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2811 }
Joe Onorato36115782010-06-17 13:28:48 -04002812 }
2813 }
2814
Sunny Goyal75b0f552015-05-20 21:57:06 -07002815 /**
2816 * Called when the icons for packages have been updated in the icon cache.
2817 */
2818 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2819 final Callbacks callbacks = getCallback();
2820 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2821 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2822
2823 // If any package icon has changed (app was updated while launcher was dead),
2824 // update the corresponding shortcuts.
2825 synchronized (sBgLock) {
2826 for (ItemInfo info : sBgItemsIdMap) {
2827 if (info instanceof ShortcutInfo && user.equals(info.user)
2828 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2829 ShortcutInfo si = (ShortcutInfo) info;
2830 ComponentName cn = si.getTargetComponent();
2831 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2832 si.updateIcon(mIconCache);
2833 updatedShortcuts.add(si);
2834 }
2835 }
2836 }
2837 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2838 }
2839
2840 if (!updatedShortcuts.isEmpty()) {
2841 final UserHandleCompat userFinal = user;
2842 mHandler.post(new Runnable() {
2843
2844 public void run() {
2845 Callbacks cb = getCallback();
2846 if (cb != null && callbacks == cb) {
2847 cb.bindShortcutsChanged(updatedShortcuts,
2848 new ArrayList<ShortcutInfo>(), userFinal);
2849 }
2850 }
2851 });
2852 }
2853
2854 if (!updatedApps.isEmpty()) {
2855 mHandler.post(new Runnable() {
2856
2857 public void run() {
2858 Callbacks cb = getCallback();
2859 if (cb != null && callbacks == cb) {
2860 cb.bindAppsUpdated(updatedApps);
2861 }
2862 }
2863 });
2864 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002865
2866 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002867 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002868 }
2869
Joe Onorato36115782010-06-17 13:28:48 -04002870 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002871 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002872 }
2873
Adam Cohen091440a2015-03-18 14:16:05 -07002874 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002875
2876 @Override
2877 public void onReceive(Context context, Intent intent) {
2878 synchronized (sBgLock) {
2879 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2880 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002881 final PackageManager manager = context.getPackageManager();
2882 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2883 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002884 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2885 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002886 packagesRemoved.clear();
2887 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002888 for (String pkg : entry.getValue()) {
2889 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002890 boolean packageOnSdcard = launcherApps.isAppEnabled(
2891 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2892 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002893 packagesUnavailable.add(pkg);
2894 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002895 packagesRemoved.add(pkg);
2896 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002897 }
2898 }
2899 if (!packagesRemoved.isEmpty()) {
2900 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2901 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2902 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002903 if (!packagesUnavailable.isEmpty()) {
2904 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2905 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2906 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002907 }
Sunny Goyal34942622014-08-29 17:20:55 -07002908 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002909 }
2910 }
2911 }
2912
Joe Onorato36115782010-06-17 13:28:48 -04002913 private class PackageUpdatedTask implements Runnable {
2914 int mOp;
2915 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002916 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002917
2918 public static final int OP_NONE = 0;
2919 public static final int OP_ADD = 1;
2920 public static final int OP_UPDATE = 2;
2921 public static final int OP_REMOVE = 3; // uninstlled
2922 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002923 public static final int OP_SUSPEND = 5; // package suspended
2924 public static final int OP_UNSUSPEND = 6; // package unsuspended
Joe Onorato36115782010-06-17 13:28:48 -04002925
2926
Kenny Guyed131872014-04-30 03:02:21 +01002927 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002928 mOp = op;
2929 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002930 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002931 }
2932
2933 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002934 if (!mHasLoaderCompletedOnce) {
2935 // Loader has not yet run.
2936 return;
2937 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002938 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002939
2940 final String[] packages = mPackages;
2941 final int N = packages.length;
2942 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002943 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002944 for (int i=0; i<N; i++) {
2945 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002946 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002947 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002948 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002949
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002950 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2951 if (heuristic != null) {
2952 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002953 }
Joe Onorato36115782010-06-17 13:28:48 -04002954 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002955 }
Joe Onorato36115782010-06-17 13:28:48 -04002956 case OP_UPDATE:
2957 for (int i=0; i<N; i++) {
2958 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002959 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002960 mBgAllAppsList.updatePackage(context, 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;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002964 case OP_REMOVE: {
2965 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2966 if (heuristic != null) {
2967 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002968 }
Joe Onorato36115782010-06-17 13:28:48 -04002969 for (int i=0; i<N; i++) {
2970 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002971 mIconCache.removeIconsForPkg(packages[i], mUser);
2972 }
2973 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002974 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002975 case OP_UNAVAILABLE:
2976 for (int i=0; i<N; i++) {
2977 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2978 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002979 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002980 }
2981 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002982 case OP_SUSPEND:
2983 case OP_UNSUSPEND:
2984 boolean suspend = mOp == OP_SUSPEND;
2985 for (int i=0; i<N; i++) {
2986 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.suspendPackage "
2987 + suspend + " " + packages[i]);
2988 mBgAllAppsList.suspendPackage(packages[i], mUser, suspend);
2989 }
2990 break;
Joe Onorato36115782010-06-17 13:28:48 -04002991 }
2992
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002993 ArrayList<AppInfo> added = null;
2994 ArrayList<AppInfo> modified = null;
2995 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002996
Adam Cohen487f7dd2012-06-28 18:12:10 -07002997 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002998 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002999 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003000 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003001 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003002 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003003 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003004 }
Winson Chung5d55f332012-07-16 20:45:03 -07003005 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003006 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003007 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003008 }
3009
Sunny Goyale0f58d72014-11-10 18:05:31 -08003010 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003011 if (callbacks == null) {
3012 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3013 return;
3014 }
3015
Sunny Goyal4390ace2014-10-13 11:33:11 -07003016 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3017 new HashMap<ComponentName, AppInfo>();
3018
Joe Onorato36115782010-06-17 13:28:48 -04003019 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003020 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003021 for (AppInfo ai : added) {
3022 addedOrUpdatedApps.put(ai.componentName, ai);
3023 }
Joe Onorato36115782010-06-17 13:28:48 -04003024 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003025
Joe Onorato36115782010-06-17 13:28:48 -04003026 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003027 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003028 for (AppInfo ai : modified) {
3029 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003030 }
3031
Joe Onorato36115782010-06-17 13:28:48 -04003032 mHandler.post(new Runnable() {
3033 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003034 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003035 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003036 callbacks.bindAppsUpdated(modifiedFinal);
3037 }
3038 }
3039 });
3040 }
Winson Chung83892cc2013-05-01 16:53:33 -07003041
Sunny Goyal4390ace2014-10-13 11:33:11 -07003042 // Update shortcut infos
Kenny Guy44cba692016-01-21 19:50:02 +00003043 if (mOp == OP_ADD || mOp == OP_UPDATE || mOp == OP_UNSUSPEND) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003044 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3045 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3046 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3047
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003048 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003049 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003050 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003051 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3052 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003053 boolean infoUpdated = false;
3054 boolean shortcutUpdated = false;
3055
Kenny Guy44cba692016-01-21 19:50:02 +00003056 if (mOp == OP_UNSUSPEND) {
3057 si.isDisabled &= ~ ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3058 infoUpdated = true;
3059 }
3060
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003062 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003063 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003064 Bitmap icon = Utilities.createIconBitmap(
3065 si.iconResource.packageName,
3066 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003067 if (icon != null) {
3068 si.setIcon(icon);
3069 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003070 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003071 }
3072 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003073
3074 ComponentName cn = si.getTargetComponent();
3075 if (cn != null && packageSet.contains(cn.getPackageName())) {
3076 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3077
3078 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3080 // Auto install icon
3081 PackageManager pm = context.getPackageManager();
3082 ResolveInfo matched = pm.resolveActivity(
3083 new Intent(Intent.ACTION_MAIN)
3084 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3085 PackageManager.MATCH_DEFAULT_ONLY);
3086 if (matched == null) {
3087 // Try to find the best match activity.
3088 Intent intent = pm.getLaunchIntentForPackage(
3089 cn.getPackageName());
3090 if (intent != null) {
3091 cn = intent.getComponent();
3092 appInfo = addedOrUpdatedApps.get(cn);
3093 }
3094
3095 if ((intent == null) || (appInfo == null)) {
3096 removedShortcuts.add(si);
3097 continue;
3098 }
3099 si.promisedIntent = intent;
3100 }
3101 }
3102
3103 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003104 if (appInfo != null) {
3105 si.flags = appInfo.flags;
3106 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003107
Sunny Goyal756adbc2015-04-16 15:20:51 -07003108 si.intent = si.promisedIntent;
3109 si.promisedIntent = null;
3110 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003111 infoUpdated = true;
3112 si.updateIcon(mIconCache);
3113 }
3114
3115 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3116 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3117 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003118 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003119 si.contentDescription = appInfo.contentDescription;
3120 infoUpdated = true;
3121 }
3122
3123 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3124 // Since package was just updated, the target must be available now.
3125 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3126 shortcutUpdated = true;
3127 }
3128 }
3129
3130 if (infoUpdated || shortcutUpdated) {
3131 updatedShortcuts.add(si);
3132 }
3133 if (infoUpdated) {
3134 updateItemInDatabase(context, si);
3135 }
3136 } else if (info instanceof LauncherAppWidgetInfo) {
3137 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3138 if (mUser.equals(widgetInfo.user)
3139 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3140 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003141 widgetInfo.restoreStatus &=
3142 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3143 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003144
3145 // adding this flag ensures that launcher shows 'click to setup'
3146 // if the widget has a config activity. In case there is no config
3147 // activity, it will be marked as 'restored' during bind.
3148 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3149
Sunny Goyal4390ace2014-10-13 11:33:11 -07003150 widgets.add(widgetInfo);
3151 updateItemInDatabase(context, widgetInfo);
3152 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003153 }
3154 }
3155 }
3156
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3158 mHandler.post(new Runnable() {
3159
3160 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003161 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003162 if (callbacks == cb && cb != null) {
3163 callbacks.bindShortcutsChanged(
3164 updatedShortcuts, removedShortcuts, mUser);
3165 }
3166 }
3167 });
3168 if (!removedShortcuts.isEmpty()) {
3169 deleteItemsFromDatabase(context, removedShortcuts);
3170 }
3171 }
3172 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003173 mHandler.post(new Runnable() {
3174 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003175 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003176 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003178 }
3179 }
3180 });
3181 }
3182 }
3183
Winson Chungdf95eb12013-10-16 14:57:07 -07003184 final ArrayList<String> removedPackageNames =
3185 new ArrayList<String>();
Kenny Guy44cba692016-01-21 19:50:02 +00003186 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE || mOp == OP_SUSPEND) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003187 // Mark all packages in the broadcast to be removed
3188 removedPackageNames.addAll(Arrays.asList(packages));
3189 } else if (mOp == OP_UPDATE) {
3190 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003191 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003192 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003193 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003194 }
3195 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003196 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003197
Winson Chungdf95eb12013-10-16 14:57:07 -07003198 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003199 final int removeReason;
3200 if (mOp == OP_UNAVAILABLE) {
3201 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
Kenny Guy44cba692016-01-21 19:50:02 +00003202 } else if (mOp == OP_SUSPEND) {
3203 removeReason = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
Sunny Goyal1a745e82014-10-02 15:58:31 -07003204 } else {
3205 // Remove all the components associated with this package
3206 for (String pn : removedPackageNames) {
3207 deletePackageFromDatabase(context, pn, mUser);
3208 }
3209 // Remove all the specific components
3210 for (AppInfo a : removedApps) {
3211 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3212 deleteItemsFromDatabase(context, infos);
3213 }
3214 removeReason = 0;
3215 }
3216
Winson Chungdf95eb12013-10-16 14:57:07 -07003217 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003218 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003219 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003220 mHandler.post(new Runnable() {
3221 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003222 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003223 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003224 callbacks.bindComponentsRemoved(
3225 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003226 }
3227 }
3228 });
Joe Onoratobe386092009-11-17 17:32:16 -08003229 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003230
Sunny Goyal2d648b02015-08-11 13:56:28 -07003231 // Update widgets
3232 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3233 // Always refresh for a package event on secondary user
3234 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3235
3236 // Refresh widget list, if the package already had a widget.
3237 synchronized (sBgLock) {
3238 if (sBgWidgetProviders != null) {
3239 HashSet<String> pkgSet = new HashSet<>();
3240 Collections.addAll(pkgSet, mPackages);
3241
3242 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3243 needToRefresh |= key.user.equals(mUser) &&
3244 pkgSet.contains(key.componentName.getPackageName());
3245 }
3246 }
3247 }
3248
3249 if (!needToRefresh && mOp != OP_REMOVE) {
3250 // Refresh widget list, if there is any newly added widget
3251 PackageManager pm = context.getPackageManager();
3252 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003253 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003254 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003255 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003256 .setPackage(pkg), 0);
3257 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003258 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003259 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003260 throw e;
3261 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003262 // Ignore the crash. We can live with a state widget list.
3263 Log.e(TAG, "PM call failed for " + pkg, e);
3264 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003265 }
3266 }
3267
3268 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3269 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003270 }
3271 }
3272
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003273 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3274 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003275 ArrayList<LauncherAppWidgetProviderInfo> results =
3276 new ArrayList<LauncherAppWidgetProviderInfo>();
3277 try {
3278 synchronized (sBgLock) {
3279 if (sBgWidgetProviders == null || refresh) {
3280 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3281 = new HashMap<>();
3282 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3283 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003284
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003285 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3286 for (AppWidgetProviderInfo pInfo : widgets) {
3287 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3288 UserHandleCompat user = wm.getUser(info);
3289 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3290 }
Robin Lee26ace122015-03-16 19:41:43 +00003291
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003292 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3293 for (CustomAppWidget widget : customWidgets) {
3294 info = new LauncherAppWidgetProviderInfo(context, widget);
3295 UserHandleCompat user = wm.getUser(info);
3296 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3297 }
3298 // Replace the global list at the very end, so that if there is an exception,
3299 // previously loaded provider list is used.
3300 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003301 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003302 results.addAll(sBgWidgetProviders.values());
3303 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003304 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003305 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003306 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003307 (e.getCause() instanceof TransactionTooLargeException ||
3308 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003309 // the returned value may be incomplete and will not be refreshed until the next
3310 // time Launcher starts.
3311 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3312 // onResume is called to refresh the widget provider list.
3313 synchronized (sBgLock) {
3314 if (sBgWidgetProviders != null) {
3315 results.addAll(sBgWidgetProviders.values());
3316 }
3317 return results;
3318 }
3319 } else {
3320 throw e;
3321 }
Adam Cohen59400422014-03-05 18:07:04 -08003322 }
3323 }
3324
Robin Lee26ace122015-03-16 19:41:43 +00003325 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3326 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003327 synchronized (sBgLock) {
3328 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003329 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003330 }
Robin Lee26ace122015-03-16 19:41:43 +00003331 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003332 }
3333 }
3334
Sunny Goyal2d648b02015-08-11 13:56:28 -07003335 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003336
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003337 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003338 @Override
3339 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003340 updateWidgetsModel(refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003341 mHandler.post(new Runnable() {
3342 @Override
3343 public void run() {
3344 Callbacks cb = getCallback();
3345 if (callbacks == cb && cb != null) {
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003346 callbacks.bindAllPackages(mFgWidgetsModel);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003347 }
3348 }
3349 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003350 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003351 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003352 mFgWidgetsModel.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003353 }
3354 });
3355 }
3356
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003357 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003358 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003359 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003360 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003361 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003362 @Thunk void updateWidgetsModel(boolean refresh) {
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003363 Utilities.assertWorkerThread();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003364 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003365 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003366 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003367
3368 // Update shortcut providers
3369 synchronized (sBgLock) {
3370 try {
3371 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3372 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3373 sBgShortcutProviders = providers;
3374 } catch (RuntimeException e) {
Sunny Goyal877f9282015-09-18 10:18:18 -07003375 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal31860582015-09-18 08:38:57 -07003376 (e.getCause() instanceof TransactionTooLargeException ||
3377 e.getCause() instanceof DeadObjectException)) {
3378 /**
3379 * Ignore exception and use the cached list if available.
3380 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3381 */
3382 } else {
3383 throw e;
3384 }
3385 }
3386 if (sBgShortcutProviders != null) {
3387 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3388 }
3389 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003390 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003391 mFgWidgetsModel = mBgWidgetsModel.clone();
Michael Jurkac402cd92013-05-20 15:49:32 +02003392 }
3393
Adam Cohen091440a2015-03-18 14:16:05 -07003394 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003395 UserHandleCompat user) {
3396 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3397 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003398 }
Adam Cohen556f6132014-01-15 15:18:08 -08003399
Kenny Guyed131872014-04-30 03:02:21 +01003400 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3401 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003402 if (cn == null) {
3403 return false;
3404 }
Kenny Guyed131872014-04-30 03:02:21 +01003405 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3406 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003407 return false;
3408 }
Kenny Guyed131872014-04-30 03:02:21 +01003409 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003410 }
3411
Adam Cohena28b78e2014-05-20 17:03:04 -07003412 public static boolean isValidPackage(Context context, String packageName,
3413 UserHandleCompat user) {
3414 if (packageName == null) {
3415 return false;
3416 }
3417 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3418 return launcherApps.isPackageEnabledForProfile(packageName, user);
3419 }
3420
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003422 * Make an ShortcutInfo object for a restored application or shortcut item that points
3423 * to a package that is not yet installed on the system.
3424 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003425 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003426 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003427 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003428 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003429
3430 Bitmap icon = iconInfo.loadIcon(c, info, context);
3431 // the fallback icon
3432 if (icon == null) {
3433 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3434 } else {
3435 info.setIcon(icon);
3436 }
Sunny Goyal34942622014-08-29 17:20:55 -07003437
3438 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003439 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003440 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003441 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003442 }
Sunny Goyal34942622014-08-29 17:20:55 -07003443 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3444 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003445 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003446 }
Sunny Goyal34942622014-08-29 17:20:55 -07003447 } else {
3448 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3449 }
3450
Winson Chung82b016c2015-05-08 17:00:10 -07003451 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003452 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003453 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003454 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003455 return info;
3456 }
3457
3458 /**
3459 * Make an Intent object for a restored application or shortcut item that points
3460 * to the market page for the item.
3461 */
Adam Cohen091440a2015-03-18 14:16:05 -07003462 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003463 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003464 return getMarketIntent(componentName.getPackageName());
3465 }
3466
3467 static Intent getMarketIntent(String packageName) {
3468 return new Intent(Intent.ACTION_VIEW)
3469 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003470 .scheme("market")
3471 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003472 .appendQueryParameter("id", packageName)
3473 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003474 }
3475
3476 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003477 * Make an ShortcutInfo object for a shortcut that is an application.
3478 *
3479 * If c is not null, then it will be used to fill in missing data like the title and icon.
3480 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003481 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003482 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003483 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003484 if (user == null) {
3485 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003486 return null;
3487 }
3488
Kenny Guyed131872014-04-30 03:02:21 +01003489 ComponentName componentName = intent.getComponent();
3490 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003491 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003492 return null;
3493 }
3494
3495 Intent newIntent = new Intent(intent.getAction(), null);
3496 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3497 newIntent.setComponent(componentName);
3498 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003499 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003500 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3501 return null;
3502 }
3503
3504 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003505 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003506 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3507 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3508 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003509 }
3510
Joe Onorato56d82912010-03-07 14:32:10 -05003511 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003512 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003513 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003514 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003515
Joe Onorato56d82912010-03-07 14:32:10 -05003516 // fall back to the class name of the activity
3517 if (info.title == null) {
3518 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003519 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003520
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003522 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003523 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003524 if (lai != null) {
3525 info.flags = AppInfo.initFlags(lai);
3526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003529
Sunny Goyale2df0622015-04-24 11:27:00 -07003530 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003531 ItemInfoFilter f) {
3532 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3533 for (ItemInfo i : infos) {
3534 if (i instanceof ShortcutInfo) {
3535 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003536 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003537 if (cn != null && f.filterItem(null, info, cn)) {
3538 filtered.add(info);
3539 }
3540 } else if (i instanceof FolderInfo) {
3541 FolderInfo info = (FolderInfo) i;
3542 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003543 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003544 if (cn != null && f.filterItem(info, s, cn)) {
3545 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003546 }
3547 }
Winson Chung64359a52013-07-08 17:17:08 -07003548 } else if (i instanceof LauncherAppWidgetInfo) {
3549 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3550 ComponentName cn = info.providerName;
3551 if (cn != null && f.filterItem(null, info, cn)) {
3552 filtered.add(info);
3553 }
Winson Chung8a435102012-08-30 17:16:53 -07003554 }
3555 }
Winson Chung64359a52013-07-08 17:17:08 -07003556 return new ArrayList<ItemInfo>(filtered);
3557 }
3558
Adam Cohen091440a2015-03-18 14:16:05 -07003559 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003560 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003561 ItemInfoFilter filter = new ItemInfoFilter() {
3562 @Override
3563 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003564 if (info.user == null) {
3565 return cn.equals(cname);
3566 } else {
3567 return cn.equals(cname) && info.user.equals(user);
3568 }
Winson Chung64359a52013-07-08 17:17:08 -07003569 }
3570 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003571 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003572 }
3573
Sunny Goyal1a745e82014-10-02 15:58:31 -07003574 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003575 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003576 */
Adam Cohen091440a2015-03-18 14:16:05 -07003577 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003578 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003579 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003580 // Non-app shortcuts are only supported for current user.
3581 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003583
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003584 // TODO: If there's an explicit component and we can't install that, delete it.
3585
Winson Chung82b016c2015-05-08 17:00:10 -07003586 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003587
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003588 Bitmap icon = iconInfo.loadIcon(c, info, context);
3589 // the fallback icon
3590 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003591 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003592 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003593 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003594 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 return info;
3596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003597
Sunny Goyal2350bc92014-10-14 16:42:54 -07003598 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003599 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3600 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3601 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3602
Adam Cohend9198822011-11-22 16:42:47 -08003603 if (intent == null) {
3604 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3605 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3606 return null;
3607 }
3608
Joe Onorato0589f0f2010-02-08 13:44:00 -08003609 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003610 boolean customIcon = false;
3611 ShortcutIconResource iconResource = null;
3612
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003613 if (bitmap instanceof Bitmap) {
3614 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003615 customIcon = true;
3616 } else {
3617 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003618 if (extra instanceof ShortcutIconResource) {
3619 iconResource = (ShortcutIconResource) extra;
3620 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003621 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003622 }
3623 }
3624
Michael Jurkac9d95c52011-08-29 14:03:34 -07003625 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003626
Kenny Guyed131872014-04-30 03:02:21 +01003627 // Only support intents for current user for now. Intents sent from other
3628 // users wouldn't get here without intent forwarding anyway.
3629 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003630 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003631 icon = mIconCache.getDefaultIcon(info.user);
3632 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003633 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003634 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003635
Winson Chung82b016c2015-05-08 17:00:10 -07003636 info.title = Utilities.trim(name);
3637 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003638 info.intent = intent;
3639 info.customIcon = customIcon;
3640 info.iconResource = iconResource;
3641
3642 return info;
3643 }
3644
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003646 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 * or make a new one.
3648 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003649 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 // See if a placeholder was created for us already
3651 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003652 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003654 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 folders.put(id, folderInfo);
3656 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003657 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003658 }
3659
Joe Onoratobe386092009-11-17 17:32:16 -08003660
Sunny Goyal651077b2014-06-30 14:15:31 -07003661 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3662 return (provider != null) && (provider.provider != null)
3663 && (provider.provider.getPackageName() != null);
3664 }
3665
Joe Onoratobe386092009-11-17 17:32:16 -08003666 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003667 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003668 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3669 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3670 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3671 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003672 if (mLoaderTask != null) {
3673 mLoaderTask.dumpState();
3674 } else {
3675 Log.d(TAG, "mLoaderTask=null");
3676 }
Joe Onoratobe386092009-11-17 17:32:16 -08003677 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003678
3679 public Callbacks getCallback() {
3680 return mCallbacks != null ? mCallbacks.get() : null;
3681 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003682
3683 /**
3684 * @return {@link FolderInfo} if its already loaded.
3685 */
3686 public FolderInfo findFolderById(Long folderId) {
3687 synchronized (sBgLock) {
3688 return sBgFolders.get(folderId);
3689 }
3690 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003691
Sunny Goyal527c7d32015-08-28 15:19:36 -07003692 @Thunk class DeferredMainThreadExecutor implements Executor {
3693
3694 @Override
3695 public void execute(Runnable command) {
3696 runOnMainThread(command);
3697 }
3698 }
3699
Sunny Goyal756adbc2015-04-16 15:20:51 -07003700 /**
3701 * @return the looper for the worker thread which can be used to start background tasks.
3702 */
3703 public static Looper getWorkerLooper() {
3704 return sWorkerThread.getLooper();
3705 }
Sunny Goyala1365452015-10-01 15:46:24 -07003706
3707 @Thunk static final void addDumpLog(String log) {
3708 Launcher.addDumpLog(TAG, log);
3709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003710}