blob: 4c80ccf690acb3364724936eafff1f97b71b4f1d [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);
Winson Chunga0b7e862013-09-05 16:03:15 -0700199 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700200 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700201 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Winson Chung64359a52013-07-08 17:17:08 -0700204 public interface ItemInfoFilter {
205 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
206 }
207
Bjorn Bringert1307f632013-10-03 22:31:03 +0100208 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800209 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400210 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100211 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700212 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 mIconCache = iconCache;
214
Kenny Guyed131872014-04-30 03:02:21 +0100215 mLauncherApps = LauncherAppsCompat.getInstance(context);
216 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 }
218
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700219 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
220 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700222 if (sWorkerThread.getThreadId() == Process.myTid()) {
223 // If we are on the worker thread, post onto the main handler
224 mHandler.post(r);
225 } else {
226 r.run();
227 }
228 }
229
230 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
231 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700232 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700233 if (sWorkerThread.getThreadId() == Process.myTid()) {
234 r.run();
235 } else {
236 // If we are not on the worker thread, then post to the worker handler
237 sWorker.post(r);
238 }
239 }
240
Sunny Goyal756adbc2015-04-16 15:20:51 -0700241 public void setPackageState(final PackageInstallInfo installInfo) {
242 Runnable updateRunnable = new Runnable() {
243
244 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500245 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700246 synchronized (sBgLock) {
247 final HashSet<ItemInfo> updates = new HashSet<>();
248
249 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
250 // Ignore install success events as they are handled by Package add events.
251 return;
252 }
253
Sunny Goyale2df0622015-04-24 11:27:00 -0700254 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700255 if (info instanceof ShortcutInfo) {
256 ShortcutInfo si = (ShortcutInfo) info;
257 ComponentName cn = si.getTargetComponent();
258 if (si.isPromise() && (cn != null)
259 && installInfo.packageName.equals(cn.getPackageName())) {
260 si.setInstallProgress(installInfo.progress);
261
262 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
263 // Mark this info as broken.
264 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
265 }
266 updates.add(si);
267 }
268 }
269 }
270
271 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
272 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
273 widget.installProgress = installInfo.progress;
274 updates.add(widget);
275 }
276 }
277
278 if (!updates.isEmpty()) {
279 // Push changes to the callback.
280 Runnable r = new Runnable() {
281 public void run() {
282 Callbacks callbacks = getCallback();
283 if (callbacks != null) {
284 callbacks.bindRestoreItemsChange(updates);
285 }
286 }
287 };
288 mHandler.post(r);
289 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500290 }
291 }
292 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500294 }
295
Sunny Goyal756adbc2015-04-16 15:20:51 -0700296 /**
297 * Updates the icons and label of all pending icons for the provided package name.
298 */
299 public void updateSessionDisplayInfo(final String packageName) {
300 Runnable updateRunnable = new Runnable() {
301
302 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700303 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700304 synchronized (sBgLock) {
305 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
306 final UserHandleCompat user = UserHandleCompat.myUserHandle();
307
Sunny Goyale2df0622015-04-24 11:27:00 -0700308 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700309 if (info instanceof ShortcutInfo) {
310 ShortcutInfo si = (ShortcutInfo) info;
311 ComponentName cn = si.getTargetComponent();
312 if (si.isPromise() && (cn != null)
313 && packageName.equals(cn.getPackageName())) {
314 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
315 // For auto install apps update the icon as well as label.
316 mIconCache.getTitleAndIcon(si,
317 si.promisedIntent, user,
318 si.shouldUseLowResIcon());
319 } else {
320 // Only update the icon for restored apps.
321 si.updateIcon(mIconCache);
322 }
323 updates.add(si);
324 }
325 }
326 }
327
328 if (!updates.isEmpty()) {
329 // Push changes to the callback.
330 Runnable r = new Runnable() {
331 public void run() {
332 Callbacks callbacks = getCallback();
333 if (callbacks != null) {
334 callbacks.bindShortcutsChanged(updates,
335 new ArrayList<ShortcutInfo>(), user);
336 }
337 }
338 };
339 mHandler.post(r);
340 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700341 }
342 }
343 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700345 }
346
Adam Cohen76a47a12014-02-05 11:47:43 -0800347 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800348 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800349
350 if (allAppsApps == null) {
351 throw new RuntimeException("allAppsApps must not be null");
352 }
353 if (allAppsApps.isEmpty()) {
354 return;
355 }
356
357 // Process the newly added applications and add them to the database first
358 Runnable r = new Runnable() {
359 public void run() {
360 runOnMainThread(new Runnable() {
361 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800362 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800363 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500364 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800365 }
366 }
367 });
368 }
369 };
370 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700371 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800372
Sunny Goyala9116722015-04-29 13:55:58 -0700373 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800374 int[] xy, int spanX, int spanY) {
375 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700376 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700377
378 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800379 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700380 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700381 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800382 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800383 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700384 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800385 }
386
387 /**
388 * Find a position on the screen for the given size or adds a new screen.
389 * @return screenId and the coordinates for the item.
390 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700391 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700392 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 ArrayList<Long> workspaceScreens,
394 ArrayList<Long> addedWorkspaceScreensFinal,
395 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700396 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800397
Sunny Goyala9116722015-04-29 13:55:58 -0700398 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700399 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700400 synchronized (sBgLock) {
401 for (ItemInfo info : sBgItemsIdMap) {
402 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
403 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
404 if (items == null) {
405 items = new ArrayList<>();
406 screenItems.put(info.screenId, items);
407 }
408 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800410 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 }
412
413 // Find appropriate space for the item.
414 long screenId = 0;
415 int[] cordinates = new int[2];
416 boolean found = false;
417
418 int screenCount = workspaceScreens.size();
419 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700420 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 if (preferredScreenIndex < screenCount) {
422 screenId = workspaceScreens.get(preferredScreenIndex);
423 found = findNextAvailableIconSpaceInScreen(
424 screenItems.get(screenId), cordinates, spanX, spanY);
425 }
426
427 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700428 // Search on any of the screens starting from the first screen.
429 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 screenId = workspaceScreens.get(screen);
431 if (findNextAvailableIconSpaceInScreen(
432 screenItems.get(screenId), cordinates, spanX, spanY)) {
433 // We found a space for it
434 found = true;
435 break;
436 }
437 }
438 }
439
440 if (!found) {
441 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700442 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
443 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
444 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445
446 // Save the screen id for binding in the workspace
447 workspaceScreens.add(screenId);
448 addedWorkspaceScreensFinal.add(screenId);
449
450 // If we still can't find an empty space, then God help us all!!!
451 if (!findNextAvailableIconSpaceInScreen(
452 screenItems.get(screenId), cordinates, spanX, spanY)) {
453 throw new RuntimeException("Can't find space to add the item");
454 }
455 }
456 return Pair.create(screenId, cordinates);
457 }
458
459 /**
460 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800461 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700462 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700463 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800465 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700466 return;
Winson Chung997a9232013-07-24 15:33:46 -0700467 }
Winson Chung64359a52013-07-08 17:17:08 -0700468 // Process the newly added applications and add them to the database first
469 Runnable r = new Runnable() {
470 public void run() {
471 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
472 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
473
Winson Chung76828c82013-08-19 15:43:29 -0700474 // Get the list of workspace screens. We need to append to this list and
475 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
476 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800477 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700478 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800479 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700480 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800481 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700482 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700484 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800485 }
Winson Chung76828c82013-08-19 15:43:29 -0700486
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800487 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700488 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700489 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800490 long screenId = coords.first;
491 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700492
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700493 ItemInfo itemInfo;
494 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
495 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800496 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700497 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700498 } else {
499 throw new RuntimeException("Unexpected info type");
500 }
Winson Chung94d67682013-09-25 16:29:40 -0700501
Winson Chung64359a52013-07-08 17:17:08 -0700502 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700503 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700504 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700505 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700506 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700507 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700508 }
509 }
510
Winson Chung76828c82013-08-19 15:43:29 -0700511 // Update the workspace screens
512 updateWorkspaceScreenOrder(context, workspaceScreens);
513
Adam Cohen76a47a12014-02-05 11:47:43 -0800514 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700515 runOnMainThread(new Runnable() {
516 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800517 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700518 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700519 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
520 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700521 if (!addedShortcutsFinal.isEmpty()) {
522 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
523 long lastScreenId = info.screenId;
524 for (ItemInfo i : addedShortcutsFinal) {
525 if (i.screenId == lastScreenId) {
526 addAnimated.add(i);
527 } else {
528 addNotAnimated.add(i);
529 }
Winson Chung997a9232013-07-24 15:33:46 -0700530 }
531 }
Winson Chungd64d1762013-08-20 14:37:16 -0700532 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800533 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700534 }
Winson Chung64359a52013-07-08 17:17:08 -0700535 }
Winson Chung997a9232013-07-24 15:33:46 -0700536 });
537 }
Winson Chung64359a52013-07-08 17:17:08 -0700538 }
539 };
540 runOnWorkerThread(r);
541 }
542
Joe Onorato9c1289c2009-08-17 11:03:03 -0400543 /**
544 * Adds an item to the DB if it was not created previously, or move it to a new
545 * <container, screen, cellX, cellY>
546 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800547 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700548 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549 if (item.container == ItemInfo.NO_ID) {
550 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700551 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 } else {
553 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700554 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556 }
557
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700558 static void checkItemInfoLocked(
559 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
560 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
561 if (modelItem != null && item != modelItem) {
562 // check all the data is consistent
563 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
564 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
565 ShortcutInfo shortcut = (ShortcutInfo) item;
566 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
567 modelShortcut.intent.filterEquals(shortcut.intent) &&
568 modelShortcut.id == shortcut.id &&
569 modelShortcut.itemType == shortcut.itemType &&
570 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700571 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700572 modelShortcut.cellX == shortcut.cellX &&
573 modelShortcut.cellY == shortcut.cellY &&
574 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700575 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700576 // For all intents and purposes, this is the same object
577 return;
578 }
579 }
580
581 // the modelItem needs to match up perfectly with item if our model is
582 // to be consistent with the database-- for now, just require
583 // modelItem == item or the equality check above
584 String msg = "item: " + ((item != null) ? item.toString() : "null") +
585 "modelItem: " +
586 ((modelItem != null) ? modelItem.toString() : "null") +
587 "Error: ItemInfo passed to checkItemInfo doesn't match original";
588 RuntimeException e = new RuntimeException(msg);
589 if (stackTrace != null) {
590 e.setStackTrace(stackTrace);
591 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800592 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700593 }
594 }
595
Michael Jurka816474f2012-06-25 14:49:02 -0700596 static void checkItemInfo(final ItemInfo item) {
597 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
598 final long itemId = item.id;
599 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700600 public void run() {
601 synchronized (sBgLock) {
602 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700603 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700604 }
605 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700606 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700607 }
608
Michael Jurkac9d95c52011-08-29 14:03:34 -0700609 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
610 final ItemInfo item, final String callingFunction) {
611 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700612 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700613 final ContentResolver cr = context.getContentResolver();
614
Adam Cohen487f7dd2012-06-28 18:12:10 -0700615 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700616 Runnable r = new Runnable() {
617 public void run() {
618 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700619 updateItemArrays(item, itemId, stackTrace);
620 }
621 };
622 runOnWorkerThread(r);
623 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700625 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
626 final ArrayList<ItemInfo> items, final String callingFunction) {
627 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700628
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700629 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
630 Runnable r = new Runnable() {
631 public void run() {
632 ArrayList<ContentProviderOperation> ops =
633 new ArrayList<ContentProviderOperation>();
634 int count = items.size();
635 for (int i = 0; i < count; i++) {
636 ItemInfo item = items.get(i);
637 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700638 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700639 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700640
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700641 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
642 updateItemArrays(item, itemId, stackTrace);
643
644 }
645 try {
646 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
647 } catch (Exception e) {
648 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700649 }
650 }
651 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700652 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700653 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700654
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700655 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
656 // Lock on mBgLock *after* the db operation
657 synchronized (sBgLock) {
658 checkItemInfoLocked(itemId, item, stackTrace);
659
660 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
661 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
662 // Item is in a folder, make sure this folder exists
663 if (!sBgFolders.containsKey(item.container)) {
664 // An items container is being set to a that of an item which is not in
665 // the list of Folders.
666 String msg = "item: " + item + " container being set to: " +
667 item.container + ", not in the list of folders";
668 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700669 }
670 }
671
672 // Items are added/removed from the corresponding FolderInfo elsewhere, such
673 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
674 // that are on the desktop, as appropriate
675 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800676 if (modelItem != null &&
677 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
678 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 switch (modelItem.itemType) {
680 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
681 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
682 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
683 if (!sBgWorkspaceItems.contains(modelItem)) {
684 sBgWorkspaceItems.add(modelItem);
685 }
686 break;
687 default:
688 break;
689 }
690 } else {
691 sBgWorkspaceItems.remove(modelItem);
692 }
693 }
694 }
695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700698 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700699 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700700 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 item.cellX = cellX;
703 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700704
Winson Chung3d503fb2011-07-13 17:25:49 -0700705 // We store hotseat items in canonical form which is this orientation invariant position
706 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700708 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700710 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700712 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713
714 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700716 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
717 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800718 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720
Michael Jurkac9d95c52011-08-29 14:03:34 -0700721 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700722 }
723
724 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
726 * cellX, cellY have already been updated on the ItemInfos.
727 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800728 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 final long container, final int screen) {
730
731 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
732 int count = items.size();
733
734 for (int i = 0; i < count; i++) {
735 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 item.container = container;
737
738 // We store hotseat items in canonical form which is this orientation invariant position
739 // in the hotseat
740 if (context instanceof Launcher && screen < 0 &&
741 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 item.cellY);
744 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700745 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700746 }
747
748 final ContentValues values = new ContentValues();
749 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
750 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
751 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800752 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700753 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754
755 contentValues.add(values);
756 }
757 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
758 }
759
760 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700761 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800762 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700763 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700765 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800766 item.cellX = cellX;
767 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700768 item.spanX = spanX;
769 item.spanY = spanY;
770
771 // We store hotseat items in canonical form which is this orientation invariant position
772 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700774 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700776 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700778 }
Adam Cohend4844c32011-02-18 19:25:06 -0800779
Adam Cohend4844c32011-02-18 19:25:06 -0800780 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800781 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700782 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
783 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800784 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
786 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800788
Michael Jurka816474f2012-06-25 14:49:02 -0700789 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700790 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700791
792 /**
793 * Update an item to the database in a specified container.
794 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700795 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700796 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100797 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700798 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800799 }
800
Sunny Goyal756a28a2015-04-23 17:07:55 -0700801 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700802 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700803 synchronized (mLock) {
804 if (!mHasLoaderCompletedOnce ||
805 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
806 throw new RuntimeException("Trying to add shortcut while loader is running");
807 }
808 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700809 }
810 }
811
Adam Cohend4844c32011-02-18 19:25:06 -0800812 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700813 * Returns true if the shortcuts already exists on the workspace. This must be called after
814 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700816 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
817 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700818 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700819 if (intent.getComponent() != null) {
820 // If component is not null, an intent with null package will produce
821 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700822 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700823 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700824 intentWithPkg = intent.toUri(0);
825 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700826 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700827 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
828 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700829 }
830 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700831 intentWithPkg = intent.toUri(0);
832 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700833 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700834
835 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700836 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700837 if (item instanceof ShortcutInfo) {
838 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700839 Intent targetIntent = info.promisedIntent == null
840 ? info.intent : info.promisedIntent;
841 if (targetIntent != null && info.user.equals(user)) {
842 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700843 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
844 return true;
845 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700846 }
847 }
848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700850 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700851 }
852
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 * Add an item to the database in a specified container. Sets the container, screen, cellX and
855 * cellY fields of the item. Also assigns an ID to the item.
856 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700857 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700858 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860 item.cellX = cellX;
861 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 // We store hotseat items in canonical form which is this orientation invariant position
863 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700867 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700868 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870
871 final ContentValues values = new ContentValues();
872 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100873 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874
Sunny Goyald2497482015-09-22 18:24:19 -0700875 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
876 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
877
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700878 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700879
Jason Monk8e19cf22014-03-20 15:06:57 -0400880 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700881 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700882 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700883 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700885 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700886 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400887 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700888 sBgItemsIdMap.put(item.id, item);
889 switch (item.itemType) {
890 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
891 sBgFolders.put(item.id, (FolderInfo) item);
892 // Fall through
893 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
894 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
895 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
896 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
897 sBgWorkspaceItems.add(item);
898 } else {
899 if (!sBgFolders.containsKey(item.container)) {
900 // Adding an item to a folder that doesn't exist.
901 String msg = "adding item: " + item + " to a folder that " +
902 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700903 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700904 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 break;
907 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
908 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
909 break;
910 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700911 }
912 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700915 }
916
Sunny Goyal34942622014-08-29 17:20:55 -0700917 private static ArrayList<ItemInfo> getItemsByPackageName(
918 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700919 ItemInfoFilter filter = new ItemInfoFilter() {
920 @Override
921 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
922 return cn.getPackageName().equals(pn) && info.user.equals(user);
923 }
924 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700925 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700926 }
927
928 /**
929 * Removes all the items from the database corresponding to the specified package.
930 */
931 static void deletePackageFromDatabase(Context context, final String pn,
932 final UserHandleCompat user) {
933 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700934 }
935
936 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700937 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700939 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700940 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
941 items.add(item);
942 deleteItemsFromDatabase(context, items);
943 }
944
945 /**
946 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700947 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700948 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700950 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700952 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700953 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700954 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700956 // Lock on mBgLock *after* the db operation
957 synchronized (sBgLock) {
958 switch (item.itemType) {
959 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
960 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700961 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700962 if (info.container == item.id) {
963 // We are deleting a folder which still contains items that
964 // think they are contained by that folder.
965 String msg = "deleting a folder (" + item + ") which still " +
966 "contains items (" + info + ")";
967 Log.e(TAG, msg);
968 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700969 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700970 sBgWorkspaceItems.remove(item);
971 break;
972 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
973 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
974 sBgWorkspaceItems.remove(item);
975 break;
976 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
977 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
978 break;
979 }
980 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700981 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700982 }
983 }
Michael Jurka83df1882011-08-31 20:59:26 -0700984 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 }
987
988 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 * Update the order of the workspace screens in the database. The array list contains
990 * a list of screen ids in the order that they should appear.
991 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700992 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700993 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 final ContentResolver cr = context.getContentResolver();
995 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
996
997 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700998 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700999 while (iter.hasNext()) {
1000 long id = iter.next();
1001 if (id < 0) {
1002 iter.remove();
1003 }
1004 }
1005
1006 Runnable r = new Runnable() {
1007 @Override
1008 public void run() {
Yura085c8532014-02-11 15:15:29 +00001009 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001010 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001011 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001012 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001013 for (int i = 0; i < count; i++) {
1014 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001015 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001016 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1017 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001018 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001019 }
Yura085c8532014-02-11 15:15:29 +00001020
1021 try {
1022 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1023 } catch (Exception ex) {
1024 throw new RuntimeException(ex);
1025 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001026
Winson Chungba9c37f2013-08-30 14:11:37 -07001027 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001028 sBgWorkspaceScreens.clear();
1029 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001030 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001031 }
1032 };
1033 runOnWorkerThread(r);
1034 }
1035
1036 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001037 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 */
Winsonc0b52fe2015-09-09 16:38:15 -07001039 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 final ContentResolver cr = context.getContentResolver();
1041
Michael Jurkac9d95c52011-08-29 14:03:34 -07001042 Runnable r = new Runnable() {
1043 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001044 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001046 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 sBgItemsIdMap.remove(info.id);
1048 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 sBgWorkspaceItems.remove(info);
1050 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001051
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001052 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001053 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001055 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 for (ItemInfo childInfo : info.contents) {
1057 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001059 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001060 }
1061 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001062 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 }
1064
1065 /**
1066 * Set this as the current Launcher activity object for the loader.
1067 */
1068 public void initialize(Callbacks callbacks) {
1069 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001070 Preconditions.assertUIThread();
1071 // Remove any queued UI runnables
1072 mHandler.cancelAll();
1073 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 }
1075 }
1076
Kenny Guyed131872014-04-30 03:02:21 +01001077 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001078 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001079 int op = PackageUpdatedTask.OP_UPDATE;
1080 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1081 user));
1082 }
1083
1084 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001085 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001086 int op = PackageUpdatedTask.OP_REMOVE;
1087 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1088 user));
1089 }
1090
1091 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001092 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001093 int op = PackageUpdatedTask.OP_ADD;
1094 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1095 user));
1096 }
1097
1098 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001099 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001100 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001101 enqueuePackageUpdated(
1102 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001103 }
1104
1105 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001106 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001107 boolean replacing) {
1108 if (!replacing) {
1109 enqueuePackageUpdated(new PackageUpdatedTask(
1110 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1111 user));
1112 }
Kenny Guyed131872014-04-30 03:02:21 +01001113 }
1114
Kenny Guy44cba692016-01-21 19:50:02 +00001115 @Override
1116 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1117 enqueuePackageUpdated(new PackageUpdatedTask(
1118 PackageUpdatedTask.OP_SUSPEND, packageNames,
1119 user));
1120 }
1121
1122 @Override
1123 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1124 enqueuePackageUpdated(new PackageUpdatedTask(
1125 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1126 user));
1127 }
1128
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001129 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1131 * ACTION_PACKAGE_CHANGED.
1132 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001133 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001134 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001135 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001136
Joe Onorato36115782010-06-17 13:28:48 -04001137 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001138 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001139 // If we have changed locale we need to clear out the labels in all apps/workspace.
1140 forceReload();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001141 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001142 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001143 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001144 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001145 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1146 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001147 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1148 if (user != null) {
1149 enqueuePackageUpdated(new PackageUpdatedTask(
1150 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1151 new String[0], user));
1152 }
Tony Wickham827cef22016-03-17 15:39:39 -07001153 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1154 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001155 }
1156 }
1157
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001158 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001159 resetLoadedState(true, true);
1160
Reena Lee93f824a2011-09-23 17:20:28 -07001161 // Do this here because if the launcher activity is running it will be restarted.
1162 // If it's not running startLoaderFromBackground will merely tell it that it needs
1163 // to reload.
1164 startLoaderFromBackground();
1165 }
1166
Winson Chungf0c6ae02012-03-21 16:10:31 -07001167 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1168 synchronized (mLock) {
1169 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1170 // mWorkspaceLoaded to true later
1171 stopLoaderLocked();
1172 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1173 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1174 }
1175 }
1176
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001177 /**
1178 * When the launcher is in the background, it's possible for it to miss paired
1179 * configuration changes. So whenever we trigger the loader from the background
1180 * tell the launcher that it needs to re-run the loader when it comes back instead
1181 * of doing it now.
1182 */
1183 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001184 Callbacks callbacks = getCallback();
1185 if (callbacks != null) {
1186 // Only actually run the loader if they're not paused.
1187 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001188 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001189 }
1190 }
Joe Onorato36115782010-06-17 13:28:48 -04001191 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001192
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001193 /**
1194 * If there is already a loader task running, tell it to stop.
1195 */
1196 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001197 LoaderTask oldTask = mLoaderTask;
1198 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001199 oldTask.stopLocked();
1200 }
Reena Lee93f824a2011-09-23 17:20:28 -07001201 }
1202
Adam Cohen1a85c582014-09-30 09:48:49 -07001203 public boolean isCurrentCallbacks(Callbacks callbacks) {
1204 return (mCallbacks != null && mCallbacks.get() == callbacks);
1205 }
1206
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001207 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001208 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1209 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001210 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001211 // Don't bother to start the thread if we know it's not going to do anything
1212 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001213 final Callbacks oldCallbacks = mCallbacks.get();
1214 // Clear any pending bind-runnables from the synchronized load process.
1215 runOnMainThread(new Runnable() {
1216 public void run() {
1217 oldCallbacks.clearPendingBinds();
1218 }
1219 });
1220
Joe Onorato36115782010-06-17 13:28:48 -04001221 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001222 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001223 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001224 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001225 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001226 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1227 } else {
1228 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1229 sWorker.post(mLoaderTask);
1230 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001231 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 }
1233 }
1234
Joe Onorato36115782010-06-17 13:28:48 -04001235 public void stopLoader() {
1236 synchronized (mLock) {
1237 if (mLoaderTask != null) {
1238 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 }
1240 }
Joe Onorato36115782010-06-17 13:28:48 -04001241 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001242
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001243 /**
1244 * Loads the workspace screen ids in an ordered list.
1245 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001246 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001247 final ContentResolver contentResolver = context.getContentResolver();
1248 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001249
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001250 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001251 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1252 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001253 }
1254
Joe Onorato36115782010-06-17 13:28:48 -04001255 /**
1256 * Runnable for the thread that loads the contents of the launcher:
1257 * - workspace icons
1258 * - widgets
1259 * - all apps icons
1260 */
1261 private class LoaderTask implements Runnable {
1262 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001263 private int mPageToBindFirst;
1264
Adam Cohen091440a2015-03-18 14:16:05 -07001265 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001266 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001267 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001268
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001269 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001270 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001271 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001272 }
1273
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001274 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001275 mIsLoadingAndBindingWorkspace = true;
1276
Joe Onorato36115782010-06-17 13:28:48 -04001277 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001278 if (DEBUG_LOADERS) {
1279 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001281
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001282 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001283 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001284 synchronized (LoaderTask.this) {
1285 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001286 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001287 }
1288 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 }
1290 }
1291
Joe Onorato36115782010-06-17 13:28:48 -04001292 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001293 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001294 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001295
Joe Onorato36115782010-06-17 13:28:48 -04001296 private void waitForIdle() {
1297 // Wait until the either we're stopped or the other threads are done.
1298 // This way we don't start loading all apps until the workspace has settled
1299 // down.
1300 synchronized (LoaderTask.this) {
1301 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001302
Joe Onorato36115782010-06-17 13:28:48 -04001303 mHandler.postIdle(new Runnable() {
1304 public void run() {
1305 synchronized (LoaderTask.this) {
1306 mLoadAndBindStepFinished = true;
1307 if (DEBUG_LOADERS) {
1308 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001309 }
Joe Onorato36115782010-06-17 13:28:48 -04001310 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001311 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001312 }
Joe Onorato36115782010-06-17 13:28:48 -04001313 });
1314
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001315 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001316 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001317 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1318 // wait no longer than 1sec at a time
1319 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001320 } catch (InterruptedException ex) {
1321 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001322 }
1323 }
Joe Onorato36115782010-06-17 13:28:48 -04001324 if (DEBUG_LOADERS) {
1325 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001326 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001327 + "ms for previous step to finish binding");
1328 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001329 }
Joe Onorato36115782010-06-17 13:28:48 -04001330 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001331
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001332 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001333 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001334 // Ensure that we have a valid page index to load synchronously
1335 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1336 "valid page index");
1337 }
1338 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1339 // Ensure that we don't try and bind a specified page when the pages have not been
1340 // loaded already (we should load everything asynchronously in that case)
1341 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1342 }
1343 synchronized (mLock) {
1344 if (mIsLoaderTaskRunning) {
1345 // Ensure that we are never running the background loading at this point since
1346 // we also touch the background collections
1347 throw new RuntimeException("Error! Background loading is already running");
1348 }
1349 }
1350
1351 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1352 // data structures, we can't allow any other thread to touch that data, but because
1353 // this call is synchronous, we can get away with not locking).
1354
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001355 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001356 // operations from the previous activity. We need to ensure that all queued operations
1357 // are executed before any synchronous binding work is done.
1358 mHandler.flush();
1359
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001360 // Divide the set of loaded items into those that we are binding synchronously, and
1361 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001362 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001363 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1364 // arise from that.
1365 onlyBindAllApps();
1366 }
1367
Joe Onorato36115782010-06-17 13:28:48 -04001368 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001370 if (mStopped) {
1371 return;
1372 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 mIsLoaderTaskRunning = true;
1374 }
Joe Onorato36115782010-06-17 13:28:48 -04001375 // Optimize for end-user experience: if the Launcher is up and // running with the
1376 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1377 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001378 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001379 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001380 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001381
Joe Onorato36115782010-06-17 13:28:48 -04001382 if (mStopped) {
1383 break keep_running;
1384 }
1385
Joe Onorato36115782010-06-17 13:28:48 -04001386 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001387
1388 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001389 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1390 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392
Joe Onorato36115782010-06-17 13:28:48 -04001393 // Clear out this reference, otherwise we end up holding it until all of the
1394 // callback runnables are done.
1395 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001396
Joe Onorato36115782010-06-17 13:28:48 -04001397 synchronized (mLock) {
1398 // If we are still the last one to be scheduled, remove ourselves.
1399 if (mLoaderTask == this) {
1400 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001401 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001402 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001403 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001404 }
Joe Onorato36115782010-06-17 13:28:48 -04001405 }
1406
1407 public void stopLocked() {
1408 synchronized (LoaderTask.this) {
1409 mStopped = true;
1410 this.notify();
1411 }
1412 }
1413
1414 /**
1415 * Gets the callbacks object. If we've been stopped, or if the launcher object
1416 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1417 * object that was around when the deferred message was scheduled, and if there's
1418 * a new Callbacks object around then also return null. This will save us from
1419 * calling onto it with data that will be ignored.
1420 */
1421 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1422 synchronized (mLock) {
1423 if (mStopped) {
1424 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001425 }
Joe Onorato36115782010-06-17 13:28:48 -04001426
1427 if (mCallbacks == null) {
1428 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001429 }
Joe Onorato36115782010-06-17 13:28:48 -04001430
1431 final Callbacks callbacks = mCallbacks.get();
1432 if (callbacks != oldCallbacks) {
1433 return null;
1434 }
1435 if (callbacks == null) {
1436 Log.w(TAG, "no mCallbacks");
1437 return null;
1438 }
1439
1440 return callbacks;
1441 }
1442 }
1443
1444 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001445 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001446 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001447 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001448 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001449
Adam Cohendcd297f2013-06-18 13:13:40 -07001450 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001451 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001452 // Return early if we detect that an item is under the hotseat button
1453 if (mCallbacks == null ||
1454 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001455 Log.e(TAG, "Error loading shortcut into hotseat " + item
1456 + " into position (" + item.screenId + ":" + item.cellX + ","
1457 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001458 return false;
1459 }
1460
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001461 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001462 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1463
Adam Cohen2e6da152015-05-06 11:42:25 -07001464 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001465 Log.e(TAG, "Error loading shortcut " + item
1466 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001467 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001468 + ")");
1469 return false;
1470 }
1471
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001472 if (hotseatOccupancy != null) {
1473 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001474 Log.e(TAG, "Error loading shortcut into hotseat " + item
1475 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001476 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001478 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001479 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001480 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001481 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001482 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001483 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1484 occupancy.cells[(int) item.screenId][0] = true;
1485 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001486 return true;
1487 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001488 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1489 if (!workspaceScreens.contains((Long) item.screenId)) {
1490 // The item has an invalid screen id.
1491 return false;
1492 }
1493 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001494 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001495 return true;
1496 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001497
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001498 final int countX = profile.numColumns;
1499 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001500 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1501 item.cellX < 0 || item.cellY < 0 ||
1502 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1503 Log.e(TAG, "Error loading shortcut " + item
1504 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1505 + item.cellX + "," + item.cellY
1506 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1507 return false;
1508 }
1509
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001510 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001511 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1512 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001513 // Mark the first row as occupied (if the feature is enabled)
1514 // in order to account for the QSB.
1515 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001516 }
1517 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001518 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001519 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001520
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001521 // Check if any workspace icons overlap with each other
1522 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1523 occupancy.markCells(item, true);
1524 return true;
1525 } else {
1526 Log.e(TAG, "Error loading shortcut " + item
1527 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1528 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1529 + ") already occupied");
1530 return false;
1531 }
Joe Onorato36115782010-06-17 13:28:48 -04001532 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533
Winson Chungba9c37f2013-08-30 14:11:37 -07001534 /** Clears all the sBg data structures */
1535 private void clearSBgDataStructures() {
1536 synchronized (sBgLock) {
1537 sBgWorkspaceItems.clear();
1538 sBgAppWidgets.clear();
1539 sBgFolders.clear();
1540 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001541 sBgWorkspaceScreens.clear();
1542 }
1543 }
1544
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001545 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001546 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001547
Joe Onorato36115782010-06-17 13:28:48 -04001548 final Context context = mContext;
1549 final ContentResolver contentResolver = context.getContentResolver();
1550 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001551 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001552 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001553 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001554
Winson Chung892c74d2013-08-22 16:15:50 -07001555 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001557 int countX = profile.numColumns;
1558 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001559
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001560 boolean clearDb = false;
Sunny Goyalf076eae2016-01-11 12:25:10 -08001561 if (GridSizeMigrationTask.ENABLED &&
1562 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1563 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001564 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001565 }
1566
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001567 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001568 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001569 LauncherSettings.Settings.call(contentResolver,
1570 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001571 }
1572
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001573 Log.d(TAG, "loadWorkspace: loading default favorites");
1574 LauncherSettings.Settings.call(contentResolver,
1575 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001576
Winson Chung2abf94d2012-07-18 18:16:38 -07001577 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001578 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001579 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001580 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001581 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001582
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001583 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1584 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001585 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001586 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001587 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001588
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001589 // +1 for the hotseat (it can be larger than the workspace)
1590 // Load workspace in reverse order to ensure that latest items are loaded first (and
1591 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001592 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001593 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001594
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001595 try {
1596 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1597 final int intentIndex = c.getColumnIndexOrThrow
1598 (LauncherSettings.Favorites.INTENT);
1599 final int titleIndex = c.getColumnIndexOrThrow
1600 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001601 final int containerIndex = c.getColumnIndexOrThrow(
1602 LauncherSettings.Favorites.CONTAINER);
1603 final int itemTypeIndex = c.getColumnIndexOrThrow(
1604 LauncherSettings.Favorites.ITEM_TYPE);
1605 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1606 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001607 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1608 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001609 final int screenIndex = c.getColumnIndexOrThrow(
1610 LauncherSettings.Favorites.SCREEN);
1611 final int cellXIndex = c.getColumnIndexOrThrow
1612 (LauncherSettings.Favorites.CELLX);
1613 final int cellYIndex = c.getColumnIndexOrThrow
1614 (LauncherSettings.Favorites.CELLY);
1615 final int spanXIndex = c.getColumnIndexOrThrow
1616 (LauncherSettings.Favorites.SPANX);
1617 final int spanYIndex = c.getColumnIndexOrThrow(
1618 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001619 final int rankIndex = c.getColumnIndexOrThrow(
1620 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001621 final int restoredIndex = c.getColumnIndexOrThrow(
1622 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001623 final int profileIdIndex = c.getColumnIndexOrThrow(
1624 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001625 final int optionsIndex = c.getColumnIndexOrThrow(
1626 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001627 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001628
Sunny Goyal7f834d22015-04-21 10:10:23 -07001629 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001630 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001631 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001632 long serialNo = mUserManager.getSerialNumberForUser(user);
1633 allUsers.put(serialNo, user);
1634 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001635 }
1636
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 ShortcutInfo info;
1638 String intentDescription;
1639 LauncherAppWidgetInfo appWidgetInfo;
1640 int container;
1641 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001642 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001643 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001644 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001645 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001646
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001647 while (!mStopped && c.moveToNext()) {
1648 try {
1649 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001650 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001651 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001652 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001653
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001654 switch (itemType) {
1655 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1656 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001657 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001658 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001659 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001660 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001661 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001662 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001663 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001664 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001665 if (user == null) {
1666 // User has been deleted remove the item.
1667 itemsToRemove.add(id);
1668 continue;
1669 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001670 try {
1671 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001672 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001673 if (cn != null && cn.getPackageName() != null) {
1674 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1675 cn.getPackageName(), user);
1676 boolean validComponent = validPkg &&
1677 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001678 if (validPkg) {
1679 targetPackage = cn.getPackageName();
1680 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001681
1682 if (validComponent) {
1683 if (restored) {
1684 // no special handling necessary for this item
1685 restoredRows.add(id);
1686 restored = false;
1687 }
Kenny Guyff05f432016-01-22 17:48:29 +00001688 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001689 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001690 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001691 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001692 intent = null;
1693 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1694 // We allow auto install apps to have their intent
1695 // updated after an install.
1696 intent = manager.getLaunchIntentForPackage(
1697 cn.getPackageName());
1698 if (intent != null) {
1699 ContentValues values = new ContentValues();
1700 values.put(LauncherSettings.Favorites.INTENT,
1701 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001702 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001703 }
1704 }
1705
1706 if (intent == null) {
1707 // The app is installed but the component is no
1708 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001709 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001710 itemsToRemove.add(id);
1711 continue;
1712 } else {
1713 // no special handling necessary for this item
1714 restoredRows.add(id);
1715 restored = false;
1716 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001717 } else if (restored) {
1718 // Package is not yet available but might be
1719 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001720 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001721
1722 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1723 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001724 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001725 // App restore has started. Update the flag
1726 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1727 ContentValues values = new ContentValues();
1728 values.put(LauncherSettings.Favorites.RESTORED,
1729 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001730 updateItem(id, values);
1731 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1732 // This is a common app. Try to replace this.
1733 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1734 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1735 if (parser.findDefaultApp()) {
1736 // Default app found. Replace it.
1737 intent = parser.parsedIntent;
1738 cn = intent.getComponent();
1739 ContentValues values = parser.parsedValues;
1740 values.put(LauncherSettings.Favorites.RESTORED, 0);
1741 updateItem(id, values);
1742 restored = false;
1743 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001744
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001745 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001746 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001747 itemsToRemove.add(id);
1748 continue;
1749 }
Sunny Goyal94485362014-09-18 16:13:58 -07001750 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001751 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001752 itemsToRemove.add(id);
1753 continue;
1754 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001755 } else if (PackageManagerHelper.isAppOnSdcard(
1756 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001757 // Package is present but not available.
1758 allowMissingTarget = true;
1759 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1760 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001761 // SdCard is not ready yet. Package might get available,
1762 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001763 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001764 HashSet<String> pkgs = sPendingPackages.get(user);
1765 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001766 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001767 sPendingPackages.put(user, pkgs);
1768 }
1769 pkgs.add(cn.getPackageName());
1770 allowMissingTarget = true;
1771 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001772
1773 } else {
1774 // Do not wait for external media load anymore.
1775 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001776 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001777 itemsToRemove.add(id);
1778 continue;
Winson Chungee055712013-07-30 14:46:24 -07001779 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001780 } else if (cn == null) {
1781 // For shortcuts with no component, keep them as they are
1782 restoredRows.add(id);
1783 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001784 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001786 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001787 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 continue;
1789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790
Sunny Goyal34b65272015-03-11 16:56:52 -07001791 boolean useLowResIcon = container >= 0 &&
1792 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1793
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001794 if (itemReplaced) {
1795 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001796 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001797 cursorIconInfo.iconIndex, titleIndex,
1798 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001799 } else {
1800 // Don't replace items for other profiles.
1801 itemsToRemove.add(id);
1802 continue;
1803 }
1804 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001805 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001806 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001807 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001808 intent = getRestoredItemIntent(c, context, intent);
1809 } else {
1810 // Don't restore items for other profiles.
1811 itemsToRemove.add(id);
1812 continue;
1813 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001814 } else if (itemType ==
1815 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001816 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001817 cursorIconInfo.iconIndex, titleIndex,
1818 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001819 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001820 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001821
Sunny Goyald09c3702016-04-06 16:18:20 -07001822 // Shortcuts are only available on the primary profile
1823 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1824 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1825 }
1826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 // App shortcuts that used to be automatically added to Launcher
1828 // didn't always have the correct intent flags set, so do that
1829 // here
1830 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001831 intent.getCategories() != null &&
1832 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001833 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 intent.addFlags(
1835 Intent.FLAG_ACTIVITY_NEW_TASK |
1836 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1837 }
Michael Jurka96879562012-03-22 05:54:33 -07001838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001841 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001844 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 info.cellX = c.getInt(cellXIndex);
1846 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001847 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001848 info.spanX = 1;
1849 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001850 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001851 if (info.promisedIntent != null) {
1852 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1853 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001854 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001855 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1856 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1857 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001860 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001861 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 break;
1863 }
1864
Sunny Goyal756adbc2015-04-16 15:20:51 -07001865 if (restored) {
1866 ComponentName cn = info.getTargetComponent();
1867 if (cn != null) {
1868 Integer progress = installingPkgs.get(cn.getPackageName());
1869 if (progress != null) {
1870 info.setInstallProgress(progress);
1871 } else {
1872 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1873 }
1874 }
1875 }
1876
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 switch (container) {
1878 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1879 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1880 sBgWorkspaceItems.add(info);
1881 break;
1882 default:
1883 // Item is in a user folder
1884 FolderInfo folderInfo =
1885 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07001886 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887 break;
1888 }
1889 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001890 } else {
1891 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 }
1893 break;
1894
1895 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1896 id = c.getLong(idIndex);
1897 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1898
Sunny Goyala508e4f2015-05-21 09:33:57 -07001899 // Do not trim the folder label, as is was set by the user.
1900 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001902 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001903 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 folderInfo.cellX = c.getInt(cellXIndex);
1905 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001906 folderInfo.spanX = 1;
1907 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001908 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001909
Daniel Sandler8802e962010-05-26 16:28:16 -04001910 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001911 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001912 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001913 break;
1914 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001915
Joe Onorato9c1289c2009-08-17 11:03:03 -04001916 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1918 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1919 sBgWorkspaceItems.add(folderInfo);
1920 break;
Joe Onorato36115782010-06-17 13:28:48 -04001921 }
Joe Onorato17a89222011-02-08 17:26:11 -08001922
Chris Wrenf4d08112014-01-16 18:13:56 -05001923 if (restored) {
1924 // no special handling required for restored folders
1925 restoredRows.add(id);
1926 }
1927
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1929 sBgFolders.put(folderInfo.id, folderInfo);
1930 break;
1931
1932 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001933 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001934 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001935 boolean customWidget = itemType ==
1936 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1937
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001938 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001939 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001940 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001941 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001942 user = allUsers.get(serialNumber);
1943 if (user == null) {
1944 itemsToRemove.add(id);
1945 continue;
1946 }
1947
Sunny Goyalff572272014-07-23 13:58:07 -07001948 final ComponentName component =
1949 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001950
Sunny Goyal651077b2014-06-30 14:15:31 -07001951 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001952 final boolean isIdValid = (restoreStatus &
1953 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001954 final boolean wasProviderReady = (restoreStatus &
1955 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001956
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001957 if (widgetProvidersMap == null) {
1958 widgetProvidersMap = AppWidgetManagerCompat
1959 .getInstance(mContext).getAllProvidersMap();
1960 }
1961 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1962 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001963 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001964 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001965
1966 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001967 if (!isSafeMode && !customWidget &&
1968 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001969 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001970 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001971 itemsToRemove.add(id);
1972 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001973 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001974 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1975 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001976
Sunny Goyal53f96722015-07-13 19:54:53 -07001977 // The provider is available. So the widget is either
1978 // available or not available. We do not need to track
1979 // any future restore updates.
1980 int status = restoreStatus &
1981 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001982 if (!wasProviderReady) {
1983 // If provider was not previously ready, update the
1984 // status and UI flag.
1985
1986 // Id would be valid only if the widget restore broadcast was received.
1987 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001988 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001989 } else {
1990 status &= ~LauncherAppWidgetInfo
1991 .FLAG_PROVIDER_NOT_READY;
1992 }
1993 }
1994 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001995 } else {
1996 Log.v(TAG, "Widget restore pending id=" + id
1997 + " appWidgetId=" + appWidgetId
1998 + " status =" + restoreStatus);
1999 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002000 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002001 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002002 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002003
2004 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2005 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002006 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002007 // App restore has started. Update the flag
2008 appWidgetInfo.restoreStatus |=
2009 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002010 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002011 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002012 itemsToRemove.add(id);
2013 continue;
2014 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002015
2016 appWidgetInfo.installProgress =
2017 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002018 }
Sunny Goyalff572272014-07-23 13:58:07 -07002019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002021 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 appWidgetInfo.cellX = c.getInt(cellXIndex);
2023 appWidgetInfo.cellY = c.getInt(cellYIndex);
2024 appWidgetInfo.spanX = c.getInt(spanXIndex);
2025 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002026 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002027
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2029 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2030 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002031 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2032 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 continue;
2034 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002036 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002037 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002038 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002039 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 break;
2041 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002042
Adam Cohen59400422014-03-05 18:07:04 -08002043 if (!customWidget) {
2044 String providerName =
2045 appWidgetInfo.providerName.flattenToString();
2046 if (!providerName.equals(savedProvider) ||
2047 (appWidgetInfo.restoreStatus != restoreStatus)) {
2048 ContentValues values = new ContentValues();
2049 values.put(
2050 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2051 providerName);
2052 values.put(LauncherSettings.Favorites.RESTORED,
2053 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002054 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002055 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002056 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2058 sBgAppWidgets.add(appWidgetInfo);
2059 }
Joe Onorato36115782010-06-17 13:28:48 -04002060 break;
2061 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002063 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002064 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 }
2066 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002067 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 }
2069
Winson Chungba9c37f2013-08-30 14:11:37 -07002070 // Break early if we've stopped loading
2071 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002072 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002073 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002074 }
2075
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002078 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2079 Utilities.createDbSelectionQuery(
2080 LauncherSettings.Favorites._ID, itemsToRemove), null);
2081 if (DEBUG_LOADERS) {
2082 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2083 LauncherSettings.Favorites._ID, itemsToRemove));
2084 }
2085
2086 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002087 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2088 .call(contentResolver,
2089 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2090 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2091 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002092 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2093 sBgFolders.remove(folderId);
2094 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002095 }
2096 }
2097
Sunny Goyal317698b2015-07-29 11:45:41 -07002098 // Sort all the folder items and make sure the first 3 items are high resolution.
2099 for (FolderInfo folder : sBgFolders) {
2100 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2101 int pos = 0;
2102 for (ShortcutInfo info : folder.contents) {
2103 if (info.usingLowResIcon) {
2104 info.updateIcon(mIconCache, false);
2105 }
2106 pos ++;
2107 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2108 break;
2109 }
2110 }
2111 }
2112
Chris Wrenf4d08112014-01-16 18:13:56 -05002113 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002114 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002115 ContentValues values = new ContentValues();
2116 values.put(LauncherSettings.Favorites.RESTORED, 0);
2117 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2118 Utilities.createDbSelectionQuery(
2119 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002120 }
2121
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002122 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2123 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002124 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002125 null, sWorker);
2126 }
2127
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002128 // Remove any empty screens
2129 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002130 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002131 long screenId = item.screenId;
2132 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2133 unusedScreens.contains(screenId)) {
2134 unusedScreens.remove(screenId);
2135 }
2136 }
2137
2138 // If there are any empty screens remove them, and update.
2139 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002140 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002141 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 }
2143
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 if (DEBUG_LOADERS) {
2145 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2146 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002147 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002148 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002150
Sunny Goyale2df0622015-04-24 11:27:00 -07002151 for (int i = 0; i < nScreens; i++) {
2152 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002153 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 line += " | ";
2155 }
Joe Onorato36115782010-06-17 13:28:48 -04002156 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002158 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002159 }
Joe Onorato36115782010-06-17 13:28:48 -04002160 }
Adam Cohene25af792013-06-06 23:08:25 -07002161 }
2162
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002163 /**
2164 * Partially updates the item without any notification. Must be called on the worker thread.
2165 */
2166 private void updateItem(long itemId, ContentValues update) {
2167 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002168 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002169 update,
2170 BaseColumns._ID + "= ?",
2171 new String[]{Long.toString(itemId)});
2172 }
2173
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002174 /** Filters the set of items who are directly or indirectly (via another container) on the
2175 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002176 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 ArrayList<ItemInfo> allWorkspaceItems,
2178 ArrayList<ItemInfo> currentScreenItems,
2179 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002180 // Purge any null ItemInfos
2181 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2182 while (iter.hasNext()) {
2183 ItemInfo i = iter.next();
2184 if (i == null) {
2185 iter.remove();
2186 }
2187 }
2188
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 // Order the set of items by their containers first, this allows use to walk through the
2190 // list sequentially, build up a list of containers that are in the specified screen,
2191 // as well as all items in those containers.
2192 Set<Long> itemsOnScreen = new HashSet<Long>();
2193 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2194 @Override
2195 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002196 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 }
2198 });
2199 for (ItemInfo info : allWorkspaceItems) {
2200 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002201 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 currentScreenItems.add(info);
2203 itemsOnScreen.add(info.id);
2204 } else {
2205 otherScreenItems.add(info);
2206 }
2207 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2208 currentScreenItems.add(info);
2209 itemsOnScreen.add(info.id);
2210 } else {
2211 if (itemsOnScreen.contains(info.container)) {
2212 currentScreenItems.add(info);
2213 itemsOnScreen.add(info.id);
2214 } else {
2215 otherScreenItems.add(info);
2216 }
2217 }
2218 }
2219 }
2220
2221 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002222 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223 ArrayList<LauncherAppWidgetInfo> appWidgets,
2224 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2225 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226
2227 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002228 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002230 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 currentScreenWidgets.add(widget);
2232 } else {
2233 otherScreenWidgets.add(widget);
2234 }
2235 }
2236 }
2237
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2239 * right) */
2240 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002241 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002242 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002243 final int screenCols = profile.numColumns;
2244 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002246 @Override
2247 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002248 if (lhs.container == rhs.container) {
2249 // Within containers, order by their spatial position in that container
2250 switch ((int) lhs.container) {
2251 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2252 long lr = (lhs.screenId * screenCellCount +
2253 lhs.cellY * screenCols + lhs.cellX);
2254 long rr = (rhs.screenId * screenCellCount +
2255 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002256 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002257 }
2258 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2259 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002260 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002261 }
2262 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002263 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002264 throw new RuntimeException("Unexpected container type when " +
2265 "sorting workspace items.");
2266 }
2267 return 0;
2268 }
2269 } else {
2270 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002271 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002272 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002273 }
2274 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002276
Adam Cohendcd297f2013-06-18 13:13:40 -07002277 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2278 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 final Runnable r = new Runnable() {
2280 @Override
2281 public void run() {
2282 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2283 if (callbacks != null) {
2284 callbacks.bindScreens(orderedScreens);
2285 }
2286 }
2287 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002288 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 }
2290
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2292 final ArrayList<ItemInfo> workspaceItems,
2293 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002294 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295
2296 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002297 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002298 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002299 final int start = i;
2300 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 final Runnable r = new Runnable() {
2302 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002303 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002304 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002305 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002306 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2307 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002308 }
2309 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002311 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002312 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002314 // Bind the widgets, one at a time
2315 N = appWidgets.size();
2316 for (int i = 0; i < N; i++) {
2317 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2318 final Runnable r = new Runnable() {
2319 public void run() {
2320 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2321 if (callbacks != null) {
2322 callbacks.bindAppWidget(widget);
2323 }
2324 }
2325 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002326 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327 }
2328 }
2329
2330 /**
2331 * Binds all loaded data to actual views on the main thread.
2332 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002333 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 final long t = SystemClock.uptimeMillis();
2335 Runnable r;
2336
2337 // Don't use these two variables in any of the callback runnables.
2338 // Otherwise we hold a reference to them.
2339 final Callbacks oldCallbacks = mCallbacks.get();
2340 if (oldCallbacks == null) {
2341 // This launcher has exited and nobody bothered to tell us. Just bail.
2342 Log.w(TAG, "LoaderTask running with no launcher");
2343 return;
2344 }
2345
Winson Chung9b9fb962013-11-15 15:39:34 -08002346 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002347 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2348 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2349 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002350
Winson Chung2abf94d2012-07-18 18:16:38 -07002351 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 workspaceItems.addAll(sBgWorkspaceItems);
2353 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002354 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 }
2356
Derek Prothro7aff3992013-12-10 14:00:37 -05002357 final boolean isLoadingSynchronously =
2358 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002359 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002360 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002361 if (currScreen >= orderedScreenIds.size()) {
2362 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002363 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002364 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002365 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002366 final long currentScreenId = currentScreen < 0
2367 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002368
Winson Chung9b9fb962013-11-15 15:39:34 -08002369 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002370 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2371 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2372 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2373 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374
Winson Chung9b9fb962013-11-15 15:39:34 -08002375 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002377 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2380 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2381
2382 // Tell the workspace that we're about to start binding items
2383 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002384 public void run() {
2385 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2386 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002387 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002389 }
2390 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002392 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393
Adam Cohendcd297f2013-06-18 13:13:40 -07002394 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2395
Sunny Goyal527c7d32015-08-28 15:19:36 -07002396 Executor mainExecutor = new DeferredMainThreadExecutor();
2397 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002398 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399
Sunny Goyal527c7d32015-08-28 15:19:36 -07002400 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2401 // remaining screens after first onDraw is called. This ensures that the first screen
2402 // is immediately visible (eg. during rotation)
2403 // In case of !isLoadingSynchronously, bind all pages one after other.
2404 final Executor deferredExecutor = isLoadingSynchronously ?
2405 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2406
Sunny Goyal44c06432016-04-02 10:56:02 -07002407 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002408
2409 // Tell the workspace that we're done binding items
2410 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002411 public void run() {
2412 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2413 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002414 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002415 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002416
Sunny Goyal639e9062015-08-19 19:17:06 -07002417 mIsLoadingAndBindingWorkspace = false;
2418
2419 // Run all the bind complete runnables after workspace is bound.
2420 if (!mBindCompleteRunnables.isEmpty()) {
2421 synchronized (mBindCompleteRunnables) {
2422 for (final Runnable r : mBindCompleteRunnables) {
2423 runOnWorkerThread(r);
2424 }
2425 mBindCompleteRunnables.clear();
2426 }
2427 }
2428
Winson Chung98e030b2012-05-07 16:01:11 -07002429 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002430 if (DEBUG_LOADERS) {
2431 Log.d(TAG, "bound workspace in "
2432 + (SystemClock.uptimeMillis()-t) + "ms");
2433 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002434
Joe Onorato36115782010-06-17 13:28:48 -04002435 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002437 deferredExecutor.execute(r);
2438
Winson Chung4a2afa32012-07-19 14:53:05 -07002439 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002440 r = new Runnable() {
2441 public void run() {
2442 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2443 if (callbacks != null) {
2444 // We are loading synchronously, which means, some of the pages will be
2445 // bound after first draw. Inform the callbacks that page binding is
2446 // not complete, and schedule the remaining pages.
2447 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2448 callbacks.onPageBoundSynchronously(currentScreen);
2449 }
2450 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2451 }
2452 }
2453 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002454 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002455 }
Joe Onorato36115782010-06-17 13:28:48 -04002456 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002457
Joe Onorato36115782010-06-17 13:28:48 -04002458 private void loadAndBindAllApps() {
2459 if (DEBUG_LOADERS) {
2460 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2461 }
2462 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002463 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002464 synchronized (LoaderTask.this) {
2465 if (mStopped) {
2466 return;
2467 }
2468 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002469 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002470 synchronized (LoaderTask.this) {
2471 if (mStopped) {
2472 return;
2473 }
2474 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002475 }
Joe Onorato36115782010-06-17 13:28:48 -04002476 } else {
2477 onlyBindAllApps();
2478 }
2479 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002480
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002481 private void updateIconCache() {
2482 // Ignore packages which have a promise icon.
2483 HashSet<String> packagesToIgnore = new HashSet<>();
2484 synchronized (sBgLock) {
2485 for (ItemInfo info : sBgItemsIdMap) {
2486 if (info instanceof ShortcutInfo) {
2487 ShortcutInfo si = (ShortcutInfo) info;
2488 if (si.isPromise() && si.getTargetComponent() != null) {
2489 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2490 }
2491 } else if (info instanceof LauncherAppWidgetInfo) {
2492 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2493 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2494 packagesToIgnore.add(lawi.providerName.getPackageName());
2495 }
2496 }
2497 }
2498 }
2499 mIconCache.updateDbIcons(packagesToIgnore);
2500 }
2501
Joe Onorato36115782010-06-17 13:28:48 -04002502 private void onlyBindAllApps() {
2503 final Callbacks oldCallbacks = mCallbacks.get();
2504 if (oldCallbacks == null) {
2505 // This launcher has exited and nobody bothered to tell us. Just bail.
2506 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2507 return;
2508 }
2509
2510 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002511 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002512 final ArrayList<AppInfo> list
2513 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002514 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002515 public void run() {
2516 final long t = SystemClock.uptimeMillis();
2517 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2518 if (callbacks != null) {
2519 callbacks.bindAllApplications(list);
2520 }
2521 if (DEBUG_LOADERS) {
2522 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002523 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002524 }
2525 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002526 };
2527 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002528 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002529 r.run();
2530 } else {
2531 mHandler.post(r);
2532 }
Joe Onorato36115782010-06-17 13:28:48 -04002533 }
2534
Winson Chung64359a52013-07-08 17:17:08 -07002535 private void loadAllApps() {
2536 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002537
Joe Onorato36115782010-06-17 13:28:48 -04002538 final Callbacks oldCallbacks = mCallbacks.get();
2539 if (oldCallbacks == null) {
2540 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002541 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002542 return;
2543 }
2544
Kenny Guyed131872014-04-30 03:02:21 +01002545 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2546
Winson Chung64359a52013-07-08 17:17:08 -07002547 // Clear the list of apps
2548 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002549 for (UserHandleCompat user : profiles) {
2550 // Query for the set of apps
2551 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002552 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002553 if (DEBUG_LOADERS) {
2554 Log.d(TAG, "getActivityList took "
2555 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2556 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2557 }
2558 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002559 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002560 if (apps == null || apps.isEmpty()) {
2561 return;
2562 }
Kenny Guyff05f432016-01-22 17:48:29 +00002563 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002564 // Create the ApplicationInfos
2565 for (int i = 0; i < apps.size(); i++) {
2566 LauncherActivityInfoCompat app = apps.get(i);
2567 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002568 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002569 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002570
Sunny Goyal756a28a2015-04-23 17:07:55 -07002571 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2572 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002573 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002574
2575 @Override
2576 public void run() {
2577 heuristic.processUserApps(apps);
2578 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002579 };
2580 runOnMainThread(new Runnable() {
2581
2582 @Override
2583 public void run() {
2584 // Check isLoadingWorkspace on the UI thread, as it is updated on
2585 // the UI thread.
2586 if (mIsLoadingAndBindingWorkspace) {
2587 synchronized (mBindCompleteRunnables) {
2588 mBindCompleteRunnables.add(r);
2589 }
2590 } else {
2591 runOnWorkerThread(r);
2592 }
2593 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002594 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002595 }
Winson Chung64359a52013-07-08 17:17:08 -07002596 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002597 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002598 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2599 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002600
2601 // Post callback on main thread
2602 mHandler.post(new Runnable() {
2603 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002604
Winson Chung64359a52013-07-08 17:17:08 -07002605 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002606 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002607 if (callbacks != null) {
2608 callbacks.bindAllApplications(added);
2609 if (DEBUG_LOADERS) {
2610 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002611 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002612 }
2613 } else {
2614 Log.i(TAG, "not binding apps: no Launcher activity");
2615 }
2616 }
2617 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002618 // Cleanup any data stored for a deleted user.
2619 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002620 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002621 Log.d(TAG, "Icons processed in "
2622 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002623 }
2624 }
2625
2626 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002627 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002628 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2630 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2631 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2632 }
Joe Onorato36115782010-06-17 13:28:48 -04002633 }
2634 }
2635
Sunny Goyal75b0f552015-05-20 21:57:06 -07002636 /**
2637 * Called when the icons for packages have been updated in the icon cache.
2638 */
2639 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2640 final Callbacks callbacks = getCallback();
2641 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2642 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2643
2644 // If any package icon has changed (app was updated while launcher was dead),
2645 // update the corresponding shortcuts.
2646 synchronized (sBgLock) {
2647 for (ItemInfo info : sBgItemsIdMap) {
2648 if (info instanceof ShortcutInfo && user.equals(info.user)
2649 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2650 ShortcutInfo si = (ShortcutInfo) info;
2651 ComponentName cn = si.getTargetComponent();
2652 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2653 si.updateIcon(mIconCache);
2654 updatedShortcuts.add(si);
2655 }
2656 }
2657 }
2658 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2659 }
2660
2661 if (!updatedShortcuts.isEmpty()) {
2662 final UserHandleCompat userFinal = user;
2663 mHandler.post(new Runnable() {
2664
2665 public void run() {
2666 Callbacks cb = getCallback();
2667 if (cb != null && callbacks == cb) {
2668 cb.bindShortcutsChanged(updatedShortcuts,
2669 new ArrayList<ShortcutInfo>(), userFinal);
2670 }
2671 }
2672 });
2673 }
2674
2675 if (!updatedApps.isEmpty()) {
2676 mHandler.post(new Runnable() {
2677
2678 public void run() {
2679 Callbacks cb = getCallback();
2680 if (cb != null && callbacks == cb) {
2681 cb.bindAppsUpdated(updatedApps);
2682 }
2683 }
2684 });
2685 }
2686 }
2687
Joe Onorato36115782010-06-17 13:28:48 -04002688 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002689 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002690 }
2691
Adam Cohen091440a2015-03-18 14:16:05 -07002692 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002693
2694 @Override
2695 public void onReceive(Context context, Intent intent) {
2696 synchronized (sBgLock) {
2697 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2698 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002699 final PackageManager manager = context.getPackageManager();
2700 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2701 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002702 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2703 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002704 packagesRemoved.clear();
2705 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002706 for (String pkg : entry.getValue()) {
2707 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002708 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002709 packagesUnavailable.add(pkg);
2710 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002711 packagesRemoved.add(pkg);
2712 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002713 }
2714 }
2715 if (!packagesRemoved.isEmpty()) {
2716 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2717 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2718 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002719 if (!packagesUnavailable.isEmpty()) {
2720 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2721 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2722 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002723 }
Sunny Goyal34942622014-08-29 17:20:55 -07002724 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002725 }
2726 }
2727 }
2728
Joe Onorato36115782010-06-17 13:28:48 -04002729 private class PackageUpdatedTask implements Runnable {
2730 int mOp;
2731 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002732 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002733
2734 public static final int OP_NONE = 0;
2735 public static final int OP_ADD = 1;
2736 public static final int OP_UPDATE = 2;
2737 public static final int OP_REMOVE = 3; // uninstlled
2738 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002739 public static final int OP_SUSPEND = 5; // package suspended
2740 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002741 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002742
Kenny Guyed131872014-04-30 03:02:21 +01002743 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002744 mOp = op;
2745 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002746 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002747 }
2748
2749 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002750 if (!mHasLoaderCompletedOnce) {
2751 // Loader has not yet run.
2752 return;
2753 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002754 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002755
2756 final String[] packages = mPackages;
2757 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002758 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002759 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002760 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002761 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002762 for (int i=0; i<N; i++) {
2763 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002764 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002765 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002766 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002767
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002768 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2769 if (heuristic != null) {
2770 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002771 }
Joe Onorato36115782010-06-17 13:28:48 -04002772 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002773 }
Joe Onorato36115782010-06-17 13:28:48 -04002774 case OP_UPDATE:
2775 for (int i=0; i<N; i++) {
2776 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002777 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002778 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002779 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002780 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002781 // Since package was just updated, the target must be available now.
2782 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002783 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002784 case OP_REMOVE: {
2785 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2786 if (heuristic != null) {
2787 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002788 }
Joe Onorato36115782010-06-17 13:28:48 -04002789 for (int i=0; i<N; i++) {
2790 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002791 mIconCache.removeIconsForPkg(packages[i], mUser);
2792 }
2793 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002794 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002795 case OP_UNAVAILABLE:
2796 for (int i=0; i<N; i++) {
2797 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2798 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002799 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002800 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002801 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002802 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002803 case OP_SUSPEND:
2804 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002805 flagOp = mOp == OP_SUSPEND ?
2806 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2807 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002808 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002809 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2810 break;
2811 case OP_USER_AVAILABILITY_CHANGE:
2812 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2813 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2814 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2815 // We want to update all packages for this user.
2816 pkgFilter = StringFilter.matchesAll();
2817 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002818 break;
Joe Onorato36115782010-06-17 13:28:48 -04002819 }
2820
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002821 ArrayList<AppInfo> added = null;
2822 ArrayList<AppInfo> modified = null;
2823 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002824
Adam Cohen487f7dd2012-06-28 18:12:10 -07002825 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002826 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002827 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002828 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002829 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002830 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002831 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002832 }
Winson Chung5d55f332012-07-16 20:45:03 -07002833 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002834 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002835 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002836 }
2837
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002838 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002839
Joe Onorato36115782010-06-17 13:28:48 -04002840 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002841 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002842 for (AppInfo ai : added) {
2843 addedOrUpdatedApps.put(ai.componentName, ai);
2844 }
Joe Onorato36115782010-06-17 13:28:48 -04002845 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002846
Joe Onorato36115782010-06-17 13:28:48 -04002847 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002848 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002849 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002850 for (AppInfo ai : modified) {
2851 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002852 }
2853
Joe Onorato36115782010-06-17 13:28:48 -04002854 mHandler.post(new Runnable() {
2855 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002856 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002857 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002858 callbacks.bindAppsUpdated(modifiedFinal);
2859 }
2860 }
2861 });
2862 }
Winson Chung83892cc2013-05-01 16:53:33 -07002863
Sunny Goyal4390ace2014-10-13 11:33:11 -07002864 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002865 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002866 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
2867 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
2868 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
2869
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002870 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002871 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002872 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2873 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002874 boolean infoUpdated = false;
2875 boolean shortcutUpdated = false;
2876
2877 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002878 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07002879 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002880 Bitmap icon = Utilities.createIconBitmap(
2881 si.iconResource.packageName,
2882 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002883 if (icon != null) {
2884 si.setIcon(icon);
2885 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002886 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002887 }
2888 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002889
2890 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07002891 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002892 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2893
2894 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002895 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2896 // Auto install icon
2897 PackageManager pm = context.getPackageManager();
2898 ResolveInfo matched = pm.resolveActivity(
2899 new Intent(Intent.ACTION_MAIN)
2900 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2901 PackageManager.MATCH_DEFAULT_ONLY);
2902 if (matched == null) {
2903 // Try to find the best match activity.
2904 Intent intent = pm.getLaunchIntentForPackage(
2905 cn.getPackageName());
2906 if (intent != null) {
2907 cn = intent.getComponent();
2908 appInfo = addedOrUpdatedApps.get(cn);
2909 }
2910
2911 if ((intent == null) || (appInfo == null)) {
2912 removedShortcuts.add(si);
2913 continue;
2914 }
2915 si.promisedIntent = intent;
2916 }
2917 }
2918
2919 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07002920 if (appInfo != null) {
2921 si.flags = appInfo.flags;
2922 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002923
Sunny Goyal756adbc2015-04-16 15:20:51 -07002924 si.intent = si.promisedIntent;
2925 si.promisedIntent = null;
2926 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002927 infoUpdated = true;
2928 si.updateIcon(mIconCache);
2929 }
2930
2931 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2932 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2933 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002934 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002935 si.contentDescription = appInfo.contentDescription;
2936 infoUpdated = true;
2937 }
2938
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002939 int oldDisabledFlags = si.isDisabled;
2940 si.isDisabled = flagOp.apply(si.isDisabled);
2941 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002942 shortcutUpdated = true;
2943 }
2944 }
2945
2946 if (infoUpdated || shortcutUpdated) {
2947 updatedShortcuts.add(si);
2948 }
2949 if (infoUpdated) {
2950 updateItemInDatabase(context, si);
2951 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002952 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002953 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2954 if (mUser.equals(widgetInfo.user)
2955 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07002956 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002957 widgetInfo.restoreStatus &=
2958 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2959 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002960
2961 // adding this flag ensures that launcher shows 'click to setup'
2962 // if the widget has a config activity. In case there is no config
2963 // activity, it will be marked as 'restored' during bind.
2964 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2965
Sunny Goyal4390ace2014-10-13 11:33:11 -07002966 widgets.add(widgetInfo);
2967 updateItemInDatabase(context, widgetInfo);
2968 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002969 }
2970 }
2971 }
2972
Sunny Goyal4390ace2014-10-13 11:33:11 -07002973 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002974 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002975 mHandler.post(new Runnable() {
2976
2977 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002978 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002979 if (callbacks == cb && cb != null) {
2980 callbacks.bindShortcutsChanged(
2981 updatedShortcuts, removedShortcuts, mUser);
2982 }
2983 }
2984 });
2985 if (!removedShortcuts.isEmpty()) {
2986 deleteItemsFromDatabase(context, removedShortcuts);
2987 }
2988 }
2989 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002990 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002991 mHandler.post(new Runnable() {
2992 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002993 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002994 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002995 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002996 }
2997 }
2998 });
2999 }
3000 }
3001
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003002 final HashSet<String> removedPackages = new HashSet<>();
3003 final HashSet<ComponentName> removedComponents = new HashSet<>();
3004 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003005 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003006 Collections.addAll(removedPackages, packages);
3007
3008 // No need to update the removedComponents as
3009 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003010 } else if (mOp == OP_UPDATE) {
3011 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003012 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003013 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003014 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003015 }
3016 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003017
3018 // Update removedComponents as some components can get removed during package update
3019 for (AppInfo info : removedApps) {
3020 removedComponents.add(info.componentName);
3021 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003022 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003023
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003024 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3025 for (String pn : removedPackages) {
3026 deletePackageFromDatabase(context, pn, mUser);
3027 }
3028 for (ComponentName cn : removedComponents) {
3029 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003030 }
3031
Winson Chungdf95eb12013-10-16 14:57:07 -07003032 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003033 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3034
Winson Chungdf95eb12013-10-16 14:57:07 -07003035 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003036 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003037 mHandler.post(new Runnable() {
3038 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003039 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003040 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003041 callbacks.bindWorkspaceComponentsRemoved(
3042 removedPackages, removedComponents, mUser);
3043 }
3044 }
3045 });
3046 }
3047
3048 if (!removedApps.isEmpty()) {
3049 // Remove corresponding apps from All-Apps
3050 final Callbacks callbacks = getCallback();
3051 mHandler.post(new Runnable() {
3052 public void run() {
3053 Callbacks cb = getCallback();
3054 if (callbacks == cb && cb != null) {
3055 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003056 }
3057 }
3058 });
Joe Onoratobe386092009-11-17 17:32:16 -08003059 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003060
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003061 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3062 // get widget update signals.
3063 if (!Utilities.ATLEAST_MARSHMALLOW &&
3064 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003065 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003066 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003067 public void run() {
3068 Callbacks cb = getCallback();
3069 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003070 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003071 }
3072 }
3073 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003074 }
3075 }
3076 }
3077
3078 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3079 mHandler.post(new Runnable() {
3080 @Override
3081 public void run() {
3082 Callbacks cb = getCallback();
3083 if (callbacks == cb && cb != null) {
3084 callbacks.bindWidgetsModel(model);
3085 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003086 }
3087 });
3088 }
3089
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003090 public void refreshAndBindWidgetsAndShortcuts(
3091 final Callbacks callbacks, final boolean bindFirst) {
3092 runOnWorkerThread(new Runnable() {
3093 @Override
3094 public void run() {
3095 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3096 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003097 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003098 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3099 bindWidgetsModel(callbacks, model);
3100 // update the Widget entries inside DB on the worker thread.
3101 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3102 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003103 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003104 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003105 }
3106
Adam Cohen091440a2015-03-18 14:16:05 -07003107 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003108 UserHandleCompat user) {
3109 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3110 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003111 }
Adam Cohen556f6132014-01-15 15:18:08 -08003112
Kenny Guyed131872014-04-30 03:02:21 +01003113 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3114 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003115 if (cn == null) {
3116 return false;
3117 }
Kenny Guyed131872014-04-30 03:02:21 +01003118 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3119 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003120 return false;
3121 }
Kenny Guyed131872014-04-30 03:02:21 +01003122 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003123 }
3124
Adam Cohena28b78e2014-05-20 17:03:04 -07003125 public static boolean isValidPackage(Context context, String packageName,
3126 UserHandleCompat user) {
3127 if (packageName == null) {
3128 return false;
3129 }
3130 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3131 return launcherApps.isPackageEnabledForProfile(packageName, user);
3132 }
3133
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003135 * Make an ShortcutInfo object for a restored application or shortcut item that points
3136 * to a package that is not yet installed on the system.
3137 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003138 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003139 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003140 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003141 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003142
3143 Bitmap icon = iconInfo.loadIcon(c, info, context);
3144 // the fallback icon
3145 if (icon == null) {
3146 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3147 } else {
3148 info.setIcon(icon);
3149 }
Sunny Goyal34942622014-08-29 17:20:55 -07003150
3151 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003152 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003153 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003154 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003155 }
Sunny Goyal34942622014-08-29 17:20:55 -07003156 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3157 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003158 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003159 }
Sunny Goyal34942622014-08-29 17:20:55 -07003160 } else {
3161 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3162 }
3163
Winson Chung82b016c2015-05-08 17:00:10 -07003164 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003165 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003166 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003167 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003168 return info;
3169 }
3170
3171 /**
3172 * Make an Intent object for a restored application or shortcut item that points
3173 * to the market page for the item.
3174 */
Adam Cohen091440a2015-03-18 14:16:05 -07003175 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003176 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003177 return getMarketIntent(componentName.getPackageName());
3178 }
3179
3180 static Intent getMarketIntent(String packageName) {
3181 return new Intent(Intent.ACTION_VIEW)
3182 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003183 .scheme("market")
3184 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003185 .appendQueryParameter("id", packageName)
3186 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003187 }
3188
3189 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003190 * Make an ShortcutInfo object for a shortcut that is an application.
3191 *
3192 * If c is not null, then it will be used to fill in missing data like the title and icon.
3193 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003194 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003195 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003196 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003197 if (user == null) {
3198 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003199 return null;
3200 }
3201
Kenny Guyed131872014-04-30 03:02:21 +01003202 ComponentName componentName = intent.getComponent();
3203 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003204 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003205 return null;
3206 }
3207
3208 Intent newIntent = new Intent(intent.getAction(), null);
3209 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3210 newIntent.setComponent(componentName);
3211 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003212 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003213 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3214 return null;
3215 }
3216
3217 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003218 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003219 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3220 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3221 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003222 }
3223
Sunny Goyald09c3702016-04-06 16:18:20 -07003224 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3225 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3226 }
3227
Joe Onorato56d82912010-03-07 14:32:10 -05003228 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003229 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003230 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003231 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003232
Joe Onorato56d82912010-03-07 14:32:10 -05003233 // fall back to the class name of the activity
3234 if (info.title == null) {
3235 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003236 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003237
Joe Onorato9c1289c2009-08-17 11:03:03 -04003238 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003239 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003240 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003241 if (lai != null) {
3242 info.flags = AppInfo.initFlags(lai);
3243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003244 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003245 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003246
Sunny Goyale2df0622015-04-24 11:27:00 -07003247 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003248 ItemInfoFilter f) {
3249 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3250 for (ItemInfo i : infos) {
3251 if (i instanceof ShortcutInfo) {
3252 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003253 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003254 if (cn != null && f.filterItem(null, info, cn)) {
3255 filtered.add(info);
3256 }
3257 } else if (i instanceof FolderInfo) {
3258 FolderInfo info = (FolderInfo) i;
3259 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003260 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003261 if (cn != null && f.filterItem(info, s, cn)) {
3262 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003263 }
3264 }
Winson Chung64359a52013-07-08 17:17:08 -07003265 } else if (i instanceof LauncherAppWidgetInfo) {
3266 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3267 ComponentName cn = info.providerName;
3268 if (cn != null && f.filterItem(null, info, cn)) {
3269 filtered.add(info);
3270 }
Winson Chung8a435102012-08-30 17:16:53 -07003271 }
3272 }
Winson Chung64359a52013-07-08 17:17:08 -07003273 return new ArrayList<ItemInfo>(filtered);
3274 }
3275
Adam Cohen091440a2015-03-18 14:16:05 -07003276 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003277 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003278 ItemInfoFilter filter = new ItemInfoFilter() {
3279 @Override
3280 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003281 if (info.user == null) {
3282 return cn.equals(cname);
3283 } else {
3284 return cn.equals(cname) && info.user.equals(user);
3285 }
Winson Chung64359a52013-07-08 17:17:08 -07003286 }
3287 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003288 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003289 }
3290
Sunny Goyal1a745e82014-10-02 15:58:31 -07003291 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003292 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003293 */
Adam Cohen091440a2015-03-18 14:16:05 -07003294 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003295 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003296 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003297 // Non-app shortcuts are only supported for current user.
3298 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003299 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003300
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003301 // TODO: If there's an explicit component and we can't install that, delete it.
3302
Winson Chung82b016c2015-05-08 17:00:10 -07003303 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003304
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003305 Bitmap icon = iconInfo.loadIcon(c, info, context);
3306 // the fallback icon
3307 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003308 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003309 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003310 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003311 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 return info;
3313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003314
Sunny Goyal2350bc92014-10-14 16:42:54 -07003315 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003316 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3317 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3318 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3319
Adam Cohend9198822011-11-22 16:42:47 -08003320 if (intent == null) {
3321 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3322 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3323 return null;
3324 }
3325
Joe Onorato0589f0f2010-02-08 13:44:00 -08003326 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003327 boolean customIcon = false;
3328 ShortcutIconResource iconResource = null;
3329
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003330 if (bitmap instanceof Bitmap) {
3331 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003332 customIcon = true;
3333 } else {
3334 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003335 if (extra instanceof ShortcutIconResource) {
3336 iconResource = (ShortcutIconResource) extra;
3337 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003338 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003339 }
3340 }
3341
Michael Jurkac9d95c52011-08-29 14:03:34 -07003342 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003343
Kenny Guyed131872014-04-30 03:02:21 +01003344 // Only support intents for current user for now. Intents sent from other
3345 // users wouldn't get here without intent forwarding anyway.
3346 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003347 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003348 icon = mIconCache.getDefaultIcon(info.user);
3349 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003350 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003351 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003352
Winson Chung82b016c2015-05-08 17:00:10 -07003353 info.title = Utilities.trim(name);
3354 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003355 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003356 info.iconResource = iconResource;
3357
3358 return info;
3359 }
3360
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003362 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 * or make a new one.
3364 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003365 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 // See if a placeholder was created for us already
3367 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003368 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003370 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 folders.put(id, folderInfo);
3372 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003373 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003374 }
3375
Joe Onoratobe386092009-11-17 17:32:16 -08003376
Sunny Goyal651077b2014-06-30 14:15:31 -07003377 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3378 return (provider != null) && (provider.provider != null)
3379 && (provider.provider.getPackageName() != null);
3380 }
3381
Joe Onoratobe386092009-11-17 17:32:16 -08003382 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003383 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003384 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3385 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3386 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3387 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003388 if (mLoaderTask != null) {
3389 mLoaderTask.dumpState();
3390 } else {
3391 Log.d(TAG, "mLoaderTask=null");
3392 }
Joe Onoratobe386092009-11-17 17:32:16 -08003393 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003394
3395 public Callbacks getCallback() {
3396 return mCallbacks != null ? mCallbacks.get() : null;
3397 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003398
3399 /**
3400 * @return {@link FolderInfo} if its already loaded.
3401 */
3402 public FolderInfo findFolderById(Long folderId) {
3403 synchronized (sBgLock) {
3404 return sBgFolders.get(folderId);
3405 }
3406 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003407
Sunny Goyal527c7d32015-08-28 15:19:36 -07003408 @Thunk class DeferredMainThreadExecutor implements Executor {
3409
3410 @Override
3411 public void execute(Runnable command) {
3412 runOnMainThread(command);
3413 }
3414 }
3415
Sunny Goyal756adbc2015-04-16 15:20:51 -07003416 /**
3417 * @return the looper for the worker thread which can be used to start background tasks.
3418 */
3419 public static Looper getWorkerLooper() {
3420 return sWorkerThread.getLooper();
3421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003422}