blob: 5e57aa42a962544be1dd705d0af484e6450f5055 [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 Goyal10629b02016-09-01 12:50:11 -070060import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070061import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080062import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalc2936bc2016-09-01 15:50:36 -070063import com.android.launcher3.model.SdCardAvailableReceiver;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070064import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070065import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070066import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070067import com.android.launcher3.shortcuts.DeepShortcutManager;
68import com.android.launcher3.shortcuts.ShortcutInfoCompat;
69import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000070import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070071import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070072import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070073import com.android.launcher3.util.GridOccupancy;
Sunny Goyal40452cf2016-09-01 15:17:46 -070074import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyale2df0622015-04-24 11:27:00 -070075import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070076import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070077import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070078import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070079import com.android.launcher3.util.Preconditions;
Adam Cohen091440a2015-03-18 14:16:05 -070080import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070081import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080082
Michael Jurkac2f801e2011-07-12 14:19:46 -070083import java.lang.ref.WeakReference;
84import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070085import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070087import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070088import java.util.Collections;
89import java.util.Comparator;
90import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070091import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070092import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070093import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070094import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070095import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070096import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070097import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Maintains in-memory state of the Launcher. It is expected that there should be only one
101 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700102 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 */
Kenny Guyed131872014-04-30 03:02:21 +0100104public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100105 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400107 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700110
Joe Onorato36115782010-06-17 13:28:48 -0400111 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500112 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk final LauncherAppState mApp;
115 @Thunk final Object mLock = new Object();
116 @Thunk DeferredHandler mHandler = new DeferredHandler();
117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700119 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122 static {
123 sWorkerThread.start();
124 }
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 // We start off with everything not loaded. After that, we assume that
128 // our monitoring of the package manager provides all updates and we never
129 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700130 private boolean mWorkspaceLoaded;
131 private boolean mAllAppsLoaded;
132 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700133
Sunny Goyal756a28a2015-04-23 17:07:55 -0700134 /**
135 * Set of runnables to be called on the background thread after the workspace binding
136 * is complete.
137 */
138 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
139
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700144 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800145 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700147 // Maps all launcher activities to the id's of their shortcuts (if they have any).
148 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
149
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700150 private boolean mHasShortcutHostPermission;
151 // Runnable to check if the shortcuts permission has changed.
152 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
153 @Override
154 public void run() {
155 if (mDeepShortcutsLoaded) {
156 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
157 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
158 mApp.reloadWorkspace();
159 }
160 }
161 }
162 };
163
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700164 // The lock that must be acquired before referencing any static bg data structures. Unlike
165 // other locks, this one can generally be held long-term because we never expect any of these
166 // static data structures to be referenced outside of the worker thread except on the first
167 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700168 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700169
Adam Cohen487f7dd2012-06-28 18:12:10 -0700170 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700172 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700173
Adam Cohen487f7dd2012-06-28 18:12:10 -0700174 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
175 // created by LauncherModel that are directly on the home screen (however, no widgets or
176 // shortcuts within folders).
177 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700178
Adam Cohen487f7dd2012-06-28 18:12:10 -0700179 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
180 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700181 new ArrayList<LauncherAppWidgetInfo>();
182
Adam Cohen487f7dd2012-06-28 18:12:10 -0700183 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700184 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700185
Adam Cohendcd297f2013-06-18 13:13:40 -0700186 // sBgWorkspaceScreens is the ordered set of workspace screens.
187 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
188
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700189 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
190 // times it is pinned.
191 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
192
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700193 // </ only access in worker thread >
194
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700195 private IconCache mIconCache;
196 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700198 private final LauncherAppsCompat mLauncherApps;
199 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700202 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700204 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700206 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
207 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700208 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700209 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800210 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200212 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700213 public void bindAppsAdded(ArrayList<Long> newScreens,
214 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700215 ArrayList<ItemInfo> addAnimated,
216 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200217 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700218 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
219 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
220 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700221 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700222 public void bindWorkspaceComponentsRemoved(
223 HashSet<String> packageNames, HashSet<ComponentName> components,
224 UserHandleCompat user);
225 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800226 public void notifyWidgetProvidersChanged();
227 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700228 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700229 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700230 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700233 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
234 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800235 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400236 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100237 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700238 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800239 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700240 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241
Kenny Guyed131872014-04-30 03:02:21 +0100242 mLauncherApps = LauncherAppsCompat.getInstance(context);
243 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 }
245
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700246 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
247 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700248 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700249 if (sWorkerThread.getThreadId() == Process.myTid()) {
250 // If we are on the worker thread, post onto the main handler
251 mHandler.post(r);
252 } else {
253 r.run();
254 }
255 }
256
257 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
258 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700259 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700260 if (sWorkerThread.getThreadId() == Process.myTid()) {
261 r.run();
262 } else {
263 // If we are not on the worker thread, then post to the worker handler
264 sWorker.post(r);
265 }
266 }
267
Sunny Goyal756adbc2015-04-16 15:20:51 -0700268 public void setPackageState(final PackageInstallInfo installInfo) {
269 Runnable updateRunnable = new Runnable() {
270
271 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500272 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700273 synchronized (sBgLock) {
274 final HashSet<ItemInfo> updates = new HashSet<>();
275
276 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
277 // Ignore install success events as they are handled by Package add events.
278 return;
279 }
280
Sunny Goyale2df0622015-04-24 11:27:00 -0700281 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700282 if (info instanceof ShortcutInfo) {
283 ShortcutInfo si = (ShortcutInfo) info;
284 ComponentName cn = si.getTargetComponent();
285 if (si.isPromise() && (cn != null)
286 && installInfo.packageName.equals(cn.getPackageName())) {
287 si.setInstallProgress(installInfo.progress);
288
289 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
290 // Mark this info as broken.
291 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
292 }
293 updates.add(si);
294 }
295 }
296 }
297
298 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
299 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
300 widget.installProgress = installInfo.progress;
301 updates.add(widget);
302 }
303 }
304
305 if (!updates.isEmpty()) {
306 // Push changes to the callback.
307 Runnable r = new Runnable() {
308 public void run() {
309 Callbacks callbacks = getCallback();
310 if (callbacks != null) {
311 callbacks.bindRestoreItemsChange(updates);
312 }
313 }
314 };
315 mHandler.post(r);
316 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500317 }
318 }
319 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700320 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500321 }
322
Sunny Goyal756adbc2015-04-16 15:20:51 -0700323 /**
324 * Updates the icons and label of all pending icons for the provided package name.
325 */
326 public void updateSessionDisplayInfo(final String packageName) {
327 Runnable updateRunnable = new Runnable() {
328
329 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700330 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700331 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700332 ArrayList<ShortcutInfo> updates = new ArrayList<>();
333 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700334
Sunny Goyale2df0622015-04-24 11:27:00 -0700335 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700336 if (info instanceof ShortcutInfo) {
337 ShortcutInfo si = (ShortcutInfo) info;
338 ComponentName cn = si.getTargetComponent();
339 if (si.isPromise() && (cn != null)
340 && packageName.equals(cn.getPackageName())) {
341 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
342 // For auto install apps update the icon as well as label.
343 mIconCache.getTitleAndIcon(si,
344 si.promisedIntent, user,
345 si.shouldUseLowResIcon());
346 } else {
347 // Only update the icon for restored apps.
348 si.updateIcon(mIconCache);
349 }
350 updates.add(si);
351 }
352 }
353 }
354
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700355 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700356 }
357 }
358 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700359 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700360 }
361
Adam Cohen76a47a12014-02-05 11:47:43 -0800362 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800363 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800364
365 if (allAppsApps == null) {
366 throw new RuntimeException("allAppsApps must not be null");
367 }
368 if (allAppsApps.isEmpty()) {
369 return;
370 }
371
372 // Process the newly added applications and add them to the database first
373 Runnable r = new Runnable() {
374 public void run() {
375 runOnMainThread(new Runnable() {
376 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800377 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800378 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500379 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800380 }
381 }
382 });
383 }
384 };
385 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700386 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800387
Sunny Goyala9116722015-04-29 13:55:58 -0700388 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800389 int[] xy, int spanX, int spanY) {
390 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700391 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700392
393 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800394 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700395 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700396 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800397 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800398 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700399 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800400 }
401
402 /**
403 * Find a position on the screen for the given size or adds a new screen.
404 * @return screenId and the coordinates for the item.
405 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700406 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700407 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800408 ArrayList<Long> workspaceScreens,
409 ArrayList<Long> addedWorkspaceScreensFinal,
410 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700411 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412
Sunny Goyala9116722015-04-29 13:55:58 -0700413 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700414 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700415 synchronized (sBgLock) {
416 for (ItemInfo info : sBgItemsIdMap) {
417 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
418 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
419 if (items == null) {
420 items = new ArrayList<>();
421 screenItems.put(info.screenId, items);
422 }
423 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800424 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800425 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426 }
427
428 // Find appropriate space for the item.
429 long screenId = 0;
430 int[] cordinates = new int[2];
431 boolean found = false;
432
433 int screenCount = workspaceScreens.size();
434 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700435 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800436 if (preferredScreenIndex < screenCount) {
437 screenId = workspaceScreens.get(preferredScreenIndex);
438 found = findNextAvailableIconSpaceInScreen(
439 screenItems.get(screenId), cordinates, spanX, spanY);
440 }
441
442 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700443 // Search on any of the screens starting from the first screen.
444 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 screenId = workspaceScreens.get(screen);
446 if (findNextAvailableIconSpaceInScreen(
447 screenItems.get(screenId), cordinates, spanX, spanY)) {
448 // We found a space for it
449 found = true;
450 break;
451 }
452 }
453 }
454
455 if (!found) {
456 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700457 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
458 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
459 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800460
461 // Save the screen id for binding in the workspace
462 workspaceScreens.add(screenId);
463 addedWorkspaceScreensFinal.add(screenId);
464
465 // If we still can't find an empty space, then God help us all!!!
466 if (!findNextAvailableIconSpaceInScreen(
467 screenItems.get(screenId), cordinates, spanX, spanY)) {
468 throw new RuntimeException("Can't find space to add the item");
469 }
470 }
471 return Pair.create(screenId, cordinates);
472 }
473
474 /**
475 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800476 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700477 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700478 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800479 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800480 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700481 return;
Winson Chung997a9232013-07-24 15:33:46 -0700482 }
Winson Chung64359a52013-07-08 17:17:08 -0700483 // Process the newly added applications and add them to the database first
484 Runnable r = new Runnable() {
485 public void run() {
486 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
487 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
488
Winson Chung76828c82013-08-19 15:43:29 -0700489 // Get the list of workspace screens. We need to append to this list and
490 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
491 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800492 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700493 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800494 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700495 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800496 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700497 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700499 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 }
Winson Chung76828c82013-08-19 15:43:29 -0700501
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700503 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700504 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 long screenId = coords.first;
506 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700507
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700508 ItemInfo itemInfo;
509 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
510 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800511 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700512 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700513 } else {
514 throw new RuntimeException("Unexpected info type");
515 }
Winson Chung94d67682013-09-25 16:29:40 -0700516
Winson Chung64359a52013-07-08 17:17:08 -0700517 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700518 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700519 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700520 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700521 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700522 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700523 }
524 }
525
Winson Chung76828c82013-08-19 15:43:29 -0700526 // Update the workspace screens
527 updateWorkspaceScreenOrder(context, workspaceScreens);
528
Adam Cohen76a47a12014-02-05 11:47:43 -0800529 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700530 runOnMainThread(new Runnable() {
531 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800532 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700533 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700534 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
535 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700536 if (!addedShortcutsFinal.isEmpty()) {
537 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
538 long lastScreenId = info.screenId;
539 for (ItemInfo i : addedShortcutsFinal) {
540 if (i.screenId == lastScreenId) {
541 addAnimated.add(i);
542 } else {
543 addNotAnimated.add(i);
544 }
Winson Chung997a9232013-07-24 15:33:46 -0700545 }
546 }
Winson Chungd64d1762013-08-20 14:37:16 -0700547 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800548 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700549 }
Winson Chung64359a52013-07-08 17:17:08 -0700550 }
Winson Chung997a9232013-07-24 15:33:46 -0700551 });
552 }
Winson Chung64359a52013-07-08 17:17:08 -0700553 }
554 };
555 runOnWorkerThread(r);
556 }
557
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 /**
559 * Adds an item to the DB if it was not created previously, or move it to a new
560 * <container, screen, cellX, cellY>
561 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800562 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700563 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 if (item.container == ItemInfo.NO_ID) {
565 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700566 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 } else {
568 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700569 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
571 }
572
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700573 static void checkItemInfoLocked(
574 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
575 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
576 if (modelItem != null && item != modelItem) {
577 // check all the data is consistent
578 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
579 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
580 ShortcutInfo shortcut = (ShortcutInfo) item;
581 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
582 modelShortcut.intent.filterEquals(shortcut.intent) &&
583 modelShortcut.id == shortcut.id &&
584 modelShortcut.itemType == shortcut.itemType &&
585 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700586 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700587 modelShortcut.cellX == shortcut.cellX &&
588 modelShortcut.cellY == shortcut.cellY &&
589 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700590 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700591 // For all intents and purposes, this is the same object
592 return;
593 }
594 }
595
596 // the modelItem needs to match up perfectly with item if our model is
597 // to be consistent with the database-- for now, just require
598 // modelItem == item or the equality check above
599 String msg = "item: " + ((item != null) ? item.toString() : "null") +
600 "modelItem: " +
601 ((modelItem != null) ? modelItem.toString() : "null") +
602 "Error: ItemInfo passed to checkItemInfo doesn't match original";
603 RuntimeException e = new RuntimeException(msg);
604 if (stackTrace != null) {
605 e.setStackTrace(stackTrace);
606 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800607 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700608 }
609 }
610
Michael Jurka816474f2012-06-25 14:49:02 -0700611 static void checkItemInfo(final ItemInfo item) {
612 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
613 final long itemId = item.id;
614 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700615 public void run() {
616 synchronized (sBgLock) {
617 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700618 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700619 }
620 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700621 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700622 }
623
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
625 final ItemInfo item, final String callingFunction) {
626 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700627 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700628 final ContentResolver cr = context.getContentResolver();
629
Adam Cohen487f7dd2012-06-28 18:12:10 -0700630 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631 Runnable r = new Runnable() {
632 public void run() {
633 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700634 updateItemArrays(item, itemId, stackTrace);
635 }
636 };
637 runOnWorkerThread(r);
638 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700639
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
641 final ArrayList<ItemInfo> items, final String callingFunction) {
642 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700643
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700644 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
645 Runnable r = new Runnable() {
646 public void run() {
647 ArrayList<ContentProviderOperation> ops =
648 new ArrayList<ContentProviderOperation>();
649 int count = items.size();
650 for (int i = 0; i < count; i++) {
651 ItemInfo item = items.get(i);
652 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700653 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700654 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700655
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700656 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
657 updateItemArrays(item, itemId, stackTrace);
658
659 }
660 try {
661 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
662 } catch (Exception e) {
663 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700664 }
665 }
666 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700667 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700669
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700670 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
671 // Lock on mBgLock *after* the db operation
672 synchronized (sBgLock) {
673 checkItemInfoLocked(itemId, item, stackTrace);
674
675 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
676 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
677 // Item is in a folder, make sure this folder exists
678 if (!sBgFolders.containsKey(item.container)) {
679 // An items container is being set to a that of an item which is not in
680 // the list of Folders.
681 String msg = "item: " + item + " container being set to: " +
682 item.container + ", not in the list of folders";
683 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 }
685 }
686
687 // Items are added/removed from the corresponding FolderInfo elsewhere, such
688 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
689 // that are on the desktop, as appropriate
690 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800691 if (modelItem != null &&
692 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
693 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694 switch (modelItem.itemType) {
695 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
696 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700697 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
699 if (!sBgWorkspaceItems.contains(modelItem)) {
700 sBgWorkspaceItems.add(modelItem);
701 }
702 break;
703 default:
704 break;
705 }
706 } else {
707 sBgWorkspaceItems.remove(modelItem);
708 }
709 }
710 }
711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700714 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700715 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 item.cellX = cellX;
719 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 // We store hotseat items in canonical form which is this orientation invariant position
722 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700725 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700726 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729
730 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
733 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800734 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700735 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736
Michael Jurkac9d95c52011-08-29 14:03:34 -0700737 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700738 }
739
740 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700741 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
742 * cellX, cellY have already been updated on the ItemInfos.
743 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800744 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 final long container, final int screen) {
746
747 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
748 int count = items.size();
749
750 for (int i = 0; i < count; i++) {
751 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 item.container = container;
753
754 // We store hotseat items in canonical form which is this orientation invariant position
755 // in the hotseat
756 if (context instanceof Launcher && screen < 0 &&
757 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700758 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700759 item.cellY);
760 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700761 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700762 }
763
764 final ContentValues values = new ContentValues();
765 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
766 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
767 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800768 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700769 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700770
771 contentValues.add(values);
772 }
773 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
774 }
775
776 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700777 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800778 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700781 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800782 item.cellX = cellX;
783 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 item.spanX = spanX;
785 item.spanY = spanY;
786
787 // We store hotseat items in canonical form which is this orientation invariant position
788 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700790 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700791 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700792 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700794 }
Adam Cohend4844c32011-02-18 19:25:06 -0800795
Adam Cohend4844c32011-02-18 19:25:06 -0800796 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800797 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
799 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800800 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
802 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700803 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800804
Michael Jurka816474f2012-06-25 14:49:02 -0700805 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700806 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700807
808 /**
809 * Update an item to the database in a specified container.
810 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700811 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700812 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100813 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700814 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800815 }
816
Sunny Goyal756a28a2015-04-23 17:07:55 -0700817 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700818 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700819 synchronized (mLock) {
820 if (!mHasLoaderCompletedOnce ||
821 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
822 throw new RuntimeException("Trying to add shortcut while loader is running");
823 }
824 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700825 }
826 }
827
Adam Cohend4844c32011-02-18 19:25:06 -0800828 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700829 * Returns true if the shortcuts already exists on the workspace. This must be called after
830 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700832 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
833 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700834 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700835 if (intent.getComponent() != null) {
836 // If component is not null, an intent with null package will produce
837 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700838 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700839 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700840 intentWithPkg = intent.toUri(0);
841 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700842 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700843 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
844 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700845 }
846 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700847 intentWithPkg = intent.toUri(0);
848 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700849 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700850
851 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700852 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700853 if (item instanceof ShortcutInfo) {
854 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700855 Intent targetIntent = info.promisedIntent == null
856 ? info.intent : info.promisedIntent;
857 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700858 Intent copyIntent = new Intent(targetIntent);
859 copyIntent.setSourceBounds(intent.getSourceBounds());
860 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700861 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
862 return true;
863 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700864 }
865 }
866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700868 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700869 }
870
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872 * Add an item to the database in a specified container. Sets the container, screen, cellX and
873 * cellY fields of the item. Also assigns an ID to the item.
874 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700875 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700876 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 item.cellX = cellX;
879 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700880 // We store hotseat items in canonical form which is this orientation invariant position
881 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700882 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700885 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700886 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888
889 final ContentValues values = new ContentValues();
890 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100891 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892
Sunny Goyald2497482015-09-22 18:24:19 -0700893 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
894 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
895
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700896 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700897
Jason Monk8e19cf22014-03-20 15:06:57 -0400898 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700899 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700900 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700901 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700904 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400905 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 sBgItemsIdMap.put(item.id, item);
907 switch (item.itemType) {
908 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
909 sBgFolders.put(item.id, (FolderInfo) item);
910 // Fall through
911 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
912 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700913 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
915 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
916 sBgWorkspaceItems.add(item);
917 } else {
918 if (!sBgFolders.containsKey(item.container)) {
919 // Adding an item to a folder that doesn't exist.
920 String msg = "adding item: " + item + " to a folder that " +
921 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700922 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700923 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700924 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700925 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700926 incrementPinnedShortcutCount(
Tony Wickhamfc02c1b2016-08-29 15:17:48 -0700927 ShortcutKey.fromShortcutInfo((ShortcutInfo) item),
928 true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700929 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700930 break;
931 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
932 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
933 break;
934 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700935 }
936 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700937 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700938 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700939 }
940
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700941 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700942 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700944 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700945 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700946 items.add(item);
947 deleteItemsFromDatabase(context, items);
948 }
949
950 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700951 * Removes all the items from the database matching {@param matcher}.
952 */
953 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
954 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgItemsIdMap));
955 }
956
957 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700958 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700959 */
Sunny Goyal40452cf2016-09-01 15:17:46 -0700960 static void deleteItemsFromDatabase(Context context, final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700962 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700963 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700964 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700965 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700966 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700967
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700968 // Lock on mBgLock *after* the db operation
969 synchronized (sBgLock) {
970 switch (item.itemType) {
971 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
972 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700973 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700974 if (info.container == item.id) {
975 // We are deleting a folder which still contains items that
976 // think they are contained by that folder.
977 String msg = "deleting a folder (" + item + ") which still " +
978 "contains items (" + info + ")";
979 Log.e(TAG, msg);
980 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700981 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700982 sBgWorkspaceItems.remove(item);
983 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700984 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Tony Wickhamfc02c1b2016-08-29 15:17:48 -0700985 decrementPinnedShortcutCount(ShortcutKey.fromShortcutInfo(
986 (ShortcutInfo) item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700987 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700988 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
989 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
990 sBgWorkspaceItems.remove(item);
991 break;
992 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
993 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
994 break;
995 }
996 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700997 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700998 }
999 }
Michael Jurka83df1882011-08-31 20:59:26 -07001000 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 }
1003
1004 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001005 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1006 */
1007 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1008 synchronized (sBgLock) {
1009 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1010 if (count == null || --count.value == 0) {
1011 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1012 }
1013 }
1014 }
1015
1016 /**
1017 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1018 *
1019 * As an optimization, the caller can pass shouldPin == false to avoid
1020 * unnecessary RPC's if the shortcut is already pinned.
1021 */
1022 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1023 synchronized (sBgLock) {
1024 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1025 if (count == null) {
1026 count = new MutableInt(1);
1027 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1028 } else {
1029 count.value++;
1030 }
1031 if (shouldPin && count.value == 1) {
1032 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1033 }
1034 }
1035 }
1036
1037 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001038 * Update the order of the workspace screens in the database. The array list contains
1039 * a list of screen ids in the order that they should appear.
1040 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001041 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001042 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001043 final ContentResolver cr = context.getContentResolver();
1044 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1045
1046 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001047 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001048 while (iter.hasNext()) {
1049 long id = iter.next();
1050 if (id < 0) {
1051 iter.remove();
1052 }
1053 }
1054
1055 Runnable r = new Runnable() {
1056 @Override
1057 public void run() {
Yura085c8532014-02-11 15:15:29 +00001058 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001060 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001061 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 for (int i = 0; i < count; i++) {
1063 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001064 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001065 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1066 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001067 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 }
Yura085c8532014-02-11 15:15:29 +00001069
1070 try {
1071 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1072 } catch (Exception ex) {
1073 throw new RuntimeException(ex);
1074 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001075
Winson Chungba9c37f2013-08-30 14:11:37 -07001076 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001077 sBgWorkspaceScreens.clear();
1078 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001079 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 }
1081 };
1082 runOnWorkerThread(r);
1083 }
1084
1085 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001086 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 */
Winsonc0b52fe2015-09-09 16:38:15 -07001088 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089 final ContentResolver cr = context.getContentResolver();
1090
Michael Jurkac9d95c52011-08-29 14:03:34 -07001091 Runnable r = new Runnable() {
1092 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001093 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001094 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001095 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001096 sBgItemsIdMap.remove(info.id);
1097 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001098 sBgWorkspaceItems.remove(info);
1099 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001100
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001101 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001102 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001103 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001104 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001105 for (ItemInfo childInfo : info.contents) {
1106 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001107 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001108 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001109 }
1110 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001111 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112 }
1113
1114 /**
1115 * Set this as the current Launcher activity object for the loader.
1116 */
1117 public void initialize(Callbacks callbacks) {
1118 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001119 Preconditions.assertUIThread();
1120 // Remove any queued UI runnables
1121 mHandler.cancelAll();
1122 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 }
1124 }
1125
Kenny Guyed131872014-04-30 03:02:21 +01001126 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001127 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001128 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001129 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001130 user));
1131 }
1132
1133 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001134 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001135 onPackagesRemoved(user, packageName);
1136 }
1137
1138 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +01001139 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001140 enqueueItemUpdatedTask(new PackageUpdatedTask(op, packages, user));
Kenny Guyed131872014-04-30 03:02:21 +01001141 }
1142
1143 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001144 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001145 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001146 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001147 user));
1148 }
1149
1150 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001151 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001152 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001153 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001154 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001155 }
1156
1157 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001158 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001159 boolean replacing) {
1160 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001161 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001162 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1163 user));
1164 }
Kenny Guyed131872014-04-30 03:02:21 +01001165 }
1166
Kenny Guy44cba692016-01-21 19:50:02 +00001167 @Override
1168 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001169 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001170 PackageUpdatedTask.OP_SUSPEND, packageNames,
1171 user));
1172 }
1173
1174 @Override
1175 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001176 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001177 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1178 user));
1179 }
1180
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001181 @Override
1182 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1183 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001184 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1185 }
1186
1187 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1188 UserHandleCompat user) {
1189 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001190 }
1191
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001192 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1194 * ACTION_PACKAGE_CHANGED.
1195 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001196 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001197 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001198 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001199
Joe Onorato36115782010-06-17 13:28:48 -04001200 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001201 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001202 // If we have changed locale we need to clear out the labels in all apps/workspace.
1203 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001204 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1205 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001206 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001207 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001208 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1209 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1210 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001211 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1212 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001213 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1214 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1215 enqueueItemUpdatedTask(new PackageUpdatedTask(
1216 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1217 new String[0], user));
1218 }
1219
1220 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1221 // we need to run the state change task again.
1222 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1223 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1224 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1225 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001226 }
Tony Wickham827cef22016-03-17 15:39:39 -07001227 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1228 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001229 }
1230 }
1231
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001232 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001233 resetLoadedState(true, true);
1234
Reena Lee93f824a2011-09-23 17:20:28 -07001235 // Do this here because if the launcher activity is running it will be restarted.
1236 // If it's not running startLoaderFromBackground will merely tell it that it needs
1237 // to reload.
1238 startLoaderFromBackground();
1239 }
1240
Winson Chungf0c6ae02012-03-21 16:10:31 -07001241 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1242 synchronized (mLock) {
1243 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1244 // mWorkspaceLoaded to true later
1245 stopLoaderLocked();
1246 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1247 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001248 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001249 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001250 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001251 }
1252 }
1253
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001254 /**
1255 * When the launcher is in the background, it's possible for it to miss paired
1256 * configuration changes. So whenever we trigger the loader from the background
1257 * tell the launcher that it needs to re-run the loader when it comes back instead
1258 * of doing it now.
1259 */
1260 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001261 Callbacks callbacks = getCallback();
1262 if (callbacks != null) {
1263 // Only actually run the loader if they're not paused.
1264 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001265 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001266 }
1267 }
Joe Onorato36115782010-06-17 13:28:48 -04001268 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001269
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001270 /**
1271 * If there is already a loader task running, tell it to stop.
1272 */
1273 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001274 LoaderTask oldTask = mLoaderTask;
1275 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001276 oldTask.stopLocked();
1277 }
Reena Lee93f824a2011-09-23 17:20:28 -07001278 }
1279
Adam Cohen1a85c582014-09-30 09:48:49 -07001280 public boolean isCurrentCallbacks(Callbacks callbacks) {
1281 return (mCallbacks != null && mCallbacks.get() == callbacks);
1282 }
1283
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001284 /**
1285 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1286 * @return true if the page could be bound synchronously.
1287 */
1288 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001289 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1290 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001291 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001292 // Don't bother to start the thread if we know it's not going to do anything
1293 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001294 final Callbacks oldCallbacks = mCallbacks.get();
1295 // Clear any pending bind-runnables from the synchronized load process.
1296 runOnMainThread(new Runnable() {
1297 public void run() {
1298 oldCallbacks.clearPendingBinds();
1299 }
1300 });
1301
Joe Onorato36115782010-06-17 13:28:48 -04001302 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001303 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001304 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001305 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001306 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1307 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001308 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001309 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001310 } else {
1311 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1312 sWorker.post(mLoaderTask);
1313 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001316 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 }
1318
Joe Onorato36115782010-06-17 13:28:48 -04001319 public void stopLoader() {
1320 synchronized (mLock) {
1321 if (mLoaderTask != null) {
1322 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323 }
1324 }
Joe Onorato36115782010-06-17 13:28:48 -04001325 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001327 /**
1328 * Loads the workspace screen ids in an ordered list.
1329 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001330 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001331 final ContentResolver contentResolver = context.getContentResolver();
1332 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001333
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001334 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001335 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1336 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001337 }
1338
Joe Onorato36115782010-06-17 13:28:48 -04001339 /**
1340 * Runnable for the thread that loads the contents of the launcher:
1341 * - workspace icons
1342 * - widgets
1343 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001344 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001345 */
1346 private class LoaderTask implements Runnable {
1347 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001348 private int mPageToBindFirst;
1349
Adam Cohen091440a2015-03-18 14:16:05 -07001350 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001351 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001352 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001354 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001355 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001356 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 }
1358
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001359 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001360 mIsLoadingAndBindingWorkspace = true;
1361
Joe Onorato36115782010-06-17 13:28:48 -04001362 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001363 if (DEBUG_LOADERS) {
1364 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001366
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001367 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001368 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001369 synchronized (LoaderTask.this) {
1370 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001371 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001372 }
1373 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001374 }
1375 }
1376
Joe Onorato36115782010-06-17 13:28:48 -04001377 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001378 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001379 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001380
Joe Onorato36115782010-06-17 13:28:48 -04001381 private void waitForIdle() {
1382 // Wait until the either we're stopped or the other threads are done.
1383 // This way we don't start loading all apps until the workspace has settled
1384 // down.
1385 synchronized (LoaderTask.this) {
1386 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001387
Joe Onorato36115782010-06-17 13:28:48 -04001388 mHandler.postIdle(new Runnable() {
1389 public void run() {
1390 synchronized (LoaderTask.this) {
1391 mLoadAndBindStepFinished = true;
1392 if (DEBUG_LOADERS) {
1393 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001394 }
Joe Onorato36115782010-06-17 13:28:48 -04001395 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001396 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001397 }
Joe Onorato36115782010-06-17 13:28:48 -04001398 });
1399
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001400 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001401 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001402 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1403 // wait no longer than 1sec at a time
1404 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001405 } catch (InterruptedException ex) {
1406 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 }
1408 }
Joe Onorato36115782010-06-17 13:28:48 -04001409 if (DEBUG_LOADERS) {
1410 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001411 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001412 + "ms for previous step to finish binding");
1413 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001414 }
Joe Onorato36115782010-06-17 13:28:48 -04001415 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001416
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001417 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001418 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001419 // Ensure that we have a valid page index to load synchronously
1420 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1421 "valid page index");
1422 }
1423 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1424 // Ensure that we don't try and bind a specified page when the pages have not been
1425 // loaded already (we should load everything asynchronously in that case)
1426 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1427 }
1428 synchronized (mLock) {
1429 if (mIsLoaderTaskRunning) {
1430 // Ensure that we are never running the background loading at this point since
1431 // we also touch the background collections
1432 throw new RuntimeException("Error! Background loading is already running");
1433 }
1434 }
1435
1436 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1437 // data structures, we can't allow any other thread to touch that data, but because
1438 // this call is synchronous, we can get away with not locking).
1439
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001440 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001441 // operations from the previous activity. We need to ensure that all queued operations
1442 // are executed before any synchronous binding work is done.
1443 mHandler.flush();
1444
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001445 // Divide the set of loaded items into those that we are binding synchronously, and
1446 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001447 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001448 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1449 // arise from that.
1450 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001451
1452 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001453 }
1454
Joe Onorato36115782010-06-17 13:28:48 -04001455 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001457 if (mStopped) {
1458 return;
1459 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001460 mIsLoaderTaskRunning = true;
1461 }
Joe Onorato36115782010-06-17 13:28:48 -04001462 // Optimize for end-user experience: if the Launcher is up and // running with the
1463 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1464 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001465 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001466 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001467 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001468
Joe Onorato36115782010-06-17 13:28:48 -04001469 if (mStopped) {
1470 break keep_running;
1471 }
1472
Joe Onorato36115782010-06-17 13:28:48 -04001473 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001474
1475 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001476 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1477 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001478
1479 waitForIdle();
1480
1481 // third step
1482 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1483 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001484 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485
Joe Onorato36115782010-06-17 13:28:48 -04001486 // Clear out this reference, otherwise we end up holding it until all of the
1487 // callback runnables are done.
1488 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489
Joe Onorato36115782010-06-17 13:28:48 -04001490 synchronized (mLock) {
1491 // If we are still the last one to be scheduled, remove ourselves.
1492 if (mLoaderTask == this) {
1493 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001494 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001495 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001496 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001497 }
Joe Onorato36115782010-06-17 13:28:48 -04001498 }
1499
1500 public void stopLocked() {
1501 synchronized (LoaderTask.this) {
1502 mStopped = true;
1503 this.notify();
1504 }
1505 }
1506
1507 /**
1508 * Gets the callbacks object. If we've been stopped, or if the launcher object
1509 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1510 * object that was around when the deferred message was scheduled, and if there's
1511 * a new Callbacks object around then also return null. This will save us from
1512 * calling onto it with data that will be ignored.
1513 */
1514 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1515 synchronized (mLock) {
1516 if (mStopped) {
1517 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001518 }
Joe Onorato36115782010-06-17 13:28:48 -04001519
1520 if (mCallbacks == null) {
1521 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001522 }
Joe Onorato36115782010-06-17 13:28:48 -04001523
1524 final Callbacks callbacks = mCallbacks.get();
1525 if (callbacks != oldCallbacks) {
1526 return null;
1527 }
1528 if (callbacks == null) {
1529 Log.w(TAG, "no mCallbacks");
1530 return null;
1531 }
1532
1533 return callbacks;
1534 }
1535 }
1536
1537 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001538 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001539 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001540 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001541 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001542
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001544 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001545 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001546 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1547 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001548 Log.e(TAG, "Error loading shortcut into hotseat " + item
1549 + " into position (" + item.screenId + ":" + item.cellX + ","
1550 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001551 return false;
1552 }
1553
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001554 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001555 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1556
Adam Cohen2e6da152015-05-06 11:42:25 -07001557 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001558 Log.e(TAG, "Error loading shortcut " + item
1559 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001560 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001561 + ")");
1562 return false;
1563 }
1564
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001565 if (hotseatOccupancy != null) {
1566 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 Log.e(TAG, "Error loading shortcut into hotseat " + item
1568 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001569 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001571 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001572 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001573 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001575 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001576 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1577 occupancy.cells[(int) item.screenId][0] = true;
1578 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001579 return true;
1580 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001581 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1582 if (!workspaceScreens.contains((Long) item.screenId)) {
1583 // The item has an invalid screen id.
1584 return false;
1585 }
1586 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001587 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001588 return true;
1589 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001590
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001591 final int countX = profile.numColumns;
1592 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001593 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1594 item.cellX < 0 || item.cellY < 0 ||
1595 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1596 Log.e(TAG, "Error loading shortcut " + item
1597 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1598 + item.cellX + "," + item.cellY
1599 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1600 return false;
1601 }
1602
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001603 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001604 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1605 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001606 // Mark the first row as occupied (if the feature is enabled)
1607 // in order to account for the QSB.
1608 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001609 }
1610 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001611 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001612 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001613
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001614 // Check if any workspace icons overlap with each other
1615 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1616 occupancy.markCells(item, true);
1617 return true;
1618 } else {
1619 Log.e(TAG, "Error loading shortcut " + item
1620 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1621 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1622 + ") already occupied");
1623 return false;
1624 }
Joe Onorato36115782010-06-17 13:28:48 -04001625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626
Winson Chungba9c37f2013-08-30 14:11:37 -07001627 /** Clears all the sBg data structures */
1628 private void clearSBgDataStructures() {
1629 synchronized (sBgLock) {
1630 sBgWorkspaceItems.clear();
1631 sBgAppWidgets.clear();
1632 sBgFolders.clear();
1633 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001634 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001635 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001636 }
1637 }
1638
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001639 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001640 if (LauncherAppState.PROFILE_STARTUP) {
1641 Trace.beginSection("Loading Workspace");
1642 }
Joe Onorato36115782010-06-17 13:28:48 -04001643 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644
Joe Onorato36115782010-06-17 13:28:48 -04001645 final Context context = mContext;
1646 final ContentResolver contentResolver = context.getContentResolver();
1647 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001648 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001649 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001650 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001651 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001652
Winson Chung892c74d2013-08-22 16:15:50 -07001653 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001654 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001655 int countX = profile.numColumns;
1656 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001657
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001658 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001659 try {
1660 ImportDataTask.performImportIfPossible(context);
1661 } catch (Exception e) {
1662 // Migration failed. Clear workspace.
1663 clearDb = true;
1664 }
1665
1666 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001667 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1668 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001669 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001670 }
1671
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001672 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001673 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001674 LauncherSettings.Settings.call(contentResolver,
1675 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001676 }
1677
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001678 Log.d(TAG, "loadWorkspace: loading default favorites");
1679 LauncherSettings.Settings.call(contentResolver,
1680 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001681
Winson Chung2abf94d2012-07-18 18:16:38 -07001682 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001683 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001684 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001685 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001686 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001687
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001688 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1689 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001690 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001691 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001692 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001693 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001694
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 // +1 for the hotseat (it can be larger than the workspace)
1696 // Load workspace in reverse order to ensure that latest items are loaded first (and
1697 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001698 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001699 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 try {
1702 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1703 final int intentIndex = c.getColumnIndexOrThrow
1704 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001705 final int containerIndex = c.getColumnIndexOrThrow(
1706 LauncherSettings.Favorites.CONTAINER);
1707 final int itemTypeIndex = c.getColumnIndexOrThrow(
1708 LauncherSettings.Favorites.ITEM_TYPE);
1709 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1710 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001711 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1712 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 final int screenIndex = c.getColumnIndexOrThrow(
1714 LauncherSettings.Favorites.SCREEN);
1715 final int cellXIndex = c.getColumnIndexOrThrow
1716 (LauncherSettings.Favorites.CELLX);
1717 final int cellYIndex = c.getColumnIndexOrThrow
1718 (LauncherSettings.Favorites.CELLY);
1719 final int spanXIndex = c.getColumnIndexOrThrow
1720 (LauncherSettings.Favorites.SPANX);
1721 final int spanYIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001723 final int rankIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001725 final int restoredIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001727 final int profileIdIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001729 final int optionsIndex = c.getColumnIndexOrThrow(
1730 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001731 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001732
Sunny Goyal7f834d22015-04-21 10:10:23 -07001733 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001734 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001735 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001736 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001737 long serialNo = mUserManager.getSerialNumberForUser(user);
1738 allUsers.put(serialNo, user);
1739 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001740
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001741 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001742
1743 // We can only query for shortcuts when the user is unlocked.
1744 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001745 List<ShortcutInfoCompat> pinnedShortcuts =
1746 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1747 if (mDeepShortcutManager.wasLastCallSuccess()) {
1748 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1749 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1750 shortcut);
1751 }
1752 } else {
1753 // Shortcut manager can fail due to some race condition when the
1754 // lock state changes too frequently. For the purpose of the loading
1755 // shortcuts, consider the user is still locked.
1756 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001757 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001758 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001759 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001760 }
1761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 ShortcutInfo info;
1763 String intentDescription;
1764 LauncherAppWidgetInfo appWidgetInfo;
1765 int container;
1766 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001767 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001768 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001769 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001770 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001772 while (!mStopped && c.moveToNext()) {
1773 try {
1774 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001775 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001776 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001777 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 switch (itemType) {
1780 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1781 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001782 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001783 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001785 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001786 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001787 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001788 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001789 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001790 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001791 if (user == null) {
1792 // User has been deleted remove the item.
1793 itemsToRemove.add(id);
1794 continue;
1795 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001796 try {
1797 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001798 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001799 if (cn != null && cn.getPackageName() != null) {
1800 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1801 cn.getPackageName(), user);
1802 boolean validComponent = validPkg &&
1803 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001804 if (validPkg) {
1805 targetPackage = cn.getPackageName();
1806 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001807
1808 if (validComponent) {
1809 if (restored) {
1810 // no special handling necessary for this item
1811 restoredRows.add(id);
1812 restored = false;
1813 }
Kenny Guyff05f432016-01-22 17:48:29 +00001814 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001815 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001816 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001817 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001818 intent = null;
1819 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1820 // We allow auto install apps to have their intent
1821 // updated after an install.
1822 intent = manager.getLaunchIntentForPackage(
1823 cn.getPackageName());
1824 if (intent != null) {
1825 ContentValues values = new ContentValues();
1826 values.put(LauncherSettings.Favorites.INTENT,
1827 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001828 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001829 }
1830 }
1831
1832 if (intent == null) {
1833 // The app is installed but the component is no
1834 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001835 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001836 itemsToRemove.add(id);
1837 continue;
1838 } else {
1839 // no special handling necessary for this item
1840 restoredRows.add(id);
1841 restored = false;
1842 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001843 } else if (restored) {
1844 // Package is not yet available but might be
1845 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001846 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001847
1848 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1849 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001850 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001851 // App restore has started. Update the flag
1852 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1853 ContentValues values = new ContentValues();
1854 values.put(LauncherSettings.Favorites.RESTORED,
1855 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001856 updateItem(id, values);
1857 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1858 // This is a common app. Try to replace this.
1859 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1860 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1861 if (parser.findDefaultApp()) {
1862 // Default app found. Replace it.
1863 intent = parser.parsedIntent;
1864 cn = intent.getComponent();
1865 ContentValues values = parser.parsedValues;
1866 values.put(LauncherSettings.Favorites.RESTORED, 0);
1867 updateItem(id, values);
1868 restored = false;
1869 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001870
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001871 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001872 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001873 itemsToRemove.add(id);
1874 continue;
1875 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001876 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001877 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001878 itemsToRemove.add(id);
1879 continue;
1880 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001881 } else if (PackageManagerHelper.isAppOnSdcard(
1882 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001883 // Package is present but not available.
1884 allowMissingTarget = true;
1885 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1886 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001887 // SdCard is not ready yet. Package might get available,
1888 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001889 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001890 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001891 allowMissingTarget = true;
1892 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001893
1894 } else {
1895 // Do not wait for external media load anymore.
1896 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001897 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001898 itemsToRemove.add(id);
1899 continue;
Winson Chungee055712013-07-30 14:46:24 -07001900 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001901 } else if (cn == null) {
1902 // For shortcuts with no component, keep them as they are
1903 restoredRows.add(id);
1904 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001907 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001908 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 continue;
1910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911
Sunny Goyal34b65272015-03-11 16:56:52 -07001912 boolean useLowResIcon = container >= 0 &&
1913 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1914
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001915 if (itemReplaced) {
1916 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001917 info = getAppShortcutInfo(intent, user, null,
1918 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001919 } else {
1920 // Don't replace items for other profiles.
1921 itemsToRemove.add(id);
1922 continue;
1923 }
1924 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001925 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001926 info = getRestoredItemInfo(c, intent,
1927 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001928 intent = getRestoredItemIntent(c, context, intent);
1929 } else {
1930 // Don't restore items for other profiles.
1931 itemsToRemove.add(id);
1932 continue;
1933 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001934 } else if (itemType ==
1935 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001936 info = getAppShortcutInfo(intent, user, c,
1937 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001938 } else if (itemType ==
1939 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001940
1941 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1942 if (unlockedUsers.get(serialNumber)) {
1943 ShortcutInfoCompat pinnedShortcut =
1944 shortcutKeyToPinnedShortcuts.get(key);
1945 if (pinnedShortcut == null) {
1946 // The shortcut is no longer valid.
1947 itemsToRemove.add(id);
1948 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001949 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001950 info = new ShortcutInfo(pinnedShortcut, context);
1951 intent = info.intent;
1952 } else {
1953 // Create a shortcut info in disabled mode for now.
1954 info = new ShortcutInfo();
1955 info.user = user;
1956 info.itemType = itemType;
1957 loadInfoFromCursor(info, c, cursorIconInfo);
1958
1959 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001960 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001961 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001962 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001963 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001964
Sunny Goyald09c3702016-04-06 16:18:20 -07001965 // Shortcuts are only available on the primary profile
1966 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1967 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1968 }
1969
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 // App shortcuts that used to be automatically added to Launcher
1971 // didn't always have the correct intent flags set, so do that
1972 // here
1973 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001974 intent.getCategories() != null &&
1975 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001976 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 intent.addFlags(
1978 Intent.FLAG_ACTIVITY_NEW_TASK |
1979 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1980 }
Michael Jurka96879562012-03-22 05:54:33 -07001981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001984 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 info.cellX = c.getInt(cellXIndex);
1989 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001990 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001991 info.spanX = 1;
1992 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001993 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001994 if (info.promisedIntent != null) {
1995 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1996 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001997 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001998 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1999 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2000 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002003 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002004 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 break;
2006 }
2007
Sunny Goyal756adbc2015-04-16 15:20:51 -07002008 if (restored) {
2009 ComponentName cn = info.getTargetComponent();
2010 if (cn != null) {
2011 Integer progress = installingPkgs.get(cn.getPackageName());
2012 if (progress != null) {
2013 info.setInstallProgress(progress);
2014 } else {
2015 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2016 }
2017 }
2018 }
2019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 switch (container) {
2021 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2022 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2023 sBgWorkspaceItems.add(info);
2024 break;
2025 default:
2026 // Item is in a user folder
2027 FolderInfo folderInfo =
2028 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002029 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 break;
2031 }
2032 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002033 } else {
2034 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 }
2036 break;
2037
2038 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2039 id = c.getLong(idIndex);
2040 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2041
Sunny Goyala508e4f2015-05-21 09:33:57 -07002042 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002043 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 folderInfo.cellX = c.getInt(cellXIndex);
2048 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002049 folderInfo.spanX = 1;
2050 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002051 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052
Daniel Sandler8802e962010-05-26 16:28:16 -04002053 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002054 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002055 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002056 break;
2057 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002058
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2061 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2062 sBgWorkspaceItems.add(folderInfo);
2063 break;
Joe Onorato36115782010-06-17 13:28:48 -04002064 }
Joe Onorato17a89222011-02-08 17:26:11 -08002065
Chris Wrenf4d08112014-01-16 18:13:56 -05002066 if (restored) {
2067 // no special handling required for restored folders
2068 restoredRows.add(id);
2069 }
2070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2072 sBgFolders.put(folderInfo.id, folderInfo);
2073 break;
2074
2075 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002076 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002078 boolean customWidget = itemType ==
2079 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2080
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002082 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002083 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002084 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002085 user = allUsers.get(serialNumber);
2086 if (user == null) {
2087 itemsToRemove.add(id);
2088 continue;
2089 }
2090
Sunny Goyalff572272014-07-23 13:58:07 -07002091 final ComponentName component =
2092 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002093
Sunny Goyal651077b2014-06-30 14:15:31 -07002094 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002095 final boolean isIdValid = (restoreStatus &
2096 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002097 final boolean wasProviderReady = (restoreStatus &
2098 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002099
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002100 if (widgetProvidersMap == null) {
2101 widgetProvidersMap = AppWidgetManagerCompat
2102 .getInstance(mContext).getAllProvidersMap();
2103 }
2104 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2105 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002106 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002107 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002108
2109 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002110 if (!isSafeMode && !customWidget &&
2111 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002112 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002113 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 itemsToRemove.add(id);
2115 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002116 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002117 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2118 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002119
Sunny Goyal53f96722015-07-13 19:54:53 -07002120 // The provider is available. So the widget is either
2121 // available or not available. We do not need to track
2122 // any future restore updates.
2123 int status = restoreStatus &
2124 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002125 if (!wasProviderReady) {
2126 // If provider was not previously ready, update the
2127 // status and UI flag.
2128
2129 // Id would be valid only if the widget restore broadcast was received.
2130 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002131 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002132 } else {
2133 status &= ~LauncherAppWidgetInfo
2134 .FLAG_PROVIDER_NOT_READY;
2135 }
2136 }
2137 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002138 } else {
2139 Log.v(TAG, "Widget restore pending id=" + id
2140 + " appWidgetId=" + appWidgetId
2141 + " status =" + restoreStatus);
2142 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002143 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002144 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002145 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002146
2147 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2148 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002149 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002150 // App restore has started. Update the flag
2151 appWidgetInfo.restoreStatus |=
2152 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07002153 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002154 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002155 itemsToRemove.add(id);
2156 continue;
2157 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002158
2159 appWidgetInfo.installProgress =
2160 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002161 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002162 if (appWidgetInfo.hasRestoreFlag(
2163 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2164 intentDescription = c.getString(intentIndex);
2165 if (!TextUtils.isEmpty(intentDescription)) {
2166 appWidgetInfo.bindOptions =
2167 Intent.parseUri(intentDescription, 0);
2168 }
2169 }
Sunny Goyalff572272014-07-23 13:58:07 -07002170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002172 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173 appWidgetInfo.cellX = c.getInt(cellXIndex);
2174 appWidgetInfo.cellY = c.getInt(cellYIndex);
2175 appWidgetInfo.spanX = c.getInt(spanXIndex);
2176 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002177 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2180 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2181 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002182 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2183 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 continue;
2185 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002187 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002189 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002190 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 break;
2192 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002193
Adam Cohen59400422014-03-05 18:07:04 -08002194 if (!customWidget) {
2195 String providerName =
2196 appWidgetInfo.providerName.flattenToString();
2197 if (!providerName.equals(savedProvider) ||
2198 (appWidgetInfo.restoreStatus != restoreStatus)) {
2199 ContentValues values = new ContentValues();
2200 values.put(
2201 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2202 providerName);
2203 values.put(LauncherSettings.Favorites.RESTORED,
2204 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002205 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002206 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002207 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2209 sBgAppWidgets.add(appWidgetInfo);
2210 }
Joe Onorato36115782010-06-17 13:28:48 -04002211 break;
2212 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002214 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002215 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 }
2217 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002218 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 }
2220
Winson Chungba9c37f2013-08-30 14:11:37 -07002221 // Break early if we've stopped loading
2222 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002223 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002224 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002225 }
2226
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002229 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2230 Utilities.createDbSelectionQuery(
2231 LauncherSettings.Favorites._ID, itemsToRemove), null);
2232 if (DEBUG_LOADERS) {
2233 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2234 LauncherSettings.Favorites._ID, itemsToRemove));
2235 }
2236
2237 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002238 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2239 .call(contentResolver,
2240 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2241 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2242 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002243 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2244 sBgFolders.remove(folderId);
2245 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002246 }
2247 }
2248
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002249 // Unpin shortcuts that don't exist on the workspace.
2250 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2251 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2252 if (numTimesPinned == null || numTimesPinned.value == 0) {
2253 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2254 mDeepShortcutManager.unpinShortcut(key);
2255 }
2256 }
2257
Sunny Goyal317698b2015-07-29 11:45:41 -07002258 // Sort all the folder items and make sure the first 3 items are high resolution.
2259 for (FolderInfo folder : sBgFolders) {
2260 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2261 int pos = 0;
2262 for (ShortcutInfo info : folder.contents) {
2263 if (info.usingLowResIcon) {
2264 info.updateIcon(mIconCache, false);
2265 }
2266 pos ++;
2267 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2268 break;
2269 }
2270 }
2271 }
2272
Chris Wrenf4d08112014-01-16 18:13:56 -05002273 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002274 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002275 ContentValues values = new ContentValues();
2276 values.put(LauncherSettings.Favorites.RESTORED, 0);
2277 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2278 Utilities.createDbSelectionQuery(
2279 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002280 }
2281
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002282 if (!isSdCardReady && !pendingPackages.isEmpty()) {
2283 context.registerReceiver(
2284 new SdCardAvailableReceiver(
2285 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002286 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002287 null,
2288 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002289 }
2290
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002291 // Remove any empty screens
2292 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002293 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002294 long screenId = item.screenId;
2295 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2296 unusedScreens.contains(screenId)) {
2297 unusedScreens.remove(screenId);
2298 }
2299 }
2300
2301 // If there are any empty screens remove them, and update.
2302 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002303 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 }
2306
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002307 if (DEBUG_LOADERS) {
2308 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2309 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002311 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002313
Sunny Goyale2df0622015-04-24 11:27:00 -07002314 for (int i = 0; i < nScreens; i++) {
2315 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002316 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 line += " | ";
2318 }
Joe Onorato36115782010-06-17 13:28:48 -04002319 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002321 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002322 }
Joe Onorato36115782010-06-17 13:28:48 -04002323 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002324 if (LauncherAppState.PROFILE_STARTUP) {
2325 Trace.endSection();
2326 }
Adam Cohene25af792013-06-06 23:08:25 -07002327 }
2328
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002329 /**
2330 * Partially updates the item without any notification. Must be called on the worker thread.
2331 */
2332 private void updateItem(long itemId, ContentValues update) {
2333 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002334 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002335 update,
2336 BaseColumns._ID + "= ?",
2337 new String[]{Long.toString(itemId)});
2338 }
2339
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 /** Filters the set of items who are directly or indirectly (via another container) on the
2341 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002342 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 ArrayList<ItemInfo> allWorkspaceItems,
2344 ArrayList<ItemInfo> currentScreenItems,
2345 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002346 // Purge any null ItemInfos
2347 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2348 while (iter.hasNext()) {
2349 ItemInfo i = iter.next();
2350 if (i == null) {
2351 iter.remove();
2352 }
2353 }
2354
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 // Order the set of items by their containers first, this allows use to walk through the
2356 // list sequentially, build up a list of containers that are in the specified screen,
2357 // as well as all items in those containers.
2358 Set<Long> itemsOnScreen = new HashSet<Long>();
2359 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2360 @Override
2361 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002362 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002363 }
2364 });
2365 for (ItemInfo info : allWorkspaceItems) {
2366 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002367 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 currentScreenItems.add(info);
2369 itemsOnScreen.add(info.id);
2370 } else {
2371 otherScreenItems.add(info);
2372 }
2373 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2374 currentScreenItems.add(info);
2375 itemsOnScreen.add(info.id);
2376 } else {
2377 if (itemsOnScreen.contains(info.container)) {
2378 currentScreenItems.add(info);
2379 itemsOnScreen.add(info.id);
2380 } else {
2381 otherScreenItems.add(info);
2382 }
2383 }
2384 }
2385 }
2386
2387 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002388 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002389 ArrayList<LauncherAppWidgetInfo> appWidgets,
2390 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2391 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392
2393 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002394 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002395 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002396 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002397 currentScreenWidgets.add(widget);
2398 } else {
2399 otherScreenWidgets.add(widget);
2400 }
2401 }
2402 }
2403
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2405 * right) */
2406 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002407 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002408 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002409 final int screenCols = profile.numColumns;
2410 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002412 @Override
2413 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002414 if (lhs.container == rhs.container) {
2415 // Within containers, order by their spatial position in that container
2416 switch ((int) lhs.container) {
2417 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2418 long lr = (lhs.screenId * screenCellCount +
2419 lhs.cellY * screenCols + lhs.cellX);
2420 long rr = (rhs.screenId * screenCellCount +
2421 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002422 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002423 }
2424 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2425 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002426 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002427 }
2428 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002429 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002430 throw new RuntimeException("Unexpected container type when " +
2431 "sorting workspace items.");
2432 }
2433 return 0;
2434 }
2435 } else {
2436 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002437 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002438 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002439 }
2440 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002442
Adam Cohendcd297f2013-06-18 13:13:40 -07002443 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2444 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002445 final Runnable r = new Runnable() {
2446 @Override
2447 public void run() {
2448 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2449 if (callbacks != null) {
2450 callbacks.bindScreens(orderedScreens);
2451 }
2452 }
2453 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002454 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002455 }
2456
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2458 final ArrayList<ItemInfo> workspaceItems,
2459 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002460 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002461
2462 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002463 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002465 final int start = i;
2466 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467 final Runnable r = new Runnable() {
2468 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002469 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002470 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002471 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002472 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2473 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002474 }
2475 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002477 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002478 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 // Bind the widgets, one at a time
2481 N = appWidgets.size();
2482 for (int i = 0; i < N; i++) {
2483 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2484 final Runnable r = new Runnable() {
2485 public void run() {
2486 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2487 if (callbacks != null) {
2488 callbacks.bindAppWidget(widget);
2489 }
2490 }
2491 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002492 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 }
2494 }
2495
2496 /**
2497 * Binds all loaded data to actual views on the main thread.
2498 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002499 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500 final long t = SystemClock.uptimeMillis();
2501 Runnable r;
2502
2503 // Don't use these two variables in any of the callback runnables.
2504 // Otherwise we hold a reference to them.
2505 final Callbacks oldCallbacks = mCallbacks.get();
2506 if (oldCallbacks == null) {
2507 // This launcher has exited and nobody bothered to tell us. Just bail.
2508 Log.w(TAG, "LoaderTask running with no launcher");
2509 return;
2510 }
2511
Winson Chung9b9fb962013-11-15 15:39:34 -08002512 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002513 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2514 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2515 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002516
Winson Chung2abf94d2012-07-18 18:16:38 -07002517 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518 workspaceItems.addAll(sBgWorkspaceItems);
2519 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002520 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521 }
2522
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002523 final int currentScreen;
2524 {
2525 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2526 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2527 if (currScreen >= orderedScreenIds.size()) {
2528 // There may be no workspace screens (just hotseat items and an empty page).
2529 currScreen = PagedView.INVALID_RESTORE_PAGE;
2530 }
2531 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002532 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002533 final boolean validFirstPage = currentScreen >= 0;
2534 final long currentScreenId =
2535 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002536
Winson Chung9b9fb962013-11-15 15:39:34 -08002537 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002538 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2539 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2540 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2541 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542
Winson Chung9b9fb962013-11-15 15:39:34 -08002543 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002545 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2548 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2549
2550 // Tell the workspace that we're about to start binding items
2551 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002552 public void run() {
2553 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2554 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002555 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002557 }
2558 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002560 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561
Adam Cohendcd297f2013-06-18 13:13:40 -07002562 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2563
Sunny Goyal527c7d32015-08-28 15:19:36 -07002564 Executor mainExecutor = new DeferredMainThreadExecutor();
2565 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002566 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002568 // In case of validFirstPage, only bind the first screen, and defer binding the
2569 // remaining screens after first onDraw (and an optional the fade animation whichever
2570 // happens later).
2571 // This ensures that the first screen is immediately visible (eg. during rotation)
2572 // In case of !validFirstPage, bind all pages one after other.
2573 final Executor deferredExecutor =
2574 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2575
2576 mainExecutor.execute(new Runnable() {
2577 @Override
2578 public void run() {
2579 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2580 if (callbacks != null) {
2581 callbacks.finishFirstPageBind(
2582 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2583 }
2584 }
2585 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002586
Sunny Goyal44c06432016-04-02 10:56:02 -07002587 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002588
2589 // Tell the workspace that we're done binding items
2590 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002591 public void run() {
2592 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2593 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002594 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002595 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002596
Sunny Goyal639e9062015-08-19 19:17:06 -07002597 mIsLoadingAndBindingWorkspace = false;
2598
2599 // Run all the bind complete runnables after workspace is bound.
2600 if (!mBindCompleteRunnables.isEmpty()) {
2601 synchronized (mBindCompleteRunnables) {
2602 for (final Runnable r : mBindCompleteRunnables) {
2603 runOnWorkerThread(r);
2604 }
2605 mBindCompleteRunnables.clear();
2606 }
2607 }
2608
Winson Chung98e030b2012-05-07 16:01:11 -07002609 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002610 if (DEBUG_LOADERS) {
2611 Log.d(TAG, "bound workspace in "
2612 + (SystemClock.uptimeMillis()-t) + "ms");
2613 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002614
Joe Onorato36115782010-06-17 13:28:48 -04002615 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002616 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002617 deferredExecutor.execute(r);
2618
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002619 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002620 r = new Runnable() {
2621 public void run() {
2622 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2623 if (callbacks != null) {
2624 // We are loading synchronously, which means, some of the pages will be
2625 // bound after first draw. Inform the callbacks that page binding is
2626 // not complete, and schedule the remaining pages.
2627 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2628 callbacks.onPageBoundSynchronously(currentScreen);
2629 }
2630 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2631 }
2632 }
2633 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002634 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002635 }
Joe Onorato36115782010-06-17 13:28:48 -04002636 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002637
Joe Onorato36115782010-06-17 13:28:48 -04002638 private void loadAndBindAllApps() {
2639 if (DEBUG_LOADERS) {
2640 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2641 }
2642 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002643 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002644 synchronized (LoaderTask.this) {
2645 if (mStopped) {
2646 return;
2647 }
2648 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002649 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002650 synchronized (LoaderTask.this) {
2651 if (mStopped) {
2652 return;
2653 }
2654 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002655 }
Joe Onorato36115782010-06-17 13:28:48 -04002656 } else {
2657 onlyBindAllApps();
2658 }
2659 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002660
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002661 private void updateIconCache() {
2662 // Ignore packages which have a promise icon.
2663 HashSet<String> packagesToIgnore = new HashSet<>();
2664 synchronized (sBgLock) {
2665 for (ItemInfo info : sBgItemsIdMap) {
2666 if (info instanceof ShortcutInfo) {
2667 ShortcutInfo si = (ShortcutInfo) info;
2668 if (si.isPromise() && si.getTargetComponent() != null) {
2669 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2670 }
2671 } else if (info instanceof LauncherAppWidgetInfo) {
2672 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2673 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2674 packagesToIgnore.add(lawi.providerName.getPackageName());
2675 }
2676 }
2677 }
2678 }
2679 mIconCache.updateDbIcons(packagesToIgnore);
2680 }
2681
Joe Onorato36115782010-06-17 13:28:48 -04002682 private void onlyBindAllApps() {
2683 final Callbacks oldCallbacks = mCallbacks.get();
2684 if (oldCallbacks == null) {
2685 // This launcher has exited and nobody bothered to tell us. Just bail.
2686 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2687 return;
2688 }
2689
2690 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002691 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002692 final ArrayList<AppInfo> list
2693 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002694 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002695 public void run() {
2696 final long t = SystemClock.uptimeMillis();
2697 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2698 if (callbacks != null) {
2699 callbacks.bindAllApplications(list);
2700 }
2701 if (DEBUG_LOADERS) {
2702 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002703 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002704 }
2705 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002706 };
Tony Wickham80f57872016-06-29 18:12:15 -07002707 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002708 }
2709
Winson Chung64359a52013-07-08 17:17:08 -07002710 private void loadAllApps() {
2711 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002712
Joe Onorato36115782010-06-17 13:28:48 -04002713 final Callbacks oldCallbacks = mCallbacks.get();
2714 if (oldCallbacks == null) {
2715 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002716 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002717 return;
2718 }
2719
Kenny Guyed131872014-04-30 03:02:21 +01002720 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2721
Winson Chung64359a52013-07-08 17:17:08 -07002722 // Clear the list of apps
2723 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002724 for (UserHandleCompat user : profiles) {
2725 // Query for the set of apps
2726 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002727 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002728 if (DEBUG_LOADERS) {
2729 Log.d(TAG, "getActivityList took "
2730 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2731 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2732 }
2733 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002734 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002735 if (apps == null || apps.isEmpty()) {
2736 return;
2737 }
Kenny Guyff05f432016-01-22 17:48:29 +00002738 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002739 // Create the ApplicationInfos
2740 for (int i = 0; i < apps.size(); i++) {
2741 LauncherActivityInfoCompat app = apps.get(i);
2742 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002743 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002744 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002745
Sunny Goyal756a28a2015-04-23 17:07:55 -07002746 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2747 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002748 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002749
2750 @Override
2751 public void run() {
2752 heuristic.processUserApps(apps);
2753 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002754 };
2755 runOnMainThread(new Runnable() {
2756
2757 @Override
2758 public void run() {
2759 // Check isLoadingWorkspace on the UI thread, as it is updated on
2760 // the UI thread.
2761 if (mIsLoadingAndBindingWorkspace) {
2762 synchronized (mBindCompleteRunnables) {
2763 mBindCompleteRunnables.add(r);
2764 }
2765 } else {
2766 runOnWorkerThread(r);
2767 }
2768 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002769 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002770 }
Winson Chung64359a52013-07-08 17:17:08 -07002771 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002772 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002773 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2774 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002775
2776 // Post callback on main thread
2777 mHandler.post(new Runnable() {
2778 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002779
Winson Chung64359a52013-07-08 17:17:08 -07002780 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002781 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002782 if (callbacks != null) {
2783 callbacks.bindAllApplications(added);
2784 if (DEBUG_LOADERS) {
2785 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002786 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002787 }
2788 } else {
2789 Log.i(TAG, "not binding apps: no Launcher activity");
2790 }
2791 }
2792 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002793 // Cleanup any data stored for a deleted user.
2794 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002795 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002796 Log.d(TAG, "Icons processed in "
2797 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002798 }
2799 }
2800
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002801 private void loadAndBindDeepShortcuts() {
2802 if (DEBUG_LOADERS) {
2803 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2804 }
2805 if (!mDeepShortcutsLoaded) {
2806 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002807 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2808 if (mHasShortcutHostPermission) {
2809 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2810 if (mUserManager.isUserUnlocked(user)) {
2811 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2812 .queryForAllShortcuts(user);
2813 updateDeepShortcutMap(null, user, shortcuts);
2814 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002815 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002816 }
2817 synchronized (LoaderTask.this) {
2818 if (mStopped) {
2819 return;
2820 }
2821 mDeepShortcutsLoaded = true;
2822 }
2823 }
Tony Wickham80f57872016-06-29 18:12:15 -07002824 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002825 }
2826
Joe Onoratobe386092009-11-17 17:32:16 -08002827 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002828 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002829 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002830 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2831 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2832 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2833 }
Joe Onorato36115782010-06-17 13:28:48 -04002834 }
2835 }
2836
Sunny Goyal860538d2016-07-20 12:35:16 -07002837 /**
2838 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2839 */
2840 private void updateDeepShortcutMap(
2841 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002842 if (packageName != null) {
2843 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2844 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002845 ComponentKey next = keysIter.next();
2846 if (next.componentName.getPackageName().equals(packageName)
2847 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002848 keysIter.remove();
2849 }
2850 }
2851 }
2852
2853 // Now add the new shortcuts to the map.
2854 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002855 boolean shouldShowInContainer = shortcut.isEnabled()
2856 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2857 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002858 ComponentKey targetComponent
2859 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2860 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2861 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002862 }
2863 }
2864
Tony Wickham80f57872016-06-29 18:12:15 -07002865 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002866 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002867 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002868 @Override
2869 public void run() {
2870 Callbacks callbacks = getCallback();
2871 if (callbacks != null) {
2872 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2873 }
2874 }
Tony Wickham80f57872016-06-29 18:12:15 -07002875 };
2876 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002877 }
2878
Sunny Goyal75b0f552015-05-20 21:57:06 -07002879 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002880 * Refreshes the cached shortcuts if the shortcut permission has changed.
2881 * Current implementation simply reloads the workspace, but it can be optimized to
2882 * use partial updates similar to {@link UserManagerCompat}
2883 */
2884 public void refreshShortcutsIfRequired() {
2885 if (Utilities.isNycMR1OrAbove()) {
2886 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2887 sWorker.post(mShortcutPermissionCheckRunnable);
2888 }
2889 }
2890
2891 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002892 * Called when the icons for packages have been updated in the icon cache.
2893 */
2894 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2895 final Callbacks callbacks = getCallback();
2896 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2897 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2898
2899 // If any package icon has changed (app was updated while launcher was dead),
2900 // update the corresponding shortcuts.
2901 synchronized (sBgLock) {
2902 for (ItemInfo info : sBgItemsIdMap) {
2903 if (info instanceof ShortcutInfo && user.equals(info.user)
2904 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2905 ShortcutInfo si = (ShortcutInfo) info;
2906 ComponentName cn = si.getTargetComponent();
2907 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2908 si.updateIcon(mIconCache);
2909 updatedShortcuts.add(si);
2910 }
2911 }
2912 }
2913 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2914 }
2915
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002916 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002917
2918 if (!updatedApps.isEmpty()) {
2919 mHandler.post(new Runnable() {
2920
2921 public void run() {
2922 Callbacks cb = getCallback();
2923 if (cb != null && callbacks == cb) {
2924 cb.bindAppsUpdated(updatedApps);
2925 }
2926 }
2927 });
2928 }
2929 }
2930
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002931 private void bindUpdatedShortcuts(
2932 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2933 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2934 }
2935
2936 private void bindUpdatedShortcuts(
2937 final ArrayList<ShortcutInfo> updatedShortcuts,
2938 final ArrayList<ShortcutInfo> removedShortcuts,
2939 final UserHandleCompat user) {
2940 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002941 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002942 mHandler.post(new Runnable() {
2943
2944 public void run() {
2945 Callbacks cb = getCallback();
2946 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002947 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002948 }
2949 }
2950 });
2951 }
2952 }
2953
2954 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002955 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002956 }
2957
2958 private class PackageUpdatedTask implements Runnable {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002959 final int mOp;
2960 final String[] mPackages;
2961 final UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002962
2963 public static final int OP_NONE = 0;
2964 public static final int OP_ADD = 1;
2965 public static final int OP_UPDATE = 2;
2966 public static final int OP_REMOVE = 3; // uninstlled
2967 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002968 public static final int OP_SUSPEND = 5; // package suspended
2969 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002970 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002971
Kenny Guyed131872014-04-30 03:02:21 +01002972 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002973 mOp = op;
2974 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002975 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002976 }
2977
2978 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002979 if (!mHasLoaderCompletedOnce) {
2980 // Loader has not yet run.
2981 return;
2982 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002983 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002984
2985 final String[] packages = mPackages;
2986 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002987 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyal40452cf2016-09-01 15:17:46 -07002988 final HashSet<String> packageSet = new HashSet<>(Arrays.asList(packages));
Joe Onorato36115782010-06-17 13:28:48 -04002989 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002990 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002991 for (int i=0; i<N; i++) {
2992 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002993 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002994 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002995 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002996
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002997 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2998 if (heuristic != null) {
2999 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003000 }
Joe Onorato36115782010-06-17 13:28:48 -04003001 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003002 }
Joe Onorato36115782010-06-17 13:28:48 -04003003 case OP_UPDATE:
3004 for (int i=0; i<N; i++) {
3005 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003006 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003007 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003008 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003009 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003010 // Since package was just updated, the target must be available now.
3011 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003012 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003013 case OP_REMOVE: {
3014 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3015 if (heuristic != null) {
3016 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003017 }
Joe Onorato36115782010-06-17 13:28:48 -04003018 for (int i=0; i<N; i++) {
3019 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003020 mIconCache.removeIconsForPkg(packages[i], mUser);
3021 }
3022 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003023 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003024 case OP_UNAVAILABLE:
3025 for (int i=0; i<N; i++) {
3026 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3027 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003028 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003029 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003030 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003031 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003032 case OP_SUSPEND:
3033 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003034 flagOp = mOp == OP_SUSPEND ?
3035 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3036 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003037 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyal40452cf2016-09-01 15:17:46 -07003038 mBgAllAppsList.updateDisabledFlags(
3039 ItemInfoMatcher.ofPackages(packageSet, mUser), flagOp);
Sunny Goyalda891c12016-03-18 18:29:24 -07003040 break;
3041 case OP_USER_AVAILABILITY_CHANGE:
3042 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3043 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3044 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3045 // We want to update all packages for this user.
Sunny Goyal40452cf2016-09-01 15:17:46 -07003046 mBgAllAppsList.updateDisabledFlags(ItemInfoMatcher.ofUser(mUser), flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003047 break;
Joe Onorato36115782010-06-17 13:28:48 -04003048 }
3049
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003050 ArrayList<AppInfo> added = null;
3051 ArrayList<AppInfo> modified = null;
3052 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003053
Adam Cohen487f7dd2012-06-28 18:12:10 -07003054 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003055 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003056 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003057 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003058 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003059 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003060 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
Winson Chung5d55f332012-07-16 20:45:03 -07003062 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003063 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003064 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003065 }
3066
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003067 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003068
Joe Onorato36115782010-06-17 13:28:48 -04003069 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003070 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003071 for (AppInfo ai : added) {
3072 addedOrUpdatedApps.put(ai.componentName, ai);
3073 }
Joe Onorato36115782010-06-17 13:28:48 -04003074 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003075
Joe Onorato36115782010-06-17 13:28:48 -04003076 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003077 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003078 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 for (AppInfo ai : modified) {
3080 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003081 }
3082
Joe Onorato36115782010-06-17 13:28:48 -04003083 mHandler.post(new Runnable() {
3084 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003085 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003086 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003087 callbacks.bindAppsUpdated(modifiedFinal);
3088 }
3089 }
3090 });
3091 }
Winson Chung83892cc2013-05-01 16:53:33 -07003092
Sunny Goyal4390ace2014-10-13 11:33:11 -07003093 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003094 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07003095 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
3096 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<>();
3097 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003098
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003099 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003100 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003101 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3102 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003103 boolean infoUpdated = false;
3104 boolean shortcutUpdated = false;
3105
3106 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003107 if ((si.iconResource != null)
Sunny Goyal40452cf2016-09-01 15:17:46 -07003108 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003109 Bitmap icon = LauncherIcons.createIconBitmap(
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003110 si.iconResource.packageName,
3111 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003112 if (icon != null) {
3113 si.setIcon(icon);
3114 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003116 }
3117 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118
3119 ComponentName cn = si.getTargetComponent();
Sunny Goyal40452cf2016-09-01 15:17:46 -07003120 if (cn != null && packageSet.contains(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3122
3123 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3125 // Auto install icon
3126 PackageManager pm = context.getPackageManager();
3127 ResolveInfo matched = pm.resolveActivity(
3128 new Intent(Intent.ACTION_MAIN)
3129 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3130 PackageManager.MATCH_DEFAULT_ONLY);
3131 if (matched == null) {
3132 // Try to find the best match activity.
3133 Intent intent = pm.getLaunchIntentForPackage(
3134 cn.getPackageName());
3135 if (intent != null) {
3136 cn = intent.getComponent();
3137 appInfo = addedOrUpdatedApps.get(cn);
3138 }
3139
3140 if ((intent == null) || (appInfo == null)) {
3141 removedShortcuts.add(si);
3142 continue;
3143 }
3144 si.promisedIntent = intent;
3145 }
3146 }
3147
3148 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003149 if (appInfo != null) {
3150 si.flags = appInfo.flags;
3151 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003152
Sunny Goyal756adbc2015-04-16 15:20:51 -07003153 si.intent = si.promisedIntent;
3154 si.promisedIntent = null;
3155 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003156 infoUpdated = true;
3157 si.updateIcon(mIconCache);
3158 }
3159
3160 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3161 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3162 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003163 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003164 si.contentDescription = appInfo.contentDescription;
3165 infoUpdated = true;
3166 }
3167
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003168 int oldDisabledFlags = si.isDisabled;
3169 si.isDisabled = flagOp.apply(si.isDisabled);
3170 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 shortcutUpdated = true;
3172 }
3173 }
3174
3175 if (infoUpdated || shortcutUpdated) {
3176 updatedShortcuts.add(si);
3177 }
3178 if (infoUpdated) {
3179 updateItemInDatabase(context, si);
3180 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003181 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3183 if (mUser.equals(widgetInfo.user)
3184 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyal40452cf2016-09-01 15:17:46 -07003185 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003186 widgetInfo.restoreStatus &=
3187 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3188 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003189
3190 // adding this flag ensures that launcher shows 'click to setup'
3191 // if the widget has a config activity. In case there is no config
3192 // activity, it will be marked as 'restored' during bind.
3193 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3194
Sunny Goyal4390ace2014-10-13 11:33:11 -07003195 widgets.add(widgetInfo);
3196 updateItemInDatabase(context, widgetInfo);
3197 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003198 }
3199 }
3200 }
3201
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003202 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3203 if (!removedShortcuts.isEmpty()) {
3204 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003205 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003206
Sunny Goyal4390ace2014-10-13 11:33:11 -07003207 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003208 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003209 mHandler.post(new Runnable() {
3210 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003211 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003212 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003213 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003214 }
3215 }
3216 });
3217 }
3218 }
3219
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003220 final HashSet<String> removedPackages = new HashSet<>();
3221 final HashSet<ComponentName> removedComponents = new HashSet<>();
3222 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003223 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003224 Collections.addAll(removedPackages, packages);
3225
3226 // No need to update the removedComponents as
3227 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003228 } else if (mOp == OP_UPDATE) {
3229 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003230 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003231 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003232 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003233 }
3234 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003235
3236 // Update removedComponents as some components can get removed during package update
3237 for (AppInfo info : removedApps) {
3238 removedComponents.add(info.componentName);
3239 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003240 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003241
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003242 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07003243 deleteItemsFromDatabase(
3244 context, ItemInfoMatcher.ofPackages(removedPackages, mUser));
3245 deleteItemsFromDatabase(
3246 context, ItemInfoMatcher.ofComponents(removedComponents, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003247
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003249 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3250
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003252 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003253 mHandler.post(new Runnable() {
3254 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003255 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003256 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003257 callbacks.bindWorkspaceComponentsRemoved(
3258 removedPackages, removedComponents, mUser);
3259 }
3260 }
3261 });
3262 }
3263
3264 if (!removedApps.isEmpty()) {
3265 // Remove corresponding apps from All-Apps
3266 final Callbacks callbacks = getCallback();
3267 mHandler.post(new Runnable() {
3268 public void run() {
3269 Callbacks cb = getCallback();
3270 if (callbacks == cb && cb != null) {
3271 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003272 }
3273 }
3274 });
Joe Onoratobe386092009-11-17 17:32:16 -08003275 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003276
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003277 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3278 // get widget update signals.
3279 if (!Utilities.ATLEAST_MARSHMALLOW &&
3280 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003281 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003282 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003283 public void run() {
3284 Callbacks cb = getCallback();
3285 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003286 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003287 }
3288 }
3289 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003290 }
3291 }
3292 }
3293
Sunny Goyal10923b32016-07-20 15:42:44 -07003294 /**
3295 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3296 */
3297 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3298 enqueueItemUpdatedTask(new Runnable() {
3299 @Override
3300 public void run() {
3301 info.updateFromDeepShortcutInfo(
3302 fullDetail, LauncherAppState.getInstance().getContext());
3303 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3304 update.add(info);
3305 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3306 }
3307 });
3308 }
3309
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003310 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003311 private final String mPackageName;
3312 private final List<ShortcutInfoCompat> mShortcuts;
3313 private final UserHandleCompat mUser;
3314 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003315
3316 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003317 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003318 mPackageName = packageName;
3319 mShortcuts = shortcuts;
3320 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003321 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003322 }
3323
3324 @Override
3325 public void run() {
3326 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3327
3328 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3329 for (ShortcutInfoCompat shortcut : mShortcuts) {
3330 idsToShortcuts.put(shortcut.getId(), shortcut);
3331 }
3332
3333 // Find ShortcutInfo's that have changed on the workspace.
3334 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3335 for (ItemInfo itemInfo : sBgItemsIdMap) {
3336 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3337 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003338 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3339 && si.user.equals(mUser)) {
Tony Wickham672d07f2016-07-27 18:22:41 -07003340 String shortcutId = si.getDeepShortcutId();
3341 if (idsToShortcuts.containsKey(shortcutId)) {
3342 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3343 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003344 }
3345 }
3346 }
3347
3348 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3349 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3350 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3351 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3352 Context context = LauncherAppState.getInstance().getContext();
3353 for (ShortcutInfoCompat fullDetails : shortcuts) {
3354 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3355 .get(fullDetails.getId());
3356 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003357 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003358 updatedShortcutInfos.add(shortcutInfo);
3359 }
3360 }
3361 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3362
Sunny Goyal50941fb2016-08-04 12:03:52 -07003363 if (mUpdateIdMap) {
3364 // Update the deep shortcut map if the list of ids has changed for an activity.
3365 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3366 bindDeepShortcuts();
3367 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003368 }
3369 }
3370
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003371 /**
3372 * Task to handle changing of lock state of the user
3373 */
3374 private class UserLockStateChangedTask implements Runnable {
3375
3376 private final UserHandleCompat mUser;
3377
3378 public UserLockStateChangedTask(UserHandleCompat user) {
3379 mUser = user;
3380 }
3381
3382 @Override
3383 public void run() {
3384 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3385 Context context = mApp.getContext();
3386
3387 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3388 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003389 List<ShortcutInfoCompat> shortcuts =
3390 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3391 if (mDeepShortcutManager.wasLastCallSuccess()) {
3392 for (ShortcutInfoCompat shortcut : shortcuts) {
3393 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3394 }
3395 } else {
3396 // Shortcut manager can fail due to some race condition when the lock state
3397 // changes too frequently. For the purpose of the update,
3398 // consider it as still locked.
3399 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003400 }
3401 }
3402
3403 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3404 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3405 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3406 for (ItemInfo itemInfo : sBgItemsIdMap) {
3407 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3408 && mUser.equals(itemInfo.user)) {
3409 ShortcutInfo si = (ShortcutInfo) itemInfo;
3410 if (isUserUnlocked) {
3411 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003412 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003413 // We couldn't verify the shortcut during loader. If its no longer available
3414 // (probably due to clear data), delete the workspace item as well
3415 if (shortcut == null) {
3416 deletedShortcutInfos.add(si);
3417 continue;
3418 }
3419 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3420 si.updateFromDeepShortcutInfo(shortcut, context);
3421 } else {
3422 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3423 }
3424 updatedShortcutInfos.add(si);
3425 }
3426 }
3427 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3428 if (!deletedShortcutInfos.isEmpty()) {
3429 deleteItemsFromDatabase(context, deletedShortcutInfos);
3430 }
3431
3432 // Remove shortcut id map for that user
3433 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3434 while (keysIter.hasNext()) {
3435 if (keysIter.next().user.equals(mUser)) {
3436 keysIter.remove();
3437 }
3438 }
3439
3440 if (isUserUnlocked) {
3441 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3442 }
3443 bindDeepShortcuts();
3444 }
3445 }
3446
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003447 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3448 mHandler.post(new Runnable() {
3449 @Override
3450 public void run() {
3451 Callbacks cb = getCallback();
3452 if (callbacks == cb && cb != null) {
3453 callbacks.bindWidgetsModel(model);
3454 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003455 }
3456 });
3457 }
3458
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003459 public void refreshAndBindWidgetsAndShortcuts(
3460 final Callbacks callbacks, final boolean bindFirst) {
3461 runOnWorkerThread(new Runnable() {
3462 @Override
3463 public void run() {
3464 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3465 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003466 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003467 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3468 bindWidgetsModel(callbacks, model);
3469 // update the Widget entries inside DB on the worker thread.
3470 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3471 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003472 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003473 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003474 }
3475
Adam Cohen091440a2015-03-18 14:16:05 -07003476 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003477 UserHandleCompat user) {
3478 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3479 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003480 }
Adam Cohen556f6132014-01-15 15:18:08 -08003481
Adam Cohena28b78e2014-05-20 17:03:04 -07003482 public static boolean isValidPackage(Context context, String packageName,
3483 UserHandleCompat user) {
3484 if (packageName == null) {
3485 return false;
3486 }
3487 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3488 return launcherApps.isPackageEnabledForProfile(packageName, user);
3489 }
3490
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003492 * Make an ShortcutInfo object for a restored application or shortcut item that points
3493 * to a package that is not yet installed on the system.
3494 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003495 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3496 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003497 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003498 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003499
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003500 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003501 // the fallback icon
3502 if (icon == null) {
3503 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3504 } else {
3505 info.setIcon(icon);
3506 }
Sunny Goyal34942622014-08-29 17:20:55 -07003507
3508 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003509 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003510 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003511 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003512 }
Sunny Goyal34942622014-08-29 17:20:55 -07003513 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3514 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003515 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003516 }
Sunny Goyal34942622014-08-29 17:20:55 -07003517 } else {
3518 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3519 }
3520
Winson Chung82b016c2015-05-08 17:00:10 -07003521 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003522 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003523 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003524 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003525 return info;
3526 }
3527
3528 /**
3529 * Make an Intent object for a restored application or shortcut item that points
3530 * to the market page for the item.
3531 */
Adam Cohen091440a2015-03-18 14:16:05 -07003532 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003533 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003534 return getMarketIntent(componentName.getPackageName());
3535 }
3536
3537 static Intent getMarketIntent(String packageName) {
3538 return new Intent(Intent.ACTION_VIEW)
3539 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003540 .scheme("market")
3541 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003542 .appendQueryParameter("id", packageName)
3543 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003544 }
3545
3546 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003547 * Make an ShortcutInfo object for a shortcut that is an application.
3548 *
3549 * If c is not null, then it will be used to fill in missing data like the title and icon.
3550 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003551 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003552 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003553 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003554 if (user == null) {
3555 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003556 return null;
3557 }
3558
Kenny Guyed131872014-04-30 03:02:21 +01003559 ComponentName componentName = intent.getComponent();
3560 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003561 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003562 return null;
3563 }
3564
3565 Intent newIntent = new Intent(intent.getAction(), null);
3566 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3567 newIntent.setComponent(componentName);
3568 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003569 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003570 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3571 return null;
3572 }
3573
3574 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003575 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003576 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003577 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003578 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003579 }
3580
Sunny Goyald09c3702016-04-06 16:18:20 -07003581 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3582 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3583 }
3584
Joe Onorato56d82912010-03-07 14:32:10 -05003585 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003586 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003587 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003588 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003589
Joe Onorato56d82912010-03-07 14:32:10 -05003590 // fall back to the class name of the activity
3591 if (info.title == null) {
3592 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003593 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003594
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003596 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003597 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003598 if (lai != null) {
3599 info.flags = AppInfo.initFlags(lai);
3600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003602 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003603
Sunny Goyal1a745e82014-10-02 15:58:31 -07003604 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003605 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003606 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003607 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003608 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003609 // Non-app shortcuts are only supported for current user.
3610 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003612
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003613 // TODO: If there's an explicit component and we can't install that, delete it.
3614
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003615 loadInfoFromCursor(info, c, iconInfo);
3616 return info;
3617 }
Joe Onorato56d82912010-03-07 14:32:10 -05003618
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003619 /**
3620 * Make an ShortcutInfo object for a shortcut that isn't an application.
3621 */
3622 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3623 info.title = iconInfo.getTitle(c);
3624 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003625 // the fallback icon
3626 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003627 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003628 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003629 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003630 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003632
Sunny Goyal2350bc92014-10-14 16:42:54 -07003633 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003634 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3635 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3636 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3637
Adam Cohend9198822011-11-22 16:42:47 -08003638 if (intent == null) {
3639 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3640 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3641 return null;
3642 }
3643
Joe Onorato0589f0f2010-02-08 13:44:00 -08003644 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003645 ShortcutIconResource iconResource = null;
3646
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003647 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003648 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003649 } else {
3650 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003651 if (extra instanceof ShortcutIconResource) {
3652 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07003653 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003654 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003655 }
3656 }
3657
Michael Jurkac9d95c52011-08-29 14:03:34 -07003658 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003659
Kenny Guyed131872014-04-30 03:02:21 +01003660 // Only support intents for current user for now. Intents sent from other
3661 // users wouldn't get here without intent forwarding anyway.
3662 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003663 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003664 icon = mIconCache.getDefaultIcon(info.user);
3665 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003666 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003667 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003668
Winson Chung82b016c2015-05-08 17:00:10 -07003669 info.title = Utilities.trim(name);
3670 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003671 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003672 info.iconResource = iconResource;
3673
3674 return info;
3675 }
3676
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003678 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 * or make a new one.
3680 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003681 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003682 // See if a placeholder was created for us already
3683 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003684 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003686 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687 folders.put(id, folderInfo);
3688 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003689 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003690 }
3691
Joe Onoratobe386092009-11-17 17:32:16 -08003692
Sunny Goyal651077b2014-06-30 14:15:31 -07003693 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3694 return (provider != null) && (provider.provider != null)
3695 && (provider.provider.getPackageName() != null);
3696 }
3697
Joe Onoratobe386092009-11-17 17:32:16 -08003698 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003699 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003700 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3701 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3702 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3703 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003704 if (mLoaderTask != null) {
3705 mLoaderTask.dumpState();
3706 } else {
3707 Log.d(TAG, "mLoaderTask=null");
3708 }
Joe Onoratobe386092009-11-17 17:32:16 -08003709 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003710
3711 public Callbacks getCallback() {
3712 return mCallbacks != null ? mCallbacks.get() : null;
3713 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003714
3715 /**
3716 * @return {@link FolderInfo} if its already loaded.
3717 */
3718 public FolderInfo findFolderById(Long folderId) {
3719 synchronized (sBgLock) {
3720 return sBgFolders.get(folderId);
3721 }
3722 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003723
Sunny Goyal527c7d32015-08-28 15:19:36 -07003724 @Thunk class DeferredMainThreadExecutor implements Executor {
3725
3726 @Override
3727 public void execute(Runnable command) {
3728 runOnMainThread(command);
3729 }
3730 }
3731
Sunny Goyal756adbc2015-04-16 15:20:51 -07003732 /**
3733 * @return the looper for the worker thread which can be used to start background tasks.
3734 */
3735 public static Looper getWorkerLooper() {
3736 return sWorkerThread.getLooper();
3737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003738}