blob: a812317985e36b68a92569ed77f769301201bab0 [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 Goyale9956a72016-09-01 17:24:47 -070062import com.android.launcher3.model.BgDataModel;
Sunny Goyalf862a262015-12-14 14:27:38 -080063import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalc2936bc2016-09-01 15:50:36 -070064import com.android.launcher3.model.SdCardAvailableReceiver;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070065import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070066import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070067import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070068import com.android.launcher3.shortcuts.DeepShortcutManager;
69import com.android.launcher3.shortcuts.ShortcutInfoCompat;
70import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000071import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070072import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070073import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070074import com.android.launcher3.util.GridOccupancy;
Sunny Goyal40452cf2016-09-01 15:17:46 -070075import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyale2df0622015-04-24 11:27:00 -070076import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070077import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070078import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070079import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070080import com.android.launcher3.util.Preconditions;
Adam Cohen091440a2015-03-18 14:16:05 -070081import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070082import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080083
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.lang.ref.WeakReference;
85import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070086import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070088import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089import java.util.Collections;
90import java.util.Comparator;
91import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070093import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070094import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070095import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070096import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070097import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070098import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100/**
101 * Maintains in-memory state of the Launcher. It is expected that there should be only one
102 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700103 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 */
Kenny Guyed131872014-04-30 03:02:21 +0100105public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100106 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800107 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400108 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700111
Joe Onorato36115782010-06-17 13:28:48 -0400112 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500113 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800114
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk final LauncherAppState mApp;
116 @Thunk final Object mLock = new Object();
117 @Thunk DeferredHandler mHandler = new DeferredHandler();
118 @Thunk LoaderTask mLoaderTask;
119 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700120 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700123 static {
124 sWorkerThread.start();
125 }
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700127
Joe Onoratocc67f472010-06-08 10:54:30 -0700128 // We start off with everything not loaded. After that, we assume that
129 // our monitoring of the package manager provides all updates and we never
130 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700131 private boolean mWorkspaceLoaded;
132 private boolean mAllAppsLoaded;
133 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700134
Sunny Goyal756a28a2015-04-23 17:07:55 -0700135 /**
136 * Set of runnables to be called on the background thread after the workspace binding
137 * is complete.
138 */
139 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
140
Adam Cohen091440a2015-03-18 14:16:05 -0700141 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700145 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800146 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800147
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700148 // Maps all launcher activities to the id's of their shortcuts (if they have any).
149 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
150
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700151 private boolean mHasShortcutHostPermission;
152 // Runnable to check if the shortcuts permission has changed.
153 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
154 @Override
155 public void run() {
156 if (mDeepShortcutsLoaded) {
157 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
158 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
159 mApp.reloadWorkspace();
160 }
161 }
162 }
163 };
164
Sunny Goyale9956a72016-09-01 17:24:47 -0700165 /**
166 * All the static data should be accessed on the background thread, A lock should be acquired
167 * on this object when accessing any data from this model.
168 */
169 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700170
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171 // </ only access in worker thread >
172
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700173 private IconCache mIconCache;
174 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700176 private final LauncherAppsCompat mLauncherApps;
177 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100178
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700180 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700182 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700184 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
185 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700186 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700187 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800188 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200190 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700191 public void bindAppsAdded(ArrayList<Long> newScreens,
192 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700193 ArrayList<ItemInfo> addAnimated,
194 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700196 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
197 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
198 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700199 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700200 public void bindWorkspaceComponentsRemoved(
201 HashSet<String> packageNames, HashSet<ComponentName> components,
202 UserHandleCompat user);
203 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800204 public void notifyWidgetProvidersChanged();
205 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700206 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700207 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700208 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700211 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
212 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800213 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400214 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100215 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700216 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700218 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219
Kenny Guyed131872014-04-30 03:02:21 +0100220 mLauncherApps = LauncherAppsCompat.getInstance(context);
221 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800222 }
223
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700224 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
225 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700226 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700227 if (sWorkerThread.getThreadId() == Process.myTid()) {
228 // If we are on the worker thread, post onto the main handler
229 mHandler.post(r);
230 } else {
231 r.run();
232 }
233 }
234
235 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
236 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700237 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700238 if (sWorkerThread.getThreadId() == Process.myTid()) {
239 r.run();
240 } else {
241 // If we are not on the worker thread, then post to the worker handler
242 sWorker.post(r);
243 }
244 }
245
Sunny Goyal756adbc2015-04-16 15:20:51 -0700246 public void setPackageState(final PackageInstallInfo installInfo) {
247 Runnable updateRunnable = new Runnable() {
248
249 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500250 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700251 synchronized (sBgDataModel) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700252 final HashSet<ItemInfo> updates = new HashSet<>();
253
254 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
255 // Ignore install success events as they are handled by Package add events.
256 return;
257 }
258
Sunny Goyale9956a72016-09-01 17:24:47 -0700259 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700260 if (info instanceof ShortcutInfo) {
261 ShortcutInfo si = (ShortcutInfo) info;
262 ComponentName cn = si.getTargetComponent();
263 if (si.isPromise() && (cn != null)
264 && installInfo.packageName.equals(cn.getPackageName())) {
265 si.setInstallProgress(installInfo.progress);
266
267 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
268 // Mark this info as broken.
269 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
270 }
271 updates.add(si);
272 }
273 }
274 }
275
Sunny Goyale9956a72016-09-01 17:24:47 -0700276 for (LauncherAppWidgetInfo widget : sBgDataModel.appWidgets) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700277 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
278 widget.installProgress = installInfo.progress;
279 updates.add(widget);
280 }
281 }
282
283 if (!updates.isEmpty()) {
284 // Push changes to the callback.
285 Runnable r = new Runnable() {
286 public void run() {
287 Callbacks callbacks = getCallback();
288 if (callbacks != null) {
289 callbacks.bindRestoreItemsChange(updates);
290 }
291 }
292 };
293 mHandler.post(r);
294 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500295 }
296 }
297 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700298 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500299 }
300
Sunny Goyal756adbc2015-04-16 15:20:51 -0700301 /**
302 * Updates the icons and label of all pending icons for the provided package name.
303 */
304 public void updateSessionDisplayInfo(final String packageName) {
305 Runnable updateRunnable = new Runnable() {
306
307 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700308 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700309 synchronized (sBgDataModel) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700310 ArrayList<ShortcutInfo> updates = new ArrayList<>();
311 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700312
Sunny Goyale9956a72016-09-01 17:24:47 -0700313 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700314 if (info instanceof ShortcutInfo) {
315 ShortcutInfo si = (ShortcutInfo) info;
316 ComponentName cn = si.getTargetComponent();
317 if (si.isPromise() && (cn != null)
318 && packageName.equals(cn.getPackageName())) {
319 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
320 // For auto install apps update the icon as well as label.
321 mIconCache.getTitleAndIcon(si,
322 si.promisedIntent, user,
323 si.shouldUseLowResIcon());
324 } else {
325 // Only update the icon for restored apps.
326 si.updateIcon(mIconCache);
327 }
328 updates.add(si);
329 }
330 }
331 }
332
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700333 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700334 }
335 }
336 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700337 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700338 }
339
Adam Cohen76a47a12014-02-05 11:47:43 -0800340 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800341 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800342
343 if (allAppsApps == null) {
344 throw new RuntimeException("allAppsApps must not be null");
345 }
346 if (allAppsApps.isEmpty()) {
347 return;
348 }
349
350 // Process the newly added applications and add them to the database first
351 Runnable r = new Runnable() {
352 public void run() {
353 runOnMainThread(new Runnable() {
354 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800355 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800356 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500357 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800358 }
359 }
360 });
361 }
362 };
363 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700364 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800365
Sunny Goyala9116722015-04-29 13:55:58 -0700366 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800367 int[] xy, int spanX, int spanY) {
368 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700369 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700370
371 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800372 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700373 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700374 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800375 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800376 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700377 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800378 }
379
380 /**
381 * Find a position on the screen for the given size or adds a new screen.
382 * @return screenId and the coordinates for the item.
383 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700384 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700385 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800386 ArrayList<Long> workspaceScreens,
387 ArrayList<Long> addedWorkspaceScreensFinal,
388 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700389 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800390
Sunny Goyala9116722015-04-29 13:55:58 -0700391 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700392 assertWorkspaceLoaded();
Sunny Goyale9956a72016-09-01 17:24:47 -0700393 synchronized (sBgDataModel) {
394 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyala9116722015-04-29 13:55:58 -0700395 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
396 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
397 if (items == null) {
398 items = new ArrayList<>();
399 screenItems.put(info.screenId, items);
400 }
401 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800402 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800403 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 }
405
406 // Find appropriate space for the item.
407 long screenId = 0;
408 int[] cordinates = new int[2];
409 boolean found = false;
410
411 int screenCount = workspaceScreens.size();
412 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700413 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 if (preferredScreenIndex < screenCount) {
415 screenId = workspaceScreens.get(preferredScreenIndex);
416 found = findNextAvailableIconSpaceInScreen(
417 screenItems.get(screenId), cordinates, spanX, spanY);
418 }
419
420 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700421 // Search on any of the screens starting from the first screen.
422 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800423 screenId = workspaceScreens.get(screen);
424 if (findNextAvailableIconSpaceInScreen(
425 screenItems.get(screenId), cordinates, spanX, spanY)) {
426 // We found a space for it
427 found = true;
428 break;
429 }
430 }
431 }
432
433 if (!found) {
434 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700435 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
436 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
437 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438
439 // Save the screen id for binding in the workspace
440 workspaceScreens.add(screenId);
441 addedWorkspaceScreensFinal.add(screenId);
442
443 // If we still can't find an empty space, then God help us all!!!
444 if (!findNextAvailableIconSpaceInScreen(
445 screenItems.get(screenId), cordinates, spanX, spanY)) {
446 throw new RuntimeException("Can't find space to add the item");
447 }
448 }
449 return Pair.create(screenId, cordinates);
450 }
451
452 /**
453 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800454 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700455 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700456 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800457 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800458 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700459 return;
Winson Chung997a9232013-07-24 15:33:46 -0700460 }
Winson Chung64359a52013-07-08 17:17:08 -0700461 // Process the newly added applications and add them to the database first
462 Runnable r = new Runnable() {
463 public void run() {
464 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
465 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
466
Winson Chung76828c82013-08-19 15:43:29 -0700467 // Get the list of workspace screens. We need to append to this list and
468 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
469 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800470 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Sunny Goyale9956a72016-09-01 17:24:47 -0700471 synchronized(sBgDataModel) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800472 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700473 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800474 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700475 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800476 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700477 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800478 }
Winson Chung76828c82013-08-19 15:43:29 -0700479
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700481 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700482 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 long screenId = coords.first;
484 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700485
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700486 ItemInfo itemInfo;
487 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
488 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800489 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700490 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700491 } else {
492 throw new RuntimeException("Unexpected info type");
493 }
Winson Chung94d67682013-09-25 16:29:40 -0700494
Winson Chung64359a52013-07-08 17:17:08 -0700495 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700496 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700497 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700498 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700499 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700500 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700501 }
502 }
503
Winson Chung76828c82013-08-19 15:43:29 -0700504 // Update the workspace screens
505 updateWorkspaceScreenOrder(context, workspaceScreens);
506
Adam Cohen76a47a12014-02-05 11:47:43 -0800507 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700508 runOnMainThread(new Runnable() {
509 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800510 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700511 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700512 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
513 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700514 if (!addedShortcutsFinal.isEmpty()) {
515 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
516 long lastScreenId = info.screenId;
517 for (ItemInfo i : addedShortcutsFinal) {
518 if (i.screenId == lastScreenId) {
519 addAnimated.add(i);
520 } else {
521 addNotAnimated.add(i);
522 }
Winson Chung997a9232013-07-24 15:33:46 -0700523 }
524 }
Winson Chungd64d1762013-08-20 14:37:16 -0700525 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800526 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700527 }
Winson Chung64359a52013-07-08 17:17:08 -0700528 }
Winson Chung997a9232013-07-24 15:33:46 -0700529 });
530 }
Winson Chung64359a52013-07-08 17:17:08 -0700531 }
532 };
533 runOnWorkerThread(r);
534 }
535
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 /**
537 * Adds an item to the DB if it was not created previously, or move it to a new
538 * <container, screen, cellX, cellY>
539 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800540 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700541 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 if (item.container == ItemInfo.NO_ID) {
543 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700544 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400545 } else {
546 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700547 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
549 }
550
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700551 static void checkItemInfoLocked(
552 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700553 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700554 if (modelItem != null && item != modelItem) {
555 // check all the data is consistent
556 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
557 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
558 ShortcutInfo shortcut = (ShortcutInfo) item;
559 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
560 modelShortcut.intent.filterEquals(shortcut.intent) &&
561 modelShortcut.id == shortcut.id &&
562 modelShortcut.itemType == shortcut.itemType &&
563 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700564 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700565 modelShortcut.cellX == shortcut.cellX &&
566 modelShortcut.cellY == shortcut.cellY &&
567 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700568 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700569 // For all intents and purposes, this is the same object
570 return;
571 }
572 }
573
574 // the modelItem needs to match up perfectly with item if our model is
575 // to be consistent with the database-- for now, just require
576 // modelItem == item or the equality check above
577 String msg = "item: " + ((item != null) ? item.toString() : "null") +
578 "modelItem: " +
579 ((modelItem != null) ? modelItem.toString() : "null") +
580 "Error: ItemInfo passed to checkItemInfo doesn't match original";
581 RuntimeException e = new RuntimeException(msg);
582 if (stackTrace != null) {
583 e.setStackTrace(stackTrace);
584 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800585 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700586 }
587 }
588
Michael Jurka816474f2012-06-25 14:49:02 -0700589 static void checkItemInfo(final ItemInfo item) {
590 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
591 final long itemId = item.id;
592 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700593 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700594 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700595 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700596 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700597 }
598 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700599 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700600 }
601
Michael Jurkac9d95c52011-08-29 14:03:34 -0700602 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
603 final ItemInfo item, final String callingFunction) {
604 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700605 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700606 final ContentResolver cr = context.getContentResolver();
607
Adam Cohen487f7dd2012-06-28 18:12:10 -0700608 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700609 Runnable r = new Runnable() {
610 public void run() {
611 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700612 updateItemArrays(item, itemId, stackTrace);
613 }
614 };
615 runOnWorkerThread(r);
616 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700617
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700618 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
619 final ArrayList<ItemInfo> items, final String callingFunction) {
620 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700621
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700622 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
623 Runnable r = new Runnable() {
624 public void run() {
625 ArrayList<ContentProviderOperation> ops =
626 new ArrayList<ContentProviderOperation>();
627 int count = items.size();
628 for (int i = 0; i < count; i++) {
629 ItemInfo item = items.get(i);
630 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700631 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700632 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700633
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700634 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
635 updateItemArrays(item, itemId, stackTrace);
636
637 }
638 try {
639 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
640 } catch (Exception e) {
641 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700642 }
643 }
644 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700645 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700646 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700647
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700648 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
649 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700650 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700651 checkItemInfoLocked(itemId, item, stackTrace);
652
653 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
654 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
655 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700656 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700657 // An items container is being set to a that of an item which is not in
658 // the list of Folders.
659 String msg = "item: " + item + " container being set to: " +
660 item.container + ", not in the list of folders";
661 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700662 }
663 }
664
665 // Items are added/removed from the corresponding FolderInfo elsewhere, such
666 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
667 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700668 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800669 if (modelItem != null &&
670 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
671 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 switch (modelItem.itemType) {
673 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
674 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700675 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700677 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
678 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 }
680 break;
681 default:
682 break;
683 }
684 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700685 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686 }
687 }
688 }
689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700692 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700693 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 item.cellX = cellX;
697 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700698
Winson Chung3d503fb2011-07-13 17:25:49 -0700699 // We store hotseat items in canonical form which is this orientation invariant position
700 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700702 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700704 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700706 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707
708 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700710 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
711 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800712 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700713 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714
Michael Jurkac9d95c52011-08-29 14:03:34 -0700715 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700716 }
717
718 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700719 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
720 * cellX, cellY have already been updated on the ItemInfos.
721 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800722 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700723 final long container, final int screen) {
724
725 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
726 int count = items.size();
727
728 for (int i = 0; i < count; i++) {
729 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700730 item.container = container;
731
732 // We store hotseat items in canonical form which is this orientation invariant position
733 // in the hotseat
734 if (context instanceof Launcher && screen < 0 &&
735 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 item.cellY);
738 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700739 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 }
741
742 final ContentValues values = new ContentValues();
743 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
744 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
745 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800746 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700747 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748
749 contentValues.add(values);
750 }
751 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
752 }
753
754 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700755 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800756 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700757 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700758 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700759 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800760 item.cellX = cellX;
761 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700762 item.spanX = spanX;
763 item.spanY = spanY;
764
765 // We store hotseat items in canonical form which is this orientation invariant position
766 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700768 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700769 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700770 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700772 }
Adam Cohend4844c32011-02-18 19:25:06 -0800773
Adam Cohend4844c32011-02-18 19:25:06 -0800774 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800775 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700776 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
777 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800778 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
780 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800782
Michael Jurka816474f2012-06-25 14:49:02 -0700783 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700785
786 /**
787 * Update an item to the database in a specified container.
788 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700789 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700790 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100791 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700792 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800793 }
794
Sunny Goyal756a28a2015-04-23 17:07:55 -0700795 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700796 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700797 synchronized (mLock) {
798 if (!mHasLoaderCompletedOnce ||
799 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
800 throw new RuntimeException("Trying to add shortcut while loader is running");
801 }
802 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700803 }
804 }
805
Adam Cohend4844c32011-02-18 19:25:06 -0800806 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700807 * Returns true if the shortcuts already exists on the workspace. This must be called after
808 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700810 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
811 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700812 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700813 if (intent.getComponent() != null) {
814 // If component is not null, an intent with null package will produce
815 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700816 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700817 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700818 intentWithPkg = intent.toUri(0);
819 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700820 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700821 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
822 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700823 }
824 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700825 intentWithPkg = intent.toUri(0);
826 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700827 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700828
Sunny Goyale9956a72016-09-01 17:24:47 -0700829 synchronized (sBgDataModel) {
830 for (ItemInfo item : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700831 if (item instanceof ShortcutInfo) {
832 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700833 Intent targetIntent = info.promisedIntent == null
834 ? info.intent : info.promisedIntent;
835 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700836 Intent copyIntent = new Intent(targetIntent);
837 copyIntent.setSourceBounds(intent.getSourceBounds());
838 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700839 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
840 return true;
841 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700842 }
843 }
844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700846 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700847 }
848
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400850 * Add an item to the database in a specified container. Sets the container, screen, cellX and
851 * cellY fields of the item. Also assigns an ID to the item.
852 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700853 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700854 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 item.cellX = cellX;
857 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 // We store hotseat items in canonical form which is this orientation invariant position
859 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
867 final ContentValues values = new ContentValues();
868 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100869 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870
Sunny Goyald2497482015-09-22 18:24:19 -0700871 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
872 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
873
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700874 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700875
Jason Monk8e19cf22014-03-20 15:06:57 -0400876 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700877 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700878 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700879 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880
Sunny Goyale9956a72016-09-01 17:24:47 -0700881 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400882 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700883 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700884 }
885 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700886 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700888 }
889
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700890 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700891 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700893 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700894 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700895 items.add(item);
896 deleteItemsFromDatabase(context, items);
897 }
898
899 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700900 * Removes all the items from the database matching {@param matcher}.
901 */
902 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700903 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700904 }
905
906 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700907 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700908 */
Sunny Goyal40452cf2016-09-01 15:17:46 -0700909 static void deleteItemsFromDatabase(Context context, final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700911 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700912 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700913 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700914 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700915 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700916
Sunny Goyale9956a72016-09-01 17:24:47 -0700917 sBgDataModel.removeItem(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700918 }
919 }
Michael Jurka83df1882011-08-31 20:59:26 -0700920 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700921 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 }
923
924 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700925 * Update the order of the workspace screens in the database. The array list contains
926 * a list of screen ids in the order that they should appear.
927 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700928 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700929 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700930 final ContentResolver cr = context.getContentResolver();
931 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
932
933 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700934 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 while (iter.hasNext()) {
936 long id = iter.next();
937 if (id < 0) {
938 iter.remove();
939 }
940 }
941
942 Runnable r = new Runnable() {
943 @Override
944 public void run() {
Yura085c8532014-02-11 15:15:29 +0000945 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700946 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000947 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700948 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 for (int i = 0; i < count; i++) {
950 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700951 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
953 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000954 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700955 }
Yura085c8532014-02-11 15:15:29 +0000956
957 try {
958 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
959 } catch (Exception ex) {
960 throw new RuntimeException(ex);
961 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700962
Sunny Goyale9956a72016-09-01 17:24:47 -0700963 synchronized (sBgDataModel) {
964 sBgDataModel.workspaceScreens.clear();
965 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700966 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 }
968 };
969 runOnWorkerThread(r);
970 }
971
972 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700973 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 */
Winsonc0b52fe2015-09-09 16:38:15 -0700975 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 final ContentResolver cr = context.getContentResolver();
977
Michael Jurkac9d95c52011-08-29 14:03:34 -0700978 Runnable r = new Runnable() {
979 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700980 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700981 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700982 sBgDataModel.removeItem(info.contents);
983 info.contents.clear();
984
985 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
986 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700987 }
988 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700989 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 }
991
992 /**
993 * Set this as the current Launcher activity object for the loader.
994 */
995 public void initialize(Callbacks callbacks) {
996 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700997 Preconditions.assertUIThread();
998 // Remove any queued UI runnables
999 mHandler.cancelAll();
1000 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001 }
1002 }
1003
Kenny Guyed131872014-04-30 03:02:21 +01001004 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001005 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001006 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001007 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001008 user));
1009 }
1010
1011 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001012 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001013 onPackagesRemoved(user, packageName);
1014 }
1015
1016 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +01001017 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001018 enqueueItemUpdatedTask(new PackageUpdatedTask(op, packages, user));
Kenny Guyed131872014-04-30 03:02:21 +01001019 }
1020
1021 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001022 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001023 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001024 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001025 user));
1026 }
1027
1028 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001029 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001030 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001031 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001032 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001033 }
1034
1035 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001036 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001037 boolean replacing) {
1038 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001039 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001040 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1041 user));
1042 }
Kenny Guyed131872014-04-30 03:02:21 +01001043 }
1044
Kenny Guy44cba692016-01-21 19:50:02 +00001045 @Override
1046 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001047 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001048 PackageUpdatedTask.OP_SUSPEND, packageNames,
1049 user));
1050 }
1051
1052 @Override
1053 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001054 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001055 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1056 user));
1057 }
1058
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001059 @Override
1060 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1061 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001062 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1063 }
1064
1065 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1066 UserHandleCompat user) {
1067 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001068 }
1069
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001070 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001071 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1072 * ACTION_PACKAGE_CHANGED.
1073 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001074 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001075 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001076 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001077
Joe Onorato36115782010-06-17 13:28:48 -04001078 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001079 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001080 // If we have changed locale we need to clear out the labels in all apps/workspace.
1081 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001082 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1083 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001084 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001085 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001086 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1087 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1088 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001089 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1090 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001091 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1092 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1093 enqueueItemUpdatedTask(new PackageUpdatedTask(
1094 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1095 new String[0], user));
1096 }
1097
1098 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1099 // we need to run the state change task again.
1100 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1101 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1102 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1103 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001104 }
Tony Wickham827cef22016-03-17 15:39:39 -07001105 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1106 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001107 }
1108 }
1109
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001110 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001111 resetLoadedState(true, true);
1112
Reena Lee93f824a2011-09-23 17:20:28 -07001113 // Do this here because if the launcher activity is running it will be restarted.
1114 // If it's not running startLoaderFromBackground will merely tell it that it needs
1115 // to reload.
1116 startLoaderFromBackground();
1117 }
1118
Winson Chungf0c6ae02012-03-21 16:10:31 -07001119 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1120 synchronized (mLock) {
1121 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1122 // mWorkspaceLoaded to true later
1123 stopLoaderLocked();
1124 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1125 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001126 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001127 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001128 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001129 }
1130 }
1131
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001132 /**
1133 * When the launcher is in the background, it's possible for it to miss paired
1134 * configuration changes. So whenever we trigger the loader from the background
1135 * tell the launcher that it needs to re-run the loader when it comes back instead
1136 * of doing it now.
1137 */
1138 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001139 Callbacks callbacks = getCallback();
1140 if (callbacks != null) {
1141 // Only actually run the loader if they're not paused.
1142 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001143 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001144 }
1145 }
Joe Onorato36115782010-06-17 13:28:48 -04001146 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001147
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001148 /**
1149 * If there is already a loader task running, tell it to stop.
1150 */
1151 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001152 LoaderTask oldTask = mLoaderTask;
1153 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001154 oldTask.stopLocked();
1155 }
Reena Lee93f824a2011-09-23 17:20:28 -07001156 }
1157
Adam Cohen1a85c582014-09-30 09:48:49 -07001158 public boolean isCurrentCallbacks(Callbacks callbacks) {
1159 return (mCallbacks != null && mCallbacks.get() == callbacks);
1160 }
1161
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001162 /**
1163 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1164 * @return true if the page could be bound synchronously.
1165 */
1166 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001167 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1168 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001169 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001170 // Don't bother to start the thread if we know it's not going to do anything
1171 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001172 final Callbacks oldCallbacks = mCallbacks.get();
1173 // Clear any pending bind-runnables from the synchronized load process.
1174 runOnMainThread(new Runnable() {
1175 public void run() {
1176 oldCallbacks.clearPendingBinds();
1177 }
1178 });
1179
Joe Onorato36115782010-06-17 13:28:48 -04001180 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001181 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001182 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001183 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001184 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1185 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001187 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 } else {
1189 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1190 sWorker.post(mLoaderTask);
1191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001194 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 }
1196
Joe Onorato36115782010-06-17 13:28:48 -04001197 public void stopLoader() {
1198 synchronized (mLock) {
1199 if (mLoaderTask != null) {
1200 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
1202 }
Joe Onorato36115782010-06-17 13:28:48 -04001203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001205 /**
1206 * Loads the workspace screen ids in an ordered list.
1207 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001208 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001209 final ContentResolver contentResolver = context.getContentResolver();
1210 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001211
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001212 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001213 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1214 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001215 }
1216
Joe Onorato36115782010-06-17 13:28:48 -04001217 /**
1218 * Runnable for the thread that loads the contents of the launcher:
1219 * - workspace icons
1220 * - widgets
1221 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001222 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001223 */
1224 private class LoaderTask implements Runnable {
1225 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001226 private int mPageToBindFirst;
1227
Adam Cohen091440a2015-03-18 14:16:05 -07001228 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001229 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001230 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001231
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001232 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001233 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001234 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001235 }
1236
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001237 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001238 mIsLoadingAndBindingWorkspace = true;
1239
Joe Onorato36115782010-06-17 13:28:48 -04001240 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001241 if (DEBUG_LOADERS) {
1242 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001244
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001245 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001246 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001247 synchronized (LoaderTask.this) {
1248 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001249 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001250 }
1251 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 }
1253 }
1254
Joe Onorato36115782010-06-17 13:28:48 -04001255 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001256 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001257 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001258
Joe Onorato36115782010-06-17 13:28:48 -04001259 private void waitForIdle() {
1260 // Wait until the either we're stopped or the other threads are done.
1261 // This way we don't start loading all apps until the workspace has settled
1262 // down.
1263 synchronized (LoaderTask.this) {
1264 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001265
Joe Onorato36115782010-06-17 13:28:48 -04001266 mHandler.postIdle(new Runnable() {
1267 public void run() {
1268 synchronized (LoaderTask.this) {
1269 mLoadAndBindStepFinished = true;
1270 if (DEBUG_LOADERS) {
1271 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001272 }
Joe Onorato36115782010-06-17 13:28:48 -04001273 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001274 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001275 }
Joe Onorato36115782010-06-17 13:28:48 -04001276 });
1277
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001278 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001279 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001280 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1281 // wait no longer than 1sec at a time
1282 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001283 } catch (InterruptedException ex) {
1284 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001285 }
1286 }
Joe Onorato36115782010-06-17 13:28:48 -04001287 if (DEBUG_LOADERS) {
1288 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001289 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001290 + "ms for previous step to finish binding");
1291 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001292 }
Joe Onorato36115782010-06-17 13:28:48 -04001293 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001294
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001295 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001296 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001297 // Ensure that we have a valid page index to load synchronously
1298 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1299 "valid page index");
1300 }
1301 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1302 // Ensure that we don't try and bind a specified page when the pages have not been
1303 // loaded already (we should load everything asynchronously in that case)
1304 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1305 }
1306 synchronized (mLock) {
1307 if (mIsLoaderTaskRunning) {
1308 // Ensure that we are never running the background loading at this point since
1309 // we also touch the background collections
1310 throw new RuntimeException("Error! Background loading is already running");
1311 }
1312 }
1313
1314 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1315 // data structures, we can't allow any other thread to touch that data, but because
1316 // this call is synchronous, we can get away with not locking).
1317
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001318 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001319 // operations from the previous activity. We need to ensure that all queued operations
1320 // are executed before any synchronous binding work is done.
1321 mHandler.flush();
1322
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 // Divide the set of loaded items into those that we are binding synchronously, and
1324 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001325 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1327 // arise from that.
1328 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001329
1330 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001331 }
1332
Joe Onorato36115782010-06-17 13:28:48 -04001333 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001334 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001335 if (mStopped) {
1336 return;
1337 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001338 mIsLoaderTaskRunning = true;
1339 }
Joe Onorato36115782010-06-17 13:28:48 -04001340 // Optimize for end-user experience: if the Launcher is up and // running with the
1341 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1342 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001343 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001344 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001345 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001346
Joe Onorato36115782010-06-17 13:28:48 -04001347 if (mStopped) {
1348 break keep_running;
1349 }
1350
Joe Onorato36115782010-06-17 13:28:48 -04001351 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001352
1353 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001354 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1355 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001356
1357 waitForIdle();
1358
1359 // third step
1360 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1361 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001362 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363
Joe Onorato36115782010-06-17 13:28:48 -04001364 // Clear out this reference, otherwise we end up holding it until all of the
1365 // callback runnables are done.
1366 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367
Joe Onorato36115782010-06-17 13:28:48 -04001368 synchronized (mLock) {
1369 // If we are still the last one to be scheduled, remove ourselves.
1370 if (mLoaderTask == this) {
1371 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001374 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001375 }
Joe Onorato36115782010-06-17 13:28:48 -04001376 }
1377
1378 public void stopLocked() {
1379 synchronized (LoaderTask.this) {
1380 mStopped = true;
1381 this.notify();
1382 }
1383 }
1384
1385 /**
1386 * Gets the callbacks object. If we've been stopped, or if the launcher object
1387 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1388 * object that was around when the deferred message was scheduled, and if there's
1389 * a new Callbacks object around then also return null. This will save us from
1390 * calling onto it with data that will be ignored.
1391 */
1392 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1393 synchronized (mLock) {
1394 if (mStopped) {
1395 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001396 }
Joe Onorato36115782010-06-17 13:28:48 -04001397
1398 if (mCallbacks == null) {
1399 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001400 }
Joe Onorato36115782010-06-17 13:28:48 -04001401
1402 final Callbacks callbacks = mCallbacks.get();
1403 if (callbacks != oldCallbacks) {
1404 return null;
1405 }
1406 if (callbacks == null) {
1407 Log.w(TAG, "no mCallbacks");
1408 return null;
1409 }
1410
1411 return callbacks;
1412 }
1413 }
1414
1415 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001416 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001417 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001418 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001419 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001420
Adam Cohendcd297f2013-06-18 13:13:40 -07001421 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001422 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001423 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001424 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1425 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001426 Log.e(TAG, "Error loading shortcut into hotseat " + item
1427 + " into position (" + item.screenId + ":" + item.cellX + ","
1428 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001429 return false;
1430 }
1431
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001432 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001433 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1434
Adam Cohen2e6da152015-05-06 11:42:25 -07001435 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001436 Log.e(TAG, "Error loading shortcut " + item
1437 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001438 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001439 + ")");
1440 return false;
1441 }
1442
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001443 if (hotseatOccupancy != null) {
1444 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001445 Log.e(TAG, "Error loading shortcut into hotseat " + item
1446 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001447 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001448 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001449 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001450 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001451 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001452 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001453 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001454 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1455 occupancy.cells[(int) item.screenId][0] = true;
1456 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001457 return true;
1458 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001459 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1460 if (!workspaceScreens.contains((Long) item.screenId)) {
1461 // The item has an invalid screen id.
1462 return false;
1463 }
1464 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001465 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001466 return true;
1467 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001468
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001469 final int countX = profile.numColumns;
1470 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001471 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1472 item.cellX < 0 || item.cellY < 0 ||
1473 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1474 Log.e(TAG, "Error loading shortcut " + item
1475 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1476 + item.cellX + "," + item.cellY
1477 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1478 return false;
1479 }
1480
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001481 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001482 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1483 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001484 // Mark the first row as occupied (if the feature is enabled)
1485 // in order to account for the QSB.
1486 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001487 }
1488 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001489 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001490 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001491
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001492 // Check if any workspace icons overlap with each other
1493 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1494 occupancy.markCells(item, true);
1495 return true;
1496 } else {
1497 Log.e(TAG, "Error loading shortcut " + item
1498 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1499 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1500 + ") already occupied");
1501 return false;
1502 }
Joe Onorato36115782010-06-17 13:28:48 -04001503 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001505 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001506 if (LauncherAppState.PROFILE_STARTUP) {
1507 Trace.beginSection("Loading Workspace");
1508 }
Joe Onorato36115782010-06-17 13:28:48 -04001509 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510
Joe Onorato36115782010-06-17 13:28:48 -04001511 final Context context = mContext;
1512 final ContentResolver contentResolver = context.getContentResolver();
1513 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001514 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001515 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001516 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001517 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001518
Winson Chung892c74d2013-08-22 16:15:50 -07001519 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001520 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001521 int countX = profile.numColumns;
1522 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001523
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001524 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001525 try {
1526 ImportDataTask.performImportIfPossible(context);
1527 } catch (Exception e) {
1528 // Migration failed. Clear workspace.
1529 clearDb = true;
1530 }
1531
1532 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001533 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1534 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001535 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001536 }
1537
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001538 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001539 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001540 LauncherSettings.Settings.call(contentResolver,
1541 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001542 }
1543
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001544 Log.d(TAG, "loadWorkspace: loading default favorites");
1545 LauncherSettings.Settings.call(contentResolver,
1546 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001547
Sunny Goyale9956a72016-09-01 17:24:47 -07001548 synchronized (sBgDataModel) {
1549 sBgDataModel.clear();
1550
Sunny Goyal756adbc2015-04-16 15:20:51 -07001551 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001552 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001553 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001554
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001555 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1556 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001557 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001558 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001559 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001560 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001561
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001562 // +1 for the hotseat (it can be larger than the workspace)
1563 // Load workspace in reverse order to ensure that latest items are loaded first (and
1564 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001565 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001566 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001568 try {
1569 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1570 final int intentIndex = c.getColumnIndexOrThrow
1571 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001572 final int containerIndex = c.getColumnIndexOrThrow(
1573 LauncherSettings.Favorites.CONTAINER);
1574 final int itemTypeIndex = c.getColumnIndexOrThrow(
1575 LauncherSettings.Favorites.ITEM_TYPE);
1576 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1577 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001578 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1579 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001580 final int screenIndex = c.getColumnIndexOrThrow(
1581 LauncherSettings.Favorites.SCREEN);
1582 final int cellXIndex = c.getColumnIndexOrThrow
1583 (LauncherSettings.Favorites.CELLX);
1584 final int cellYIndex = c.getColumnIndexOrThrow
1585 (LauncherSettings.Favorites.CELLY);
1586 final int spanXIndex = c.getColumnIndexOrThrow
1587 (LauncherSettings.Favorites.SPANX);
1588 final int spanYIndex = c.getColumnIndexOrThrow(
1589 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001590 final int rankIndex = c.getColumnIndexOrThrow(
1591 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001592 final int restoredIndex = c.getColumnIndexOrThrow(
1593 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001594 final int profileIdIndex = c.getColumnIndexOrThrow(
1595 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001596 final int optionsIndex = c.getColumnIndexOrThrow(
1597 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001598 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001599
Sunny Goyal7f834d22015-04-21 10:10:23 -07001600 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001601 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001602 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001603 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001604 long serialNo = mUserManager.getSerialNumberForUser(user);
1605 allUsers.put(serialNo, user);
1606 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001607
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001608 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001609
1610 // We can only query for shortcuts when the user is unlocked.
1611 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001612 List<ShortcutInfoCompat> pinnedShortcuts =
1613 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1614 if (mDeepShortcutManager.wasLastCallSuccess()) {
1615 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1616 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1617 shortcut);
1618 }
1619 } else {
1620 // Shortcut manager can fail due to some race condition when the
1621 // lock state changes too frequently. For the purpose of the loading
1622 // shortcuts, consider the user is still locked.
1623 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001624 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001625 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001626 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001627 }
1628
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001629 ShortcutInfo info;
1630 String intentDescription;
1631 LauncherAppWidgetInfo appWidgetInfo;
1632 int container;
1633 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001634 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001635 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001636 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001637 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001639 while (!mStopped && c.moveToNext()) {
1640 try {
1641 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001642 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001643 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001644 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001646 switch (itemType) {
1647 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1648 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001649 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001650 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001651 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001652 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001653 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001654 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001655 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001656 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001657 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001658 if (user == null) {
1659 // User has been deleted remove the item.
1660 itemsToRemove.add(id);
1661 continue;
1662 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001663 try {
1664 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001665 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001666 if (cn != null && cn.getPackageName() != null) {
1667 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1668 cn.getPackageName(), user);
1669 boolean validComponent = validPkg &&
1670 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001671 if (validPkg) {
1672 targetPackage = cn.getPackageName();
1673 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001674
1675 if (validComponent) {
1676 if (restored) {
1677 // no special handling necessary for this item
1678 restoredRows.add(id);
1679 restored = false;
1680 }
Kenny Guyff05f432016-01-22 17:48:29 +00001681 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001682 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001683 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001684 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001685 intent = null;
1686 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1687 // We allow auto install apps to have their intent
1688 // updated after an install.
1689 intent = manager.getLaunchIntentForPackage(
1690 cn.getPackageName());
1691 if (intent != null) {
1692 ContentValues values = new ContentValues();
1693 values.put(LauncherSettings.Favorites.INTENT,
1694 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001695 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001696 }
1697 }
1698
1699 if (intent == null) {
1700 // The app is installed but the component is no
1701 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001702 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001703 itemsToRemove.add(id);
1704 continue;
1705 } else {
1706 // no special handling necessary for this item
1707 restoredRows.add(id);
1708 restored = false;
1709 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001710 } else if (restored) {
1711 // Package is not yet available but might be
1712 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001713 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001714
1715 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1716 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001717 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001718 // App restore has started. Update the flag
1719 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1720 ContentValues values = new ContentValues();
1721 values.put(LauncherSettings.Favorites.RESTORED,
1722 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001723 updateItem(id, values);
1724 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1725 // This is a common app. Try to replace this.
1726 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1727 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1728 if (parser.findDefaultApp()) {
1729 // Default app found. Replace it.
1730 intent = parser.parsedIntent;
1731 cn = intent.getComponent();
1732 ContentValues values = parser.parsedValues;
1733 values.put(LauncherSettings.Favorites.RESTORED, 0);
1734 updateItem(id, values);
1735 restored = false;
1736 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001737
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001738 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001739 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001740 itemsToRemove.add(id);
1741 continue;
1742 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001743 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001744 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001745 itemsToRemove.add(id);
1746 continue;
1747 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001748 } else if (PackageManagerHelper.isAppOnSdcard(
1749 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001750 // Package is present but not available.
1751 allowMissingTarget = true;
1752 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1753 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001754 // SdCard is not ready yet. Package might get available,
1755 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001756 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001757 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001758 allowMissingTarget = true;
1759 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001760
1761 } else {
1762 // Do not wait for external media load anymore.
1763 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001764 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001765 itemsToRemove.add(id);
1766 continue;
Winson Chungee055712013-07-30 14:46:24 -07001767 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001768 } else if (cn == null) {
1769 // For shortcuts with no component, keep them as they are
1770 restoredRows.add(id);
1771 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001772 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001774 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001775 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 continue;
1777 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778
Sunny Goyal34b65272015-03-11 16:56:52 -07001779 boolean useLowResIcon = container >= 0 &&
1780 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1781
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001782 if (itemReplaced) {
1783 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001784 info = getAppShortcutInfo(intent, user, null,
1785 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001786 } else {
1787 // Don't replace items for other profiles.
1788 itemsToRemove.add(id);
1789 continue;
1790 }
1791 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001792 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001793 info = getRestoredItemInfo(c, intent,
1794 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001795 intent = getRestoredItemIntent(c, context, intent);
1796 } else {
1797 // Don't restore items for other profiles.
1798 itemsToRemove.add(id);
1799 continue;
1800 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001801 } else if (itemType ==
1802 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001803 info = getAppShortcutInfo(intent, user, c,
1804 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001805 } else if (itemType ==
1806 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001807
1808 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1809 if (unlockedUsers.get(serialNumber)) {
1810 ShortcutInfoCompat pinnedShortcut =
1811 shortcutKeyToPinnedShortcuts.get(key);
1812 if (pinnedShortcut == null) {
1813 // The shortcut is no longer valid.
1814 itemsToRemove.add(id);
1815 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001816 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001817 info = new ShortcutInfo(pinnedShortcut, context);
1818 intent = info.intent;
1819 } else {
1820 // Create a shortcut info in disabled mode for now.
1821 info = new ShortcutInfo();
1822 info.user = user;
1823 info.itemType = itemType;
1824 loadInfoFromCursor(info, c, cursorIconInfo);
1825
1826 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001827 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001828 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001829 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001830
Sunny Goyald09c3702016-04-06 16:18:20 -07001831 // Shortcuts are only available on the primary profile
1832 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1833 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1834 }
1835
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 // App shortcuts that used to be automatically added to Launcher
1837 // didn't always have the correct intent flags set, so do that
1838 // here
1839 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001840 intent.getCategories() != null &&
1841 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001842 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 intent.addFlags(
1844 Intent.FLAG_ACTIVITY_NEW_TASK |
1845 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1846 }
Michael Jurka96879562012-03-22 05:54:33 -07001847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001850 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001852 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001853 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 info.cellX = c.getInt(cellXIndex);
1855 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001856 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001857 info.spanX = 1;
1858 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001859 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001860 if (info.promisedIntent != null) {
1861 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1862 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001863 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001864 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1865 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1866 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001867
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001869 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001870 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001871 break;
1872 }
1873
Sunny Goyal756adbc2015-04-16 15:20:51 -07001874 if (restored) {
1875 ComponentName cn = info.getTargetComponent();
1876 if (cn != null) {
1877 Integer progress = installingPkgs.get(cn.getPackageName());
1878 if (progress != null) {
1879 info.setInstallProgress(progress);
1880 } else {
1881 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1882 }
1883 }
1884 }
1885
Sunny Goyale9956a72016-09-01 17:24:47 -07001886 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001887 } else {
1888 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 }
1890 break;
1891
1892 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1893 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001894 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895
Sunny Goyala508e4f2015-05-21 09:33:57 -07001896 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001897 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001899 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001900 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 folderInfo.cellX = c.getInt(cellXIndex);
1902 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001903 folderInfo.spanX = 1;
1904 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001905 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906
Daniel Sandler8802e962010-05-26 16:28:16 -04001907 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001908 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001909 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001910 break;
1911 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001912 if (restored) {
1913 // no special handling required for restored folders
1914 restoredRows.add(id);
1915 }
1916
Sunny Goyale9956a72016-09-01 17:24:47 -07001917 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 break;
1919
1920 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001921 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001923 boolean customWidget = itemType ==
1924 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1925
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001927 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001928 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001929 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001930 user = allUsers.get(serialNumber);
1931 if (user == null) {
1932 itemsToRemove.add(id);
1933 continue;
1934 }
1935
Sunny Goyalff572272014-07-23 13:58:07 -07001936 final ComponentName component =
1937 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001938
Sunny Goyal651077b2014-06-30 14:15:31 -07001939 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001940 final boolean isIdValid = (restoreStatus &
1941 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001942 final boolean wasProviderReady = (restoreStatus &
1943 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001944
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001945 if (widgetProvidersMap == null) {
1946 widgetProvidersMap = AppWidgetManagerCompat
1947 .getInstance(mContext).getAllProvidersMap();
1948 }
1949 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1950 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001951 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001952 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001953
1954 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001955 if (!isSafeMode && !customWidget &&
1956 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001957 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001958 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 itemsToRemove.add(id);
1960 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001961 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001962 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1963 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001964
Sunny Goyal53f96722015-07-13 19:54:53 -07001965 // The provider is available. So the widget is either
1966 // available or not available. We do not need to track
1967 // any future restore updates.
1968 int status = restoreStatus &
1969 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001970 if (!wasProviderReady) {
1971 // If provider was not previously ready, update the
1972 // status and UI flag.
1973
1974 // Id would be valid only if the widget restore broadcast was received.
1975 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001976 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001977 } else {
1978 status &= ~LauncherAppWidgetInfo
1979 .FLAG_PROVIDER_NOT_READY;
1980 }
1981 }
1982 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001983 } else {
1984 Log.v(TAG, "Widget restore pending id=" + id
1985 + " appWidgetId=" + appWidgetId
1986 + " status =" + restoreStatus);
1987 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001988 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001989 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001990 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001991
1992 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1993 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001994 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001995 // App restore has started. Update the flag
1996 appWidgetInfo.restoreStatus |=
1997 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001998 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001999 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002000 itemsToRemove.add(id);
2001 continue;
2002 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002003
2004 appWidgetInfo.installProgress =
2005 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002006 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002007 if (appWidgetInfo.hasRestoreFlag(
2008 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2009 intentDescription = c.getString(intentIndex);
2010 if (!TextUtils.isEmpty(intentDescription)) {
2011 appWidgetInfo.bindOptions =
2012 Intent.parseUri(intentDescription, 0);
2013 }
2014 }
Sunny Goyalff572272014-07-23 13:58:07 -07002015
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 appWidgetInfo.cellX = c.getInt(cellXIndex);
2019 appWidgetInfo.cellY = c.getInt(cellYIndex);
2020 appWidgetInfo.spanX = c.getInt(spanXIndex);
2021 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002022 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002023
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2025 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2026 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002027 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2028 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 continue;
2030 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002032 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07002034 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002035 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 break;
2037 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002038
Adam Cohen59400422014-03-05 18:07:04 -08002039 if (!customWidget) {
2040 String providerName =
2041 appWidgetInfo.providerName.flattenToString();
2042 if (!providerName.equals(savedProvider) ||
2043 (appWidgetInfo.restoreStatus != restoreStatus)) {
2044 ContentValues values = new ContentValues();
2045 values.put(
2046 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2047 providerName);
2048 values.put(LauncherSettings.Favorites.RESTORED,
2049 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002050 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002051 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002052 }
Sunny Goyale9956a72016-09-01 17:24:47 -07002053 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 }
Joe Onorato36115782010-06-17 13:28:48 -04002055 break;
2056 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002058 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002059 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 }
2061 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002062 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 }
2064
Winson Chungba9c37f2013-08-30 14:11:37 -07002065 // Break early if we've stopped loading
2066 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002067 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002068 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002069 }
2070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002073 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2074 Utilities.createDbSelectionQuery(
2075 LauncherSettings.Favorites._ID, itemsToRemove), null);
2076 if (DEBUG_LOADERS) {
2077 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2078 LauncherSettings.Favorites._ID, itemsToRemove));
2079 }
2080
2081 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002082 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2083 .call(contentResolver,
2084 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2085 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2086 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002087 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
2088 sBgDataModel.folders.remove(folderId);
2089 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002090 }
2091 }
2092
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002093 // Unpin shortcuts that don't exist on the workspace.
2094 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002095 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002096 if (numTimesPinned == null || numTimesPinned.value == 0) {
2097 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2098 mDeepShortcutManager.unpinShortcut(key);
2099 }
2100 }
2101
Sunny Goyal317698b2015-07-29 11:45:41 -07002102 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07002103 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07002104 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2105 int pos = 0;
2106 for (ShortcutInfo info : folder.contents) {
2107 if (info.usingLowResIcon) {
2108 info.updateIcon(mIconCache, false);
2109 }
2110 pos ++;
2111 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2112 break;
2113 }
2114 }
2115 }
2116
Chris Wrenf4d08112014-01-16 18:13:56 -05002117 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002118 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002119 ContentValues values = new ContentValues();
2120 values.put(LauncherSettings.Favorites.RESTORED, 0);
2121 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2122 Utilities.createDbSelectionQuery(
2123 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002124 }
2125
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002126 if (!isSdCardReady && !pendingPackages.isEmpty()) {
2127 context.registerReceiver(
2128 new SdCardAvailableReceiver(
2129 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002130 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002131 null,
2132 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002133 }
2134
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002135 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07002136 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
2137 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002138 long screenId = item.screenId;
2139 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2140 unusedScreens.contains(screenId)) {
2141 unusedScreens.remove(screenId);
2142 }
2143 }
2144
2145 // If there are any empty screens remove them, and update.
2146 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002147 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
2148 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002149 }
2150
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 if (DEBUG_LOADERS) {
2152 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2153 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002154 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002155 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002157
Sunny Goyale2df0622015-04-24 11:27:00 -07002158 for (int i = 0; i < nScreens; i++) {
2159 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002160 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161 line += " | ";
2162 }
Joe Onorato36115782010-06-17 13:28:48 -04002163 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 }
Joe Onorato36115782010-06-17 13:28:48 -04002167 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002168 if (LauncherAppState.PROFILE_STARTUP) {
2169 Trace.endSection();
2170 }
Adam Cohene25af792013-06-06 23:08:25 -07002171 }
2172
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002173 /**
2174 * Partially updates the item without any notification. Must be called on the worker thread.
2175 */
2176 private void updateItem(long itemId, ContentValues update) {
2177 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002178 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002179 update,
2180 BaseColumns._ID + "= ?",
2181 new String[]{Long.toString(itemId)});
2182 }
2183
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 /** Filters the set of items who are directly or indirectly (via another container) on the
2185 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002186 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 ArrayList<ItemInfo> allWorkspaceItems,
2188 ArrayList<ItemInfo> currentScreenItems,
2189 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002190 // Purge any null ItemInfos
2191 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2192 while (iter.hasNext()) {
2193 ItemInfo i = iter.next();
2194 if (i == null) {
2195 iter.remove();
2196 }
2197 }
2198
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 // Order the set of items by their containers first, this allows use to walk through the
2200 // list sequentially, build up a list of containers that are in the specified screen,
2201 // as well as all items in those containers.
2202 Set<Long> itemsOnScreen = new HashSet<Long>();
2203 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2204 @Override
2205 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002206 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 }
2208 });
2209 for (ItemInfo info : allWorkspaceItems) {
2210 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002211 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 currentScreenItems.add(info);
2213 itemsOnScreen.add(info.id);
2214 } else {
2215 otherScreenItems.add(info);
2216 }
2217 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2218 currentScreenItems.add(info);
2219 itemsOnScreen.add(info.id);
2220 } else {
2221 if (itemsOnScreen.contains(info.container)) {
2222 currentScreenItems.add(info);
2223 itemsOnScreen.add(info.id);
2224 } else {
2225 otherScreenItems.add(info);
2226 }
2227 }
2228 }
2229 }
2230
2231 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002232 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 ArrayList<LauncherAppWidgetInfo> appWidgets,
2234 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2235 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236
2237 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002238 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002240 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 currentScreenWidgets.add(widget);
2242 } else {
2243 otherScreenWidgets.add(widget);
2244 }
2245 }
2246 }
2247
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2249 * right) */
2250 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002251 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002252 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002253 final int screenCols = profile.numColumns;
2254 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002256 @Override
2257 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002258 if (lhs.container == rhs.container) {
2259 // Within containers, order by their spatial position in that container
2260 switch ((int) lhs.container) {
2261 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2262 long lr = (lhs.screenId * screenCellCount +
2263 lhs.cellY * screenCols + lhs.cellX);
2264 long rr = (rhs.screenId * screenCellCount +
2265 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002266 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002267 }
2268 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2269 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002270 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002271 }
2272 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002273 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002274 throw new RuntimeException("Unexpected container type when " +
2275 "sorting workspace items.");
2276 }
2277 return 0;
2278 }
2279 } else {
2280 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002281 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002282 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002283 }
2284 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002286
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2288 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 final Runnable r = new Runnable() {
2290 @Override
2291 public void run() {
2292 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2293 if (callbacks != null) {
2294 callbacks.bindScreens(orderedScreens);
2295 }
2296 }
2297 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002298 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 }
2300
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2302 final ArrayList<ItemInfo> workspaceItems,
2303 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002304 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305
2306 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002307 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002309 final int start = i;
2310 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 final Runnable r = new Runnable() {
2312 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002313 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002314 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002315 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002316 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2317 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002318 }
2319 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002321 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002322 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 // Bind the widgets, one at a time
2325 N = appWidgets.size();
2326 for (int i = 0; i < N; i++) {
2327 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2328 final Runnable r = new Runnable() {
2329 public void run() {
2330 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2331 if (callbacks != null) {
2332 callbacks.bindAppWidget(widget);
2333 }
2334 }
2335 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002336 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 }
2338 }
2339
2340 /**
2341 * Binds all loaded data to actual views on the main thread.
2342 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002343 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 final long t = SystemClock.uptimeMillis();
2345 Runnable r;
2346
2347 // Don't use these two variables in any of the callback runnables.
2348 // Otherwise we hold a reference to them.
2349 final Callbacks oldCallbacks = mCallbacks.get();
2350 if (oldCallbacks == null) {
2351 // This launcher has exited and nobody bothered to tell us. Just bail.
2352 Log.w(TAG, "LoaderTask running with no launcher");
2353 return;
2354 }
2355
Winson Chung9b9fb962013-11-15 15:39:34 -08002356 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002357 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2358 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2359 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002360
Sunny Goyale9956a72016-09-01 17:24:47 -07002361 synchronized (sBgDataModel) {
2362 workspaceItems.addAll(sBgDataModel.workspaceItems);
2363 appWidgets.addAll(sBgDataModel.appWidgets);
2364 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002365 }
2366
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002367 final int currentScreen;
2368 {
2369 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2370 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2371 if (currScreen >= orderedScreenIds.size()) {
2372 // There may be no workspace screens (just hotseat items and an empty page).
2373 currScreen = PagedView.INVALID_RESTORE_PAGE;
2374 }
2375 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002376 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002377 final boolean validFirstPage = currentScreen >= 0;
2378 final long currentScreenId =
2379 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002380
Winson Chung9b9fb962013-11-15 15:39:34 -08002381 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002382 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2383 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2384 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2385 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386
Winson Chung9b9fb962013-11-15 15:39:34 -08002387 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002389 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2392 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2393
2394 // Tell the workspace that we're about to start binding items
2395 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002396 public void run() {
2397 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2398 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002399 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002401 }
2402 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002403 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002404 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405
Adam Cohendcd297f2013-06-18 13:13:40 -07002406 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2407
Sunny Goyal527c7d32015-08-28 15:19:36 -07002408 Executor mainExecutor = new DeferredMainThreadExecutor();
2409 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002410 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002412 // In case of validFirstPage, only bind the first screen, and defer binding the
2413 // remaining screens after first onDraw (and an optional the fade animation whichever
2414 // happens later).
2415 // This ensures that the first screen is immediately visible (eg. during rotation)
2416 // In case of !validFirstPage, bind all pages one after other.
2417 final Executor deferredExecutor =
2418 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2419
2420 mainExecutor.execute(new Runnable() {
2421 @Override
2422 public void run() {
2423 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2424 if (callbacks != null) {
2425 callbacks.finishFirstPageBind(
2426 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2427 }
2428 }
2429 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002430
Sunny Goyal44c06432016-04-02 10:56:02 -07002431 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002432
2433 // Tell the workspace that we're done binding items
2434 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002435 public void run() {
2436 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2437 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002438 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002439 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002440
Sunny Goyal639e9062015-08-19 19:17:06 -07002441 mIsLoadingAndBindingWorkspace = false;
2442
2443 // Run all the bind complete runnables after workspace is bound.
2444 if (!mBindCompleteRunnables.isEmpty()) {
2445 synchronized (mBindCompleteRunnables) {
2446 for (final Runnable r : mBindCompleteRunnables) {
2447 runOnWorkerThread(r);
2448 }
2449 mBindCompleteRunnables.clear();
2450 }
2451 }
2452
Winson Chung98e030b2012-05-07 16:01:11 -07002453 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002454 if (DEBUG_LOADERS) {
2455 Log.d(TAG, "bound workspace in "
2456 + (SystemClock.uptimeMillis()-t) + "ms");
2457 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002458
Joe Onorato36115782010-06-17 13:28:48 -04002459 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002461 deferredExecutor.execute(r);
2462
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002463 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002464 r = new Runnable() {
2465 public void run() {
2466 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2467 if (callbacks != null) {
2468 // We are loading synchronously, which means, some of the pages will be
2469 // bound after first draw. Inform the callbacks that page binding is
2470 // not complete, and schedule the remaining pages.
2471 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2472 callbacks.onPageBoundSynchronously(currentScreen);
2473 }
2474 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2475 }
2476 }
2477 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002478 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002479 }
Joe Onorato36115782010-06-17 13:28:48 -04002480 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002481
Joe Onorato36115782010-06-17 13:28:48 -04002482 private void loadAndBindAllApps() {
2483 if (DEBUG_LOADERS) {
2484 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2485 }
2486 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002487 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002488 synchronized (LoaderTask.this) {
2489 if (mStopped) {
2490 return;
2491 }
2492 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002493 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002494 synchronized (LoaderTask.this) {
2495 if (mStopped) {
2496 return;
2497 }
2498 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002499 }
Joe Onorato36115782010-06-17 13:28:48 -04002500 } else {
2501 onlyBindAllApps();
2502 }
2503 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002504
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002505 private void updateIconCache() {
2506 // Ignore packages which have a promise icon.
2507 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002508 synchronized (sBgDataModel) {
2509 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002510 if (info instanceof ShortcutInfo) {
2511 ShortcutInfo si = (ShortcutInfo) info;
2512 if (si.isPromise() && si.getTargetComponent() != null) {
2513 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2514 }
2515 } else if (info instanceof LauncherAppWidgetInfo) {
2516 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2517 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2518 packagesToIgnore.add(lawi.providerName.getPackageName());
2519 }
2520 }
2521 }
2522 }
2523 mIconCache.updateDbIcons(packagesToIgnore);
2524 }
2525
Joe Onorato36115782010-06-17 13:28:48 -04002526 private void onlyBindAllApps() {
2527 final Callbacks oldCallbacks = mCallbacks.get();
2528 if (oldCallbacks == null) {
2529 // This launcher has exited and nobody bothered to tell us. Just bail.
2530 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2531 return;
2532 }
2533
2534 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002535 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002536 final ArrayList<AppInfo> list
2537 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002538 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002539 public void run() {
2540 final long t = SystemClock.uptimeMillis();
2541 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2542 if (callbacks != null) {
2543 callbacks.bindAllApplications(list);
2544 }
2545 if (DEBUG_LOADERS) {
2546 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002547 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002548 }
2549 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002550 };
Tony Wickham80f57872016-06-29 18:12:15 -07002551 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002552 }
2553
Winson Chung64359a52013-07-08 17:17:08 -07002554 private void loadAllApps() {
2555 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002556
Joe Onorato36115782010-06-17 13:28:48 -04002557 final Callbacks oldCallbacks = mCallbacks.get();
2558 if (oldCallbacks == null) {
2559 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002560 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002561 return;
2562 }
2563
Kenny Guyed131872014-04-30 03:02:21 +01002564 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2565
Winson Chung64359a52013-07-08 17:17:08 -07002566 // Clear the list of apps
2567 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002568 for (UserHandleCompat user : profiles) {
2569 // Query for the set of apps
2570 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002571 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002572 if (DEBUG_LOADERS) {
2573 Log.d(TAG, "getActivityList took "
2574 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2575 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2576 }
2577 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002578 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002579 if (apps == null || apps.isEmpty()) {
2580 return;
2581 }
Kenny Guyff05f432016-01-22 17:48:29 +00002582 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002583 // Create the ApplicationInfos
2584 for (int i = 0; i < apps.size(); i++) {
2585 LauncherActivityInfoCompat app = apps.get(i);
2586 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002587 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002588 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002589
Sunny Goyal756a28a2015-04-23 17:07:55 -07002590 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2591 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002592 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002593
2594 @Override
2595 public void run() {
2596 heuristic.processUserApps(apps);
2597 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002598 };
2599 runOnMainThread(new Runnable() {
2600
2601 @Override
2602 public void run() {
2603 // Check isLoadingWorkspace on the UI thread, as it is updated on
2604 // the UI thread.
2605 if (mIsLoadingAndBindingWorkspace) {
2606 synchronized (mBindCompleteRunnables) {
2607 mBindCompleteRunnables.add(r);
2608 }
2609 } else {
2610 runOnWorkerThread(r);
2611 }
2612 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002613 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002614 }
Winson Chung64359a52013-07-08 17:17:08 -07002615 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002616 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002617 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2618 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002619
2620 // Post callback on main thread
2621 mHandler.post(new Runnable() {
2622 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002623
Winson Chung64359a52013-07-08 17:17:08 -07002624 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002625 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002626 if (callbacks != null) {
2627 callbacks.bindAllApplications(added);
2628 if (DEBUG_LOADERS) {
2629 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002630 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002631 }
2632 } else {
2633 Log.i(TAG, "not binding apps: no Launcher activity");
2634 }
2635 }
2636 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002637 // Cleanup any data stored for a deleted user.
2638 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002639 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002640 Log.d(TAG, "Icons processed in "
2641 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002642 }
2643 }
2644
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002645 private void loadAndBindDeepShortcuts() {
2646 if (DEBUG_LOADERS) {
2647 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2648 }
2649 if (!mDeepShortcutsLoaded) {
2650 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002651 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2652 if (mHasShortcutHostPermission) {
2653 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2654 if (mUserManager.isUserUnlocked(user)) {
2655 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2656 .queryForAllShortcuts(user);
2657 updateDeepShortcutMap(null, user, shortcuts);
2658 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002659 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002660 }
2661 synchronized (LoaderTask.this) {
2662 if (mStopped) {
2663 return;
2664 }
2665 mDeepShortcutsLoaded = true;
2666 }
2667 }
Tony Wickham80f57872016-06-29 18:12:15 -07002668 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002669 }
2670
Joe Onoratobe386092009-11-17 17:32:16 -08002671 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002672 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002673 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002674 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2675 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002676 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002677 }
Joe Onorato36115782010-06-17 13:28:48 -04002678 }
2679 }
2680
Sunny Goyal860538d2016-07-20 12:35:16 -07002681 /**
2682 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2683 */
2684 private void updateDeepShortcutMap(
2685 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002686 if (packageName != null) {
2687 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2688 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002689 ComponentKey next = keysIter.next();
2690 if (next.componentName.getPackageName().equals(packageName)
2691 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002692 keysIter.remove();
2693 }
2694 }
2695 }
2696
2697 // Now add the new shortcuts to the map.
2698 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002699 boolean shouldShowInContainer = shortcut.isEnabled()
2700 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2701 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002702 ComponentKey targetComponent
2703 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2704 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2705 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002706 }
2707 }
2708
Tony Wickham80f57872016-06-29 18:12:15 -07002709 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002710 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002711 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002712 @Override
2713 public void run() {
2714 Callbacks callbacks = getCallback();
2715 if (callbacks != null) {
2716 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2717 }
2718 }
Tony Wickham80f57872016-06-29 18:12:15 -07002719 };
2720 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002721 }
2722
Sunny Goyal75b0f552015-05-20 21:57:06 -07002723 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002724 * Refreshes the cached shortcuts if the shortcut permission has changed.
2725 * Current implementation simply reloads the workspace, but it can be optimized to
2726 * use partial updates similar to {@link UserManagerCompat}
2727 */
2728 public void refreshShortcutsIfRequired() {
2729 if (Utilities.isNycMR1OrAbove()) {
2730 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2731 sWorker.post(mShortcutPermissionCheckRunnable);
2732 }
2733 }
2734
2735 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002736 * Called when the icons for packages have been updated in the icon cache.
2737 */
2738 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2739 final Callbacks callbacks = getCallback();
2740 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2741 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2742
2743 // If any package icon has changed (app was updated while launcher was dead),
2744 // update the corresponding shortcuts.
Sunny Goyale9956a72016-09-01 17:24:47 -07002745 synchronized (sBgDataModel) {
2746 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002747 if (info instanceof ShortcutInfo && user.equals(info.user)
2748 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2749 ShortcutInfo si = (ShortcutInfo) info;
2750 ComponentName cn = si.getTargetComponent();
2751 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2752 si.updateIcon(mIconCache);
2753 updatedShortcuts.add(si);
2754 }
2755 }
2756 }
2757 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2758 }
2759
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002760 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002761
2762 if (!updatedApps.isEmpty()) {
2763 mHandler.post(new Runnable() {
2764
2765 public void run() {
2766 Callbacks cb = getCallback();
2767 if (cb != null && callbacks == cb) {
2768 cb.bindAppsUpdated(updatedApps);
2769 }
2770 }
2771 });
2772 }
2773 }
2774
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002775 private void bindUpdatedShortcuts(
2776 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2777 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2778 }
2779
2780 private void bindUpdatedShortcuts(
2781 final ArrayList<ShortcutInfo> updatedShortcuts,
2782 final ArrayList<ShortcutInfo> removedShortcuts,
2783 final UserHandleCompat user) {
2784 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002785 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002786 mHandler.post(new Runnable() {
2787
2788 public void run() {
2789 Callbacks cb = getCallback();
2790 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002791 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002792 }
2793 }
2794 });
2795 }
2796 }
2797
2798 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002799 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002800 }
2801
2802 private class PackageUpdatedTask implements Runnable {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002803 final int mOp;
2804 final String[] mPackages;
2805 final UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002806
2807 public static final int OP_NONE = 0;
2808 public static final int OP_ADD = 1;
2809 public static final int OP_UPDATE = 2;
2810 public static final int OP_REMOVE = 3; // uninstlled
2811 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002812 public static final int OP_SUSPEND = 5; // package suspended
2813 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002814 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002815
Kenny Guyed131872014-04-30 03:02:21 +01002816 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002817 mOp = op;
2818 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002819 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002820 }
2821
2822 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002823 if (!mHasLoaderCompletedOnce) {
2824 // Loader has not yet run.
2825 return;
2826 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002827 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002828
2829 final String[] packages = mPackages;
2830 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002831 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyal40452cf2016-09-01 15:17:46 -07002832 final HashSet<String> packageSet = new HashSet<>(Arrays.asList(packages));
Joe Onorato36115782010-06-17 13:28:48 -04002833 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002834 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002835 for (int i=0; i<N; i++) {
2836 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002837 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002838 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002839 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002840
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002841 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2842 if (heuristic != null) {
2843 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002844 }
Joe Onorato36115782010-06-17 13:28:48 -04002845 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002846 }
Joe Onorato36115782010-06-17 13:28:48 -04002847 case OP_UPDATE:
2848 for (int i=0; i<N; i++) {
2849 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002850 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002851 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002852 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002853 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002854 // Since package was just updated, the target must be available now.
2855 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002856 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002857 case OP_REMOVE: {
2858 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2859 if (heuristic != null) {
2860 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002861 }
Joe Onorato36115782010-06-17 13:28:48 -04002862 for (int i=0; i<N; i++) {
2863 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002864 mIconCache.removeIconsForPkg(packages[i], mUser);
2865 }
2866 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002867 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002868 case OP_UNAVAILABLE:
2869 for (int i=0; i<N; i++) {
2870 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2871 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002872 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002873 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002874 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002875 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002876 case OP_SUSPEND:
2877 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002878 flagOp = mOp == OP_SUSPEND ?
2879 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2880 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002881 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyal40452cf2016-09-01 15:17:46 -07002882 mBgAllAppsList.updateDisabledFlags(
2883 ItemInfoMatcher.ofPackages(packageSet, mUser), flagOp);
Sunny Goyalda891c12016-03-18 18:29:24 -07002884 break;
2885 case OP_USER_AVAILABILITY_CHANGE:
2886 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2887 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2888 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2889 // We want to update all packages for this user.
Sunny Goyal40452cf2016-09-01 15:17:46 -07002890 mBgAllAppsList.updateDisabledFlags(ItemInfoMatcher.ofUser(mUser), flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002891 break;
Joe Onorato36115782010-06-17 13:28:48 -04002892 }
2893
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002894 ArrayList<AppInfo> added = null;
2895 ArrayList<AppInfo> modified = null;
2896 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002897
Adam Cohen487f7dd2012-06-28 18:12:10 -07002898 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002899 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002900 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002901 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002902 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002903 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002904 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002905 }
Winson Chung5d55f332012-07-16 20:45:03 -07002906 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002907 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002908 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002909 }
2910
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002911 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002912
Joe Onorato36115782010-06-17 13:28:48 -04002913 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002914 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002915 for (AppInfo ai : added) {
2916 addedOrUpdatedApps.put(ai.componentName, ai);
2917 }
Joe Onorato36115782010-06-17 13:28:48 -04002918 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002919
Joe Onorato36115782010-06-17 13:28:48 -04002920 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002921 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002922 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002923 for (AppInfo ai : modified) {
2924 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002925 }
2926
Joe Onorato36115782010-06-17 13:28:48 -04002927 mHandler.post(new Runnable() {
2928 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002929 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002930 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002931 callbacks.bindAppsUpdated(modifiedFinal);
2932 }
2933 }
2934 });
2935 }
Winson Chung83892cc2013-05-01 16:53:33 -07002936
Sunny Goyal4390ace2014-10-13 11:33:11 -07002937 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002938 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002939 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2940 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<>();
2941 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002942
Sunny Goyale9956a72016-09-01 17:24:47 -07002943 synchronized (sBgDataModel) {
2944 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002945 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2946 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002947 boolean infoUpdated = false;
2948 boolean shortcutUpdated = false;
2949
2950 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002951 if ((si.iconResource != null)
Sunny Goyal40452cf2016-09-01 15:17:46 -07002952 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal10629b02016-09-01 12:50:11 -07002953 Bitmap icon = LauncherIcons.createIconBitmap(
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002954 si.iconResource.packageName,
2955 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002956 if (icon != null) {
2957 si.setIcon(icon);
2958 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002959 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002960 }
2961 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002962
2963 ComponentName cn = si.getTargetComponent();
Sunny Goyal40452cf2016-09-01 15:17:46 -07002964 if (cn != null && packageSet.contains(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002965 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2966
2967 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002968 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2969 // Auto install icon
2970 PackageManager pm = context.getPackageManager();
2971 ResolveInfo matched = pm.resolveActivity(
2972 new Intent(Intent.ACTION_MAIN)
2973 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2974 PackageManager.MATCH_DEFAULT_ONLY);
2975 if (matched == null) {
2976 // Try to find the best match activity.
2977 Intent intent = pm.getLaunchIntentForPackage(
2978 cn.getPackageName());
2979 if (intent != null) {
2980 cn = intent.getComponent();
2981 appInfo = addedOrUpdatedApps.get(cn);
2982 }
2983
2984 if ((intent == null) || (appInfo == null)) {
2985 removedShortcuts.add(si);
2986 continue;
2987 }
2988 si.promisedIntent = intent;
2989 }
2990 }
2991
2992 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07002993 if (appInfo != null) {
2994 si.flags = appInfo.flags;
2995 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002996
Sunny Goyal756adbc2015-04-16 15:20:51 -07002997 si.intent = si.promisedIntent;
2998 si.promisedIntent = null;
2999 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003000 infoUpdated = true;
3001 si.updateIcon(mIconCache);
3002 }
3003
3004 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3005 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3006 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003007 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003008 si.contentDescription = appInfo.contentDescription;
3009 infoUpdated = true;
3010 }
3011
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003012 int oldDisabledFlags = si.isDisabled;
3013 si.isDisabled = flagOp.apply(si.isDisabled);
3014 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003015 shortcutUpdated = true;
3016 }
3017 }
3018
3019 if (infoUpdated || shortcutUpdated) {
3020 updatedShortcuts.add(si);
3021 }
3022 if (infoUpdated) {
3023 updateItemInDatabase(context, si);
3024 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003025 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003026 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3027 if (mUser.equals(widgetInfo.user)
3028 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyal40452cf2016-09-01 15:17:46 -07003029 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003030 widgetInfo.restoreStatus &=
3031 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3032 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003033
3034 // adding this flag ensures that launcher shows 'click to setup'
3035 // if the widget has a config activity. In case there is no config
3036 // activity, it will be marked as 'restored' during bind.
3037 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3038
Sunny Goyal4390ace2014-10-13 11:33:11 -07003039 widgets.add(widgetInfo);
3040 updateItemInDatabase(context, widgetInfo);
3041 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003042 }
3043 }
3044 }
3045
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003046 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3047 if (!removedShortcuts.isEmpty()) {
3048 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003049 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003050
Sunny Goyal4390ace2014-10-13 11:33:11 -07003051 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003052 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003053 mHandler.post(new Runnable() {
3054 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003055 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003056 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003057 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003058 }
3059 }
3060 });
3061 }
3062 }
3063
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003064 final HashSet<String> removedPackages = new HashSet<>();
3065 final HashSet<ComponentName> removedComponents = new HashSet<>();
3066 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003067 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003068 Collections.addAll(removedPackages, packages);
3069
3070 // No need to update the removedComponents as
3071 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003072 } else if (mOp == OP_UPDATE) {
3073 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003074 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003075 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003076 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003077 }
3078 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003079
3080 // Update removedComponents as some components can get removed during package update
3081 for (AppInfo info : removedApps) {
3082 removedComponents.add(info.componentName);
3083 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003084 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003085
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003086 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07003087 deleteItemsFromDatabase(
3088 context, ItemInfoMatcher.ofPackages(removedPackages, mUser));
3089 deleteItemsFromDatabase(
3090 context, ItemInfoMatcher.ofComponents(removedComponents, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003091
Winson Chungdf95eb12013-10-16 14:57:07 -07003092 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003093 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3094
Winson Chungdf95eb12013-10-16 14:57:07 -07003095 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003096 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003097 mHandler.post(new Runnable() {
3098 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003099 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003100 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003101 callbacks.bindWorkspaceComponentsRemoved(
3102 removedPackages, removedComponents, mUser);
3103 }
3104 }
3105 });
3106 }
3107
3108 if (!removedApps.isEmpty()) {
3109 // Remove corresponding apps from All-Apps
3110 final Callbacks callbacks = getCallback();
3111 mHandler.post(new Runnable() {
3112 public void run() {
3113 Callbacks cb = getCallback();
3114 if (callbacks == cb && cb != null) {
3115 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003116 }
3117 }
3118 });
Joe Onoratobe386092009-11-17 17:32:16 -08003119 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003120
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003121 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3122 // get widget update signals.
3123 if (!Utilities.ATLEAST_MARSHMALLOW &&
3124 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003125 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003126 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003127 public void run() {
3128 Callbacks cb = getCallback();
3129 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003130 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003131 }
3132 }
3133 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003134 }
3135 }
3136 }
3137
Sunny Goyal10923b32016-07-20 15:42:44 -07003138 /**
3139 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3140 */
3141 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3142 enqueueItemUpdatedTask(new Runnable() {
3143 @Override
3144 public void run() {
3145 info.updateFromDeepShortcutInfo(
3146 fullDetail, LauncherAppState.getInstance().getContext());
3147 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3148 update.add(info);
3149 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3150 }
3151 });
3152 }
3153
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003154 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003155 private final String mPackageName;
3156 private final List<ShortcutInfoCompat> mShortcuts;
3157 private final UserHandleCompat mUser;
3158 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003159
3160 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003161 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003162 mPackageName = packageName;
3163 mShortcuts = shortcuts;
3164 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003165 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003166 }
3167
3168 @Override
3169 public void run() {
3170 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3171
3172 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3173 for (ShortcutInfoCompat shortcut : mShortcuts) {
3174 idsToShortcuts.put(shortcut.getId(), shortcut);
3175 }
3176
3177 // Find ShortcutInfo's that have changed on the workspace.
3178 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003179 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003180 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3181 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003182 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3183 && si.user.equals(mUser)) {
Tony Wickham672d07f2016-07-27 18:22:41 -07003184 String shortcutId = si.getDeepShortcutId();
3185 if (idsToShortcuts.containsKey(shortcutId)) {
3186 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3187 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003188 }
3189 }
3190 }
3191
3192 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3193 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3194 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3195 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3196 Context context = LauncherAppState.getInstance().getContext();
3197 for (ShortcutInfoCompat fullDetails : shortcuts) {
3198 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3199 .get(fullDetails.getId());
3200 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003201 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003202 updatedShortcutInfos.add(shortcutInfo);
3203 }
3204 }
3205 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3206
Sunny Goyal50941fb2016-08-04 12:03:52 -07003207 if (mUpdateIdMap) {
3208 // Update the deep shortcut map if the list of ids has changed for an activity.
3209 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3210 bindDeepShortcuts();
3211 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003212 }
3213 }
3214
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003215 /**
3216 * Task to handle changing of lock state of the user
3217 */
3218 private class UserLockStateChangedTask implements Runnable {
3219
3220 private final UserHandleCompat mUser;
3221
3222 public UserLockStateChangedTask(UserHandleCompat user) {
3223 mUser = user;
3224 }
3225
3226 @Override
3227 public void run() {
3228 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3229 Context context = mApp.getContext();
3230
3231 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3232 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003233 List<ShortcutInfoCompat> shortcuts =
3234 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3235 if (mDeepShortcutManager.wasLastCallSuccess()) {
3236 for (ShortcutInfoCompat shortcut : shortcuts) {
3237 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3238 }
3239 } else {
3240 // Shortcut manager can fail due to some race condition when the lock state
3241 // changes too frequently. For the purpose of the update,
3242 // consider it as still locked.
3243 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003244 }
3245 }
3246
3247 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3248 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3249 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003250 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003251 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3252 && mUser.equals(itemInfo.user)) {
3253 ShortcutInfo si = (ShortcutInfo) itemInfo;
3254 if (isUserUnlocked) {
3255 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003256 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003257 // We couldn't verify the shortcut during loader. If its no longer available
3258 // (probably due to clear data), delete the workspace item as well
3259 if (shortcut == null) {
3260 deletedShortcutInfos.add(si);
3261 continue;
3262 }
3263 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3264 si.updateFromDeepShortcutInfo(shortcut, context);
3265 } else {
3266 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3267 }
3268 updatedShortcutInfos.add(si);
3269 }
3270 }
3271 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3272 if (!deletedShortcutInfos.isEmpty()) {
3273 deleteItemsFromDatabase(context, deletedShortcutInfos);
3274 }
3275
3276 // Remove shortcut id map for that user
3277 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3278 while (keysIter.hasNext()) {
3279 if (keysIter.next().user.equals(mUser)) {
3280 keysIter.remove();
3281 }
3282 }
3283
3284 if (isUserUnlocked) {
3285 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3286 }
3287 bindDeepShortcuts();
3288 }
3289 }
3290
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003291 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3292 mHandler.post(new Runnable() {
3293 @Override
3294 public void run() {
3295 Callbacks cb = getCallback();
3296 if (callbacks == cb && cb != null) {
3297 callbacks.bindWidgetsModel(model);
3298 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003299 }
3300 });
3301 }
3302
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003303 public void refreshAndBindWidgetsAndShortcuts(
3304 final Callbacks callbacks, final boolean bindFirst) {
3305 runOnWorkerThread(new Runnable() {
3306 @Override
3307 public void run() {
3308 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3309 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003310 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003311 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3312 bindWidgetsModel(callbacks, model);
3313 // update the Widget entries inside DB on the worker thread.
3314 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3315 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003316 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003317 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003318 }
3319
Adam Cohen091440a2015-03-18 14:16:05 -07003320 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003321 UserHandleCompat user) {
3322 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3323 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003324 }
Adam Cohen556f6132014-01-15 15:18:08 -08003325
Adam Cohena28b78e2014-05-20 17:03:04 -07003326 public static boolean isValidPackage(Context context, String packageName,
3327 UserHandleCompat user) {
3328 if (packageName == null) {
3329 return false;
3330 }
3331 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3332 return launcherApps.isPackageEnabledForProfile(packageName, user);
3333 }
3334
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003336 * Make an ShortcutInfo object for a restored application or shortcut item that points
3337 * to a package that is not yet installed on the system.
3338 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003339 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3340 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003341 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003342 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003343
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003344 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003345 // the fallback icon
3346 if (icon == null) {
3347 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3348 } else {
3349 info.setIcon(icon);
3350 }
Sunny Goyal34942622014-08-29 17:20:55 -07003351
3352 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003353 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003354 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003355 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003356 }
Sunny Goyal34942622014-08-29 17:20:55 -07003357 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3358 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003359 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003360 }
Sunny Goyal34942622014-08-29 17:20:55 -07003361 } else {
3362 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3363 }
3364
Winson Chung82b016c2015-05-08 17:00:10 -07003365 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003366 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003367 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003368 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003369 return info;
3370 }
3371
3372 /**
3373 * Make an Intent object for a restored application or shortcut item that points
3374 * to the market page for the item.
3375 */
Adam Cohen091440a2015-03-18 14:16:05 -07003376 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003377 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003378 return getMarketIntent(componentName.getPackageName());
3379 }
3380
3381 static Intent getMarketIntent(String packageName) {
3382 return new Intent(Intent.ACTION_VIEW)
3383 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003384 .scheme("market")
3385 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003386 .appendQueryParameter("id", packageName)
3387 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003388 }
3389
3390 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003391 * Make an ShortcutInfo object for a shortcut that is an application.
3392 *
3393 * If c is not null, then it will be used to fill in missing data like the title and icon.
3394 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003395 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003396 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003397 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003398 if (user == null) {
3399 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003400 return null;
3401 }
3402
Kenny Guyed131872014-04-30 03:02:21 +01003403 ComponentName componentName = intent.getComponent();
3404 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003405 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003406 return null;
3407 }
3408
3409 Intent newIntent = new Intent(intent.getAction(), null);
3410 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3411 newIntent.setComponent(componentName);
3412 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003413 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003414 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3415 return null;
3416 }
3417
3418 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003419 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003420 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003421 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003422 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003423 }
3424
Sunny Goyald09c3702016-04-06 16:18:20 -07003425 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3426 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3427 }
3428
Joe Onorato56d82912010-03-07 14:32:10 -05003429 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003430 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003431 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003432 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003433
Joe Onorato56d82912010-03-07 14:32:10 -05003434 // fall back to the class name of the activity
3435 if (info.title == null) {
3436 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003437 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003438
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003440 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003441 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003442 if (lai != null) {
3443 info.flags = AppInfo.initFlags(lai);
3444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003446 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003447
Sunny Goyal1a745e82014-10-02 15:58:31 -07003448 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003449 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003450 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003451 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003452 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003453 // Non-app shortcuts are only supported for current user.
3454 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003456
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003457 // TODO: If there's an explicit component and we can't install that, delete it.
3458
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003459 loadInfoFromCursor(info, c, iconInfo);
3460 return info;
3461 }
Joe Onorato56d82912010-03-07 14:32:10 -05003462
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003463 /**
3464 * Make an ShortcutInfo object for a shortcut that isn't an application.
3465 */
3466 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3467 info.title = iconInfo.getTitle(c);
3468 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003469 // the fallback icon
3470 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003471 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003472 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003473 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003474 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003476
Sunny Goyal2350bc92014-10-14 16:42:54 -07003477 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003478 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3479 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3480 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3481
Adam Cohend9198822011-11-22 16:42:47 -08003482 if (intent == null) {
3483 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3484 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3485 return null;
3486 }
3487
Joe Onorato0589f0f2010-02-08 13:44:00 -08003488 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003489 ShortcutIconResource iconResource = null;
3490
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003491 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003492 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003493 } else {
3494 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003495 if (extra instanceof ShortcutIconResource) {
3496 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07003497 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003498 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003499 }
3500 }
3501
Michael Jurkac9d95c52011-08-29 14:03:34 -07003502 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003503
Kenny Guyed131872014-04-30 03:02:21 +01003504 // Only support intents for current user for now. Intents sent from other
3505 // users wouldn't get here without intent forwarding anyway.
3506 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003507 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003508 icon = mIconCache.getDefaultIcon(info.user);
3509 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003510 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003511 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003512
Winson Chung82b016c2015-05-08 17:00:10 -07003513 info.title = Utilities.trim(name);
3514 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003515 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003516 info.iconResource = iconResource;
3517
3518 return info;
3519 }
3520
Sunny Goyal651077b2014-06-30 14:15:31 -07003521 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3522 return (provider != null) && (provider.provider != null)
3523 && (provider.provider.getPackageName() != null);
3524 }
3525
Joe Onoratobe386092009-11-17 17:32:16 -08003526 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003527 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003528 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3529 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3530 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3531 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003532 if (mLoaderTask != null) {
3533 mLoaderTask.dumpState();
3534 } else {
3535 Log.d(TAG, "mLoaderTask=null");
3536 }
Joe Onoratobe386092009-11-17 17:32:16 -08003537 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003538
3539 public Callbacks getCallback() {
3540 return mCallbacks != null ? mCallbacks.get() : null;
3541 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003542
3543 /**
3544 * @return {@link FolderInfo} if its already loaded.
3545 */
3546 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07003547 synchronized (sBgDataModel) {
3548 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003549 }
3550 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003551
Sunny Goyal527c7d32015-08-28 15:19:36 -07003552 @Thunk class DeferredMainThreadExecutor implements Executor {
3553
3554 @Override
3555 public void execute(Runnable command) {
3556 runOnMainThread(command);
3557 }
3558 }
3559
Sunny Goyal756adbc2015-04-16 15:20:51 -07003560 /**
3561 * @return the looper for the worker thread which can be used to start background tasks.
3562 */
3563 public static Looper getWorkerLooper() {
3564 return sWorkerThread.getLooper();
3565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003566}