blob: 4561111158e890f1cbfabba0bfaa72fa46275f9a [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;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070060import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070063import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070064import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070065import com.android.launcher3.shortcuts.DeepShortcutManager;
66import com.android.launcher3.shortcuts.ShortcutInfoCompat;
67import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000068import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070069import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070070import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070071import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070072import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070073import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070074import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070075import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070076import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070077import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070079import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080080
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.lang.ref.WeakReference;
82import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070083import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070085import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070093import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -0700106 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109
Joe Onorato36115782010-06-17 13:28:48 -0400110 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500111 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final LauncherAppState mApp;
114 @Thunk final Object mLock = new Object();
115 @Thunk DeferredHandler mHandler = new DeferredHandler();
116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700118 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 static {
122 sWorkerThread.start();
123 }
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700146 // Maps all launcher activities to the id's of their shortcuts (if they have any).
147 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
148
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149 // The lock that must be acquired before referencing any static bg data structures. Unlike
150 // other locks, this one can generally be held long-term because we never expect any of these
151 // static data structures to be referenced outside of the worker thread except on the first
152 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700153 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700157 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
160 // created by LauncherModel that are directly on the home screen (however, no widgets or
161 // shortcuts within folders).
162 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
165 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700166 new ArrayList<LauncherAppWidgetInfo>();
167
Adam Cohen487f7dd2012-06-28 18:12:10 -0700168 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700169 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700170
Adam Cohendcd297f2013-06-18 13:13:40 -0700171 // sBgWorkspaceScreens is the ordered set of workspace screens.
172 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
173
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
175 // times it is pinned.
176 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
177
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700178 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700179 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
180 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700181
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700182 // </ only access in worker thread >
183
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700184 private IconCache mIconCache;
185 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700187 private final LauncherAppsCompat mLauncherApps;
188 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700191 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700193 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700195 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
196 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700197 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800198 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700201 public void bindAppsAdded(ArrayList<Long> newScreens,
202 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700203 ArrayList<ItemInfo> addAnimated,
204 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200205 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700206 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
207 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
208 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700209 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700210 public void bindWorkspaceComponentsRemoved(
211 HashSet<String> packageNames, HashSet<ComponentName> components,
212 UserHandleCompat user);
213 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800214 public void notifyWidgetProvidersChanged();
215 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700216 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700217 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700218 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
Winson Chung64359a52013-07-08 17:17:08 -0700221 public interface ItemInfoFilter {
222 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
223 }
224
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700225 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
226 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800227 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400228 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100229 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700230 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700232 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800233
Kenny Guyed131872014-04-30 03:02:21 +0100234 mLauncherApps = LauncherAppsCompat.getInstance(context);
235 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800236 }
237
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700238 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
239 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700240 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700241 if (sWorkerThread.getThreadId() == Process.myTid()) {
242 // If we are on the worker thread, post onto the main handler
243 mHandler.post(r);
244 } else {
245 r.run();
246 }
247 }
248
249 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
250 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700251 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 if (sWorkerThread.getThreadId() == Process.myTid()) {
253 r.run();
254 } else {
255 // If we are not on the worker thread, then post to the worker handler
256 sWorker.post(r);
257 }
258 }
259
Sunny Goyal756adbc2015-04-16 15:20:51 -0700260 public void setPackageState(final PackageInstallInfo installInfo) {
261 Runnable updateRunnable = new Runnable() {
262
263 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500264 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700265 synchronized (sBgLock) {
266 final HashSet<ItemInfo> updates = new HashSet<>();
267
268 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
269 // Ignore install success events as they are handled by Package add events.
270 return;
271 }
272
Sunny Goyale2df0622015-04-24 11:27:00 -0700273 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700274 if (info instanceof ShortcutInfo) {
275 ShortcutInfo si = (ShortcutInfo) info;
276 ComponentName cn = si.getTargetComponent();
277 if (si.isPromise() && (cn != null)
278 && installInfo.packageName.equals(cn.getPackageName())) {
279 si.setInstallProgress(installInfo.progress);
280
281 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
282 // Mark this info as broken.
283 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
284 }
285 updates.add(si);
286 }
287 }
288 }
289
290 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
291 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
292 widget.installProgress = installInfo.progress;
293 updates.add(widget);
294 }
295 }
296
297 if (!updates.isEmpty()) {
298 // Push changes to the callback.
299 Runnable r = new Runnable() {
300 public void run() {
301 Callbacks callbacks = getCallback();
302 if (callbacks != null) {
303 callbacks.bindRestoreItemsChange(updates);
304 }
305 }
306 };
307 mHandler.post(r);
308 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500309 }
310 }
311 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700312 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500313 }
314
Sunny Goyal756adbc2015-04-16 15:20:51 -0700315 /**
316 * Updates the icons and label of all pending icons for the provided package name.
317 */
318 public void updateSessionDisplayInfo(final String packageName) {
319 Runnable updateRunnable = new Runnable() {
320
321 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700322 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700323 synchronized (sBgLock) {
324 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
325 final UserHandleCompat user = UserHandleCompat.myUserHandle();
326
Sunny Goyale2df0622015-04-24 11:27:00 -0700327 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 if (info instanceof ShortcutInfo) {
329 ShortcutInfo si = (ShortcutInfo) info;
330 ComponentName cn = si.getTargetComponent();
331 if (si.isPromise() && (cn != null)
332 && packageName.equals(cn.getPackageName())) {
333 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
334 // For auto install apps update the icon as well as label.
335 mIconCache.getTitleAndIcon(si,
336 si.promisedIntent, user,
337 si.shouldUseLowResIcon());
338 } else {
339 // Only update the icon for restored apps.
340 si.updateIcon(mIconCache);
341 }
342 updates.add(si);
343 }
344 }
345 }
346
347 if (!updates.isEmpty()) {
348 // Push changes to the callback.
349 Runnable r = new Runnable() {
350 public void run() {
351 Callbacks callbacks = getCallback();
352 if (callbacks != null) {
353 callbacks.bindShortcutsChanged(updates,
354 new ArrayList<ShortcutInfo>(), user);
355 }
356 }
357 };
358 mHandler.post(r);
359 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700360 }
361 }
362 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700363 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700364 }
365
Adam Cohen76a47a12014-02-05 11:47:43 -0800366 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800367 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800368
369 if (allAppsApps == null) {
370 throw new RuntimeException("allAppsApps must not be null");
371 }
372 if (allAppsApps.isEmpty()) {
373 return;
374 }
375
376 // Process the newly added applications and add them to the database first
377 Runnable r = new Runnable() {
378 public void run() {
379 runOnMainThread(new Runnable() {
380 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800381 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500383 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800384 }
385 }
386 });
387 }
388 };
389 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700390 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800391
Sunny Goyala9116722015-04-29 13:55:58 -0700392 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 int[] xy, int spanX, int spanY) {
394 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700395 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700396
397 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800398 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700399 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700400 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800401 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800402 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700403 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 }
405
406 /**
407 * Find a position on the screen for the given size or adds a new screen.
408 * @return screenId and the coordinates for the item.
409 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700410 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700411 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 ArrayList<Long> workspaceScreens,
413 ArrayList<Long> addedWorkspaceScreensFinal,
414 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700415 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800416
Sunny Goyala9116722015-04-29 13:55:58 -0700417 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700418 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700419 synchronized (sBgLock) {
420 for (ItemInfo info : sBgItemsIdMap) {
421 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
422 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
423 if (items == null) {
424 items = new ArrayList<>();
425 screenItems.put(info.screenId, items);
426 }
427 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
431
432 // Find appropriate space for the item.
433 long screenId = 0;
434 int[] cordinates = new int[2];
435 boolean found = false;
436
437 int screenCount = workspaceScreens.size();
438 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700439 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440 if (preferredScreenIndex < screenCount) {
441 screenId = workspaceScreens.get(preferredScreenIndex);
442 found = findNextAvailableIconSpaceInScreen(
443 screenItems.get(screenId), cordinates, spanX, spanY);
444 }
445
446 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700447 // Search on any of the screens starting from the first screen.
448 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 screenId = workspaceScreens.get(screen);
450 if (findNextAvailableIconSpaceInScreen(
451 screenItems.get(screenId), cordinates, spanX, spanY)) {
452 // We found a space for it
453 found = true;
454 break;
455 }
456 }
457 }
458
459 if (!found) {
460 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700461 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
462 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
463 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464
465 // Save the screen id for binding in the workspace
466 workspaceScreens.add(screenId);
467 addedWorkspaceScreensFinal.add(screenId);
468
469 // If we still can't find an empty space, then God help us all!!!
470 if (!findNextAvailableIconSpaceInScreen(
471 screenItems.get(screenId), cordinates, spanX, spanY)) {
472 throw new RuntimeException("Can't find space to add the item");
473 }
474 }
475 return Pair.create(screenId, cordinates);
476 }
477
478 /**
479 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700481 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700482 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800484 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700485 return;
Winson Chung997a9232013-07-24 15:33:46 -0700486 }
Winson Chung64359a52013-07-08 17:17:08 -0700487 // Process the newly added applications and add them to the database first
488 Runnable r = new Runnable() {
489 public void run() {
490 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
491 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
492
Winson Chung76828c82013-08-19 15:43:29 -0700493 // Get the list of workspace screens. We need to append to this list and
494 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
495 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800496 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700497 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700499 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700501 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700503 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 }
Winson Chung76828c82013-08-19 15:43:29 -0700505
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700507 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700508 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800509 long screenId = coords.first;
510 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700511
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700512 ItemInfo itemInfo;
513 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
514 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700516 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700517 } else {
518 throw new RuntimeException("Unexpected info type");
519 }
Winson Chung94d67682013-09-25 16:29:40 -0700520
Winson Chung64359a52013-07-08 17:17:08 -0700521 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700522 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700523 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700524 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700525 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700526 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700527 }
528 }
529
Winson Chung76828c82013-08-19 15:43:29 -0700530 // Update the workspace screens
531 updateWorkspaceScreenOrder(context, workspaceScreens);
532
Adam Cohen76a47a12014-02-05 11:47:43 -0800533 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700534 runOnMainThread(new Runnable() {
535 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800536 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700537 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700538 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
539 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700540 if (!addedShortcutsFinal.isEmpty()) {
541 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
542 long lastScreenId = info.screenId;
543 for (ItemInfo i : addedShortcutsFinal) {
544 if (i.screenId == lastScreenId) {
545 addAnimated.add(i);
546 } else {
547 addNotAnimated.add(i);
548 }
Winson Chung997a9232013-07-24 15:33:46 -0700549 }
550 }
Winson Chungd64d1762013-08-20 14:37:16 -0700551 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800552 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700553 }
Winson Chung64359a52013-07-08 17:17:08 -0700554 }
Winson Chung997a9232013-07-24 15:33:46 -0700555 });
556 }
Winson Chung64359a52013-07-08 17:17:08 -0700557 }
558 };
559 runOnWorkerThread(r);
560 }
561
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 /**
563 * Adds an item to the DB if it was not created previously, or move it to a new
564 * <container, screen, cellX, cellY>
565 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800566 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700567 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 if (item.container == ItemInfo.NO_ID) {
569 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700570 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 } else {
572 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700573 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575 }
576
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700577 static void checkItemInfoLocked(
578 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
579 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
580 if (modelItem != null && item != modelItem) {
581 // check all the data is consistent
582 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
583 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
584 ShortcutInfo shortcut = (ShortcutInfo) item;
585 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
586 modelShortcut.intent.filterEquals(shortcut.intent) &&
587 modelShortcut.id == shortcut.id &&
588 modelShortcut.itemType == shortcut.itemType &&
589 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700590 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700591 modelShortcut.cellX == shortcut.cellX &&
592 modelShortcut.cellY == shortcut.cellY &&
593 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700594 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700595 // For all intents and purposes, this is the same object
596 return;
597 }
598 }
599
600 // the modelItem needs to match up perfectly with item if our model is
601 // to be consistent with the database-- for now, just require
602 // modelItem == item or the equality check above
603 String msg = "item: " + ((item != null) ? item.toString() : "null") +
604 "modelItem: " +
605 ((modelItem != null) ? modelItem.toString() : "null") +
606 "Error: ItemInfo passed to checkItemInfo doesn't match original";
607 RuntimeException e = new RuntimeException(msg);
608 if (stackTrace != null) {
609 e.setStackTrace(stackTrace);
610 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800611 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700612 }
613 }
614
Michael Jurka816474f2012-06-25 14:49:02 -0700615 static void checkItemInfo(final ItemInfo item) {
616 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
617 final long itemId = item.id;
618 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700619 public void run() {
620 synchronized (sBgLock) {
621 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700622 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700623 }
624 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700626 }
627
Michael Jurkac9d95c52011-08-29 14:03:34 -0700628 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
629 final ItemInfo item, final String callingFunction) {
630 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700631 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700632 final ContentResolver cr = context.getContentResolver();
633
Adam Cohen487f7dd2012-06-28 18:12:10 -0700634 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635 Runnable r = new Runnable() {
636 public void run() {
637 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700638 updateItemArrays(item, itemId, stackTrace);
639 }
640 };
641 runOnWorkerThread(r);
642 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700644 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
645 final ArrayList<ItemInfo> items, final String callingFunction) {
646 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700647
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700648 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
649 Runnable r = new Runnable() {
650 public void run() {
651 ArrayList<ContentProviderOperation> ops =
652 new ArrayList<ContentProviderOperation>();
653 int count = items.size();
654 for (int i = 0; i < count; i++) {
655 ItemInfo item = items.get(i);
656 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700657 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700658 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700659
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
661 updateItemArrays(item, itemId, stackTrace);
662
663 }
664 try {
665 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
666 } catch (Exception e) {
667 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 }
669 }
670 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700671 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700672 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700673
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
675 // Lock on mBgLock *after* the db operation
676 synchronized (sBgLock) {
677 checkItemInfoLocked(itemId, item, stackTrace);
678
679 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
680 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
681 // Item is in a folder, make sure this folder exists
682 if (!sBgFolders.containsKey(item.container)) {
683 // An items container is being set to a that of an item which is not in
684 // the list of Folders.
685 String msg = "item: " + item + " container being set to: " +
686 item.container + ", not in the list of folders";
687 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688 }
689 }
690
691 // Items are added/removed from the corresponding FolderInfo elsewhere, such
692 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
693 // that are on the desktop, as appropriate
694 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800695 if (modelItem != null &&
696 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
697 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 switch (modelItem.itemType) {
699 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
700 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700701 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
703 if (!sBgWorkspaceItems.contains(modelItem)) {
704 sBgWorkspaceItems.add(modelItem);
705 }
706 break;
707 default:
708 break;
709 }
710 } else {
711 sBgWorkspaceItems.remove(modelItem);
712 }
713 }
714 }
715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700718 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700719 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 item.cellX = cellX;
723 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 // We store hotseat items in canonical form which is this orientation invariant position
726 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700729 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700731 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733
734 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
737 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800738 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700739 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740
Michael Jurkac9d95c52011-08-29 14:03:34 -0700741 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700742 }
743
744 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
746 * cellX, cellY have already been updated on the ItemInfos.
747 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800748 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 final long container, final int screen) {
750
751 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
752 int count = items.size();
753
754 for (int i = 0; i < count; i++) {
755 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700756 item.container = container;
757
758 // We store hotseat items in canonical form which is this orientation invariant position
759 // in the hotseat
760 if (context instanceof Launcher && screen < 0 &&
761 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700762 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700763 item.cellY);
764 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 }
767
768 final ContentValues values = new ContentValues();
769 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
770 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
771 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800772 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700774
775 contentValues.add(values);
776 }
777 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
778 }
779
780 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800782 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700785 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800786 item.cellX = cellX;
787 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700788 item.spanX = spanX;
789 item.spanY = spanY;
790
791 // We store hotseat items in canonical form which is this orientation invariant position
792 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700794 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 }
Adam Cohend4844c32011-02-18 19:25:06 -0800799
Adam Cohend4844c32011-02-18 19:25:06 -0800800 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800801 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700802 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
803 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800804 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700805 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
806 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800808
Michael Jurka816474f2012-06-25 14:49:02 -0700809 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700810 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700811
812 /**
813 * Update an item to the database in a specified container.
814 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700815 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700816 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100817 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700818 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800819 }
820
Sunny Goyal756a28a2015-04-23 17:07:55 -0700821 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700822 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700823 synchronized (mLock) {
824 if (!mHasLoaderCompletedOnce ||
825 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
826 throw new RuntimeException("Trying to add shortcut while loader is running");
827 }
828 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700829 }
830 }
831
Adam Cohend4844c32011-02-18 19:25:06 -0800832 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700833 * Returns true if the shortcuts already exists on the workspace. This must be called after
834 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700836 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
837 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700838 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700839 if (intent.getComponent() != null) {
840 // If component is not null, an intent with null package will produce
841 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700842 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700843 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700844 intentWithPkg = intent.toUri(0);
845 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700846 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700847 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
848 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700849 }
850 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700851 intentWithPkg = intent.toUri(0);
852 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700853 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700854
855 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700856 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700857 if (item instanceof ShortcutInfo) {
858 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700859 Intent targetIntent = info.promisedIntent == null
860 ? info.intent : info.promisedIntent;
861 if (targetIntent != null && info.user.equals(user)) {
862 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700863 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
864 return true;
865 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700866 }
867 }
868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700870 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700871 }
872
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 * Add an item to the database in a specified container. Sets the container, screen, cellX and
875 * cellY fields of the item. Also assigns an ID to the item.
876 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700877 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700878 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 item.cellX = cellX;
881 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700882 // We store hotseat items in canonical form which is this orientation invariant position
883 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700885 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700886 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890
891 final ContentValues values = new ContentValues();
892 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100893 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894
Sunny Goyald2497482015-09-22 18:24:19 -0700895 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
896 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
897
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700898 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700899
Jason Monk8e19cf22014-03-20 15:06:57 -0400900 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700901 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700902 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700903 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700906 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400907 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700908 sBgItemsIdMap.put(item.id, item);
909 switch (item.itemType) {
910 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
911 sBgFolders.put(item.id, (FolderInfo) item);
912 // Fall through
913 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
914 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700915 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700916 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
917 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
918 sBgWorkspaceItems.add(item);
919 } else {
920 if (!sBgFolders.containsKey(item.container)) {
921 // Adding an item to a folder that doesn't exist.
922 String msg = "adding item: " + item + " to a folder that " +
923 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700924 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700925 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700926 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700927 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
928 ShortcutInfo shortcutInfo = (ShortcutInfo) item;
929 ShortcutKey shortcutToPin = new ShortcutKey(
930 shortcutInfo.intent.getPackage(),
931 shortcutInfo.user,
932 shortcutInfo.getDeepShortcutId());
933 incrementPinnedShortcutCount(shortcutToPin, true /* shouldPin */);
934 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700935 break;
936 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
937 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
938 break;
939 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700940 }
941 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700942 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700943 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700944 }
945
Sunny Goyal34942622014-08-29 17:20:55 -0700946 private static ArrayList<ItemInfo> getItemsByPackageName(
947 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700948 ItemInfoFilter filter = new ItemInfoFilter() {
949 @Override
950 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
951 return cn.getPackageName().equals(pn) && info.user.equals(user);
952 }
953 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700954 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700955 }
956
957 /**
958 * Removes all the items from the database corresponding to the specified package.
959 */
960 static void deletePackageFromDatabase(Context context, final String pn,
961 final UserHandleCompat user) {
962 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700963 }
964
965 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700966 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700968 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700969 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
970 items.add(item);
971 deleteItemsFromDatabase(context, items);
972 }
973
974 /**
975 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700976 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700977 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700979 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700980 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700981 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700982 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700983 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700985 // Lock on mBgLock *after* the db operation
986 synchronized (sBgLock) {
987 switch (item.itemType) {
988 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
989 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700990 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700991 if (info.container == item.id) {
992 // We are deleting a folder which still contains items that
993 // think they are contained by that folder.
994 String msg = "deleting a folder (" + item + ") which still " +
995 "contains items (" + info + ")";
996 Log.e(TAG, msg);
997 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700999 sBgWorkspaceItems.remove(item);
1000 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001001 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
1002 ShortcutInfo shortcutInfo = ((ShortcutInfo) item);
1003 ShortcutKey pinnedShortcut = new ShortcutKey(
1004 shortcutInfo.intent.getPackage(),
1005 shortcutInfo.user,
1006 shortcutInfo.getDeepShortcutId());
1007 decrementPinnedShortcutCount(pinnedShortcut);
1008 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001009 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1010 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1011 sBgWorkspaceItems.remove(item);
1012 break;
1013 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1014 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1015 break;
1016 }
1017 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001019 }
1020 }
Michael Jurka83df1882011-08-31 20:59:26 -07001021 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 }
1024
1025 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001026 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1027 */
1028 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1029 synchronized (sBgLock) {
1030 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1031 if (count == null || --count.value == 0) {
1032 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1033 }
1034 }
1035 }
1036
1037 /**
1038 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1039 *
1040 * As an optimization, the caller can pass shouldPin == false to avoid
1041 * unnecessary RPC's if the shortcut is already pinned.
1042 */
1043 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1044 synchronized (sBgLock) {
1045 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1046 if (count == null) {
1047 count = new MutableInt(1);
1048 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1049 } else {
1050 count.value++;
1051 }
1052 if (shouldPin && count.value == 1) {
1053 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1054 }
1055 }
1056 }
1057
1058 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 * Update the order of the workspace screens in the database. The array list contains
1060 * a list of screen ids in the order that they should appear.
1061 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001062 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001063 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 final ContentResolver cr = context.getContentResolver();
1065 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1066
1067 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001068 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 while (iter.hasNext()) {
1070 long id = iter.next();
1071 if (id < 0) {
1072 iter.remove();
1073 }
1074 }
1075
1076 Runnable r = new Runnable() {
1077 @Override
1078 public void run() {
Yura085c8532014-02-11 15:15:29 +00001079 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001081 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001082 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 for (int i = 0; i < count; i++) {
1084 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001085 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1087 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001088 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 }
Yura085c8532014-02-11 15:15:29 +00001090
1091 try {
1092 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1093 } catch (Exception ex) {
1094 throw new RuntimeException(ex);
1095 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001096
Winson Chungba9c37f2013-08-30 14:11:37 -07001097 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001098 sBgWorkspaceScreens.clear();
1099 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001100 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 }
1102 };
1103 runOnWorkerThread(r);
1104 }
1105
1106 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001107 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 */
Winsonc0b52fe2015-09-09 16:38:15 -07001109 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 final ContentResolver cr = context.getContentResolver();
1111
Michael Jurkac9d95c52011-08-29 14:03:34 -07001112 Runnable r = new Runnable() {
1113 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001114 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001116 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 sBgItemsIdMap.remove(info.id);
1118 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 sBgWorkspaceItems.remove(info);
1120 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001122 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001123 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001125 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 for (ItemInfo childInfo : info.contents) {
1127 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001129 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001130 }
1131 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 }
1134
1135 /**
1136 * Set this as the current Launcher activity object for the loader.
1137 */
1138 public void initialize(Callbacks callbacks) {
1139 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001140 Preconditions.assertUIThread();
1141 // Remove any queued UI runnables
1142 mHandler.cancelAll();
1143 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 }
1145 }
1146
Kenny Guyed131872014-04-30 03:02:21 +01001147 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001148 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001149 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001150 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001151 user));
1152 }
1153
1154 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001155 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001156 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001157 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001158 user));
1159 }
1160
1161 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001162 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001163 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001164 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001165 user));
1166 }
1167
1168 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001169 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001170 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001171 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001172 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001173 }
1174
1175 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001176 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001177 boolean replacing) {
1178 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001179 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001180 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1181 user));
1182 }
Kenny Guyed131872014-04-30 03:02:21 +01001183 }
1184
Kenny Guy44cba692016-01-21 19:50:02 +00001185 @Override
1186 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001187 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001188 PackageUpdatedTask.OP_SUSPEND, packageNames,
1189 user));
1190 }
1191
1192 @Override
1193 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001194 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001195 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1196 user));
1197 }
1198
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001199 @Override
1200 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1201 UserHandleCompat user) {
1202 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user));
1203 }
1204
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001205 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1207 * ACTION_PACKAGE_CHANGED.
1208 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001209 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001210 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001211 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001212
Joe Onorato36115782010-06-17 13:28:48 -04001213 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001214 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001215 // If we have changed locale we need to clear out the labels in all apps/workspace.
1216 forceReload();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001217 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001218 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001219 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001220 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001221 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1222 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001223 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1224 if (user != null) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001225 enqueueItemUpdatedTask(new PackageUpdatedTask(
Sunny Goyalda891c12016-03-18 18:29:24 -07001226 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1227 new String[0], user));
1228 }
Tony Wickham827cef22016-03-17 15:39:39 -07001229 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1230 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001231 }
1232 }
1233
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001234 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001235 resetLoadedState(true, true);
1236
Reena Lee93f824a2011-09-23 17:20:28 -07001237 // Do this here because if the launcher activity is running it will be restarted.
1238 // If it's not running startLoaderFromBackground will merely tell it that it needs
1239 // to reload.
1240 startLoaderFromBackground();
1241 }
1242
Winson Chungf0c6ae02012-03-21 16:10:31 -07001243 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1244 synchronized (mLock) {
1245 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1246 // mWorkspaceLoaded to true later
1247 stopLoaderLocked();
1248 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1249 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001250 // Always reset deep shortcuts loaded.
1251 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001252 }
1253 }
1254
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001255 /**
1256 * When the launcher is in the background, it's possible for it to miss paired
1257 * configuration changes. So whenever we trigger the loader from the background
1258 * tell the launcher that it needs to re-run the loader when it comes back instead
1259 * of doing it now.
1260 */
1261 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001262 Callbacks callbacks = getCallback();
1263 if (callbacks != null) {
1264 // Only actually run the loader if they're not paused.
1265 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001266 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001267 }
1268 }
Joe Onorato36115782010-06-17 13:28:48 -04001269 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001270
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001271 /**
1272 * If there is already a loader task running, tell it to stop.
1273 */
1274 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001275 LoaderTask oldTask = mLoaderTask;
1276 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001277 oldTask.stopLocked();
1278 }
Reena Lee93f824a2011-09-23 17:20:28 -07001279 }
1280
Adam Cohen1a85c582014-09-30 09:48:49 -07001281 public boolean isCurrentCallbacks(Callbacks callbacks) {
1282 return (mCallbacks != null && mCallbacks.get() == callbacks);
1283 }
1284
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001285 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001286 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1287 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001288 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001289 // Don't bother to start the thread if we know it's not going to do anything
1290 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001291 final Callbacks oldCallbacks = mCallbacks.get();
1292 // Clear any pending bind-runnables from the synchronized load process.
1293 runOnMainThread(new Runnable() {
1294 public void run() {
1295 oldCallbacks.clearPendingBinds();
1296 }
1297 });
1298
Joe Onorato36115782010-06-17 13:28:48 -04001299 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001300 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001301 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Tony Wickham80f57872016-06-29 18:12:15 -07001302 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1303 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001304 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1305 } else {
1306 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1307 sWorker.post(mLoaderTask);
1308 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 }
1311 }
1312
Joe Onorato36115782010-06-17 13:28:48 -04001313 public void stopLoader() {
1314 synchronized (mLock) {
1315 if (mLoaderTask != null) {
1316 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 }
1318 }
Joe Onorato36115782010-06-17 13:28:48 -04001319 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001321 /**
1322 * Loads the workspace screen ids in an ordered list.
1323 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001324 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001325 final ContentResolver contentResolver = context.getContentResolver();
1326 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001327
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001328 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001329 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1330 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001331 }
1332
Joe Onorato36115782010-06-17 13:28:48 -04001333 /**
1334 * Runnable for the thread that loads the contents of the launcher:
1335 * - workspace icons
1336 * - widgets
1337 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001338 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001339 */
1340 private class LoaderTask implements Runnable {
1341 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001342 private int mPageToBindFirst;
1343
Adam Cohen091440a2015-03-18 14:16:05 -07001344 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001345 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001346 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001347
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001348 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001349 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001350 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001351 }
1352
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001353 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001354 mIsLoadingAndBindingWorkspace = true;
1355
Joe Onorato36115782010-06-17 13:28:48 -04001356 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001357 if (DEBUG_LOADERS) {
1358 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001360
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001361 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001362 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001363 synchronized (LoaderTask.this) {
1364 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001365 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001366 }
1367 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 }
1369 }
1370
Joe Onorato36115782010-06-17 13:28:48 -04001371 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001372 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001373 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001374
Joe Onorato36115782010-06-17 13:28:48 -04001375 private void waitForIdle() {
1376 // Wait until the either we're stopped or the other threads are done.
1377 // This way we don't start loading all apps until the workspace has settled
1378 // down.
1379 synchronized (LoaderTask.this) {
1380 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001381
Joe Onorato36115782010-06-17 13:28:48 -04001382 mHandler.postIdle(new Runnable() {
1383 public void run() {
1384 synchronized (LoaderTask.this) {
1385 mLoadAndBindStepFinished = true;
1386 if (DEBUG_LOADERS) {
1387 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 }
Joe Onorato36115782010-06-17 13:28:48 -04001389 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001391 }
Joe Onorato36115782010-06-17 13:28:48 -04001392 });
1393
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001394 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001395 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001396 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1397 // wait no longer than 1sec at a time
1398 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001399 } catch (InterruptedException ex) {
1400 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001401 }
1402 }
Joe Onorato36115782010-06-17 13:28:48 -04001403 if (DEBUG_LOADERS) {
1404 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001405 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001406 + "ms for previous step to finish binding");
1407 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 }
Joe Onorato36115782010-06-17 13:28:48 -04001409 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001410
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001411 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001412 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001413 // Ensure that we have a valid page index to load synchronously
1414 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1415 "valid page index");
1416 }
1417 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1418 // Ensure that we don't try and bind a specified page when the pages have not been
1419 // loaded already (we should load everything asynchronously in that case)
1420 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1421 }
1422 synchronized (mLock) {
1423 if (mIsLoaderTaskRunning) {
1424 // Ensure that we are never running the background loading at this point since
1425 // we also touch the background collections
1426 throw new RuntimeException("Error! Background loading is already running");
1427 }
1428 }
1429
1430 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1431 // data structures, we can't allow any other thread to touch that data, but because
1432 // this call is synchronous, we can get away with not locking).
1433
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001434 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001435 // operations from the previous activity. We need to ensure that all queued operations
1436 // are executed before any synchronous binding work is done.
1437 mHandler.flush();
1438
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 // Divide the set of loaded items into those that we are binding synchronously, and
1440 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001441 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1443 // arise from that.
1444 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001445
1446 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 }
1448
Joe Onorato36115782010-06-17 13:28:48 -04001449 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001451 if (mStopped) {
1452 return;
1453 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 mIsLoaderTaskRunning = true;
1455 }
Joe Onorato36115782010-06-17 13:28:48 -04001456 // Optimize for end-user experience: if the Launcher is up and // running with the
1457 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1458 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001459 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001460 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001461 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001462
Joe Onorato36115782010-06-17 13:28:48 -04001463 if (mStopped) {
1464 break keep_running;
1465 }
1466
Joe Onorato36115782010-06-17 13:28:48 -04001467 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001468
1469 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001470 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1471 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001472
1473 waitForIdle();
1474
1475 // third step
1476 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1477 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479
Joe Onorato36115782010-06-17 13:28:48 -04001480 // Clear out this reference, otherwise we end up holding it until all of the
1481 // callback runnables are done.
1482 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483
Joe Onorato36115782010-06-17 13:28:48 -04001484 synchronized (mLock) {
1485 // If we are still the last one to be scheduled, remove ourselves.
1486 if (mLoaderTask == this) {
1487 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001490 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
1493
1494 public void stopLocked() {
1495 synchronized (LoaderTask.this) {
1496 mStopped = true;
1497 this.notify();
1498 }
1499 }
1500
1501 /**
1502 * Gets the callbacks object. If we've been stopped, or if the launcher object
1503 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1504 * object that was around when the deferred message was scheduled, and if there's
1505 * a new Callbacks object around then also return null. This will save us from
1506 * calling onto it with data that will be ignored.
1507 */
1508 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1509 synchronized (mLock) {
1510 if (mStopped) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 if (mCallbacks == null) {
1515 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001516 }
Joe Onorato36115782010-06-17 13:28:48 -04001517
1518 final Callbacks callbacks = mCallbacks.get();
1519 if (callbacks != oldCallbacks) {
1520 return null;
1521 }
1522 if (callbacks == null) {
1523 Log.w(TAG, "no mCallbacks");
1524 return null;
1525 }
1526
1527 return callbacks;
1528 }
1529 }
1530
1531 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001532 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001533 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001534 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001535 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001536
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001538 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001539 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001540 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1541 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001542 Log.e(TAG, "Error loading shortcut into hotseat " + item
1543 + " into position (" + item.screenId + ":" + item.cellX + ","
1544 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001545 return false;
1546 }
1547
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001548 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001549 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1550
Adam Cohen2e6da152015-05-06 11:42:25 -07001551 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001552 Log.e(TAG, "Error loading shortcut " + item
1553 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001554 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001555 + ")");
1556 return false;
1557 }
1558
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001559 if (hotseatOccupancy != null) {
1560 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 Log.e(TAG, "Error loading shortcut into hotseat " + item
1562 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001563 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001565 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001566 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001567 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001569 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001570 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1571 occupancy.cells[(int) item.screenId][0] = true;
1572 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001573 return true;
1574 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001575 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1576 if (!workspaceScreens.contains((Long) item.screenId)) {
1577 // The item has an invalid screen id.
1578 return false;
1579 }
1580 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001581 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001582 return true;
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001585 final int countX = profile.numColumns;
1586 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001587 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1588 item.cellX < 0 || item.cellY < 0 ||
1589 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1590 Log.e(TAG, "Error loading shortcut " + item
1591 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1592 + item.cellX + "," + item.cellY
1593 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1594 return false;
1595 }
1596
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001597 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001598 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1599 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001600 // Mark the first row as occupied (if the feature is enabled)
1601 // in order to account for the QSB.
1602 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001603 }
1604 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001605 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001606 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001607
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001608 // Check if any workspace icons overlap with each other
1609 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1610 occupancy.markCells(item, true);
1611 return true;
1612 } else {
1613 Log.e(TAG, "Error loading shortcut " + item
1614 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1615 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1616 + ") already occupied");
1617 return false;
1618 }
Joe Onorato36115782010-06-17 13:28:48 -04001619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001620
Winson Chungba9c37f2013-08-30 14:11:37 -07001621 /** Clears all the sBg data structures */
1622 private void clearSBgDataStructures() {
1623 synchronized (sBgLock) {
1624 sBgWorkspaceItems.clear();
1625 sBgAppWidgets.clear();
1626 sBgFolders.clear();
1627 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001628 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001629 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001630 }
1631 }
1632
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001633 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001634 if (LauncherAppState.PROFILE_STARTUP) {
1635 Trace.beginSection("Loading Workspace");
1636 }
Joe Onorato36115782010-06-17 13:28:48 -04001637 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638
Joe Onorato36115782010-06-17 13:28:48 -04001639 final Context context = mContext;
1640 final ContentResolver contentResolver = context.getContentResolver();
1641 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001642 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001643 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001644 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001645
Winson Chung892c74d2013-08-22 16:15:50 -07001646 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001647 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001648 int countX = profile.numColumns;
1649 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001650
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001651 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001652 try {
1653 ImportDataTask.performImportIfPossible(context);
1654 } catch (Exception e) {
1655 // Migration failed. Clear workspace.
1656 clearDb = true;
1657 }
1658
1659 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001660 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1661 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001662 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001663 }
1664
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001665 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001666 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001667 LauncherSettings.Settings.call(contentResolver,
1668 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001669 }
1670
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001671 Log.d(TAG, "loadWorkspace: loading default favorites");
1672 LauncherSettings.Settings.call(contentResolver,
1673 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001674
Winson Chung2abf94d2012-07-18 18:16:38 -07001675 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001676 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001677 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001678 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001679 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001680
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001681 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1682 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001683 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001684 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001685 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001686 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001687
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001688 // +1 for the hotseat (it can be larger than the workspace)
1689 // Load workspace in reverse order to ensure that latest items are loaded first (and
1690 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001691 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001692 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001693
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 try {
1695 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1696 final int intentIndex = c.getColumnIndexOrThrow
1697 (LauncherSettings.Favorites.INTENT);
1698 final int titleIndex = c.getColumnIndexOrThrow
1699 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 final int containerIndex = c.getColumnIndexOrThrow(
1701 LauncherSettings.Favorites.CONTAINER);
1702 final int itemTypeIndex = c.getColumnIndexOrThrow(
1703 LauncherSettings.Favorites.ITEM_TYPE);
1704 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1705 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001706 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 final int screenIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.SCREEN);
1710 final int cellXIndex = c.getColumnIndexOrThrow
1711 (LauncherSettings.Favorites.CELLX);
1712 final int cellYIndex = c.getColumnIndexOrThrow
1713 (LauncherSettings.Favorites.CELLY);
1714 final int spanXIndex = c.getColumnIndexOrThrow
1715 (LauncherSettings.Favorites.SPANX);
1716 final int spanYIndex = c.getColumnIndexOrThrow(
1717 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001718 final int rankIndex = c.getColumnIndexOrThrow(
1719 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001720 final int restoredIndex = c.getColumnIndexOrThrow(
1721 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001722 final int profileIdIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001724 final int optionsIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001726 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Sunny Goyal7f834d22015-04-21 10:10:23 -07001728 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001729 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001730 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001731 long serialNo = mUserManager.getSerialNumberForUser(user);
1732 allUsers.put(serialNo, user);
1733 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001734
1735 List<ShortcutInfoCompat> pinnedShortcuts = mDeepShortcutManager
1736 .queryForPinnedShortcuts(null, user);
1737 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1738 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1739 shortcut);
1740 }
Sunny Goyal7f834d22015-04-21 10:10:23 -07001741 }
1742
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 ShortcutInfo info;
1744 String intentDescription;
1745 LauncherAppWidgetInfo appWidgetInfo;
1746 int container;
1747 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001748 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001750 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001751 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 while (!mStopped && c.moveToNext()) {
1754 try {
1755 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001756 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001757 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001758 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001760 switch (itemType) {
1761 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1762 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001763 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001764 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001765 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001766 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001767 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001768 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001769 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001770 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001771 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001772 if (user == null) {
1773 // User has been deleted remove the item.
1774 itemsToRemove.add(id);
1775 continue;
1776 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 try {
1778 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001779 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001780 if (cn != null && cn.getPackageName() != null) {
1781 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1782 cn.getPackageName(), user);
1783 boolean validComponent = validPkg &&
1784 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001785 if (validPkg) {
1786 targetPackage = cn.getPackageName();
1787 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001788
1789 if (validComponent) {
1790 if (restored) {
1791 // no special handling necessary for this item
1792 restoredRows.add(id);
1793 restored = false;
1794 }
Kenny Guyff05f432016-01-22 17:48:29 +00001795 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001796 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001797 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001798 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001799 intent = null;
1800 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1801 // We allow auto install apps to have their intent
1802 // updated after an install.
1803 intent = manager.getLaunchIntentForPackage(
1804 cn.getPackageName());
1805 if (intent != null) {
1806 ContentValues values = new ContentValues();
1807 values.put(LauncherSettings.Favorites.INTENT,
1808 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001809 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001810 }
1811 }
1812
1813 if (intent == null) {
1814 // The app is installed but the component is no
1815 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001816 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001817 itemsToRemove.add(id);
1818 continue;
1819 } else {
1820 // no special handling necessary for this item
1821 restoredRows.add(id);
1822 restored = false;
1823 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001824 } else if (restored) {
1825 // Package is not yet available but might be
1826 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001827 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001828
1829 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1830 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001831 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001832 // App restore has started. Update the flag
1833 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1834 ContentValues values = new ContentValues();
1835 values.put(LauncherSettings.Favorites.RESTORED,
1836 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001837 updateItem(id, values);
1838 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1839 // This is a common app. Try to replace this.
1840 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1841 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1842 if (parser.findDefaultApp()) {
1843 // Default app found. Replace it.
1844 intent = parser.parsedIntent;
1845 cn = intent.getComponent();
1846 ContentValues values = parser.parsedValues;
1847 values.put(LauncherSettings.Favorites.RESTORED, 0);
1848 updateItem(id, values);
1849 restored = false;
1850 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001851
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001852 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001853 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001854 itemsToRemove.add(id);
1855 continue;
1856 }
Sunny Goyal94485362014-09-18 16:13:58 -07001857 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001858 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001859 itemsToRemove.add(id);
1860 continue;
1861 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001862 } else if (PackageManagerHelper.isAppOnSdcard(
1863 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001864 // Package is present but not available.
1865 allowMissingTarget = true;
1866 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1867 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001868 // SdCard is not ready yet. Package might get available,
1869 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001870 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001871 HashSet<String> pkgs = sPendingPackages.get(user);
1872 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001873 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001874 sPendingPackages.put(user, pkgs);
1875 }
1876 pkgs.add(cn.getPackageName());
1877 allowMissingTarget = true;
1878 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001879
1880 } else {
1881 // Do not wait for external media load anymore.
1882 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001883 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001884 itemsToRemove.add(id);
1885 continue;
Winson Chungee055712013-07-30 14:46:24 -07001886 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001887 } else if (cn == null) {
1888 // For shortcuts with no component, keep them as they are
1889 restoredRows.add(id);
1890 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001891 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001893 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001894 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895 continue;
1896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897
Sunny Goyal34b65272015-03-11 16:56:52 -07001898 boolean useLowResIcon = container >= 0 &&
1899 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1900
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001901 if (itemReplaced) {
1902 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001903 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001904 cursorIconInfo.iconIndex, titleIndex,
1905 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001906 } else {
1907 // Don't replace items for other profiles.
1908 itemsToRemove.add(id);
1909 continue;
1910 }
1911 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001912 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001913 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001914 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001915 intent = getRestoredItemIntent(c, context, intent);
1916 } else {
1917 // Don't restore items for other profiles.
1918 itemsToRemove.add(id);
1919 continue;
1920 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001921 } else if (itemType ==
1922 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001923 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001924 cursorIconInfo.iconIndex, titleIndex,
1925 allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001926 } else if (itemType ==
1927 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1928 String shortcutId = intent.getStringExtra(
1929 ShortcutInfoCompat.EXTRA_SHORTCUT_ID);
1930 String packageName = intent.getPackage();
1931 ShortcutKey key = new ShortcutKey(intent.getPackage(),
1932 user, shortcutId);
1933 ShortcutInfoCompat pinnedShortcut =
1934 shortcutKeyToPinnedShortcuts.get(key);
1935 boolean shouldPin = false; // It's already pinned.
1936 if (pinnedShortcut == null) {
1937 // It shouldn't be possible for a shortcut to be on the
1938 // workspace without being pinned, but if one somehow is,
1939 // we should pin it now to get back to a good state.
1940 Log.w(TAG, "Shortcut was on workspace but wasn't pinned");
1941 // Get full details; incrementing the count will pin it.
1942 List<ShortcutInfoCompat> fullDetails = mDeepShortcutManager
1943 .queryForFullDetails(packageName,
1944 Collections.singletonList(shortcutId), user);
Tony Wickhamd82a39d2016-07-01 15:44:13 -07001945 if (fullDetails == null || fullDetails.isEmpty()) {
1946 // There are no details for the shortcut. If this is due
1947 // to a SecurityException, keep it in the database so
1948 // we can restore the icon when the launcher regains
1949 // permission. Otherwise remove the icon from the db.
1950 if (!mDeepShortcutManager.wasLastCallSuccess()) {
1951 itemsToRemove.add(id);
1952 continue;
1953 }
1954 } else {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001955 pinnedShortcut = fullDetails.get(0);
1956 shouldPin = true;
1957 }
1958 }
1959 incrementPinnedShortcutCount(key, shouldPin);
Sunny Goyal10923b32016-07-20 15:42:44 -07001960 info = new ShortcutInfo(pinnedShortcut, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001961 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001962 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001963
Sunny Goyald09c3702016-04-06 16:18:20 -07001964 // Shortcuts are only available on the primary profile
1965 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1966 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1967 }
1968
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001969 // App shortcuts that used to be automatically added to Launcher
1970 // didn't always have the correct intent flags set, so do that
1971 // here
1972 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001973 intent.getCategories() != null &&
1974 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001975 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 intent.addFlags(
1977 Intent.FLAG_ACTIVITY_NEW_TASK |
1978 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1979 }
Michael Jurka96879562012-03-22 05:54:33 -07001980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001983 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001986 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001987 info.cellX = c.getInt(cellXIndex);
1988 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001989 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001990 info.spanX = 1;
1991 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001992 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001993 if (info.promisedIntent != null) {
1994 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1995 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001996 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001997 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1998 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1999 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002002 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002003 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 break;
2005 }
2006
Sunny Goyal756adbc2015-04-16 15:20:51 -07002007 if (restored) {
2008 ComponentName cn = info.getTargetComponent();
2009 if (cn != null) {
2010 Integer progress = installingPkgs.get(cn.getPackageName());
2011 if (progress != null) {
2012 info.setInstallProgress(progress);
2013 } else {
2014 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2015 }
2016 }
2017 }
2018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 switch (container) {
2020 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2021 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2022 sBgWorkspaceItems.add(info);
2023 break;
2024 default:
2025 // Item is in a user folder
2026 FolderInfo folderInfo =
2027 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002028 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 break;
2030 }
2031 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002032 } else {
2033 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034 }
2035 break;
2036
2037 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2038 id = c.getLong(idIndex);
2039 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2040
Sunny Goyala508e4f2015-05-21 09:33:57 -07002041 // Do not trim the folder label, as is was set by the user.
2042 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002045 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 folderInfo.cellX = c.getInt(cellXIndex);
2047 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002048 folderInfo.spanX = 1;
2049 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002050 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002051
Daniel Sandler8802e962010-05-26 16:28:16 -04002052 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002053 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002054 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002055 break;
2056 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002057
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2060 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2061 sBgWorkspaceItems.add(folderInfo);
2062 break;
Joe Onorato36115782010-06-17 13:28:48 -04002063 }
Joe Onorato17a89222011-02-08 17:26:11 -08002064
Chris Wrenf4d08112014-01-16 18:13:56 -05002065 if (restored) {
2066 // no special handling required for restored folders
2067 restoredRows.add(id);
2068 }
2069
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2071 sBgFolders.put(folderInfo.id, folderInfo);
2072 break;
2073
2074 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002075 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002077 boolean customWidget = itemType ==
2078 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2079
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002081 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002082 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002083 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002084 user = allUsers.get(serialNumber);
2085 if (user == null) {
2086 itemsToRemove.add(id);
2087 continue;
2088 }
2089
Sunny Goyalff572272014-07-23 13:58:07 -07002090 final ComponentName component =
2091 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002092
Sunny Goyal651077b2014-06-30 14:15:31 -07002093 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002094 final boolean isIdValid = (restoreStatus &
2095 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002096 final boolean wasProviderReady = (restoreStatus &
2097 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002098
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002099 if (widgetProvidersMap == null) {
2100 widgetProvidersMap = AppWidgetManagerCompat
2101 .getInstance(mContext).getAllProvidersMap();
2102 }
2103 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2104 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002105 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002106 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002107
2108 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002109 if (!isSafeMode && !customWidget &&
2110 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002111 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002112 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 itemsToRemove.add(id);
2114 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002115 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002116 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2117 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002118
Sunny Goyal53f96722015-07-13 19:54:53 -07002119 // The provider is available. So the widget is either
2120 // available or not available. We do not need to track
2121 // any future restore updates.
2122 int status = restoreStatus &
2123 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002124 if (!wasProviderReady) {
2125 // If provider was not previously ready, update the
2126 // status and UI flag.
2127
2128 // Id would be valid only if the widget restore broadcast was received.
2129 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002130 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002131 } else {
2132 status &= ~LauncherAppWidgetInfo
2133 .FLAG_PROVIDER_NOT_READY;
2134 }
2135 }
2136 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002137 } else {
2138 Log.v(TAG, "Widget restore pending id=" + id
2139 + " appWidgetId=" + appWidgetId
2140 + " status =" + restoreStatus);
2141 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002142 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002143 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002144 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002145
2146 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2147 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002148 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002149 // App restore has started. Update the flag
2150 appWidgetInfo.restoreStatus |=
2151 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002152 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002153 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002154 itemsToRemove.add(id);
2155 continue;
2156 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002157
2158 appWidgetInfo.installProgress =
2159 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002160 }
Sunny Goyalff572272014-07-23 13:58:07 -07002161
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002162 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002163 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 appWidgetInfo.cellX = c.getInt(cellXIndex);
2165 appWidgetInfo.cellY = c.getInt(cellYIndex);
2166 appWidgetInfo.spanX = c.getInt(spanXIndex);
2167 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002168 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002169
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002170 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2171 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2172 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002173 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2174 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 continue;
2176 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002178 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002180 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002181 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 break;
2183 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002184
Adam Cohen59400422014-03-05 18:07:04 -08002185 if (!customWidget) {
2186 String providerName =
2187 appWidgetInfo.providerName.flattenToString();
2188 if (!providerName.equals(savedProvider) ||
2189 (appWidgetInfo.restoreStatus != restoreStatus)) {
2190 ContentValues values = new ContentValues();
2191 values.put(
2192 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2193 providerName);
2194 values.put(LauncherSettings.Favorites.RESTORED,
2195 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002196 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002197 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002198 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2200 sBgAppWidgets.add(appWidgetInfo);
2201 }
Joe Onorato36115782010-06-17 13:28:48 -04002202 break;
2203 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002205 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002206 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 }
2208 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002209 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 }
2211
Winson Chungba9c37f2013-08-30 14:11:37 -07002212 // Break early if we've stopped loading
2213 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002214 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002215 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002216 }
2217
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002220 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2221 Utilities.createDbSelectionQuery(
2222 LauncherSettings.Favorites._ID, itemsToRemove), null);
2223 if (DEBUG_LOADERS) {
2224 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2225 LauncherSettings.Favorites._ID, itemsToRemove));
2226 }
2227
2228 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002229 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2230 .call(contentResolver,
2231 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2232 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2233 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002234 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2235 sBgFolders.remove(folderId);
2236 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002237 }
2238 }
2239
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002240 // Unpin shortcuts that don't exist on the workspace.
2241 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2242 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2243 if (numTimesPinned == null || numTimesPinned.value == 0) {
2244 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2245 mDeepShortcutManager.unpinShortcut(key);
2246 }
2247 }
2248
Sunny Goyal317698b2015-07-29 11:45:41 -07002249 // Sort all the folder items and make sure the first 3 items are high resolution.
2250 for (FolderInfo folder : sBgFolders) {
2251 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2252 int pos = 0;
2253 for (ShortcutInfo info : folder.contents) {
2254 if (info.usingLowResIcon) {
2255 info.updateIcon(mIconCache, false);
2256 }
2257 pos ++;
2258 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2259 break;
2260 }
2261 }
2262 }
2263
Chris Wrenf4d08112014-01-16 18:13:56 -05002264 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002265 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002266 ContentValues values = new ContentValues();
2267 values.put(LauncherSettings.Favorites.RESTORED, 0);
2268 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2269 Utilities.createDbSelectionQuery(
2270 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002271 }
2272
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002273 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2274 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002275 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002276 null, sWorker);
2277 }
2278
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002279 // Remove any empty screens
2280 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002281 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002282 long screenId = item.screenId;
2283 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2284 unusedScreens.contains(screenId)) {
2285 unusedScreens.remove(screenId);
2286 }
2287 }
2288
2289 // If there are any empty screens remove them, and update.
2290 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002291 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 }
2294
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 if (DEBUG_LOADERS) {
2296 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2297 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002299 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002301
Sunny Goyale2df0622015-04-24 11:27:00 -07002302 for (int i = 0; i < nScreens; i++) {
2303 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002304 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305 line += " | ";
2306 }
Joe Onorato36115782010-06-17 13:28:48 -04002307 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002310 }
Joe Onorato36115782010-06-17 13:28:48 -04002311 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002312 if (LauncherAppState.PROFILE_STARTUP) {
2313 Trace.endSection();
2314 }
Adam Cohene25af792013-06-06 23:08:25 -07002315 }
2316
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002317 /**
2318 * Partially updates the item without any notification. Must be called on the worker thread.
2319 */
2320 private void updateItem(long itemId, ContentValues update) {
2321 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002322 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002323 update,
2324 BaseColumns._ID + "= ?",
2325 new String[]{Long.toString(itemId)});
2326 }
2327
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 /** Filters the set of items who are directly or indirectly (via another container) on the
2329 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002330 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 ArrayList<ItemInfo> allWorkspaceItems,
2332 ArrayList<ItemInfo> currentScreenItems,
2333 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002334 // Purge any null ItemInfos
2335 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2336 while (iter.hasNext()) {
2337 ItemInfo i = iter.next();
2338 if (i == null) {
2339 iter.remove();
2340 }
2341 }
2342
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 // Order the set of items by their containers first, this allows use to walk through the
2344 // list sequentially, build up a list of containers that are in the specified screen,
2345 // as well as all items in those containers.
2346 Set<Long> itemsOnScreen = new HashSet<Long>();
2347 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2348 @Override
2349 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002350 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 }
2352 });
2353 for (ItemInfo info : allWorkspaceItems) {
2354 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002355 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 currentScreenItems.add(info);
2357 itemsOnScreen.add(info.id);
2358 } else {
2359 otherScreenItems.add(info);
2360 }
2361 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2362 currentScreenItems.add(info);
2363 itemsOnScreen.add(info.id);
2364 } else {
2365 if (itemsOnScreen.contains(info.container)) {
2366 currentScreenItems.add(info);
2367 itemsOnScreen.add(info.id);
2368 } else {
2369 otherScreenItems.add(info);
2370 }
2371 }
2372 }
2373 }
2374
2375 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002376 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 ArrayList<LauncherAppWidgetInfo> appWidgets,
2378 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2379 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002380
2381 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002382 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002384 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 currentScreenWidgets.add(widget);
2386 } else {
2387 otherScreenWidgets.add(widget);
2388 }
2389 }
2390 }
2391
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2393 * right) */
2394 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002395 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002396 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002397 final int screenCols = profile.numColumns;
2398 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002400 @Override
2401 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002402 if (lhs.container == rhs.container) {
2403 // Within containers, order by their spatial position in that container
2404 switch ((int) lhs.container) {
2405 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2406 long lr = (lhs.screenId * screenCellCount +
2407 lhs.cellY * screenCols + lhs.cellX);
2408 long rr = (rhs.screenId * screenCellCount +
2409 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002410 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002411 }
2412 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2413 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002414 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002415 }
2416 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002417 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002418 throw new RuntimeException("Unexpected container type when " +
2419 "sorting workspace items.");
2420 }
2421 return 0;
2422 }
2423 } else {
2424 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002425 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002426 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002427 }
2428 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002429 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002430
Adam Cohendcd297f2013-06-18 13:13:40 -07002431 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2432 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002433 final Runnable r = new Runnable() {
2434 @Override
2435 public void run() {
2436 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2437 if (callbacks != null) {
2438 callbacks.bindScreens(orderedScreens);
2439 }
2440 }
2441 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002442 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002443 }
2444
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2446 final ArrayList<ItemInfo> workspaceItems,
2447 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002448 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449
2450 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002451 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002453 final int start = i;
2454 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 final Runnable r = new Runnable() {
2456 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002457 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002458 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002459 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002460 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2461 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002462 }
2463 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002465 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002466 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468 // Bind the widgets, one at a time
2469 N = appWidgets.size();
2470 for (int i = 0; i < N; i++) {
2471 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2472 final Runnable r = new Runnable() {
2473 public void run() {
2474 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2475 if (callbacks != null) {
2476 callbacks.bindAppWidget(widget);
2477 }
2478 }
2479 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002480 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 }
2482 }
2483
2484 /**
2485 * Binds all loaded data to actual views on the main thread.
2486 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002487 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 final long t = SystemClock.uptimeMillis();
2489 Runnable r;
2490
2491 // Don't use these two variables in any of the callback runnables.
2492 // Otherwise we hold a reference to them.
2493 final Callbacks oldCallbacks = mCallbacks.get();
2494 if (oldCallbacks == null) {
2495 // This launcher has exited and nobody bothered to tell us. Just bail.
2496 Log.w(TAG, "LoaderTask running with no launcher");
2497 return;
2498 }
2499
Winson Chung9b9fb962013-11-15 15:39:34 -08002500 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002501 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2502 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2503 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002504
Winson Chung2abf94d2012-07-18 18:16:38 -07002505 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506 workspaceItems.addAll(sBgWorkspaceItems);
2507 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002508 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 }
2510
Derek Prothro7aff3992013-12-10 14:00:37 -05002511 final boolean isLoadingSynchronously =
2512 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002513 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002514 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002515 if (currScreen >= orderedScreenIds.size()) {
2516 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002517 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002518 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002519 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002520 final long currentScreenId = currentScreen < 0
2521 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002522
Winson Chung9b9fb962013-11-15 15:39:34 -08002523 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002524 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2525 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2526 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2527 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528
Winson Chung9b9fb962013-11-15 15:39:34 -08002529 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002531 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002532 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2534 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2535
2536 // Tell the workspace that we're about to start binding items
2537 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002538 public void run() {
2539 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2540 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002541 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002543 }
2544 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002546 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547
Adam Cohendcd297f2013-06-18 13:13:40 -07002548 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2549
Sunny Goyal527c7d32015-08-28 15:19:36 -07002550 Executor mainExecutor = new DeferredMainThreadExecutor();
2551 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002552 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002553
Sunny Goyal527c7d32015-08-28 15:19:36 -07002554 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2555 // remaining screens after first onDraw is called. This ensures that the first screen
2556 // is immediately visible (eg. during rotation)
2557 // In case of !isLoadingSynchronously, bind all pages one after other.
2558 final Executor deferredExecutor = isLoadingSynchronously ?
2559 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2560
Sunny Goyal44c06432016-04-02 10:56:02 -07002561 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562
2563 // Tell the workspace that we're done binding items
2564 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002565 public void run() {
2566 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2567 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002568 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002569 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002570
Sunny Goyal639e9062015-08-19 19:17:06 -07002571 mIsLoadingAndBindingWorkspace = false;
2572
2573 // Run all the bind complete runnables after workspace is bound.
2574 if (!mBindCompleteRunnables.isEmpty()) {
2575 synchronized (mBindCompleteRunnables) {
2576 for (final Runnable r : mBindCompleteRunnables) {
2577 runOnWorkerThread(r);
2578 }
2579 mBindCompleteRunnables.clear();
2580 }
2581 }
2582
Winson Chung98e030b2012-05-07 16:01:11 -07002583 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002584 if (DEBUG_LOADERS) {
2585 Log.d(TAG, "bound workspace in "
2586 + (SystemClock.uptimeMillis()-t) + "ms");
2587 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002588
Joe Onorato36115782010-06-17 13:28:48 -04002589 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002590 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002591 deferredExecutor.execute(r);
2592
Winson Chung4a2afa32012-07-19 14:53:05 -07002593 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002594 r = new Runnable() {
2595 public void run() {
2596 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2597 if (callbacks != null) {
2598 // We are loading synchronously, which means, some of the pages will be
2599 // bound after first draw. Inform the callbacks that page binding is
2600 // not complete, and schedule the remaining pages.
2601 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2602 callbacks.onPageBoundSynchronously(currentScreen);
2603 }
2604 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2605 }
2606 }
2607 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002608 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002609 }
Joe Onorato36115782010-06-17 13:28:48 -04002610 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002611
Joe Onorato36115782010-06-17 13:28:48 -04002612 private void loadAndBindAllApps() {
2613 if (DEBUG_LOADERS) {
2614 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2615 }
2616 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002617 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002618 synchronized (LoaderTask.this) {
2619 if (mStopped) {
2620 return;
2621 }
2622 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002623 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002624 synchronized (LoaderTask.this) {
2625 if (mStopped) {
2626 return;
2627 }
2628 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002629 }
Joe Onorato36115782010-06-17 13:28:48 -04002630 } else {
2631 onlyBindAllApps();
2632 }
2633 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002634
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002635 private void updateIconCache() {
2636 // Ignore packages which have a promise icon.
2637 HashSet<String> packagesToIgnore = new HashSet<>();
2638 synchronized (sBgLock) {
2639 for (ItemInfo info : sBgItemsIdMap) {
2640 if (info instanceof ShortcutInfo) {
2641 ShortcutInfo si = (ShortcutInfo) info;
2642 if (si.isPromise() && si.getTargetComponent() != null) {
2643 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2644 }
2645 } else if (info instanceof LauncherAppWidgetInfo) {
2646 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2647 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2648 packagesToIgnore.add(lawi.providerName.getPackageName());
2649 }
2650 }
2651 }
2652 }
2653 mIconCache.updateDbIcons(packagesToIgnore);
2654 }
2655
Joe Onorato36115782010-06-17 13:28:48 -04002656 private void onlyBindAllApps() {
2657 final Callbacks oldCallbacks = mCallbacks.get();
2658 if (oldCallbacks == null) {
2659 // This launcher has exited and nobody bothered to tell us. Just bail.
2660 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2661 return;
2662 }
2663
2664 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002665 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002666 final ArrayList<AppInfo> list
2667 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002668 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002669 public void run() {
2670 final long t = SystemClock.uptimeMillis();
2671 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2672 if (callbacks != null) {
2673 callbacks.bindAllApplications(list);
2674 }
2675 if (DEBUG_LOADERS) {
2676 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002677 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002678 }
2679 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002680 };
Tony Wickham80f57872016-06-29 18:12:15 -07002681 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002682 }
2683
Winson Chung64359a52013-07-08 17:17:08 -07002684 private void loadAllApps() {
2685 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002686
Joe Onorato36115782010-06-17 13:28:48 -04002687 final Callbacks oldCallbacks = mCallbacks.get();
2688 if (oldCallbacks == null) {
2689 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002690 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002691 return;
2692 }
2693
Kenny Guyed131872014-04-30 03:02:21 +01002694 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2695
Winson Chung64359a52013-07-08 17:17:08 -07002696 // Clear the list of apps
2697 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002698 for (UserHandleCompat user : profiles) {
2699 // Query for the set of apps
2700 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002701 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002702 if (DEBUG_LOADERS) {
2703 Log.d(TAG, "getActivityList took "
2704 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2705 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2706 }
2707 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002708 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002709 if (apps == null || apps.isEmpty()) {
2710 return;
2711 }
Kenny Guyff05f432016-01-22 17:48:29 +00002712 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002713 // Create the ApplicationInfos
2714 for (int i = 0; i < apps.size(); i++) {
2715 LauncherActivityInfoCompat app = apps.get(i);
2716 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002717 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002718 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002719
Sunny Goyal756a28a2015-04-23 17:07:55 -07002720 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2721 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002722 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002723
2724 @Override
2725 public void run() {
2726 heuristic.processUserApps(apps);
2727 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002728 };
2729 runOnMainThread(new Runnable() {
2730
2731 @Override
2732 public void run() {
2733 // Check isLoadingWorkspace on the UI thread, as it is updated on
2734 // the UI thread.
2735 if (mIsLoadingAndBindingWorkspace) {
2736 synchronized (mBindCompleteRunnables) {
2737 mBindCompleteRunnables.add(r);
2738 }
2739 } else {
2740 runOnWorkerThread(r);
2741 }
2742 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002743 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002744 }
Winson Chung64359a52013-07-08 17:17:08 -07002745 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002746 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002747 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2748 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002749
2750 // Post callback on main thread
2751 mHandler.post(new Runnable() {
2752 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002753
Winson Chung64359a52013-07-08 17:17:08 -07002754 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002755 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002756 if (callbacks != null) {
2757 callbacks.bindAllApplications(added);
2758 if (DEBUG_LOADERS) {
2759 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002760 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002761 }
2762 } else {
2763 Log.i(TAG, "not binding apps: no Launcher activity");
2764 }
2765 }
2766 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002767 // Cleanup any data stored for a deleted user.
2768 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002769 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002770 Log.d(TAG, "Icons processed in "
2771 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002772 }
2773 }
2774
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002775 private void loadAndBindDeepShortcuts() {
2776 if (DEBUG_LOADERS) {
2777 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2778 }
2779 if (!mDeepShortcutsLoaded) {
2780 mBgDeepShortcutMap.clear();
2781 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2782 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2783 .queryForAllShortcuts(user);
Sunny Goyal860538d2016-07-20 12:35:16 -07002784 updateDeepShortcutMap(null, user, shortcuts);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002785 }
2786 synchronized (LoaderTask.this) {
2787 if (mStopped) {
2788 return;
2789 }
2790 mDeepShortcutsLoaded = true;
2791 }
2792 }
Tony Wickham80f57872016-06-29 18:12:15 -07002793 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002794 }
2795
Joe Onoratobe386092009-11-17 17:32:16 -08002796 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002797 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002798 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002799 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2800 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2801 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2802 }
Joe Onorato36115782010-06-17 13:28:48 -04002803 }
2804 }
2805
Sunny Goyal860538d2016-07-20 12:35:16 -07002806 /**
2807 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2808 */
2809 private void updateDeepShortcutMap(
2810 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002811 if (packageName != null) {
2812 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2813 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002814 ComponentKey next = keysIter.next();
2815 if (next.componentName.getPackageName().equals(packageName)
2816 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002817 keysIter.remove();
2818 }
2819 }
2820 }
2821
2822 // Now add the new shortcuts to the map.
2823 for (ShortcutInfoCompat shortcut : shortcuts) {
2824 ComponentKey targetComponent
2825 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2826 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2827 }
2828 }
2829
Tony Wickham80f57872016-06-29 18:12:15 -07002830 public void bindDeepShortcuts() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002831 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2832 shortcutMapCopy.putAll(mBgDeepShortcutMap);
Tony Wickham80f57872016-06-29 18:12:15 -07002833 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002834 @Override
2835 public void run() {
2836 Callbacks callbacks = getCallback();
2837 if (callbacks != null) {
2838 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2839 }
2840 }
Tony Wickham80f57872016-06-29 18:12:15 -07002841 };
2842 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002843 }
2844
Sunny Goyal75b0f552015-05-20 21:57:06 -07002845 /**
2846 * Called when the icons for packages have been updated in the icon cache.
2847 */
2848 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2849 final Callbacks callbacks = getCallback();
2850 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2851 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2852
2853 // If any package icon has changed (app was updated while launcher was dead),
2854 // update the corresponding shortcuts.
2855 synchronized (sBgLock) {
2856 for (ItemInfo info : sBgItemsIdMap) {
2857 if (info instanceof ShortcutInfo && user.equals(info.user)
2858 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2859 ShortcutInfo si = (ShortcutInfo) info;
2860 ComponentName cn = si.getTargetComponent();
2861 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2862 si.updateIcon(mIconCache);
2863 updatedShortcuts.add(si);
2864 }
2865 }
2866 }
2867 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2868 }
2869
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002870 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002871
2872 if (!updatedApps.isEmpty()) {
2873 mHandler.post(new Runnable() {
2874
2875 public void run() {
2876 Callbacks cb = getCallback();
2877 if (cb != null && callbacks == cb) {
2878 cb.bindAppsUpdated(updatedApps);
2879 }
2880 }
2881 });
2882 }
2883 }
2884
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002885 private void bindUpdatedShortcuts(final ArrayList<ShortcutInfo> updatedShortcuts,
2886 UserHandleCompat user) {
2887 if (!updatedShortcuts.isEmpty()) {
2888 final Callbacks callbacks = getCallback();
2889 final UserHandleCompat userFinal = user;
2890 mHandler.post(new Runnable() {
2891
2892 public void run() {
2893 Callbacks cb = getCallback();
2894 if (cb != null && callbacks == cb) {
2895 cb.bindShortcutsChanged(updatedShortcuts,
2896 new ArrayList<ShortcutInfo>(), userFinal);
2897 }
2898 }
2899 });
2900 }
2901 }
2902
2903 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002904 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002905 }
2906
Adam Cohen091440a2015-03-18 14:16:05 -07002907 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002908
2909 @Override
2910 public void onReceive(Context context, Intent intent) {
2911 synchronized (sBgLock) {
2912 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2913 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002914 final PackageManager manager = context.getPackageManager();
2915 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2916 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002917 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2918 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002919 packagesRemoved.clear();
2920 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002921 for (String pkg : entry.getValue()) {
2922 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002923 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002924 packagesUnavailable.add(pkg);
2925 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002926 packagesRemoved.add(pkg);
2927 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002928 }
2929 }
2930 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002931 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002932 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2933 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002934 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002935 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002936 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2937 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002938 }
Sunny Goyal34942622014-08-29 17:20:55 -07002939 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002940 }
2941 }
2942 }
2943
Joe Onorato36115782010-06-17 13:28:48 -04002944 private class PackageUpdatedTask implements Runnable {
2945 int mOp;
2946 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002947 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002948
2949 public static final int OP_NONE = 0;
2950 public static final int OP_ADD = 1;
2951 public static final int OP_UPDATE = 2;
2952 public static final int OP_REMOVE = 3; // uninstlled
2953 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002954 public static final int OP_SUSPEND = 5; // package suspended
2955 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002956 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002957
Kenny Guyed131872014-04-30 03:02:21 +01002958 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002959 mOp = op;
2960 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002961 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002962 }
2963
2964 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002965 if (!mHasLoaderCompletedOnce) {
2966 // Loader has not yet run.
2967 return;
2968 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002969 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002970
2971 final String[] packages = mPackages;
2972 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002973 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002974 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002975 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002976 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002977 for (int i=0; i<N; i++) {
2978 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002979 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002980 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002981 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002982
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002983 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2984 if (heuristic != null) {
2985 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002986 }
Joe Onorato36115782010-06-17 13:28:48 -04002987 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002988 }
Joe Onorato36115782010-06-17 13:28:48 -04002989 case OP_UPDATE:
2990 for (int i=0; i<N; i++) {
2991 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002992 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002993 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002994 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002995 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002996 // Since package was just updated, the target must be available now.
2997 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002998 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002999 case OP_REMOVE: {
3000 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3001 if (heuristic != null) {
3002 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003003 }
Joe Onorato36115782010-06-17 13:28:48 -04003004 for (int i=0; i<N; i++) {
3005 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003006 mIconCache.removeIconsForPkg(packages[i], mUser);
3007 }
3008 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003009 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003010 case OP_UNAVAILABLE:
3011 for (int i=0; i<N; i++) {
3012 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3013 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003014 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003015 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003016 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003017 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003018 case OP_SUSPEND:
3019 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003020 flagOp = mOp == OP_SUSPEND ?
3021 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3022 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003023 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003024 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3025 break;
3026 case OP_USER_AVAILABILITY_CHANGE:
3027 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3028 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3029 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3030 // We want to update all packages for this user.
3031 pkgFilter = StringFilter.matchesAll();
3032 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003033 break;
Joe Onorato36115782010-06-17 13:28:48 -04003034 }
3035
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003036 ArrayList<AppInfo> added = null;
3037 ArrayList<AppInfo> modified = null;
3038 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003039
Adam Cohen487f7dd2012-06-28 18:12:10 -07003040 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003041 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003042 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003043 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003044 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003045 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003046 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003047 }
Winson Chung5d55f332012-07-16 20:45:03 -07003048 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003049 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003050 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003051 }
3052
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003053 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003054
Joe Onorato36115782010-06-17 13:28:48 -04003055 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003056 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003057 for (AppInfo ai : added) {
3058 addedOrUpdatedApps.put(ai.componentName, ai);
3059 }
Joe Onorato36115782010-06-17 13:28:48 -04003060 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003061
Joe Onorato36115782010-06-17 13:28:48 -04003062 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003063 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003064 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003065 for (AppInfo ai : modified) {
3066 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003067 }
3068
Joe Onorato36115782010-06-17 13:28:48 -04003069 mHandler.post(new Runnable() {
3070 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003071 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003072 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003073 callbacks.bindAppsUpdated(modifiedFinal);
3074 }
3075 }
3076 });
3077 }
Winson Chung83892cc2013-05-01 16:53:33 -07003078
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003080 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003081 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3082 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3083 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3084
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003085 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003086 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003087 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3088 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003089 boolean infoUpdated = false;
3090 boolean shortcutUpdated = false;
3091
3092 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003093 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003094 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003095 Bitmap icon = Utilities.createIconBitmap(
3096 si.iconResource.packageName,
3097 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003098 if (icon != null) {
3099 si.setIcon(icon);
3100 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003101 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003102 }
3103 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003104
3105 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003106 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003107 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3108
3109 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3111 // Auto install icon
3112 PackageManager pm = context.getPackageManager();
3113 ResolveInfo matched = pm.resolveActivity(
3114 new Intent(Intent.ACTION_MAIN)
3115 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3116 PackageManager.MATCH_DEFAULT_ONLY);
3117 if (matched == null) {
3118 // Try to find the best match activity.
3119 Intent intent = pm.getLaunchIntentForPackage(
3120 cn.getPackageName());
3121 if (intent != null) {
3122 cn = intent.getComponent();
3123 appInfo = addedOrUpdatedApps.get(cn);
3124 }
3125
3126 if ((intent == null) || (appInfo == null)) {
3127 removedShortcuts.add(si);
3128 continue;
3129 }
3130 si.promisedIntent = intent;
3131 }
3132 }
3133
3134 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003135 if (appInfo != null) {
3136 si.flags = appInfo.flags;
3137 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003138
Sunny Goyal756adbc2015-04-16 15:20:51 -07003139 si.intent = si.promisedIntent;
3140 si.promisedIntent = null;
3141 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003142 infoUpdated = true;
3143 si.updateIcon(mIconCache);
3144 }
3145
3146 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3147 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3148 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003149 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003150 si.contentDescription = appInfo.contentDescription;
3151 infoUpdated = true;
3152 }
3153
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003154 int oldDisabledFlags = si.isDisabled;
3155 si.isDisabled = flagOp.apply(si.isDisabled);
3156 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157 shortcutUpdated = true;
3158 }
3159 }
3160
3161 if (infoUpdated || shortcutUpdated) {
3162 updatedShortcuts.add(si);
3163 }
3164 if (infoUpdated) {
3165 updateItemInDatabase(context, si);
3166 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003167 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3169 if (mUser.equals(widgetInfo.user)
3170 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003171 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003172 widgetInfo.restoreStatus &=
3173 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3174 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003175
3176 // adding this flag ensures that launcher shows 'click to setup'
3177 // if the widget has a config activity. In case there is no config
3178 // activity, it will be marked as 'restored' during bind.
3179 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3180
Sunny Goyal4390ace2014-10-13 11:33:11 -07003181 widgets.add(widgetInfo);
3182 updateItemInDatabase(context, widgetInfo);
3183 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003184 }
3185 }
3186 }
3187
Sunny Goyal4390ace2014-10-13 11:33:11 -07003188 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003189 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003190 mHandler.post(new Runnable() {
3191
3192 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003193 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003194 if (callbacks == cb && cb != null) {
3195 callbacks.bindShortcutsChanged(
3196 updatedShortcuts, removedShortcuts, mUser);
3197 }
3198 }
3199 });
3200 if (!removedShortcuts.isEmpty()) {
3201 deleteItemsFromDatabase(context, removedShortcuts);
3202 }
3203 }
3204 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003205 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003206 mHandler.post(new Runnable() {
3207 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003208 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003209 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003210 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003211 }
3212 }
3213 });
3214 }
3215 }
3216
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003217 final HashSet<String> removedPackages = new HashSet<>();
3218 final HashSet<ComponentName> removedComponents = new HashSet<>();
3219 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003220 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003221 Collections.addAll(removedPackages, packages);
3222
3223 // No need to update the removedComponents as
3224 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003225 } else if (mOp == OP_UPDATE) {
3226 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003227 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003228 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003229 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003230 }
3231 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003232
3233 // Update removedComponents as some components can get removed during package update
3234 for (AppInfo info : removedApps) {
3235 removedComponents.add(info.componentName);
3236 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003237 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003238
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003239 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3240 for (String pn : removedPackages) {
3241 deletePackageFromDatabase(context, pn, mUser);
3242 }
3243 for (ComponentName cn : removedComponents) {
3244 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003245 }
3246
Winson Chungdf95eb12013-10-16 14:57:07 -07003247 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003248 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3249
Winson Chungdf95eb12013-10-16 14:57:07 -07003250 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003251 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003252 mHandler.post(new Runnable() {
3253 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003254 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003255 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003256 callbacks.bindWorkspaceComponentsRemoved(
3257 removedPackages, removedComponents, mUser);
3258 }
3259 }
3260 });
3261 }
3262
3263 if (!removedApps.isEmpty()) {
3264 // Remove corresponding apps from All-Apps
3265 final Callbacks callbacks = getCallback();
3266 mHandler.post(new Runnable() {
3267 public void run() {
3268 Callbacks cb = getCallback();
3269 if (callbacks == cb && cb != null) {
3270 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003271 }
3272 }
3273 });
Joe Onoratobe386092009-11-17 17:32:16 -08003274 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003275
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003276 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3277 // get widget update signals.
3278 if (!Utilities.ATLEAST_MARSHMALLOW &&
3279 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003280 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003281 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003282 public void run() {
3283 Callbacks cb = getCallback();
3284 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003285 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003286 }
3287 }
3288 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003289 }
3290 }
3291 }
3292
Sunny Goyal10923b32016-07-20 15:42:44 -07003293 /**
3294 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3295 */
3296 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3297 enqueueItemUpdatedTask(new Runnable() {
3298 @Override
3299 public void run() {
3300 info.updateFromDeepShortcutInfo(
3301 fullDetail, LauncherAppState.getInstance().getContext());
3302 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3303 update.add(info);
3304 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3305 }
3306 });
3307 }
3308
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003309 private class ShortcutsChangedTask implements Runnable {
3310 private String mPackageName;
3311 private List<ShortcutInfoCompat> mShortcuts;
3312 private UserHandleCompat mUser;
3313
3314 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3315 UserHandleCompat user) {
3316 mPackageName = packageName;
3317 mShortcuts = shortcuts;
3318 mUser = user;
3319 }
3320
3321 @Override
3322 public void run() {
3323 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3324
3325 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3326 for (ShortcutInfoCompat shortcut : mShortcuts) {
3327 idsToShortcuts.put(shortcut.getId(), shortcut);
3328 }
3329
3330 // Find ShortcutInfo's that have changed on the workspace.
3331 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3332 for (ItemInfo itemInfo : sBgItemsIdMap) {
3333 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3334 ShortcutInfo si = (ShortcutInfo) itemInfo;
3335 String shortcutId = si.getDeepShortcutId();
3336 if (idsToShortcuts.containsKey(shortcutId)) {
3337 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3338 }
3339 }
3340 }
3341
3342 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3343 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3344 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3345 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3346 Context context = LauncherAppState.getInstance().getContext();
3347 for (ShortcutInfoCompat fullDetails : shortcuts) {
3348 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3349 .get(fullDetails.getId());
3350 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003351 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003352 updatedShortcutInfos.add(shortcutInfo);
3353 }
3354 }
3355 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3356
3357 // Update the deep shortcut map, in case the list of ids has changed for an activity.
Sunny Goyal860538d2016-07-20 12:35:16 -07003358 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Tony Wickham80f57872016-06-29 18:12:15 -07003359 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003360 }
3361 }
3362
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003363 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3364 mHandler.post(new Runnable() {
3365 @Override
3366 public void run() {
3367 Callbacks cb = getCallback();
3368 if (callbacks == cb && cb != null) {
3369 callbacks.bindWidgetsModel(model);
3370 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003371 }
3372 });
3373 }
3374
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003375 public void refreshAndBindWidgetsAndShortcuts(
3376 final Callbacks callbacks, final boolean bindFirst) {
3377 runOnWorkerThread(new Runnable() {
3378 @Override
3379 public void run() {
3380 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3381 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003382 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003383 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3384 bindWidgetsModel(callbacks, model);
3385 // update the Widget entries inside DB on the worker thread.
3386 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3387 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003388 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003389 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003390 }
3391
Adam Cohen091440a2015-03-18 14:16:05 -07003392 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003393 UserHandleCompat user) {
3394 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3395 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003396 }
Adam Cohen556f6132014-01-15 15:18:08 -08003397
Kenny Guyed131872014-04-30 03:02:21 +01003398 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3399 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003400 if (cn == null) {
3401 return false;
3402 }
Kenny Guyed131872014-04-30 03:02:21 +01003403 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3404 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003405 return false;
3406 }
Kenny Guyed131872014-04-30 03:02:21 +01003407 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003408 }
3409
Adam Cohena28b78e2014-05-20 17:03:04 -07003410 public static boolean isValidPackage(Context context, String packageName,
3411 UserHandleCompat user) {
3412 if (packageName == null) {
3413 return false;
3414 }
3415 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3416 return launcherApps.isPackageEnabledForProfile(packageName, user);
3417 }
3418
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003420 * Make an ShortcutInfo object for a restored application or shortcut item that points
3421 * to a package that is not yet installed on the system.
3422 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003423 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003424 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003425 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003426 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003427
3428 Bitmap icon = iconInfo.loadIcon(c, info, context);
3429 // the fallback icon
3430 if (icon == null) {
3431 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3432 } else {
3433 info.setIcon(icon);
3434 }
Sunny Goyal34942622014-08-29 17:20:55 -07003435
3436 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003437 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003438 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003439 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003440 }
Sunny Goyal34942622014-08-29 17:20:55 -07003441 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3442 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003443 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003444 }
Sunny Goyal34942622014-08-29 17:20:55 -07003445 } else {
3446 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3447 }
3448
Winson Chung82b016c2015-05-08 17:00:10 -07003449 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003450 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003451 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003452 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003453 return info;
3454 }
3455
3456 /**
3457 * Make an Intent object for a restored application or shortcut item that points
3458 * to the market page for the item.
3459 */
Adam Cohen091440a2015-03-18 14:16:05 -07003460 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003461 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003462 return getMarketIntent(componentName.getPackageName());
3463 }
3464
3465 static Intent getMarketIntent(String packageName) {
3466 return new Intent(Intent.ACTION_VIEW)
3467 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003468 .scheme("market")
3469 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003470 .appendQueryParameter("id", packageName)
3471 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003472 }
3473
3474 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003475 * Make an ShortcutInfo object for a shortcut that is an application.
3476 *
3477 * If c is not null, then it will be used to fill in missing data like the title and icon.
3478 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003479 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003480 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003481 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003482 if (user == null) {
3483 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003484 return null;
3485 }
3486
Kenny Guyed131872014-04-30 03:02:21 +01003487 ComponentName componentName = intent.getComponent();
3488 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003489 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003490 return null;
3491 }
3492
3493 Intent newIntent = new Intent(intent.getAction(), null);
3494 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3495 newIntent.setComponent(componentName);
3496 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003497 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003498 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3499 return null;
3500 }
3501
3502 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003503 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003504 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3505 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3506 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003507 }
3508
Sunny Goyald09c3702016-04-06 16:18:20 -07003509 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3510 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3511 }
3512
Joe Onorato56d82912010-03-07 14:32:10 -05003513 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003514 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003515 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003516 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003517
Joe Onorato56d82912010-03-07 14:32:10 -05003518 // fall back to the class name of the activity
3519 if (info.title == null) {
3520 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003521 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003522
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003524 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003525 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003526 if (lai != null) {
3527 info.flags = AppInfo.initFlags(lai);
3528 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003530 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003531
Sunny Goyale2df0622015-04-24 11:27:00 -07003532 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003533 ItemInfoFilter f) {
3534 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3535 for (ItemInfo i : infos) {
3536 if (i instanceof ShortcutInfo) {
3537 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003538 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003539 if (cn != null && f.filterItem(null, info, cn)) {
3540 filtered.add(info);
3541 }
3542 } else if (i instanceof FolderInfo) {
3543 FolderInfo info = (FolderInfo) i;
3544 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003545 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003546 if (cn != null && f.filterItem(info, s, cn)) {
3547 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003548 }
3549 }
Winson Chung64359a52013-07-08 17:17:08 -07003550 } else if (i instanceof LauncherAppWidgetInfo) {
3551 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3552 ComponentName cn = info.providerName;
3553 if (cn != null && f.filterItem(null, info, cn)) {
3554 filtered.add(info);
3555 }
Winson Chung8a435102012-08-30 17:16:53 -07003556 }
3557 }
Winson Chung64359a52013-07-08 17:17:08 -07003558 return new ArrayList<ItemInfo>(filtered);
3559 }
3560
Adam Cohen091440a2015-03-18 14:16:05 -07003561 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003562 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003563 ItemInfoFilter filter = new ItemInfoFilter() {
3564 @Override
3565 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003566 if (info.user == null) {
3567 return cn.equals(cname);
3568 } else {
3569 return cn.equals(cname) && info.user.equals(user);
3570 }
Winson Chung64359a52013-07-08 17:17:08 -07003571 }
3572 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003573 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003574 }
3575
Sunny Goyal1a745e82014-10-02 15:58:31 -07003576 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003577 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003578 */
Adam Cohen091440a2015-03-18 14:16:05 -07003579 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003580 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003581 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003582 // Non-app shortcuts are only supported for current user.
3583 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003585
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003586 // TODO: If there's an explicit component and we can't install that, delete it.
3587
Winson Chung82b016c2015-05-08 17:00:10 -07003588 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003589
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003590 Bitmap icon = iconInfo.loadIcon(c, info, context);
3591 // the fallback icon
3592 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003593 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003594 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003595 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003596 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 return info;
3598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599
Sunny Goyal2350bc92014-10-14 16:42:54 -07003600 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003601 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3602 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3603 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3604
Adam Cohend9198822011-11-22 16:42:47 -08003605 if (intent == null) {
3606 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3607 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3608 return null;
3609 }
3610
Joe Onorato0589f0f2010-02-08 13:44:00 -08003611 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003612 boolean customIcon = false;
3613 ShortcutIconResource iconResource = null;
3614
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003615 if (bitmap instanceof Bitmap) {
3616 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003617 customIcon = true;
3618 } else {
3619 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003620 if (extra instanceof ShortcutIconResource) {
3621 iconResource = (ShortcutIconResource) extra;
3622 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003623 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003624 }
3625 }
3626
Michael Jurkac9d95c52011-08-29 14:03:34 -07003627 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003628
Kenny Guyed131872014-04-30 03:02:21 +01003629 // Only support intents for current user for now. Intents sent from other
3630 // users wouldn't get here without intent forwarding anyway.
3631 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003632 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003633 icon = mIconCache.getDefaultIcon(info.user);
3634 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003635 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003636 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003637
Winson Chung82b016c2015-05-08 17:00:10 -07003638 info.title = Utilities.trim(name);
3639 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003640 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003641 info.iconResource = iconResource;
3642
3643 return info;
3644 }
3645
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003647 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 * or make a new one.
3649 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003650 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 // See if a placeholder was created for us already
3652 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003653 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003655 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 folders.put(id, folderInfo);
3657 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003658 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003659 }
3660
Joe Onoratobe386092009-11-17 17:32:16 -08003661
Sunny Goyal651077b2014-06-30 14:15:31 -07003662 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3663 return (provider != null) && (provider.provider != null)
3664 && (provider.provider.getPackageName() != null);
3665 }
3666
Joe Onoratobe386092009-11-17 17:32:16 -08003667 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003668 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003669 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3670 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3671 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3672 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003673 if (mLoaderTask != null) {
3674 mLoaderTask.dumpState();
3675 } else {
3676 Log.d(TAG, "mLoaderTask=null");
3677 }
Joe Onoratobe386092009-11-17 17:32:16 -08003678 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003679
3680 public Callbacks getCallback() {
3681 return mCallbacks != null ? mCallbacks.get() : null;
3682 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003683
3684 /**
3685 * @return {@link FolderInfo} if its already loaded.
3686 */
3687 public FolderInfo findFolderById(Long folderId) {
3688 synchronized (sBgLock) {
3689 return sBgFolders.get(folderId);
3690 }
3691 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003692
Sunny Goyal527c7d32015-08-28 15:19:36 -07003693 @Thunk class DeferredMainThreadExecutor implements Executor {
3694
3695 @Override
3696 public void execute(Runnable command) {
3697 runOnMainThread(command);
3698 }
3699 }
3700
Sunny Goyal756adbc2015-04-16 15:20:51 -07003701 /**
3702 * @return the looper for the worker thread which can be used to start background tasks.
3703 */
3704 public static Looper getWorkerLooper() {
3705 return sWorkerThread.getLooper();
3706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003707}