blob: f9cb9edea414d745e2df3ad9bbec20370bb187ac [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
Joe Onorato36115782010-06-17 13:28:48 -0400100 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500101 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800102
Adam Cohen091440a2015-03-18 14:16:05 -0700103 @Thunk final LauncherAppState mApp;
104 @Thunk final Object mLock = new Object();
105 @Thunk DeferredHandler mHandler = new DeferredHandler();
106 @Thunk LoaderTask mLoaderTask;
107 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700108 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Adam Cohen091440a2015-03-18 14:16:05 -0700110 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700111 static {
112 sWorkerThread.start();
113 }
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700115
Joe Onoratocc67f472010-06-08 10:54:30 -0700116 // We start off with everything not loaded. After that, we assume that
117 // our monitoring of the package manager provides all updates and we never
118 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk boolean mWorkspaceLoaded;
120 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700121
Sunny Goyal756a28a2015-04-23 17:07:55 -0700122 /**
123 * Set of runnables to be called on the background thread after the workspace binding
124 * is complete.
125 */
126 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
127
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800131 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700132 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800133 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700135 // The lock that must be acquired before referencing any static bg data structures. Unlike
136 // other locks, this one can generally be held long-term because we never expect any of these
137 // static data structures to be referenced outside of the worker thread except on the first
138 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700139 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700140
Adam Cohen487f7dd2012-06-28 18:12:10 -0700141 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700143 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144
Adam Cohen487f7dd2012-06-28 18:12:10 -0700145 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
146 // created by LauncherModel that are directly on the home screen (however, no widgets or
147 // shortcuts within folders).
148 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
151 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 new ArrayList<LauncherAppWidgetInfo>();
153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700155 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700156
Adam Cohendcd297f2013-06-18 13:13:40 -0700157 // sBgWorkspaceScreens is the ordered set of workspace screens.
158 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
159
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700160 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700161 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
162 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700163
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700164 // </ only access in worker thread >
165
Adam Cohen091440a2015-03-18 14:16:05 -0700166 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk final LauncherAppsCompat mLauncherApps;
169 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100170
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700172 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700174 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700176 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
177 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700178 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700179 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800180 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200182 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700183 public void bindAppsAdded(ArrayList<Long> newScreens,
184 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700185 ArrayList<ItemInfo> addAnimated,
186 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200187 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700188 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
189 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
190 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700191 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700192 public void bindWorkspaceComponentsRemoved(
193 HashSet<String> packageNames, HashSet<ComponentName> components,
194 UserHandleCompat user);
195 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800196 public void notifyWidgetProvidersChanged();
197 public void bindWidgetsModel(WidgetsModel model);
Winson Chung88fa7412015-08-03 14:25:28 -0700198 public void bindSearchProviderChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700199 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700200 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700201 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Winson Chung64359a52013-07-08 17:17:08 -0700204 public interface ItemInfoFilter {
205 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
206 }
207
Bjorn Bringert1307f632013-10-03 22:31:03 +0100208 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800209 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400210 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100211 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700212 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 mIconCache = iconCache;
214
Kenny Guyed131872014-04-30 03:02:21 +0100215 mLauncherApps = LauncherAppsCompat.getInstance(context);
216 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 }
218
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700219 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
220 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700222 if (sWorkerThread.getThreadId() == Process.myTid()) {
223 // If we are on the worker thread, post onto the main handler
224 mHandler.post(r);
225 } else {
226 r.run();
227 }
228 }
229
230 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
231 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700232 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700233 if (sWorkerThread.getThreadId() == Process.myTid()) {
234 r.run();
235 } else {
236 // If we are not on the worker thread, then post to the worker handler
237 sWorker.post(r);
238 }
239 }
240
Sunny Goyal756adbc2015-04-16 15:20:51 -0700241 public void setPackageState(final PackageInstallInfo installInfo) {
242 Runnable updateRunnable = new Runnable() {
243
244 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500245 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700246 synchronized (sBgLock) {
247 final HashSet<ItemInfo> updates = new HashSet<>();
248
249 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
250 // Ignore install success events as they are handled by Package add events.
251 return;
252 }
253
Sunny Goyale2df0622015-04-24 11:27:00 -0700254 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700255 if (info instanceof ShortcutInfo) {
256 ShortcutInfo si = (ShortcutInfo) info;
257 ComponentName cn = si.getTargetComponent();
258 if (si.isPromise() && (cn != null)
259 && installInfo.packageName.equals(cn.getPackageName())) {
260 si.setInstallProgress(installInfo.progress);
261
262 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
263 // Mark this info as broken.
264 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
265 }
266 updates.add(si);
267 }
268 }
269 }
270
271 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
272 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
273 widget.installProgress = installInfo.progress;
274 updates.add(widget);
275 }
276 }
277
278 if (!updates.isEmpty()) {
279 // Push changes to the callback.
280 Runnable r = new Runnable() {
281 public void run() {
282 Callbacks callbacks = getCallback();
283 if (callbacks != null) {
284 callbacks.bindRestoreItemsChange(updates);
285 }
286 }
287 };
288 mHandler.post(r);
289 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500290 }
291 }
292 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500294 }
295
Sunny Goyal756adbc2015-04-16 15:20:51 -0700296 /**
297 * Updates the icons and label of all pending icons for the provided package name.
298 */
299 public void updateSessionDisplayInfo(final String packageName) {
300 Runnable updateRunnable = new Runnable() {
301
302 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700303 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700304 synchronized (sBgLock) {
305 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
306 final UserHandleCompat user = UserHandleCompat.myUserHandle();
307
Sunny Goyale2df0622015-04-24 11:27:00 -0700308 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700309 if (info instanceof ShortcutInfo) {
310 ShortcutInfo si = (ShortcutInfo) info;
311 ComponentName cn = si.getTargetComponent();
312 if (si.isPromise() && (cn != null)
313 && packageName.equals(cn.getPackageName())) {
314 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
315 // For auto install apps update the icon as well as label.
316 mIconCache.getTitleAndIcon(si,
317 si.promisedIntent, user,
318 si.shouldUseLowResIcon());
319 } else {
320 // Only update the icon for restored apps.
321 si.updateIcon(mIconCache);
322 }
323 updates.add(si);
324 }
325 }
326 }
327
328 if (!updates.isEmpty()) {
329 // Push changes to the callback.
330 Runnable r = new Runnable() {
331 public void run() {
332 Callbacks callbacks = getCallback();
333 if (callbacks != null) {
334 callbacks.bindShortcutsChanged(updates,
335 new ArrayList<ShortcutInfo>(), user);
336 }
337 }
338 };
339 mHandler.post(r);
340 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700341 }
342 }
343 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700345 }
346
Adam Cohen76a47a12014-02-05 11:47:43 -0800347 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800348 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800349
350 if (allAppsApps == null) {
351 throw new RuntimeException("allAppsApps must not be null");
352 }
353 if (allAppsApps.isEmpty()) {
354 return;
355 }
356
357 // Process the newly added applications and add them to the database first
358 Runnable r = new Runnable() {
359 public void run() {
360 runOnMainThread(new Runnable() {
361 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800362 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800363 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500364 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800365 }
366 }
367 });
368 }
369 };
370 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700371 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800372
Sunny Goyala9116722015-04-29 13:55:58 -0700373 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800374 int[] xy, int spanX, int spanY) {
375 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700376 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
377 final int xCount = (int) profile.numColumns;
378 final int yCount = (int) profile.numRows;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800379 boolean[][] occupied = new boolean[xCount][yCount];
380 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700381 for (ItemInfo r : occupiedPos) {
382 int right = r.cellX + r.spanX;
383 int bottom = r.cellY + r.spanY;
384 for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) {
385 for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800386 occupied[x][y] = true;
387 }
388 }
389 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800390 }
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700391 return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800392 }
393
394 /**
395 * Find a position on the screen for the given size or adds a new screen.
396 * @return screenId and the coordinates for the item.
397 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700398 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700399 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800400 ArrayList<Long> workspaceScreens,
401 ArrayList<Long> addedWorkspaceScreensFinal,
402 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700403 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404
Sunny Goyala9116722015-04-29 13:55:58 -0700405 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700406 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700407 synchronized (sBgLock) {
408 for (ItemInfo info : sBgItemsIdMap) {
409 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
410 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
411 if (items == null) {
412 items = new ArrayList<>();
413 screenItems.put(info.screenId, items);
414 }
415 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800416 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800417 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800418 }
419
420 // Find appropriate space for the item.
421 long screenId = 0;
422 int[] cordinates = new int[2];
423 boolean found = false;
424
425 int screenCount = workspaceScreens.size();
426 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700427 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 if (preferredScreenIndex < screenCount) {
429 screenId = workspaceScreens.get(preferredScreenIndex);
430 found = findNextAvailableIconSpaceInScreen(
431 screenItems.get(screenId), cordinates, spanX, spanY);
432 }
433
434 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700435 // Search on any of the screens starting from the first screen.
436 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800437 screenId = workspaceScreens.get(screen);
438 if (findNextAvailableIconSpaceInScreen(
439 screenItems.get(screenId), cordinates, spanX, spanY)) {
440 // We found a space for it
441 found = true;
442 break;
443 }
444 }
445 }
446
447 if (!found) {
448 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700449 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
450 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
451 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452
453 // Save the screen id for binding in the workspace
454 workspaceScreens.add(screenId);
455 addedWorkspaceScreensFinal.add(screenId);
456
457 // If we still can't find an empty space, then God help us all!!!
458 if (!findNextAvailableIconSpaceInScreen(
459 screenItems.get(screenId), cordinates, spanX, spanY)) {
460 throw new RuntimeException("Can't find space to add the item");
461 }
462 }
463 return Pair.create(screenId, cordinates);
464 }
465
466 /**
467 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700469 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700470 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800471 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800472 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700473 return;
Winson Chung997a9232013-07-24 15:33:46 -0700474 }
Winson Chung64359a52013-07-08 17:17:08 -0700475 // Process the newly added applications and add them to the database first
476 Runnable r = new Runnable() {
477 public void run() {
478 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
479 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
480
Winson Chung76828c82013-08-19 15:43:29 -0700481 // Get the list of workspace screens. We need to append to this list and
482 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
483 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800484 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700485 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700487 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800488 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700489 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800490 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700491 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800492 }
Winson Chung76828c82013-08-19 15:43:29 -0700493
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800494 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700495 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700496 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800497 long screenId = coords.first;
498 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700499
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700500 ItemInfo itemInfo;
501 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
502 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700504 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700505 } else {
506 throw new RuntimeException("Unexpected info type");
507 }
Winson Chung94d67682013-09-25 16:29:40 -0700508
Winson Chung64359a52013-07-08 17:17:08 -0700509 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700510 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700511 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700512 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700513 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700514 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700515 }
516 }
517
Winson Chung76828c82013-08-19 15:43:29 -0700518 // Update the workspace screens
519 updateWorkspaceScreenOrder(context, workspaceScreens);
520
Adam Cohen76a47a12014-02-05 11:47:43 -0800521 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700522 runOnMainThread(new Runnable() {
523 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800524 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700525 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700526 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
527 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700528 if (!addedShortcutsFinal.isEmpty()) {
529 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
530 long lastScreenId = info.screenId;
531 for (ItemInfo i : addedShortcutsFinal) {
532 if (i.screenId == lastScreenId) {
533 addAnimated.add(i);
534 } else {
535 addNotAnimated.add(i);
536 }
Winson Chung997a9232013-07-24 15:33:46 -0700537 }
538 }
Winson Chungd64d1762013-08-20 14:37:16 -0700539 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800540 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700541 }
Winson Chung64359a52013-07-08 17:17:08 -0700542 }
Winson Chung997a9232013-07-24 15:33:46 -0700543 });
544 }
Winson Chung64359a52013-07-08 17:17:08 -0700545 }
546 };
547 runOnWorkerThread(r);
548 }
549
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 /**
551 * Adds an item to the DB if it was not created previously, or move it to a new
552 * <container, screen, cellX, cellY>
553 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800554 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700555 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 if (item.container == ItemInfo.NO_ID) {
557 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700558 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400559 } else {
560 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700561 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 }
563 }
564
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700565 static void checkItemInfoLocked(
566 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
567 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
568 if (modelItem != null && item != modelItem) {
569 // check all the data is consistent
570 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
571 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
572 ShortcutInfo shortcut = (ShortcutInfo) item;
573 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
574 modelShortcut.intent.filterEquals(shortcut.intent) &&
575 modelShortcut.id == shortcut.id &&
576 modelShortcut.itemType == shortcut.itemType &&
577 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700578 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700579 modelShortcut.cellX == shortcut.cellX &&
580 modelShortcut.cellY == shortcut.cellY &&
581 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700582 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700583 // For all intents and purposes, this is the same object
584 return;
585 }
586 }
587
588 // the modelItem needs to match up perfectly with item if our model is
589 // to be consistent with the database-- for now, just require
590 // modelItem == item or the equality check above
591 String msg = "item: " + ((item != null) ? item.toString() : "null") +
592 "modelItem: " +
593 ((modelItem != null) ? modelItem.toString() : "null") +
594 "Error: ItemInfo passed to checkItemInfo doesn't match original";
595 RuntimeException e = new RuntimeException(msg);
596 if (stackTrace != null) {
597 e.setStackTrace(stackTrace);
598 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800599 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700600 }
601 }
602
Michael Jurka816474f2012-06-25 14:49:02 -0700603 static void checkItemInfo(final ItemInfo item) {
604 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
605 final long itemId = item.id;
606 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700607 public void run() {
608 synchronized (sBgLock) {
609 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700610 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700611 }
612 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700613 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700614 }
615
Michael Jurkac9d95c52011-08-29 14:03:34 -0700616 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
617 final ItemInfo item, final String callingFunction) {
618 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700619 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700620 final ContentResolver cr = context.getContentResolver();
621
Adam Cohen487f7dd2012-06-28 18:12:10 -0700622 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700623 Runnable r = new Runnable() {
624 public void run() {
625 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700626 updateItemArrays(item, itemId, stackTrace);
627 }
628 };
629 runOnWorkerThread(r);
630 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700632 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
633 final ArrayList<ItemInfo> items, final String callingFunction) {
634 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700635
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700636 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
637 Runnable r = new Runnable() {
638 public void run() {
639 ArrayList<ContentProviderOperation> ops =
640 new ArrayList<ContentProviderOperation>();
641 int count = items.size();
642 for (int i = 0; i < count; i++) {
643 ItemInfo item = items.get(i);
644 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700645 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700646 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700647
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700648 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
649 updateItemArrays(item, itemId, stackTrace);
650
651 }
652 try {
653 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
654 } catch (Exception e) {
655 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700656 }
657 }
658 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700659 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700660 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700661
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700662 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
663 // Lock on mBgLock *after* the db operation
664 synchronized (sBgLock) {
665 checkItemInfoLocked(itemId, item, stackTrace);
666
667 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
668 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
669 // Item is in a folder, make sure this folder exists
670 if (!sBgFolders.containsKey(item.container)) {
671 // An items container is being set to a that of an item which is not in
672 // the list of Folders.
673 String msg = "item: " + item + " container being set to: " +
674 item.container + ", not in the list of folders";
675 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 }
677 }
678
679 // Items are added/removed from the corresponding FolderInfo elsewhere, such
680 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
681 // that are on the desktop, as appropriate
682 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800683 if (modelItem != null &&
684 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
685 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686 switch (modelItem.itemType) {
687 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
688 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
689 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
690 if (!sBgWorkspaceItems.contains(modelItem)) {
691 sBgWorkspaceItems.add(modelItem);
692 }
693 break;
694 default:
695 break;
696 }
697 } else {
698 sBgWorkspaceItems.remove(modelItem);
699 }
700 }
701 }
702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700705 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700706 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 item.cellX = cellX;
710 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700711
Winson Chung3d503fb2011-07-13 17:25:49 -0700712 // We store hotseat items in canonical form which is this orientation invariant position
713 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700715 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700719 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720
721 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700723 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
724 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800725 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700726 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727
Michael Jurkac9d95c52011-08-29 14:03:34 -0700728 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700729 }
730
731 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700732 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
733 * cellX, cellY have already been updated on the ItemInfos.
734 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800735 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 final long container, final int screen) {
737
738 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
739 int count = items.size();
740
741 for (int i = 0; i < count; i++) {
742 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 item.container = container;
744
745 // We store hotseat items in canonical form which is this orientation invariant position
746 // in the hotseat
747 if (context instanceof Launcher && screen < 0 &&
748 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700749 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 item.cellY);
751 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700752 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753 }
754
755 final ContentValues values = new ContentValues();
756 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
757 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
758 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800759 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700760 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700761
762 contentValues.add(values);
763 }
764 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
765 }
766
767 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700768 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800769 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700770 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700772 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800773 item.cellX = cellX;
774 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700775 item.spanX = spanX;
776 item.spanY = spanY;
777
778 // We store hotseat items in canonical form which is this orientation invariant position
779 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 }
Adam Cohend4844c32011-02-18 19:25:06 -0800786
Adam Cohend4844c32011-02-18 19:25:06 -0800787 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800788 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700789 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
790 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800791 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700792 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
793 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800795
Michael Jurka816474f2012-06-25 14:49:02 -0700796 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700798
799 /**
800 * Update an item to the database in a specified container.
801 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700802 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700803 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100804 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700805 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800806 }
807
Sunny Goyal756a28a2015-04-23 17:07:55 -0700808 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700809 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700810 synchronized (mLock) {
811 if (!mHasLoaderCompletedOnce ||
812 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
813 throw new RuntimeException("Trying to add shortcut while loader is running");
814 }
815 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700816 }
817 }
818
Adam Cohend4844c32011-02-18 19:25:06 -0800819 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700820 * Returns true if the shortcuts already exists on the workspace. This must be called after
821 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700823 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
824 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700825 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700826 if (intent.getComponent() != null) {
827 // If component is not null, an intent with null package will produce
828 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700829 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700830 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700831 intentWithPkg = intent.toUri(0);
832 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700833 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700834 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
835 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700836 }
837 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700838 intentWithPkg = intent.toUri(0);
839 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700840 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700841
842 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700843 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700844 if (item instanceof ShortcutInfo) {
845 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700846 Intent targetIntent = info.promisedIntent == null
847 ? info.intent : info.promisedIntent;
848 if (targetIntent != null && info.user.equals(user)) {
849 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700850 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
851 return true;
852 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700853 }
854 }
855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700857 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700858 }
859
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 * Add an item to the database in a specified container. Sets the container, screen, cellX and
862 * cellY fields of the item. Also assigns an ID to the item.
863 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700864 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700865 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867 item.cellX = cellX;
868 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 // We store hotseat items in canonical form which is this orientation invariant position
870 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700871 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700872 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700874 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700876 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877
878 final ContentValues values = new ContentValues();
879 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100880 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881
Sunny Goyald2497482015-09-22 18:24:19 -0700882 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
883 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
884
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700885 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700886
Jason Monk8e19cf22014-03-20 15:06:57 -0400887 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700888 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700889 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700890 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700892 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700893 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400894 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700895 sBgItemsIdMap.put(item.id, item);
896 switch (item.itemType) {
897 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
898 sBgFolders.put(item.id, (FolderInfo) item);
899 // Fall through
900 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
901 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
902 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
903 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
904 sBgWorkspaceItems.add(item);
905 } else {
906 if (!sBgFolders.containsKey(item.container)) {
907 // Adding an item to a folder that doesn't exist.
908 String msg = "adding item: " + item + " to a folder that " +
909 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700910 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700911 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700912 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700913 break;
914 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
915 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
916 break;
917 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700918 }
919 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700920 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700921 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700922 }
923
Sunny Goyal34942622014-08-29 17:20:55 -0700924 private static ArrayList<ItemInfo> getItemsByPackageName(
925 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700926 ItemInfoFilter filter = new ItemInfoFilter() {
927 @Override
928 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
929 return cn.getPackageName().equals(pn) && info.user.equals(user);
930 }
931 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700932 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700933 }
934
935 /**
936 * Removes all the items from the database corresponding to the specified package.
937 */
938 static void deletePackageFromDatabase(Context context, final String pn,
939 final UserHandleCompat user) {
940 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700941 }
942
943 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700944 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700946 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700947 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
948 items.add(item);
949 deleteItemsFromDatabase(context, items);
950 }
951
952 /**
953 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700954 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700955 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700957 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700958 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700959 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700960 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700961 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700962
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700963 // Lock on mBgLock *after* the db operation
964 synchronized (sBgLock) {
965 switch (item.itemType) {
966 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
967 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700968 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700969 if (info.container == item.id) {
970 // We are deleting a folder which still contains items that
971 // think they are contained by that folder.
972 String msg = "deleting a folder (" + item + ") which still " +
973 "contains items (" + info + ")";
974 Log.e(TAG, msg);
975 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700976 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700977 sBgWorkspaceItems.remove(item);
978 break;
979 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
980 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
981 sBgWorkspaceItems.remove(item);
982 break;
983 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
984 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
985 break;
986 }
987 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700988 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700989 }
990 }
Michael Jurka83df1882011-08-31 20:59:26 -0700991 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700992 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 }
994
995 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 * Update the order of the workspace screens in the database. The array list contains
997 * a list of screen ids in the order that they should appear.
998 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700999 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001000 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 final ContentResolver cr = context.getContentResolver();
1002 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1003
1004 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001005 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 while (iter.hasNext()) {
1007 long id = iter.next();
1008 if (id < 0) {
1009 iter.remove();
1010 }
1011 }
1012
1013 Runnable r = new Runnable() {
1014 @Override
1015 public void run() {
Yura085c8532014-02-11 15:15:29 +00001016 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001017 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001018 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001019 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001020 for (int i = 0; i < count; i++) {
1021 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001022 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1024 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001025 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001026 }
Yura085c8532014-02-11 15:15:29 +00001027
1028 try {
1029 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1030 } catch (Exception ex) {
1031 throw new RuntimeException(ex);
1032 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001033
Winson Chungba9c37f2013-08-30 14:11:37 -07001034 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001035 sBgWorkspaceScreens.clear();
1036 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001037 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001038 }
1039 };
1040 runOnWorkerThread(r);
1041 }
1042
1043 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001044 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 */
Winsonc0b52fe2015-09-09 16:38:15 -07001046 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 final ContentResolver cr = context.getContentResolver();
1048
Michael Jurkac9d95c52011-08-29 14:03:34 -07001049 Runnable r = new Runnable() {
1050 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001051 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001053 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 sBgItemsIdMap.remove(info.id);
1055 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 sBgWorkspaceItems.remove(info);
1057 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001058
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001059 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001060 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001061 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001062 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 for (ItemInfo childInfo : info.contents) {
1064 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001066 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001067 }
1068 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001069 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 }
1071
1072 /**
1073 * Set this as the current Launcher activity object for the loader.
1074 */
1075 public void initialize(Callbacks callbacks) {
1076 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001077 Preconditions.assertUIThread();
1078 // Remove any queued UI runnables
1079 mHandler.cancelAll();
1080 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081 }
1082 }
1083
Kenny Guyed131872014-04-30 03:02:21 +01001084 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001085 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001086 int op = PackageUpdatedTask.OP_UPDATE;
1087 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1088 user));
1089 }
1090
1091 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001092 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001093 int op = PackageUpdatedTask.OP_REMOVE;
1094 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1095 user));
1096 }
1097
1098 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001099 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001100 int op = PackageUpdatedTask.OP_ADD;
1101 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1102 user));
1103 }
1104
1105 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001106 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001107 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001108 enqueuePackageUpdated(
1109 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001110 }
1111
1112 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001113 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001114 boolean replacing) {
1115 if (!replacing) {
1116 enqueuePackageUpdated(new PackageUpdatedTask(
1117 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1118 user));
1119 }
Kenny Guyed131872014-04-30 03:02:21 +01001120 }
1121
Kenny Guy44cba692016-01-21 19:50:02 +00001122 @Override
1123 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1124 enqueuePackageUpdated(new PackageUpdatedTask(
1125 PackageUpdatedTask.OP_SUSPEND, packageNames,
1126 user));
1127 }
1128
1129 @Override
1130 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1131 enqueuePackageUpdated(new PackageUpdatedTask(
1132 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1133 user));
1134 }
1135
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001136 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1138 * ACTION_PACKAGE_CHANGED.
1139 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001140 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001141 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001142 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001143
Joe Onorato36115782010-06-17 13:28:48 -04001144 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001145 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001146 // If we have changed locale we need to clear out the labels in all apps/workspace.
1147 forceReload();
Winson Chung88fa7412015-08-03 14:25:28 -07001148 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001149 Callbacks callbacks = getCallback();
1150 if (callbacks != null) {
Winson Chung88fa7412015-08-03 14:25:28 -07001151 callbacks.bindSearchProviderChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001152 }
Sunny Goyal957c13f2015-05-01 13:02:20 -07001153 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001154 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001155 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001156 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001157 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1158 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001159 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1160 if (user != null) {
1161 enqueuePackageUpdated(new PackageUpdatedTask(
1162 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1163 new String[0], user));
1164 }
Tony Wickham827cef22016-03-17 15:39:39 -07001165 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1166 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001167 }
1168 }
1169
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001170 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001171 resetLoadedState(true, true);
1172
Reena Lee93f824a2011-09-23 17:20:28 -07001173 // Do this here because if the launcher activity is running it will be restarted.
1174 // If it's not running startLoaderFromBackground will merely tell it that it needs
1175 // to reload.
1176 startLoaderFromBackground();
1177 }
1178
Winson Chungf0c6ae02012-03-21 16:10:31 -07001179 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1180 synchronized (mLock) {
1181 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1182 // mWorkspaceLoaded to true later
1183 stopLoaderLocked();
1184 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1185 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1186 }
1187 }
1188
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001189 /**
1190 * When the launcher is in the background, it's possible for it to miss paired
1191 * configuration changes. So whenever we trigger the loader from the background
1192 * tell the launcher that it needs to re-run the loader when it comes back instead
1193 * of doing it now.
1194 */
1195 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001196 Callbacks callbacks = getCallback();
1197 if (callbacks != null) {
1198 // Only actually run the loader if they're not paused.
1199 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001200 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001201 }
1202 }
Joe Onorato36115782010-06-17 13:28:48 -04001203 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001204
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001205 /**
1206 * If there is already a loader task running, tell it to stop.
1207 */
1208 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001209 LoaderTask oldTask = mLoaderTask;
1210 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001211 oldTask.stopLocked();
1212 }
Reena Lee93f824a2011-09-23 17:20:28 -07001213 }
1214
Adam Cohen1a85c582014-09-30 09:48:49 -07001215 public boolean isCurrentCallbacks(Callbacks callbacks) {
1216 return (mCallbacks != null && mCallbacks.get() == callbacks);
1217 }
1218
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001219 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001220 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1221 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001222 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001223 // Don't bother to start the thread if we know it's not going to do anything
1224 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001225 final Callbacks oldCallbacks = mCallbacks.get();
1226 // Clear any pending bind-runnables from the synchronized load process.
1227 runOnMainThread(new Runnable() {
1228 public void run() {
1229 oldCallbacks.clearPendingBinds();
1230 }
1231 });
1232
Joe Onorato36115782010-06-17 13:28:48 -04001233 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001234 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001235 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001236 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001237 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001238 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1239 } else {
1240 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1241 sWorker.post(mLoaderTask);
1242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 }
1245 }
1246
Joe Onorato36115782010-06-17 13:28:48 -04001247 public void stopLoader() {
1248 synchronized (mLock) {
1249 if (mLoaderTask != null) {
1250 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001251 }
1252 }
Joe Onorato36115782010-06-17 13:28:48 -04001253 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001255 /**
1256 * Loads the workspace screen ids in an ordered list.
1257 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001258 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001259 final ContentResolver contentResolver = context.getContentResolver();
1260 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001261
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001262 // Get screens ordered by rank.
1263 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1264 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1265 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001266 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001267 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001268 while (sc.moveToNext()) {
1269 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001270 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001271 } catch (Exception e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001272 FileLog.d(TAG, "Invalid screen id", e);
Winson Chung76828c82013-08-19 15:43:29 -07001273 }
1274 }
1275 } finally {
1276 sc.close();
1277 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001278 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001279 }
1280
Joe Onorato36115782010-06-17 13:28:48 -04001281 /**
1282 * Runnable for the thread that loads the contents of the launcher:
1283 * - workspace icons
1284 * - widgets
1285 * - all apps icons
1286 */
1287 private class LoaderTask implements Runnable {
1288 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001289 private int mPageToBindFirst;
1290
Adam Cohen091440a2015-03-18 14:16:05 -07001291 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001292 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001293 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001294
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001295 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001296 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001297 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298 }
1299
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001300 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001301 mIsLoadingAndBindingWorkspace = true;
1302
Joe Onorato36115782010-06-17 13:28:48 -04001303 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001304 if (DEBUG_LOADERS) {
1305 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001306 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001307
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001308 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001309 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001310 synchronized (LoaderTask.this) {
1311 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001312 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001313 }
1314 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 }
1316 }
1317
Joe Onorato36115782010-06-17 13:28:48 -04001318 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001319 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001320 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001321
Joe Onorato36115782010-06-17 13:28:48 -04001322 private void waitForIdle() {
1323 // Wait until the either we're stopped or the other threads are done.
1324 // This way we don't start loading all apps until the workspace has settled
1325 // down.
1326 synchronized (LoaderTask.this) {
1327 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001328
Joe Onorato36115782010-06-17 13:28:48 -04001329 mHandler.postIdle(new Runnable() {
1330 public void run() {
1331 synchronized (LoaderTask.this) {
1332 mLoadAndBindStepFinished = true;
1333 if (DEBUG_LOADERS) {
1334 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001335 }
Joe Onorato36115782010-06-17 13:28:48 -04001336 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001337 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001338 }
Joe Onorato36115782010-06-17 13:28:48 -04001339 });
1340
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001341 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001342 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001343 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1344 // wait no longer than 1sec at a time
1345 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001346 } catch (InterruptedException ex) {
1347 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001348 }
1349 }
Joe Onorato36115782010-06-17 13:28:48 -04001350 if (DEBUG_LOADERS) {
1351 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001352 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001353 + "ms for previous step to finish binding");
1354 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001355 }
Joe Onorato36115782010-06-17 13:28:48 -04001356 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001357
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001359 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001360 // Ensure that we have a valid page index to load synchronously
1361 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1362 "valid page index");
1363 }
1364 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1365 // Ensure that we don't try and bind a specified page when the pages have not been
1366 // loaded already (we should load everything asynchronously in that case)
1367 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1368 }
1369 synchronized (mLock) {
1370 if (mIsLoaderTaskRunning) {
1371 // Ensure that we are never running the background loading at this point since
1372 // we also touch the background collections
1373 throw new RuntimeException("Error! Background loading is already running");
1374 }
1375 }
1376
1377 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1378 // data structures, we can't allow any other thread to touch that data, but because
1379 // this call is synchronous, we can get away with not locking).
1380
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001381 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001382 // operations from the previous activity. We need to ensure that all queued operations
1383 // are executed before any synchronous binding work is done.
1384 mHandler.flush();
1385
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 // Divide the set of loaded items into those that we are binding synchronously, and
1387 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001388 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1390 // arise from that.
1391 onlyBindAllApps();
1392 }
1393
Joe Onorato36115782010-06-17 13:28:48 -04001394 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001395 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001396 if (mStopped) {
1397 return;
1398 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001399 mIsLoaderTaskRunning = true;
1400 }
Joe Onorato36115782010-06-17 13:28:48 -04001401 // Optimize for end-user experience: if the Launcher is up and // running with the
1402 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1403 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001404 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001405 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001406 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001407
Joe Onorato36115782010-06-17 13:28:48 -04001408 if (mStopped) {
1409 break keep_running;
1410 }
1411
Joe Onorato36115782010-06-17 13:28:48 -04001412 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001413
1414 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001415 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1416 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001418
Joe Onorato36115782010-06-17 13:28:48 -04001419 // Clear out this reference, otherwise we end up holding it until all of the
1420 // callback runnables are done.
1421 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422
Joe Onorato36115782010-06-17 13:28:48 -04001423 synchronized (mLock) {
1424 // If we are still the last one to be scheduled, remove ourselves.
1425 if (mLoaderTask == this) {
1426 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001428 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001429 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001430 }
Joe Onorato36115782010-06-17 13:28:48 -04001431 }
1432
1433 public void stopLocked() {
1434 synchronized (LoaderTask.this) {
1435 mStopped = true;
1436 this.notify();
1437 }
1438 }
1439
1440 /**
1441 * Gets the callbacks object. If we've been stopped, or if the launcher object
1442 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1443 * object that was around when the deferred message was scheduled, and if there's
1444 * a new Callbacks object around then also return null. This will save us from
1445 * calling onto it with data that will be ignored.
1446 */
1447 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1448 synchronized (mLock) {
1449 if (mStopped) {
1450 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001451 }
Joe Onorato36115782010-06-17 13:28:48 -04001452
1453 if (mCallbacks == null) {
1454 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001455 }
Joe Onorato36115782010-06-17 13:28:48 -04001456
1457 final Callbacks callbacks = mCallbacks.get();
1458 if (callbacks != oldCallbacks) {
1459 return null;
1460 }
1461 if (callbacks == null) {
1462 Log.w(TAG, "no mCallbacks");
1463 return null;
1464 }
1465
1466 return callbacks;
1467 }
1468 }
1469
1470 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001471 private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item,
1472 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001473 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001474 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001475 final int countX = profile.numColumns;
1476 final int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001477
Adam Cohendcd297f2013-06-18 13:13:40 -07001478 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001479 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001480 // Return early if we detect that an item is under the hotseat button
1481 if (mCallbacks == null ||
1482 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001483 Log.e(TAG, "Error loading shortcut into hotseat " + item
1484 + " into position (" + item.screenId + ":" + item.cellX + ","
1485 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001486 return false;
1487 }
1488
Dan Sandler295ae182013-12-10 16:05:47 -05001489 final ItemInfo[][] hotseatItems =
1490 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1491
Adam Cohen2e6da152015-05-06 11:42:25 -07001492 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001493 Log.e(TAG, "Error loading shortcut " + item
1494 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001495 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001496 + ")");
1497 return false;
1498 }
1499
Dan Sandler295ae182013-12-10 16:05:47 -05001500 if (hotseatItems != null) {
1501 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 Log.e(TAG, "Error loading shortcut into hotseat " + item
1503 + " into position (" + item.screenId + ":" + item.cellX + ","
1504 + item.cellY + ") occupied by "
1505 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1506 [(int) item.screenId][0]);
1507 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001508 } else {
1509 hotseatItems[(int) item.screenId][0] = item;
1510 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001511 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001512 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -07001513 final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 items[(int) item.screenId][0] = item;
1515 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001516 return true;
1517 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001518 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1519 if (!workspaceScreens.contains((Long) item.screenId)) {
1520 // The item has an invalid screen id.
1521 return false;
1522 }
1523 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001524 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001525 return true;
1526 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001527
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001529 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 occupied.put(item.screenId, items);
1531 }
1532
Dan Sandler295ae182013-12-10 16:05:47 -05001533 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001534 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1535 item.cellX < 0 || item.cellY < 0 ||
1536 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1537 Log.e(TAG, "Error loading shortcut " + item
1538 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1539 + item.cellX + "," + item.cellY
1540 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1541 return false;
1542 }
1543
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001544 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001545 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1546 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001548 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001550 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001551 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001553 return false;
1554 }
1555 }
1556 }
1557 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1558 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001560 }
1561 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001562
Joe Onorato36115782010-06-17 13:28:48 -04001563 return true;
1564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565
Winson Chungba9c37f2013-08-30 14:11:37 -07001566 /** Clears all the sBg data structures */
1567 private void clearSBgDataStructures() {
1568 synchronized (sBgLock) {
1569 sBgWorkspaceItems.clear();
1570 sBgAppWidgets.clear();
1571 sBgFolders.clear();
1572 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001573 sBgWorkspaceScreens.clear();
1574 }
1575 }
1576
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001577 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001578 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579
Joe Onorato36115782010-06-17 13:28:48 -04001580 final Context context = mContext;
1581 final ContentResolver contentResolver = context.getContentResolver();
1582 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001583 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001584 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001585 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001586
Winson Chung892c74d2013-08-22 16:15:50 -07001587 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001588 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001589 int countX = profile.numColumns;
1590 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001591
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001592 boolean clearDb = false;
Sunny Goyalf076eae2016-01-11 12:25:10 -08001593 if (GridSizeMigrationTask.ENABLED &&
1594 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1595 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001596 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001597 }
1598
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001599 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001600 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001601 LauncherSettings.Settings.call(contentResolver,
1602 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001603 }
1604
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001605 Log.d(TAG, "loadWorkspace: loading default favorites");
1606 LauncherSettings.Settings.call(contentResolver,
1607 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001608
Winson Chung2abf94d2012-07-18 18:16:38 -07001609 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001610 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001611 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001612 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001613 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001614
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001615 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1616 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001617 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001618 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001619 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001620
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001621 // +1 for the hotseat (it can be larger than the workspace)
1622 // Load workspace in reverse order to ensure that latest items are loaded first (and
1623 // before any earlier duplicates)
Sunny Goyale2df0622015-04-24 11:27:00 -07001624 final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001625 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001627 try {
1628 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1629 final int intentIndex = c.getColumnIndexOrThrow
1630 (LauncherSettings.Favorites.INTENT);
1631 final int titleIndex = c.getColumnIndexOrThrow
1632 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001633 final int containerIndex = c.getColumnIndexOrThrow(
1634 LauncherSettings.Favorites.CONTAINER);
1635 final int itemTypeIndex = c.getColumnIndexOrThrow(
1636 LauncherSettings.Favorites.ITEM_TYPE);
1637 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1638 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001639 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1640 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001641 final int screenIndex = c.getColumnIndexOrThrow(
1642 LauncherSettings.Favorites.SCREEN);
1643 final int cellXIndex = c.getColumnIndexOrThrow
1644 (LauncherSettings.Favorites.CELLX);
1645 final int cellYIndex = c.getColumnIndexOrThrow
1646 (LauncherSettings.Favorites.CELLY);
1647 final int spanXIndex = c.getColumnIndexOrThrow
1648 (LauncherSettings.Favorites.SPANX);
1649 final int spanYIndex = c.getColumnIndexOrThrow(
1650 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001651 final int rankIndex = c.getColumnIndexOrThrow(
1652 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001653 final int restoredIndex = c.getColumnIndexOrThrow(
1654 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001655 final int profileIdIndex = c.getColumnIndexOrThrow(
1656 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001657 final int optionsIndex = c.getColumnIndexOrThrow(
1658 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001659 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660
Sunny Goyal7f834d22015-04-21 10:10:23 -07001661 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001662 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001663 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001664 long serialNo = mUserManager.getSerialNumberForUser(user);
1665 allUsers.put(serialNo, user);
1666 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001667 }
1668
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 ShortcutInfo info;
1670 String intentDescription;
1671 LauncherAppWidgetInfo appWidgetInfo;
1672 int container;
1673 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001674 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001676 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001677 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 while (!mStopped && c.moveToNext()) {
1680 try {
1681 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001682 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001683 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001684 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001686 switch (itemType) {
1687 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1688 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001689 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001691 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001692 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001693 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001694 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001695 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001696 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001697 if (user == null) {
1698 // User has been deleted remove the item.
1699 itemsToRemove.add(id);
1700 continue;
1701 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 try {
1703 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001704 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001705 if (cn != null && cn.getPackageName() != null) {
1706 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1707 cn.getPackageName(), user);
1708 boolean validComponent = validPkg &&
1709 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001710 if (validPkg) {
1711 targetPackage = cn.getPackageName();
1712 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001713
1714 if (validComponent) {
1715 if (restored) {
1716 // no special handling necessary for this item
1717 restoredRows.add(id);
1718 restored = false;
1719 }
Kenny Guyff05f432016-01-22 17:48:29 +00001720 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001721 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001722 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001723 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001724 intent = null;
1725 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1726 // We allow auto install apps to have their intent
1727 // updated after an install.
1728 intent = manager.getLaunchIntentForPackage(
1729 cn.getPackageName());
1730 if (intent != null) {
1731 ContentValues values = new ContentValues();
1732 values.put(LauncherSettings.Favorites.INTENT,
1733 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001734 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001735 }
1736 }
1737
1738 if (intent == null) {
1739 // The app is installed but the component is no
1740 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001741 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001742 itemsToRemove.add(id);
1743 continue;
1744 } else {
1745 // no special handling necessary for this item
1746 restoredRows.add(id);
1747 restored = false;
1748 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001749 } else if (restored) {
1750 // Package is not yet available but might be
1751 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001752 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001753
1754 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1755 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001756 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001757 // App restore has started. Update the flag
1758 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1759 ContentValues values = new ContentValues();
1760 values.put(LauncherSettings.Favorites.RESTORED,
1761 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001762 updateItem(id, values);
1763 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1764 // This is a common app. Try to replace this.
1765 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1766 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1767 if (parser.findDefaultApp()) {
1768 // Default app found. Replace it.
1769 intent = parser.parsedIntent;
1770 cn = intent.getComponent();
1771 ContentValues values = parser.parsedValues;
1772 values.put(LauncherSettings.Favorites.RESTORED, 0);
1773 updateItem(id, values);
1774 restored = false;
1775 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001776
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001777 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001778 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001779 itemsToRemove.add(id);
1780 continue;
1781 }
Sunny Goyal94485362014-09-18 16:13:58 -07001782 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001783 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001784 itemsToRemove.add(id);
1785 continue;
1786 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001787 } else if (PackageManagerHelper.isAppOnSdcard(
1788 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001789 // Package is present but not available.
1790 allowMissingTarget = true;
1791 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1792 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001793 // SdCard is not ready yet. Package might get available,
1794 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001795 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001796 HashSet<String> pkgs = sPendingPackages.get(user);
1797 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001798 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001799 sPendingPackages.put(user, pkgs);
1800 }
1801 pkgs.add(cn.getPackageName());
1802 allowMissingTarget = true;
1803 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001804
1805 } else {
1806 // Do not wait for external media load anymore.
1807 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001808 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001809 itemsToRemove.add(id);
1810 continue;
Winson Chungee055712013-07-30 14:46:24 -07001811 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001812 } else if (cn == null) {
1813 // For shortcuts with no component, keep them as they are
1814 restoredRows.add(id);
1815 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001816 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001818 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001819 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 continue;
1821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822
Sunny Goyal34b65272015-03-11 16:56:52 -07001823 boolean useLowResIcon = container >= 0 &&
1824 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1825
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001826 if (itemReplaced) {
1827 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001828 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001829 cursorIconInfo.iconIndex, titleIndex,
1830 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001831 } else {
1832 // Don't replace items for other profiles.
1833 itemsToRemove.add(id);
1834 continue;
1835 }
1836 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001837 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001838 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001839 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001840 intent = getRestoredItemIntent(c, context, intent);
1841 } else {
1842 // Don't restore items for other profiles.
1843 itemsToRemove.add(id);
1844 continue;
1845 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001846 } else if (itemType ==
1847 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001848 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001849 cursorIconInfo.iconIndex, titleIndex,
1850 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001852 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001853
Sunny Goyald09c3702016-04-06 16:18:20 -07001854 // Shortcuts are only available on the primary profile
1855 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1856 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1857 }
1858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 // App shortcuts that used to be automatically added to Launcher
1860 // didn't always have the correct intent flags set, so do that
1861 // here
1862 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001863 intent.getCategories() != null &&
1864 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001865 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 intent.addFlags(
1867 Intent.FLAG_ACTIVITY_NEW_TASK |
1868 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1869 }
Michael Jurka96879562012-03-22 05:54:33 -07001870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001872 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001873 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001876 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 info.cellX = c.getInt(cellXIndex);
1878 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001879 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001880 info.spanX = 1;
1881 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001882 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001883 if (info.promisedIntent != null) {
1884 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1885 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001886 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001887 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1888 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1889 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001890
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001892 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001893 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 break;
1895 }
1896
Sunny Goyal756adbc2015-04-16 15:20:51 -07001897 if (restored) {
1898 ComponentName cn = info.getTargetComponent();
1899 if (cn != null) {
1900 Integer progress = installingPkgs.get(cn.getPackageName());
1901 if (progress != null) {
1902 info.setInstallProgress(progress);
1903 } else {
1904 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1905 }
1906 }
1907 }
1908
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 switch (container) {
1910 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1911 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1912 sBgWorkspaceItems.add(info);
1913 break;
1914 default:
1915 // Item is in a user folder
1916 FolderInfo folderInfo =
1917 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07001918 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 break;
1920 }
1921 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001922 } else {
1923 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 }
1925 break;
1926
1927 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1928 id = c.getLong(idIndex);
1929 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1930
Sunny Goyala508e4f2015-05-21 09:33:57 -07001931 // Do not trim the folder label, as is was set by the user.
1932 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001933 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001934 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001936 folderInfo.cellX = c.getInt(cellXIndex);
1937 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001938 folderInfo.spanX = 1;
1939 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001940 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941
Daniel Sandler8802e962010-05-26 16:28:16 -04001942 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001943 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001944 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001945 break;
1946 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001947
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001949 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1950 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1951 sBgWorkspaceItems.add(folderInfo);
1952 break;
Joe Onorato36115782010-06-17 13:28:48 -04001953 }
Joe Onorato17a89222011-02-08 17:26:11 -08001954
Chris Wrenf4d08112014-01-16 18:13:56 -05001955 if (restored) {
1956 // no special handling required for restored folders
1957 restoredRows.add(id);
1958 }
1959
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001960 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1961 sBgFolders.put(folderInfo.id, folderInfo);
1962 break;
1963
1964 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001965 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001967 boolean customWidget = itemType ==
1968 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1969
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001971 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001972 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001973 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001974 user = allUsers.get(serialNumber);
1975 if (user == null) {
1976 itemsToRemove.add(id);
1977 continue;
1978 }
1979
Sunny Goyalff572272014-07-23 13:58:07 -07001980 final ComponentName component =
1981 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001982
Sunny Goyal651077b2014-06-30 14:15:31 -07001983 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001984 final boolean isIdValid = (restoreStatus &
1985 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001986 final boolean wasProviderReady = (restoreStatus &
1987 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001988
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001989 if (widgetProvidersMap == null) {
1990 widgetProvidersMap = AppWidgetManagerCompat
1991 .getInstance(mContext).getAllProvidersMap();
1992 }
1993 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1994 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001995 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001996 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001997
1998 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001999 if (!isSafeMode && !customWidget &&
2000 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002001 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002002 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 itemsToRemove.add(id);
2004 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002005 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002006 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2007 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002008
Sunny Goyal53f96722015-07-13 19:54:53 -07002009 // The provider is available. So the widget is either
2010 // available or not available. We do not need to track
2011 // any future restore updates.
2012 int status = restoreStatus &
2013 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002014 if (!wasProviderReady) {
2015 // If provider was not previously ready, update the
2016 // status and UI flag.
2017
2018 // Id would be valid only if the widget restore broadcast was received.
2019 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002020 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002021 } else {
2022 status &= ~LauncherAppWidgetInfo
2023 .FLAG_PROVIDER_NOT_READY;
2024 }
2025 }
2026 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002027 } else {
2028 Log.v(TAG, "Widget restore pending id=" + id
2029 + " appWidgetId=" + appWidgetId
2030 + " status =" + restoreStatus);
2031 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002032 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002033 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002034 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002035
2036 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2037 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002038 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002039 // App restore has started. Update the flag
2040 appWidgetInfo.restoreStatus |=
2041 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002042 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002043 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002044 itemsToRemove.add(id);
2045 continue;
2046 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002047
2048 appWidgetInfo.installProgress =
2049 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002050 }
Sunny Goyalff572272014-07-23 13:58:07 -07002051
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002053 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 appWidgetInfo.cellX = c.getInt(cellXIndex);
2055 appWidgetInfo.cellY = c.getInt(cellYIndex);
2056 appWidgetInfo.spanX = c.getInt(spanXIndex);
2057 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002058 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002059
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2061 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2062 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002063 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2064 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 continue;
2066 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002068 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002070 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002071 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 break;
2073 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002074
Adam Cohen59400422014-03-05 18:07:04 -08002075 if (!customWidget) {
2076 String providerName =
2077 appWidgetInfo.providerName.flattenToString();
2078 if (!providerName.equals(savedProvider) ||
2079 (appWidgetInfo.restoreStatus != restoreStatus)) {
2080 ContentValues values = new ContentValues();
2081 values.put(
2082 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2083 providerName);
2084 values.put(LauncherSettings.Favorites.RESTORED,
2085 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002086 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002087 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002088 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2090 sBgAppWidgets.add(appWidgetInfo);
2091 }
Joe Onorato36115782010-06-17 13:28:48 -04002092 break;
2093 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002095 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002096 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 }
2098 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002099 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 }
2101
Winson Chungba9c37f2013-08-30 14:11:37 -07002102 // Break early if we've stopped loading
2103 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002104 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002105 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002106 }
2107
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002109 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002110 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2111 Utilities.createDbSelectionQuery(
2112 LauncherSettings.Favorites._ID, itemsToRemove), null);
2113 if (DEBUG_LOADERS) {
2114 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2115 LauncherSettings.Favorites._ID, itemsToRemove));
2116 }
2117
2118 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002119 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2120 .call(contentResolver,
2121 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2122 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2123 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002124 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2125 sBgFolders.remove(folderId);
2126 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002127 }
2128 }
2129
Sunny Goyal317698b2015-07-29 11:45:41 -07002130 // Sort all the folder items and make sure the first 3 items are high resolution.
2131 for (FolderInfo folder : sBgFolders) {
2132 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2133 int pos = 0;
2134 for (ShortcutInfo info : folder.contents) {
2135 if (info.usingLowResIcon) {
2136 info.updateIcon(mIconCache, false);
2137 }
2138 pos ++;
2139 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2140 break;
2141 }
2142 }
2143 }
2144
Chris Wrenf4d08112014-01-16 18:13:56 -05002145 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002146 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002147 ContentValues values = new ContentValues();
2148 values.put(LauncherSettings.Favorites.RESTORED, 0);
2149 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2150 Utilities.createDbSelectionQuery(
2151 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002152 }
2153
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002154 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2155 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002156 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002157 null, sWorker);
2158 }
2159
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002160 // Remove any empty screens
2161 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002162 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002163 long screenId = item.screenId;
2164 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2165 unusedScreens.contains(screenId)) {
2166 unusedScreens.remove(screenId);
2167 }
2168 }
2169
2170 // If there are any empty screens remove them, and update.
2171 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002172 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002173 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002174 }
2175
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 if (DEBUG_LOADERS) {
2177 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2178 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002180 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002182
Sunny Goyale2df0622015-04-24 11:27:00 -07002183 for (int i = 0; i < nScreens; i++) {
2184 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002185 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186 line += " | ";
2187 }
Sunny Goyale2df0622015-04-24 11:27:00 -07002188 ItemInfo[][] screen = occupied.valueAt(i);
Winson Chung892c74d2013-08-22 16:15:50 -07002189 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002190 if (x < screen.length && y < screen[x].length) {
2191 line += (screen[x][y] != null) ? "#" : ".";
2192 } else {
2193 line += "!";
2194 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 }
Joe Onorato36115782010-06-17 13:28:48 -04002196 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 }
Joe Onorato36115782010-06-17 13:28:48 -04002200 }
Adam Cohene25af792013-06-06 23:08:25 -07002201 }
2202
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002203 /**
2204 * Partially updates the item without any notification. Must be called on the worker thread.
2205 */
2206 private void updateItem(long itemId, ContentValues update) {
2207 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002208 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002209 update,
2210 BaseColumns._ID + "= ?",
2211 new String[]{Long.toString(itemId)});
2212 }
2213
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214 /** Filters the set of items who are directly or indirectly (via another container) on the
2215 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002216 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 ArrayList<ItemInfo> allWorkspaceItems,
2218 ArrayList<ItemInfo> currentScreenItems,
2219 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002220 // Purge any null ItemInfos
2221 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2222 while (iter.hasNext()) {
2223 ItemInfo i = iter.next();
2224 if (i == null) {
2225 iter.remove();
2226 }
2227 }
2228
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 // Order the set of items by their containers first, this allows use to walk through the
2230 // list sequentially, build up a list of containers that are in the specified screen,
2231 // as well as all items in those containers.
2232 Set<Long> itemsOnScreen = new HashSet<Long>();
2233 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2234 @Override
2235 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002236 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 }
2238 });
2239 for (ItemInfo info : allWorkspaceItems) {
2240 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002241 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 currentScreenItems.add(info);
2243 itemsOnScreen.add(info.id);
2244 } else {
2245 otherScreenItems.add(info);
2246 }
2247 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2248 currentScreenItems.add(info);
2249 itemsOnScreen.add(info.id);
2250 } else {
2251 if (itemsOnScreen.contains(info.container)) {
2252 currentScreenItems.add(info);
2253 itemsOnScreen.add(info.id);
2254 } else {
2255 otherScreenItems.add(info);
2256 }
2257 }
2258 }
2259 }
2260
2261 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002262 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 ArrayList<LauncherAppWidgetInfo> appWidgets,
2264 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2265 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266
2267 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002268 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002270 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002271 currentScreenWidgets.add(widget);
2272 } else {
2273 otherScreenWidgets.add(widget);
2274 }
2275 }
2276 }
2277
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002278 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2279 * right) */
2280 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002281 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002282 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002283 final int screenCols = profile.numColumns;
2284 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002286 @Override
2287 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002288 if (lhs.container == rhs.container) {
2289 // Within containers, order by their spatial position in that container
2290 switch ((int) lhs.container) {
2291 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2292 long lr = (lhs.screenId * screenCellCount +
2293 lhs.cellY * screenCols + lhs.cellX);
2294 long rr = (rhs.screenId * screenCellCount +
2295 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002296 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002297 }
2298 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2299 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002300 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002301 }
2302 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002303 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002304 throw new RuntimeException("Unexpected container type when " +
2305 "sorting workspace items.");
2306 }
2307 return 0;
2308 }
2309 } else {
2310 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002311 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002312 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002313 }
2314 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002316
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2318 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 final Runnable r = new Runnable() {
2320 @Override
2321 public void run() {
2322 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2323 if (callbacks != null) {
2324 callbacks.bindScreens(orderedScreens);
2325 }
2326 }
2327 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002328 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 }
2330
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2332 final ArrayList<ItemInfo> workspaceItems,
2333 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002334 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335
2336 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002337 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002339 final int start = i;
2340 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341 final Runnable r = new Runnable() {
2342 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002343 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002344 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002345 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002346 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2347 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002348 }
2349 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002351 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002352 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002354 // Bind the widgets, one at a time
2355 N = appWidgets.size();
2356 for (int i = 0; i < N; i++) {
2357 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2358 final Runnable r = new Runnable() {
2359 public void run() {
2360 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2361 if (callbacks != null) {
2362 callbacks.bindAppWidget(widget);
2363 }
2364 }
2365 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002366 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002367 }
2368 }
2369
2370 /**
2371 * Binds all loaded data to actual views on the main thread.
2372 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002373 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 final long t = SystemClock.uptimeMillis();
2375 Runnable r;
2376
2377 // Don't use these two variables in any of the callback runnables.
2378 // Otherwise we hold a reference to them.
2379 final Callbacks oldCallbacks = mCallbacks.get();
2380 if (oldCallbacks == null) {
2381 // This launcher has exited and nobody bothered to tell us. Just bail.
2382 Log.w(TAG, "LoaderTask running with no launcher");
2383 return;
2384 }
2385
Winson Chung9b9fb962013-11-15 15:39:34 -08002386 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002387 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2388 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2389 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002390
Winson Chung2abf94d2012-07-18 18:16:38 -07002391 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392 workspaceItems.addAll(sBgWorkspaceItems);
2393 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002394 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002395 }
2396
Derek Prothro7aff3992013-12-10 14:00:37 -05002397 final boolean isLoadingSynchronously =
2398 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002399 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002400 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002401 if (currScreen >= orderedScreenIds.size()) {
2402 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002403 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002404 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002405 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002406 final long currentScreenId = currentScreen < 0
2407 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002408
Winson Chung9b9fb962013-11-15 15:39:34 -08002409 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002410 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2411 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2412 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2413 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002414
Winson Chung9b9fb962013-11-15 15:39:34 -08002415 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002416 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002417 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002419 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2420 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2421
2422 // Tell the workspace that we're about to start binding items
2423 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002424 public void run() {
2425 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2426 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002427 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002429 }
2430 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002432 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433
Adam Cohendcd297f2013-06-18 13:13:40 -07002434 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2435
Sunny Goyal527c7d32015-08-28 15:19:36 -07002436 Executor mainExecutor = new DeferredMainThreadExecutor();
2437 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002438 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439
Sunny Goyal527c7d32015-08-28 15:19:36 -07002440 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2441 // remaining screens after first onDraw is called. This ensures that the first screen
2442 // is immediately visible (eg. during rotation)
2443 // In case of !isLoadingSynchronously, bind all pages one after other.
2444 final Executor deferredExecutor = isLoadingSynchronously ?
2445 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2446
Sunny Goyal44c06432016-04-02 10:56:02 -07002447 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448
2449 // Tell the workspace that we're done binding items
2450 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002451 public void run() {
2452 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2453 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002454 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002455 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456
Sunny Goyal639e9062015-08-19 19:17:06 -07002457 mIsLoadingAndBindingWorkspace = false;
2458
2459 // Run all the bind complete runnables after workspace is bound.
2460 if (!mBindCompleteRunnables.isEmpty()) {
2461 synchronized (mBindCompleteRunnables) {
2462 for (final Runnable r : mBindCompleteRunnables) {
2463 runOnWorkerThread(r);
2464 }
2465 mBindCompleteRunnables.clear();
2466 }
2467 }
2468
Winson Chung98e030b2012-05-07 16:01:11 -07002469 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002470 if (DEBUG_LOADERS) {
2471 Log.d(TAG, "bound workspace in "
2472 + (SystemClock.uptimeMillis()-t) + "ms");
2473 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002474
Joe Onorato36115782010-06-17 13:28:48 -04002475 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002477 deferredExecutor.execute(r);
2478
Winson Chung4a2afa32012-07-19 14:53:05 -07002479 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002480 r = new Runnable() {
2481 public void run() {
2482 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2483 if (callbacks != null) {
2484 // We are loading synchronously, which means, some of the pages will be
2485 // bound after first draw. Inform the callbacks that page binding is
2486 // not complete, and schedule the remaining pages.
2487 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2488 callbacks.onPageBoundSynchronously(currentScreen);
2489 }
2490 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2491 }
2492 }
2493 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002494 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002495 }
Joe Onorato36115782010-06-17 13:28:48 -04002496 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002497
Joe Onorato36115782010-06-17 13:28:48 -04002498 private void loadAndBindAllApps() {
2499 if (DEBUG_LOADERS) {
2500 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2501 }
2502 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002503 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002504 synchronized (LoaderTask.this) {
2505 if (mStopped) {
2506 return;
2507 }
2508 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002509 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002510 synchronized (LoaderTask.this) {
2511 if (mStopped) {
2512 return;
2513 }
2514 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002515 }
Joe Onorato36115782010-06-17 13:28:48 -04002516 } else {
2517 onlyBindAllApps();
2518 }
2519 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002520
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002521 private void updateIconCache() {
2522 // Ignore packages which have a promise icon.
2523 HashSet<String> packagesToIgnore = new HashSet<>();
2524 synchronized (sBgLock) {
2525 for (ItemInfo info : sBgItemsIdMap) {
2526 if (info instanceof ShortcutInfo) {
2527 ShortcutInfo si = (ShortcutInfo) info;
2528 if (si.isPromise() && si.getTargetComponent() != null) {
2529 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2530 }
2531 } else if (info instanceof LauncherAppWidgetInfo) {
2532 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2533 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2534 packagesToIgnore.add(lawi.providerName.getPackageName());
2535 }
2536 }
2537 }
2538 }
2539 mIconCache.updateDbIcons(packagesToIgnore);
2540 }
2541
Joe Onorato36115782010-06-17 13:28:48 -04002542 private void onlyBindAllApps() {
2543 final Callbacks oldCallbacks = mCallbacks.get();
2544 if (oldCallbacks == null) {
2545 // This launcher has exited and nobody bothered to tell us. Just bail.
2546 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2547 return;
2548 }
2549
2550 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002551 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002552 final ArrayList<AppInfo> list
2553 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002554 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002555 public void run() {
2556 final long t = SystemClock.uptimeMillis();
2557 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2558 if (callbacks != null) {
2559 callbacks.bindAllApplications(list);
2560 }
2561 if (DEBUG_LOADERS) {
2562 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002563 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002564 }
2565 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002566 };
2567 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002568 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002569 r.run();
2570 } else {
2571 mHandler.post(r);
2572 }
Joe Onorato36115782010-06-17 13:28:48 -04002573 }
2574
Winson Chung64359a52013-07-08 17:17:08 -07002575 private void loadAllApps() {
2576 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002577
Joe Onorato36115782010-06-17 13:28:48 -04002578 final Callbacks oldCallbacks = mCallbacks.get();
2579 if (oldCallbacks == null) {
2580 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002581 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002582 return;
2583 }
2584
Kenny Guyed131872014-04-30 03:02:21 +01002585 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2586
Winson Chung64359a52013-07-08 17:17:08 -07002587 // Clear the list of apps
2588 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002589 for (UserHandleCompat user : profiles) {
2590 // Query for the set of apps
2591 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002592 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002593 if (DEBUG_LOADERS) {
2594 Log.d(TAG, "getActivityList took "
2595 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2596 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2597 }
2598 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002599 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002600 if (apps == null || apps.isEmpty()) {
2601 return;
2602 }
Kenny Guyff05f432016-01-22 17:48:29 +00002603 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002604 // Create the ApplicationInfos
2605 for (int i = 0; i < apps.size(); i++) {
2606 LauncherActivityInfoCompat app = apps.get(i);
2607 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002608 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002609 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002610
Sunny Goyal756a28a2015-04-23 17:07:55 -07002611 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2612 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002613 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002614
2615 @Override
2616 public void run() {
2617 heuristic.processUserApps(apps);
2618 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002619 };
2620 runOnMainThread(new Runnable() {
2621
2622 @Override
2623 public void run() {
2624 // Check isLoadingWorkspace on the UI thread, as it is updated on
2625 // the UI thread.
2626 if (mIsLoadingAndBindingWorkspace) {
2627 synchronized (mBindCompleteRunnables) {
2628 mBindCompleteRunnables.add(r);
2629 }
2630 } else {
2631 runOnWorkerThread(r);
2632 }
2633 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002634 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002635 }
Winson Chung64359a52013-07-08 17:17:08 -07002636 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002637 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002638 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2639 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002640
2641 // Post callback on main thread
2642 mHandler.post(new Runnable() {
2643 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002644
Winson Chung64359a52013-07-08 17:17:08 -07002645 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002646 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002647 if (callbacks != null) {
2648 callbacks.bindAllApplications(added);
2649 if (DEBUG_LOADERS) {
2650 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002651 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002652 }
2653 } else {
2654 Log.i(TAG, "not binding apps: no Launcher activity");
2655 }
2656 }
2657 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002658 // Cleanup any data stored for a deleted user.
2659 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002660 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002661 Log.d(TAG, "Icons processed in "
2662 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002663 }
2664 }
2665
2666 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002667 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002669 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2670 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2671 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2672 }
Joe Onorato36115782010-06-17 13:28:48 -04002673 }
2674 }
2675
Sunny Goyal75b0f552015-05-20 21:57:06 -07002676 /**
2677 * Called when the icons for packages have been updated in the icon cache.
2678 */
2679 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2680 final Callbacks callbacks = getCallback();
2681 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2682 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2683
2684 // If any package icon has changed (app was updated while launcher was dead),
2685 // update the corresponding shortcuts.
2686 synchronized (sBgLock) {
2687 for (ItemInfo info : sBgItemsIdMap) {
2688 if (info instanceof ShortcutInfo && user.equals(info.user)
2689 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2690 ShortcutInfo si = (ShortcutInfo) info;
2691 ComponentName cn = si.getTargetComponent();
2692 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2693 si.updateIcon(mIconCache);
2694 updatedShortcuts.add(si);
2695 }
2696 }
2697 }
2698 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2699 }
2700
2701 if (!updatedShortcuts.isEmpty()) {
2702 final UserHandleCompat userFinal = user;
2703 mHandler.post(new Runnable() {
2704
2705 public void run() {
2706 Callbacks cb = getCallback();
2707 if (cb != null && callbacks == cb) {
2708 cb.bindShortcutsChanged(updatedShortcuts,
2709 new ArrayList<ShortcutInfo>(), userFinal);
2710 }
2711 }
2712 });
2713 }
2714
2715 if (!updatedApps.isEmpty()) {
2716 mHandler.post(new Runnable() {
2717
2718 public void run() {
2719 Callbacks cb = getCallback();
2720 if (cb != null && callbacks == cb) {
2721 cb.bindAppsUpdated(updatedApps);
2722 }
2723 }
2724 });
2725 }
2726 }
2727
Joe Onorato36115782010-06-17 13:28:48 -04002728 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002729 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002730 }
2731
Adam Cohen091440a2015-03-18 14:16:05 -07002732 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002733
2734 @Override
2735 public void onReceive(Context context, Intent intent) {
2736 synchronized (sBgLock) {
2737 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2738 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002739 final PackageManager manager = context.getPackageManager();
2740 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2741 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002742 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2743 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002744 packagesRemoved.clear();
2745 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002746 for (String pkg : entry.getValue()) {
2747 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002748 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002749 packagesUnavailable.add(pkg);
2750 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002751 packagesRemoved.add(pkg);
2752 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002753 }
2754 }
2755 if (!packagesRemoved.isEmpty()) {
2756 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2757 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2758 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002759 if (!packagesUnavailable.isEmpty()) {
2760 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2761 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2762 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002763 }
Sunny Goyal34942622014-08-29 17:20:55 -07002764 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002765 }
2766 }
2767 }
2768
Joe Onorato36115782010-06-17 13:28:48 -04002769 private class PackageUpdatedTask implements Runnable {
2770 int mOp;
2771 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002772 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002773
2774 public static final int OP_NONE = 0;
2775 public static final int OP_ADD = 1;
2776 public static final int OP_UPDATE = 2;
2777 public static final int OP_REMOVE = 3; // uninstlled
2778 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002779 public static final int OP_SUSPEND = 5; // package suspended
2780 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002781 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002782
Kenny Guyed131872014-04-30 03:02:21 +01002783 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002784 mOp = op;
2785 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002786 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002787 }
2788
2789 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002790 if (!mHasLoaderCompletedOnce) {
2791 // Loader has not yet run.
2792 return;
2793 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002794 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002795
2796 final String[] packages = mPackages;
2797 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002798 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002799 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002800 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002801 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002802 for (int i=0; i<N; i++) {
2803 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002804 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002805 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002806 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002807
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002808 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2809 if (heuristic != null) {
2810 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002811 }
Joe Onorato36115782010-06-17 13:28:48 -04002812 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002813 }
Joe Onorato36115782010-06-17 13:28:48 -04002814 case OP_UPDATE:
2815 for (int i=0; i<N; i++) {
2816 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002817 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002818 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002819 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002820 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002821 // Since package was just updated, the target must be available now.
2822 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002823 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002824 case OP_REMOVE: {
2825 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2826 if (heuristic != null) {
2827 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002828 }
Joe Onorato36115782010-06-17 13:28:48 -04002829 for (int i=0; i<N; i++) {
2830 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002831 mIconCache.removeIconsForPkg(packages[i], mUser);
2832 }
2833 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002834 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002835 case OP_UNAVAILABLE:
2836 for (int i=0; i<N; i++) {
2837 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2838 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002839 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002840 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002841 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002842 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002843 case OP_SUSPEND:
2844 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002845 flagOp = mOp == OP_SUSPEND ?
2846 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2847 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002848 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002849 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2850 break;
2851 case OP_USER_AVAILABILITY_CHANGE:
2852 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2853 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2854 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2855 // We want to update all packages for this user.
2856 pkgFilter = StringFilter.matchesAll();
2857 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002858 break;
Joe Onorato36115782010-06-17 13:28:48 -04002859 }
2860
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002861 ArrayList<AppInfo> added = null;
2862 ArrayList<AppInfo> modified = null;
2863 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002864
Adam Cohen487f7dd2012-06-28 18:12:10 -07002865 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002866 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002867 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002868 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002869 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002870 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002871 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002872 }
Winson Chung5d55f332012-07-16 20:45:03 -07002873 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002874 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002875 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002876 }
2877
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002878 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002879
Joe Onorato36115782010-06-17 13:28:48 -04002880 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002881 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002882 for (AppInfo ai : added) {
2883 addedOrUpdatedApps.put(ai.componentName, ai);
2884 }
Joe Onorato36115782010-06-17 13:28:48 -04002885 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002886
Joe Onorato36115782010-06-17 13:28:48 -04002887 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002888 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002889 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002890 for (AppInfo ai : modified) {
2891 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002892 }
2893
Joe Onorato36115782010-06-17 13:28:48 -04002894 mHandler.post(new Runnable() {
2895 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002896 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002897 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002898 callbacks.bindAppsUpdated(modifiedFinal);
2899 }
2900 }
2901 });
2902 }
Winson Chung83892cc2013-05-01 16:53:33 -07002903
Sunny Goyal4390ace2014-10-13 11:33:11 -07002904 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002905 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002906 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
2907 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
2908 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
2909
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002910 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002911 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002912 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2913 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002914 boolean infoUpdated = false;
2915 boolean shortcutUpdated = false;
2916
2917 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002918 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07002919 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002920 Bitmap icon = Utilities.createIconBitmap(
2921 si.iconResource.packageName,
2922 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002923 if (icon != null) {
2924 si.setIcon(icon);
2925 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002926 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002927 }
2928 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002929
2930 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07002931 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002932 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2933
2934 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002935 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2936 // Auto install icon
2937 PackageManager pm = context.getPackageManager();
2938 ResolveInfo matched = pm.resolveActivity(
2939 new Intent(Intent.ACTION_MAIN)
2940 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2941 PackageManager.MATCH_DEFAULT_ONLY);
2942 if (matched == null) {
2943 // Try to find the best match activity.
2944 Intent intent = pm.getLaunchIntentForPackage(
2945 cn.getPackageName());
2946 if (intent != null) {
2947 cn = intent.getComponent();
2948 appInfo = addedOrUpdatedApps.get(cn);
2949 }
2950
2951 if ((intent == null) || (appInfo == null)) {
2952 removedShortcuts.add(si);
2953 continue;
2954 }
2955 si.promisedIntent = intent;
2956 }
2957 }
2958
2959 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07002960 if (appInfo != null) {
2961 si.flags = appInfo.flags;
2962 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002963
Sunny Goyal756adbc2015-04-16 15:20:51 -07002964 si.intent = si.promisedIntent;
2965 si.promisedIntent = null;
2966 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002967 infoUpdated = true;
2968 si.updateIcon(mIconCache);
2969 }
2970
2971 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2972 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2973 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002974 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002975 si.contentDescription = appInfo.contentDescription;
2976 infoUpdated = true;
2977 }
2978
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002979 int oldDisabledFlags = si.isDisabled;
2980 si.isDisabled = flagOp.apply(si.isDisabled);
2981 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002982 shortcutUpdated = true;
2983 }
2984 }
2985
2986 if (infoUpdated || shortcutUpdated) {
2987 updatedShortcuts.add(si);
2988 }
2989 if (infoUpdated) {
2990 updateItemInDatabase(context, si);
2991 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002992 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002993 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2994 if (mUser.equals(widgetInfo.user)
2995 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07002996 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002997 widgetInfo.restoreStatus &=
2998 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2999 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003000
3001 // adding this flag ensures that launcher shows 'click to setup'
3002 // if the widget has a config activity. In case there is no config
3003 // activity, it will be marked as 'restored' during bind.
3004 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3005
Sunny Goyal4390ace2014-10-13 11:33:11 -07003006 widgets.add(widgetInfo);
3007 updateItemInDatabase(context, widgetInfo);
3008 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003009 }
3010 }
3011 }
3012
Sunny Goyal4390ace2014-10-13 11:33:11 -07003013 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003014 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003015 mHandler.post(new Runnable() {
3016
3017 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003018 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003019 if (callbacks == cb && cb != null) {
3020 callbacks.bindShortcutsChanged(
3021 updatedShortcuts, removedShortcuts, mUser);
3022 }
3023 }
3024 });
3025 if (!removedShortcuts.isEmpty()) {
3026 deleteItemsFromDatabase(context, removedShortcuts);
3027 }
3028 }
3029 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003030 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003031 mHandler.post(new Runnable() {
3032 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003033 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003034 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003035 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003036 }
3037 }
3038 });
3039 }
3040 }
3041
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003042 final HashSet<String> removedPackages = new HashSet<>();
3043 final HashSet<ComponentName> removedComponents = new HashSet<>();
3044 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003045 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003046 Collections.addAll(removedPackages, packages);
3047
3048 // No need to update the removedComponents as
3049 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003050 } else if (mOp == OP_UPDATE) {
3051 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003052 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003053 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003054 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003055 }
3056 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003057
3058 // Update removedComponents as some components can get removed during package update
3059 for (AppInfo info : removedApps) {
3060 removedComponents.add(info.componentName);
3061 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003062 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003063
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003064 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3065 for (String pn : removedPackages) {
3066 deletePackageFromDatabase(context, pn, mUser);
3067 }
3068 for (ComponentName cn : removedComponents) {
3069 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003070 }
3071
Winson Chungdf95eb12013-10-16 14:57:07 -07003072 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003073 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3074
Winson Chungdf95eb12013-10-16 14:57:07 -07003075 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003076 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003077 mHandler.post(new Runnable() {
3078 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003079 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003080 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003081 callbacks.bindWorkspaceComponentsRemoved(
3082 removedPackages, removedComponents, mUser);
3083 }
3084 }
3085 });
3086 }
3087
3088 if (!removedApps.isEmpty()) {
3089 // Remove corresponding apps from All-Apps
3090 final Callbacks callbacks = getCallback();
3091 mHandler.post(new Runnable() {
3092 public void run() {
3093 Callbacks cb = getCallback();
3094 if (callbacks == cb && cb != null) {
3095 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003096 }
3097 }
3098 });
Joe Onoratobe386092009-11-17 17:32:16 -08003099 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003100
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003101 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3102 // get widget update signals.
3103 if (!Utilities.ATLEAST_MARSHMALLOW &&
3104 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003105 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003106 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003107 public void run() {
3108 Callbacks cb = getCallback();
3109 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003110 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003111 }
3112 }
3113 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003114 }
3115 }
3116 }
3117
3118 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3119 mHandler.post(new Runnable() {
3120 @Override
3121 public void run() {
3122 Callbacks cb = getCallback();
3123 if (callbacks == cb && cb != null) {
3124 callbacks.bindWidgetsModel(model);
3125 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003126 }
3127 });
3128 }
3129
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003130 public void refreshAndBindWidgetsAndShortcuts(
3131 final Callbacks callbacks, final boolean bindFirst) {
3132 runOnWorkerThread(new Runnable() {
3133 @Override
3134 public void run() {
3135 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3136 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003137 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003138 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3139 bindWidgetsModel(callbacks, model);
3140 // update the Widget entries inside DB on the worker thread.
3141 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3142 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003143 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003144 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003145 }
3146
Adam Cohen091440a2015-03-18 14:16:05 -07003147 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003148 UserHandleCompat user) {
3149 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3150 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003151 }
Adam Cohen556f6132014-01-15 15:18:08 -08003152
Kenny Guyed131872014-04-30 03:02:21 +01003153 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3154 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003155 if (cn == null) {
3156 return false;
3157 }
Kenny Guyed131872014-04-30 03:02:21 +01003158 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3159 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003160 return false;
3161 }
Kenny Guyed131872014-04-30 03:02:21 +01003162 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003163 }
3164
Adam Cohena28b78e2014-05-20 17:03:04 -07003165 public static boolean isValidPackage(Context context, String packageName,
3166 UserHandleCompat user) {
3167 if (packageName == null) {
3168 return false;
3169 }
3170 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3171 return launcherApps.isPackageEnabledForProfile(packageName, user);
3172 }
3173
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003175 * Make an ShortcutInfo object for a restored application or shortcut item that points
3176 * to a package that is not yet installed on the system.
3177 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003178 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003179 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003180 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003181 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003182
3183 Bitmap icon = iconInfo.loadIcon(c, info, context);
3184 // the fallback icon
3185 if (icon == null) {
3186 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3187 } else {
3188 info.setIcon(icon);
3189 }
Sunny Goyal34942622014-08-29 17:20:55 -07003190
3191 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003192 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003193 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003194 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003195 }
Sunny Goyal34942622014-08-29 17:20:55 -07003196 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3197 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003198 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003199 }
Sunny Goyal34942622014-08-29 17:20:55 -07003200 } else {
3201 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3202 }
3203
Winson Chung82b016c2015-05-08 17:00:10 -07003204 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003205 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003206 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003207 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003208 return info;
3209 }
3210
3211 /**
3212 * Make an Intent object for a restored application or shortcut item that points
3213 * to the market page for the item.
3214 */
Adam Cohen091440a2015-03-18 14:16:05 -07003215 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003216 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003217 return getMarketIntent(componentName.getPackageName());
3218 }
3219
3220 static Intent getMarketIntent(String packageName) {
3221 return new Intent(Intent.ACTION_VIEW)
3222 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003223 .scheme("market")
3224 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003225 .appendQueryParameter("id", packageName)
3226 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003227 }
3228
3229 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003230 * Make an ShortcutInfo object for a shortcut that is an application.
3231 *
3232 * If c is not null, then it will be used to fill in missing data like the title and icon.
3233 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003234 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003235 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003236 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003237 if (user == null) {
3238 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003239 return null;
3240 }
3241
Kenny Guyed131872014-04-30 03:02:21 +01003242 ComponentName componentName = intent.getComponent();
3243 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003244 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003245 return null;
3246 }
3247
3248 Intent newIntent = new Intent(intent.getAction(), null);
3249 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3250 newIntent.setComponent(componentName);
3251 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003252 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003253 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3254 return null;
3255 }
3256
3257 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003258 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003259 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3260 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3261 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003262 }
3263
Sunny Goyald09c3702016-04-06 16:18:20 -07003264 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3265 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3266 }
3267
Joe Onorato56d82912010-03-07 14:32:10 -05003268 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003269 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003270 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003271 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003272
Joe Onorato56d82912010-03-07 14:32:10 -05003273 // fall back to the class name of the activity
3274 if (info.title == null) {
3275 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003276 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003277
Joe Onorato9c1289c2009-08-17 11:03:03 -04003278 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003279 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003280 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003281 if (lai != null) {
3282 info.flags = AppInfo.initFlags(lai);
3283 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003284 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003285 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003286
Sunny Goyale2df0622015-04-24 11:27:00 -07003287 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003288 ItemInfoFilter f) {
3289 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3290 for (ItemInfo i : infos) {
3291 if (i instanceof ShortcutInfo) {
3292 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003293 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003294 if (cn != null && f.filterItem(null, info, cn)) {
3295 filtered.add(info);
3296 }
3297 } else if (i instanceof FolderInfo) {
3298 FolderInfo info = (FolderInfo) i;
3299 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003300 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003301 if (cn != null && f.filterItem(info, s, cn)) {
3302 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003303 }
3304 }
Winson Chung64359a52013-07-08 17:17:08 -07003305 } else if (i instanceof LauncherAppWidgetInfo) {
3306 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3307 ComponentName cn = info.providerName;
3308 if (cn != null && f.filterItem(null, info, cn)) {
3309 filtered.add(info);
3310 }
Winson Chung8a435102012-08-30 17:16:53 -07003311 }
3312 }
Winson Chung64359a52013-07-08 17:17:08 -07003313 return new ArrayList<ItemInfo>(filtered);
3314 }
3315
Adam Cohen091440a2015-03-18 14:16:05 -07003316 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003317 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003318 ItemInfoFilter filter = new ItemInfoFilter() {
3319 @Override
3320 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003321 if (info.user == null) {
3322 return cn.equals(cname);
3323 } else {
3324 return cn.equals(cname) && info.user.equals(user);
3325 }
Winson Chung64359a52013-07-08 17:17:08 -07003326 }
3327 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003328 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003329 }
3330
Sunny Goyal1a745e82014-10-02 15:58:31 -07003331 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003332 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003333 */
Adam Cohen091440a2015-03-18 14:16:05 -07003334 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003335 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003336 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003337 // Non-app shortcuts are only supported for current user.
3338 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003340
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003341 // TODO: If there's an explicit component and we can't install that, delete it.
3342
Winson Chung82b016c2015-05-08 17:00:10 -07003343 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003344
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003345 Bitmap icon = iconInfo.loadIcon(c, info, context);
3346 // the fallback icon
3347 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003348 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003349 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003350 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003351 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 return info;
3353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003354
Sunny Goyal2350bc92014-10-14 16:42:54 -07003355 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003356 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3357 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3358 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3359
Adam Cohend9198822011-11-22 16:42:47 -08003360 if (intent == null) {
3361 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3362 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3363 return null;
3364 }
3365
Joe Onorato0589f0f2010-02-08 13:44:00 -08003366 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003367 boolean customIcon = false;
3368 ShortcutIconResource iconResource = null;
3369
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003370 if (bitmap instanceof Bitmap) {
3371 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003372 customIcon = true;
3373 } else {
3374 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003375 if (extra instanceof ShortcutIconResource) {
3376 iconResource = (ShortcutIconResource) extra;
3377 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003378 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003379 }
3380 }
3381
Michael Jurkac9d95c52011-08-29 14:03:34 -07003382 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003383
Kenny Guyed131872014-04-30 03:02:21 +01003384 // Only support intents for current user for now. Intents sent from other
3385 // users wouldn't get here without intent forwarding anyway.
3386 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003387 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003388 icon = mIconCache.getDefaultIcon(info.user);
3389 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003390 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003391 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003392
Winson Chung82b016c2015-05-08 17:00:10 -07003393 info.title = Utilities.trim(name);
3394 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003395 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003396 info.iconResource = iconResource;
3397
3398 return info;
3399 }
3400
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003402 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 * or make a new one.
3404 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003405 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 // See if a placeholder was created for us already
3407 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003408 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003410 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 folders.put(id, folderInfo);
3412 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003413 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003414 }
3415
Joe Onoratobe386092009-11-17 17:32:16 -08003416
Sunny Goyal651077b2014-06-30 14:15:31 -07003417 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3418 return (provider != null) && (provider.provider != null)
3419 && (provider.provider.getPackageName() != null);
3420 }
3421
Joe Onoratobe386092009-11-17 17:32:16 -08003422 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003423 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003424 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3425 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3426 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3427 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003428 if (mLoaderTask != null) {
3429 mLoaderTask.dumpState();
3430 } else {
3431 Log.d(TAG, "mLoaderTask=null");
3432 }
Joe Onoratobe386092009-11-17 17:32:16 -08003433 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003434
3435 public Callbacks getCallback() {
3436 return mCallbacks != null ? mCallbacks.get() : null;
3437 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003438
3439 /**
3440 * @return {@link FolderInfo} if its already loaded.
3441 */
3442 public FolderInfo findFolderById(Long folderId) {
3443 synchronized (sBgLock) {
3444 return sBgFolders.get(folderId);
3445 }
3446 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003447
Sunny Goyal527c7d32015-08-28 15:19:36 -07003448 @Thunk class DeferredMainThreadExecutor implements Executor {
3449
3450 @Override
3451 public void execute(Runnable command) {
3452 runOnMainThread(command);
3453 }
3454 }
3455
Sunny Goyal756adbc2015-04-16 15:20:51 -07003456 /**
3457 * @return the looper for the worker thread which can be used to start background tasks.
3458 */
3459 public static Looper getWorkerLooper() {
3460 return sWorkerThread.getLooper();
3461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003462}