blob: f707ec50187fdec1dbe0db64dbcf038eb2aa0ab6 [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 Goyal26119432016-02-18 22:09:23 +000060import com.android.launcher3.folder.Folder;
61import com.android.launcher3.folder.FolderIcon;
Sunny Goyalf862a262015-12-14 14:27:38 -080062import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070063import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000064import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070065import com.android.launcher3.util.CursorIconInfo;
Sunny Goyale2df0622015-04-24 11:27:00 -070066import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070067import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070068import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070069import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080070
Michael Jurkac2f801e2011-07-12 14:19:46 -070071import java.lang.ref.WeakReference;
72import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070073import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070075import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070076import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070077import java.util.Collections;
78import java.util.Comparator;
79import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070080import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070081import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070082import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070083import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070084import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070085import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087/**
88 * Maintains in-memory state of the Launcher. It is expected that there should be only one
89 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070090 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 */
Kenny Guyed131872014-04-30 03:02:21 +010092public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010093 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080094 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040095 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070096 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040097
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070099
Dan Sandlerd5024042014-01-09 15:01:33 -0500100 public static final int LOADER_FLAG_NONE = 0;
101 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
102 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
103
Joe Onorato36115782010-06-17 13:28:48 -0400104 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500105 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800106
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800108 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400109
Adam Cohen091440a2015-03-18 14:16:05 -0700110 @Thunk final LauncherAppState mApp;
111 @Thunk final Object mLock = new Object();
112 @Thunk DeferredHandler mHandler = new DeferredHandler();
113 @Thunk LoaderTask mLoaderTask;
114 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700115 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Jason Monkbbe1e242014-05-16 17:37:34 -0400117 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 // We start off with everything not loaded. After that, we assume that
126 // our monitoring of the package manager provides all updates and we never
127 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk boolean mWorkspaceLoaded;
129 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700140 AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
142 WidgetsModel mBgWidgetsModel;
Hyunyoung Song747a5bc2016-02-08 11:31:33 -0800143 // Keep a clone of widgets that can be accessed from non-worker thread.
144 WidgetsModel mFgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700146 // The lock that must be acquired before referencing any static bg data structures. Unlike
147 // other locks, this one can generally be held long-term because we never expect any of these
148 // static data structures to be referenced outside of the worker thread except on the first
149 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700150 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700154 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
157 // created by LauncherModel that are directly on the home screen (however, no widgets or
158 // shortcuts within folders).
159 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160
Adam Cohen487f7dd2012-06-28 18:12:10 -0700161 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
162 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163 new ArrayList<LauncherAppWidgetInfo>();
164
Adam Cohen487f7dd2012-06-28 18:12:10 -0700165 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700166 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700167
Adam Cohendcd297f2013-06-18 13:13:40 -0700168 // sBgWorkspaceScreens is the ordered set of workspace screens.
169 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
170
Adam Cohen59400422014-03-05 18:07:04 -0800171 // sBgWidgetProviders is the set of widget providers including custom internal widgets
Robin Lee26ace122015-03-16 19:41:43 +0000172 public static HashMap<ComponentKey, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
Adam Cohen59400422014-03-05 18:07:04 -0800173
Sunny Goyal31860582015-09-18 08:38:57 -0700174 // sBgShortcutProviders is the set of custom shortcut providers
175 public static List<ResolveInfo> sBgShortcutProviders;
176
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700177 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700178 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
179 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700180
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700181 // </ only access in worker thread >
182
Adam Cohen091440a2015-03-18 14:16:05 -0700183 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Adam Cohen091440a2015-03-18 14:16:05 -0700185 @Thunk final LauncherAppsCompat mLauncherApps;
186 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700189 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700191 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700193 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
194 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700195 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700196 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyale2df0622015-04-24 11:27:00 -0700197 public void bindFolders(LongArrayMap<FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800198 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700201 public void bindAppsAdded(ArrayList<Long> newScreens,
202 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700203 ArrayList<ItemInfo> addAnimated,
204 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200205 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700206 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
207 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
208 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700209 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Winson Chung83892cc2013-05-01 16:53:33 -0700210 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700211 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700212 public void bindAllPackages(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700213 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700214 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700215 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700216 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chung64359a52013-07-08 17:17:08 -0700219 public interface ItemInfoFilter {
220 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
221 }
222
Bjorn Bringert1307f632013-10-03 22:31:03 +0100223 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800224 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400225
Winson Chungee055712013-07-30 14:46:24 -0700226 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700227 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400228 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
229 // resource string.
230 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
231 ProviderInfo providerInfo =
232 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
233 ProviderInfo redirectProvider =
234 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700235
236 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400237 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700238
239 if (mOldContentProviderExists) {
240 Log.d(TAG, "Old launcher provider exists.");
241 } else {
242 Log.d(TAG, "Old launcher provider does not exist.");
243 }
244
Daniel Sandlere4f98912013-06-25 15:13:26 -0400245 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100246 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700247 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Hyunyoung Song747a5bc2016-02-08 11:31:33 -0800248 mFgWidgetsModel = mBgWidgetsModel.clone();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 mIconCache = iconCache;
250
Kenny Guyed131872014-04-30 03:02:21 +0100251 mLauncherApps = LauncherAppsCompat.getInstance(context);
252 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 }
254
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700255 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
256 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700257 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700258 if (sWorkerThread.getThreadId() == Process.myTid()) {
259 // If we are on the worker thread, post onto the main handler
260 mHandler.post(r);
261 } else {
262 r.run();
263 }
264 }
265
266 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
267 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700268 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700269 if (sWorkerThread.getThreadId() == Process.myTid()) {
270 r.run();
271 } else {
272 // If we are not on the worker thread, then post to the worker handler
273 sWorker.post(r);
274 }
275 }
276
Winson Chunge43a1e72014-01-15 10:33:02 -0800277 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
278 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800279 }
280
Sunny Goyal756adbc2015-04-16 15:20:51 -0700281 public void setPackageState(final PackageInstallInfo installInfo) {
282 Runnable updateRunnable = new Runnable() {
283
284 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500285 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700286 synchronized (sBgLock) {
287 final HashSet<ItemInfo> updates = new HashSet<>();
288
289 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
290 // Ignore install success events as they are handled by Package add events.
291 return;
292 }
293
Sunny Goyale2df0622015-04-24 11:27:00 -0700294 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700295 if (info instanceof ShortcutInfo) {
296 ShortcutInfo si = (ShortcutInfo) info;
297 ComponentName cn = si.getTargetComponent();
298 if (si.isPromise() && (cn != null)
299 && installInfo.packageName.equals(cn.getPackageName())) {
300 si.setInstallProgress(installInfo.progress);
301
302 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
303 // Mark this info as broken.
304 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
305 }
306 updates.add(si);
307 }
308 }
309 }
310
311 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
312 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
313 widget.installProgress = installInfo.progress;
314 updates.add(widget);
315 }
316 }
317
318 if (!updates.isEmpty()) {
319 // Push changes to the callback.
320 Runnable r = new Runnable() {
321 public void run() {
322 Callbacks callbacks = getCallback();
323 if (callbacks != null) {
324 callbacks.bindRestoreItemsChange(updates);
325 }
326 }
327 };
328 mHandler.post(r);
329 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500330 }
331 }
332 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700333 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500334 }
335
Sunny Goyal756adbc2015-04-16 15:20:51 -0700336 /**
337 * Updates the icons and label of all pending icons for the provided package name.
338 */
339 public void updateSessionDisplayInfo(final String packageName) {
340 Runnable updateRunnable = new Runnable() {
341
342 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700343 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 synchronized (sBgLock) {
345 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
346 final UserHandleCompat user = UserHandleCompat.myUserHandle();
347
Sunny Goyale2df0622015-04-24 11:27:00 -0700348 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700349 if (info instanceof ShortcutInfo) {
350 ShortcutInfo si = (ShortcutInfo) info;
351 ComponentName cn = si.getTargetComponent();
352 if (si.isPromise() && (cn != null)
353 && packageName.equals(cn.getPackageName())) {
354 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
355 // For auto install apps update the icon as well as label.
356 mIconCache.getTitleAndIcon(si,
357 si.promisedIntent, user,
358 si.shouldUseLowResIcon());
359 } else {
360 // Only update the icon for restored apps.
361 si.updateIcon(mIconCache);
362 }
363 updates.add(si);
364 }
365 }
366 }
367
368 if (!updates.isEmpty()) {
369 // Push changes to the callback.
370 Runnable r = new Runnable() {
371 public void run() {
372 Callbacks callbacks = getCallback();
373 if (callbacks != null) {
374 callbacks.bindShortcutsChanged(updates,
375 new ArrayList<ShortcutInfo>(), user);
376 }
377 }
378 };
379 mHandler.post(r);
380 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700381 }
382 }
383 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700384 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700385 }
386
Adam Cohen76a47a12014-02-05 11:47:43 -0800387 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800388 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800389
390 if (allAppsApps == null) {
391 throw new RuntimeException("allAppsApps must not be null");
392 }
393 if (allAppsApps.isEmpty()) {
394 return;
395 }
396
397 // Process the newly added applications and add them to the database first
398 Runnable r = new Runnable() {
399 public void run() {
400 runOnMainThread(new Runnable() {
401 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800402 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800403 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500404 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800405 }
406 }
407 });
408 }
409 };
410 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700411 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800412
Sunny Goyala9116722015-04-29 13:55:58 -0700413 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 int[] xy, int spanX, int spanY) {
415 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700416 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
417 final int xCount = (int) profile.numColumns;
418 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419 boolean[][] occupied = new boolean[xCount][yCount];
420 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700421 for (ItemInfo r : occupiedPos) {
422 int right = r.cellX + r.spanX;
423 int bottom = r.cellY + r.spanY;
424 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
425 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426 occupied[x][y] = true;
427 }
428 }
429 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800430 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700431 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 }
433
434 /**
435 * Find a position on the screen for the given size or adds a new screen.
436 * @return screenId and the coordinates for the item.
437 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700438 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700439 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440 ArrayList<Long> workspaceScreens,
441 ArrayList<Long> addedWorkspaceScreensFinal,
442 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700443 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444
Sunny Goyala9116722015-04-29 13:55:58 -0700445 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700446 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700447 synchronized (sBgLock) {
448 for (ItemInfo info : sBgItemsIdMap) {
449 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
450 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
451 if (items == null) {
452 items = new ArrayList<>();
453 screenItems.put(info.screenId, items);
454 }
455 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800456 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800457 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800458 }
459
460 // Find appropriate space for the item.
461 long screenId = 0;
462 int[] cordinates = new int[2];
463 boolean found = false;
464
465 int screenCount = workspaceScreens.size();
466 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700467 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 if (preferredScreenIndex < screenCount) {
469 screenId = workspaceScreens.get(preferredScreenIndex);
470 found = findNextAvailableIconSpaceInScreen(
471 screenItems.get(screenId), cordinates, spanX, spanY);
472 }
473
474 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700475 // Search on any of the screens starting from the first screen.
476 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800477 screenId = workspaceScreens.get(screen);
478 if (findNextAvailableIconSpaceInScreen(
479 screenItems.get(screenId), cordinates, spanX, spanY)) {
480 // We found a space for it
481 found = true;
482 break;
483 }
484 }
485 }
486
487 if (!found) {
488 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700489 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
490 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
491 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800492
493 // Save the screen id for binding in the workspace
494 workspaceScreens.add(screenId);
495 addedWorkspaceScreensFinal.add(screenId);
496
497 // If we still can't find an empty space, then God help us all!!!
498 if (!findNextAvailableIconSpaceInScreen(
499 screenItems.get(screenId), cordinates, spanX, spanY)) {
500 throw new RuntimeException("Can't find space to add the item");
501 }
502 }
503 return Pair.create(screenId, cordinates);
504 }
505
506 /**
507 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800508 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700509 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700510 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800511 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800512 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700513 return;
Winson Chung997a9232013-07-24 15:33:46 -0700514 }
Winson Chung64359a52013-07-08 17:17:08 -0700515 // Process the newly added applications and add them to the database first
516 Runnable r = new Runnable() {
517 public void run() {
518 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
519 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
520
Winson Chung76828c82013-08-19 15:43:29 -0700521 // Get the list of workspace screens. We need to append to this list and
522 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
523 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800524 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700525 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800526 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700527 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800528 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700529 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800530 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700531 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800532 }
Winson Chung76828c82013-08-19 15:43:29 -0700533
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800534 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700535 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700536 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800537 long screenId = coords.first;
538 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700539
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700540 ItemInfo itemInfo;
541 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
542 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800543 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700544 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700545 } else {
546 throw new RuntimeException("Unexpected info type");
547 }
Winson Chung94d67682013-09-25 16:29:40 -0700548
Winson Chung64359a52013-07-08 17:17:08 -0700549 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700550 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700551 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700552 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700553 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700554 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700555 }
556 }
557
Winson Chung76828c82013-08-19 15:43:29 -0700558 // Update the workspace screens
559 updateWorkspaceScreenOrder(context, workspaceScreens);
560
Adam Cohen76a47a12014-02-05 11:47:43 -0800561 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700562 runOnMainThread(new Runnable() {
563 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800564 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700565 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700566 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
567 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700568 if (!addedShortcutsFinal.isEmpty()) {
569 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
570 long lastScreenId = info.screenId;
571 for (ItemInfo i : addedShortcutsFinal) {
572 if (i.screenId == lastScreenId) {
573 addAnimated.add(i);
574 } else {
575 addNotAnimated.add(i);
576 }
Winson Chung997a9232013-07-24 15:33:46 -0700577 }
578 }
Winson Chungd64d1762013-08-20 14:37:16 -0700579 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800580 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700581 }
Winson Chung64359a52013-07-08 17:17:08 -0700582 }
Winson Chung997a9232013-07-24 15:33:46 -0700583 });
584 }
Winson Chung64359a52013-07-08 17:17:08 -0700585 }
586 };
587 runOnWorkerThread(r);
588 }
589
Sunny Goyald33860f2015-04-23 16:02:20 -0700590 private void unbindItemInfosAndClearQueuedBindRunnables() {
Winson Chung81b52252012-08-27 15:34:29 -0700591 if (sWorkerThread.getThreadId() == Process.myTid()) {
592 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
593 "main thread");
594 }
595
Sunny Goyald33860f2015-04-23 16:02:20 -0700596 // Remove any queued UI runnables
597 mHandler.cancelAll();
Winson Chung81b52252012-08-27 15:34:29 -0700598 // Unbind all the workspace items
599 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700600 }
601
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700602 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700603 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700604 // Ensure that we don't use the same workspace items data structure on the main thread
605 // by making a copy of workspace items first.
Sunny Goyald33860f2015-04-23 16:02:20 -0700606 final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700607 synchronized (sBgLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -0700608 tmpItems.addAll(sBgWorkspaceItems);
609 tmpItems.addAll(sBgAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700610 }
611 Runnable r = new Runnable() {
612 @Override
613 public void run() {
Sunny Goyald33860f2015-04-23 16:02:20 -0700614 for (ItemInfo item : tmpItems) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700615 item.unbind();
616 }
617 }
618 };
619 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700620 }
621
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622 /**
623 * Adds an item to the DB if it was not created previously, or move it to a new
624 * <container, screen, cellX, cellY>
625 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800626 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700627 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 if (item.container == ItemInfo.NO_ID) {
629 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700630 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 } else {
632 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
635 }
636
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700637 static void checkItemInfoLocked(
638 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
639 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
640 if (modelItem != null && item != modelItem) {
641 // check all the data is consistent
642 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
643 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
644 ShortcutInfo shortcut = (ShortcutInfo) item;
645 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
646 modelShortcut.intent.filterEquals(shortcut.intent) &&
647 modelShortcut.id == shortcut.id &&
648 modelShortcut.itemType == shortcut.itemType &&
649 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700650 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700651 modelShortcut.cellX == shortcut.cellX &&
652 modelShortcut.cellY == shortcut.cellY &&
653 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700654 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700655 // For all intents and purposes, this is the same object
656 return;
657 }
658 }
659
660 // the modelItem needs to match up perfectly with item if our model is
661 // to be consistent with the database-- for now, just require
662 // modelItem == item or the equality check above
663 String msg = "item: " + ((item != null) ? item.toString() : "null") +
664 "modelItem: " +
665 ((modelItem != null) ? modelItem.toString() : "null") +
666 "Error: ItemInfo passed to checkItemInfo doesn't match original";
667 RuntimeException e = new RuntimeException(msg);
668 if (stackTrace != null) {
669 e.setStackTrace(stackTrace);
670 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800671 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700672 }
673 }
674
Michael Jurka816474f2012-06-25 14:49:02 -0700675 static void checkItemInfo(final ItemInfo item) {
676 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
677 final long itemId = item.id;
678 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700679 public void run() {
680 synchronized (sBgLock) {
681 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700682 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700683 }
684 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700685 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700686 }
687
Michael Jurkac9d95c52011-08-29 14:03:34 -0700688 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
689 final ItemInfo item, final String callingFunction) {
690 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700691 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700692 final ContentResolver cr = context.getContentResolver();
693
Adam Cohen487f7dd2012-06-28 18:12:10 -0700694 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700695 Runnable r = new Runnable() {
696 public void run() {
697 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 updateItemArrays(item, itemId, stackTrace);
699 }
700 };
701 runOnWorkerThread(r);
702 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700703
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700704 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
705 final ArrayList<ItemInfo> items, final String callingFunction) {
706 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700707
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700708 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
709 Runnable r = new Runnable() {
710 public void run() {
711 ArrayList<ContentProviderOperation> ops =
712 new ArrayList<ContentProviderOperation>();
713 int count = items.size();
714 for (int i = 0; i < count; i++) {
715 ItemInfo item = items.get(i);
716 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700717 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700719
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700720 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
721 updateItemArrays(item, itemId, stackTrace);
722
723 }
724 try {
725 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
726 } catch (Exception e) {
727 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700728 }
729 }
730 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700731 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700732 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700733
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700734 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
735 // Lock on mBgLock *after* the db operation
736 synchronized (sBgLock) {
737 checkItemInfoLocked(itemId, item, stackTrace);
738
739 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
740 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
741 // Item is in a folder, make sure this folder exists
742 if (!sBgFolders.containsKey(item.container)) {
743 // An items container is being set to a that of an item which is not in
744 // the list of Folders.
745 String msg = "item: " + item + " container being set to: " +
746 item.container + ", not in the list of folders";
747 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 }
749 }
750
751 // Items are added/removed from the corresponding FolderInfo elsewhere, such
752 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
753 // that are on the desktop, as appropriate
754 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800755 if (modelItem != null &&
756 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
757 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700758 switch (modelItem.itemType) {
759 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
760 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
761 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
762 if (!sBgWorkspaceItems.contains(modelItem)) {
763 sBgWorkspaceItems.add(modelItem);
764 }
765 break;
766 default:
767 break;
768 }
769 } else {
770 sBgWorkspaceItems.remove(modelItem);
771 }
772 }
773 }
774
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700777 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700778 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700779 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781 item.cellX = cellX;
782 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700783
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 // We store hotseat items in canonical form which is this orientation invariant position
785 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700791 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792
793 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700795 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
796 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800797 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799
Michael Jurkac9d95c52011-08-29 14:03:34 -0700800 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700801 }
802
803 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700804 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
805 * cellX, cellY have already been updated on the ItemInfos.
806 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800807 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700808 final long container, final int screen) {
809
810 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
811 int count = items.size();
812
813 for (int i = 0; i < count; i++) {
814 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700815 item.container = container;
816
817 // We store hotseat items in canonical form which is this orientation invariant position
818 // in the hotseat
819 if (context instanceof Launcher && screen < 0 &&
820 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700821 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700822 item.cellY);
823 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 }
826
827 final ContentValues values = new ContentValues();
828 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
829 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
830 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800831 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700832 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700833
834 contentValues.add(values);
835 }
836 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
837 }
838
839 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700840 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800841 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700842 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700843 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700844 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800845 item.cellX = cellX;
846 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700847 item.spanX = spanX;
848 item.spanY = spanY;
849
850 // We store hotseat items in canonical form which is this orientation invariant position
851 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700853 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700855 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700857 }
Adam Cohend4844c32011-02-18 19:25:06 -0800858
Adam Cohend4844c32011-02-18 19:25:06 -0800859 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800860 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700861 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
862 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800863 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700864 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
865 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800867
Michael Jurka816474f2012-06-25 14:49:02 -0700868 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700869 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700870
871 /**
872 * Update an item to the database in a specified container.
873 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700874 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700875 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100876 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700877 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800878 }
879
Sunny Goyal756a28a2015-04-23 17:07:55 -0700880 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700881 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700882 synchronized (mLock) {
883 if (!mHasLoaderCompletedOnce ||
884 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
885 throw new RuntimeException("Trying to add shortcut while loader is running");
886 }
887 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700888 }
889 }
890
Adam Cohend4844c32011-02-18 19:25:06 -0800891 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700892 * Returns true if the shortcuts already exists on the workspace. This must be called after
893 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700895 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
896 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700897 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700898 if (intent.getComponent() != null) {
899 // If component is not null, an intent with null package will produce
900 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700901 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700902 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700903 intentWithPkg = intent.toUri(0);
904 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700905 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700906 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
907 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700908 }
909 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700910 intentWithPkg = intent.toUri(0);
911 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700912 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700913
914 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700915 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700916 if (item instanceof ShortcutInfo) {
917 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700918 Intent targetIntent = info.promisedIntent == null
919 ? info.intent : info.promisedIntent;
920 if (targetIntent != null && info.user.equals(user)) {
921 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700922 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
923 return true;
924 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700925 }
926 }
927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700929 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700930 }
931
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 * Add an item to the database in a specified container. Sets the container, screen, cellX and
934 * cellY fields of the item. Also assigns an ID to the item.
935 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700936 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700937 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 item.cellX = cellX;
940 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 // We store hotseat items in canonical form which is this orientation invariant position
942 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949
950 final ContentValues values = new ContentValues();
951 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100952 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
Sunny Goyald2497482015-09-22 18:24:19 -0700954 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
955 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
956
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700957 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700958
Jason Monk8e19cf22014-03-20 15:06:57 -0400959 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700960 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700961 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700962 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700964 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700965 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400966 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700967 sBgItemsIdMap.put(item.id, item);
968 switch (item.itemType) {
969 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
970 sBgFolders.put(item.id, (FolderInfo) item);
971 // Fall through
972 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
973 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
974 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
975 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
976 sBgWorkspaceItems.add(item);
977 } else {
978 if (!sBgFolders.containsKey(item.container)) {
979 // Adding an item to a folder that doesn't exist.
980 String msg = "adding item: " + item + " to a folder that " +
981 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700982 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700984 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985 break;
986 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
987 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
988 break;
989 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700990 }
991 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700992 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700993 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700994 }
995
Sunny Goyal34942622014-08-29 17:20:55 -0700996 private static ArrayList<ItemInfo> getItemsByPackageName(
997 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700998 ItemInfoFilter filter = new ItemInfoFilter() {
999 @Override
1000 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1001 return cn.getPackageName().equals(pn) && info.user.equals(user);
1002 }
1003 };
Sunny Goyale2df0622015-04-24 11:27:00 -07001004 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -07001005 }
1006
1007 /**
1008 * Removes all the items from the database corresponding to the specified package.
1009 */
1010 static void deletePackageFromDatabase(Context context, final String pn,
1011 final UserHandleCompat user) {
1012 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001013 }
1014
1015 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001016 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 */
Sunny Goyalfa401a12015-04-10 13:45:42 -07001018 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001019 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1020 items.add(item);
1021 deleteItemsFromDatabase(context, items);
1022 }
1023
1024 /**
1025 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001026 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001027 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -07001029 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001030 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001031 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001032 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001033 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001035 // Lock on mBgLock *after* the db operation
1036 synchronized (sBgLock) {
1037 switch (item.itemType) {
1038 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1039 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -07001040 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001041 if (info.container == item.id) {
1042 // We are deleting a folder which still contains items that
1043 // think they are contained by that folder.
1044 String msg = "deleting a folder (" + item + ") which still " +
1045 "contains items (" + info + ")";
1046 Log.e(TAG, msg);
1047 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001049 sBgWorkspaceItems.remove(item);
1050 break;
1051 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1052 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1053 sBgWorkspaceItems.remove(item);
1054 break;
1055 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1056 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1057 break;
1058 }
1059 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001060 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001061 }
1062 }
Michael Jurka83df1882011-08-31 20:59:26 -07001063 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001064 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 }
1066
1067 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 * Update the order of the workspace screens in the database. The array list contains
1069 * a list of screen ids in the order that they should appear.
1070 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001071 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001072 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 final ContentResolver cr = context.getContentResolver();
1074 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1075
1076 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001077 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 while (iter.hasNext()) {
1079 long id = iter.next();
1080 if (id < 0) {
1081 iter.remove();
1082 }
1083 }
1084
1085 Runnable r = new Runnable() {
1086 @Override
1087 public void run() {
Yura085c8532014-02-11 15:15:29 +00001088 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001090 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001091 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001092 for (int i = 0; i < count; i++) {
1093 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001094 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1096 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001097 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 }
Yura085c8532014-02-11 15:15:29 +00001099
1100 try {
1101 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1102 } catch (Exception ex) {
1103 throw new RuntimeException(ex);
1104 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001105
Winson Chungba9c37f2013-08-30 14:11:37 -07001106 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001107 sBgWorkspaceScreens.clear();
1108 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001109 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001110 }
1111 };
1112 runOnWorkerThread(r);
1113 }
1114
1115 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001116 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 */
Winsonc0b52fe2015-09-09 16:38:15 -07001118 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 final ContentResolver cr = context.getContentResolver();
1120
Michael Jurkac9d95c52011-08-29 14:03:34 -07001121 Runnable r = new Runnable() {
1122 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001123 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001125 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 sBgItemsIdMap.remove(info.id);
1127 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 sBgWorkspaceItems.remove(info);
1129 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001130
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001131 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001132 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001133 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001134 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001135 for (ItemInfo childInfo : info.contents) {
1136 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001137 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001138 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001139 }
1140 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001141 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 }
1143
1144 /**
1145 * Set this as the current Launcher activity object for the loader.
1146 */
1147 public void initialize(Callbacks callbacks) {
1148 synchronized (mLock) {
Sunny Goyald33860f2015-04-23 16:02:20 -07001149 // Disconnect any of the callbacks and drawables associated with ItemInfos on the
1150 // workspace to prevent leaking Launcher activities on orientation change.
1151 unbindItemInfosAndClearQueuedBindRunnables();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 mCallbacks = new WeakReference<Callbacks>(callbacks);
1153 }
1154 }
1155
Kenny Guyed131872014-04-30 03:02:21 +01001156 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001157 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001158 int op = PackageUpdatedTask.OP_UPDATE;
1159 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1160 user));
1161 }
1162
1163 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001164 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001165 int op = PackageUpdatedTask.OP_REMOVE;
1166 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1167 user));
1168 }
1169
1170 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001171 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001172 int op = PackageUpdatedTask.OP_ADD;
1173 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1174 user));
1175 }
1176
1177 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001178 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001179 boolean replacing) {
1180 if (!replacing) {
1181 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1182 user));
1183 if (mAppsCanBeOnRemoveableStorage) {
1184 // Only rebind if we support removable storage. It catches the
1185 // case where
1186 // apps on the external sd card need to be reloaded
1187 startLoaderFromBackground();
1188 }
1189 } else {
1190 // If we are replacing then just update the packages in the list
1191 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1192 packageNames, user));
1193 }
1194 }
1195
1196 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001197 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001198 boolean replacing) {
1199 if (!replacing) {
1200 enqueuePackageUpdated(new PackageUpdatedTask(
1201 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1202 user));
1203 }
Kenny Guyed131872014-04-30 03:02:21 +01001204 }
1205
Kenny Guy44cba692016-01-21 19:50:02 +00001206 @Override
1207 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1208 enqueuePackageUpdated(new PackageUpdatedTask(
1209 PackageUpdatedTask.OP_SUSPEND, packageNames,
1210 user));
1211 }
1212
1213 @Override
1214 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1215 enqueuePackageUpdated(new PackageUpdatedTask(
1216 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1217 user));
1218 }
1219
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001220 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001221 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1222 * ACTION_PACKAGE_CHANGED.
1223 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001224 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001225 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001226 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001227
Joe Onorato36115782010-06-17 13:28:48 -04001228 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001229 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001230 // If we have changed locale we need to clear out the labels in all apps/workspace.
1231 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001232 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001233 Callbacks callbacks = getCallback();
1234 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001235 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001236 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001237 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Kenny Guyff05f432016-01-22 17:48:29 +00001238 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)
1239 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001240 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001241 forceReload();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001242 }
1243 }
1244
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001245 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001246 resetLoadedState(true, true);
1247
Reena Lee93f824a2011-09-23 17:20:28 -07001248 // Do this here because if the launcher activity is running it will be restarted.
1249 // If it's not running startLoaderFromBackground will merely tell it that it needs
1250 // to reload.
1251 startLoaderFromBackground();
1252 }
1253
Winson Chungf0c6ae02012-03-21 16:10:31 -07001254 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1255 synchronized (mLock) {
1256 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1257 // mWorkspaceLoaded to true later
1258 stopLoaderLocked();
1259 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1260 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1261 }
1262 }
1263
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001264 /**
1265 * When the launcher is in the background, it's possible for it to miss paired
1266 * configuration changes. So whenever we trigger the loader from the background
1267 * tell the launcher that it needs to re-run the loader when it comes back instead
1268 * of doing it now.
1269 */
1270 public void startLoaderFromBackground() {
1271 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001272 Callbacks callbacks = getCallback();
1273 if (callbacks != null) {
1274 // Only actually run the loader if they're not paused.
1275 if (!callbacks.setLoadOnResume()) {
1276 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001277 }
1278 }
1279 if (runLoader) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001280 startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001281 }
Joe Onorato36115782010-06-17 13:28:48 -04001282 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001283
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001284 /**
1285 * If there is already a loader task running, tell it to stop.
1286 */
1287 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001288 LoaderTask oldTask = mLoaderTask;
1289 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001290 oldTask.stopLocked();
1291 }
Reena Lee93f824a2011-09-23 17:20:28 -07001292 }
1293
Adam Cohen1a85c582014-09-30 09:48:49 -07001294 public boolean isCurrentCallbacks(Callbacks callbacks) {
1295 return (mCallbacks != null && mCallbacks.get() == callbacks);
1296 }
1297
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001298 public void startLoader(int synchronousBindPage) {
1299 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001300 }
1301
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001302 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001303 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1304 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001305 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001306 // Don't bother to start the thread if we know it's not going to do anything
1307 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001308 final Callbacks oldCallbacks = mCallbacks.get();
1309 // Clear any pending bind-runnables from the synchronized load process.
1310 runOnMainThread(new Runnable() {
1311 public void run() {
1312 oldCallbacks.clearPendingBinds();
1313 }
1314 });
1315
Joe Onorato36115782010-06-17 13:28:48 -04001316 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001317 stopLoaderLocked();
1318 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001319 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001320 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001321 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1322 } else {
1323 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1324 sWorker.post(mLoaderTask);
1325 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 }
1328 }
1329
Joe Onorato36115782010-06-17 13:28:48 -04001330 public void stopLoader() {
1331 synchronized (mLock) {
1332 if (mLoaderTask != null) {
1333 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001334 }
1335 }
Joe Onorato36115782010-06-17 13:28:48 -04001336 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001337
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001338 /**
1339 * Loads the workspace screen ids in an ordered list.
1340 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001341 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001342 final ContentResolver contentResolver = context.getContentResolver();
1343 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001344
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001345 // Get screens ordered by rank.
1346 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1347 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1348 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001349 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001350 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001351 while (sc.moveToNext()) {
1352 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001353 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001354 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001355 addDumpLog("Invalid screen id: " + e);
Winson Chung76828c82013-08-19 15:43:29 -07001356 }
1357 }
1358 } finally {
1359 sc.close();
1360 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001361 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001362 }
1363
Joe Onorato36115782010-06-17 13:28:48 -04001364 /**
1365 * Runnable for the thread that loads the contents of the launcher:
1366 * - workspace icons
1367 * - widgets
1368 * - all apps icons
1369 */
1370 private class LoaderTask implements Runnable {
1371 private Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -07001372 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001373 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001374 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001375 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001377 LoaderTask(Context context, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001378 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001379 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 }
1381
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001382 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001383 mIsLoadingAndBindingWorkspace = true;
1384
Joe Onorato36115782010-06-17 13:28:48 -04001385 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001386 if (DEBUG_LOADERS) {
1387 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001389
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001390 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001391 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001392 synchronized (LoaderTask.this) {
1393 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001394 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001395 }
1396 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001397 }
1398 }
1399
Joe Onorato36115782010-06-17 13:28:48 -04001400 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001401 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001402 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001403
Joe Onorato36115782010-06-17 13:28:48 -04001404 private void waitForIdle() {
1405 // Wait until the either we're stopped or the other threads are done.
1406 // This way we don't start loading all apps until the workspace has settled
1407 // down.
1408 synchronized (LoaderTask.this) {
1409 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001410
Joe Onorato36115782010-06-17 13:28:48 -04001411 mHandler.postIdle(new Runnable() {
1412 public void run() {
1413 synchronized (LoaderTask.this) {
1414 mLoadAndBindStepFinished = true;
1415 if (DEBUG_LOADERS) {
1416 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001417 }
Joe Onorato36115782010-06-17 13:28:48 -04001418 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001419 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001420 }
Joe Onorato36115782010-06-17 13:28:48 -04001421 });
1422
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001423 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001424 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001425 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1426 // wait no longer than 1sec at a time
1427 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001428 } catch (InterruptedException ex) {
1429 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001430 }
1431 }
Joe Onorato36115782010-06-17 13:28:48 -04001432 if (DEBUG_LOADERS) {
1433 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001434 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001435 + "ms for previous step to finish binding");
1436 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001437 }
Joe Onorato36115782010-06-17 13:28:48 -04001438 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001439
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001440 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001441 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 // Ensure that we have a valid page index to load synchronously
1443 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1444 "valid page index");
1445 }
1446 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1447 // Ensure that we don't try and bind a specified page when the pages have not been
1448 // loaded already (we should load everything asynchronously in that case)
1449 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1450 }
1451 synchronized (mLock) {
1452 if (mIsLoaderTaskRunning) {
1453 // Ensure that we are never running the background loading at this point since
1454 // we also touch the background collections
1455 throw new RuntimeException("Error! Background loading is already running");
1456 }
1457 }
1458
1459 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1460 // data structures, we can't allow any other thread to touch that data, but because
1461 // this call is synchronous, we can get away with not locking).
1462
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001463 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001464 // operations from the previous activity. We need to ensure that all queued operations
1465 // are executed before any synchronous binding work is done.
1466 mHandler.flush();
1467
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 // Divide the set of loaded items into those that we are binding synchronously, and
1469 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001470 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1472 // arise from that.
1473 onlyBindAllApps();
1474 }
1475
Joe Onorato36115782010-06-17 13:28:48 -04001476 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001477 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001478 if (mStopped) {
1479 return;
1480 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001481 mIsLoaderTaskRunning = true;
1482 }
Joe Onorato36115782010-06-17 13:28:48 -04001483 // Optimize for end-user experience: if the Launcher is up and // running with the
1484 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1485 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001486 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001487 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001488 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001489
Joe Onorato36115782010-06-17 13:28:48 -04001490 if (mStopped) {
1491 break keep_running;
1492 }
1493
Joe Onorato36115782010-06-17 13:28:48 -04001494 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001495
1496 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001497 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1498 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500
Joe Onorato36115782010-06-17 13:28:48 -04001501 // Clear out this reference, otherwise we end up holding it until all of the
1502 // callback runnables are done.
1503 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504
Joe Onorato36115782010-06-17 13:28:48 -04001505 synchronized (mLock) {
1506 // If we are still the last one to be scheduled, remove ourselves.
1507 if (mLoaderTask == this) {
1508 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001511 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513 }
1514
1515 public void stopLocked() {
1516 synchronized (LoaderTask.this) {
1517 mStopped = true;
1518 this.notify();
1519 }
1520 }
1521
1522 /**
1523 * Gets the callbacks object. If we've been stopped, or if the launcher object
1524 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1525 * object that was around when the deferred message was scheduled, and if there's
1526 * a new Callbacks object around then also return null. This will save us from
1527 * calling onto it with data that will be ignored.
1528 */
1529 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1530 synchronized (mLock) {
1531 if (mStopped) {
1532 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001533 }
Joe Onorato36115782010-06-17 13:28:48 -04001534
1535 if (mCallbacks == null) {
1536 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001537 }
Joe Onorato36115782010-06-17 13:28:48 -04001538
1539 final Callbacks callbacks = mCallbacks.get();
1540 if (callbacks != oldCallbacks) {
1541 return null;
1542 }
1543 if (callbacks == null) {
1544 Log.w(TAG, "no mCallbacks");
1545 return null;
1546 }
1547
1548 return callbacks;
1549 }
1550 }
1551
1552 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001553 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1554 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001555 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001557 final int countX = profile.numColumns;
1558 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001559
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001561 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001562 // Return early if we detect that an item is under the hotseat button
1563 if (mCallbacks == null ||
1564 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001565 Log.e(TAG, "Error loading shortcut into hotseat " + item
1566 + " into position (" + item.screenId + ":" + item.cellX + ","
1567 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001568 return false;
1569 }
1570
Dan Sandler295ae182013-12-10 16:05:47 -05001571 final ItemInfo[][] hotseatItems =
1572 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1573
Adam Cohen2e6da152015-05-06 11:42:25 -07001574 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001575 Log.e(TAG, "Error loading shortcut " + item
1576 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001577 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001578 + ")");
1579 return false;
1580 }
1581
Dan Sandler295ae182013-12-10 16:05:47 -05001582 if (hotseatItems != null) {
1583 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001584 Log.e(TAG, "Error loading shortcut into hotseat " + item
1585 + " into position (" + item.screenId + ":" + item.cellX + ","
1586 + item.cellY + ") occupied by "
1587 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1588 [(int) item.screenId][0]);
1589 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001590 } else {
1591 hotseatItems[(int) item.screenId][0] = item;
1592 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001593 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001594 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001595 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001596 items[(int) item.screenId][0] = item;
1597 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001598 return true;
1599 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001600 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1601 if (!workspaceScreens.contains((Long) item.screenId)) {
1602 // The item has an invalid screen id.
1603 return false;
1604 }
1605 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001606 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001607 return true;
1608 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001609
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001611 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001612 occupied.put(item.screenId, items);
1613 }
1614
Dan Sandler295ae182013-12-10 16:05:47 -05001615 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001616 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1617 item.cellX < 0 || item.cellY < 0 ||
1618 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1619 Log.e(TAG, "Error loading shortcut " + item
1620 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1621 + item.cellX + "," + item.cellY
1622 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1623 return false;
1624 }
1625
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001626 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001627 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1628 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001630 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001631 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001632 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001633 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001634 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001635 return false;
1636 }
1637 }
1638 }
1639 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1640 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001641 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001642 }
1643 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001644
Joe Onorato36115782010-06-17 13:28:48 -04001645 return true;
1646 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001647
Winson Chungba9c37f2013-08-30 14:11:37 -07001648 /** Clears all the sBg data structures */
1649 private void clearSBgDataStructures() {
1650 synchronized (sBgLock) {
1651 sBgWorkspaceItems.clear();
1652 sBgAppWidgets.clear();
1653 sBgFolders.clear();
1654 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001655 sBgWorkspaceScreens.clear();
1656 }
1657 }
1658
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001659 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001660 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661
Joe Onorato36115782010-06-17 13:28:48 -04001662 final Context context = mContext;
1663 final ContentResolver contentResolver = context.getContentResolver();
1664 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001665 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001666 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001667 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001668
Winson Chung892c74d2013-08-22 16:15:50 -07001669 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001670 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001671 int countX = profile.numColumns;
1672 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001673
Sunny Goyalf076eae2016-01-11 12:25:10 -08001674 if (GridSizeMigrationTask.ENABLED &&
1675 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1676 // Migration failed. Clear workspace.
1677 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001678 }
1679
Dan Sandlerd5024042014-01-09 15:01:33 -05001680 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001681 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001682 LauncherSettings.Settings.call(contentResolver,
1683 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001684 }
1685
1686 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1687 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001688 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001689 LauncherSettings.Settings.call(contentResolver,
1690 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001691 } else {
1692 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001693 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001694 LauncherSettings.Settings.call(contentResolver,
1695 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001696 }
Adam Cohene25af792013-06-06 23:08:25 -07001697
Winson Chung2abf94d2012-07-18 18:16:38 -07001698 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001699 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001700 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001701 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001702 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001703
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001705 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001706 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001707 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001708 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001709
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 // +1 for the hotseat (it can be larger than the workspace)
1711 // Load workspace in reverse order to ensure that latest items are loaded first (and
1712 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001713 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001714
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001715 try {
1716 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1717 final int intentIndex = c.getColumnIndexOrThrow
1718 (LauncherSettings.Favorites.INTENT);
1719 final int titleIndex = c.getColumnIndexOrThrow
1720 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001721 final int containerIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.CONTAINER);
1723 final int itemTypeIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.ITEM_TYPE);
1725 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001727 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 final int screenIndex = c.getColumnIndexOrThrow(
1730 LauncherSettings.Favorites.SCREEN);
1731 final int cellXIndex = c.getColumnIndexOrThrow
1732 (LauncherSettings.Favorites.CELLX);
1733 final int cellYIndex = c.getColumnIndexOrThrow
1734 (LauncherSettings.Favorites.CELLY);
1735 final int spanXIndex = c.getColumnIndexOrThrow
1736 (LauncherSettings.Favorites.SPANX);
1737 final int spanYIndex = c.getColumnIndexOrThrow(
1738 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001739 final int rankIndex = c.getColumnIndexOrThrow(
1740 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001741 final int restoredIndex = c.getColumnIndexOrThrow(
1742 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001743 final int profileIdIndex = c.getColumnIndexOrThrow(
1744 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001745 final int optionsIndex = c.getColumnIndexOrThrow(
1746 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001747 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748
Sunny Goyal7f834d22015-04-21 10:10:23 -07001749 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001750 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001751 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001752 long serialNo = mUserManager.getSerialNumberForUser(user);
1753 allUsers.put(serialNo, user);
1754 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001755 }
1756
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001757 ShortcutInfo info;
1758 String intentDescription;
1759 LauncherAppWidgetInfo appWidgetInfo;
1760 int container;
1761 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001762 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001763 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001764 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001765
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 while (!mStopped && c.moveToNext()) {
1767 try {
1768 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001769 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001770 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001771 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 switch (itemType) {
1774 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1775 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001776 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001778 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001779 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001780 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001781 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001782 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001783 if (user == null) {
1784 // User has been deleted remove the item.
1785 itemsToRemove.add(id);
1786 continue;
1787 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 try {
1789 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001790 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001791 if (cn != null && cn.getPackageName() != null) {
1792 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1793 cn.getPackageName(), user);
1794 boolean validComponent = validPkg &&
1795 launcherApps.isActivityEnabledForProfile(cn, user);
1796
1797 if (validComponent) {
1798 if (restored) {
1799 // no special handling necessary for this item
1800 restoredRows.add(id);
1801 restored = false;
1802 }
Kenny Guy44cba692016-01-21 19:50:02 +00001803 boolean isSuspended = launcherApps.isPackageSuspendedForProfile(
1804 cn.getPackageName(), user);
1805 if (isSuspended) {
1806 disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1807 }
Kenny Guyff05f432016-01-22 17:48:29 +00001808 if (quietMode.get(serialNumber)) {
1809 disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER;
1810 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001811 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001812 intent = null;
1813 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1814 // We allow auto install apps to have their intent
1815 // updated after an install.
1816 intent = manager.getLaunchIntentForPackage(
1817 cn.getPackageName());
1818 if (intent != null) {
1819 ContentValues values = new ContentValues();
1820 values.put(LauncherSettings.Favorites.INTENT,
1821 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001822 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001823 }
1824 }
1825
1826 if (intent == null) {
1827 // The app is installed but the component is no
1828 // longer available.
Sunny Goyala1365452015-10-01 15:46:24 -07001829 addDumpLog("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001830 itemsToRemove.add(id);
1831 continue;
1832 } else {
1833 // no special handling necessary for this item
1834 restoredRows.add(id);
1835 restored = false;
1836 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001837 } else if (restored) {
1838 // Package is not yet available but might be
1839 // installed later.
Sunny Goyala1365452015-10-01 15:46:24 -07001840 addDumpLog("package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001841
1842 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1843 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001844 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001845 // App restore has started. Update the flag
1846 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1847 ContentValues values = new ContentValues();
1848 values.put(LauncherSettings.Favorites.RESTORED,
1849 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001850 updateItem(id, values);
1851 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1852 // This is a common app. Try to replace this.
1853 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1854 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1855 if (parser.findDefaultApp()) {
1856 // Default app found. Replace it.
1857 intent = parser.parsedIntent;
1858 cn = intent.getComponent();
1859 ContentValues values = parser.parsedValues;
1860 values.put(LauncherSettings.Favorites.RESTORED, 0);
1861 updateItem(id, values);
1862 restored = false;
1863 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001864
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001865 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001866 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001867 itemsToRemove.add(id);
1868 continue;
1869 }
Sunny Goyal94485362014-09-18 16:13:58 -07001870 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyala1365452015-10-01 15:46:24 -07001871 addDumpLog("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001872 itemsToRemove.add(id);
1873 continue;
1874 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001875 } else if (launcherApps.isAppEnabled(
1876 manager, cn.getPackageName(),
1877 PackageManager.GET_UNINSTALLED_PACKAGES)) {
1878 // Package is present but not available.
1879 allowMissingTarget = true;
1880 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1881 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001882 // SdCard is not ready yet. Package might get available,
1883 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001884 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001885 HashSet<String> pkgs = sPendingPackages.get(user);
1886 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001887 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001888 sPendingPackages.put(user, pkgs);
1889 }
1890 pkgs.add(cn.getPackageName());
1891 allowMissingTarget = true;
1892 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001893
1894 } else {
1895 // Do not wait for external media load anymore.
1896 // Log the invalid package, and remove it
Sunny Goyala1365452015-10-01 15:46:24 -07001897 addDumpLog("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001898 itemsToRemove.add(id);
1899 continue;
Winson Chungee055712013-07-30 14:46:24 -07001900 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001901 } else if (cn == null) {
1902 // For shortcuts with no component, keep them as they are
1903 restoredRows.add(id);
1904 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 } catch (URISyntaxException e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001907 addDumpLog("Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001908 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 continue;
1910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911
Sunny Goyal34b65272015-03-11 16:56:52 -07001912 boolean useLowResIcon = container >= 0 &&
1913 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1914
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001915 if (itemReplaced) {
1916 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001917 info = getAppShortcutInfo(manager, intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001918 cursorIconInfo.iconIndex, titleIndex,
1919 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001920 } else {
1921 // Don't replace items for other profiles.
1922 itemsToRemove.add(id);
1923 continue;
1924 }
1925 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001926 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001927 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001928 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001929 intent = getRestoredItemIntent(c, context, intent);
1930 } else {
1931 // Don't restore items for other profiles.
1932 itemsToRemove.add(id);
1933 continue;
1934 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001935 } else if (itemType ==
1936 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -08001937 info = getAppShortcutInfo(manager, intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001938 cursorIconInfo.iconIndex, titleIndex,
1939 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001940 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001941 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001942
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001943 // App shortcuts that used to be automatically added to Launcher
1944 // didn't always have the correct intent flags set, so do that
1945 // here
1946 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001947 intent.getCategories() != null &&
1948 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001949 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 intent.addFlags(
1951 Intent.FLAG_ACTIVITY_NEW_TASK |
1952 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1953 }
Michael Jurka96879562012-03-22 05:54:33 -07001954 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001956 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001957 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001958 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 info.cellX = c.getInt(cellXIndex);
1962 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001963 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001964 info.spanX = 1;
1965 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001966 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001967 if (info.promisedIntent != null) {
1968 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1969 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001970 info.isDisabled = disabledState;
1971 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1972 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1973 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001974
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001976 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001977 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 break;
1979 }
1980
Sunny Goyal756adbc2015-04-16 15:20:51 -07001981 if (restored) {
1982 ComponentName cn = info.getTargetComponent();
1983 if (cn != null) {
1984 Integer progress = installingPkgs.get(cn.getPackageName());
1985 if (progress != null) {
1986 info.setInstallProgress(progress);
1987 } else {
1988 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1989 }
1990 }
1991 }
1992
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 switch (container) {
1994 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1995 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1996 sBgWorkspaceItems.add(info);
1997 break;
1998 default:
1999 // Item is in a user folder
2000 FolderInfo folderInfo =
2001 findOrMakeFolder(sBgFolders, container);
2002 folderInfo.add(info);
2003 break;
2004 }
2005 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002006 } else {
2007 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 }
2009 break;
2010
2011 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2012 id = c.getLong(idIndex);
2013 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2014
Sunny Goyala508e4f2015-05-21 09:33:57 -07002015 // Do not trim the folder label, as is was set by the user.
2016 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 folderInfo.cellX = c.getInt(cellXIndex);
2021 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002022 folderInfo.spanX = 1;
2023 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002024 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002025
Daniel Sandler8802e962010-05-26 16:28:16 -04002026 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002027 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002028 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002029 break;
2030 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002031
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2034 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2035 sBgWorkspaceItems.add(folderInfo);
2036 break;
Joe Onorato36115782010-06-17 13:28:48 -04002037 }
Joe Onorato17a89222011-02-08 17:26:11 -08002038
Chris Wrenf4d08112014-01-16 18:13:56 -05002039 if (restored) {
2040 // no special handling required for restored folders
2041 restoredRows.add(id);
2042 }
2043
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2045 sBgFolders.put(folderInfo.id, folderInfo);
2046 break;
2047
2048 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002049 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002051 boolean customWidget = itemType ==
2052 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2053
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002055 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002056 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002057 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002058 user = allUsers.get(serialNumber);
2059 if (user == null) {
2060 itemsToRemove.add(id);
2061 continue;
2062 }
2063
Sunny Goyalff572272014-07-23 13:58:07 -07002064 final ComponentName component =
2065 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002066
Sunny Goyal651077b2014-06-30 14:15:31 -07002067 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002068 final boolean isIdValid = (restoreStatus &
2069 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002070 final boolean wasProviderReady = (restoreStatus &
2071 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002072
Adam Cohen59400422014-03-05 18:07:04 -08002073 final LauncherAppWidgetProviderInfo provider =
2074 LauncherModel.getProviderInfo(context,
Robin Lee26ace122015-03-16 19:41:43 +00002075 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal7f834d22015-04-21 10:10:23 -07002076 user);
Sunny Goyalff572272014-07-23 13:58:07 -07002077
2078 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002079 if (!isSafeMode && !customWidget &&
2080 wasProviderReady && !isProviderReady) {
Sunny Goyala1365452015-10-01 15:46:24 -07002081 addDumpLog("Deleting widget that isn't installed anymore: "
2082 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083 itemsToRemove.add(id);
2084 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002085 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002086 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2087 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002088
Sunny Goyal53f96722015-07-13 19:54:53 -07002089 // The provider is available. So the widget is either
2090 // available or not available. We do not need to track
2091 // any future restore updates.
2092 int status = restoreStatus &
2093 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002094 if (!wasProviderReady) {
2095 // If provider was not previously ready, update the
2096 // status and UI flag.
2097
2098 // Id would be valid only if the widget restore broadcast was received.
2099 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002100 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002101 } else {
2102 status &= ~LauncherAppWidgetInfo
2103 .FLAG_PROVIDER_NOT_READY;
2104 }
2105 }
2106 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002107 } else {
2108 Log.v(TAG, "Widget restore pending id=" + id
2109 + " appWidgetId=" + appWidgetId
2110 + " status =" + restoreStatus);
2111 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002112 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002113 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002114 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002115
2116 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2117 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002118 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002119 // App restore has started. Update the flag
2120 appWidgetInfo.restoreStatus |=
2121 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002122 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyala1365452015-10-01 15:46:24 -07002123 addDumpLog("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002124 itemsToRemove.add(id);
2125 continue;
2126 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002127
2128 appWidgetInfo.installProgress =
2129 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002130 }
Sunny Goyalff572272014-07-23 13:58:07 -07002131
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002133 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 appWidgetInfo.cellX = c.getInt(cellXIndex);
2135 appWidgetInfo.cellY = c.getInt(cellYIndex);
2136 appWidgetInfo.spanX = c.getInt(spanXIndex);
2137 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002138 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002139
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002140 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2141 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2142 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002143 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2144 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 continue;
2146 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002148 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002150 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002151 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 break;
2153 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002154
Adam Cohen59400422014-03-05 18:07:04 -08002155 if (!customWidget) {
2156 String providerName =
2157 appWidgetInfo.providerName.flattenToString();
2158 if (!providerName.equals(savedProvider) ||
2159 (appWidgetInfo.restoreStatus != restoreStatus)) {
2160 ContentValues values = new ContentValues();
2161 values.put(
2162 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2163 providerName);
2164 values.put(LauncherSettings.Favorites.RESTORED,
2165 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002166 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002167 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002168 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002169 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2170 sBgAppWidgets.add(appWidgetInfo);
2171 }
Joe Onorato36115782010-06-17 13:28:48 -04002172 break;
2173 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002174 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002175 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002176 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 }
2178 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002179 if (c != null) {
2180 c.close();
2181 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 }
2183
Winson Chungba9c37f2013-08-30 14:11:37 -07002184 // Break early if we've stopped loading
2185 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002186 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002187 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002188 }
2189
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002192 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2193 Utilities.createDbSelectionQuery(
2194 LauncherSettings.Favorites._ID, itemsToRemove), null);
2195 if (DEBUG_LOADERS) {
2196 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2197 LauncherSettings.Favorites._ID, itemsToRemove));
2198 }
2199
2200 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002201 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2202 .call(contentResolver,
2203 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2204 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2205 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002206 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2207 sBgFolders.remove(folderId);
2208 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002209 }
2210 }
2211
Sunny Goyal317698b2015-07-29 11:45:41 -07002212 // Sort all the folder items and make sure the first 3 items are high resolution.
2213 for (FolderInfo folder : sBgFolders) {
2214 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2215 int pos = 0;
2216 for (ShortcutInfo info : folder.contents) {
2217 if (info.usingLowResIcon) {
2218 info.updateIcon(mIconCache, false);
2219 }
2220 pos ++;
2221 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2222 break;
2223 }
2224 }
2225 }
2226
Chris Wrenf4d08112014-01-16 18:13:56 -05002227 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002228 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002229 ContentValues values = new ContentValues();
2230 values.put(LauncherSettings.Favorites.RESTORED, 0);
2231 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2232 Utilities.createDbSelectionQuery(
2233 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002234 }
2235
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002236 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2237 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002238 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002239 null, sWorker);
2240 }
2241
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002242 // Remove any empty screens
2243 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002244 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002245 long screenId = item.screenId;
2246 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2247 unusedScreens.contains(screenId)) {
2248 unusedScreens.remove(screenId);
2249 }
2250 }
2251
2252 // If there are any empty screens remove them, and update.
2253 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002254 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002256 }
2257
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 if (DEBUG_LOADERS) {
2259 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2260 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002261 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002262 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002264
Sunny Goyale2df0622015-04-24 11:27:00 -07002265 for (int i = 0; i < nScreens; i++) {
2266 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002267 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 line += " | ";
2269 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002270 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002271 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002272 if (x < screen.length && y < screen[x].length) {
2273 line += (screen[x][y] != null) ? "#" : ".";
2274 } else {
2275 line += "!";
2276 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 }
Joe Onorato36115782010-06-17 13:28:48 -04002278 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002280 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002281 }
Joe Onorato36115782010-06-17 13:28:48 -04002282 }
Adam Cohene25af792013-06-06 23:08:25 -07002283 }
2284
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002285 /**
2286 * Partially updates the item without any notification. Must be called on the worker thread.
2287 */
2288 private void updateItem(long itemId, ContentValues update) {
2289 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002290 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002291 update,
2292 BaseColumns._ID + "= ?",
2293 new String[]{Long.toString(itemId)});
2294 }
2295
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 /** Filters the set of items who are directly or indirectly (via another container) on the
2297 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002298 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002299 ArrayList<ItemInfo> allWorkspaceItems,
2300 ArrayList<ItemInfo> currentScreenItems,
2301 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002302 // Purge any null ItemInfos
2303 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2304 while (iter.hasNext()) {
2305 ItemInfo i = iter.next();
2306 if (i == null) {
2307 iter.remove();
2308 }
2309 }
2310
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 // Order the set of items by their containers first, this allows use to walk through the
2312 // list sequentially, build up a list of containers that are in the specified screen,
2313 // as well as all items in those containers.
2314 Set<Long> itemsOnScreen = new HashSet<Long>();
2315 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2316 @Override
2317 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002318 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319 }
2320 });
2321 for (ItemInfo info : allWorkspaceItems) {
2322 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002323 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 currentScreenItems.add(info);
2325 itemsOnScreen.add(info.id);
2326 } else {
2327 otherScreenItems.add(info);
2328 }
2329 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2330 currentScreenItems.add(info);
2331 itemsOnScreen.add(info.id);
2332 } else {
2333 if (itemsOnScreen.contains(info.container)) {
2334 currentScreenItems.add(info);
2335 itemsOnScreen.add(info.id);
2336 } else {
2337 otherScreenItems.add(info);
2338 }
2339 }
2340 }
2341 }
2342
2343 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002344 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 ArrayList<LauncherAppWidgetInfo> appWidgets,
2346 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2347 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348
2349 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002350 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002352 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 currentScreenWidgets.add(widget);
2354 } else {
2355 otherScreenWidgets.add(widget);
2356 }
2357 }
2358 }
2359
2360 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002361 private void filterCurrentFolders(long currentScreenId,
Sunny Goyale2df0622015-04-24 11:27:00 -07002362 LongArrayMap<ItemInfo> itemsIdMap,
2363 LongArrayMap<FolderInfo> folders,
2364 LongArrayMap<FolderInfo> currentScreenFolders,
2365 LongArrayMap<FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366
Sunny Goyale2df0622015-04-24 11:27:00 -07002367 int total = folders.size();
2368 for (int i = 0; i < total; i++) {
2369 long id = folders.keyAt(i);
2370 FolderInfo folder = folders.valueAt(i);
2371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 ItemInfo info = itemsIdMap.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002373 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002375 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376 currentScreenFolders.put(id, folder);
2377 } else {
2378 otherScreenFolders.put(id, folder);
2379 }
2380 }
2381 }
2382
2383 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2384 * right) */
2385 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002386 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002387 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002388 final int screenCols = profile.numColumns;
2389 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002391 @Override
2392 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002393 if (lhs.container == rhs.container) {
2394 // Within containers, order by their spatial position in that container
2395 switch ((int) lhs.container) {
2396 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2397 long lr = (lhs.screenId * screenCellCount +
2398 lhs.cellY * screenCols + lhs.cellX);
2399 long rr = (rhs.screenId * screenCellCount +
2400 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002401 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002402 }
2403 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2404 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002405 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002406 }
2407 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002408 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002409 throw new RuntimeException("Unexpected container type when " +
2410 "sorting workspace items.");
2411 }
2412 return 0;
2413 }
2414 } else {
2415 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002416 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002417 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002418 }
2419 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002420 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002421
Adam Cohendcd297f2013-06-18 13:13:40 -07002422 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2423 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002424 final Runnable r = new Runnable() {
2425 @Override
2426 public void run() {
2427 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2428 if (callbacks != null) {
2429 callbacks.bindScreens(orderedScreens);
2430 }
2431 }
2432 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002433 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002434 }
2435
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2437 final ArrayList<ItemInfo> workspaceItems,
2438 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyale2df0622015-04-24 11:27:00 -07002439 final LongArrayMap<FolderInfo> folders,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002440 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441
2442 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002443 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002444 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002445 final int start = i;
2446 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002447 final Runnable r = new Runnable() {
2448 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002449 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002450 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002452 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2453 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 }
2455 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002457 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002458 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459
2460 // Bind the folders
2461 if (!folders.isEmpty()) {
2462 final Runnable r = new Runnable() {
2463 public void run() {
2464 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2465 if (callbacks != null) {
2466 callbacks.bindFolders(folders);
2467 }
2468 }
2469 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002470 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471 }
2472
2473 // Bind the widgets, one at a time
2474 N = appWidgets.size();
2475 for (int i = 0; i < N; i++) {
2476 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2477 final Runnable r = new Runnable() {
2478 public void run() {
2479 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2480 if (callbacks != null) {
2481 callbacks.bindAppWidget(widget);
2482 }
2483 }
2484 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002485 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 }
2487 }
2488
2489 /**
2490 * Binds all loaded data to actual views on the main thread.
2491 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002492 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 final long t = SystemClock.uptimeMillis();
2494 Runnable r;
2495
2496 // Don't use these two variables in any of the callback runnables.
2497 // Otherwise we hold a reference to them.
2498 final Callbacks oldCallbacks = mCallbacks.get();
2499 if (oldCallbacks == null) {
2500 // This launcher has exited and nobody bothered to tell us. Just bail.
2501 Log.w(TAG, "LoaderTask running with no launcher");
2502 return;
2503 }
2504
Winson Chung9b9fb962013-11-15 15:39:34 -08002505 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2507 ArrayList<LauncherAppWidgetInfo> appWidgets =
2508 new ArrayList<LauncherAppWidgetInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002509 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002510
2511 final LongArrayMap<FolderInfo> folders;
2512 final LongArrayMap<ItemInfo> itemsIdMap;
2513
Winson Chung2abf94d2012-07-18 18:16:38 -07002514 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002515 workspaceItems.addAll(sBgWorkspaceItems);
2516 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002517 orderedScreenIds.addAll(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002518
2519 folders = sBgFolders.clone();
2520 itemsIdMap = sBgItemsIdMap.clone();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521 }
2522
Derek Prothro7aff3992013-12-10 14:00:37 -05002523 final boolean isLoadingSynchronously =
2524 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002525 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002526 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002527 if (currScreen >= orderedScreenIds.size()) {
2528 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002529 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002530 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002531 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002532 final long currentScreenId = currentScreen < 0
2533 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002534
2535 // Load all the items that are on the current page first (and in the process, unbind
2536 // all the existing workspace items before we call startBinding() below.
2537 unbindWorkspaceItemsOnMainThread();
2538
2539 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2541 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2542 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2543 new ArrayList<LauncherAppWidgetInfo>();
2544 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2545 new ArrayList<LauncherAppWidgetInfo>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002546 LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>();
2547 LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548
Winson Chung9b9fb962013-11-15 15:39:34 -08002549 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002551 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002553 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 otherFolders);
2555 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2556 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2557
2558 // Tell the workspace that we're about to start binding items
2559 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002560 public void run() {
2561 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2562 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002563 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002564 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002565 }
2566 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002568 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002569
Adam Cohendcd297f2013-06-18 13:13:40 -07002570 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2571
Sunny Goyal527c7d32015-08-28 15:19:36 -07002572 Executor mainExecutor = new DeferredMainThreadExecutor();
2573 // Load items on the current page.
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002574 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002575 currentFolders, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576
Sunny Goyal527c7d32015-08-28 15:19:36 -07002577 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2578 // remaining screens after first onDraw is called. This ensures that the first screen
2579 // is immediately visible (eg. during rotation)
2580 // In case of !isLoadingSynchronously, bind all pages one after other.
2581 final Executor deferredExecutor = isLoadingSynchronously ?
2582 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2583
2584 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets,
2585 otherFolders, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002586
2587 // Tell the workspace that we're done binding items
2588 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002589 public void run() {
2590 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2591 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002592 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002593 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594
Sunny Goyal639e9062015-08-19 19:17:06 -07002595 mIsLoadingAndBindingWorkspace = false;
2596
2597 // Run all the bind complete runnables after workspace is bound.
2598 if (!mBindCompleteRunnables.isEmpty()) {
2599 synchronized (mBindCompleteRunnables) {
2600 for (final Runnable r : mBindCompleteRunnables) {
2601 runOnWorkerThread(r);
2602 }
2603 mBindCompleteRunnables.clear();
2604 }
2605 }
2606
Winson Chung98e030b2012-05-07 16:01:11 -07002607 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002608 if (DEBUG_LOADERS) {
2609 Log.d(TAG, "bound workspace in "
2610 + (SystemClock.uptimeMillis()-t) + "ms");
2611 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002612
Joe Onorato36115782010-06-17 13:28:48 -04002613 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002614 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002615 deferredExecutor.execute(r);
2616
Winson Chung4a2afa32012-07-19 14:53:05 -07002617 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002618 r = new Runnable() {
2619 public void run() {
2620 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2621 if (callbacks != null) {
2622 // We are loading synchronously, which means, some of the pages will be
2623 // bound after first draw. Inform the callbacks that page binding is
2624 // not complete, and schedule the remaining pages.
2625 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2626 callbacks.onPageBoundSynchronously(currentScreen);
2627 }
2628 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2629 }
2630 }
2631 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002632 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002633 }
Joe Onorato36115782010-06-17 13:28:48 -04002634 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002635
Joe Onorato36115782010-06-17 13:28:48 -04002636 private void loadAndBindAllApps() {
2637 if (DEBUG_LOADERS) {
2638 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2639 }
2640 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002641 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002642 synchronized (LoaderTask.this) {
2643 if (mStopped) {
2644 return;
2645 }
2646 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002647 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002648 synchronized (LoaderTask.this) {
2649 if (mStopped) {
2650 return;
2651 }
2652 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002653 }
Joe Onorato36115782010-06-17 13:28:48 -04002654 } else {
2655 onlyBindAllApps();
2656 }
2657 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002658
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002659 private void updateIconCache() {
2660 // Ignore packages which have a promise icon.
2661 HashSet<String> packagesToIgnore = new HashSet<>();
2662 synchronized (sBgLock) {
2663 for (ItemInfo info : sBgItemsIdMap) {
2664 if (info instanceof ShortcutInfo) {
2665 ShortcutInfo si = (ShortcutInfo) info;
2666 if (si.isPromise() && si.getTargetComponent() != null) {
2667 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2668 }
2669 } else if (info instanceof LauncherAppWidgetInfo) {
2670 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2671 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2672 packagesToIgnore.add(lawi.providerName.getPackageName());
2673 }
2674 }
2675 }
2676 }
2677 mIconCache.updateDbIcons(packagesToIgnore);
2678 }
2679
Joe Onorato36115782010-06-17 13:28:48 -04002680 private void onlyBindAllApps() {
2681 final Callbacks oldCallbacks = mCallbacks.get();
2682 if (oldCallbacks == null) {
2683 // This launcher has exited and nobody bothered to tell us. Just bail.
2684 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2685 return;
2686 }
2687
2688 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002689 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002690 final ArrayList<AppInfo> list
2691 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002692 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002693 public void run() {
2694 final long t = SystemClock.uptimeMillis();
2695 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2696 if (callbacks != null) {
2697 callbacks.bindAllApplications(list);
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002698 callbacks.bindAllPackages(mFgWidgetsModel);
Joe Onorato36115782010-06-17 13:28:48 -04002699 }
2700 if (DEBUG_LOADERS) {
2701 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002702 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002703 }
2704 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002705 };
2706 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002707 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002708 r.run();
2709 } else {
2710 mHandler.post(r);
2711 }
Joe Onorato36115782010-06-17 13:28:48 -04002712 }
2713
Winson Chung64359a52013-07-08 17:17:08 -07002714 private void loadAllApps() {
2715 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002716
Joe Onorato36115782010-06-17 13:28:48 -04002717 final Callbacks oldCallbacks = mCallbacks.get();
2718 if (oldCallbacks == null) {
2719 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002720 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002721 return;
2722 }
2723
Kenny Guyed131872014-04-30 03:02:21 +01002724 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2725
Winson Chung64359a52013-07-08 17:17:08 -07002726 // Clear the list of apps
2727 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002728 for (UserHandleCompat user : profiles) {
2729 // Query for the set of apps
2730 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002731 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002732 if (DEBUG_LOADERS) {
2733 Log.d(TAG, "getActivityList took "
2734 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2735 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2736 }
2737 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002738 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002739 if (apps == null || apps.isEmpty()) {
2740 return;
2741 }
Kenny Guyff05f432016-01-22 17:48:29 +00002742 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002743 // Create the ApplicationInfos
2744 for (int i = 0; i < apps.size(); i++) {
2745 LauncherActivityInfoCompat app = apps.get(i);
2746 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002747 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002748 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002749
Sunny Goyal756a28a2015-04-23 17:07:55 -07002750 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2751 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002752 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002753
2754 @Override
2755 public void run() {
2756 heuristic.processUserApps(apps);
2757 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002758 };
2759 runOnMainThread(new Runnable() {
2760
2761 @Override
2762 public void run() {
2763 // Check isLoadingWorkspace on the UI thread, as it is updated on
2764 // the UI thread.
2765 if (mIsLoadingAndBindingWorkspace) {
2766 synchronized (mBindCompleteRunnables) {
2767 mBindCompleteRunnables.add(r);
2768 }
2769 } else {
2770 runOnWorkerThread(r);
2771 }
2772 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002773 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002774 }
Winson Chung64359a52013-07-08 17:17:08 -07002775 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002776 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002777 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2778 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002779
2780 // Post callback on main thread
2781 mHandler.post(new Runnable() {
2782 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002783
Winson Chung64359a52013-07-08 17:17:08 -07002784 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002785 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002786 if (callbacks != null) {
2787 callbacks.bindAllApplications(added);
2788 if (DEBUG_LOADERS) {
2789 Log.d(TAG, "bound " + added.size() + " apps in "
2790 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2791 }
2792 } else {
2793 Log.i(TAG, "not binding apps: no Launcher activity");
2794 }
2795 }
2796 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002797 // Cleanup any data stored for a deleted user.
2798 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Winson Chung64359a52013-07-08 17:17:08 -07002799
Sunny Goyal2d648b02015-08-11 13:56:28 -07002800 loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);
Joe Onorato36115782010-06-17 13:28:48 -04002801 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002802 Log.d(TAG, "Icons processed in "
2803 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002804 }
2805 }
2806
2807 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002808 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002809 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002810 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2811 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2812 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2813 }
Joe Onorato36115782010-06-17 13:28:48 -04002814 }
2815 }
2816
Sunny Goyal75b0f552015-05-20 21:57:06 -07002817 /**
2818 * Called when the icons for packages have been updated in the icon cache.
2819 */
2820 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2821 final Callbacks callbacks = getCallback();
2822 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2823 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2824
2825 // If any package icon has changed (app was updated while launcher was dead),
2826 // update the corresponding shortcuts.
2827 synchronized (sBgLock) {
2828 for (ItemInfo info : sBgItemsIdMap) {
2829 if (info instanceof ShortcutInfo && user.equals(info.user)
2830 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2831 ShortcutInfo si = (ShortcutInfo) info;
2832 ComponentName cn = si.getTargetComponent();
2833 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2834 si.updateIcon(mIconCache);
2835 updatedShortcuts.add(si);
2836 }
2837 }
2838 }
2839 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2840 }
2841
2842 if (!updatedShortcuts.isEmpty()) {
2843 final UserHandleCompat userFinal = user;
2844 mHandler.post(new Runnable() {
2845
2846 public void run() {
2847 Callbacks cb = getCallback();
2848 if (cb != null && callbacks == cb) {
2849 cb.bindShortcutsChanged(updatedShortcuts,
2850 new ArrayList<ShortcutInfo>(), userFinal);
2851 }
2852 }
2853 });
2854 }
2855
2856 if (!updatedApps.isEmpty()) {
2857 mHandler.post(new Runnable() {
2858
2859 public void run() {
2860 Callbacks cb = getCallback();
2861 if (cb != null && callbacks == cb) {
2862 cb.bindAppsUpdated(updatedApps);
2863 }
2864 }
2865 });
2866 }
Sunny Goyal091f0ff2015-06-04 15:19:31 -07002867
2868 // Reload widget list. No need to refresh, as we only want to update the icons and labels.
Sunny Goyal2d648b02015-08-11 13:56:28 -07002869 loadAndBindWidgetsAndShortcuts(callbacks, false);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002870 }
2871
Joe Onorato36115782010-06-17 13:28:48 -04002872 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002873 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002874 }
2875
Adam Cohen091440a2015-03-18 14:16:05 -07002876 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002877
2878 @Override
2879 public void onReceive(Context context, Intent intent) {
2880 synchronized (sBgLock) {
2881 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2882 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002883 final PackageManager manager = context.getPackageManager();
2884 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2885 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002886 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2887 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002888 packagesRemoved.clear();
2889 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002890 for (String pkg : entry.getValue()) {
2891 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002892 boolean packageOnSdcard = launcherApps.isAppEnabled(
2893 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2894 if (packageOnSdcard) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002895 packagesUnavailable.add(pkg);
2896 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002897 packagesRemoved.add(pkg);
2898 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002899 }
2900 }
2901 if (!packagesRemoved.isEmpty()) {
2902 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2903 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2904 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002905 if (!packagesUnavailable.isEmpty()) {
2906 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2907 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2908 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002909 }
Sunny Goyal34942622014-08-29 17:20:55 -07002910 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002911 }
2912 }
2913 }
2914
Joe Onorato36115782010-06-17 13:28:48 -04002915 private class PackageUpdatedTask implements Runnable {
2916 int mOp;
2917 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002918 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002919
2920 public static final int OP_NONE = 0;
2921 public static final int OP_ADD = 1;
2922 public static final int OP_UPDATE = 2;
2923 public static final int OP_REMOVE = 3; // uninstlled
2924 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002925 public static final int OP_SUSPEND = 5; // package suspended
2926 public static final int OP_UNSUSPEND = 6; // package unsuspended
Joe Onorato36115782010-06-17 13:28:48 -04002927
2928
Kenny Guyed131872014-04-30 03:02:21 +01002929 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002930 mOp = op;
2931 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002932 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002933 }
2934
2935 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002936 if (!mHasLoaderCompletedOnce) {
2937 // Loader has not yet run.
2938 return;
2939 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002940 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002941
2942 final String[] packages = mPackages;
2943 final int N = packages.length;
2944 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002945 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002946 for (int i=0; i<N; i++) {
2947 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002948 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002949 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002950 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002951
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002952 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2953 if (heuristic != null) {
2954 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002955 }
Joe Onorato36115782010-06-17 13:28:48 -04002956 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002957 }
Joe Onorato36115782010-06-17 13:28:48 -04002958 case OP_UPDATE:
2959 for (int i=0; i<N; i++) {
2960 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002961 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002962 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002963 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002964 }
2965 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002966 case OP_REMOVE: {
2967 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2968 if (heuristic != null) {
2969 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002970 }
Joe Onorato36115782010-06-17 13:28:48 -04002971 for (int i=0; i<N; i++) {
2972 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002973 mIconCache.removeIconsForPkg(packages[i], mUser);
2974 }
2975 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002976 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002977 case OP_UNAVAILABLE:
2978 for (int i=0; i<N; i++) {
2979 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2980 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002981 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002982 }
2983 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002984 case OP_SUSPEND:
2985 case OP_UNSUSPEND:
2986 boolean suspend = mOp == OP_SUSPEND;
2987 for (int i=0; i<N; i++) {
2988 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.suspendPackage "
2989 + suspend + " " + packages[i]);
2990 mBgAllAppsList.suspendPackage(packages[i], mUser, suspend);
2991 }
2992 break;
Joe Onorato36115782010-06-17 13:28:48 -04002993 }
2994
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002995 ArrayList<AppInfo> added = null;
2996 ArrayList<AppInfo> modified = null;
2997 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002998
Adam Cohen487f7dd2012-06-28 18:12:10 -07002999 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003000 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003001 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003002 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003003 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003004 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003005 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003006 }
Winson Chung5d55f332012-07-16 20:45:03 -07003007 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003008 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003009 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003010 }
3011
Sunny Goyale0f58d72014-11-10 18:05:31 -08003012 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003013 if (callbacks == null) {
3014 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3015 return;
3016 }
3017
Sunny Goyal4390ace2014-10-13 11:33:11 -07003018 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3019 new HashMap<ComponentName, AppInfo>();
3020
Joe Onorato36115782010-06-17 13:28:48 -04003021 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003022 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003023 for (AppInfo ai : added) {
3024 addedOrUpdatedApps.put(ai.componentName, ai);
3025 }
Joe Onorato36115782010-06-17 13:28:48 -04003026 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003027
Joe Onorato36115782010-06-17 13:28:48 -04003028 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003029 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003030 for (AppInfo ai : modified) {
3031 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003032 }
3033
Joe Onorato36115782010-06-17 13:28:48 -04003034 mHandler.post(new Runnable() {
3035 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003036 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003037 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003038 callbacks.bindAppsUpdated(modifiedFinal);
3039 }
3040 }
3041 });
3042 }
Winson Chung83892cc2013-05-01 16:53:33 -07003043
Sunny Goyal4390ace2014-10-13 11:33:11 -07003044 // Update shortcut infos
Kenny Guy44cba692016-01-21 19:50:02 +00003045 if (mOp == OP_ADD || mOp == OP_UPDATE || mOp == OP_UNSUSPEND) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003046 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3047 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3048 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3049
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003050 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003051 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003052 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003053 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3054 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003055 boolean infoUpdated = false;
3056 boolean shortcutUpdated = false;
3057
Kenny Guy44cba692016-01-21 19:50:02 +00003058 if (mOp == OP_UNSUSPEND) {
3059 si.isDisabled &= ~ ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3060 infoUpdated = true;
3061 }
3062
Sunny Goyal4390ace2014-10-13 11:33:11 -07003063 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003064 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003065 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003066 Bitmap icon = Utilities.createIconBitmap(
3067 si.iconResource.packageName,
3068 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003069 if (icon != null) {
3070 si.setIcon(icon);
3071 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003072 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003073 }
3074 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075
3076 ComponentName cn = si.getTargetComponent();
3077 if (cn != null && packageSet.contains(cn.getPackageName())) {
3078 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3079
3080 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003081 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3082 // Auto install icon
3083 PackageManager pm = context.getPackageManager();
3084 ResolveInfo matched = pm.resolveActivity(
3085 new Intent(Intent.ACTION_MAIN)
3086 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3087 PackageManager.MATCH_DEFAULT_ONLY);
3088 if (matched == null) {
3089 // Try to find the best match activity.
3090 Intent intent = pm.getLaunchIntentForPackage(
3091 cn.getPackageName());
3092 if (intent != null) {
3093 cn = intent.getComponent();
3094 appInfo = addedOrUpdatedApps.get(cn);
3095 }
3096
3097 if ((intent == null) || (appInfo == null)) {
3098 removedShortcuts.add(si);
3099 continue;
3100 }
3101 si.promisedIntent = intent;
3102 }
3103 }
3104
3105 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003106 if (appInfo != null) {
3107 si.flags = appInfo.flags;
3108 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003109
Sunny Goyal756adbc2015-04-16 15:20:51 -07003110 si.intent = si.promisedIntent;
3111 si.promisedIntent = null;
3112 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003113 infoUpdated = true;
3114 si.updateIcon(mIconCache);
3115 }
3116
3117 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3118 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3119 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003120 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121 si.contentDescription = appInfo.contentDescription;
3122 infoUpdated = true;
3123 }
3124
3125 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3126 // Since package was just updated, the target must be available now.
3127 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3128 shortcutUpdated = true;
3129 }
3130 }
3131
3132 if (infoUpdated || shortcutUpdated) {
3133 updatedShortcuts.add(si);
3134 }
3135 if (infoUpdated) {
3136 updateItemInDatabase(context, si);
3137 }
3138 } else if (info instanceof LauncherAppWidgetInfo) {
3139 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3140 if (mUser.equals(widgetInfo.user)
3141 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3142 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003143 widgetInfo.restoreStatus &=
3144 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3145 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003146
3147 // adding this flag ensures that launcher shows 'click to setup'
3148 // if the widget has a config activity. In case there is no config
3149 // activity, it will be marked as 'restored' during bind.
3150 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3151
Sunny Goyal4390ace2014-10-13 11:33:11 -07003152 widgets.add(widgetInfo);
3153 updateItemInDatabase(context, widgetInfo);
3154 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003155 }
3156 }
3157 }
3158
Sunny Goyal4390ace2014-10-13 11:33:11 -07003159 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3160 mHandler.post(new Runnable() {
3161
3162 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003163 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003164 if (callbacks == cb && cb != null) {
3165 callbacks.bindShortcutsChanged(
3166 updatedShortcuts, removedShortcuts, mUser);
3167 }
3168 }
3169 });
3170 if (!removedShortcuts.isEmpty()) {
3171 deleteItemsFromDatabase(context, removedShortcuts);
3172 }
3173 }
3174 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003175 mHandler.post(new Runnable() {
3176 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003177 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003178 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003179 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003180 }
3181 }
3182 });
3183 }
3184 }
3185
Winson Chungdf95eb12013-10-16 14:57:07 -07003186 final ArrayList<String> removedPackageNames =
3187 new ArrayList<String>();
Kenny Guy44cba692016-01-21 19:50:02 +00003188 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE || mOp == OP_SUSPEND) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003189 // Mark all packages in the broadcast to be removed
3190 removedPackageNames.addAll(Arrays.asList(packages));
3191 } else if (mOp == OP_UPDATE) {
3192 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003193 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003194 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003195 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003196 }
3197 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003198 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003199
Winson Chungdf95eb12013-10-16 14:57:07 -07003200 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003201 final int removeReason;
3202 if (mOp == OP_UNAVAILABLE) {
3203 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
Kenny Guy44cba692016-01-21 19:50:02 +00003204 } else if (mOp == OP_SUSPEND) {
3205 removeReason = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
Sunny Goyal1a745e82014-10-02 15:58:31 -07003206 } else {
3207 // Remove all the components associated with this package
3208 for (String pn : removedPackageNames) {
3209 deletePackageFromDatabase(context, pn, mUser);
3210 }
3211 // Remove all the specific components
3212 for (AppInfo a : removedApps) {
3213 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3214 deleteItemsFromDatabase(context, infos);
3215 }
3216 removeReason = 0;
3217 }
3218
Winson Chungdf95eb12013-10-16 14:57:07 -07003219 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003220 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003221 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003222 mHandler.post(new Runnable() {
3223 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003224 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003225 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003226 callbacks.bindComponentsRemoved(
3227 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003228 }
3229 }
3230 });
Joe Onoratobe386092009-11-17 17:32:16 -08003231 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003232
Sunny Goyal2d648b02015-08-11 13:56:28 -07003233 // Update widgets
3234 if (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE) {
3235 // Always refresh for a package event on secondary user
3236 boolean needToRefresh = !mUser.equals(UserHandleCompat.myUserHandle());
3237
3238 // Refresh widget list, if the package already had a widget.
3239 synchronized (sBgLock) {
3240 if (sBgWidgetProviders != null) {
3241 HashSet<String> pkgSet = new HashSet<>();
3242 Collections.addAll(pkgSet, mPackages);
3243
3244 for (ComponentKey key : sBgWidgetProviders.keySet()) {
3245 needToRefresh |= key.user.equals(mUser) &&
3246 pkgSet.contains(key.componentName.getPackageName());
3247 }
3248 }
3249 }
3250
3251 if (!needToRefresh && mOp != OP_REMOVE) {
3252 // Refresh widget list, if there is any newly added widget
3253 PackageManager pm = context.getPackageManager();
3254 for (String pkg : mPackages) {
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003255 try {
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003256 List<ResolveInfo> widgets = pm.queryBroadcastReceivers(
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003257 new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
Sunny Goyal7b221ef2015-09-17 14:09:07 -07003258 .setPackage(pkg), 0);
3259 needToRefresh |= widgets != null && !widgets.isEmpty();
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003260 } catch (RuntimeException e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003261 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal79afe012015-09-17 12:50:11 -07003262 throw e;
3263 }
Sunny Goyal3dc7bee2015-09-15 11:32:58 -07003264 // Ignore the crash. We can live with a state widget list.
3265 Log.e(TAG, "PM call failed for " + pkg, e);
3266 }
Sunny Goyal2d648b02015-08-11 13:56:28 -07003267 }
3268 }
3269
3270 loadAndBindWidgetsAndShortcuts(callbacks, needToRefresh);
3271 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 }
3273 }
3274
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003275 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context,
3276 boolean refresh) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003277 ArrayList<LauncherAppWidgetProviderInfo> results =
3278 new ArrayList<LauncherAppWidgetProviderInfo>();
3279 try {
3280 synchronized (sBgLock) {
3281 if (sBgWidgetProviders == null || refresh) {
3282 HashMap<ComponentKey, LauncherAppWidgetProviderInfo> tmpWidgetProviders
3283 = new HashMap<>();
3284 AppWidgetManagerCompat wm = AppWidgetManagerCompat.getInstance(context);
3285 LauncherAppWidgetProviderInfo info;
Adam Cohen59400422014-03-05 18:07:04 -08003286
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003287 List<AppWidgetProviderInfo> widgets = wm.getAllProviders();
3288 for (AppWidgetProviderInfo pInfo : widgets) {
3289 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3290 UserHandleCompat user = wm.getUser(info);
3291 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3292 }
Robin Lee26ace122015-03-16 19:41:43 +00003293
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003294 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3295 for (CustomAppWidget widget : customWidgets) {
3296 info = new LauncherAppWidgetProviderInfo(context, widget);
3297 UserHandleCompat user = wm.getUser(info);
3298 tmpWidgetProviders.put(new ComponentKey(info.provider, user), info);
3299 }
3300 // Replace the global list at the very end, so that if there is an exception,
3301 // previously loaded provider list is used.
3302 sBgWidgetProviders = tmpWidgetProviders;
Robin Lee26ace122015-03-16 19:41:43 +00003303 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003304 results.addAll(sBgWidgetProviders.values());
3305 return results;
Adam Cohen59400422014-03-05 18:07:04 -08003306 }
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003307 } catch (Exception e) {
Sunny Goyal3a6205f2015-09-17 15:01:11 -07003308 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal79afe012015-09-17 12:50:11 -07003309 (e.getCause() instanceof TransactionTooLargeException ||
3310 e.getCause() instanceof DeadObjectException)) {
Hyunyoung Song3abd5482015-06-08 18:41:04 -07003311 // the returned value may be incomplete and will not be refreshed until the next
3312 // time Launcher starts.
3313 // TODO: after figuring out a repro step, introduce a dirty bit to check when
3314 // onResume is called to refresh the widget provider list.
3315 synchronized (sBgLock) {
3316 if (sBgWidgetProviders != null) {
3317 results.addAll(sBgWidgetProviders.values());
3318 }
3319 return results;
3320 }
3321 } else {
3322 throw e;
3323 }
Adam Cohen59400422014-03-05 18:07:04 -08003324 }
3325 }
3326
Robin Lee26ace122015-03-16 19:41:43 +00003327 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name,
3328 UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -08003329 synchronized (sBgLock) {
3330 if (sBgWidgetProviders == null) {
Hyunyoung Song70a48be2015-03-11 16:36:52 -07003331 getWidgetProviders(ctx, false /* refresh */);
Adam Cohen59400422014-03-05 18:07:04 -08003332 }
Robin Lee26ace122015-03-16 19:41:43 +00003333 return sBgWidgetProviders.get(new ComponentKey(name, user));
Adam Cohen59400422014-03-05 18:07:04 -08003334 }
3335 }
3336
Sunny Goyal2d648b02015-08-11 13:56:28 -07003337 public void loadAndBindWidgetsAndShortcuts(final Callbacks callbacks, final boolean refresh) {
Hyunyoung Song9110d482015-05-22 14:49:23 -07003338
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003339 runOnWorkerThread(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003340 @Override
3341 public void run() {
Sunny Goyal2d648b02015-08-11 13:56:28 -07003342 updateWidgetsModel(refresh);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003343 mHandler.post(new Runnable() {
3344 @Override
3345 public void run() {
3346 Callbacks cb = getCallback();
3347 if (callbacks == cb && cb != null) {
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003348 callbacks.bindAllPackages(mFgWidgetsModel);
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003349 }
3350 }
3351 });
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003352 // update the Widget entries inside DB on the worker thread.
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07003353 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003354 mFgWidgetsModel.getRawList());
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003355 }
3356 });
3357 }
3358
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003359 /**
Hyunyoung Song9110d482015-05-22 14:49:23 -07003360 * Returns a list of ResolveInfos/AppWidgetInfos.
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003361 *
Hyunyoung Song9110d482015-05-22 14:49:23 -07003362 * @see #loadAndBindWidgetsAndShortcuts
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003363 */
Sunny Goyal2d648b02015-08-11 13:56:28 -07003364 @Thunk void updateWidgetsModel(boolean refresh) {
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003365 Utilities.assertWorkerThread();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003366 PackageManager packageManager = mApp.getContext().getPackageManager();
Michael Jurkac402cd92013-05-20 15:49:32 +02003367 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyal2d648b02015-08-11 13:56:28 -07003368 widgetsAndShortcuts.addAll(getWidgetProviders(mApp.getContext(), refresh));
Sunny Goyal31860582015-09-18 08:38:57 -07003369
3370 // Update shortcut providers
3371 synchronized (sBgLock) {
3372 try {
3373 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3374 List<ResolveInfo> providers = packageManager.queryIntentActivities(shortcutsIntent, 0);
3375 sBgShortcutProviders = providers;
3376 } catch (RuntimeException e) {
Sunny Goyal877f9282015-09-18 10:18:18 -07003377 if (!ProviderConfig.IS_DOGFOOD_BUILD &&
Sunny Goyal31860582015-09-18 08:38:57 -07003378 (e.getCause() instanceof TransactionTooLargeException ||
3379 e.getCause() instanceof DeadObjectException)) {
3380 /**
3381 * Ignore exception and use the cached list if available.
3382 * Refer to {@link #getWidgetProviders(Context, boolean}} for more info.
3383 */
3384 } else {
3385 throw e;
3386 }
3387 }
3388 if (sBgShortcutProviders != null) {
3389 widgetsAndShortcuts.addAll(sBgShortcutProviders);
3390 }
3391 }
Hyunyoung Songeaf291b2015-06-17 21:12:44 -07003392 mBgWidgetsModel.setWidgetsAndShortcuts(widgetsAndShortcuts);
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08003393 mFgWidgetsModel = mBgWidgetsModel.clone();
Michael Jurkac402cd92013-05-20 15:49:32 +02003394 }
3395
Adam Cohen091440a2015-03-18 14:16:05 -07003396 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003397 UserHandleCompat user) {
3398 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3399 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003400 }
Adam Cohen556f6132014-01-15 15:18:08 -08003401
Kenny Guyed131872014-04-30 03:02:21 +01003402 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3403 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003404 if (cn == null) {
3405 return false;
3406 }
Kenny Guyed131872014-04-30 03:02:21 +01003407 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3408 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003409 return false;
3410 }
Kenny Guyed131872014-04-30 03:02:21 +01003411 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003412 }
3413
Adam Cohena28b78e2014-05-20 17:03:04 -07003414 public static boolean isValidPackage(Context context, String packageName,
3415 UserHandleCompat user) {
3416 if (packageName == null) {
3417 return false;
3418 }
3419 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3420 return launcherApps.isPackageEnabledForProfile(packageName, user);
3421 }
3422
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003424 * Make an ShortcutInfo object for a restored application or shortcut item that points
3425 * to a package that is not yet installed on the system.
3426 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003427 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003428 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003429 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003430 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003431
3432 Bitmap icon = iconInfo.loadIcon(c, info, context);
3433 // the fallback icon
3434 if (icon == null) {
3435 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3436 } else {
3437 info.setIcon(icon);
3438 }
Sunny Goyal34942622014-08-29 17:20:55 -07003439
3440 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003441 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003442 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003443 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003444 }
Sunny Goyal34942622014-08-29 17:20:55 -07003445 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3446 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003447 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003448 }
Sunny Goyal34942622014-08-29 17:20:55 -07003449 } else {
3450 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3451 }
3452
Winson Chung82b016c2015-05-08 17:00:10 -07003453 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003454 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003455 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003456 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003457 return info;
3458 }
3459
3460 /**
3461 * Make an Intent object for a restored application or shortcut item that points
3462 * to the market page for the item.
3463 */
Adam Cohen091440a2015-03-18 14:16:05 -07003464 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003465 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003466 return getMarketIntent(componentName.getPackageName());
3467 }
3468
3469 static Intent getMarketIntent(String packageName) {
3470 return new Intent(Intent.ACTION_VIEW)
3471 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003472 .scheme("market")
3473 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003474 .appendQueryParameter("id", packageName)
3475 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003476 }
3477
3478 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003479 * Make an ShortcutInfo object for a shortcut that is an application.
3480 *
3481 * If c is not null, then it will be used to fill in missing data like the title and icon.
3482 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003483 public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003484 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003485 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003486 if (user == null) {
3487 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003488 return null;
3489 }
3490
Kenny Guyed131872014-04-30 03:02:21 +01003491 ComponentName componentName = intent.getComponent();
3492 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003493 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003494 return null;
3495 }
3496
3497 Intent newIntent = new Intent(intent.getAction(), null);
3498 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3499 newIntent.setComponent(componentName);
3500 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003501 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003502 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3503 return null;
3504 }
3505
3506 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003507 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003508 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3509 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3510 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003511 }
3512
Joe Onorato56d82912010-03-07 14:32:10 -05003513 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003514 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003515 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003516 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003517
Joe Onorato56d82912010-03-07 14:32:10 -05003518 // fall back to the class name of the activity
3519 if (info.title == null) {
3520 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003521 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003522
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003524 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003525 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003526 if (lai != null) {
3527 info.flags = AppInfo.initFlags(lai);
3528 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003530 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003531
Sunny Goyale2df0622015-04-24 11:27:00 -07003532 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003533 ItemInfoFilter f) {
3534 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3535 for (ItemInfo i : infos) {
3536 if (i instanceof ShortcutInfo) {
3537 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003538 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003539 if (cn != null && f.filterItem(null, info, cn)) {
3540 filtered.add(info);
3541 }
3542 } else if (i instanceof FolderInfo) {
3543 FolderInfo info = (FolderInfo) i;
3544 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003545 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003546 if (cn != null && f.filterItem(info, s, cn)) {
3547 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003548 }
3549 }
Winson Chung64359a52013-07-08 17:17:08 -07003550 } else if (i instanceof LauncherAppWidgetInfo) {
3551 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3552 ComponentName cn = info.providerName;
3553 if (cn != null && f.filterItem(null, info, cn)) {
3554 filtered.add(info);
3555 }
Winson Chung8a435102012-08-30 17:16:53 -07003556 }
3557 }
Winson Chung64359a52013-07-08 17:17:08 -07003558 return new ArrayList<ItemInfo>(filtered);
3559 }
3560
Adam Cohen091440a2015-03-18 14:16:05 -07003561 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003562 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003563 ItemInfoFilter filter = new ItemInfoFilter() {
3564 @Override
3565 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003566 if (info.user == null) {
3567 return cn.equals(cname);
3568 } else {
3569 return cn.equals(cname) && info.user.equals(user);
3570 }
Winson Chung64359a52013-07-08 17:17:08 -07003571 }
3572 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003573 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003574 }
3575
Sunny Goyal1a745e82014-10-02 15:58:31 -07003576 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003577 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003578 */
Adam Cohen091440a2015-03-18 14:16:05 -07003579 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003580 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003581 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003582 // Non-app shortcuts are only supported for current user.
3583 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003585
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003586 // TODO: If there's an explicit component and we can't install that, delete it.
3587
Winson Chung82b016c2015-05-08 17:00:10 -07003588 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003589
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003590 Bitmap icon = iconInfo.loadIcon(c, info, context);
3591 // the fallback icon
3592 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003593 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003594 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003595 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003596 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 return info;
3598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599
Sunny Goyal2350bc92014-10-14 16:42:54 -07003600 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003601 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3602 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3603 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3604
Adam Cohend9198822011-11-22 16:42:47 -08003605 if (intent == null) {
3606 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3607 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3608 return null;
3609 }
3610
Joe Onorato0589f0f2010-02-08 13:44:00 -08003611 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003612 boolean customIcon = false;
3613 ShortcutIconResource iconResource = null;
3614
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003615 if (bitmap instanceof Bitmap) {
3616 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003617 customIcon = true;
3618 } else {
3619 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003620 if (extra instanceof ShortcutIconResource) {
3621 iconResource = (ShortcutIconResource) extra;
3622 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003623 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003624 }
3625 }
3626
Michael Jurkac9d95c52011-08-29 14:03:34 -07003627 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003628
Kenny Guyed131872014-04-30 03:02:21 +01003629 // Only support intents for current user for now. Intents sent from other
3630 // users wouldn't get here without intent forwarding anyway.
3631 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003632 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003633 icon = mIconCache.getDefaultIcon(info.user);
3634 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003635 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003636 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003637
Winson Chung82b016c2015-05-08 17:00:10 -07003638 info.title = Utilities.trim(name);
3639 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003640 info.intent = intent;
3641 info.customIcon = customIcon;
3642 info.iconResource = iconResource;
3643
3644 return info;
3645 }
3646
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003648 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 * or make a new one.
3650 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003651 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 // See if a placeholder was created for us already
3653 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003654 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003656 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 folders.put(id, folderInfo);
3658 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003659 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003660 }
3661
Joe Onoratobe386092009-11-17 17:32:16 -08003662
Sunny Goyal651077b2014-06-30 14:15:31 -07003663 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3664 return (provider != null) && (provider.provider != null)
3665 && (provider.provider.getPackageName() != null);
3666 }
3667
Joe Onoratobe386092009-11-17 17:32:16 -08003668 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003669 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003670 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3671 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3672 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3673 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003674 if (mLoaderTask != null) {
3675 mLoaderTask.dumpState();
3676 } else {
3677 Log.d(TAG, "mLoaderTask=null");
3678 }
Joe Onoratobe386092009-11-17 17:32:16 -08003679 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003680
3681 public Callbacks getCallback() {
3682 return mCallbacks != null ? mCallbacks.get() : null;
3683 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003684
3685 /**
3686 * @return {@link FolderInfo} if its already loaded.
3687 */
3688 public FolderInfo findFolderById(Long folderId) {
3689 synchronized (sBgLock) {
3690 return sBgFolders.get(folderId);
3691 }
3692 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003693
Sunny Goyal527c7d32015-08-28 15:19:36 -07003694 @Thunk class DeferredMainThreadExecutor implements Executor {
3695
3696 @Override
3697 public void execute(Runnable command) {
3698 runOnMainThread(command);
3699 }
3700 }
3701
Sunny Goyal756adbc2015-04-16 15:20:51 -07003702 /**
3703 * @return the looper for the worker thread which can be used to start background tasks.
3704 */
3705 public static Looper getWorkerLooper() {
3706 return sWorkerThread.getLooper();
3707 }
Sunny Goyala1365452015-10-01 15:46:24 -07003708
3709 @Thunk static final void addDumpLog(String log) {
3710 Launcher.addDumpLog(TAG, log);
3711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003712}