blob: fec96ca2ee38bffcc7662bd9f06338ee08f44a20 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070023import android.content.ContentProviderOperation;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070029import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.database.Cursor;
33import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040035import android.os.Handler;
36import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070037import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040040import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Sunny Goyalffe83f12014-08-14 17:39:34 -070047import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.LauncherActivityInfoCompat;
49import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070050import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070051import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.UserHandleCompat;
53import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070054import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070055import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.Folder;
57import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070058import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080059import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070060import com.android.launcher3.model.WidgetsModel;
Robin Lee26ace122015-03-16 19:41:43 +000061import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070062import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070063import com.android.launcher3.util.FlagOp;
Sunny Goyale2df0622015-04-24 11:27:00 -070064import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070065import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyald09c3702016-04-06 16:18:20 -070066import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070067import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070068import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070069import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070070import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080071
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.lang.ref.WeakReference;
73import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070074import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070075import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070076import java.util.Arrays;
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 LauncherAppState mApp;
108 @Thunk final Object mLock = new Object();
109 @Thunk DeferredHandler mHandler = new DeferredHandler();
110 @Thunk LoaderTask mLoaderTask;
111 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700112 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115 static {
116 sWorkerThread.start();
117 }
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 // We start off with everything not loaded. After that, we assume that
121 // our monitoring of the package manager provides all updates and we never
122 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk boolean mWorkspaceLoaded;
124 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700125
Sunny Goyal756a28a2015-04-23 17:07:55 -0700126 /**
127 * Set of runnables to be called on the background thread after the workspace binding
128 * is complete.
129 */
130 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
131
Adam Cohen091440a2015-03-18 14:16:05 -0700132 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700134 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800135 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700136 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800137 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800138
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700139 // The lock that must be acquired before referencing any static bg data structures. Unlike
140 // other locks, this one can generally be held long-term because we never expect any of these
141 // static data structures to be referenced outside of the worker thread except on the first
142 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700143 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700144
Adam Cohen487f7dd2012-06-28 18:12:10 -0700145 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700147 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
150 // created by LauncherModel that are directly on the home screen (however, no widgets or
151 // shortcuts within folders).
152 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
155 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156 new ArrayList<LauncherAppWidgetInfo>();
157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700159 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700160
Adam Cohendcd297f2013-06-18 13:13:40 -0700161 // sBgWorkspaceScreens is the ordered set of workspace screens.
162 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
163
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700164 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700165 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
166 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700167
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700168 // </ only access in worker thread >
169
Adam Cohen091440a2015-03-18 14:16:05 -0700170 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Adam Cohen091440a2015-03-18 14:16:05 -0700172 @Thunk final LauncherAppsCompat mLauncherApps;
173 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700176 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700178 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700180 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
181 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700182 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700183 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800184 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200186 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700187 public void bindAppsAdded(ArrayList<Long> newScreens,
188 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700189 ArrayList<ItemInfo> addAnimated,
190 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200191 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700192 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
193 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
194 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700195 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700196 public void bindWorkspaceComponentsRemoved(
197 HashSet<String> packageNames, HashSet<ComponentName> components,
198 UserHandleCompat user);
199 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800200 public void notifyWidgetProvidersChanged();
201 public void bindWidgetsModel(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700202 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700203 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700204 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700205 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chung64359a52013-07-08 17:17:08 -0700208 public interface ItemInfoFilter {
209 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
210 }
211
Bjorn Bringert1307f632013-10-03 22:31:03 +0100212 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800213 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400214 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100215 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700216 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 mIconCache = iconCache;
218
Kenny Guyed131872014-04-30 03:02:21 +0100219 mLauncherApps = LauncherAppsCompat.getInstance(context);
220 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800221 }
222
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700223 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
224 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700226 if (sWorkerThread.getThreadId() == Process.myTid()) {
227 // If we are on the worker thread, post onto the main handler
228 mHandler.post(r);
229 } else {
230 r.run();
231 }
232 }
233
234 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
235 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700236 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700237 if (sWorkerThread.getThreadId() == Process.myTid()) {
238 r.run();
239 } else {
240 // If we are not on the worker thread, then post to the worker handler
241 sWorker.post(r);
242 }
243 }
244
Sunny Goyal756adbc2015-04-16 15:20:51 -0700245 public void setPackageState(final PackageInstallInfo installInfo) {
246 Runnable updateRunnable = new Runnable() {
247
248 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500249 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700250 synchronized (sBgLock) {
251 final HashSet<ItemInfo> updates = new HashSet<>();
252
253 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
254 // Ignore install success events as they are handled by Package add events.
255 return;
256 }
257
Sunny Goyale2df0622015-04-24 11:27:00 -0700258 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700259 if (info instanceof ShortcutInfo) {
260 ShortcutInfo si = (ShortcutInfo) info;
261 ComponentName cn = si.getTargetComponent();
262 if (si.isPromise() && (cn != null)
263 && installInfo.packageName.equals(cn.getPackageName())) {
264 si.setInstallProgress(installInfo.progress);
265
266 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
267 // Mark this info as broken.
268 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
269 }
270 updates.add(si);
271 }
272 }
273 }
274
275 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
276 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
277 widget.installProgress = installInfo.progress;
278 updates.add(widget);
279 }
280 }
281
282 if (!updates.isEmpty()) {
283 // Push changes to the callback.
284 Runnable r = new Runnable() {
285 public void run() {
286 Callbacks callbacks = getCallback();
287 if (callbacks != null) {
288 callbacks.bindRestoreItemsChange(updates);
289 }
290 }
291 };
292 mHandler.post(r);
293 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500294 }
295 }
296 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700297 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500298 }
299
Sunny Goyal756adbc2015-04-16 15:20:51 -0700300 /**
301 * Updates the icons and label of all pending icons for the provided package name.
302 */
303 public void updateSessionDisplayInfo(final String packageName) {
304 Runnable updateRunnable = new Runnable() {
305
306 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700307 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700308 synchronized (sBgLock) {
309 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
310 final UserHandleCompat user = UserHandleCompat.myUserHandle();
311
Sunny Goyale2df0622015-04-24 11:27:00 -0700312 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700313 if (info instanceof ShortcutInfo) {
314 ShortcutInfo si = (ShortcutInfo) info;
315 ComponentName cn = si.getTargetComponent();
316 if (si.isPromise() && (cn != null)
317 && packageName.equals(cn.getPackageName())) {
318 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
319 // For auto install apps update the icon as well as label.
320 mIconCache.getTitleAndIcon(si,
321 si.promisedIntent, user,
322 si.shouldUseLowResIcon());
323 } else {
324 // Only update the icon for restored apps.
325 si.updateIcon(mIconCache);
326 }
327 updates.add(si);
328 }
329 }
330 }
331
332 if (!updates.isEmpty()) {
333 // Push changes to the callback.
334 Runnable r = new Runnable() {
335 public void run() {
336 Callbacks callbacks = getCallback();
337 if (callbacks != null) {
338 callbacks.bindShortcutsChanged(updates,
339 new ArrayList<ShortcutInfo>(), user);
340 }
341 }
342 };
343 mHandler.post(r);
344 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700345 }
346 }
347 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700348 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700349 }
350
Adam Cohen76a47a12014-02-05 11:47:43 -0800351 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800352 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800353
354 if (allAppsApps == null) {
355 throw new RuntimeException("allAppsApps must not be null");
356 }
357 if (allAppsApps.isEmpty()) {
358 return;
359 }
360
361 // Process the newly added applications and add them to the database first
362 Runnable r = new Runnable() {
363 public void run() {
364 runOnMainThread(new Runnable() {
365 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800366 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800367 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500368 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800369 }
370 }
371 });
372 }
373 };
374 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700375 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800376
Sunny Goyala9116722015-04-29 13:55:58 -0700377 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800378 int[] xy, int spanX, int spanY) {
379 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700380 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
381 final int xCount = (int) profile.numColumns;
382 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800383 boolean[][] occupied = new boolean[xCount][yCount];
384 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700385 for (ItemInfo r : occupiedPos) {
386 int right = r.cellX + r.spanX;
387 int bottom = r.cellY + r.spanY;
388 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
389 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800390 occupied[x][y] = true;
391 }
392 }
393 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800394 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700395 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800396 }
397
398 /**
399 * Find a position on the screen for the given size or adds a new screen.
400 * @return screenId and the coordinates for the item.
401 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700402 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700403 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 ArrayList<Long> workspaceScreens,
405 ArrayList<Long> addedWorkspaceScreensFinal,
406 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700407 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800408
Sunny Goyala9116722015-04-29 13:55:58 -0700409 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700410 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700411 synchronized (sBgLock) {
412 for (ItemInfo info : sBgItemsIdMap) {
413 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
414 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
415 if (items == null) {
416 items = new ArrayList<>();
417 screenItems.put(info.screenId, items);
418 }
419 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800420 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800422 }
423
424 // Find appropriate space for the item.
425 long screenId = 0;
426 int[] cordinates = new int[2];
427 boolean found = false;
428
429 int screenCount = workspaceScreens.size();
430 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700431 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 if (preferredScreenIndex < screenCount) {
433 screenId = workspaceScreens.get(preferredScreenIndex);
434 found = findNextAvailableIconSpaceInScreen(
435 screenItems.get(screenId), cordinates, spanX, spanY);
436 }
437
438 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700439 // Search on any of the screens starting from the first screen.
440 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800441 screenId = workspaceScreens.get(screen);
442 if (findNextAvailableIconSpaceInScreen(
443 screenItems.get(screenId), cordinates, spanX, spanY)) {
444 // We found a space for it
445 found = true;
446 break;
447 }
448 }
449 }
450
451 if (!found) {
452 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700453 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
454 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
455 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800456
457 // Save the screen id for binding in the workspace
458 workspaceScreens.add(screenId);
459 addedWorkspaceScreensFinal.add(screenId);
460
461 // If we still can't find an empty space, then God help us all!!!
462 if (!findNextAvailableIconSpaceInScreen(
463 screenItems.get(screenId), cordinates, spanX, spanY)) {
464 throw new RuntimeException("Can't find space to add the item");
465 }
466 }
467 return Pair.create(screenId, cordinates);
468 }
469
470 /**
471 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800472 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700473 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700474 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800475 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800476 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700477 return;
Winson Chung997a9232013-07-24 15:33:46 -0700478 }
Winson Chung64359a52013-07-08 17:17:08 -0700479 // Process the newly added applications and add them to the database first
480 Runnable r = new Runnable() {
481 public void run() {
482 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
483 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
484
Winson Chung76828c82013-08-19 15:43:29 -0700485 // Get the list of workspace screens. We need to append to this list and
486 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
487 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800488 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700489 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800490 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700491 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800492 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700493 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800494 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700495 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800496 }
Winson Chung76828c82013-08-19 15:43:29 -0700497
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700499 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700500 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 long screenId = coords.first;
502 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700503
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700504 ItemInfo itemInfo;
505 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
506 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800507 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700508 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700509 } else {
510 throw new RuntimeException("Unexpected info type");
511 }
Winson Chung94d67682013-09-25 16:29:40 -0700512
Winson Chung64359a52013-07-08 17:17:08 -0700513 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700514 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700515 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700516 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700517 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700518 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700519 }
520 }
521
Winson Chung76828c82013-08-19 15:43:29 -0700522 // Update the workspace screens
523 updateWorkspaceScreenOrder(context, workspaceScreens);
524
Adam Cohen76a47a12014-02-05 11:47:43 -0800525 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700526 runOnMainThread(new Runnable() {
527 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800528 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700529 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700530 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
531 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700532 if (!addedShortcutsFinal.isEmpty()) {
533 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
534 long lastScreenId = info.screenId;
535 for (ItemInfo i : addedShortcutsFinal) {
536 if (i.screenId == lastScreenId) {
537 addAnimated.add(i);
538 } else {
539 addNotAnimated.add(i);
540 }
Winson Chung997a9232013-07-24 15:33:46 -0700541 }
542 }
Winson Chungd64d1762013-08-20 14:37:16 -0700543 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800544 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700545 }
Winson Chung64359a52013-07-08 17:17:08 -0700546 }
Winson Chung997a9232013-07-24 15:33:46 -0700547 });
548 }
Winson Chung64359a52013-07-08 17:17:08 -0700549 }
550 };
551 runOnWorkerThread(r);
552 }
553
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 /**
555 * Adds an item to the DB if it was not created previously, or move it to a new
556 * <container, screen, cellX, cellY>
557 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800558 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700559 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 if (item.container == ItemInfo.NO_ID) {
561 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700562 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 } else {
564 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700565 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 }
567 }
568
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700569 static void checkItemInfoLocked(
570 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
571 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
572 if (modelItem != null && item != modelItem) {
573 // check all the data is consistent
574 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
575 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
576 ShortcutInfo shortcut = (ShortcutInfo) item;
577 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
578 modelShortcut.intent.filterEquals(shortcut.intent) &&
579 modelShortcut.id == shortcut.id &&
580 modelShortcut.itemType == shortcut.itemType &&
581 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700582 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700583 modelShortcut.cellX == shortcut.cellX &&
584 modelShortcut.cellY == shortcut.cellY &&
585 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700586 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700587 // For all intents and purposes, this is the same object
588 return;
589 }
590 }
591
592 // the modelItem needs to match up perfectly with item if our model is
593 // to be consistent with the database-- for now, just require
594 // modelItem == item or the equality check above
595 String msg = "item: " + ((item != null) ? item.toString() : "null") +
596 "modelItem: " +
597 ((modelItem != null) ? modelItem.toString() : "null") +
598 "Error: ItemInfo passed to checkItemInfo doesn't match original";
599 RuntimeException e = new RuntimeException(msg);
600 if (stackTrace != null) {
601 e.setStackTrace(stackTrace);
602 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800603 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700604 }
605 }
606
Michael Jurka816474f2012-06-25 14:49:02 -0700607 static void checkItemInfo(final ItemInfo item) {
608 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
609 final long itemId = item.id;
610 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700611 public void run() {
612 synchronized (sBgLock) {
613 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700614 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700615 }
616 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700617 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700618 }
619
Michael Jurkac9d95c52011-08-29 14:03:34 -0700620 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
621 final ItemInfo item, final String callingFunction) {
622 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700623 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624 final ContentResolver cr = context.getContentResolver();
625
Adam Cohen487f7dd2012-06-28 18:12:10 -0700626 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700627 Runnable r = new Runnable() {
628 public void run() {
629 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700630 updateItemArrays(item, itemId, stackTrace);
631 }
632 };
633 runOnWorkerThread(r);
634 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700636 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
637 final ArrayList<ItemInfo> items, final String callingFunction) {
638 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700639
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
641 Runnable r = new Runnable() {
642 public void run() {
643 ArrayList<ContentProviderOperation> ops =
644 new ArrayList<ContentProviderOperation>();
645 int count = items.size();
646 for (int i = 0; i < count; i++) {
647 ItemInfo item = items.get(i);
648 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700649 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700650 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700651
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700652 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
653 updateItemArrays(item, itemId, stackTrace);
654
655 }
656 try {
657 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
658 } catch (Exception e) {
659 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700660 }
661 }
662 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700663 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700664 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700665
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700666 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
667 // Lock on mBgLock *after* the db operation
668 synchronized (sBgLock) {
669 checkItemInfoLocked(itemId, item, stackTrace);
670
671 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
672 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
673 // Item is in a folder, make sure this folder exists
674 if (!sBgFolders.containsKey(item.container)) {
675 // An items container is being set to a that of an item which is not in
676 // the list of Folders.
677 String msg = "item: " + item + " container being set to: " +
678 item.container + ", not in the list of folders";
679 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700680 }
681 }
682
683 // Items are added/removed from the corresponding FolderInfo elsewhere, such
684 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
685 // that are on the desktop, as appropriate
686 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800687 if (modelItem != null &&
688 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
689 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 switch (modelItem.itemType) {
691 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
692 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
693 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
694 if (!sBgWorkspaceItems.contains(modelItem)) {
695 sBgWorkspaceItems.add(modelItem);
696 }
697 break;
698 default:
699 break;
700 }
701 } else {
702 sBgWorkspaceItems.remove(modelItem);
703 }
704 }
705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700709 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700710 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 item.cellX = cellX;
714 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700715
Winson Chung3d503fb2011-07-13 17:25:49 -0700716 // We store hotseat items in canonical form which is this orientation invariant position
717 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700719 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724
725 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400726 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700727 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
728 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800729 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731
Michael Jurkac9d95c52011-08-29 14:03:34 -0700732 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700733 }
734
735 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
737 * cellX, cellY have already been updated on the ItemInfos.
738 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800739 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 final long container, final int screen) {
741
742 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
743 int count = items.size();
744
745 for (int i = 0; i < count; i++) {
746 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 item.container = container;
748
749 // We store hotseat items in canonical form which is this orientation invariant position
750 // in the hotseat
751 if (context instanceof Launcher && screen < 0 &&
752 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700753 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754 item.cellY);
755 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700756 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700757 }
758
759 final ContentValues values = new ContentValues();
760 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
761 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
762 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800763 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765
766 contentValues.add(values);
767 }
768 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
769 }
770
771 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700772 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800773 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700774 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700776 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800777 item.cellX = cellX;
778 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 item.spanX = spanX;
780 item.spanY = spanY;
781
782 // We store hotseat items in canonical form which is this orientation invariant position
783 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700787 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700789 }
Adam Cohend4844c32011-02-18 19:25:06 -0800790
Adam Cohend4844c32011-02-18 19:25:06 -0800791 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800792 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700793 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
794 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800795 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
797 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800799
Michael Jurka816474f2012-06-25 14:49:02 -0700800 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700802
803 /**
804 * Update an item to the database in a specified container.
805 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700806 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700807 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100808 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700809 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800810 }
811
Sunny Goyal756a28a2015-04-23 17:07:55 -0700812 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700813 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700814 synchronized (mLock) {
815 if (!mHasLoaderCompletedOnce ||
816 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
817 throw new RuntimeException("Trying to add shortcut while loader is running");
818 }
819 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700820 }
821 }
822
Adam Cohend4844c32011-02-18 19:25:06 -0800823 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700824 * Returns true if the shortcuts already exists on the workspace. This must be called after
825 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700827 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
828 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700829 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700830 if (intent.getComponent() != null) {
831 // If component is not null, an intent with null package will produce
832 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700833 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700834 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700835 intentWithPkg = intent.toUri(0);
836 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700837 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700838 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
839 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700840 }
841 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700842 intentWithPkg = intent.toUri(0);
843 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700844 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700845
846 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700847 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700848 if (item instanceof ShortcutInfo) {
849 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700850 Intent targetIntent = info.promisedIntent == null
851 ? info.intent : info.promisedIntent;
852 if (targetIntent != null && info.user.equals(user)) {
853 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700854 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
855 return true;
856 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700857 }
858 }
859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700861 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700862 }
863
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865 * Add an item to the database in a specified container. Sets the container, screen, cellX and
866 * cellY fields of the item. Also assigns an ID to the item.
867 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700868 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700869 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 item.cellX = cellX;
872 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700873 // We store hotseat items in canonical form which is this orientation invariant position
874 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700876 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700879 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881
882 final ContentValues values = new ContentValues();
883 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100884 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Sunny Goyald2497482015-09-22 18:24:19 -0700886 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
887 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
888
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700889 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700890
Jason Monk8e19cf22014-03-20 15:06:57 -0400891 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700892 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700893 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700894 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700896 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700897 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400898 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700899 sBgItemsIdMap.put(item.id, item);
900 switch (item.itemType) {
901 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
902 sBgFolders.put(item.id, (FolderInfo) item);
903 // Fall through
904 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
905 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
906 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
907 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
908 sBgWorkspaceItems.add(item);
909 } else {
910 if (!sBgFolders.containsKey(item.container)) {
911 // Adding an item to a folder that doesn't exist.
912 String msg = "adding item: " + item + " to a folder that " +
913 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700914 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700915 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700916 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700917 break;
918 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
919 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
920 break;
921 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700922 }
923 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700924 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700925 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700926 }
927
Sunny Goyal34942622014-08-29 17:20:55 -0700928 private static ArrayList<ItemInfo> getItemsByPackageName(
929 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700930 ItemInfoFilter filter = new ItemInfoFilter() {
931 @Override
932 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
933 return cn.getPackageName().equals(pn) && info.user.equals(user);
934 }
935 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700936 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700937 }
938
939 /**
940 * Removes all the items from the database corresponding to the specified package.
941 */
942 static void deletePackageFromDatabase(Context context, final String pn,
943 final UserHandleCompat user) {
944 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700945 }
946
947 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700948 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700950 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700951 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
952 items.add(item);
953 deleteItemsFromDatabase(context, items);
954 }
955
956 /**
957 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700958 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700959 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700961 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700962 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700963 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700964 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700965 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700966
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700967 // Lock on mBgLock *after* the db operation
968 synchronized (sBgLock) {
969 switch (item.itemType) {
970 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
971 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700972 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700973 if (info.container == item.id) {
974 // We are deleting a folder which still contains items that
975 // think they are contained by that folder.
976 String msg = "deleting a folder (" + item + ") which still " +
977 "contains items (" + info + ")";
978 Log.e(TAG, msg);
979 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700980 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700981 sBgWorkspaceItems.remove(item);
982 break;
983 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
984 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
985 sBgWorkspaceItems.remove(item);
986 break;
987 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
988 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
989 break;
990 }
991 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700992 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700993 }
994 }
Michael Jurka83df1882011-08-31 20:59:26 -0700995 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700996 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 }
998
999 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 * Update the order of the workspace screens in the database. The array list contains
1001 * a list of screen ids in the order that they should appear.
1002 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001003 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001004 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001005 final ContentResolver cr = context.getContentResolver();
1006 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1007
1008 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001009 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001010 while (iter.hasNext()) {
1011 long id = iter.next();
1012 if (id < 0) {
1013 iter.remove();
1014 }
1015 }
1016
1017 Runnable r = new Runnable() {
1018 @Override
1019 public void run() {
Yura085c8532014-02-11 15:15:29 +00001020 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001021 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001022 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001023 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001024 for (int i = 0; i < count; i++) {
1025 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001026 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001027 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1028 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001029 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001030 }
Yura085c8532014-02-11 15:15:29 +00001031
1032 try {
1033 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1034 } catch (Exception ex) {
1035 throw new RuntimeException(ex);
1036 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001037
Winson Chungba9c37f2013-08-30 14:11:37 -07001038 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001039 sBgWorkspaceScreens.clear();
1040 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001041 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001042 }
1043 };
1044 runOnWorkerThread(r);
1045 }
1046
1047 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001048 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 */
Winsonc0b52fe2015-09-09 16:38:15 -07001050 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 final ContentResolver cr = context.getContentResolver();
1052
Michael Jurkac9d95c52011-08-29 14:03:34 -07001053 Runnable r = new Runnable() {
1054 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001055 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001057 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 sBgItemsIdMap.remove(info.id);
1059 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001060 sBgWorkspaceItems.remove(info);
1061 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001062
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001063 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001064 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001066 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001067 for (ItemInfo childInfo : info.contents) {
1068 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001069 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001070 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001071 }
1072 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001073 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 }
1075
1076 /**
1077 * Set this as the current Launcher activity object for the loader.
1078 */
1079 public void initialize(Callbacks callbacks) {
1080 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001081 Preconditions.assertUIThread();
1082 // Remove any queued UI runnables
1083 mHandler.cancelAll();
1084 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 }
1086 }
1087
Kenny Guyed131872014-04-30 03:02:21 +01001088 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001089 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001090 int op = PackageUpdatedTask.OP_UPDATE;
1091 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1092 user));
1093 }
1094
1095 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001096 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001097 int op = PackageUpdatedTask.OP_REMOVE;
1098 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1099 user));
1100 }
1101
1102 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001103 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001104 int op = PackageUpdatedTask.OP_ADD;
1105 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1106 user));
1107 }
1108
1109 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001110 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001111 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001112 enqueuePackageUpdated(
1113 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001114 }
1115
1116 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001117 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001118 boolean replacing) {
1119 if (!replacing) {
1120 enqueuePackageUpdated(new PackageUpdatedTask(
1121 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1122 user));
1123 }
Kenny Guyed131872014-04-30 03:02:21 +01001124 }
1125
Kenny Guy44cba692016-01-21 19:50:02 +00001126 @Override
1127 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1128 enqueuePackageUpdated(new PackageUpdatedTask(
1129 PackageUpdatedTask.OP_SUSPEND, packageNames,
1130 user));
1131 }
1132
1133 @Override
1134 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1135 enqueuePackageUpdated(new PackageUpdatedTask(
1136 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1137 user));
1138 }
1139
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001140 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1142 * ACTION_PACKAGE_CHANGED.
1143 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001144 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001145 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001146 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001147
Joe Onorato36115782010-06-17 13:28:48 -04001148 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001149 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001150 // If we have changed locale we need to clear out the labels in all apps/workspace.
1151 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001152 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001153 Callbacks callbacks = getCallback();
1154 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001155 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001156 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001157 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001158 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001159 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001160 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001161 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1162 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001163 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1164 if (user != null) {
1165 enqueuePackageUpdated(new PackageUpdatedTask(
1166 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1167 new String[0], user));
1168 }
Tony Wickham827cef22016-03-17 15:39:39 -07001169 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1170 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001171 }
1172 }
1173
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001174 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001175 resetLoadedState(true, true);
1176
Reena Lee93f824a2011-09-23 17:20:28 -07001177 // Do this here because if the launcher activity is running it will be restarted.
1178 // If it's not running startLoaderFromBackground will merely tell it that it needs
1179 // to reload.
1180 startLoaderFromBackground();
1181 }
1182
Winson Chungf0c6ae02012-03-21 16:10:31 -07001183 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1184 synchronized (mLock) {
1185 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1186 // mWorkspaceLoaded to true later
1187 stopLoaderLocked();
1188 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1189 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1190 }
1191 }
1192
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001193 /**
1194 * When the launcher is in the background, it's possible for it to miss paired
1195 * configuration changes. So whenever we trigger the loader from the background
1196 * tell the launcher that it needs to re-run the loader when it comes back instead
1197 * of doing it now.
1198 */
1199 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001200 Callbacks callbacks = getCallback();
1201 if (callbacks != null) {
1202 // Only actually run the loader if they're not paused.
1203 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001204 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001205 }
1206 }
Joe Onorato36115782010-06-17 13:28:48 -04001207 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001208
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001209 /**
1210 * If there is already a loader task running, tell it to stop.
1211 */
1212 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001213 LoaderTask oldTask = mLoaderTask;
1214 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001215 oldTask.stopLocked();
1216 }
Reena Lee93f824a2011-09-23 17:20:28 -07001217 }
1218
Adam Cohen1a85c582014-09-30 09:48:49 -07001219 public boolean isCurrentCallbacks(Callbacks callbacks) {
1220 return (mCallbacks != null && mCallbacks.get() == callbacks);
1221 }
1222
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001223 public void startLoader(int synchronousBindPage) {
1224 startLoader(synchronousBindPage, LOADER_FLAG_NONE);
Dan Sandlerd5024042014-01-09 15:01:33 -05001225 }
1226
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001227 public void startLoader(int synchronousBindPage, int loadFlags) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001228 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1229 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001230 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001231 // Don't bother to start the thread if we know it's not going to do anything
1232 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001233 final Callbacks oldCallbacks = mCallbacks.get();
1234 // Clear any pending bind-runnables from the synchronized load process.
1235 runOnMainThread(new Runnable() {
1236 public void run() {
1237 oldCallbacks.clearPendingBinds();
1238 }
1239 });
1240
Joe Onorato36115782010-06-17 13:28:48 -04001241 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001242 stopLoaderLocked();
Sunny Goyal93f878c2016-03-30 17:31:24 -07001243 mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags, synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001244 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001245 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001246 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1247 } else {
1248 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1249 sWorker.post(mLoaderTask);
1250 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001251 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 }
1253 }
1254
Joe Onorato36115782010-06-17 13:28:48 -04001255 public void stopLoader() {
1256 synchronized (mLock) {
1257 if (mLoaderTask != null) {
1258 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001259 }
1260 }
Joe Onorato36115782010-06-17 13:28:48 -04001261 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001263 /**
1264 * Loads the workspace screen ids in an ordered list.
1265 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001266 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001267 final ContentResolver contentResolver = context.getContentResolver();
1268 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001269
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001270 // Get screens ordered by rank.
1271 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1272 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1273 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001274 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001275 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001276 while (sc.moveToNext()) {
1277 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001278 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001279 } catch (Exception e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001280 FileLog.d(TAG, "Invalid screen id", e);
Winson Chung76828c82013-08-19 15:43:29 -07001281 }
1282 }
1283 } finally {
1284 sc.close();
1285 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001286 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001287 }
1288
Joe Onorato36115782010-06-17 13:28:48 -04001289 /**
1290 * Runnable for the thread that loads the contents of the launcher:
1291 * - workspace icons
1292 * - widgets
1293 * - all apps icons
1294 */
1295 private class LoaderTask implements Runnable {
1296 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001297 private int mPageToBindFirst;
1298
Adam Cohen091440a2015-03-18 14:16:05 -07001299 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001300 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001301 @Thunk boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001302 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001303
Sunny Goyal93f878c2016-03-30 17:31:24 -07001304 LoaderTask(Context context, int flags, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001305 mContext = context;
Dan Sandlerd5024042014-01-09 15:01:33 -05001306 mFlags = flags;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001307 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
1309
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001310 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001311 mIsLoadingAndBindingWorkspace = true;
1312
Joe Onorato36115782010-06-17 13:28:48 -04001313 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001314 if (DEBUG_LOADERS) {
1315 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001317
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001318 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001319 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001320 synchronized (LoaderTask.this) {
1321 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001322 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001323 }
1324 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 }
1326 }
1327
Joe Onorato36115782010-06-17 13:28:48 -04001328 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001329 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001330 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001331
Joe Onorato36115782010-06-17 13:28:48 -04001332 private void waitForIdle() {
1333 // Wait until the either we're stopped or the other threads are done.
1334 // This way we don't start loading all apps until the workspace has settled
1335 // down.
1336 synchronized (LoaderTask.this) {
1337 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001338
Joe Onorato36115782010-06-17 13:28:48 -04001339 mHandler.postIdle(new Runnable() {
1340 public void run() {
1341 synchronized (LoaderTask.this) {
1342 mLoadAndBindStepFinished = true;
1343 if (DEBUG_LOADERS) {
1344 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001345 }
Joe Onorato36115782010-06-17 13:28:48 -04001346 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001347 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001348 }
Joe Onorato36115782010-06-17 13:28:48 -04001349 });
1350
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001351 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001352 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001353 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1354 // wait no longer than 1sec at a time
1355 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001356 } catch (InterruptedException ex) {
1357 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001358 }
1359 }
Joe Onorato36115782010-06-17 13:28:48 -04001360 if (DEBUG_LOADERS) {
1361 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001362 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001363 + "ms for previous step to finish binding");
1364 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001365 }
Joe Onorato36115782010-06-17 13:28:48 -04001366 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001367
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001368 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001369 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001370 // Ensure that we have a valid page index to load synchronously
1371 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1372 "valid page index");
1373 }
1374 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1375 // Ensure that we don't try and bind a specified page when the pages have not been
1376 // loaded already (we should load everything asynchronously in that case)
1377 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1378 }
1379 synchronized (mLock) {
1380 if (mIsLoaderTaskRunning) {
1381 // Ensure that we are never running the background loading at this point since
1382 // we also touch the background collections
1383 throw new RuntimeException("Error! Background loading is already running");
1384 }
1385 }
1386
1387 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1388 // data structures, we can't allow any other thread to touch that data, but because
1389 // this call is synchronous, we can get away with not locking).
1390
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001391 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001392 // operations from the previous activity. We need to ensure that all queued operations
1393 // are executed before any synchronous binding work is done.
1394 mHandler.flush();
1395
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001396 // Divide the set of loaded items into those that we are binding synchronously, and
1397 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001398 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001399 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1400 // arise from that.
1401 onlyBindAllApps();
1402 }
1403
Joe Onorato36115782010-06-17 13:28:48 -04001404 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001405 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001406 if (mStopped) {
1407 return;
1408 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 mIsLoaderTaskRunning = true;
1410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 // Optimize for end-user experience: if the Launcher is up and // running with the
1412 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1413 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001414 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001415 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001416 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001417
Joe Onorato36115782010-06-17 13:28:48 -04001418 if (mStopped) {
1419 break keep_running;
1420 }
1421
Joe Onorato36115782010-06-17 13:28:48 -04001422 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001423
1424 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001425 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1426 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001427 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001428
Joe Onorato36115782010-06-17 13:28:48 -04001429 // Clear out this reference, otherwise we end up holding it until all of the
1430 // callback runnables are done.
1431 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432
Joe Onorato36115782010-06-17 13:28:48 -04001433 synchronized (mLock) {
1434 // If we are still the last one to be scheduled, remove ourselves.
1435 if (mLoaderTask == this) {
1436 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001438 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001439 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001440 }
Joe Onorato36115782010-06-17 13:28:48 -04001441 }
1442
1443 public void stopLocked() {
1444 synchronized (LoaderTask.this) {
1445 mStopped = true;
1446 this.notify();
1447 }
1448 }
1449
1450 /**
1451 * Gets the callbacks object. If we've been stopped, or if the launcher object
1452 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1453 * object that was around when the deferred message was scheduled, and if there's
1454 * a new Callbacks object around then also return null. This will save us from
1455 * calling onto it with data that will be ignored.
1456 */
1457 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1458 synchronized (mLock) {
1459 if (mStopped) {
1460 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001461 }
Joe Onorato36115782010-06-17 13:28:48 -04001462
1463 if (mCallbacks == null) {
1464 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001465 }
Joe Onorato36115782010-06-17 13:28:48 -04001466
1467 final Callbacks callbacks = mCallbacks.get();
1468 if (callbacks != oldCallbacks) {
1469 return null;
1470 }
1471 if (callbacks == null) {
1472 Log.w(TAG, "no mCallbacks");
1473 return null;
1474 }
1475
1476 return callbacks;
1477 }
1478 }
1479
1480 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001481 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1482 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001483 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001484 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001485 final int countX = profile.numColumns;
1486 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001487
Adam Cohendcd297f2013-06-18 13:13:40 -07001488 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001489 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001490 // Return early if we detect that an item is under the hotseat button
1491 if (mCallbacks == null ||
1492 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001493 Log.e(TAG, "Error loading shortcut into hotseat " + item
1494 + " into position (" + item.screenId + ":" + item.cellX + ","
1495 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001496 return false;
1497 }
1498
Dan Sandler295ae182013-12-10 16:05:47 -05001499 final ItemInfo[][] hotseatItems =
1500 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1501
Adam Cohen2e6da152015-05-06 11:42:25 -07001502 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001503 Log.e(TAG, "Error loading shortcut " + item
1504 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001505 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001506 + ")");
1507 return false;
1508 }
1509
Dan Sandler295ae182013-12-10 16:05:47 -05001510 if (hotseatItems != null) {
1511 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 Log.e(TAG, "Error loading shortcut into hotseat " + item
1513 + " into position (" + item.screenId + ":" + item.cellX + ","
1514 + item.cellY + ") occupied by "
1515 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1516 [(int) item.screenId][0]);
1517 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001518 } else {
1519 hotseatItems[(int) item.screenId][0] = item;
1520 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001521 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001522 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001523 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 items[(int) item.screenId][0] = item;
1525 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001526 return true;
1527 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001528 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1529 if (!workspaceScreens.contains((Long) item.screenId)) {
1530 // The item has an invalid screen id.
1531 return false;
1532 }
1533 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001534 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001535 return true;
1536 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001537
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001539 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 occupied.put(item.screenId, items);
1541 }
1542
Dan Sandler295ae182013-12-10 16:05:47 -05001543 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001544 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1545 item.cellX < 0 || item.cellY < 0 ||
1546 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1547 Log.e(TAG, "Error loading shortcut " + item
1548 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1549 + item.cellX + "," + item.cellY
1550 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1551 return false;
1552 }
1553
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001554 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001555 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1556 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001558 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001560 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001561 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001563 return false;
1564 }
1565 }
1566 }
1567 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1568 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001570 }
1571 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001572
Joe Onorato36115782010-06-17 13:28:48 -04001573 return true;
1574 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001575
Winson Chungba9c37f2013-08-30 14:11:37 -07001576 /** Clears all the sBg data structures */
1577 private void clearSBgDataStructures() {
1578 synchronized (sBgLock) {
1579 sBgWorkspaceItems.clear();
1580 sBgAppWidgets.clear();
1581 sBgFolders.clear();
1582 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001583 sBgWorkspaceScreens.clear();
1584 }
1585 }
1586
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001587 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001588 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001589
Joe Onorato36115782010-06-17 13:28:48 -04001590 final Context context = mContext;
1591 final ContentResolver contentResolver = context.getContentResolver();
1592 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001593 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001594 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001595 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001596
Winson Chung892c74d2013-08-22 16:15:50 -07001597 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001598 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001599 int countX = profile.numColumns;
1600 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001601
Sunny Goyalf076eae2016-01-11 12:25:10 -08001602 if (GridSizeMigrationTask.ENABLED &&
1603 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1604 // Migration failed. Clear workspace.
1605 mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001606 }
1607
Dan Sandlerd5024042014-01-09 15:01:33 -05001608 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
Sunny Goyala1365452015-10-01 15:46:24 -07001609 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001610 LauncherSettings.Settings.call(contentResolver,
1611 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001612 }
1613
1614 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1615 // append the user's Launcher2 shortcuts
Sunny Goyala1365452015-10-01 15:46:24 -07001616 Log.d(TAG, "loadWorkspace: migrating from launcher2");
Sunny Goyald2497482015-09-22 18:24:19 -07001617 LauncherSettings.Settings.call(contentResolver,
1618 LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS);
Dan Sandlerd5024042014-01-09 15:01:33 -05001619 } else {
1620 // Make sure the default workspace is loaded
Sunny Goyala1365452015-10-01 15:46:24 -07001621 Log.d(TAG, "loadWorkspace: loading default favorites");
Sunny Goyald2497482015-09-22 18:24:19 -07001622 LauncherSettings.Settings.call(contentResolver,
1623 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Dan Sandlerd5024042014-01-09 15:01:33 -05001624 }
Adam Cohene25af792013-06-06 23:08:25 -07001625
Winson Chung2abf94d2012-07-18 18:16:38 -07001626 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001627 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001628 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001629 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001630 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001631
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001632 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1633 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001634 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001635 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001636 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001637
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001638 // +1 for the hotseat (it can be larger than the workspace)
1639 // Load workspace in reverse order to ensure that latest items are loaded first (and
1640 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001641 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001642 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001643
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001644 try {
1645 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1646 final int intentIndex = c.getColumnIndexOrThrow
1647 (LauncherSettings.Favorites.INTENT);
1648 final int titleIndex = c.getColumnIndexOrThrow
1649 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001650 final int containerIndex = c.getColumnIndexOrThrow(
1651 LauncherSettings.Favorites.CONTAINER);
1652 final int itemTypeIndex = c.getColumnIndexOrThrow(
1653 LauncherSettings.Favorites.ITEM_TYPE);
1654 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1655 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001656 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1657 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001658 final int screenIndex = c.getColumnIndexOrThrow(
1659 LauncherSettings.Favorites.SCREEN);
1660 final int cellXIndex = c.getColumnIndexOrThrow
1661 (LauncherSettings.Favorites.CELLX);
1662 final int cellYIndex = c.getColumnIndexOrThrow
1663 (LauncherSettings.Favorites.CELLY);
1664 final int spanXIndex = c.getColumnIndexOrThrow
1665 (LauncherSettings.Favorites.SPANX);
1666 final int spanYIndex = c.getColumnIndexOrThrow(
1667 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001668 final int rankIndex = c.getColumnIndexOrThrow(
1669 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001670 final int restoredIndex = c.getColumnIndexOrThrow(
1671 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001672 final int profileIdIndex = c.getColumnIndexOrThrow(
1673 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001674 final int optionsIndex = c.getColumnIndexOrThrow(
1675 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001676 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677
Sunny Goyal7f834d22015-04-21 10:10:23 -07001678 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001679 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001680 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001681 long serialNo = mUserManager.getSerialNumberForUser(user);
1682 allUsers.put(serialNo, user);
1683 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001684 }
1685
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001686 ShortcutInfo info;
1687 String intentDescription;
1688 LauncherAppWidgetInfo appWidgetInfo;
1689 int container;
1690 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001691 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001692 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001693 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001694 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001695
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001696 while (!mStopped && c.moveToNext()) {
1697 try {
1698 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001699 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001700 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001701 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 switch (itemType) {
1704 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1705 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001706 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001708 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001709 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001710 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001711 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001712 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001713 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001714 if (user == null) {
1715 // User has been deleted remove the item.
1716 itemsToRemove.add(id);
1717 continue;
1718 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 try {
1720 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001721 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001722 if (cn != null && cn.getPackageName() != null) {
1723 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1724 cn.getPackageName(), user);
1725 boolean validComponent = validPkg &&
1726 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001727 if (validPkg) {
1728 targetPackage = cn.getPackageName();
1729 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001730
1731 if (validComponent) {
1732 if (restored) {
1733 // no special handling necessary for this item
1734 restoredRows.add(id);
1735 restored = false;
1736 }
Kenny Guyff05f432016-01-22 17:48:29 +00001737 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001738 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001739 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001740 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001741 intent = null;
1742 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1743 // We allow auto install apps to have their intent
1744 // updated after an install.
1745 intent = manager.getLaunchIntentForPackage(
1746 cn.getPackageName());
1747 if (intent != null) {
1748 ContentValues values = new ContentValues();
1749 values.put(LauncherSettings.Favorites.INTENT,
1750 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001751 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001752 }
1753 }
1754
1755 if (intent == null) {
1756 // The app is installed but the component is no
1757 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001758 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001759 itemsToRemove.add(id);
1760 continue;
1761 } else {
1762 // no special handling necessary for this item
1763 restoredRows.add(id);
1764 restored = false;
1765 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001766 } else if (restored) {
1767 // Package is not yet available but might be
1768 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001769 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001770
1771 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1772 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001773 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001774 // App restore has started. Update the flag
1775 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1776 ContentValues values = new ContentValues();
1777 values.put(LauncherSettings.Favorites.RESTORED,
1778 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001779 updateItem(id, values);
1780 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1781 // This is a common app. Try to replace this.
1782 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1783 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1784 if (parser.findDefaultApp()) {
1785 // Default app found. Replace it.
1786 intent = parser.parsedIntent;
1787 cn = intent.getComponent();
1788 ContentValues values = parser.parsedValues;
1789 values.put(LauncherSettings.Favorites.RESTORED, 0);
1790 updateItem(id, values);
1791 restored = false;
1792 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001793
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001794 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001795 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001796 itemsToRemove.add(id);
1797 continue;
1798 }
Sunny Goyal94485362014-09-18 16:13:58 -07001799 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001800 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001801 itemsToRemove.add(id);
1802 continue;
1803 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001804 } else if (PackageManagerHelper.isAppOnSdcard(
1805 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001806 // Package is present but not available.
1807 allowMissingTarget = true;
1808 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1809 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001810 // SdCard is not ready yet. Package might get available,
1811 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001812 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001813 HashSet<String> pkgs = sPendingPackages.get(user);
1814 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001815 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001816 sPendingPackages.put(user, pkgs);
1817 }
1818 pkgs.add(cn.getPackageName());
1819 allowMissingTarget = true;
1820 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001821
1822 } else {
1823 // Do not wait for external media load anymore.
1824 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001825 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001826 itemsToRemove.add(id);
1827 continue;
Winson Chungee055712013-07-30 14:46:24 -07001828 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001829 } else if (cn == null) {
1830 // For shortcuts with no component, keep them as they are
1831 restoredRows.add(id);
1832 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001833 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001835 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001836 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 continue;
1838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839
Sunny Goyal34b65272015-03-11 16:56:52 -07001840 boolean useLowResIcon = container >= 0 &&
1841 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1842
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001843 if (itemReplaced) {
1844 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001845 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001846 cursorIconInfo.iconIndex, titleIndex,
1847 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001848 } else {
1849 // Don't replace items for other profiles.
1850 itemsToRemove.add(id);
1851 continue;
1852 }
1853 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001854 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001855 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001856 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001857 intent = getRestoredItemIntent(c, context, intent);
1858 } else {
1859 // Don't restore items for other profiles.
1860 itemsToRemove.add(id);
1861 continue;
1862 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001863 } else if (itemType ==
1864 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001865 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001866 cursorIconInfo.iconIndex, titleIndex,
1867 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001869 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001870
Sunny Goyald09c3702016-04-06 16:18:20 -07001871 // Shortcuts are only available on the primary profile
1872 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1873 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1874 }
1875
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 // App shortcuts that used to be automatically added to Launcher
1877 // didn't always have the correct intent flags set, so do that
1878 // here
1879 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001880 intent.getCategories() != null &&
1881 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001882 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 intent.addFlags(
1884 Intent.FLAG_ACTIVITY_NEW_TASK |
1885 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1886 }
Michael Jurka96879562012-03-22 05:54:33 -07001887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001888
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001890 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001893 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 info.cellX = c.getInt(cellXIndex);
1895 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001896 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001897 info.spanX = 1;
1898 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001899 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001900 if (info.promisedIntent != null) {
1901 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1902 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001903 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001904 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1905 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1906 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001907
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001909 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001910 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001911 break;
1912 }
1913
Sunny Goyal756adbc2015-04-16 15:20:51 -07001914 if (restored) {
1915 ComponentName cn = info.getTargetComponent();
1916 if (cn != null) {
1917 Integer progress = installingPkgs.get(cn.getPackageName());
1918 if (progress != null) {
1919 info.setInstallProgress(progress);
1920 } else {
1921 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1922 }
1923 }
1924 }
1925
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 switch (container) {
1927 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1928 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1929 sBgWorkspaceItems.add(info);
1930 break;
1931 default:
1932 // Item is in a user folder
1933 FolderInfo folderInfo =
1934 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07001935 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001936 break;
1937 }
1938 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001939 } else {
1940 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001941 }
1942 break;
1943
1944 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1945 id = c.getLong(idIndex);
1946 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1947
Sunny Goyala508e4f2015-05-21 09:33:57 -07001948 // Do not trim the folder label, as is was set by the user.
1949 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001951 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001953 folderInfo.cellX = c.getInt(cellXIndex);
1954 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001955 folderInfo.spanX = 1;
1956 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001957 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001958
Daniel Sandler8802e962010-05-26 16:28:16 -04001959 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001960 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001961 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001962 break;
1963 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001964
Joe Onorato9c1289c2009-08-17 11:03:03 -04001965 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1967 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1968 sBgWorkspaceItems.add(folderInfo);
1969 break;
Joe Onorato36115782010-06-17 13:28:48 -04001970 }
Joe Onorato17a89222011-02-08 17:26:11 -08001971
Chris Wrenf4d08112014-01-16 18:13:56 -05001972 if (restored) {
1973 // no special handling required for restored folders
1974 restoredRows.add(id);
1975 }
1976
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1978 sBgFolders.put(folderInfo.id, folderInfo);
1979 break;
1980
1981 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001982 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001984 boolean customWidget = itemType ==
1985 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1986
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001987 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001988 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001989 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001990 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001991 user = allUsers.get(serialNumber);
1992 if (user == null) {
1993 itemsToRemove.add(id);
1994 continue;
1995 }
1996
Sunny Goyalff572272014-07-23 13:58:07 -07001997 final ComponentName component =
1998 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001999
Sunny Goyal651077b2014-06-30 14:15:31 -07002000 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002001 final boolean isIdValid = (restoreStatus &
2002 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002003 final boolean wasProviderReady = (restoreStatus &
2004 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002005
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002006 if (widgetProvidersMap == null) {
2007 widgetProvidersMap = AppWidgetManagerCompat
2008 .getInstance(mContext).getAllProvidersMap();
2009 }
2010 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2011 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002012 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002013 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002014
2015 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002016 if (!isSafeMode && !customWidget &&
2017 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002018 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002019 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 itemsToRemove.add(id);
2021 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002022 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002023 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2024 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002025
Sunny Goyal53f96722015-07-13 19:54:53 -07002026 // The provider is available. So the widget is either
2027 // available or not available. We do not need to track
2028 // any future restore updates.
2029 int status = restoreStatus &
2030 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002031 if (!wasProviderReady) {
2032 // If provider was not previously ready, update the
2033 // status and UI flag.
2034
2035 // Id would be valid only if the widget restore broadcast was received.
2036 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002037 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002038 } else {
2039 status &= ~LauncherAppWidgetInfo
2040 .FLAG_PROVIDER_NOT_READY;
2041 }
2042 }
2043 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002044 } else {
2045 Log.v(TAG, "Widget restore pending id=" + id
2046 + " appWidgetId=" + appWidgetId
2047 + " status =" + restoreStatus);
2048 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002049 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002050 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002051 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002052
2053 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2054 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002055 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002056 // App restore has started. Update the flag
2057 appWidgetInfo.restoreStatus |=
2058 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002059 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002060 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002061 itemsToRemove.add(id);
2062 continue;
2063 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002064
2065 appWidgetInfo.installProgress =
2066 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002067 }
Sunny Goyalff572272014-07-23 13:58:07 -07002068
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002070 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 appWidgetInfo.cellX = c.getInt(cellXIndex);
2072 appWidgetInfo.cellY = c.getInt(cellYIndex);
2073 appWidgetInfo.spanX = c.getInt(spanXIndex);
2074 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002075 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002076
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2078 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2079 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002080 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2081 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 continue;
2083 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002085 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002087 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002088 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 break;
2090 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002091
Adam Cohen59400422014-03-05 18:07:04 -08002092 if (!customWidget) {
2093 String providerName =
2094 appWidgetInfo.providerName.flattenToString();
2095 if (!providerName.equals(savedProvider) ||
2096 (appWidgetInfo.restoreStatus != restoreStatus)) {
2097 ContentValues values = new ContentValues();
2098 values.put(
2099 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2100 providerName);
2101 values.put(LauncherSettings.Favorites.RESTORED,
2102 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002103 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002104 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002105 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2107 sBgAppWidgets.add(appWidgetInfo);
2108 }
Joe Onorato36115782010-06-17 13:28:48 -04002109 break;
2110 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002111 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002112 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002113 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 }
2115 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002116 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 }
2118
Winson Chungba9c37f2013-08-30 14:11:37 -07002119 // Break early if we've stopped loading
2120 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002121 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002122 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002123 }
2124
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002127 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2128 Utilities.createDbSelectionQuery(
2129 LauncherSettings.Favorites._ID, itemsToRemove), null);
2130 if (DEBUG_LOADERS) {
2131 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2132 LauncherSettings.Favorites._ID, itemsToRemove));
2133 }
2134
2135 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002136 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2137 .call(contentResolver,
2138 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2139 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2140 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002141 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2142 sBgFolders.remove(folderId);
2143 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002144 }
2145 }
2146
Sunny Goyal317698b2015-07-29 11:45:41 -07002147 // Sort all the folder items and make sure the first 3 items are high resolution.
2148 for (FolderInfo folder : sBgFolders) {
2149 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2150 int pos = 0;
2151 for (ShortcutInfo info : folder.contents) {
2152 if (info.usingLowResIcon) {
2153 info.updateIcon(mIconCache, false);
2154 }
2155 pos ++;
2156 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2157 break;
2158 }
2159 }
2160 }
2161
Chris Wrenf4d08112014-01-16 18:13:56 -05002162 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002163 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002164 ContentValues values = new ContentValues();
2165 values.put(LauncherSettings.Favorites.RESTORED, 0);
2166 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2167 Utilities.createDbSelectionQuery(
2168 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002169 }
2170
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002171 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2172 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002173 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002174 null, sWorker);
2175 }
2176
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002177 // Remove any empty screens
2178 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002179 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002180 long screenId = item.screenId;
2181 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2182 unusedScreens.contains(screenId)) {
2183 unusedScreens.remove(screenId);
2184 }
2185 }
2186
2187 // If there are any empty screens remove them, and update.
2188 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002189 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002190 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002191 }
2192
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 if (DEBUG_LOADERS) {
2194 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2195 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002196 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002197 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002198 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002199
Sunny Goyale2df0622015-04-24 11:27:00 -07002200 for (int i = 0; i < nScreens; i++) {
2201 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002202 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 line += " | ";
2204 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002205 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002206 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002207 if (x < screen.length && y < screen[x].length) {
2208 line += (screen[x][y] != null) ? "#" : ".";
2209 } else {
2210 line += "!";
2211 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 }
Joe Onorato36115782010-06-17 13:28:48 -04002213 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002215 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 }
Joe Onorato36115782010-06-17 13:28:48 -04002217 }
Adam Cohene25af792013-06-06 23:08:25 -07002218 }
2219
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002220 /**
2221 * Partially updates the item without any notification. Must be called on the worker thread.
2222 */
2223 private void updateItem(long itemId, ContentValues update) {
2224 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002225 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002226 update,
2227 BaseColumns._ID + "= ?",
2228 new String[]{Long.toString(itemId)});
2229 }
2230
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 /** Filters the set of items who are directly or indirectly (via another container) on the
2232 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002233 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 ArrayList<ItemInfo> allWorkspaceItems,
2235 ArrayList<ItemInfo> currentScreenItems,
2236 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002237 // Purge any null ItemInfos
2238 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2239 while (iter.hasNext()) {
2240 ItemInfo i = iter.next();
2241 if (i == null) {
2242 iter.remove();
2243 }
2244 }
2245
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 // Order the set of items by their containers first, this allows use to walk through the
2247 // list sequentially, build up a list of containers that are in the specified screen,
2248 // as well as all items in those containers.
2249 Set<Long> itemsOnScreen = new HashSet<Long>();
2250 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2251 @Override
2252 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002253 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254 }
2255 });
2256 for (ItemInfo info : allWorkspaceItems) {
2257 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002258 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 currentScreenItems.add(info);
2260 itemsOnScreen.add(info.id);
2261 } else {
2262 otherScreenItems.add(info);
2263 }
2264 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2265 currentScreenItems.add(info);
2266 itemsOnScreen.add(info.id);
2267 } else {
2268 if (itemsOnScreen.contains(info.container)) {
2269 currentScreenItems.add(info);
2270 itemsOnScreen.add(info.id);
2271 } else {
2272 otherScreenItems.add(info);
2273 }
2274 }
2275 }
2276 }
2277
2278 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002279 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 ArrayList<LauncherAppWidgetInfo> appWidgets,
2281 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2282 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283
2284 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002285 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002287 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288 currentScreenWidgets.add(widget);
2289 } else {
2290 otherScreenWidgets.add(widget);
2291 }
2292 }
2293 }
2294
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2296 * right) */
2297 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002298 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002299 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002300 final int screenCols = profile.numColumns;
2301 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002303 @Override
2304 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002305 if (lhs.container == rhs.container) {
2306 // Within containers, order by their spatial position in that container
2307 switch ((int) lhs.container) {
2308 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2309 long lr = (lhs.screenId * screenCellCount +
2310 lhs.cellY * screenCols + lhs.cellX);
2311 long rr = (rhs.screenId * screenCellCount +
2312 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002313 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002314 }
2315 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2316 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002317 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002318 }
2319 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002320 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002321 throw new RuntimeException("Unexpected container type when " +
2322 "sorting workspace items.");
2323 }
2324 return 0;
2325 }
2326 } else {
2327 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002328 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002329 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002330 }
2331 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002333
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2335 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002336 final Runnable r = new Runnable() {
2337 @Override
2338 public void run() {
2339 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2340 if (callbacks != null) {
2341 callbacks.bindScreens(orderedScreens);
2342 }
2343 }
2344 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002345 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002346 }
2347
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2349 final ArrayList<ItemInfo> workspaceItems,
2350 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002351 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352
2353 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002354 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002356 final int start = i;
2357 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 final Runnable r = new Runnable() {
2359 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002360 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002361 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002362 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002363 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2364 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002365 }
2366 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002367 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002368 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002369 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002371 // Bind the widgets, one at a time
2372 N = appWidgets.size();
2373 for (int i = 0; i < N; i++) {
2374 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2375 final Runnable r = new Runnable() {
2376 public void run() {
2377 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2378 if (callbacks != null) {
2379 callbacks.bindAppWidget(widget);
2380 }
2381 }
2382 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002383 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 }
2385 }
2386
2387 /**
2388 * Binds all loaded data to actual views on the main thread.
2389 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002390 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 final long t = SystemClock.uptimeMillis();
2392 Runnable r;
2393
2394 // Don't use these two variables in any of the callback runnables.
2395 // Otherwise we hold a reference to them.
2396 final Callbacks oldCallbacks = mCallbacks.get();
2397 if (oldCallbacks == null) {
2398 // This launcher has exited and nobody bothered to tell us. Just bail.
2399 Log.w(TAG, "LoaderTask running with no launcher");
2400 return;
2401 }
2402
Winson Chung9b9fb962013-11-15 15:39:34 -08002403 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002404 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2405 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2406 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002407
Winson Chung2abf94d2012-07-18 18:16:38 -07002408 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 workspaceItems.addAll(sBgWorkspaceItems);
2410 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002411 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412 }
2413
Derek Prothro7aff3992013-12-10 14:00:37 -05002414 final boolean isLoadingSynchronously =
2415 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002416 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002417 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002418 if (currScreen >= orderedScreenIds.size()) {
2419 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002420 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002421 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002422 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002423 final long currentScreenId = currentScreen < 0
2424 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002425
Winson Chung9b9fb962013-11-15 15:39:34 -08002426 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002427 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2428 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2429 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2430 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431
Winson Chung9b9fb962013-11-15 15:39:34 -08002432 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002434 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002435 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2437 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2438
2439 // Tell the workspace that we're about to start binding items
2440 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002441 public void run() {
2442 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2443 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002444 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002446 }
2447 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002449 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450
Adam Cohendcd297f2013-06-18 13:13:40 -07002451 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2452
Sunny Goyal527c7d32015-08-28 15:19:36 -07002453 Executor mainExecutor = new DeferredMainThreadExecutor();
2454 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002455 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456
Sunny Goyal527c7d32015-08-28 15:19:36 -07002457 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2458 // remaining screens after first onDraw is called. This ensures that the first screen
2459 // is immediately visible (eg. during rotation)
2460 // In case of !isLoadingSynchronously, bind all pages one after other.
2461 final Executor deferredExecutor = isLoadingSynchronously ?
2462 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2463
Sunny Goyal44c06432016-04-02 10:56:02 -07002464 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002465
2466 // Tell the workspace that we're done binding items
2467 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002468 public void run() {
2469 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2470 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002471 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002472 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473
Sunny Goyal639e9062015-08-19 19:17:06 -07002474 mIsLoadingAndBindingWorkspace = false;
2475
2476 // Run all the bind complete runnables after workspace is bound.
2477 if (!mBindCompleteRunnables.isEmpty()) {
2478 synchronized (mBindCompleteRunnables) {
2479 for (final Runnable r : mBindCompleteRunnables) {
2480 runOnWorkerThread(r);
2481 }
2482 mBindCompleteRunnables.clear();
2483 }
2484 }
2485
Winson Chung98e030b2012-05-07 16:01:11 -07002486 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002487 if (DEBUG_LOADERS) {
2488 Log.d(TAG, "bound workspace in "
2489 + (SystemClock.uptimeMillis()-t) + "ms");
2490 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002491
Joe Onorato36115782010-06-17 13:28:48 -04002492 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002494 deferredExecutor.execute(r);
2495
Winson Chung4a2afa32012-07-19 14:53:05 -07002496 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002497 r = new Runnable() {
2498 public void run() {
2499 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2500 if (callbacks != null) {
2501 // We are loading synchronously, which means, some of the pages will be
2502 // bound after first draw. Inform the callbacks that page binding is
2503 // not complete, and schedule the remaining pages.
2504 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2505 callbacks.onPageBoundSynchronously(currentScreen);
2506 }
2507 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2508 }
2509 }
2510 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002511 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002512 }
Joe Onorato36115782010-06-17 13:28:48 -04002513 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002514
Joe Onorato36115782010-06-17 13:28:48 -04002515 private void loadAndBindAllApps() {
2516 if (DEBUG_LOADERS) {
2517 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2518 }
2519 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002520 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002521 synchronized (LoaderTask.this) {
2522 if (mStopped) {
2523 return;
2524 }
2525 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002526 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002527 synchronized (LoaderTask.this) {
2528 if (mStopped) {
2529 return;
2530 }
2531 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002532 }
Joe Onorato36115782010-06-17 13:28:48 -04002533 } else {
2534 onlyBindAllApps();
2535 }
2536 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002537
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002538 private void updateIconCache() {
2539 // Ignore packages which have a promise icon.
2540 HashSet<String> packagesToIgnore = new HashSet<>();
2541 synchronized (sBgLock) {
2542 for (ItemInfo info : sBgItemsIdMap) {
2543 if (info instanceof ShortcutInfo) {
2544 ShortcutInfo si = (ShortcutInfo) info;
2545 if (si.isPromise() && si.getTargetComponent() != null) {
2546 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2547 }
2548 } else if (info instanceof LauncherAppWidgetInfo) {
2549 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2550 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2551 packagesToIgnore.add(lawi.providerName.getPackageName());
2552 }
2553 }
2554 }
2555 }
2556 mIconCache.updateDbIcons(packagesToIgnore);
2557 }
2558
Joe Onorato36115782010-06-17 13:28:48 -04002559 private void onlyBindAllApps() {
2560 final Callbacks oldCallbacks = mCallbacks.get();
2561 if (oldCallbacks == null) {
2562 // This launcher has exited and nobody bothered to tell us. Just bail.
2563 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2564 return;
2565 }
2566
2567 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002568 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002569 final ArrayList<AppInfo> list
2570 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002571 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002572 public void run() {
2573 final long t = SystemClock.uptimeMillis();
2574 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2575 if (callbacks != null) {
2576 callbacks.bindAllApplications(list);
2577 }
2578 if (DEBUG_LOADERS) {
2579 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002580 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002581 }
2582 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002583 };
2584 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002585 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002586 r.run();
2587 } else {
2588 mHandler.post(r);
2589 }
Joe Onorato36115782010-06-17 13:28:48 -04002590 }
2591
Winson Chung64359a52013-07-08 17:17:08 -07002592 private void loadAllApps() {
2593 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002594
Joe Onorato36115782010-06-17 13:28:48 -04002595 final Callbacks oldCallbacks = mCallbacks.get();
2596 if (oldCallbacks == null) {
2597 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002598 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002599 return;
2600 }
2601
Kenny Guyed131872014-04-30 03:02:21 +01002602 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2603
Winson Chung64359a52013-07-08 17:17:08 -07002604 // Clear the list of apps
2605 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002606 for (UserHandleCompat user : profiles) {
2607 // Query for the set of apps
2608 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002609 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002610 if (DEBUG_LOADERS) {
2611 Log.d(TAG, "getActivityList took "
2612 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2613 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2614 }
2615 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002616 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002617 if (apps == null || apps.isEmpty()) {
2618 return;
2619 }
Kenny Guyff05f432016-01-22 17:48:29 +00002620 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002621 // Create the ApplicationInfos
2622 for (int i = 0; i < apps.size(); i++) {
2623 LauncherActivityInfoCompat app = apps.get(i);
2624 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002625 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002626 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002627
Sunny Goyal756a28a2015-04-23 17:07:55 -07002628 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2629 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002630 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002631
2632 @Override
2633 public void run() {
2634 heuristic.processUserApps(apps);
2635 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002636 };
2637 runOnMainThread(new Runnable() {
2638
2639 @Override
2640 public void run() {
2641 // Check isLoadingWorkspace on the UI thread, as it is updated on
2642 // the UI thread.
2643 if (mIsLoadingAndBindingWorkspace) {
2644 synchronized (mBindCompleteRunnables) {
2645 mBindCompleteRunnables.add(r);
2646 }
2647 } else {
2648 runOnWorkerThread(r);
2649 }
2650 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002651 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002652 }
Winson Chung64359a52013-07-08 17:17:08 -07002653 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002654 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002655 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2656 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002657
2658 // Post callback on main thread
2659 mHandler.post(new Runnable() {
2660 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002661
Winson Chung64359a52013-07-08 17:17:08 -07002662 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002663 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002664 if (callbacks != null) {
2665 callbacks.bindAllApplications(added);
2666 if (DEBUG_LOADERS) {
2667 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002668 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002669 }
2670 } else {
2671 Log.i(TAG, "not binding apps: no Launcher activity");
2672 }
2673 }
2674 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002675 // Cleanup any data stored for a deleted user.
2676 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002677 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002678 Log.d(TAG, "Icons processed in "
2679 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002680 }
2681 }
2682
2683 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002684 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002685 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002686 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2687 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2688 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2689 }
Joe Onorato36115782010-06-17 13:28:48 -04002690 }
2691 }
2692
Sunny Goyal75b0f552015-05-20 21:57:06 -07002693 /**
2694 * Called when the icons for packages have been updated in the icon cache.
2695 */
2696 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2697 final Callbacks callbacks = getCallback();
2698 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2699 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2700
2701 // If any package icon has changed (app was updated while launcher was dead),
2702 // update the corresponding shortcuts.
2703 synchronized (sBgLock) {
2704 for (ItemInfo info : sBgItemsIdMap) {
2705 if (info instanceof ShortcutInfo && user.equals(info.user)
2706 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2707 ShortcutInfo si = (ShortcutInfo) info;
2708 ComponentName cn = si.getTargetComponent();
2709 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2710 si.updateIcon(mIconCache);
2711 updatedShortcuts.add(si);
2712 }
2713 }
2714 }
2715 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2716 }
2717
2718 if (!updatedShortcuts.isEmpty()) {
2719 final UserHandleCompat userFinal = user;
2720 mHandler.post(new Runnable() {
2721
2722 public void run() {
2723 Callbacks cb = getCallback();
2724 if (cb != null && callbacks == cb) {
2725 cb.bindShortcutsChanged(updatedShortcuts,
2726 new ArrayList<ShortcutInfo>(), userFinal);
2727 }
2728 }
2729 });
2730 }
2731
2732 if (!updatedApps.isEmpty()) {
2733 mHandler.post(new Runnable() {
2734
2735 public void run() {
2736 Callbacks cb = getCallback();
2737 if (cb != null && callbacks == cb) {
2738 cb.bindAppsUpdated(updatedApps);
2739 }
2740 }
2741 });
2742 }
2743 }
2744
Joe Onorato36115782010-06-17 13:28:48 -04002745 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002746 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002747 }
2748
Adam Cohen091440a2015-03-18 14:16:05 -07002749 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002750
2751 @Override
2752 public void onReceive(Context context, Intent intent) {
2753 synchronized (sBgLock) {
2754 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2755 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002756 final PackageManager manager = context.getPackageManager();
2757 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2758 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002759 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2760 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002761 packagesRemoved.clear();
2762 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002763 for (String pkg : entry.getValue()) {
2764 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002765 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002766 packagesUnavailable.add(pkg);
2767 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002768 packagesRemoved.add(pkg);
2769 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002770 }
2771 }
2772 if (!packagesRemoved.isEmpty()) {
2773 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2774 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2775 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002776 if (!packagesUnavailable.isEmpty()) {
2777 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2778 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2779 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002780 }
Sunny Goyal34942622014-08-29 17:20:55 -07002781 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002782 }
2783 }
2784 }
2785
Joe Onorato36115782010-06-17 13:28:48 -04002786 private class PackageUpdatedTask implements Runnable {
2787 int mOp;
2788 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002789 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002790
2791 public static final int OP_NONE = 0;
2792 public static final int OP_ADD = 1;
2793 public static final int OP_UPDATE = 2;
2794 public static final int OP_REMOVE = 3; // uninstlled
2795 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002796 public static final int OP_SUSPEND = 5; // package suspended
2797 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002798 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002799
Kenny Guyed131872014-04-30 03:02:21 +01002800 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002801 mOp = op;
2802 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002803 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002804 }
2805
2806 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002807 if (!mHasLoaderCompletedOnce) {
2808 // Loader has not yet run.
2809 return;
2810 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002811 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002812
2813 final String[] packages = mPackages;
2814 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002815 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002816 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002817 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002818 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002819 for (int i=0; i<N; i++) {
2820 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002821 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002822 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002823 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002824
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002825 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2826 if (heuristic != null) {
2827 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002828 }
Joe Onorato36115782010-06-17 13:28:48 -04002829 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002830 }
Joe Onorato36115782010-06-17 13:28:48 -04002831 case OP_UPDATE:
2832 for (int i=0; i<N; i++) {
2833 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002834 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002835 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002836 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002837 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002838 // Since package was just updated, the target must be available now.
2839 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002840 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002841 case OP_REMOVE: {
2842 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2843 if (heuristic != null) {
2844 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002845 }
Joe Onorato36115782010-06-17 13:28:48 -04002846 for (int i=0; i<N; i++) {
2847 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002848 mIconCache.removeIconsForPkg(packages[i], mUser);
2849 }
2850 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002851 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002852 case OP_UNAVAILABLE:
2853 for (int i=0; i<N; i++) {
2854 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2855 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002856 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002857 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002858 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002859 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002860 case OP_SUSPEND:
2861 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002862 flagOp = mOp == OP_SUSPEND ?
2863 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2864 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002865 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002866 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2867 break;
2868 case OP_USER_AVAILABILITY_CHANGE:
2869 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2870 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2871 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2872 // We want to update all packages for this user.
2873 pkgFilter = StringFilter.matchesAll();
2874 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002875 break;
Joe Onorato36115782010-06-17 13:28:48 -04002876 }
2877
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002878 ArrayList<AppInfo> added = null;
2879 ArrayList<AppInfo> modified = null;
2880 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002881
Adam Cohen487f7dd2012-06-28 18:12:10 -07002882 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002883 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002884 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002885 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002886 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002887 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002888 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002889 }
Winson Chung5d55f332012-07-16 20:45:03 -07002890 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002891 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002892 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002893 }
2894
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002895 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002896
Joe Onorato36115782010-06-17 13:28:48 -04002897 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002898 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002899 for (AppInfo ai : added) {
2900 addedOrUpdatedApps.put(ai.componentName, ai);
2901 }
Joe Onorato36115782010-06-17 13:28:48 -04002902 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002903
Joe Onorato36115782010-06-17 13:28:48 -04002904 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002905 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002906 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002907 for (AppInfo ai : modified) {
2908 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002909 }
2910
Joe Onorato36115782010-06-17 13:28:48 -04002911 mHandler.post(new Runnable() {
2912 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002913 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002914 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002915 callbacks.bindAppsUpdated(modifiedFinal);
2916 }
2917 }
2918 });
2919 }
Winson Chung83892cc2013-05-01 16:53:33 -07002920
Sunny Goyal4390ace2014-10-13 11:33:11 -07002921 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002922 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002923 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
2924 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
2925 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
2926
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002927 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002928 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002929 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2930 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002931 boolean infoUpdated = false;
2932 boolean shortcutUpdated = false;
2933
2934 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002935 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07002936 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002937 Bitmap icon = Utilities.createIconBitmap(
2938 si.iconResource.packageName,
2939 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002940 if (icon != null) {
2941 si.setIcon(icon);
2942 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002943 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002944 }
2945 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002946
2947 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07002948 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002949 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2950
2951 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002952 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2953 // Auto install icon
2954 PackageManager pm = context.getPackageManager();
2955 ResolveInfo matched = pm.resolveActivity(
2956 new Intent(Intent.ACTION_MAIN)
2957 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2958 PackageManager.MATCH_DEFAULT_ONLY);
2959 if (matched == null) {
2960 // Try to find the best match activity.
2961 Intent intent = pm.getLaunchIntentForPackage(
2962 cn.getPackageName());
2963 if (intent != null) {
2964 cn = intent.getComponent();
2965 appInfo = addedOrUpdatedApps.get(cn);
2966 }
2967
2968 if ((intent == null) || (appInfo == null)) {
2969 removedShortcuts.add(si);
2970 continue;
2971 }
2972 si.promisedIntent = intent;
2973 }
2974 }
2975
2976 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07002977 if (appInfo != null) {
2978 si.flags = appInfo.flags;
2979 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002980
Sunny Goyal756adbc2015-04-16 15:20:51 -07002981 si.intent = si.promisedIntent;
2982 si.promisedIntent = null;
2983 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002984 infoUpdated = true;
2985 si.updateIcon(mIconCache);
2986 }
2987
2988 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2989 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2990 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002991 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002992 si.contentDescription = appInfo.contentDescription;
2993 infoUpdated = true;
2994 }
2995
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002996 int oldDisabledFlags = si.isDisabled;
2997 si.isDisabled = flagOp.apply(si.isDisabled);
2998 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002999 shortcutUpdated = true;
3000 }
3001 }
3002
3003 if (infoUpdated || shortcutUpdated) {
3004 updatedShortcuts.add(si);
3005 }
3006 if (infoUpdated) {
3007 updateItemInDatabase(context, si);
3008 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003009 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003010 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3011 if (mUser.equals(widgetInfo.user)
3012 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003013 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003014 widgetInfo.restoreStatus &=
3015 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3016 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003017
3018 // adding this flag ensures that launcher shows 'click to setup'
3019 // if the widget has a config activity. In case there is no config
3020 // activity, it will be marked as 'restored' during bind.
3021 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3022
Sunny Goyal4390ace2014-10-13 11:33:11 -07003023 widgets.add(widgetInfo);
3024 updateItemInDatabase(context, widgetInfo);
3025 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003026 }
3027 }
3028 }
3029
Sunny Goyal4390ace2014-10-13 11:33:11 -07003030 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003031 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003032 mHandler.post(new Runnable() {
3033
3034 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003035 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003036 if (callbacks == cb && cb != null) {
3037 callbacks.bindShortcutsChanged(
3038 updatedShortcuts, removedShortcuts, mUser);
3039 }
3040 }
3041 });
3042 if (!removedShortcuts.isEmpty()) {
3043 deleteItemsFromDatabase(context, removedShortcuts);
3044 }
3045 }
3046 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003047 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003048 mHandler.post(new Runnable() {
3049 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003050 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003051 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003052 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003053 }
3054 }
3055 });
3056 }
3057 }
3058
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003059 final HashSet<String> removedPackages = new HashSet<>();
3060 final HashSet<ComponentName> removedComponents = new HashSet<>();
3061 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003062 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003063 Collections.addAll(removedPackages, packages);
3064
3065 // No need to update the removedComponents as
3066 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003067 } else if (mOp == OP_UPDATE) {
3068 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003069 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003070 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003071 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003072 }
3073 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003074
3075 // Update removedComponents as some components can get removed during package update
3076 for (AppInfo info : removedApps) {
3077 removedComponents.add(info.componentName);
3078 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003079 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003080
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003081 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3082 for (String pn : removedPackages) {
3083 deletePackageFromDatabase(context, pn, mUser);
3084 }
3085 for (ComponentName cn : removedComponents) {
3086 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003087 }
3088
Winson Chungdf95eb12013-10-16 14:57:07 -07003089 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003090 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3091
Winson Chungdf95eb12013-10-16 14:57:07 -07003092 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003093 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003094 mHandler.post(new Runnable() {
3095 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003096 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003097 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003098 callbacks.bindWorkspaceComponentsRemoved(
3099 removedPackages, removedComponents, mUser);
3100 }
3101 }
3102 });
3103 }
3104
3105 if (!removedApps.isEmpty()) {
3106 // Remove corresponding apps from All-Apps
3107 final Callbacks callbacks = getCallback();
3108 mHandler.post(new Runnable() {
3109 public void run() {
3110 Callbacks cb = getCallback();
3111 if (callbacks == cb && cb != null) {
3112 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003113 }
3114 }
3115 });
Joe Onoratobe386092009-11-17 17:32:16 -08003116 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003117
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003118 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3119 // get widget update signals.
3120 if (!Utilities.ATLEAST_MARSHMALLOW &&
3121 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003122 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003123 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003124 public void run() {
3125 Callbacks cb = getCallback();
3126 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003127 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003128 }
3129 }
3130 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003131 }
3132 }
3133 }
3134
3135 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3136 mHandler.post(new Runnable() {
3137 @Override
3138 public void run() {
3139 Callbacks cb = getCallback();
3140 if (callbacks == cb && cb != null) {
3141 callbacks.bindWidgetsModel(model);
3142 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003143 }
3144 });
3145 }
3146
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003147 public void refreshAndBindWidgetsAndShortcuts(
3148 final Callbacks callbacks, final boolean bindFirst) {
3149 runOnWorkerThread(new Runnable() {
3150 @Override
3151 public void run() {
3152 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3153 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003154 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003155 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3156 bindWidgetsModel(callbacks, model);
3157 // update the Widget entries inside DB on the worker thread.
3158 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3159 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003160 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003161 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003162 }
3163
Adam Cohen091440a2015-03-18 14:16:05 -07003164 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003165 UserHandleCompat user) {
3166 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3167 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003168 }
Adam Cohen556f6132014-01-15 15:18:08 -08003169
Kenny Guyed131872014-04-30 03:02:21 +01003170 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3171 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003172 if (cn == null) {
3173 return false;
3174 }
Kenny Guyed131872014-04-30 03:02:21 +01003175 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3176 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003177 return false;
3178 }
Kenny Guyed131872014-04-30 03:02:21 +01003179 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003180 }
3181
Adam Cohena28b78e2014-05-20 17:03:04 -07003182 public static boolean isValidPackage(Context context, String packageName,
3183 UserHandleCompat user) {
3184 if (packageName == null) {
3185 return false;
3186 }
3187 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3188 return launcherApps.isPackageEnabledForProfile(packageName, user);
3189 }
3190
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003192 * Make an ShortcutInfo object for a restored application or shortcut item that points
3193 * to a package that is not yet installed on the system.
3194 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003195 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003196 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003197 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003198 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003199
3200 Bitmap icon = iconInfo.loadIcon(c, info, context);
3201 // the fallback icon
3202 if (icon == null) {
3203 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3204 } else {
3205 info.setIcon(icon);
3206 }
Sunny Goyal34942622014-08-29 17:20:55 -07003207
3208 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003209 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003210 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003211 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003212 }
Sunny Goyal34942622014-08-29 17:20:55 -07003213 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3214 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003215 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003216 }
Sunny Goyal34942622014-08-29 17:20:55 -07003217 } else {
3218 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3219 }
3220
Winson Chung82b016c2015-05-08 17:00:10 -07003221 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003222 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003223 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003224 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003225 return info;
3226 }
3227
3228 /**
3229 * Make an Intent object for a restored application or shortcut item that points
3230 * to the market page for the item.
3231 */
Adam Cohen091440a2015-03-18 14:16:05 -07003232 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003233 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003234 return getMarketIntent(componentName.getPackageName());
3235 }
3236
3237 static Intent getMarketIntent(String packageName) {
3238 return new Intent(Intent.ACTION_VIEW)
3239 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003240 .scheme("market")
3241 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003242 .appendQueryParameter("id", packageName)
3243 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003244 }
3245
3246 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003247 * Make an ShortcutInfo object for a shortcut that is an application.
3248 *
3249 * If c is not null, then it will be used to fill in missing data like the title and icon.
3250 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003251 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003252 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003253 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003254 if (user == null) {
3255 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003256 return null;
3257 }
3258
Kenny Guyed131872014-04-30 03:02:21 +01003259 ComponentName componentName = intent.getComponent();
3260 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003261 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003262 return null;
3263 }
3264
3265 Intent newIntent = new Intent(intent.getAction(), null);
3266 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3267 newIntent.setComponent(componentName);
3268 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003269 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003270 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3271 return null;
3272 }
3273
3274 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003275 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003276 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3277 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3278 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003279 }
3280
Sunny Goyald09c3702016-04-06 16:18:20 -07003281 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3282 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3283 }
3284
Joe Onorato56d82912010-03-07 14:32:10 -05003285 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003286 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003287 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003288 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003289
Joe Onorato56d82912010-03-07 14:32:10 -05003290 // fall back to the class name of the activity
3291 if (info.title == null) {
3292 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003293 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003294
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003296 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003297 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003298 if (lai != null) {
3299 info.flags = AppInfo.initFlags(lai);
3300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003302 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003303
Sunny Goyale2df0622015-04-24 11:27:00 -07003304 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003305 ItemInfoFilter f) {
3306 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3307 for (ItemInfo i : infos) {
3308 if (i instanceof ShortcutInfo) {
3309 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003310 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003311 if (cn != null && f.filterItem(null, info, cn)) {
3312 filtered.add(info);
3313 }
3314 } else if (i instanceof FolderInfo) {
3315 FolderInfo info = (FolderInfo) i;
3316 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003317 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003318 if (cn != null && f.filterItem(info, s, cn)) {
3319 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003320 }
3321 }
Winson Chung64359a52013-07-08 17:17:08 -07003322 } else if (i instanceof LauncherAppWidgetInfo) {
3323 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3324 ComponentName cn = info.providerName;
3325 if (cn != null && f.filterItem(null, info, cn)) {
3326 filtered.add(info);
3327 }
Winson Chung8a435102012-08-30 17:16:53 -07003328 }
3329 }
Winson Chung64359a52013-07-08 17:17:08 -07003330 return new ArrayList<ItemInfo>(filtered);
3331 }
3332
Adam Cohen091440a2015-03-18 14:16:05 -07003333 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003334 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003335 ItemInfoFilter filter = new ItemInfoFilter() {
3336 @Override
3337 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003338 if (info.user == null) {
3339 return cn.equals(cname);
3340 } else {
3341 return cn.equals(cname) && info.user.equals(user);
3342 }
Winson Chung64359a52013-07-08 17:17:08 -07003343 }
3344 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003345 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003346 }
3347
Sunny Goyal1a745e82014-10-02 15:58:31 -07003348 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003349 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003350 */
Adam Cohen091440a2015-03-18 14:16:05 -07003351 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003352 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003353 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003354 // Non-app shortcuts are only supported for current user.
3355 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003357
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003358 // TODO: If there's an explicit component and we can't install that, delete it.
3359
Winson Chung82b016c2015-05-08 17:00:10 -07003360 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003361
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003362 Bitmap icon = iconInfo.loadIcon(c, info, context);
3363 // the fallback icon
3364 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003365 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003366 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003367 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003368 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 return info;
3370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003371
Sunny Goyal2350bc92014-10-14 16:42:54 -07003372 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003373 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3374 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3375 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3376
Adam Cohend9198822011-11-22 16:42:47 -08003377 if (intent == null) {
3378 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3379 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3380 return null;
3381 }
3382
Joe Onorato0589f0f2010-02-08 13:44:00 -08003383 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003384 boolean customIcon = false;
3385 ShortcutIconResource iconResource = null;
3386
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003387 if (bitmap instanceof Bitmap) {
3388 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003389 customIcon = true;
3390 } else {
3391 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003392 if (extra instanceof ShortcutIconResource) {
3393 iconResource = (ShortcutIconResource) extra;
3394 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003395 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003396 }
3397 }
3398
Michael Jurkac9d95c52011-08-29 14:03:34 -07003399 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003400
Kenny Guyed131872014-04-30 03:02:21 +01003401 // Only support intents for current user for now. Intents sent from other
3402 // users wouldn't get here without intent forwarding anyway.
3403 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003404 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003405 icon = mIconCache.getDefaultIcon(info.user);
3406 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003407 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003408 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003409
Winson Chung82b016c2015-05-08 17:00:10 -07003410 info.title = Utilities.trim(name);
3411 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003412 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003413 info.iconResource = iconResource;
3414
3415 return info;
3416 }
3417
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003419 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 * or make a new one.
3421 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003422 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 // See if a placeholder was created for us already
3424 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003425 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003427 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003428 folders.put(id, folderInfo);
3429 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003430 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003431 }
3432
Joe Onoratobe386092009-11-17 17:32:16 -08003433
Sunny Goyal651077b2014-06-30 14:15:31 -07003434 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3435 return (provider != null) && (provider.provider != null)
3436 && (provider.provider.getPackageName() != null);
3437 }
3438
Joe Onoratobe386092009-11-17 17:32:16 -08003439 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003440 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003441 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3442 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3443 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3444 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003445 if (mLoaderTask != null) {
3446 mLoaderTask.dumpState();
3447 } else {
3448 Log.d(TAG, "mLoaderTask=null");
3449 }
Joe Onoratobe386092009-11-17 17:32:16 -08003450 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003451
3452 public Callbacks getCallback() {
3453 return mCallbacks != null ? mCallbacks.get() : null;
3454 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003455
3456 /**
3457 * @return {@link FolderInfo} if its already loaded.
3458 */
3459 public FolderInfo findFolderById(Long folderId) {
3460 synchronized (sBgLock) {
3461 return sBgFolders.get(folderId);
3462 }
3463 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003464
Sunny Goyal527c7d32015-08-28 15:19:36 -07003465 @Thunk class DeferredMainThreadExecutor implements Executor {
3466
3467 @Override
3468 public void execute(Runnable command) {
3469 runOnMainThread(command);
3470 }
3471 }
3472
Sunny Goyal756adbc2015-04-16 15:20:51 -07003473 /**
3474 * @return the looper for the worker thread which can be used to start background tasks.
3475 */
3476 public static Looper getWorkerLooper() {
3477 return sWorkerThread.getLooper();
3478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003479}