blob: a5e703eb0b6b7689c7bed37f4815d8400e845126 [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
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040040import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080041import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080043import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070044import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010045
Sunny Goyalffe83f12014-08-14 17:39:34 -070046import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.LauncherActivityInfoCompat;
48import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070049import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070050import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010051import com.android.launcher3.compat.UserHandleCompat;
52import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070053import com.android.launcher3.config.FeatureFlags;
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;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070061import com.android.launcher3.provider.LauncherDbUtils;
Robin Lee26ace122015-03-16 19:41:43 +000062import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070063import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070064import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070065import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070066import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070067import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyald09c3702016-04-06 16:18:20 -070068import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070069import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070070import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070071import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070072import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080073
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.lang.ref.WeakReference;
75import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070076import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070077import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070078import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079import java.util.Collections;
80import java.util.Comparator;
81import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070083import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070085import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070086import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070087import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089/**
90 * Maintains in-memory state of the Launcher. It is expected that there should be only one
91 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070092 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 */
Kenny Guyed131872014-04-30 03:02:21 +010094public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010095 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080096 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040097 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070098 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040099
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700101
Joe Onorato36115782010-06-17 13:28:48 -0400102 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500103 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final LauncherAppState mApp;
106 @Thunk final Object mLock = new Object();
107 @Thunk DeferredHandler mHandler = new DeferredHandler();
108 @Thunk LoaderTask mLoaderTask;
109 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700110 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700113 static {
114 sWorkerThread.start();
115 }
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700117
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 // We start off with everything not loaded. After that, we assume that
119 // our monitoring of the package manager provides all updates and we never
120 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk boolean mWorkspaceLoaded;
122 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700123
Sunny Goyal756a28a2015-04-23 17:07:55 -0700124 /**
125 * Set of runnables to be called on the background thread after the workspace binding
126 * is complete.
127 */
128 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
129
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800133 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700134 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800135 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700137 // The lock that must be acquired before referencing any static bg data structures. Unlike
138 // other locks, this one can generally be held long-term because we never expect any of these
139 // static data structures to be referenced outside of the worker thread except on the first
140 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700141 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700142
Adam Cohen487f7dd2012-06-28 18:12:10 -0700143 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700145 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146
Adam Cohen487f7dd2012-06-28 18:12:10 -0700147 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
148 // created by LauncherModel that are directly on the home screen (however, no widgets or
149 // shortcuts within folders).
150 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
153 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154 new ArrayList<LauncherAppWidgetInfo>();
155
Adam Cohen487f7dd2012-06-28 18:12:10 -0700156 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700157 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700158
Adam Cohendcd297f2013-06-18 13:13:40 -0700159 // sBgWorkspaceScreens is the ordered set of workspace screens.
160 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
161
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700162 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700163 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
164 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700165
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700166 // </ only access in worker thread >
167
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Adam Cohen091440a2015-03-18 14:16:05 -0700170 @Thunk final LauncherAppsCompat mLauncherApps;
171 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100172
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700174 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700176 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700178 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
179 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700180 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800181 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200183 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700184 public void bindAppsAdded(ArrayList<Long> newScreens,
185 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700186 ArrayList<ItemInfo> addAnimated,
187 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700189 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
190 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
191 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700192 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700193 public void bindWorkspaceComponentsRemoved(
194 HashSet<String> packageNames, HashSet<ComponentName> components,
195 UserHandleCompat user);
196 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800197 public void notifyWidgetProvidersChanged();
198 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700199 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700200 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chung64359a52013-07-08 17:17:08 -0700203 public interface ItemInfoFilter {
204 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
205 }
206
Bjorn Bringert1307f632013-10-03 22:31:03 +0100207 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800208 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400209 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100210 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700211 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800212 mIconCache = iconCache;
213
Kenny Guyed131872014-04-30 03:02:21 +0100214 mLauncherApps = LauncherAppsCompat.getInstance(context);
215 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 }
217
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700218 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
219 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700221 if (sWorkerThread.getThreadId() == Process.myTid()) {
222 // If we are on the worker thread, post onto the main handler
223 mHandler.post(r);
224 } else {
225 r.run();
226 }
227 }
228
229 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
230 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700231 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700232 if (sWorkerThread.getThreadId() == Process.myTid()) {
233 r.run();
234 } else {
235 // If we are not on the worker thread, then post to the worker handler
236 sWorker.post(r);
237 }
238 }
239
Sunny Goyal756adbc2015-04-16 15:20:51 -0700240 public void setPackageState(final PackageInstallInfo installInfo) {
241 Runnable updateRunnable = new Runnable() {
242
243 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500244 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700245 synchronized (sBgLock) {
246 final HashSet<ItemInfo> updates = new HashSet<>();
247
248 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
249 // Ignore install success events as they are handled by Package add events.
250 return;
251 }
252
Sunny Goyale2df0622015-04-24 11:27:00 -0700253 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700254 if (info instanceof ShortcutInfo) {
255 ShortcutInfo si = (ShortcutInfo) info;
256 ComponentName cn = si.getTargetComponent();
257 if (si.isPromise() && (cn != null)
258 && installInfo.packageName.equals(cn.getPackageName())) {
259 si.setInstallProgress(installInfo.progress);
260
261 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
262 // Mark this info as broken.
263 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
264 }
265 updates.add(si);
266 }
267 }
268 }
269
270 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
271 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
272 widget.installProgress = installInfo.progress;
273 updates.add(widget);
274 }
275 }
276
277 if (!updates.isEmpty()) {
278 // Push changes to the callback.
279 Runnable r = new Runnable() {
280 public void run() {
281 Callbacks callbacks = getCallback();
282 if (callbacks != null) {
283 callbacks.bindRestoreItemsChange(updates);
284 }
285 }
286 };
287 mHandler.post(r);
288 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500289 }
290 }
291 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700292 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500293 }
294
Sunny Goyal756adbc2015-04-16 15:20:51 -0700295 /**
296 * Updates the icons and label of all pending icons for the provided package name.
297 */
298 public void updateSessionDisplayInfo(final String packageName) {
299 Runnable updateRunnable = new Runnable() {
300
301 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700302 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700303 synchronized (sBgLock) {
304 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
305 final UserHandleCompat user = UserHandleCompat.myUserHandle();
306
Sunny Goyale2df0622015-04-24 11:27:00 -0700307 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700308 if (info instanceof ShortcutInfo) {
309 ShortcutInfo si = (ShortcutInfo) info;
310 ComponentName cn = si.getTargetComponent();
311 if (si.isPromise() && (cn != null)
312 && packageName.equals(cn.getPackageName())) {
313 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
314 // For auto install apps update the icon as well as label.
315 mIconCache.getTitleAndIcon(si,
316 si.promisedIntent, user,
317 si.shouldUseLowResIcon());
318 } else {
319 // Only update the icon for restored apps.
320 si.updateIcon(mIconCache);
321 }
322 updates.add(si);
323 }
324 }
325 }
326
327 if (!updates.isEmpty()) {
328 // Push changes to the callback.
329 Runnable r = new Runnable() {
330 public void run() {
331 Callbacks callbacks = getCallback();
332 if (callbacks != null) {
333 callbacks.bindShortcutsChanged(updates,
334 new ArrayList<ShortcutInfo>(), user);
335 }
336 }
337 };
338 mHandler.post(r);
339 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700340 }
341 }
342 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700343 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700344 }
345
Adam Cohen76a47a12014-02-05 11:47:43 -0800346 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800347 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800348
349 if (allAppsApps == null) {
350 throw new RuntimeException("allAppsApps must not be null");
351 }
352 if (allAppsApps.isEmpty()) {
353 return;
354 }
355
356 // Process the newly added applications and add them to the database first
357 Runnable r = new Runnable() {
358 public void run() {
359 runOnMainThread(new Runnable() {
360 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800361 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800362 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500363 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800364 }
365 }
366 });
367 }
368 };
369 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700370 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800371
Sunny Goyala9116722015-04-29 13:55:58 -0700372 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800373 int[] xy, int spanX, int spanY) {
374 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700375 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700376
377 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800378 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700379 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700380 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800381 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800382 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700383 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800384 }
385
386 /**
387 * Find a position on the screen for the given size or adds a new screen.
388 * @return screenId and the coordinates for the item.
389 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700390 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700391 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800392 ArrayList<Long> workspaceScreens,
393 ArrayList<Long> addedWorkspaceScreensFinal,
394 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700395 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800396
Sunny Goyala9116722015-04-29 13:55:58 -0700397 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700398 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700399 synchronized (sBgLock) {
400 for (ItemInfo info : sBgItemsIdMap) {
401 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
402 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
403 if (items == null) {
404 items = new ArrayList<>();
405 screenItems.put(info.screenId, items);
406 }
407 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800408 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800410 }
411
412 // Find appropriate space for the item.
413 long screenId = 0;
414 int[] cordinates = new int[2];
415 boolean found = false;
416
417 int screenCount = workspaceScreens.size();
418 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700419 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800420 if (preferredScreenIndex < screenCount) {
421 screenId = workspaceScreens.get(preferredScreenIndex);
422 found = findNextAvailableIconSpaceInScreen(
423 screenItems.get(screenId), cordinates, spanX, spanY);
424 }
425
426 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700427 // Search on any of the screens starting from the first screen.
428 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 screenId = workspaceScreens.get(screen);
430 if (findNextAvailableIconSpaceInScreen(
431 screenItems.get(screenId), cordinates, spanX, spanY)) {
432 // We found a space for it
433 found = true;
434 break;
435 }
436 }
437 }
438
439 if (!found) {
440 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700441 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
442 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
443 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444
445 // Save the screen id for binding in the workspace
446 workspaceScreens.add(screenId);
447 addedWorkspaceScreensFinal.add(screenId);
448
449 // If we still can't find an empty space, then God help us all!!!
450 if (!findNextAvailableIconSpaceInScreen(
451 screenItems.get(screenId), cordinates, spanX, spanY)) {
452 throw new RuntimeException("Can't find space to add the item");
453 }
454 }
455 return Pair.create(screenId, cordinates);
456 }
457
458 /**
459 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800460 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700461 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700462 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800464 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700465 return;
Winson Chung997a9232013-07-24 15:33:46 -0700466 }
Winson Chung64359a52013-07-08 17:17:08 -0700467 // Process the newly added applications and add them to the database first
468 Runnable r = new Runnable() {
469 public void run() {
470 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
471 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
472
Winson Chung76828c82013-08-19 15:43:29 -0700473 // Get the list of workspace screens. We need to append to this list and
474 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
475 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800476 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700477 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800478 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700479 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700481 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700483 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800484 }
Winson Chung76828c82013-08-19 15:43:29 -0700485
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700487 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700488 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800489 long screenId = coords.first;
490 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700491
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700492 ItemInfo itemInfo;
493 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
494 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800495 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700496 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700497 } else {
498 throw new RuntimeException("Unexpected info type");
499 }
Winson Chung94d67682013-09-25 16:29:40 -0700500
Winson Chung64359a52013-07-08 17:17:08 -0700501 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700502 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700503 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700504 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700505 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700506 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700507 }
508 }
509
Winson Chung76828c82013-08-19 15:43:29 -0700510 // Update the workspace screens
511 updateWorkspaceScreenOrder(context, workspaceScreens);
512
Adam Cohen76a47a12014-02-05 11:47:43 -0800513 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700514 runOnMainThread(new Runnable() {
515 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800516 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700517 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700518 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
519 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700520 if (!addedShortcutsFinal.isEmpty()) {
521 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
522 long lastScreenId = info.screenId;
523 for (ItemInfo i : addedShortcutsFinal) {
524 if (i.screenId == lastScreenId) {
525 addAnimated.add(i);
526 } else {
527 addNotAnimated.add(i);
528 }
Winson Chung997a9232013-07-24 15:33:46 -0700529 }
530 }
Winson Chungd64d1762013-08-20 14:37:16 -0700531 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800532 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700533 }
Winson Chung64359a52013-07-08 17:17:08 -0700534 }
Winson Chung997a9232013-07-24 15:33:46 -0700535 });
536 }
Winson Chung64359a52013-07-08 17:17:08 -0700537 }
538 };
539 runOnWorkerThread(r);
540 }
541
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 /**
543 * Adds an item to the DB if it was not created previously, or move it to a new
544 * <container, screen, cellX, cellY>
545 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800546 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700547 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400548 if (item.container == ItemInfo.NO_ID) {
549 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700550 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 } else {
552 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700553 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 }
555 }
556
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700557 static void checkItemInfoLocked(
558 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
559 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
560 if (modelItem != null && item != modelItem) {
561 // check all the data is consistent
562 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
563 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
564 ShortcutInfo shortcut = (ShortcutInfo) item;
565 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
566 modelShortcut.intent.filterEquals(shortcut.intent) &&
567 modelShortcut.id == shortcut.id &&
568 modelShortcut.itemType == shortcut.itemType &&
569 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700570 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700571 modelShortcut.cellX == shortcut.cellX &&
572 modelShortcut.cellY == shortcut.cellY &&
573 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700574 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700575 // For all intents and purposes, this is the same object
576 return;
577 }
578 }
579
580 // the modelItem needs to match up perfectly with item if our model is
581 // to be consistent with the database-- for now, just require
582 // modelItem == item or the equality check above
583 String msg = "item: " + ((item != null) ? item.toString() : "null") +
584 "modelItem: " +
585 ((modelItem != null) ? modelItem.toString() : "null") +
586 "Error: ItemInfo passed to checkItemInfo doesn't match original";
587 RuntimeException e = new RuntimeException(msg);
588 if (stackTrace != null) {
589 e.setStackTrace(stackTrace);
590 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800591 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700592 }
593 }
594
Michael Jurka816474f2012-06-25 14:49:02 -0700595 static void checkItemInfo(final ItemInfo item) {
596 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
597 final long itemId = item.id;
598 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700599 public void run() {
600 synchronized (sBgLock) {
601 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700602 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700603 }
604 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700605 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700606 }
607
Michael Jurkac9d95c52011-08-29 14:03:34 -0700608 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
609 final ItemInfo item, final String callingFunction) {
610 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700611 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700612 final ContentResolver cr = context.getContentResolver();
613
Adam Cohen487f7dd2012-06-28 18:12:10 -0700614 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700615 Runnable r = new Runnable() {
616 public void run() {
617 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700618 updateItemArrays(item, itemId, stackTrace);
619 }
620 };
621 runOnWorkerThread(r);
622 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700623
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700624 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
625 final ArrayList<ItemInfo> items, final String callingFunction) {
626 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700627
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700628 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
629 Runnable r = new Runnable() {
630 public void run() {
631 ArrayList<ContentProviderOperation> ops =
632 new ArrayList<ContentProviderOperation>();
633 int count = items.size();
634 for (int i = 0; i < count; i++) {
635 ItemInfo item = items.get(i);
636 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700637 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700638 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700639
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
641 updateItemArrays(item, itemId, stackTrace);
642
643 }
644 try {
645 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
646 } catch (Exception e) {
647 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700648 }
649 }
650 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700651 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700652 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700653
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700654 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
655 // Lock on mBgLock *after* the db operation
656 synchronized (sBgLock) {
657 checkItemInfoLocked(itemId, item, stackTrace);
658
659 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
660 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
661 // Item is in a folder, make sure this folder exists
662 if (!sBgFolders.containsKey(item.container)) {
663 // An items container is being set to a that of an item which is not in
664 // the list of Folders.
665 String msg = "item: " + item + " container being set to: " +
666 item.container + ", not in the list of folders";
667 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700668 }
669 }
670
671 // Items are added/removed from the corresponding FolderInfo elsewhere, such
672 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
673 // that are on the desktop, as appropriate
674 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800675 if (modelItem != null &&
676 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
677 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700678 switch (modelItem.itemType) {
679 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
680 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
681 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
682 if (!sBgWorkspaceItems.contains(modelItem)) {
683 sBgWorkspaceItems.add(modelItem);
684 }
685 break;
686 default:
687 break;
688 }
689 } else {
690 sBgWorkspaceItems.remove(modelItem);
691 }
692 }
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700697 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700698 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700699 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 item.cellX = cellX;
702 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700703
Winson Chung3d503fb2011-07-13 17:25:49 -0700704 // We store hotseat items in canonical form which is this orientation invariant position
705 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700707 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700709 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700711 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712
713 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700715 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
716 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800717 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700721 }
722
723 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700724 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
725 * cellX, cellY have already been updated on the ItemInfos.
726 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800727 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700728 final long container, final int screen) {
729
730 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
731 int count = items.size();
732
733 for (int i = 0; i < count; i++) {
734 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700735 item.container = container;
736
737 // We store hotseat items in canonical form which is this orientation invariant position
738 // in the hotseat
739 if (context instanceof Launcher && screen < 0 &&
740 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700741 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700742 item.cellY);
743 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700744 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 }
746
747 final ContentValues values = new ContentValues();
748 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
749 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
750 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800751 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700752 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753
754 contentValues.add(values);
755 }
756 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
757 }
758
759 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700760 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800761 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700762 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700763 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700764 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800765 item.cellX = cellX;
766 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700767 item.spanX = spanX;
768 item.spanY = spanY;
769
770 // We store hotseat items in canonical form which is this orientation invariant position
771 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700772 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700773 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700775 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700777 }
Adam Cohend4844c32011-02-18 19:25:06 -0800778
Adam Cohend4844c32011-02-18 19:25:06 -0800779 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800780 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
782 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800783 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
785 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800787
Michael Jurka816474f2012-06-25 14:49:02 -0700788 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700789 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700790
791 /**
792 * Update an item to the database in a specified container.
793 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700794 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700795 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100796 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700797 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800798 }
799
Sunny Goyal756a28a2015-04-23 17:07:55 -0700800 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700801 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700802 synchronized (mLock) {
803 if (!mHasLoaderCompletedOnce ||
804 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
805 throw new RuntimeException("Trying to add shortcut while loader is running");
806 }
807 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700808 }
809 }
810
Adam Cohend4844c32011-02-18 19:25:06 -0800811 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700812 * Returns true if the shortcuts already exists on the workspace. This must be called after
813 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700815 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
816 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700817 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700818 if (intent.getComponent() != null) {
819 // If component is not null, an intent with null package will produce
820 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700821 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700822 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700823 intentWithPkg = intent.toUri(0);
824 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700825 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700826 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
827 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700828 }
829 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700830 intentWithPkg = intent.toUri(0);
831 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700832 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700833
834 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700835 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700836 if (item instanceof ShortcutInfo) {
837 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700838 Intent targetIntent = info.promisedIntent == null
839 ? info.intent : info.promisedIntent;
840 if (targetIntent != null && info.user.equals(user)) {
841 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700842 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
843 return true;
844 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700845 }
846 }
847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700849 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700850 }
851
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 * Add an item to the database in a specified container. Sets the container, screen, cellX and
854 * cellY fields of the item. Also assigns an ID to the item.
855 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700856 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700857 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 item.cellX = cellX;
860 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 // We store hotseat items in canonical form which is this orientation invariant position
862 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700866 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700867 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869
870 final ContentValues values = new ContentValues();
871 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100872 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873
Sunny Goyald2497482015-09-22 18:24:19 -0700874 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
875 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
876
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700877 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700878
Jason Monk8e19cf22014-03-20 15:06:57 -0400879 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700880 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700881 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700882 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700885 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400886 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 sBgItemsIdMap.put(item.id, item);
888 switch (item.itemType) {
889 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
890 sBgFolders.put(item.id, (FolderInfo) item);
891 // Fall through
892 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
893 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
894 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
895 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
896 sBgWorkspaceItems.add(item);
897 } else {
898 if (!sBgFolders.containsKey(item.container)) {
899 // Adding an item to a folder that doesn't exist.
900 String msg = "adding item: " + item + " to a folder that " +
901 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700902 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700904 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 break;
906 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
907 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
908 break;
909 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700910 }
911 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700912 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700913 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700914 }
915
Sunny Goyal34942622014-08-29 17:20:55 -0700916 private static ArrayList<ItemInfo> getItemsByPackageName(
917 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700918 ItemInfoFilter filter = new ItemInfoFilter() {
919 @Override
920 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
921 return cn.getPackageName().equals(pn) && info.user.equals(user);
922 }
923 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700924 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700925 }
926
927 /**
928 * Removes all the items from the database corresponding to the specified package.
929 */
930 static void deletePackageFromDatabase(Context context, final String pn,
931 final UserHandleCompat user) {
932 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700933 }
934
935 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700936 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700938 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700939 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
940 items.add(item);
941 deleteItemsFromDatabase(context, items);
942 }
943
944 /**
945 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700946 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700947 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700949 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700950 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700951 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700952 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700953 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700954
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700955 // Lock on mBgLock *after* the db operation
956 synchronized (sBgLock) {
957 switch (item.itemType) {
958 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
959 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700960 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700961 if (info.container == item.id) {
962 // We are deleting a folder which still contains items that
963 // think they are contained by that folder.
964 String msg = "deleting a folder (" + item + ") which still " +
965 "contains items (" + info + ")";
966 Log.e(TAG, msg);
967 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700968 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700969 sBgWorkspaceItems.remove(item);
970 break;
971 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
972 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
973 sBgWorkspaceItems.remove(item);
974 break;
975 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
976 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
977 break;
978 }
979 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700980 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 }
982 }
Michael Jurka83df1882011-08-31 20:59:26 -0700983 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 }
986
987 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 * Update the order of the workspace screens in the database. The array list contains
989 * a list of screen ids in the order that they should appear.
990 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700991 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700992 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 final ContentResolver cr = context.getContentResolver();
994 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
995
996 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700997 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700998 while (iter.hasNext()) {
999 long id = iter.next();
1000 if (id < 0) {
1001 iter.remove();
1002 }
1003 }
1004
1005 Runnable r = new Runnable() {
1006 @Override
1007 public void run() {
Yura085c8532014-02-11 15:15:29 +00001008 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001009 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001010 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001011 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001012 for (int i = 0; i < count; i++) {
1013 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001014 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001015 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1016 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001017 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001018 }
Yura085c8532014-02-11 15:15:29 +00001019
1020 try {
1021 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1022 } catch (Exception ex) {
1023 throw new RuntimeException(ex);
1024 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001025
Winson Chungba9c37f2013-08-30 14:11:37 -07001026 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001027 sBgWorkspaceScreens.clear();
1028 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001029 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001030 }
1031 };
1032 runOnWorkerThread(r);
1033 }
1034
1035 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001036 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 */
Winsonc0b52fe2015-09-09 16:38:15 -07001038 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 final ContentResolver cr = context.getContentResolver();
1040
Michael Jurkac9d95c52011-08-29 14:03:34 -07001041 Runnable r = new Runnable() {
1042 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001043 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001044 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001045 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001046 sBgItemsIdMap.remove(info.id);
1047 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 sBgWorkspaceItems.remove(info);
1049 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001050
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001051 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001052 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001053 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001054 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001055 for (ItemInfo childInfo : info.contents) {
1056 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001057 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001058 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001059 }
1060 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001061 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 }
1063
1064 /**
1065 * Set this as the current Launcher activity object for the loader.
1066 */
1067 public void initialize(Callbacks callbacks) {
1068 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001069 Preconditions.assertUIThread();
1070 // Remove any queued UI runnables
1071 mHandler.cancelAll();
1072 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 }
1074 }
1075
Kenny Guyed131872014-04-30 03:02:21 +01001076 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001077 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001078 int op = PackageUpdatedTask.OP_UPDATE;
1079 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1080 user));
1081 }
1082
1083 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001084 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001085 int op = PackageUpdatedTask.OP_REMOVE;
1086 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1087 user));
1088 }
1089
1090 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001091 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001092 int op = PackageUpdatedTask.OP_ADD;
1093 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1094 user));
1095 }
1096
1097 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001098 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001099 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001100 enqueuePackageUpdated(
1101 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001102 }
1103
1104 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001105 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001106 boolean replacing) {
1107 if (!replacing) {
1108 enqueuePackageUpdated(new PackageUpdatedTask(
1109 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1110 user));
1111 }
Kenny Guyed131872014-04-30 03:02:21 +01001112 }
1113
Kenny Guy44cba692016-01-21 19:50:02 +00001114 @Override
1115 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1116 enqueuePackageUpdated(new PackageUpdatedTask(
1117 PackageUpdatedTask.OP_SUSPEND, packageNames,
1118 user));
1119 }
1120
1121 @Override
1122 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1123 enqueuePackageUpdated(new PackageUpdatedTask(
1124 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1125 user));
1126 }
1127
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001128 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1130 * ACTION_PACKAGE_CHANGED.
1131 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001132 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001133 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001134 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001135
Joe Onorato36115782010-06-17 13:28:48 -04001136 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001137 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001138 // If we have changed locale we need to clear out the labels in all apps/workspace.
1139 forceReload();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001140 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001141 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001142 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001143 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001144 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1145 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001146 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1147 if (user != null) {
1148 enqueuePackageUpdated(new PackageUpdatedTask(
1149 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1150 new String[0], user));
1151 }
Tony Wickham827cef22016-03-17 15:39:39 -07001152 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1153 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001154 }
1155 }
1156
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001157 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001158 resetLoadedState(true, true);
1159
Reena Lee93f824a2011-09-23 17:20:28 -07001160 // Do this here because if the launcher activity is running it will be restarted.
1161 // If it's not running startLoaderFromBackground will merely tell it that it needs
1162 // to reload.
1163 startLoaderFromBackground();
1164 }
1165
Winson Chungf0c6ae02012-03-21 16:10:31 -07001166 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1167 synchronized (mLock) {
1168 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1169 // mWorkspaceLoaded to true later
1170 stopLoaderLocked();
1171 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1172 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1173 }
1174 }
1175
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001176 /**
1177 * When the launcher is in the background, it's possible for it to miss paired
1178 * configuration changes. So whenever we trigger the loader from the background
1179 * tell the launcher that it needs to re-run the loader when it comes back instead
1180 * of doing it now.
1181 */
1182 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001183 Callbacks callbacks = getCallback();
1184 if (callbacks != null) {
1185 // Only actually run the loader if they're not paused.
1186 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001187 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001188 }
1189 }
Joe Onorato36115782010-06-17 13:28:48 -04001190 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001191
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001192 /**
1193 * If there is already a loader task running, tell it to stop.
1194 */
1195 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001196 LoaderTask oldTask = mLoaderTask;
1197 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001198 oldTask.stopLocked();
1199 }
Reena Lee93f824a2011-09-23 17:20:28 -07001200 }
1201
Adam Cohen1a85c582014-09-30 09:48:49 -07001202 public boolean isCurrentCallbacks(Callbacks callbacks) {
1203 return (mCallbacks != null && mCallbacks.get() == callbacks);
1204 }
1205
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001206 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001207 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1208 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001209 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001210 // Don't bother to start the thread if we know it's not going to do anything
1211 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001212 final Callbacks oldCallbacks = mCallbacks.get();
1213 // Clear any pending bind-runnables from the synchronized load process.
1214 runOnMainThread(new Runnable() {
1215 public void run() {
1216 oldCallbacks.clearPendingBinds();
1217 }
1218 });
1219
Joe Onorato36115782010-06-17 13:28:48 -04001220 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001221 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001222 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001223 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001224 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001225 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1226 } else {
1227 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1228 sWorker.post(mLoaderTask);
1229 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001231 }
1232 }
1233
Joe Onorato36115782010-06-17 13:28:48 -04001234 public void stopLoader() {
1235 synchronized (mLock) {
1236 if (mLoaderTask != null) {
1237 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 }
1239 }
Joe Onorato36115782010-06-17 13:28:48 -04001240 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001242 /**
1243 * Loads the workspace screen ids in an ordered list.
1244 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001245 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001246 final ContentResolver contentResolver = context.getContentResolver();
1247 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001248
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001249 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001250 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1251 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001252 }
1253
Joe Onorato36115782010-06-17 13:28:48 -04001254 /**
1255 * Runnable for the thread that loads the contents of the launcher:
1256 * - workspace icons
1257 * - widgets
1258 * - all apps icons
1259 */
1260 private class LoaderTask implements Runnable {
1261 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001262 private int mPageToBindFirst;
1263
Adam Cohen091440a2015-03-18 14:16:05 -07001264 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001265 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001266 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001267
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001268 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001269 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001270 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 }
1272
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001273 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001274 mIsLoadingAndBindingWorkspace = true;
1275
Joe Onorato36115782010-06-17 13:28:48 -04001276 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001277 if (DEBUG_LOADERS) {
1278 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001280
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001281 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001282 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001283 synchronized (LoaderTask.this) {
1284 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001285 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001286 }
1287 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001288 }
1289 }
1290
Joe Onorato36115782010-06-17 13:28:48 -04001291 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001292 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001293 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001294
Joe Onorato36115782010-06-17 13:28:48 -04001295 private void waitForIdle() {
1296 // Wait until the either we're stopped or the other threads are done.
1297 // This way we don't start loading all apps until the workspace has settled
1298 // down.
1299 synchronized (LoaderTask.this) {
1300 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001301
Joe Onorato36115782010-06-17 13:28:48 -04001302 mHandler.postIdle(new Runnable() {
1303 public void run() {
1304 synchronized (LoaderTask.this) {
1305 mLoadAndBindStepFinished = true;
1306 if (DEBUG_LOADERS) {
1307 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001308 }
Joe Onorato36115782010-06-17 13:28:48 -04001309 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001310 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001311 }
Joe Onorato36115782010-06-17 13:28:48 -04001312 });
1313
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001314 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001315 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001316 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1317 // wait no longer than 1sec at a time
1318 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001319 } catch (InterruptedException ex) {
1320 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001321 }
1322 }
Joe Onorato36115782010-06-17 13:28:48 -04001323 if (DEBUG_LOADERS) {
1324 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001325 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001326 + "ms for previous step to finish binding");
1327 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001328 }
Joe Onorato36115782010-06-17 13:28:48 -04001329 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001330
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001331 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001332 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001333 // Ensure that we have a valid page index to load synchronously
1334 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1335 "valid page index");
1336 }
1337 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1338 // Ensure that we don't try and bind a specified page when the pages have not been
1339 // loaded already (we should load everything asynchronously in that case)
1340 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1341 }
1342 synchronized (mLock) {
1343 if (mIsLoaderTaskRunning) {
1344 // Ensure that we are never running the background loading at this point since
1345 // we also touch the background collections
1346 throw new RuntimeException("Error! Background loading is already running");
1347 }
1348 }
1349
1350 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1351 // data structures, we can't allow any other thread to touch that data, but because
1352 // this call is synchronous, we can get away with not locking).
1353
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001354 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001355 // operations from the previous activity. We need to ensure that all queued operations
1356 // are executed before any synchronous binding work is done.
1357 mHandler.flush();
1358
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001359 // Divide the set of loaded items into those that we are binding synchronously, and
1360 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001361 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1363 // arise from that.
1364 onlyBindAllApps();
1365 }
1366
Joe Onorato36115782010-06-17 13:28:48 -04001367 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001368 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001369 if (mStopped) {
1370 return;
1371 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001372 mIsLoaderTaskRunning = true;
1373 }
Joe Onorato36115782010-06-17 13:28:48 -04001374 // Optimize for end-user experience: if the Launcher is up and // running with the
1375 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1376 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001377 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001378 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001379 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001380
Joe Onorato36115782010-06-17 13:28:48 -04001381 if (mStopped) {
1382 break keep_running;
1383 }
1384
Joe Onorato36115782010-06-17 13:28:48 -04001385 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001386
1387 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001388 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1389 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391
Joe Onorato36115782010-06-17 13:28:48 -04001392 // Clear out this reference, otherwise we end up holding it until all of the
1393 // callback runnables are done.
1394 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395
Joe Onorato36115782010-06-17 13:28:48 -04001396 synchronized (mLock) {
1397 // If we are still the last one to be scheduled, remove ourselves.
1398 if (mLoaderTask == this) {
1399 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001402 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001403 }
Joe Onorato36115782010-06-17 13:28:48 -04001404 }
1405
1406 public void stopLocked() {
1407 synchronized (LoaderTask.this) {
1408 mStopped = true;
1409 this.notify();
1410 }
1411 }
1412
1413 /**
1414 * Gets the callbacks object. If we've been stopped, or if the launcher object
1415 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1416 * object that was around when the deferred message was scheduled, and if there's
1417 * a new Callbacks object around then also return null. This will save us from
1418 * calling onto it with data that will be ignored.
1419 */
1420 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1421 synchronized (mLock) {
1422 if (mStopped) {
1423 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425
1426 if (mCallbacks == null) {
1427 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001428 }
Joe Onorato36115782010-06-17 13:28:48 -04001429
1430 final Callbacks callbacks = mCallbacks.get();
1431 if (callbacks != oldCallbacks) {
1432 return null;
1433 }
1434 if (callbacks == null) {
1435 Log.w(TAG, "no mCallbacks");
1436 return null;
1437 }
1438
1439 return callbacks;
1440 }
1441 }
1442
1443 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001444 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001445 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001446 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001447 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001448
Adam Cohendcd297f2013-06-18 13:13:40 -07001449 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001450 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001451 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001452 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1453 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001454 Log.e(TAG, "Error loading shortcut into hotseat " + item
1455 + " into position (" + item.screenId + ":" + item.cellX + ","
1456 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001457 return false;
1458 }
1459
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001460 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001461 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1462
Adam Cohen2e6da152015-05-06 11:42:25 -07001463 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001464 Log.e(TAG, "Error loading shortcut " + item
1465 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001466 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001467 + ")");
1468 return false;
1469 }
1470
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001471 if (hotseatOccupancy != null) {
1472 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001473 Log.e(TAG, "Error loading shortcut into hotseat " + item
1474 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001475 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001476 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001477 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001478 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001479 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001480 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001481 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001482 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1483 occupancy.cells[(int) item.screenId][0] = true;
1484 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001485 return true;
1486 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001487 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1488 if (!workspaceScreens.contains((Long) item.screenId)) {
1489 // The item has an invalid screen id.
1490 return false;
1491 }
1492 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001493 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001494 return true;
1495 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001496
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001497 final int countX = profile.numColumns;
1498 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001499 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1500 item.cellX < 0 || item.cellY < 0 ||
1501 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1502 Log.e(TAG, "Error loading shortcut " + item
1503 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1504 + item.cellX + "," + item.cellY
1505 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1506 return false;
1507 }
1508
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001509 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001510 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1511 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001512 // Mark the first row as occupied (if the feature is enabled)
1513 // in order to account for the QSB.
1514 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001515 }
1516 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001517 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001518 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001519
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001520 // Check if any workspace icons overlap with each other
1521 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1522 occupancy.markCells(item, true);
1523 return true;
1524 } else {
1525 Log.e(TAG, "Error loading shortcut " + item
1526 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1527 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1528 + ") already occupied");
1529 return false;
1530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001532
Winson Chungba9c37f2013-08-30 14:11:37 -07001533 /** Clears all the sBg data structures */
1534 private void clearSBgDataStructures() {
1535 synchronized (sBgLock) {
1536 sBgWorkspaceItems.clear();
1537 sBgAppWidgets.clear();
1538 sBgFolders.clear();
1539 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001540 sBgWorkspaceScreens.clear();
1541 }
1542 }
1543
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001544 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001545 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546
Joe Onorato36115782010-06-17 13:28:48 -04001547 final Context context = mContext;
1548 final ContentResolver contentResolver = context.getContentResolver();
1549 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001550 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001551 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001552 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001553
Winson Chung892c74d2013-08-22 16:15:50 -07001554 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001555 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001556 int countX = profile.numColumns;
1557 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001558
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001559 boolean clearDb = false;
Sunny Goyalf076eae2016-01-11 12:25:10 -08001560 if (GridSizeMigrationTask.ENABLED &&
1561 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1562 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001563 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001564 }
1565
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001566 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001567 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001568 LauncherSettings.Settings.call(contentResolver,
1569 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001570 }
1571
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001572 Log.d(TAG, "loadWorkspace: loading default favorites");
1573 LauncherSettings.Settings.call(contentResolver,
1574 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001575
Winson Chung2abf94d2012-07-18 18:16:38 -07001576 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001577 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001578 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001579 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001580 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001581
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001582 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1583 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001585 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001586 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001587
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001588 // +1 for the hotseat (it can be larger than the workspace)
1589 // Load workspace in reverse order to ensure that latest items are loaded first (and
1590 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001591 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001592 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001593
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001594 try {
1595 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1596 final int intentIndex = c.getColumnIndexOrThrow
1597 (LauncherSettings.Favorites.INTENT);
1598 final int titleIndex = c.getColumnIndexOrThrow
1599 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001600 final int containerIndex = c.getColumnIndexOrThrow(
1601 LauncherSettings.Favorites.CONTAINER);
1602 final int itemTypeIndex = c.getColumnIndexOrThrow(
1603 LauncherSettings.Favorites.ITEM_TYPE);
1604 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1605 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001606 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1607 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001608 final int screenIndex = c.getColumnIndexOrThrow(
1609 LauncherSettings.Favorites.SCREEN);
1610 final int cellXIndex = c.getColumnIndexOrThrow
1611 (LauncherSettings.Favorites.CELLX);
1612 final int cellYIndex = c.getColumnIndexOrThrow
1613 (LauncherSettings.Favorites.CELLY);
1614 final int spanXIndex = c.getColumnIndexOrThrow
1615 (LauncherSettings.Favorites.SPANX);
1616 final int spanYIndex = c.getColumnIndexOrThrow(
1617 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001618 final int rankIndex = c.getColumnIndexOrThrow(
1619 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001620 final int restoredIndex = c.getColumnIndexOrThrow(
1621 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001622 final int profileIdIndex = c.getColumnIndexOrThrow(
1623 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001624 final int optionsIndex = c.getColumnIndexOrThrow(
1625 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001626 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627
Sunny Goyal7f834d22015-04-21 10:10:23 -07001628 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001629 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001630 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001631 long serialNo = mUserManager.getSerialNumberForUser(user);
1632 allUsers.put(serialNo, user);
1633 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001634 }
1635
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001636 ShortcutInfo info;
1637 String intentDescription;
1638 LauncherAppWidgetInfo appWidgetInfo;
1639 int container;
1640 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001641 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001642 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001643 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001644 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001646 while (!mStopped && c.moveToNext()) {
1647 try {
1648 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001649 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001650 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001651 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001653 switch (itemType) {
1654 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1655 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001656 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001657 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001658 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001659 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001660 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001661 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001662 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001663 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001664 if (user == null) {
1665 // User has been deleted remove the item.
1666 itemsToRemove.add(id);
1667 continue;
1668 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 try {
1670 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001671 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001672 if (cn != null && cn.getPackageName() != null) {
1673 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1674 cn.getPackageName(), user);
1675 boolean validComponent = validPkg &&
1676 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001677 if (validPkg) {
1678 targetPackage = cn.getPackageName();
1679 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001680
1681 if (validComponent) {
1682 if (restored) {
1683 // no special handling necessary for this item
1684 restoredRows.add(id);
1685 restored = false;
1686 }
Kenny Guyff05f432016-01-22 17:48:29 +00001687 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001688 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001689 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001690 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001691 intent = null;
1692 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1693 // We allow auto install apps to have their intent
1694 // updated after an install.
1695 intent = manager.getLaunchIntentForPackage(
1696 cn.getPackageName());
1697 if (intent != null) {
1698 ContentValues values = new ContentValues();
1699 values.put(LauncherSettings.Favorites.INTENT,
1700 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001701 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001702 }
1703 }
1704
1705 if (intent == null) {
1706 // The app is installed but the component is no
1707 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001708 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001709 itemsToRemove.add(id);
1710 continue;
1711 } else {
1712 // no special handling necessary for this item
1713 restoredRows.add(id);
1714 restored = false;
1715 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001716 } else if (restored) {
1717 // Package is not yet available but might be
1718 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001719 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001720
1721 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1722 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001723 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001724 // App restore has started. Update the flag
1725 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1726 ContentValues values = new ContentValues();
1727 values.put(LauncherSettings.Favorites.RESTORED,
1728 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001729 updateItem(id, values);
1730 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1731 // This is a common app. Try to replace this.
1732 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1733 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1734 if (parser.findDefaultApp()) {
1735 // Default app found. Replace it.
1736 intent = parser.parsedIntent;
1737 cn = intent.getComponent();
1738 ContentValues values = parser.parsedValues;
1739 values.put(LauncherSettings.Favorites.RESTORED, 0);
1740 updateItem(id, values);
1741 restored = false;
1742 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001743
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001744 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001745 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001746 itemsToRemove.add(id);
1747 continue;
1748 }
Sunny Goyal94485362014-09-18 16:13:58 -07001749 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001750 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001751 itemsToRemove.add(id);
1752 continue;
1753 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001754 } else if (PackageManagerHelper.isAppOnSdcard(
1755 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001756 // Package is present but not available.
1757 allowMissingTarget = true;
1758 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1759 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001760 // SdCard is not ready yet. Package might get available,
1761 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001762 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001763 HashSet<String> pkgs = sPendingPackages.get(user);
1764 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001765 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001766 sPendingPackages.put(user, pkgs);
1767 }
1768 pkgs.add(cn.getPackageName());
1769 allowMissingTarget = true;
1770 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001771
1772 } else {
1773 // Do not wait for external media load anymore.
1774 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001775 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001776 itemsToRemove.add(id);
1777 continue;
Winson Chungee055712013-07-30 14:46:24 -07001778 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001779 } else if (cn == null) {
1780 // For shortcuts with no component, keep them as they are
1781 restoredRows.add(id);
1782 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001783 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001785 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001786 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 continue;
1788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789
Sunny Goyal34b65272015-03-11 16:56:52 -07001790 boolean useLowResIcon = container >= 0 &&
1791 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1792
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001793 if (itemReplaced) {
1794 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001795 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001796 cursorIconInfo.iconIndex, titleIndex,
1797 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001798 } else {
1799 // Don't replace items for other profiles.
1800 itemsToRemove.add(id);
1801 continue;
1802 }
1803 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001804 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001805 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001806 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001807 intent = getRestoredItemIntent(c, context, intent);
1808 } else {
1809 // Don't restore items for other profiles.
1810 itemsToRemove.add(id);
1811 continue;
1812 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001813 } else if (itemType ==
1814 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001815 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001816 cursorIconInfo.iconIndex, titleIndex,
1817 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001819 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001820
Sunny Goyald09c3702016-04-06 16:18:20 -07001821 // Shortcuts are only available on the primary profile
1822 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1823 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1824 }
1825
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 // App shortcuts that used to be automatically added to Launcher
1827 // didn't always have the correct intent flags set, so do that
1828 // here
1829 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001830 intent.getCategories() != null &&
1831 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001832 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 intent.addFlags(
1834 Intent.FLAG_ACTIVITY_NEW_TASK |
1835 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1836 }
Michael Jurka96879562012-03-22 05:54:33 -07001837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001840 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001843 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 info.cellX = c.getInt(cellXIndex);
1845 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001846 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001847 info.spanX = 1;
1848 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001849 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001850 if (info.promisedIntent != null) {
1851 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1852 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001853 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001854 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1855 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1856 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001857
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001859 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001860 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 break;
1862 }
1863
Sunny Goyal756adbc2015-04-16 15:20:51 -07001864 if (restored) {
1865 ComponentName cn = info.getTargetComponent();
1866 if (cn != null) {
1867 Integer progress = installingPkgs.get(cn.getPackageName());
1868 if (progress != null) {
1869 info.setInstallProgress(progress);
1870 } else {
1871 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1872 }
1873 }
1874 }
1875
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 switch (container) {
1877 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1878 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1879 sBgWorkspaceItems.add(info);
1880 break;
1881 default:
1882 // Item is in a user folder
1883 FolderInfo folderInfo =
1884 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07001885 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 break;
1887 }
1888 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001889 } else {
1890 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 }
1892 break;
1893
1894 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1895 id = c.getLong(idIndex);
1896 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1897
Sunny Goyala508e4f2015-05-21 09:33:57 -07001898 // Do not trim the folder label, as is was set by the user.
1899 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001900 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001902 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001903 folderInfo.cellX = c.getInt(cellXIndex);
1904 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001905 folderInfo.spanX = 1;
1906 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001907 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908
Daniel Sandler8802e962010-05-26 16:28:16 -04001909 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001910 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001911 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001912 break;
1913 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001914
Joe Onorato9c1289c2009-08-17 11:03:03 -04001915 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1917 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1918 sBgWorkspaceItems.add(folderInfo);
1919 break;
Joe Onorato36115782010-06-17 13:28:48 -04001920 }
Joe Onorato17a89222011-02-08 17:26:11 -08001921
Chris Wrenf4d08112014-01-16 18:13:56 -05001922 if (restored) {
1923 // no special handling required for restored folders
1924 restoredRows.add(id);
1925 }
1926
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001927 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1928 sBgFolders.put(folderInfo.id, folderInfo);
1929 break;
1930
1931 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001932 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001933 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001934 boolean customWidget = itemType ==
1935 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1936
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001938 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001939 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001940 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001941 user = allUsers.get(serialNumber);
1942 if (user == null) {
1943 itemsToRemove.add(id);
1944 continue;
1945 }
1946
Sunny Goyalff572272014-07-23 13:58:07 -07001947 final ComponentName component =
1948 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001949
Sunny Goyal651077b2014-06-30 14:15:31 -07001950 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001951 final boolean isIdValid = (restoreStatus &
1952 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001953 final boolean wasProviderReady = (restoreStatus &
1954 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001955
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001956 if (widgetProvidersMap == null) {
1957 widgetProvidersMap = AppWidgetManagerCompat
1958 .getInstance(mContext).getAllProvidersMap();
1959 }
1960 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1961 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001962 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001963 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001964
1965 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001966 if (!isSafeMode && !customWidget &&
1967 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001968 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001969 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 itemsToRemove.add(id);
1971 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001972 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001973 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1974 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001975
Sunny Goyal53f96722015-07-13 19:54:53 -07001976 // The provider is available. So the widget is either
1977 // available or not available. We do not need to track
1978 // any future restore updates.
1979 int status = restoreStatus &
1980 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001981 if (!wasProviderReady) {
1982 // If provider was not previously ready, update the
1983 // status and UI flag.
1984
1985 // Id would be valid only if the widget restore broadcast was received.
1986 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001987 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001988 } else {
1989 status &= ~LauncherAppWidgetInfo
1990 .FLAG_PROVIDER_NOT_READY;
1991 }
1992 }
1993 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001994 } else {
1995 Log.v(TAG, "Widget restore pending id=" + id
1996 + " appWidgetId=" + appWidgetId
1997 + " status =" + restoreStatus);
1998 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001999 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002000 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002001 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002002
2003 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2004 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002005 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002006 // App restore has started. Update the flag
2007 appWidgetInfo.restoreStatus |=
2008 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002009 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002010 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002011 itemsToRemove.add(id);
2012 continue;
2013 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002014
2015 appWidgetInfo.installProgress =
2016 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002017 }
Sunny Goyalff572272014-07-23 13:58:07 -07002018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002020 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 appWidgetInfo.cellX = c.getInt(cellXIndex);
2022 appWidgetInfo.cellY = c.getInt(cellYIndex);
2023 appWidgetInfo.spanX = c.getInt(spanXIndex);
2024 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002025 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002026
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2028 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2029 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002030 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2031 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 continue;
2033 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002035 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002037 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002038 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 break;
2040 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002041
Adam Cohen59400422014-03-05 18:07:04 -08002042 if (!customWidget) {
2043 String providerName =
2044 appWidgetInfo.providerName.flattenToString();
2045 if (!providerName.equals(savedProvider) ||
2046 (appWidgetInfo.restoreStatus != restoreStatus)) {
2047 ContentValues values = new ContentValues();
2048 values.put(
2049 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2050 providerName);
2051 values.put(LauncherSettings.Favorites.RESTORED,
2052 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002053 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002054 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002055 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002056 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2057 sBgAppWidgets.add(appWidgetInfo);
2058 }
Joe Onorato36115782010-06-17 13:28:48 -04002059 break;
2060 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002062 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002063 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 }
2065 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002066 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 }
2068
Winson Chungba9c37f2013-08-30 14:11:37 -07002069 // Break early if we've stopped loading
2070 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002071 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002072 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002073 }
2074
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002075 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002077 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2078 Utilities.createDbSelectionQuery(
2079 LauncherSettings.Favorites._ID, itemsToRemove), null);
2080 if (DEBUG_LOADERS) {
2081 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2082 LauncherSettings.Favorites._ID, itemsToRemove));
2083 }
2084
2085 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002086 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2087 .call(contentResolver,
2088 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2089 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2090 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002091 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2092 sBgFolders.remove(folderId);
2093 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002094 }
2095 }
2096
Sunny Goyal317698b2015-07-29 11:45:41 -07002097 // Sort all the folder items and make sure the first 3 items are high resolution.
2098 for (FolderInfo folder : sBgFolders) {
2099 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2100 int pos = 0;
2101 for (ShortcutInfo info : folder.contents) {
2102 if (info.usingLowResIcon) {
2103 info.updateIcon(mIconCache, false);
2104 }
2105 pos ++;
2106 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2107 break;
2108 }
2109 }
2110 }
2111
Chris Wrenf4d08112014-01-16 18:13:56 -05002112 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002113 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002114 ContentValues values = new ContentValues();
2115 values.put(LauncherSettings.Favorites.RESTORED, 0);
2116 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2117 Utilities.createDbSelectionQuery(
2118 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002119 }
2120
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002121 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2122 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002123 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002124 null, sWorker);
2125 }
2126
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002127 // Remove any empty screens
2128 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002129 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002130 long screenId = item.screenId;
2131 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2132 unusedScreens.contains(screenId)) {
2133 unusedScreens.remove(screenId);
2134 }
2135 }
2136
2137 // If there are any empty screens remove them, and update.
2138 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002139 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002140 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002141 }
2142
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 if (DEBUG_LOADERS) {
2144 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2145 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002146 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002147 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002148 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002149
Sunny Goyale2df0622015-04-24 11:27:00 -07002150 for (int i = 0; i < nScreens; i++) {
2151 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002152 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 line += " | ";
2154 }
Joe Onorato36115782010-06-17 13:28:48 -04002155 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 }
Joe Onorato36115782010-06-17 13:28:48 -04002159 }
Adam Cohene25af792013-06-06 23:08:25 -07002160 }
2161
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002162 /**
2163 * Partially updates the item without any notification. Must be called on the worker thread.
2164 */
2165 private void updateItem(long itemId, ContentValues update) {
2166 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002167 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002168 update,
2169 BaseColumns._ID + "= ?",
2170 new String[]{Long.toString(itemId)});
2171 }
2172
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173 /** Filters the set of items who are directly or indirectly (via another container) on the
2174 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002175 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 ArrayList<ItemInfo> allWorkspaceItems,
2177 ArrayList<ItemInfo> currentScreenItems,
2178 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002179 // Purge any null ItemInfos
2180 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2181 while (iter.hasNext()) {
2182 ItemInfo i = iter.next();
2183 if (i == null) {
2184 iter.remove();
2185 }
2186 }
2187
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 // Order the set of items by their containers first, this allows use to walk through the
2189 // list sequentially, build up a list of containers that are in the specified screen,
2190 // as well as all items in those containers.
2191 Set<Long> itemsOnScreen = new HashSet<Long>();
2192 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2193 @Override
2194 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002195 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 }
2197 });
2198 for (ItemInfo info : allWorkspaceItems) {
2199 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002200 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 currentScreenItems.add(info);
2202 itemsOnScreen.add(info.id);
2203 } else {
2204 otherScreenItems.add(info);
2205 }
2206 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2207 currentScreenItems.add(info);
2208 itemsOnScreen.add(info.id);
2209 } else {
2210 if (itemsOnScreen.contains(info.container)) {
2211 currentScreenItems.add(info);
2212 itemsOnScreen.add(info.id);
2213 } else {
2214 otherScreenItems.add(info);
2215 }
2216 }
2217 }
2218 }
2219
2220 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002221 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222 ArrayList<LauncherAppWidgetInfo> appWidgets,
2223 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2224 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225
2226 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002227 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002229 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 currentScreenWidgets.add(widget);
2231 } else {
2232 otherScreenWidgets.add(widget);
2233 }
2234 }
2235 }
2236
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2238 * right) */
2239 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002240 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002241 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002242 final int screenCols = profile.numColumns;
2243 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002245 @Override
2246 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002247 if (lhs.container == rhs.container) {
2248 // Within containers, order by their spatial position in that container
2249 switch ((int) lhs.container) {
2250 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2251 long lr = (lhs.screenId * screenCellCount +
2252 lhs.cellY * screenCols + lhs.cellX);
2253 long rr = (rhs.screenId * screenCellCount +
2254 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002255 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002256 }
2257 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2258 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002259 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002260 }
2261 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002262 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002263 throw new RuntimeException("Unexpected container type when " +
2264 "sorting workspace items.");
2265 }
2266 return 0;
2267 }
2268 } else {
2269 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002270 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002271 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002272 }
2273 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002275
Adam Cohendcd297f2013-06-18 13:13:40 -07002276 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2277 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002278 final Runnable r = new Runnable() {
2279 @Override
2280 public void run() {
2281 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2282 if (callbacks != null) {
2283 callbacks.bindScreens(orderedScreens);
2284 }
2285 }
2286 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002287 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 }
2289
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002290 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2291 final ArrayList<ItemInfo> workspaceItems,
2292 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002293 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294
2295 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002296 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002298 final int start = i;
2299 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 final Runnable r = new Runnable() {
2301 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002302 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002303 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002304 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002305 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2306 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002307 }
2308 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002310 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002311 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 // Bind the widgets, one at a time
2314 N = appWidgets.size();
2315 for (int i = 0; i < N; i++) {
2316 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2317 final Runnable r = new Runnable() {
2318 public void run() {
2319 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2320 if (callbacks != null) {
2321 callbacks.bindAppWidget(widget);
2322 }
2323 }
2324 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002325 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 }
2327 }
2328
2329 /**
2330 * Binds all loaded data to actual views on the main thread.
2331 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002332 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 final long t = SystemClock.uptimeMillis();
2334 Runnable r;
2335
2336 // Don't use these two variables in any of the callback runnables.
2337 // Otherwise we hold a reference to them.
2338 final Callbacks oldCallbacks = mCallbacks.get();
2339 if (oldCallbacks == null) {
2340 // This launcher has exited and nobody bothered to tell us. Just bail.
2341 Log.w(TAG, "LoaderTask running with no launcher");
2342 return;
2343 }
2344
Winson Chung9b9fb962013-11-15 15:39:34 -08002345 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002346 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2347 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2348 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002349
Winson Chung2abf94d2012-07-18 18:16:38 -07002350 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 workspaceItems.addAll(sBgWorkspaceItems);
2352 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002353 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002354 }
2355
Derek Prothro7aff3992013-12-10 14:00:37 -05002356 final boolean isLoadingSynchronously =
2357 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002358 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002359 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002360 if (currScreen >= orderedScreenIds.size()) {
2361 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002362 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002363 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002364 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002365 final long currentScreenId = currentScreen < 0
2366 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002367
Winson Chung9b9fb962013-11-15 15:39:34 -08002368 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002369 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2370 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2371 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2372 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002373
Winson Chung9b9fb962013-11-15 15:39:34 -08002374 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002376 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2379 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2380
2381 // Tell the workspace that we're about to start binding items
2382 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002383 public void run() {
2384 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2385 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002386 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002388 }
2389 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002391 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2394
Sunny Goyal527c7d32015-08-28 15:19:36 -07002395 Executor mainExecutor = new DeferredMainThreadExecutor();
2396 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002397 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002398
Sunny Goyal527c7d32015-08-28 15:19:36 -07002399 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2400 // remaining screens after first onDraw is called. This ensures that the first screen
2401 // is immediately visible (eg. during rotation)
2402 // In case of !isLoadingSynchronously, bind all pages one after other.
2403 final Executor deferredExecutor = isLoadingSynchronously ?
2404 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2405
Sunny Goyal44c06432016-04-02 10:56:02 -07002406 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407
2408 // Tell the workspace that we're done binding items
2409 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002410 public void run() {
2411 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2412 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002413 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002414 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415
Sunny Goyal639e9062015-08-19 19:17:06 -07002416 mIsLoadingAndBindingWorkspace = false;
2417
2418 // Run all the bind complete runnables after workspace is bound.
2419 if (!mBindCompleteRunnables.isEmpty()) {
2420 synchronized (mBindCompleteRunnables) {
2421 for (final Runnable r : mBindCompleteRunnables) {
2422 runOnWorkerThread(r);
2423 }
2424 mBindCompleteRunnables.clear();
2425 }
2426 }
2427
Winson Chung98e030b2012-05-07 16:01:11 -07002428 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002429 if (DEBUG_LOADERS) {
2430 Log.d(TAG, "bound workspace in "
2431 + (SystemClock.uptimeMillis()-t) + "ms");
2432 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002433
Joe Onorato36115782010-06-17 13:28:48 -04002434 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002435 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002436 deferredExecutor.execute(r);
2437
Winson Chung4a2afa32012-07-19 14:53:05 -07002438 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002439 r = new Runnable() {
2440 public void run() {
2441 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2442 if (callbacks != null) {
2443 // We are loading synchronously, which means, some of the pages will be
2444 // bound after first draw. Inform the callbacks that page binding is
2445 // not complete, and schedule the remaining pages.
2446 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2447 callbacks.onPageBoundSynchronously(currentScreen);
2448 }
2449 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2450 }
2451 }
2452 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002453 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002454 }
Joe Onorato36115782010-06-17 13:28:48 -04002455 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002456
Joe Onorato36115782010-06-17 13:28:48 -04002457 private void loadAndBindAllApps() {
2458 if (DEBUG_LOADERS) {
2459 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2460 }
2461 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002462 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002463 synchronized (LoaderTask.this) {
2464 if (mStopped) {
2465 return;
2466 }
2467 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002468 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002469 synchronized (LoaderTask.this) {
2470 if (mStopped) {
2471 return;
2472 }
2473 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002474 }
Joe Onorato36115782010-06-17 13:28:48 -04002475 } else {
2476 onlyBindAllApps();
2477 }
2478 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002479
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002480 private void updateIconCache() {
2481 // Ignore packages which have a promise icon.
2482 HashSet<String> packagesToIgnore = new HashSet<>();
2483 synchronized (sBgLock) {
2484 for (ItemInfo info : sBgItemsIdMap) {
2485 if (info instanceof ShortcutInfo) {
2486 ShortcutInfo si = (ShortcutInfo) info;
2487 if (si.isPromise() && si.getTargetComponent() != null) {
2488 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2489 }
2490 } else if (info instanceof LauncherAppWidgetInfo) {
2491 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2492 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2493 packagesToIgnore.add(lawi.providerName.getPackageName());
2494 }
2495 }
2496 }
2497 }
2498 mIconCache.updateDbIcons(packagesToIgnore);
2499 }
2500
Joe Onorato36115782010-06-17 13:28:48 -04002501 private void onlyBindAllApps() {
2502 final Callbacks oldCallbacks = mCallbacks.get();
2503 if (oldCallbacks == null) {
2504 // This launcher has exited and nobody bothered to tell us. Just bail.
2505 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2506 return;
2507 }
2508
2509 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002510 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002511 final ArrayList<AppInfo> list
2512 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002513 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002514 public void run() {
2515 final long t = SystemClock.uptimeMillis();
2516 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2517 if (callbacks != null) {
2518 callbacks.bindAllApplications(list);
2519 }
2520 if (DEBUG_LOADERS) {
2521 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002522 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002523 }
2524 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002525 };
2526 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002527 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002528 r.run();
2529 } else {
2530 mHandler.post(r);
2531 }
Joe Onorato36115782010-06-17 13:28:48 -04002532 }
2533
Winson Chung64359a52013-07-08 17:17:08 -07002534 private void loadAllApps() {
2535 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002536
Joe Onorato36115782010-06-17 13:28:48 -04002537 final Callbacks oldCallbacks = mCallbacks.get();
2538 if (oldCallbacks == null) {
2539 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002540 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002541 return;
2542 }
2543
Kenny Guyed131872014-04-30 03:02:21 +01002544 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2545
Winson Chung64359a52013-07-08 17:17:08 -07002546 // Clear the list of apps
2547 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002548 for (UserHandleCompat user : profiles) {
2549 // Query for the set of apps
2550 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002551 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002552 if (DEBUG_LOADERS) {
2553 Log.d(TAG, "getActivityList took "
2554 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2555 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2556 }
2557 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002558 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002559 if (apps == null || apps.isEmpty()) {
2560 return;
2561 }
Kenny Guyff05f432016-01-22 17:48:29 +00002562 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002563 // Create the ApplicationInfos
2564 for (int i = 0; i < apps.size(); i++) {
2565 LauncherActivityInfoCompat app = apps.get(i);
2566 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002567 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002568 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002569
Sunny Goyal756a28a2015-04-23 17:07:55 -07002570 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2571 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002572 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002573
2574 @Override
2575 public void run() {
2576 heuristic.processUserApps(apps);
2577 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002578 };
2579 runOnMainThread(new Runnable() {
2580
2581 @Override
2582 public void run() {
2583 // Check isLoadingWorkspace on the UI thread, as it is updated on
2584 // the UI thread.
2585 if (mIsLoadingAndBindingWorkspace) {
2586 synchronized (mBindCompleteRunnables) {
2587 mBindCompleteRunnables.add(r);
2588 }
2589 } else {
2590 runOnWorkerThread(r);
2591 }
2592 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002593 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002594 }
Winson Chung64359a52013-07-08 17:17:08 -07002595 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002596 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002597 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2598 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002599
2600 // Post callback on main thread
2601 mHandler.post(new Runnable() {
2602 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002603
Winson Chung64359a52013-07-08 17:17:08 -07002604 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002605 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002606 if (callbacks != null) {
2607 callbacks.bindAllApplications(added);
2608 if (DEBUG_LOADERS) {
2609 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002610 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002611 }
2612 } else {
2613 Log.i(TAG, "not binding apps: no Launcher activity");
2614 }
2615 }
2616 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002617 // Cleanup any data stored for a deleted user.
2618 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002619 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002620 Log.d(TAG, "Icons processed in "
2621 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002622 }
2623 }
2624
2625 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002626 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002628 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2629 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2630 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2631 }
Joe Onorato36115782010-06-17 13:28:48 -04002632 }
2633 }
2634
Sunny Goyal75b0f552015-05-20 21:57:06 -07002635 /**
2636 * Called when the icons for packages have been updated in the icon cache.
2637 */
2638 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2639 final Callbacks callbacks = getCallback();
2640 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2641 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2642
2643 // If any package icon has changed (app was updated while launcher was dead),
2644 // update the corresponding shortcuts.
2645 synchronized (sBgLock) {
2646 for (ItemInfo info : sBgItemsIdMap) {
2647 if (info instanceof ShortcutInfo && user.equals(info.user)
2648 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2649 ShortcutInfo si = (ShortcutInfo) info;
2650 ComponentName cn = si.getTargetComponent();
2651 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2652 si.updateIcon(mIconCache);
2653 updatedShortcuts.add(si);
2654 }
2655 }
2656 }
2657 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2658 }
2659
2660 if (!updatedShortcuts.isEmpty()) {
2661 final UserHandleCompat userFinal = user;
2662 mHandler.post(new Runnable() {
2663
2664 public void run() {
2665 Callbacks cb = getCallback();
2666 if (cb != null && callbacks == cb) {
2667 cb.bindShortcutsChanged(updatedShortcuts,
2668 new ArrayList<ShortcutInfo>(), userFinal);
2669 }
2670 }
2671 });
2672 }
2673
2674 if (!updatedApps.isEmpty()) {
2675 mHandler.post(new Runnable() {
2676
2677 public void run() {
2678 Callbacks cb = getCallback();
2679 if (cb != null && callbacks == cb) {
2680 cb.bindAppsUpdated(updatedApps);
2681 }
2682 }
2683 });
2684 }
2685 }
2686
Joe Onorato36115782010-06-17 13:28:48 -04002687 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002688 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002689 }
2690
Adam Cohen091440a2015-03-18 14:16:05 -07002691 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002692
2693 @Override
2694 public void onReceive(Context context, Intent intent) {
2695 synchronized (sBgLock) {
2696 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2697 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002698 final PackageManager manager = context.getPackageManager();
2699 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2700 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002701 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2702 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002703 packagesRemoved.clear();
2704 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002705 for (String pkg : entry.getValue()) {
2706 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002707 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002708 packagesUnavailable.add(pkg);
2709 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002710 packagesRemoved.add(pkg);
2711 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002712 }
2713 }
2714 if (!packagesRemoved.isEmpty()) {
2715 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2716 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2717 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002718 if (!packagesUnavailable.isEmpty()) {
2719 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2720 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2721 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002722 }
Sunny Goyal34942622014-08-29 17:20:55 -07002723 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002724 }
2725 }
2726 }
2727
Joe Onorato36115782010-06-17 13:28:48 -04002728 private class PackageUpdatedTask implements Runnable {
2729 int mOp;
2730 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002731 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002732
2733 public static final int OP_NONE = 0;
2734 public static final int OP_ADD = 1;
2735 public static final int OP_UPDATE = 2;
2736 public static final int OP_REMOVE = 3; // uninstlled
2737 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002738 public static final int OP_SUSPEND = 5; // package suspended
2739 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002740 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002741
Kenny Guyed131872014-04-30 03:02:21 +01002742 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002743 mOp = op;
2744 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002745 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002746 }
2747
2748 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002749 if (!mHasLoaderCompletedOnce) {
2750 // Loader has not yet run.
2751 return;
2752 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002753 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002754
2755 final String[] packages = mPackages;
2756 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002757 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002758 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002759 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002760 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002761 for (int i=0; i<N; i++) {
2762 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002763 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002764 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002765 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002766
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002767 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2768 if (heuristic != null) {
2769 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002770 }
Joe Onorato36115782010-06-17 13:28:48 -04002771 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002772 }
Joe Onorato36115782010-06-17 13:28:48 -04002773 case OP_UPDATE:
2774 for (int i=0; i<N; i++) {
2775 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002776 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002777 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002778 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002779 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002780 // Since package was just updated, the target must be available now.
2781 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002782 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002783 case OP_REMOVE: {
2784 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2785 if (heuristic != null) {
2786 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002787 }
Joe Onorato36115782010-06-17 13:28:48 -04002788 for (int i=0; i<N; i++) {
2789 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002790 mIconCache.removeIconsForPkg(packages[i], mUser);
2791 }
2792 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002793 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002794 case OP_UNAVAILABLE:
2795 for (int i=0; i<N; i++) {
2796 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2797 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002798 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002799 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002800 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002801 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002802 case OP_SUSPEND:
2803 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002804 flagOp = mOp == OP_SUSPEND ?
2805 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2806 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002807 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002808 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2809 break;
2810 case OP_USER_AVAILABILITY_CHANGE:
2811 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2812 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2813 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2814 // We want to update all packages for this user.
2815 pkgFilter = StringFilter.matchesAll();
2816 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002817 break;
Joe Onorato36115782010-06-17 13:28:48 -04002818 }
2819
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002820 ArrayList<AppInfo> added = null;
2821 ArrayList<AppInfo> modified = null;
2822 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002823
Adam Cohen487f7dd2012-06-28 18:12:10 -07002824 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002825 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002826 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002827 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002828 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002829 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002830 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002831 }
Winson Chung5d55f332012-07-16 20:45:03 -07002832 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002833 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002834 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002835 }
2836
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002837 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002838
Joe Onorato36115782010-06-17 13:28:48 -04002839 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002840 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002841 for (AppInfo ai : added) {
2842 addedOrUpdatedApps.put(ai.componentName, ai);
2843 }
Joe Onorato36115782010-06-17 13:28:48 -04002844 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002845
Joe Onorato36115782010-06-17 13:28:48 -04002846 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002847 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002848 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002849 for (AppInfo ai : modified) {
2850 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002851 }
2852
Joe Onorato36115782010-06-17 13:28:48 -04002853 mHandler.post(new Runnable() {
2854 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002855 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002856 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002857 callbacks.bindAppsUpdated(modifiedFinal);
2858 }
2859 }
2860 });
2861 }
Winson Chung83892cc2013-05-01 16:53:33 -07002862
Sunny Goyal4390ace2014-10-13 11:33:11 -07002863 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002864 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002865 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
2866 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
2867 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
2868
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002869 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002870 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002871 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2872 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002873 boolean infoUpdated = false;
2874 boolean shortcutUpdated = false;
2875
2876 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002877 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07002878 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002879 Bitmap icon = Utilities.createIconBitmap(
2880 si.iconResource.packageName,
2881 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002882 if (icon != null) {
2883 si.setIcon(icon);
2884 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002885 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002886 }
2887 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002888
2889 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07002890 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002891 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2892
2893 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002894 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2895 // Auto install icon
2896 PackageManager pm = context.getPackageManager();
2897 ResolveInfo matched = pm.resolveActivity(
2898 new Intent(Intent.ACTION_MAIN)
2899 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2900 PackageManager.MATCH_DEFAULT_ONLY);
2901 if (matched == null) {
2902 // Try to find the best match activity.
2903 Intent intent = pm.getLaunchIntentForPackage(
2904 cn.getPackageName());
2905 if (intent != null) {
2906 cn = intent.getComponent();
2907 appInfo = addedOrUpdatedApps.get(cn);
2908 }
2909
2910 if ((intent == null) || (appInfo == null)) {
2911 removedShortcuts.add(si);
2912 continue;
2913 }
2914 si.promisedIntent = intent;
2915 }
2916 }
2917
2918 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07002919 if (appInfo != null) {
2920 si.flags = appInfo.flags;
2921 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002922
Sunny Goyal756adbc2015-04-16 15:20:51 -07002923 si.intent = si.promisedIntent;
2924 si.promisedIntent = null;
2925 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002926 infoUpdated = true;
2927 si.updateIcon(mIconCache);
2928 }
2929
2930 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2931 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2932 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002933 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002934 si.contentDescription = appInfo.contentDescription;
2935 infoUpdated = true;
2936 }
2937
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002938 int oldDisabledFlags = si.isDisabled;
2939 si.isDisabled = flagOp.apply(si.isDisabled);
2940 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002941 shortcutUpdated = true;
2942 }
2943 }
2944
2945 if (infoUpdated || shortcutUpdated) {
2946 updatedShortcuts.add(si);
2947 }
2948 if (infoUpdated) {
2949 updateItemInDatabase(context, si);
2950 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002951 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002952 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2953 if (mUser.equals(widgetInfo.user)
2954 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07002955 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002956 widgetInfo.restoreStatus &=
2957 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2958 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002959
2960 // adding this flag ensures that launcher shows 'click to setup'
2961 // if the widget has a config activity. In case there is no config
2962 // activity, it will be marked as 'restored' during bind.
2963 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2964
Sunny Goyal4390ace2014-10-13 11:33:11 -07002965 widgets.add(widgetInfo);
2966 updateItemInDatabase(context, widgetInfo);
2967 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002968 }
2969 }
2970 }
2971
Sunny Goyal4390ace2014-10-13 11:33:11 -07002972 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002973 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002974 mHandler.post(new Runnable() {
2975
2976 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002977 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002978 if (callbacks == cb && cb != null) {
2979 callbacks.bindShortcutsChanged(
2980 updatedShortcuts, removedShortcuts, mUser);
2981 }
2982 }
2983 });
2984 if (!removedShortcuts.isEmpty()) {
2985 deleteItemsFromDatabase(context, removedShortcuts);
2986 }
2987 }
2988 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002989 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002990 mHandler.post(new Runnable() {
2991 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002992 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002993 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002994 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002995 }
2996 }
2997 });
2998 }
2999 }
3000
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003001 final HashSet<String> removedPackages = new HashSet<>();
3002 final HashSet<ComponentName> removedComponents = new HashSet<>();
3003 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003004 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003005 Collections.addAll(removedPackages, packages);
3006
3007 // No need to update the removedComponents as
3008 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003009 } else if (mOp == OP_UPDATE) {
3010 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003011 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003012 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003013 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003014 }
3015 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003016
3017 // Update removedComponents as some components can get removed during package update
3018 for (AppInfo info : removedApps) {
3019 removedComponents.add(info.componentName);
3020 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003021 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003022
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003023 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3024 for (String pn : removedPackages) {
3025 deletePackageFromDatabase(context, pn, mUser);
3026 }
3027 for (ComponentName cn : removedComponents) {
3028 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003029 }
3030
Winson Chungdf95eb12013-10-16 14:57:07 -07003031 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003032 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3033
Winson Chungdf95eb12013-10-16 14:57:07 -07003034 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003035 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003036 mHandler.post(new Runnable() {
3037 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003038 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003039 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003040 callbacks.bindWorkspaceComponentsRemoved(
3041 removedPackages, removedComponents, mUser);
3042 }
3043 }
3044 });
3045 }
3046
3047 if (!removedApps.isEmpty()) {
3048 // Remove corresponding apps from All-Apps
3049 final Callbacks callbacks = getCallback();
3050 mHandler.post(new Runnable() {
3051 public void run() {
3052 Callbacks cb = getCallback();
3053 if (callbacks == cb && cb != null) {
3054 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003055 }
3056 }
3057 });
Joe Onoratobe386092009-11-17 17:32:16 -08003058 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003059
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003060 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3061 // get widget update signals.
3062 if (!Utilities.ATLEAST_MARSHMALLOW &&
3063 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003064 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003065 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003066 public void run() {
3067 Callbacks cb = getCallback();
3068 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003069 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003070 }
3071 }
3072 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003073 }
3074 }
3075 }
3076
3077 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3078 mHandler.post(new Runnable() {
3079 @Override
3080 public void run() {
3081 Callbacks cb = getCallback();
3082 if (callbacks == cb && cb != null) {
3083 callbacks.bindWidgetsModel(model);
3084 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003085 }
3086 });
3087 }
3088
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003089 public void refreshAndBindWidgetsAndShortcuts(
3090 final Callbacks callbacks, final boolean bindFirst) {
3091 runOnWorkerThread(new Runnable() {
3092 @Override
3093 public void run() {
3094 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3095 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003096 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003097 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3098 bindWidgetsModel(callbacks, model);
3099 // update the Widget entries inside DB on the worker thread.
3100 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3101 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003102 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003103 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003104 }
3105
Adam Cohen091440a2015-03-18 14:16:05 -07003106 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003107 UserHandleCompat user) {
3108 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3109 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003110 }
Adam Cohen556f6132014-01-15 15:18:08 -08003111
Kenny Guyed131872014-04-30 03:02:21 +01003112 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3113 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003114 if (cn == null) {
3115 return false;
3116 }
Kenny Guyed131872014-04-30 03:02:21 +01003117 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3118 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003119 return false;
3120 }
Kenny Guyed131872014-04-30 03:02:21 +01003121 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003122 }
3123
Adam Cohena28b78e2014-05-20 17:03:04 -07003124 public static boolean isValidPackage(Context context, String packageName,
3125 UserHandleCompat user) {
3126 if (packageName == null) {
3127 return false;
3128 }
3129 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3130 return launcherApps.isPackageEnabledForProfile(packageName, user);
3131 }
3132
Joe Onorato9c1289c2009-08-17 11:03:03 -04003133 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003134 * Make an ShortcutInfo object for a restored application or shortcut item that points
3135 * to a package that is not yet installed on the system.
3136 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003137 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003138 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003139 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003140 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003141
3142 Bitmap icon = iconInfo.loadIcon(c, info, context);
3143 // the fallback icon
3144 if (icon == null) {
3145 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3146 } else {
3147 info.setIcon(icon);
3148 }
Sunny Goyal34942622014-08-29 17:20:55 -07003149
3150 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003151 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003152 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003153 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003154 }
Sunny Goyal34942622014-08-29 17:20:55 -07003155 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3156 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003157 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003158 }
Sunny Goyal34942622014-08-29 17:20:55 -07003159 } else {
3160 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3161 }
3162
Winson Chung82b016c2015-05-08 17:00:10 -07003163 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003164 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003165 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003166 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003167 return info;
3168 }
3169
3170 /**
3171 * Make an Intent object for a restored application or shortcut item that points
3172 * to the market page for the item.
3173 */
Adam Cohen091440a2015-03-18 14:16:05 -07003174 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003175 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003176 return getMarketIntent(componentName.getPackageName());
3177 }
3178
3179 static Intent getMarketIntent(String packageName) {
3180 return new Intent(Intent.ACTION_VIEW)
3181 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003182 .scheme("market")
3183 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003184 .appendQueryParameter("id", packageName)
3185 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003186 }
3187
3188 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003189 * Make an ShortcutInfo object for a shortcut that is an application.
3190 *
3191 * If c is not null, then it will be used to fill in missing data like the title and icon.
3192 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003193 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003194 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003195 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003196 if (user == null) {
3197 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003198 return null;
3199 }
3200
Kenny Guyed131872014-04-30 03:02:21 +01003201 ComponentName componentName = intent.getComponent();
3202 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003203 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003204 return null;
3205 }
3206
3207 Intent newIntent = new Intent(intent.getAction(), null);
3208 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3209 newIntent.setComponent(componentName);
3210 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003211 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003212 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3213 return null;
3214 }
3215
3216 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003217 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003218 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3219 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3220 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003221 }
3222
Sunny Goyald09c3702016-04-06 16:18:20 -07003223 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3224 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3225 }
3226
Joe Onorato56d82912010-03-07 14:32:10 -05003227 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003228 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003229 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003230 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003231
Joe Onorato56d82912010-03-07 14:32:10 -05003232 // fall back to the class name of the activity
3233 if (info.title == null) {
3234 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003235 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003236
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003238 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003239 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003240 if (lai != null) {
3241 info.flags = AppInfo.initFlags(lai);
3242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003245
Sunny Goyale2df0622015-04-24 11:27:00 -07003246 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003247 ItemInfoFilter f) {
3248 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3249 for (ItemInfo i : infos) {
3250 if (i instanceof ShortcutInfo) {
3251 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003252 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003253 if (cn != null && f.filterItem(null, info, cn)) {
3254 filtered.add(info);
3255 }
3256 } else if (i instanceof FolderInfo) {
3257 FolderInfo info = (FolderInfo) i;
3258 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003259 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003260 if (cn != null && f.filterItem(info, s, cn)) {
3261 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003262 }
3263 }
Winson Chung64359a52013-07-08 17:17:08 -07003264 } else if (i instanceof LauncherAppWidgetInfo) {
3265 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3266 ComponentName cn = info.providerName;
3267 if (cn != null && f.filterItem(null, info, cn)) {
3268 filtered.add(info);
3269 }
Winson Chung8a435102012-08-30 17:16:53 -07003270 }
3271 }
Winson Chung64359a52013-07-08 17:17:08 -07003272 return new ArrayList<ItemInfo>(filtered);
3273 }
3274
Adam Cohen091440a2015-03-18 14:16:05 -07003275 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003276 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003277 ItemInfoFilter filter = new ItemInfoFilter() {
3278 @Override
3279 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003280 if (info.user == null) {
3281 return cn.equals(cname);
3282 } else {
3283 return cn.equals(cname) && info.user.equals(user);
3284 }
Winson Chung64359a52013-07-08 17:17:08 -07003285 }
3286 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003287 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003288 }
3289
Sunny Goyal1a745e82014-10-02 15:58:31 -07003290 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003291 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003292 */
Adam Cohen091440a2015-03-18 14:16:05 -07003293 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003294 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003295 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003296 // Non-app shortcuts are only supported for current user.
3297 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003299
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003300 // TODO: If there's an explicit component and we can't install that, delete it.
3301
Winson Chung82b016c2015-05-08 17:00:10 -07003302 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003303
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003304 Bitmap icon = iconInfo.loadIcon(c, info, context);
3305 // the fallback icon
3306 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003307 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003308 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003309 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003310 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 return info;
3312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003313
Sunny Goyal2350bc92014-10-14 16:42:54 -07003314 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003315 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3316 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3317 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3318
Adam Cohend9198822011-11-22 16:42:47 -08003319 if (intent == null) {
3320 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3321 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3322 return null;
3323 }
3324
Joe Onorato0589f0f2010-02-08 13:44:00 -08003325 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003326 boolean customIcon = false;
3327 ShortcutIconResource iconResource = null;
3328
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003329 if (bitmap instanceof Bitmap) {
3330 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003331 customIcon = true;
3332 } else {
3333 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003334 if (extra instanceof ShortcutIconResource) {
3335 iconResource = (ShortcutIconResource) extra;
3336 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003337 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003338 }
3339 }
3340
Michael Jurkac9d95c52011-08-29 14:03:34 -07003341 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003342
Kenny Guyed131872014-04-30 03:02:21 +01003343 // Only support intents for current user for now. Intents sent from other
3344 // users wouldn't get here without intent forwarding anyway.
3345 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003346 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003347 icon = mIconCache.getDefaultIcon(info.user);
3348 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003349 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003350 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003351
Winson Chung82b016c2015-05-08 17:00:10 -07003352 info.title = Utilities.trim(name);
3353 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003354 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003355 info.iconResource = iconResource;
3356
3357 return info;
3358 }
3359
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003361 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 * or make a new one.
3363 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003364 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 // See if a placeholder was created for us already
3366 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003367 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003369 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003370 folders.put(id, folderInfo);
3371 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003372 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003373 }
3374
Joe Onoratobe386092009-11-17 17:32:16 -08003375
Sunny Goyal651077b2014-06-30 14:15:31 -07003376 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3377 return (provider != null) && (provider.provider != null)
3378 && (provider.provider.getPackageName() != null);
3379 }
3380
Joe Onoratobe386092009-11-17 17:32:16 -08003381 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003382 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003383 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3384 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3385 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3386 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003387 if (mLoaderTask != null) {
3388 mLoaderTask.dumpState();
3389 } else {
3390 Log.d(TAG, "mLoaderTask=null");
3391 }
Joe Onoratobe386092009-11-17 17:32:16 -08003392 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003393
3394 public Callbacks getCallback() {
3395 return mCallbacks != null ? mCallbacks.get() : null;
3396 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003397
3398 /**
3399 * @return {@link FolderInfo} if its already loaded.
3400 */
3401 public FolderInfo findFolderById(Long folderId) {
3402 synchronized (sBgLock) {
3403 return sBgFolders.get(folderId);
3404 }
3405 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003406
Sunny Goyal527c7d32015-08-28 15:19:36 -07003407 @Thunk class DeferredMainThreadExecutor implements Executor {
3408
3409 @Override
3410 public void execute(Runnable command) {
3411 runOnMainThread(command);
3412 }
3413 }
3414
Sunny Goyal756adbc2015-04-16 15:20:51 -07003415 /**
3416 * @return the looper for the worker thread which can be used to start background tasks.
3417 */
3418 public static Looper getWorkerLooper() {
3419 return sWorkerThread.getLooper();
3420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003421}