blob: 955f51f5f266cd4cfe641933f19791558219cfa0 [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 Goyald164b7f2016-10-12 20:49:31 -070064import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalc2936bc2016-09-01 15:50:36 -070065import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyald164b7f2016-10-12 20:49:31 -070066import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070067import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070068import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070069import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070070import com.android.launcher3.shortcuts.DeepShortcutManager;
71import com.android.launcher3.shortcuts.ShortcutInfoCompat;
72import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000073import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070074import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070075import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070076import com.android.launcher3.util.GridOccupancy;
Sunny Goyal40452cf2016-09-01 15:17:46 -070077import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyale2df0622015-04-24 11:27:00 -070078import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070079import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070080import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070081import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070082import com.android.launcher3.util.Preconditions;
Adam Cohen091440a2015-03-18 14:16:05 -070083import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070084import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080085
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.lang.ref.WeakReference;
87import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070088import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070090import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.Collections;
92import java.util.Comparator;
93import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070095import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070097import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070098import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070099import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101/**
102 * Maintains in-memory state of the Launcher. It is expected that there should be only one
103 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700104 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 */
Kenny Guyed131872014-04-30 03:02:21 +0100106public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100107 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800108 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400109 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700112
Joe Onorato36115782010-06-17 13:28:48 -0400113 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500114 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800115
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk final LauncherAppState mApp;
117 @Thunk final Object mLock = new Object();
118 @Thunk DeferredHandler mHandler = new DeferredHandler();
119 @Thunk LoaderTask mLoaderTask;
120 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700121 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124 static {
125 sWorkerThread.start();
126 }
Adam Cohen091440a2015-03-18 14:16:05 -0700127 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700128
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 // We start off with everything not loaded. After that, we assume that
130 // our monitoring of the package manager provides all updates and we never
131 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700132 private boolean mWorkspaceLoaded;
133 private boolean mAllAppsLoaded;
134 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700135
Sunny Goyal756a28a2015-04-23 17:07:55 -0700136 /**
137 * Set of runnables to be called on the background thread after the workspace binding
138 * is complete.
139 */
140 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
141
Adam Cohen091440a2015-03-18 14:16:05 -0700142 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800145 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700146 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800147 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700149 private boolean mHasShortcutHostPermission;
150 // Runnable to check if the shortcuts permission has changed.
151 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
152 @Override
153 public void run() {
154 if (mDeepShortcutsLoaded) {
155 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
156 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
157 mApp.reloadWorkspace();
158 }
159 }
160 }
161 };
162
Sunny Goyale9956a72016-09-01 17:24:47 -0700163 /**
164 * All the static data should be accessed on the background thread, A lock should be acquired
165 * on this object when accessing any data from this model.
166 */
167 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700168
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700169 // </ only access in worker thread >
170
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700171 private IconCache mIconCache;
172 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174 private final LauncherAppsCompat mLauncherApps;
175 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700178 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700180 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700182 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
183 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700184 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700185 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800186 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700189 public void bindAppsAdded(ArrayList<Long> newScreens,
190 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700191 ArrayList<ItemInfo> addAnimated,
192 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700194 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
195 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
196 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700197 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700198 public void bindWorkspaceComponentsRemoved(
199 HashSet<String> packageNames, HashSet<ComponentName> components,
200 UserHandleCompat user);
201 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800202 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700203 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700204 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700205 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700206 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700209 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
210 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800211 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400212 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100213 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700214 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800215 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700216 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217
Kenny Guyed131872014-04-30 03:02:21 +0100218 mLauncherApps = LauncherAppsCompat.getInstance(context);
219 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 }
221
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700222 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
223 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700224 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700225 if (sWorkerThread.getThreadId() == Process.myTid()) {
226 // If we are on the worker thread, post onto the main handler
227 mHandler.post(r);
228 } else {
229 r.run();
230 }
231 }
232
233 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
234 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700235 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700236 if (sWorkerThread.getThreadId() == Process.myTid()) {
237 r.run();
238 } else {
239 // If we are not on the worker thread, then post to the worker handler
240 sWorker.post(r);
241 }
242 }
243
Sunny Goyal756adbc2015-04-16 15:20:51 -0700244 public void setPackageState(final PackageInstallInfo installInfo) {
245 Runnable updateRunnable = new Runnable() {
246
247 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500248 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700249 synchronized (sBgDataModel) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700250 final HashSet<ItemInfo> updates = new HashSet<>();
251
252 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
253 // Ignore install success events as they are handled by Package add events.
254 return;
255 }
256
Sunny Goyale9956a72016-09-01 17:24:47 -0700257 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700258 if (info instanceof ShortcutInfo) {
259 ShortcutInfo si = (ShortcutInfo) info;
260 ComponentName cn = si.getTargetComponent();
261 if (si.isPromise() && (cn != null)
262 && installInfo.packageName.equals(cn.getPackageName())) {
263 si.setInstallProgress(installInfo.progress);
264
265 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
266 // Mark this info as broken.
267 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
268 }
269 updates.add(si);
270 }
271 }
272 }
273
Sunny Goyale9956a72016-09-01 17:24:47 -0700274 for (LauncherAppWidgetInfo widget : sBgDataModel.appWidgets) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700275 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
276 widget.installProgress = installInfo.progress;
277 updates.add(widget);
278 }
279 }
280
281 if (!updates.isEmpty()) {
282 // Push changes to the callback.
283 Runnable r = new Runnable() {
284 public void run() {
285 Callbacks callbacks = getCallback();
286 if (callbacks != null) {
287 callbacks.bindRestoreItemsChange(updates);
288 }
289 }
290 };
291 mHandler.post(r);
292 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500293 }
294 }
295 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700296 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500297 }
298
Sunny Goyal756adbc2015-04-16 15:20:51 -0700299 /**
300 * Updates the icons and label of all pending icons for the provided package name.
301 */
302 public void updateSessionDisplayInfo(final String packageName) {
303 Runnable updateRunnable = new Runnable() {
304
305 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700306 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700307 synchronized (sBgDataModel) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700308 ArrayList<ShortcutInfo> updates = new ArrayList<>();
309 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700310
Sunny Goyale9956a72016-09-01 17:24:47 -0700311 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700312 if (info instanceof ShortcutInfo) {
313 ShortcutInfo si = (ShortcutInfo) info;
314 ComponentName cn = si.getTargetComponent();
315 if (si.isPromise() && (cn != null)
316 && packageName.equals(cn.getPackageName())) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700317 si.updateIcon(mIconCache);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700318 updates.add(si);
319 }
320 }
321 }
322
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700323 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700324 }
325 }
326 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700328 }
329
Adam Cohen76a47a12014-02-05 11:47:43 -0800330 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800331 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800332
333 if (allAppsApps == null) {
334 throw new RuntimeException("allAppsApps must not be null");
335 }
336 if (allAppsApps.isEmpty()) {
337 return;
338 }
339
340 // Process the newly added applications and add them to the database first
341 Runnable r = new Runnable() {
342 public void run() {
343 runOnMainThread(new Runnable() {
344 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800345 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800346 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500347 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800348 }
349 }
350 });
351 }
352 };
353 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700354 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800355
Sunny Goyala9116722015-04-29 13:55:58 -0700356 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800357 int[] xy, int spanX, int spanY) {
358 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700359 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700360
361 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800362 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700363 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700364 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800365 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800366 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700367 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800368 }
369
370 /**
371 * Find a position on the screen for the given size or adds a new screen.
372 * @return screenId and the coordinates for the item.
373 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700374 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700375 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800376 ArrayList<Long> workspaceScreens,
377 ArrayList<Long> addedWorkspaceScreensFinal,
378 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700379 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800380
Sunny Goyala9116722015-04-29 13:55:58 -0700381 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700382 assertWorkspaceLoaded();
Sunny Goyale9956a72016-09-01 17:24:47 -0700383 synchronized (sBgDataModel) {
384 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyala9116722015-04-29 13:55:58 -0700385 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
386 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
387 if (items == null) {
388 items = new ArrayList<>();
389 screenItems.put(info.screenId, items);
390 }
391 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800392 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800394 }
395
396 // Find appropriate space for the item.
397 long screenId = 0;
398 int[] cordinates = new int[2];
399 boolean found = false;
400
401 int screenCount = workspaceScreens.size();
402 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700403 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 if (preferredScreenIndex < screenCount) {
405 screenId = workspaceScreens.get(preferredScreenIndex);
406 found = findNextAvailableIconSpaceInScreen(
407 screenItems.get(screenId), cordinates, spanX, spanY);
408 }
409
410 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700411 // Search on any of the screens starting from the first screen.
412 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800413 screenId = workspaceScreens.get(screen);
414 if (findNextAvailableIconSpaceInScreen(
415 screenItems.get(screenId), cordinates, spanX, spanY)) {
416 // We found a space for it
417 found = true;
418 break;
419 }
420 }
421 }
422
423 if (!found) {
424 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700425 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
426 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
427 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428
429 // Save the screen id for binding in the workspace
430 workspaceScreens.add(screenId);
431 addedWorkspaceScreensFinal.add(screenId);
432
433 // If we still can't find an empty space, then God help us all!!!
434 if (!findNextAvailableIconSpaceInScreen(
435 screenItems.get(screenId), cordinates, spanX, spanY)) {
436 throw new RuntimeException("Can't find space to add the item");
437 }
438 }
439 return Pair.create(screenId, cordinates);
440 }
441
442 /**
443 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800444 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700445 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700446 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800448 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700449 return;
Winson Chung997a9232013-07-24 15:33:46 -0700450 }
Winson Chung64359a52013-07-08 17:17:08 -0700451 // Process the newly added applications and add them to the database first
452 Runnable r = new Runnable() {
453 public void run() {
454 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
455 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
456
Winson Chung76828c82013-08-19 15:43:29 -0700457 // Get the list of workspace screens. We need to append to this list and
458 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
459 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800460 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Sunny Goyale9956a72016-09-01 17:24:47 -0700461 synchronized(sBgDataModel) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700463 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700465 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700467 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 }
Winson Chung76828c82013-08-19 15:43:29 -0700469
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800470 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700471 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700472 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800473 long screenId = coords.first;
474 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700475
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700476 ItemInfo itemInfo;
477 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
478 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800479 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700480 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700481 } else {
482 throw new RuntimeException("Unexpected info type");
483 }
Winson Chung94d67682013-09-25 16:29:40 -0700484
Winson Chung64359a52013-07-08 17:17:08 -0700485 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700486 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700487 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700488 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700489 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700490 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700491 }
492 }
493
Winson Chung76828c82013-08-19 15:43:29 -0700494 // Update the workspace screens
495 updateWorkspaceScreenOrder(context, workspaceScreens);
496
Adam Cohen76a47a12014-02-05 11:47:43 -0800497 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700498 runOnMainThread(new Runnable() {
499 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800500 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700501 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700502 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
503 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700504 if (!addedShortcutsFinal.isEmpty()) {
505 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
506 long lastScreenId = info.screenId;
507 for (ItemInfo i : addedShortcutsFinal) {
508 if (i.screenId == lastScreenId) {
509 addAnimated.add(i);
510 } else {
511 addNotAnimated.add(i);
512 }
Winson Chung997a9232013-07-24 15:33:46 -0700513 }
514 }
Winson Chungd64d1762013-08-20 14:37:16 -0700515 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800516 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700517 }
Winson Chung64359a52013-07-08 17:17:08 -0700518 }
Winson Chung997a9232013-07-24 15:33:46 -0700519 });
520 }
Winson Chung64359a52013-07-08 17:17:08 -0700521 }
522 };
523 runOnWorkerThread(r);
524 }
525
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 /**
527 * Adds an item to the DB if it was not created previously, or move it to a new
528 * <container, screen, cellX, cellY>
529 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800530 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700531 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 if (item.container == ItemInfo.NO_ID) {
533 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700534 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 } else {
536 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700537 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 }
539 }
540
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700541 static void checkItemInfoLocked(
542 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700543 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700544 if (modelItem != null && item != modelItem) {
545 // check all the data is consistent
546 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
547 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
548 ShortcutInfo shortcut = (ShortcutInfo) item;
549 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
550 modelShortcut.intent.filterEquals(shortcut.intent) &&
551 modelShortcut.id == shortcut.id &&
552 modelShortcut.itemType == shortcut.itemType &&
553 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700554 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700555 modelShortcut.cellX == shortcut.cellX &&
556 modelShortcut.cellY == shortcut.cellY &&
557 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700558 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700559 // For all intents and purposes, this is the same object
560 return;
561 }
562 }
563
564 // the modelItem needs to match up perfectly with item if our model is
565 // to be consistent with the database-- for now, just require
566 // modelItem == item or the equality check above
567 String msg = "item: " + ((item != null) ? item.toString() : "null") +
568 "modelItem: " +
569 ((modelItem != null) ? modelItem.toString() : "null") +
570 "Error: ItemInfo passed to checkItemInfo doesn't match original";
571 RuntimeException e = new RuntimeException(msg);
572 if (stackTrace != null) {
573 e.setStackTrace(stackTrace);
574 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800575 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700576 }
577 }
578
Michael Jurka816474f2012-06-25 14:49:02 -0700579 static void checkItemInfo(final ItemInfo item) {
580 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
581 final long itemId = item.id;
582 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700583 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700584 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700585 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700586 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700587 }
588 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700589 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700590 }
591
Michael Jurkac9d95c52011-08-29 14:03:34 -0700592 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
593 final ItemInfo item, final String callingFunction) {
594 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700595 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700596 final ContentResolver cr = context.getContentResolver();
597
Adam Cohen487f7dd2012-06-28 18:12:10 -0700598 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700599 Runnable r = new Runnable() {
600 public void run() {
601 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700602 updateItemArrays(item, itemId, stackTrace);
603 }
604 };
605 runOnWorkerThread(r);
606 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700607
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700608 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
609 final ArrayList<ItemInfo> items, final String callingFunction) {
610 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700611
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700612 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
613 Runnable r = new Runnable() {
614 public void run() {
615 ArrayList<ContentProviderOperation> ops =
616 new ArrayList<ContentProviderOperation>();
617 int count = items.size();
618 for (int i = 0; i < count; i++) {
619 ItemInfo item = items.get(i);
620 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700621 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700622 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700623
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700624 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
625 updateItemArrays(item, itemId, stackTrace);
626
627 }
628 try {
629 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
630 } catch (Exception e) {
631 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700632 }
633 }
634 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700635 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700636 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700637
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700638 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
639 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700640 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700641 checkItemInfoLocked(itemId, item, stackTrace);
642
643 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
644 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
645 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700646 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 // An items container is being set to a that of an item which is not in
648 // the list of Folders.
649 String msg = "item: " + item + " container being set to: " +
650 item.container + ", not in the list of folders";
651 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700652 }
653 }
654
655 // Items are added/removed from the corresponding FolderInfo elsewhere, such
656 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
657 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700658 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800659 if (modelItem != null &&
660 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
661 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700662 switch (modelItem.itemType) {
663 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
664 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700665 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700666 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700667 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
668 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700669 }
670 break;
671 default:
672 break;
673 }
674 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700675 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 }
677 }
678 }
679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700682 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700683 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700684 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400686 item.cellX = cellX;
687 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700688
Winson Chung3d503fb2011-07-13 17:25:49 -0700689 // We store hotseat items in canonical form which is this orientation invariant position
690 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700692 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700693 item.screenId = Launcher.getLauncher(context).getHotseat()
694 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700695 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700696 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700697 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698
699 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700701 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
702 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800703 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705
Michael Jurkac9d95c52011-08-29 14:03:34 -0700706 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700707 }
708
709 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
711 * cellX, cellY have already been updated on the ItemInfos.
712 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800713 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700714 final long container, final int screen) {
715
716 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
717 int count = items.size();
718
719 for (int i = 0; i < count; i++) {
720 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700721 item.container = container;
722
723 // We store hotseat items in canonical form which is this orientation invariant position
724 // in the hotseat
725 if (context instanceof Launcher && screen < 0 &&
726 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700727 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700728 item.cellY);
729 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700731 }
732
733 final ContentValues values = new ContentValues();
734 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
735 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
736 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800737 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700739
740 contentValues.add(values);
741 }
742 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
743 }
744
745 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700746 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800747 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700748 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700749 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700750 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800751 item.cellX = cellX;
752 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700753 item.spanX = spanX;
754 item.spanY = spanY;
755
756 // We store hotseat items in canonical form which is this orientation invariant position
757 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700758 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700759 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700760 item.screenId = Launcher.getLauncher(context).getHotseat()
761 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700762 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700763 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700764 }
Adam Cohend4844c32011-02-18 19:25:06 -0800765
Adam Cohend4844c32011-02-18 19:25:06 -0800766 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800767 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700768 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
769 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800770 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700771 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
772 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800774
Michael Jurka816474f2012-06-25 14:49:02 -0700775 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700776 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700777
778 /**
779 * Update an item to the database in a specified container.
780 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700781 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700782 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100783 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700784 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800785 }
786
Sunny Goyal756a28a2015-04-23 17:07:55 -0700787 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700788 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700789 synchronized (mLock) {
790 if (!mHasLoaderCompletedOnce ||
791 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
792 throw new RuntimeException("Trying to add shortcut while loader is running");
793 }
794 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700795 }
796 }
797
Adam Cohend4844c32011-02-18 19:25:06 -0800798 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700799 * Returns true if the shortcuts already exists on the workspace. This must be called after
800 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700802 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
803 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700804 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700805 if (intent.getComponent() != null) {
806 // If component is not null, an intent with null package will produce
807 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700808 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700809 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700810 intentWithPkg = intent.toUri(0);
811 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700812 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700813 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
814 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700815 }
816 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700817 intentWithPkg = intent.toUri(0);
818 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700819 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700820
Sunny Goyale9956a72016-09-01 17:24:47 -0700821 synchronized (sBgDataModel) {
822 for (ItemInfo item : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700823 if (item instanceof ShortcutInfo) {
824 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700825 Intent targetIntent = info.promisedIntent == null
826 ? info.intent : info.promisedIntent;
827 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700828 Intent copyIntent = new Intent(targetIntent);
829 copyIntent.setSourceBounds(intent.getSourceBounds());
830 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700831 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
832 return true;
833 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700834 }
835 }
836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700838 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700839 }
840
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 * Add an item to the database in a specified container. Sets the container, screen, cellX and
843 * cellY fields of the item. Also assigns an ID to the item.
844 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700845 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700846 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400847 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 item.cellX = cellX;
849 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 // We store hotseat items in canonical form which is this orientation invariant position
851 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700854 item.screenId = Launcher.getLauncher(context).getHotseat()
855 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859
860 final ContentValues values = new ContentValues();
861 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100862 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863
Sunny Goyald2497482015-09-22 18:24:19 -0700864 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
865 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
866
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700867 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700868
Jason Monk8e19cf22014-03-20 15:06:57 -0400869 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700870 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700871 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700872 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873
Sunny Goyale9956a72016-09-01 17:24:47 -0700874 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400875 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700876 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700877 }
878 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700879 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700880 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700881 }
882
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700883 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700884 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700886 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700887 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700888 items.add(item);
889 deleteItemsFromDatabase(context, items);
890 }
891
892 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700893 * Removes all the items from the database matching {@param matcher}.
894 */
895 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700896 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700897 }
898
899 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700900 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700901 */
Sunny Goyal40452cf2016-09-01 15:17:46 -0700902 static void deleteItemsFromDatabase(Context context, final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700904 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700905 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700906 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700907 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700908 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700909
Sunny Goyale9956a72016-09-01 17:24:47 -0700910 sBgDataModel.removeItem(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700911 }
912 }
Michael Jurka83df1882011-08-31 20:59:26 -0700913 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 }
916
917 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700918 * Update the order of the workspace screens in the database. The array list contains
919 * a list of screen ids in the order that they should appear.
920 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700921 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700922 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 final ContentResolver cr = context.getContentResolver();
924 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
925
926 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700927 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700928 while (iter.hasNext()) {
929 long id = iter.next();
930 if (id < 0) {
931 iter.remove();
932 }
933 }
934
935 Runnable r = new Runnable() {
936 @Override
937 public void run() {
Yura085c8532014-02-11 15:15:29 +0000938 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000940 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700941 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700942 for (int i = 0; i < count; i++) {
943 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700944 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
946 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000947 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700948 }
Yura085c8532014-02-11 15:15:29 +0000949
950 try {
951 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
952 } catch (Exception ex) {
953 throw new RuntimeException(ex);
954 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700955
Sunny Goyale9956a72016-09-01 17:24:47 -0700956 synchronized (sBgDataModel) {
957 sBgDataModel.workspaceScreens.clear();
958 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700959 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700960 }
961 };
962 runOnWorkerThread(r);
963 }
964
965 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700966 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 */
Winsonc0b52fe2015-09-09 16:38:15 -0700968 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 final ContentResolver cr = context.getContentResolver();
970
Michael Jurkac9d95c52011-08-29 14:03:34 -0700971 Runnable r = new Runnable() {
972 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700973 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700974 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700975 sBgDataModel.removeItem(info.contents);
976 info.contents.clear();
977
978 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
979 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700980 }
981 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 }
984
985 /**
986 * Set this as the current Launcher activity object for the loader.
987 */
988 public void initialize(Callbacks callbacks) {
989 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700990 Preconditions.assertUIThread();
991 // Remove any queued UI runnables
992 mHandler.cancelAll();
993 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 }
995 }
996
Kenny Guyed131872014-04-30 03:02:21 +0100997 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100998 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100999 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001000 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001001 user));
1002 }
1003
1004 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001005 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001006 onPackagesRemoved(user, packageName);
1007 }
1008
1009 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +01001010 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001011 enqueueItemUpdatedTask(new PackageUpdatedTask(op, packages, user));
Kenny Guyed131872014-04-30 03:02:21 +01001012 }
1013
1014 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001015 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001016 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001017 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001018 user));
1019 }
1020
1021 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001022 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001023 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001024 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001025 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001026 }
1027
1028 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001029 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001030 boolean replacing) {
1031 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001032 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001033 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1034 user));
1035 }
Kenny Guyed131872014-04-30 03:02:21 +01001036 }
1037
Kenny Guy44cba692016-01-21 19:50:02 +00001038 @Override
1039 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001040 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001041 PackageUpdatedTask.OP_SUSPEND, packageNames,
1042 user));
1043 }
1044
1045 @Override
1046 public void onPackagesUnsuspended(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_UNSUSPEND, packageNames,
1049 user));
1050 }
1051
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001052 @Override
1053 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1054 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001055 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1056 }
1057
1058 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1059 UserHandleCompat user) {
1060 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001061 }
1062
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001063 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1065 * ACTION_PACKAGE_CHANGED.
1066 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001067 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001068 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001069 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001070
Joe Onorato36115782010-06-17 13:28:48 -04001071 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001072 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001073 // If we have changed locale we need to clear out the labels in all apps/workspace.
1074 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001075 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1076 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001077 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001078 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001079 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1080 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1081 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001082 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1083 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001084 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1085 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1086 enqueueItemUpdatedTask(new PackageUpdatedTask(
1087 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1088 new String[0], user));
1089 }
1090
1091 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1092 // we need to run the state change task again.
1093 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1094 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1095 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1096 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001097 }
Tony Wickham827cef22016-03-17 15:39:39 -07001098 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1099 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001100 }
1101 }
1102
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001103 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001104 resetLoadedState(true, true);
1105
Reena Lee93f824a2011-09-23 17:20:28 -07001106 // Do this here because if the launcher activity is running it will be restarted.
1107 // If it's not running startLoaderFromBackground will merely tell it that it needs
1108 // to reload.
1109 startLoaderFromBackground();
1110 }
1111
Winson Chungf0c6ae02012-03-21 16:10:31 -07001112 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1113 synchronized (mLock) {
1114 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1115 // mWorkspaceLoaded to true later
1116 stopLoaderLocked();
1117 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1118 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001119 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001120 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001121 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001122 }
1123 }
1124
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001125 /**
1126 * When the launcher is in the background, it's possible for it to miss paired
1127 * configuration changes. So whenever we trigger the loader from the background
1128 * tell the launcher that it needs to re-run the loader when it comes back instead
1129 * of doing it now.
1130 */
1131 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001132 Callbacks callbacks = getCallback();
1133 if (callbacks != null) {
1134 // Only actually run the loader if they're not paused.
1135 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001136 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001137 }
1138 }
Joe Onorato36115782010-06-17 13:28:48 -04001139 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001140
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001141 /**
1142 * If there is already a loader task running, tell it to stop.
1143 */
1144 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001145 LoaderTask oldTask = mLoaderTask;
1146 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001147 oldTask.stopLocked();
1148 }
Reena Lee93f824a2011-09-23 17:20:28 -07001149 }
1150
Adam Cohen1a85c582014-09-30 09:48:49 -07001151 public boolean isCurrentCallbacks(Callbacks callbacks) {
1152 return (mCallbacks != null && mCallbacks.get() == callbacks);
1153 }
1154
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001155 /**
1156 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1157 * @return true if the page could be bound synchronously.
1158 */
1159 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001160 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1161 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001162 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001163 // Don't bother to start the thread if we know it's not going to do anything
1164 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001165 final Callbacks oldCallbacks = mCallbacks.get();
1166 // Clear any pending bind-runnables from the synchronized load process.
1167 runOnMainThread(new Runnable() {
1168 public void run() {
1169 oldCallbacks.clearPendingBinds();
1170 }
1171 });
1172
Joe Onorato36115782010-06-17 13:28:48 -04001173 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001174 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001175 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001176 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001177 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1178 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001180 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001181 } else {
1182 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1183 sWorker.post(mLoaderTask);
1184 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001187 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 }
1189
Joe Onorato36115782010-06-17 13:28:48 -04001190 public void stopLoader() {
1191 synchronized (mLock) {
1192 if (mLoaderTask != null) {
1193 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 }
1195 }
Joe Onorato36115782010-06-17 13:28:48 -04001196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001198 /**
1199 * Loads the workspace screen ids in an ordered list.
1200 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001201 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001202 final ContentResolver contentResolver = context.getContentResolver();
1203 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001204
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001205 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001206 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1207 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001208 }
1209
Joe Onorato36115782010-06-17 13:28:48 -04001210 /**
1211 * Runnable for the thread that loads the contents of the launcher:
1212 * - workspace icons
1213 * - widgets
1214 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001215 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001216 */
1217 private class LoaderTask implements Runnable {
1218 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001219 private int mPageToBindFirst;
1220
Adam Cohen091440a2015-03-18 14:16:05 -07001221 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001222 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001223 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001224
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001225 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001226 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001227 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 }
1229
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001230 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001231 mIsLoadingAndBindingWorkspace = true;
1232
Joe Onorato36115782010-06-17 13:28:48 -04001233 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001234 if (DEBUG_LOADERS) {
1235 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001237
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001238 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001239 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001240 synchronized (LoaderTask.this) {
1241 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001242 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001243 }
1244 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 }
1246 }
1247
Joe Onorato36115782010-06-17 13:28:48 -04001248 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001249 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001250 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001251
Joe Onorato36115782010-06-17 13:28:48 -04001252 private void waitForIdle() {
1253 // Wait until the either we're stopped or the other threads are done.
1254 // This way we don't start loading all apps until the workspace has settled
1255 // down.
1256 synchronized (LoaderTask.this) {
1257 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001258
Joe Onorato36115782010-06-17 13:28:48 -04001259 mHandler.postIdle(new Runnable() {
1260 public void run() {
1261 synchronized (LoaderTask.this) {
1262 mLoadAndBindStepFinished = true;
1263 if (DEBUG_LOADERS) {
1264 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001265 }
Joe Onorato36115782010-06-17 13:28:48 -04001266 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001267 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001268 }
Joe Onorato36115782010-06-17 13:28:48 -04001269 });
1270
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001271 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001272 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001273 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1274 // wait no longer than 1sec at a time
1275 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001276 } catch (InterruptedException ex) {
1277 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001278 }
1279 }
Joe Onorato36115782010-06-17 13:28:48 -04001280 if (DEBUG_LOADERS) {
1281 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001282 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001283 + "ms for previous step to finish binding");
1284 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001285 }
Joe Onorato36115782010-06-17 13:28:48 -04001286 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001287
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001288 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001289 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001290 // Ensure that we have a valid page index to load synchronously
1291 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1292 "valid page index");
1293 }
1294 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1295 // Ensure that we don't try and bind a specified page when the pages have not been
1296 // loaded already (we should load everything asynchronously in that case)
1297 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1298 }
1299 synchronized (mLock) {
1300 if (mIsLoaderTaskRunning) {
1301 // Ensure that we are never running the background loading at this point since
1302 // we also touch the background collections
1303 throw new RuntimeException("Error! Background loading is already running");
1304 }
1305 }
1306
1307 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1308 // data structures, we can't allow any other thread to touch that data, but because
1309 // this call is synchronous, we can get away with not locking).
1310
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001311 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001312 // operations from the previous activity. We need to ensure that all queued operations
1313 // are executed before any synchronous binding work is done.
1314 mHandler.flush();
1315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001316 // Divide the set of loaded items into those that we are binding synchronously, and
1317 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001318 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1320 // arise from that.
1321 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001322
1323 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 }
1325
Joe Onorato36115782010-06-17 13:28:48 -04001326 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001327 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001328 if (mStopped) {
1329 return;
1330 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001331 mIsLoaderTaskRunning = true;
1332 }
Joe Onorato36115782010-06-17 13:28:48 -04001333 // Optimize for end-user experience: if the Launcher is up and // running with the
1334 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1335 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001336 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001337 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001338 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001339
Joe Onorato36115782010-06-17 13:28:48 -04001340 if (mStopped) {
1341 break keep_running;
1342 }
1343
Joe Onorato36115782010-06-17 13:28:48 -04001344 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001345
1346 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001347 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1348 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001349
1350 waitForIdle();
1351
1352 // third step
1353 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1354 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356
Joe Onorato36115782010-06-17 13:28:48 -04001357 // Clear out this reference, otherwise we end up holding it until all of the
1358 // callback runnables are done.
1359 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360
Joe Onorato36115782010-06-17 13:28:48 -04001361 synchronized (mLock) {
1362 // If we are still the last one to be scheduled, remove ourselves.
1363 if (mLoaderTask == this) {
1364 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001367 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001368 }
Joe Onorato36115782010-06-17 13:28:48 -04001369 }
1370
1371 public void stopLocked() {
1372 synchronized (LoaderTask.this) {
1373 mStopped = true;
1374 this.notify();
1375 }
1376 }
1377
1378 /**
1379 * Gets the callbacks object. If we've been stopped, or if the launcher object
1380 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1381 * object that was around when the deferred message was scheduled, and if there's
1382 * a new Callbacks object around then also return null. This will save us from
1383 * calling onto it with data that will be ignored.
1384 */
1385 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1386 synchronized (mLock) {
1387 if (mStopped) {
1388 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001389 }
Joe Onorato36115782010-06-17 13:28:48 -04001390
1391 if (mCallbacks == null) {
1392 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001393 }
Joe Onorato36115782010-06-17 13:28:48 -04001394
1395 final Callbacks callbacks = mCallbacks.get();
1396 if (callbacks != oldCallbacks) {
1397 return null;
1398 }
1399 if (callbacks == null) {
1400 Log.w(TAG, "no mCallbacks");
1401 return null;
1402 }
1403
1404 return callbacks;
1405 }
1406 }
1407
1408 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001409 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001410 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001411 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001412 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001413
Adam Cohendcd297f2013-06-18 13:13:40 -07001414 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001415 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001416 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001417 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1418 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001419 Log.e(TAG, "Error loading shortcut into hotseat " + item
1420 + " into position (" + item.screenId + ":" + item.cellX + ","
1421 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001422 return false;
1423 }
1424
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001425 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001426 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1427
Adam Cohen2e6da152015-05-06 11:42:25 -07001428 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001429 Log.e(TAG, "Error loading shortcut " + item
1430 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001431 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001432 + ")");
1433 return false;
1434 }
1435
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001436 if (hotseatOccupancy != null) {
1437 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001438 Log.e(TAG, "Error loading shortcut into hotseat " + item
1439 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001440 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001441 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001442 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001443 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001444 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001445 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001446 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001447 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1448 occupancy.cells[(int) item.screenId][0] = true;
1449 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001450 return true;
1451 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001452 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1453 if (!workspaceScreens.contains((Long) item.screenId)) {
1454 // The item has an invalid screen id.
1455 return false;
1456 }
1457 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001458 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001459 return true;
1460 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001461
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001462 final int countX = profile.numColumns;
1463 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001464 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1465 item.cellX < 0 || item.cellY < 0 ||
1466 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1467 Log.e(TAG, "Error loading shortcut " + item
1468 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1469 + item.cellX + "," + item.cellY
1470 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1471 return false;
1472 }
1473
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001474 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001475 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1476 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001477 // Mark the first row as occupied (if the feature is enabled)
1478 // in order to account for the QSB.
1479 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001480 }
1481 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001482 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001483 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001484
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001485 // Check if any workspace icons overlap with each other
1486 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1487 occupancy.markCells(item, true);
1488 return true;
1489 } else {
1490 Log.e(TAG, "Error loading shortcut " + item
1491 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1492 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1493 + ") already occupied");
1494 return false;
1495 }
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001498 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001499 if (LauncherAppState.PROFILE_STARTUP) {
1500 Trace.beginSection("Loading Workspace");
1501 }
Joe Onorato36115782010-06-17 13:28:48 -04001502 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001503
Joe Onorato36115782010-06-17 13:28:48 -04001504 final Context context = mContext;
1505 final ContentResolver contentResolver = context.getContentResolver();
1506 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001507 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001508 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001509 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001510 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001511
Winson Chung892c74d2013-08-22 16:15:50 -07001512 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001513 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001514 int countX = profile.numColumns;
1515 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001516
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001517 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001518 try {
1519 ImportDataTask.performImportIfPossible(context);
1520 } catch (Exception e) {
1521 // Migration failed. Clear workspace.
1522 clearDb = true;
1523 }
1524
1525 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001526 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1527 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001528 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001529 }
1530
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001531 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001532 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001533 LauncherSettings.Settings.call(contentResolver,
1534 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001535 }
1536
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001537 Log.d(TAG, "loadWorkspace: loading default favorites");
1538 LauncherSettings.Settings.call(contentResolver,
1539 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001540
Sunny Goyale9956a72016-09-01 17:24:47 -07001541 synchronized (sBgDataModel) {
1542 sBgDataModel.clear();
1543
Sunny Goyal756adbc2015-04-16 15:20:51 -07001544 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001545 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001546 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001547
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001548 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1549 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001550 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001551 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001552 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001553 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001554
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001555 // +1 for the hotseat (it can be larger than the workspace)
1556 // Load workspace in reverse order to ensure that latest items are loaded first (and
1557 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001558 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001559 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001560
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001561 try {
1562 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1563 final int intentIndex = c.getColumnIndexOrThrow
1564 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001565 final int containerIndex = c.getColumnIndexOrThrow(
1566 LauncherSettings.Favorites.CONTAINER);
1567 final int itemTypeIndex = c.getColumnIndexOrThrow(
1568 LauncherSettings.Favorites.ITEM_TYPE);
1569 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1570 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001571 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1572 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001573 final int screenIndex = c.getColumnIndexOrThrow(
1574 LauncherSettings.Favorites.SCREEN);
1575 final int cellXIndex = c.getColumnIndexOrThrow
1576 (LauncherSettings.Favorites.CELLX);
1577 final int cellYIndex = c.getColumnIndexOrThrow
1578 (LauncherSettings.Favorites.CELLY);
1579 final int spanXIndex = c.getColumnIndexOrThrow
1580 (LauncherSettings.Favorites.SPANX);
1581 final int spanYIndex = c.getColumnIndexOrThrow(
1582 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001583 final int rankIndex = c.getColumnIndexOrThrow(
1584 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001585 final int restoredIndex = c.getColumnIndexOrThrow(
1586 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001587 final int profileIdIndex = c.getColumnIndexOrThrow(
1588 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001589 final int optionsIndex = c.getColumnIndexOrThrow(
1590 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001591 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001592
Sunny Goyal7f834d22015-04-21 10:10:23 -07001593 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001594 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001595 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001596 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001597 long serialNo = mUserManager.getSerialNumberForUser(user);
1598 allUsers.put(serialNo, user);
1599 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001600
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001601 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001602
1603 // We can only query for shortcuts when the user is unlocked.
1604 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001605 List<ShortcutInfoCompat> pinnedShortcuts =
1606 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1607 if (mDeepShortcutManager.wasLastCallSuccess()) {
1608 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1609 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1610 shortcut);
1611 }
1612 } else {
1613 // Shortcut manager can fail due to some race condition when the
1614 // lock state changes too frequently. For the purpose of the loading
1615 // shortcuts, consider the user is still locked.
1616 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001617 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001618 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001619 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001620 }
1621
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001622 ShortcutInfo info;
1623 String intentDescription;
1624 LauncherAppWidgetInfo appWidgetInfo;
1625 int container;
1626 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001627 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001628 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001629 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001630 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001632 while (!mStopped && c.moveToNext()) {
1633 try {
1634 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001635 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001636 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001637 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001639 switch (itemType) {
1640 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1641 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001642 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001643 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001644 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001645 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001646 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001647 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001648 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001649 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001650 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001651 if (user == null) {
1652 // User has been deleted remove the item.
1653 itemsToRemove.add(id);
1654 continue;
1655 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 try {
1657 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001658 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001659 if (cn != null && cn.getPackageName() != null) {
1660 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1661 cn.getPackageName(), user);
1662 boolean validComponent = validPkg &&
1663 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001664 if (validPkg) {
1665 targetPackage = cn.getPackageName();
1666 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001667
1668 if (validComponent) {
1669 if (restored) {
1670 // no special handling necessary for this item
1671 restoredRows.add(id);
1672 restored = false;
1673 }
Kenny Guyff05f432016-01-22 17:48:29 +00001674 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001675 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001676 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001677 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001678 intent = null;
1679 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1680 // We allow auto install apps to have their intent
1681 // updated after an install.
1682 intent = manager.getLaunchIntentForPackage(
1683 cn.getPackageName());
1684 if (intent != null) {
1685 ContentValues values = new ContentValues();
1686 values.put(LauncherSettings.Favorites.INTENT,
1687 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001688 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001689 }
1690 }
1691
1692 if (intent == null) {
1693 // The app is installed but the component is no
1694 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001695 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001696 itemsToRemove.add(id);
1697 continue;
1698 } else {
1699 // no special handling necessary for this item
1700 restoredRows.add(id);
1701 restored = false;
1702 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001703 } else if (restored) {
1704 // Package is not yet available but might be
1705 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001706 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001707
1708 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1709 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001710 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001711 // App restore has started. Update the flag
1712 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1713 ContentValues values = new ContentValues();
1714 values.put(LauncherSettings.Favorites.RESTORED,
1715 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001716 updateItem(id, values);
1717 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1718 // This is a common app. Try to replace this.
1719 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1720 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1721 if (parser.findDefaultApp()) {
1722 // Default app found. Replace it.
1723 intent = parser.parsedIntent;
1724 cn = intent.getComponent();
1725 ContentValues values = parser.parsedValues;
1726 values.put(LauncherSettings.Favorites.RESTORED, 0);
1727 updateItem(id, values);
1728 restored = false;
1729 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001730
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001731 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001732 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001733 itemsToRemove.add(id);
1734 continue;
1735 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001736 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001737 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001738 itemsToRemove.add(id);
1739 continue;
1740 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001741 } else if (PackageManagerHelper.isAppOnSdcard(
1742 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001743 // Package is present but not available.
1744 allowMissingTarget = true;
1745 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1746 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001747 // SdCard is not ready yet. Package might get available,
1748 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001749 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001750 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001751 allowMissingTarget = true;
1752 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001753
1754 } else {
1755 // Do not wait for external media load anymore.
1756 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001757 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001758 itemsToRemove.add(id);
1759 continue;
Winson Chungee055712013-07-30 14:46:24 -07001760 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001761 } else if (cn == null) {
1762 // For shortcuts with no component, keep them as they are
1763 restoredRows.add(id);
1764 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001765 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001767 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001768 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769 continue;
1770 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771
Sunny Goyal34b65272015-03-11 16:56:52 -07001772 boolean useLowResIcon = container >= 0 &&
1773 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1774
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001775 if (itemReplaced) {
1776 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001777 info = getAppShortcutInfo(intent, user, null,
1778 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001779 } else {
1780 // Don't replace items for other profiles.
1781 itemsToRemove.add(id);
1782 continue;
1783 }
1784 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001785 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001786 info = getRestoredItemInfo(c, intent,
1787 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001788 intent = getRestoredItemIntent(c, context, intent);
1789 } else {
1790 // Don't restore items for other profiles.
1791 itemsToRemove.add(id);
1792 continue;
1793 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001794 } else if (itemType ==
1795 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001796 info = getAppShortcutInfo(intent, user, c,
1797 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001798 } else if (itemType ==
1799 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001800
1801 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1802 if (unlockedUsers.get(serialNumber)) {
1803 ShortcutInfoCompat pinnedShortcut =
1804 shortcutKeyToPinnedShortcuts.get(key);
1805 if (pinnedShortcut == null) {
1806 // The shortcut is no longer valid.
1807 itemsToRemove.add(id);
1808 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001809 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001810 info = new ShortcutInfo(pinnedShortcut, context);
1811 intent = info.intent;
1812 } else {
1813 // Create a shortcut info in disabled mode for now.
1814 info = new ShortcutInfo();
1815 info.user = user;
1816 info.itemType = itemType;
1817 loadInfoFromCursor(info, c, cursorIconInfo);
1818
1819 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001820 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001821 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001822 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001823
Sunny Goyald09c3702016-04-06 16:18:20 -07001824 // Shortcuts are only available on the primary profile
1825 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1826 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1827 }
1828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 // App shortcuts that used to be automatically added to Launcher
1830 // didn't always have the correct intent flags set, so do that
1831 // here
1832 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001833 intent.getCategories() != null &&
1834 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001835 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 intent.addFlags(
1837 Intent.FLAG_ACTIVITY_NEW_TASK |
1838 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1839 }
Michael Jurka96879562012-03-22 05:54:33 -07001840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001843 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001846 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001847 info.cellX = c.getInt(cellXIndex);
1848 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001849 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001850 info.spanX = 1;
1851 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001852 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001853 if (info.promisedIntent != null) {
1854 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1855 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001856 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001857 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1858 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1859 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001862 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001863 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 break;
1865 }
1866
Sunny Goyal756adbc2015-04-16 15:20:51 -07001867 if (restored) {
1868 ComponentName cn = info.getTargetComponent();
1869 if (cn != null) {
1870 Integer progress = installingPkgs.get(cn.getPackageName());
1871 if (progress != null) {
1872 info.setInstallProgress(progress);
1873 } else {
1874 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1875 }
1876 }
1877 }
1878
Sunny Goyale9956a72016-09-01 17:24:47 -07001879 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001880 } else {
1881 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 }
1883 break;
1884
1885 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1886 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001887 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888
Sunny Goyala508e4f2015-05-21 09:33:57 -07001889 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001890 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001893 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 folderInfo.cellX = c.getInt(cellXIndex);
1895 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001896 folderInfo.spanX = 1;
1897 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001898 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899
Daniel Sandler8802e962010-05-26 16:28:16 -04001900 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001901 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001902 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001903 break;
1904 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001905 if (restored) {
1906 // no special handling required for restored folders
1907 restoredRows.add(id);
1908 }
1909
Sunny Goyale9956a72016-09-01 17:24:47 -07001910 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001911 break;
1912
1913 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001914 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001915 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001916 boolean customWidget = itemType ==
1917 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1918
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001920 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001921 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001922 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001923 user = allUsers.get(serialNumber);
1924 if (user == null) {
1925 itemsToRemove.add(id);
1926 continue;
1927 }
1928
Sunny Goyalff572272014-07-23 13:58:07 -07001929 final ComponentName component =
1930 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001931
Sunny Goyal651077b2014-06-30 14:15:31 -07001932 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001933 final boolean isIdValid = (restoreStatus &
1934 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001935 final boolean wasProviderReady = (restoreStatus &
1936 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001937
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001938 if (widgetProvidersMap == null) {
1939 widgetProvidersMap = AppWidgetManagerCompat
1940 .getInstance(mContext).getAllProvidersMap();
1941 }
1942 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1943 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001944 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001945 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001946
1947 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001948 if (!isSafeMode && !customWidget &&
1949 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001950 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001951 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001952 itemsToRemove.add(id);
1953 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001954 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001955 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1956 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001957
Sunny Goyal53f96722015-07-13 19:54:53 -07001958 // The provider is available. So the widget is either
1959 // available or not available. We do not need to track
1960 // any future restore updates.
1961 int status = restoreStatus &
1962 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001963 if (!wasProviderReady) {
1964 // If provider was not previously ready, update the
1965 // status and UI flag.
1966
1967 // Id would be valid only if the widget restore broadcast was received.
1968 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001969 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001970 } else {
1971 status &= ~LauncherAppWidgetInfo
1972 .FLAG_PROVIDER_NOT_READY;
1973 }
1974 }
1975 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001976 } else {
1977 Log.v(TAG, "Widget restore pending id=" + id
1978 + " appWidgetId=" + appWidgetId
1979 + " status =" + restoreStatus);
1980 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001981 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001982 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001983 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001984
1985 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1986 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001987 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001988 // App restore has started. Update the flag
1989 appWidgetInfo.restoreStatus |=
1990 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001991 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001992 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001993 itemsToRemove.add(id);
1994 continue;
1995 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001996
1997 appWidgetInfo.installProgress =
1998 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001999 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002000 if (appWidgetInfo.hasRestoreFlag(
2001 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2002 intentDescription = c.getString(intentIndex);
2003 if (!TextUtils.isEmpty(intentDescription)) {
2004 appWidgetInfo.bindOptions =
2005 Intent.parseUri(intentDescription, 0);
2006 }
2007 }
Sunny Goyalff572272014-07-23 13:58:07 -07002008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002010 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 appWidgetInfo.cellX = c.getInt(cellXIndex);
2012 appWidgetInfo.cellY = c.getInt(cellYIndex);
2013 appWidgetInfo.spanX = c.getInt(spanXIndex);
2014 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002015 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002016
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2018 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2019 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002020 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2021 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 continue;
2023 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002025 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07002027 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002028 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 break;
2030 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002031
Adam Cohen59400422014-03-05 18:07:04 -08002032 if (!customWidget) {
2033 String providerName =
2034 appWidgetInfo.providerName.flattenToString();
2035 if (!providerName.equals(savedProvider) ||
2036 (appWidgetInfo.restoreStatus != restoreStatus)) {
2037 ContentValues values = new ContentValues();
2038 values.put(
2039 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2040 providerName);
2041 values.put(LauncherSettings.Favorites.RESTORED,
2042 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002043 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002044 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002045 }
Sunny Goyale9956a72016-09-01 17:24:47 -07002046 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 }
Joe Onorato36115782010-06-17 13:28:48 -04002048 break;
2049 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002051 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002052 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 }
2054 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002055 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002056 }
2057
Winson Chungba9c37f2013-08-30 14:11:37 -07002058 // Break early if we've stopped loading
2059 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002060 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002061 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002062 }
2063
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002066 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2067 Utilities.createDbSelectionQuery(
2068 LauncherSettings.Favorites._ID, itemsToRemove), null);
2069 if (DEBUG_LOADERS) {
2070 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2071 LauncherSettings.Favorites._ID, itemsToRemove));
2072 }
2073
2074 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002075 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2076 .call(contentResolver,
2077 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2078 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2079 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002080 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
2081 sBgDataModel.folders.remove(folderId);
2082 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002083 }
2084 }
2085
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002086 // Unpin shortcuts that don't exist on the workspace.
2087 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002088 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002089 if (numTimesPinned == null || numTimesPinned.value == 0) {
2090 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2091 mDeepShortcutManager.unpinShortcut(key);
2092 }
2093 }
2094
Sunny Goyal317698b2015-07-29 11:45:41 -07002095 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07002096 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07002097 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2098 int pos = 0;
2099 for (ShortcutInfo info : folder.contents) {
2100 if (info.usingLowResIcon) {
2101 info.updateIcon(mIconCache, false);
2102 }
2103 pos ++;
2104 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2105 break;
2106 }
2107 }
2108 }
2109
Chris Wrenf4d08112014-01-16 18:13:56 -05002110 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002111 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002112 ContentValues values = new ContentValues();
2113 values.put(LauncherSettings.Favorites.RESTORED, 0);
2114 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2115 Utilities.createDbSelectionQuery(
2116 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002117 }
2118
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002119 if (!isSdCardReady && !pendingPackages.isEmpty()) {
2120 context.registerReceiver(
2121 new SdCardAvailableReceiver(
2122 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002123 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002124 null,
2125 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002126 }
2127
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002128 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07002129 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
2130 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002131 long screenId = item.screenId;
2132 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2133 unusedScreens.contains(screenId)) {
2134 unusedScreens.remove(screenId);
2135 }
2136 }
2137
2138 // If there are any empty screens remove them, and update.
2139 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002140 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
2141 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 }
2143
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 if (DEBUG_LOADERS) {
2145 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2146 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002147 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002148 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002150
Sunny Goyale2df0622015-04-24 11:27:00 -07002151 for (int i = 0; i < nScreens; i++) {
2152 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002153 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 line += " | ";
2155 }
Joe Onorato36115782010-06-17 13:28:48 -04002156 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002158 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002159 }
Joe Onorato36115782010-06-17 13:28:48 -04002160 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002161 if (LauncherAppState.PROFILE_STARTUP) {
2162 Trace.endSection();
2163 }
Adam Cohene25af792013-06-06 23:08:25 -07002164 }
2165
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002166 /**
2167 * Partially updates the item without any notification. Must be called on the worker thread.
2168 */
2169 private void updateItem(long itemId, ContentValues update) {
2170 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002171 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002172 update,
2173 BaseColumns._ID + "= ?",
2174 new String[]{Long.toString(itemId)});
2175 }
2176
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 /** Filters the set of items who are directly or indirectly (via another container) on the
2178 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002179 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 ArrayList<ItemInfo> allWorkspaceItems,
2181 ArrayList<ItemInfo> currentScreenItems,
2182 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002183 // Purge any null ItemInfos
2184 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2185 while (iter.hasNext()) {
2186 ItemInfo i = iter.next();
2187 if (i == null) {
2188 iter.remove();
2189 }
2190 }
2191
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002192 // Order the set of items by their containers first, this allows use to walk through the
2193 // list sequentially, build up a list of containers that are in the specified screen,
2194 // as well as all items in those containers.
2195 Set<Long> itemsOnScreen = new HashSet<Long>();
2196 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2197 @Override
2198 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002199 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 }
2201 });
2202 for (ItemInfo info : allWorkspaceItems) {
2203 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002204 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205 currentScreenItems.add(info);
2206 itemsOnScreen.add(info.id);
2207 } else {
2208 otherScreenItems.add(info);
2209 }
2210 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2211 currentScreenItems.add(info);
2212 itemsOnScreen.add(info.id);
2213 } else {
2214 if (itemsOnScreen.contains(info.container)) {
2215 currentScreenItems.add(info);
2216 itemsOnScreen.add(info.id);
2217 } else {
2218 otherScreenItems.add(info);
2219 }
2220 }
2221 }
2222 }
2223
2224 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002225 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 ArrayList<LauncherAppWidgetInfo> appWidgets,
2227 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2228 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229
2230 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002231 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002233 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 currentScreenWidgets.add(widget);
2235 } else {
2236 otherScreenWidgets.add(widget);
2237 }
2238 }
2239 }
2240
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2242 * right) */
2243 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002244 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002245 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002246 final int screenCols = profile.numColumns;
2247 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002249 @Override
2250 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002251 if (lhs.container == rhs.container) {
2252 // Within containers, order by their spatial position in that container
2253 switch ((int) lhs.container) {
2254 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2255 long lr = (lhs.screenId * screenCellCount +
2256 lhs.cellY * screenCols + lhs.cellX);
2257 long rr = (rhs.screenId * screenCellCount +
2258 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002259 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002260 }
2261 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2262 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002263 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002264 }
2265 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002266 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002267 throw new RuntimeException("Unexpected container type when " +
2268 "sorting workspace items.");
2269 }
2270 return 0;
2271 }
2272 } else {
2273 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002274 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002275 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002276 }
2277 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002278 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002279
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2281 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 final Runnable r = new Runnable() {
2283 @Override
2284 public void run() {
2285 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2286 if (callbacks != null) {
2287 callbacks.bindScreens(orderedScreens);
2288 }
2289 }
2290 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002291 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 }
2293
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2295 final ArrayList<ItemInfo> workspaceItems,
2296 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002297 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002298
2299 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002300 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002302 final int start = i;
2303 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 final Runnable r = new Runnable() {
2305 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002306 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002307 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002308 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002309 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2310 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 }
2312 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002314 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002315 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 // Bind the widgets, one at a time
2318 N = appWidgets.size();
2319 for (int i = 0; i < N; i++) {
2320 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2321 final Runnable r = new Runnable() {
2322 public void run() {
2323 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2324 if (callbacks != null) {
2325 callbacks.bindAppWidget(widget);
2326 }
2327 }
2328 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002329 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 }
2331 }
2332
2333 /**
2334 * Binds all loaded data to actual views on the main thread.
2335 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002336 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 final long t = SystemClock.uptimeMillis();
2338 Runnable r;
2339
2340 // Don't use these two variables in any of the callback runnables.
2341 // Otherwise we hold a reference to them.
2342 final Callbacks oldCallbacks = mCallbacks.get();
2343 if (oldCallbacks == null) {
2344 // This launcher has exited and nobody bothered to tell us. Just bail.
2345 Log.w(TAG, "LoaderTask running with no launcher");
2346 return;
2347 }
2348
Winson Chung9b9fb962013-11-15 15:39:34 -08002349 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002350 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2351 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2352 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002353
Sunny Goyale9956a72016-09-01 17:24:47 -07002354 synchronized (sBgDataModel) {
2355 workspaceItems.addAll(sBgDataModel.workspaceItems);
2356 appWidgets.addAll(sBgDataModel.appWidgets);
2357 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 }
2359
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002360 final int currentScreen;
2361 {
2362 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2363 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2364 if (currScreen >= orderedScreenIds.size()) {
2365 // There may be no workspace screens (just hotseat items and an empty page).
2366 currScreen = PagedView.INVALID_RESTORE_PAGE;
2367 }
2368 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002369 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002370 final boolean validFirstPage = currentScreen >= 0;
2371 final long currentScreenId =
2372 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002373
Winson Chung9b9fb962013-11-15 15:39:34 -08002374 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002375 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2376 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2377 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2378 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379
Winson Chung9b9fb962013-11-15 15:39:34 -08002380 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002382 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2385 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2386
2387 // Tell the workspace that we're about to start binding items
2388 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002389 public void run() {
2390 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2391 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002392 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002394 }
2395 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002397 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002398
Adam Cohendcd297f2013-06-18 13:13:40 -07002399 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2400
Sunny Goyal527c7d32015-08-28 15:19:36 -07002401 Executor mainExecutor = new DeferredMainThreadExecutor();
2402 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002403 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002405 // In case of validFirstPage, only bind the first screen, and defer binding the
2406 // remaining screens after first onDraw (and an optional the fade animation whichever
2407 // happens later).
2408 // This ensures that the first screen is immediately visible (eg. during rotation)
2409 // In case of !validFirstPage, bind all pages one after other.
2410 final Executor deferredExecutor =
2411 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2412
2413 mainExecutor.execute(new Runnable() {
2414 @Override
2415 public void run() {
2416 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2417 if (callbacks != null) {
2418 callbacks.finishFirstPageBind(
2419 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2420 }
2421 }
2422 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002423
Sunny Goyal44c06432016-04-02 10:56:02 -07002424 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425
2426 // Tell the workspace that we're done binding items
2427 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002428 public void run() {
2429 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2430 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002431 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002432 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433
Sunny Goyal639e9062015-08-19 19:17:06 -07002434 mIsLoadingAndBindingWorkspace = false;
2435
2436 // Run all the bind complete runnables after workspace is bound.
2437 if (!mBindCompleteRunnables.isEmpty()) {
2438 synchronized (mBindCompleteRunnables) {
2439 for (final Runnable r : mBindCompleteRunnables) {
2440 runOnWorkerThread(r);
2441 }
2442 mBindCompleteRunnables.clear();
2443 }
2444 }
2445
Winson Chung98e030b2012-05-07 16:01:11 -07002446 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002447 if (DEBUG_LOADERS) {
2448 Log.d(TAG, "bound workspace in "
2449 + (SystemClock.uptimeMillis()-t) + "ms");
2450 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002451
Joe Onorato36115782010-06-17 13:28:48 -04002452 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002453 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002454 deferredExecutor.execute(r);
2455
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002456 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002457 r = new Runnable() {
2458 public void run() {
2459 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2460 if (callbacks != null) {
2461 // We are loading synchronously, which means, some of the pages will be
2462 // bound after first draw. Inform the callbacks that page binding is
2463 // not complete, and schedule the remaining pages.
2464 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2465 callbacks.onPageBoundSynchronously(currentScreen);
2466 }
2467 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2468 }
2469 }
2470 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002471 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002472 }
Joe Onorato36115782010-06-17 13:28:48 -04002473 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002474
Joe Onorato36115782010-06-17 13:28:48 -04002475 private void loadAndBindAllApps() {
2476 if (DEBUG_LOADERS) {
2477 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2478 }
2479 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002480 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002481 synchronized (LoaderTask.this) {
2482 if (mStopped) {
2483 return;
2484 }
2485 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002486 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002487 synchronized (LoaderTask.this) {
2488 if (mStopped) {
2489 return;
2490 }
2491 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002492 }
Joe Onorato36115782010-06-17 13:28:48 -04002493 } else {
2494 onlyBindAllApps();
2495 }
2496 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002497
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002498 private void updateIconCache() {
2499 // Ignore packages which have a promise icon.
2500 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002501 synchronized (sBgDataModel) {
2502 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002503 if (info instanceof ShortcutInfo) {
2504 ShortcutInfo si = (ShortcutInfo) info;
2505 if (si.isPromise() && si.getTargetComponent() != null) {
2506 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2507 }
2508 } else if (info instanceof LauncherAppWidgetInfo) {
2509 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2510 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2511 packagesToIgnore.add(lawi.providerName.getPackageName());
2512 }
2513 }
2514 }
2515 }
2516 mIconCache.updateDbIcons(packagesToIgnore);
2517 }
2518
Joe Onorato36115782010-06-17 13:28:48 -04002519 private void onlyBindAllApps() {
2520 final Callbacks oldCallbacks = mCallbacks.get();
2521 if (oldCallbacks == null) {
2522 // This launcher has exited and nobody bothered to tell us. Just bail.
2523 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2524 return;
2525 }
2526
2527 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002528 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002529 final ArrayList<AppInfo> list
2530 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002531 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002532 public void run() {
2533 final long t = SystemClock.uptimeMillis();
2534 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2535 if (callbacks != null) {
2536 callbacks.bindAllApplications(list);
2537 }
2538 if (DEBUG_LOADERS) {
2539 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002540 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002541 }
2542 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002543 };
Tony Wickham80f57872016-06-29 18:12:15 -07002544 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002545 }
2546
Winson Chung64359a52013-07-08 17:17:08 -07002547 private void loadAllApps() {
2548 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002549
Joe Onorato36115782010-06-17 13:28:48 -04002550 final Callbacks oldCallbacks = mCallbacks.get();
2551 if (oldCallbacks == null) {
2552 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002553 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002554 return;
2555 }
2556
Kenny Guyed131872014-04-30 03:02:21 +01002557 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2558
Winson Chung64359a52013-07-08 17:17:08 -07002559 // Clear the list of apps
2560 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002561 for (UserHandleCompat user : profiles) {
2562 // Query for the set of apps
2563 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002564 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002565 if (DEBUG_LOADERS) {
2566 Log.d(TAG, "getActivityList took "
2567 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2568 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2569 }
2570 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002571 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002572 if (apps == null || apps.isEmpty()) {
2573 return;
2574 }
Kenny Guyff05f432016-01-22 17:48:29 +00002575 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002576 // Create the ApplicationInfos
2577 for (int i = 0; i < apps.size(); i++) {
2578 LauncherActivityInfoCompat app = apps.get(i);
2579 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002580 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002581 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002582
Sunny Goyal756a28a2015-04-23 17:07:55 -07002583 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2584 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002585 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002586
2587 @Override
2588 public void run() {
2589 heuristic.processUserApps(apps);
2590 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002591 };
2592 runOnMainThread(new Runnable() {
2593
2594 @Override
2595 public void run() {
2596 // Check isLoadingWorkspace on the UI thread, as it is updated on
2597 // the UI thread.
2598 if (mIsLoadingAndBindingWorkspace) {
2599 synchronized (mBindCompleteRunnables) {
2600 mBindCompleteRunnables.add(r);
2601 }
2602 } else {
2603 runOnWorkerThread(r);
2604 }
2605 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002606 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002607 }
Winson Chung64359a52013-07-08 17:17:08 -07002608 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002609 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002610 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2611 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002612
2613 // Post callback on main thread
2614 mHandler.post(new Runnable() {
2615 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002616
Winson Chung64359a52013-07-08 17:17:08 -07002617 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002618 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002619 if (callbacks != null) {
2620 callbacks.bindAllApplications(added);
2621 if (DEBUG_LOADERS) {
2622 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002623 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002624 }
2625 } else {
2626 Log.i(TAG, "not binding apps: no Launcher activity");
2627 }
2628 }
2629 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002630 // Cleanup any data stored for a deleted user.
2631 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002632 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002633 Log.d(TAG, "Icons processed in "
2634 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002635 }
2636 }
2637
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002638 private void loadAndBindDeepShortcuts() {
2639 if (DEBUG_LOADERS) {
2640 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2641 }
2642 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002643 sBgDataModel.deepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002644 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2645 if (mHasShortcutHostPermission) {
2646 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2647 if (mUserManager.isUserUnlocked(user)) {
2648 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2649 .queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002650 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002651 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002652 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002653 }
2654 synchronized (LoaderTask.this) {
2655 if (mStopped) {
2656 return;
2657 }
2658 mDeepShortcutsLoaded = true;
2659 }
2660 }
Tony Wickham80f57872016-06-29 18:12:15 -07002661 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002662 }
2663
Joe Onoratobe386092009-11-17 17:32:16 -08002664 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002665 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002666 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002667 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2668 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002669 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002670 }
Joe Onorato36115782010-06-17 13:28:48 -04002671 }
2672 }
2673
Tony Wickham80f57872016-06-29 18:12:15 -07002674 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002675 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2676 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002677 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002678 @Override
2679 public void run() {
2680 Callbacks callbacks = getCallback();
2681 if (callbacks != null) {
2682 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2683 }
2684 }
Tony Wickham80f57872016-06-29 18:12:15 -07002685 };
2686 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002687 }
2688
Sunny Goyal75b0f552015-05-20 21:57:06 -07002689 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002690 * Refreshes the cached shortcuts if the shortcut permission has changed.
2691 * Current implementation simply reloads the workspace, but it can be optimized to
2692 * use partial updates similar to {@link UserManagerCompat}
2693 */
2694 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07002695 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002696 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2697 sWorker.post(mShortcutPermissionCheckRunnable);
2698 }
2699 }
2700
2701 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002702 * Called when the icons for packages have been updated in the icon cache.
2703 */
2704 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2705 final Callbacks callbacks = getCallback();
2706 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2707 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2708
2709 // If any package icon has changed (app was updated while launcher was dead),
2710 // update the corresponding shortcuts.
Sunny Goyale9956a72016-09-01 17:24:47 -07002711 synchronized (sBgDataModel) {
2712 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002713 if (info instanceof ShortcutInfo && user.equals(info.user)
2714 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2715 ShortcutInfo si = (ShortcutInfo) info;
2716 ComponentName cn = si.getTargetComponent();
2717 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2718 si.updateIcon(mIconCache);
2719 updatedShortcuts.add(si);
2720 }
2721 }
2722 }
2723 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2724 }
2725
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002726 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002727
2728 if (!updatedApps.isEmpty()) {
2729 mHandler.post(new Runnable() {
2730
2731 public void run() {
2732 Callbacks cb = getCallback();
2733 if (cb != null && callbacks == cb) {
2734 cb.bindAppsUpdated(updatedApps);
2735 }
2736 }
2737 });
2738 }
2739 }
2740
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002741 private void bindUpdatedShortcuts(
2742 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2743 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2744 }
2745
2746 private void bindUpdatedShortcuts(
2747 final ArrayList<ShortcutInfo> updatedShortcuts,
2748 final ArrayList<ShortcutInfo> removedShortcuts,
2749 final UserHandleCompat user) {
2750 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002751 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002752 mHandler.post(new Runnable() {
2753
2754 public void run() {
2755 Callbacks cb = getCallback();
2756 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002757 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002758 }
2759 }
2760 });
2761 }
2762 }
2763
2764 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002765 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002766 }
2767
2768 private class PackageUpdatedTask implements Runnable {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002769 final int mOp;
2770 final String[] mPackages;
2771 final UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002772
2773 public static final int OP_NONE = 0;
2774 public static final int OP_ADD = 1;
2775 public static final int OP_UPDATE = 2;
2776 public static final int OP_REMOVE = 3; // uninstlled
2777 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002778 public static final int OP_SUSPEND = 5; // package suspended
2779 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002780 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002781
Kenny Guyed131872014-04-30 03:02:21 +01002782 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002783 mOp = op;
2784 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002785 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002786 }
2787
2788 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002789 if (!mHasLoaderCompletedOnce) {
2790 // Loader has not yet run.
2791 return;
2792 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002793 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002794
2795 final String[] packages = mPackages;
2796 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002797 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyal40452cf2016-09-01 15:17:46 -07002798 final HashSet<String> packageSet = new HashSet<>(Arrays.asList(packages));
Joe Onorato36115782010-06-17 13:28:48 -04002799 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002800 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002801 for (int i=0; i<N; i++) {
2802 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002803 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002804 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002805 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002806
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002807 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2808 if (heuristic != null) {
2809 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002810 }
Joe Onorato36115782010-06-17 13:28:48 -04002811 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002812 }
Joe Onorato36115782010-06-17 13:28:48 -04002813 case OP_UPDATE:
2814 for (int i=0; i<N; i++) {
2815 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002816 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002817 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002818 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002819 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002820 // Since package was just updated, the target must be available now.
2821 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002822 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002823 case OP_REMOVE: {
2824 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2825 if (heuristic != null) {
2826 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002827 }
Joe Onorato36115782010-06-17 13:28:48 -04002828 for (int i=0; i<N; i++) {
2829 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002830 mIconCache.removeIconsForPkg(packages[i], mUser);
2831 }
2832 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002833 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002834 case OP_UNAVAILABLE:
2835 for (int i=0; i<N; i++) {
2836 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2837 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002838 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002839 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002840 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002841 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002842 case OP_SUSPEND:
2843 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002844 flagOp = mOp == OP_SUSPEND ?
2845 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2846 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002847 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyal40452cf2016-09-01 15:17:46 -07002848 mBgAllAppsList.updateDisabledFlags(
2849 ItemInfoMatcher.ofPackages(packageSet, mUser), flagOp);
Sunny Goyalda891c12016-03-18 18:29:24 -07002850 break;
2851 case OP_USER_AVAILABILITY_CHANGE:
2852 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2853 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2854 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2855 // We want to update all packages for this user.
Sunny Goyal40452cf2016-09-01 15:17:46 -07002856 mBgAllAppsList.updateDisabledFlags(ItemInfoMatcher.ofUser(mUser), flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002857 break;
Joe Onorato36115782010-06-17 13:28:48 -04002858 }
2859
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002860 ArrayList<AppInfo> added = null;
2861 ArrayList<AppInfo> modified = null;
2862 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002863
Adam Cohen487f7dd2012-06-28 18:12:10 -07002864 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002865 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002866 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002867 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002868 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002869 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002870 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002871 }
Winson Chung5d55f332012-07-16 20:45:03 -07002872 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002873 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002874 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002875 }
2876
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002877 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002878
Joe Onorato36115782010-06-17 13:28:48 -04002879 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002880 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002881 for (AppInfo ai : added) {
2882 addedOrUpdatedApps.put(ai.componentName, ai);
2883 }
Joe Onorato36115782010-06-17 13:28:48 -04002884 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002885
Joe Onorato36115782010-06-17 13:28:48 -04002886 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002887 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002888 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002889 for (AppInfo ai : modified) {
2890 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002891 }
2892
Joe Onorato36115782010-06-17 13:28:48 -04002893 mHandler.post(new Runnable() {
2894 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002895 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002896 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002897 callbacks.bindAppsUpdated(modifiedFinal);
2898 }
2899 }
2900 });
2901 }
Winson Chung83892cc2013-05-01 16:53:33 -07002902
Sunny Goyal4390ace2014-10-13 11:33:11 -07002903 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002904 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002905 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2906 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<>();
2907 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002908
Sunny Goyale9956a72016-09-01 17:24:47 -07002909 synchronized (sBgDataModel) {
2910 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002911 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2912 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002913 boolean infoUpdated = false;
2914 boolean shortcutUpdated = false;
2915
2916 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002917 if ((si.iconResource != null)
Sunny Goyal40452cf2016-09-01 15:17:46 -07002918 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal10629b02016-09-01 12:50:11 -07002919 Bitmap icon = LauncherIcons.createIconBitmap(
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002920 si.iconResource.packageName,
2921 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002922 if (icon != null) {
2923 si.setIcon(icon);
2924 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002925 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002926 }
2927 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002928
2929 ComponentName cn = si.getTargetComponent();
Sunny Goyal40452cf2016-09-01 15:17:46 -07002930 if (cn != null && packageSet.contains(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002931 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2932
2933 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002934 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2935 // Auto install icon
2936 PackageManager pm = context.getPackageManager();
2937 ResolveInfo matched = pm.resolveActivity(
2938 new Intent(Intent.ACTION_MAIN)
2939 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2940 PackageManager.MATCH_DEFAULT_ONLY);
2941 if (matched == null) {
2942 // Try to find the best match activity.
2943 Intent intent = pm.getLaunchIntentForPackage(
2944 cn.getPackageName());
2945 if (intent != null) {
2946 cn = intent.getComponent();
2947 appInfo = addedOrUpdatedApps.get(cn);
2948 }
2949
2950 if ((intent == null) || (appInfo == null)) {
2951 removedShortcuts.add(si);
2952 continue;
2953 }
2954 si.promisedIntent = intent;
2955 }
2956 }
2957
Sunny Goyal756adbc2015-04-16 15:20:51 -07002958 si.intent = si.promisedIntent;
2959 si.promisedIntent = null;
2960 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002961 infoUpdated = true;
2962 si.updateIcon(mIconCache);
2963 }
2964
2965 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2966 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2967 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002968 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002969 si.contentDescription = appInfo.contentDescription;
2970 infoUpdated = true;
2971 }
2972
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002973 int oldDisabledFlags = si.isDisabled;
2974 si.isDisabled = flagOp.apply(si.isDisabled);
2975 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002976 shortcutUpdated = true;
2977 }
2978 }
2979
2980 if (infoUpdated || shortcutUpdated) {
2981 updatedShortcuts.add(si);
2982 }
2983 if (infoUpdated) {
2984 updateItemInDatabase(context, si);
2985 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002986 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002987 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2988 if (mUser.equals(widgetInfo.user)
2989 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyal40452cf2016-09-01 15:17:46 -07002990 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002991 widgetInfo.restoreStatus &=
2992 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2993 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002994
2995 // adding this flag ensures that launcher shows 'click to setup'
2996 // if the widget has a config activity. In case there is no config
2997 // activity, it will be marked as 'restored' during bind.
2998 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2999
Sunny Goyal4390ace2014-10-13 11:33:11 -07003000 widgets.add(widgetInfo);
3001 updateItemInDatabase(context, widgetInfo);
3002 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003003 }
3004 }
3005 }
3006
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003007 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3008 if (!removedShortcuts.isEmpty()) {
3009 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003010 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003011
Sunny Goyal4390ace2014-10-13 11:33:11 -07003012 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003013 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003014 mHandler.post(new Runnable() {
3015 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003016 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003017 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003018 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003019 }
3020 }
3021 });
3022 }
3023 }
3024
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003025 final HashSet<String> removedPackages = new HashSet<>();
3026 final HashSet<ComponentName> removedComponents = new HashSet<>();
3027 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003028 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003029 Collections.addAll(removedPackages, packages);
3030
3031 // No need to update the removedComponents as
3032 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003033 } else if (mOp == OP_UPDATE) {
3034 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003035 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003036 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003037 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003038 }
3039 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003040
3041 // Update removedComponents as some components can get removed during package update
3042 for (AppInfo info : removedApps) {
3043 removedComponents.add(info.componentName);
3044 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003045 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003046
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003047 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07003048 deleteItemsFromDatabase(
3049 context, ItemInfoMatcher.ofPackages(removedPackages, mUser));
3050 deleteItemsFromDatabase(
3051 context, ItemInfoMatcher.ofComponents(removedComponents, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003052
Winson Chungdf95eb12013-10-16 14:57:07 -07003053 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003054 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3055
Winson Chungdf95eb12013-10-16 14:57:07 -07003056 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003057 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003058 mHandler.post(new Runnable() {
3059 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003060 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003061 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003062 callbacks.bindWorkspaceComponentsRemoved(
3063 removedPackages, removedComponents, mUser);
3064 }
3065 }
3066 });
3067 }
3068
3069 if (!removedApps.isEmpty()) {
3070 // Remove corresponding apps from All-Apps
3071 final Callbacks callbacks = getCallback();
3072 mHandler.post(new Runnable() {
3073 public void run() {
3074 Callbacks cb = getCallback();
3075 if (callbacks == cb && cb != null) {
3076 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003077 }
3078 }
3079 });
Joe Onoratobe386092009-11-17 17:32:16 -08003080 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003081
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003082 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3083 // get widget update signals.
3084 if (!Utilities.ATLEAST_MARSHMALLOW &&
3085 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003086 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003087 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003088 public void run() {
3089 Callbacks cb = getCallback();
3090 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003091 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003092 }
3093 }
3094 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003095 }
3096 }
3097 }
3098
Sunny Goyal10923b32016-07-20 15:42:44 -07003099 /**
3100 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3101 */
3102 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3103 enqueueItemUpdatedTask(new Runnable() {
3104 @Override
3105 public void run() {
3106 info.updateFromDeepShortcutInfo(
3107 fullDetail, LauncherAppState.getInstance().getContext());
3108 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3109 update.add(info);
3110 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3111 }
3112 });
3113 }
3114
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003115 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003116 private final String mPackageName;
3117 private final List<ShortcutInfoCompat> mShortcuts;
3118 private final UserHandleCompat mUser;
3119 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003120
3121 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003122 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003123 mPackageName = packageName;
3124 mShortcuts = shortcuts;
3125 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003126 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003127 }
3128
3129 @Override
3130 public void run() {
3131 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3132
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003133 // Find ShortcutInfo's that have changed on the workspace.
Tony Wickham7695e602016-08-05 12:44:51 -07003134 final ArrayList<ShortcutInfo> removedShortcutInfos = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003135 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003136 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003137 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3138 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003139 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3140 && si.user.equals(mUser)) {
Tony Wickham7695e602016-08-05 12:44:51 -07003141 idsToWorkspaceShortcutInfos.addToList(si.getDeepShortcutId(), si);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003142 }
3143 }
3144 }
3145
Tony Wickham7695e602016-08-05 12:44:51 -07003146 final Context context = LauncherAppState.getInstance().getContext();
3147 final ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3148 if (!idsToWorkspaceShortcutInfos.isEmpty()) {
3149 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3150 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3151 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3152 for (ShortcutInfoCompat fullDetails : shortcuts) {
3153 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3154 .remove(fullDetails.getId());
3155 if (!fullDetails.isPinned()) {
3156 // The shortcut was previously pinned but is no longer, so remove it from
3157 // the workspace and our pinned shortcut counts.
3158 // Note that we put this check here, after querying for full details,
3159 // because there's a possible race condition between pinning and
3160 // receiving this callback.
3161 removedShortcutInfos.addAll(shortcutInfos);
3162 continue;
3163 }
3164 for (ShortcutInfo shortcutInfo : shortcutInfos) {
3165 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
3166 updatedShortcutInfos.add(shortcutInfo);
3167 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003168 }
3169 }
Tony Wickham7695e602016-08-05 12:44:51 -07003170
3171 // If there are still entries in idsToWorkspaceShortcutInfos, that means that
3172 // the corresponding shortcuts weren't passed in onShortcutsChanged(). This
3173 // means they were cleared, so we remove and unpin them now.
3174 for (String id : idsToWorkspaceShortcutInfos.keySet()) {
3175 removedShortcutInfos.addAll(idsToWorkspaceShortcutInfos.get(id));
3176 }
3177
3178 bindUpdatedShortcuts(updatedShortcutInfos, removedShortcutInfos, mUser);
3179 if (!removedShortcutInfos.isEmpty()) {
3180 deleteItemsFromDatabase(context, removedShortcutInfos);
3181 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003182
Sunny Goyal50941fb2016-08-04 12:03:52 -07003183 if (mUpdateIdMap) {
3184 // Update the deep shortcut map if the list of ids has changed for an activity.
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003185 sBgDataModel.updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Sunny Goyal50941fb2016-08-04 12:03:52 -07003186 bindDeepShortcuts();
3187 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003188 }
3189 }
3190
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003191 /**
3192 * Task to handle changing of lock state of the user
3193 */
3194 private class UserLockStateChangedTask implements Runnable {
3195
3196 private final UserHandleCompat mUser;
3197
3198 public UserLockStateChangedTask(UserHandleCompat user) {
3199 mUser = user;
3200 }
3201
3202 @Override
3203 public void run() {
3204 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3205 Context context = mApp.getContext();
3206
3207 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3208 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003209 List<ShortcutInfoCompat> shortcuts =
3210 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3211 if (mDeepShortcutManager.wasLastCallSuccess()) {
3212 for (ShortcutInfoCompat shortcut : shortcuts) {
3213 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3214 }
3215 } else {
3216 // Shortcut manager can fail due to some race condition when the lock state
3217 // changes too frequently. For the purpose of the update,
3218 // consider it as still locked.
3219 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003220 }
3221 }
3222
3223 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3224 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3225 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003226 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003227 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3228 && mUser.equals(itemInfo.user)) {
3229 ShortcutInfo si = (ShortcutInfo) itemInfo;
3230 if (isUserUnlocked) {
3231 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003232 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003233 // We couldn't verify the shortcut during loader. If its no longer available
3234 // (probably due to clear data), delete the workspace item as well
3235 if (shortcut == null) {
3236 deletedShortcutInfos.add(si);
3237 continue;
3238 }
3239 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3240 si.updateFromDeepShortcutInfo(shortcut, context);
3241 } else {
3242 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3243 }
3244 updatedShortcutInfos.add(si);
3245 }
3246 }
3247 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3248 if (!deletedShortcutInfos.isEmpty()) {
3249 deleteItemsFromDatabase(context, deletedShortcutInfos);
3250 }
3251
3252 // Remove shortcut id map for that user
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003253 Iterator<ComponentKey> keysIter = sBgDataModel.deepShortcutMap.keySet().iterator();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003254 while (keysIter.hasNext()) {
3255 if (keysIter.next().user.equals(mUser)) {
3256 keysIter.remove();
3257 }
3258 }
3259
3260 if (isUserUnlocked) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003261 sBgDataModel.updateDeepShortcutMap(
3262 null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003263 }
3264 bindDeepShortcuts();
3265 }
3266 }
3267
Sunny Goyald164b7f2016-10-12 20:49:31 -07003268 private void bindWidgetsModel(final Callbacks callbacks) {
3269 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
3270 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003271 mHandler.post(new Runnable() {
3272 @Override
3273 public void run() {
3274 Callbacks cb = getCallback();
3275 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003276 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003277 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003278 }
3279 });
3280 }
3281
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003282 public void refreshAndBindWidgetsAndShortcuts(
3283 final Callbacks callbacks, final boolean bindFirst) {
3284 runOnWorkerThread(new Runnable() {
3285 @Override
3286 public void run() {
3287 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003288 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07003289 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07003290 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
3291 bindWidgetsModel(callbacks);
3292
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003293 // update the Widget entries inside DB on the worker thread.
Sunny Goyald164b7f2016-10-12 20:49:31 -07003294 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07003295 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003296 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003297 }
3298
Adam Cohen091440a2015-03-18 14:16:05 -07003299 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003300 UserHandleCompat user) {
3301 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3302 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003303 }
Adam Cohen556f6132014-01-15 15:18:08 -08003304
Joe Onorato9c1289c2009-08-17 11:03:03 -04003305 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003306 * Make an ShortcutInfo object for a restored application or shortcut item that points
3307 * to a package that is not yet installed on the system.
3308 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003309 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3310 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003311 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003312 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003313
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003314 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003315 // the fallback icon
3316 if (icon == null) {
3317 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3318 } else {
3319 info.setIcon(icon);
3320 }
Sunny Goyal34942622014-08-29 17:20:55 -07003321
3322 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003323 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003324 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003325 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003326 }
Sunny Goyal34942622014-08-29 17:20:55 -07003327 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3328 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003329 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003330 }
Sunny Goyal34942622014-08-29 17:20:55 -07003331 } else {
3332 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3333 }
3334
Winson Chung82b016c2015-05-08 17:00:10 -07003335 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003336 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003337 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003338 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003339 return info;
3340 }
3341
3342 /**
3343 * Make an Intent object for a restored application or shortcut item that points
3344 * to the market page for the item.
3345 */
Adam Cohen091440a2015-03-18 14:16:05 -07003346 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003347 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003348 return getMarketIntent(componentName.getPackageName());
3349 }
3350
3351 static Intent getMarketIntent(String packageName) {
3352 return new Intent(Intent.ACTION_VIEW)
3353 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003354 .scheme("market")
3355 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003356 .appendQueryParameter("id", packageName)
3357 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003358 }
3359
3360 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003361 * Make an ShortcutInfo object for a shortcut that is an application.
3362 *
3363 * If c is not null, then it will be used to fill in missing data like the title and icon.
3364 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003365 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003366 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003367 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003368 if (user == null) {
3369 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003370 return null;
3371 }
3372
Kenny Guyed131872014-04-30 03:02:21 +01003373 ComponentName componentName = intent.getComponent();
3374 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003375 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003376 return null;
3377 }
3378
3379 Intent newIntent = new Intent(intent.getAction(), null);
3380 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3381 newIntent.setComponent(componentName);
3382 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003383 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003384 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3385 return null;
3386 }
3387
3388 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003389 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003390 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003391 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003392 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003393 }
3394
Sunny Goyald09c3702016-04-06 16:18:20 -07003395 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3396 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3397 }
3398
Joe Onorato56d82912010-03-07 14:32:10 -05003399 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003400 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003401 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003402 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003403
Joe Onorato56d82912010-03-07 14:32:10 -05003404 // fall back to the class name of the activity
3405 if (info.title == null) {
3406 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003407 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003408
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003410 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003411 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003413 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003414
Sunny Goyal1a745e82014-10-02 15:58:31 -07003415 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003416 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003417 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003418 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003419 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003420 // Non-app shortcuts are only supported for current user.
3421 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003423
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003424 // TODO: If there's an explicit component and we can't install that, delete it.
3425
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003426 loadInfoFromCursor(info, c, iconInfo);
3427 return info;
3428 }
Joe Onorato56d82912010-03-07 14:32:10 -05003429
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003430 /**
3431 * Make an ShortcutInfo object for a shortcut that isn't an application.
3432 */
3433 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3434 info.title = iconInfo.getTitle(c);
3435 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003436 // the fallback icon
3437 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003438 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003439 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003440 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003441 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003443
Sunny Goyal2350bc92014-10-14 16:42:54 -07003444 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003445 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3446 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3447 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3448
Adam Cohend9198822011-11-22 16:42:47 -08003449 if (intent == null) {
3450 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3451 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3452 return null;
3453 }
3454
Joe Onorato0589f0f2010-02-08 13:44:00 -08003455 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003456 ShortcutIconResource iconResource = null;
3457
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003458 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003459 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003460 } else {
3461 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003462 if (extra instanceof ShortcutIconResource) {
3463 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07003464 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003465 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003466 }
3467 }
3468
Michael Jurkac9d95c52011-08-29 14:03:34 -07003469 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003470
Kenny Guyed131872014-04-30 03:02:21 +01003471 // Only support intents for current user for now. Intents sent from other
3472 // users wouldn't get here without intent forwarding anyway.
3473 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003474 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003475 icon = mIconCache.getDefaultIcon(info.user);
3476 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003477 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003478 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003479
Winson Chung82b016c2015-05-08 17:00:10 -07003480 info.title = Utilities.trim(name);
3481 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003482 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003483 info.iconResource = iconResource;
3484
3485 return info;
3486 }
3487
Sunny Goyal651077b2014-06-30 14:15:31 -07003488 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3489 return (provider != null) && (provider.provider != null)
3490 && (provider.provider.getPackageName() != null);
3491 }
3492
Joe Onoratobe386092009-11-17 17:32:16 -08003493 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003494 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003495 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3496 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3497 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3498 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003499 if (mLoaderTask != null) {
3500 mLoaderTask.dumpState();
3501 } else {
3502 Log.d(TAG, "mLoaderTask=null");
3503 }
Joe Onoratobe386092009-11-17 17:32:16 -08003504 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003505
3506 public Callbacks getCallback() {
3507 return mCallbacks != null ? mCallbacks.get() : null;
3508 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003509
3510 /**
3511 * @return {@link FolderInfo} if its already loaded.
3512 */
3513 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07003514 synchronized (sBgDataModel) {
3515 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003516 }
3517 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003518
Sunny Goyal527c7d32015-08-28 15:19:36 -07003519 @Thunk class DeferredMainThreadExecutor implements Executor {
3520
3521 @Override
3522 public void execute(Runnable command) {
3523 runOnMainThread(command);
3524 }
3525 }
3526
Sunny Goyal756adbc2015-04-16 15:20:51 -07003527 /**
3528 * @return the looper for the worker thread which can be used to start background tasks.
3529 */
3530 public static Looper getWorkerLooper() {
3531 return sWorkerThread.getLooper();
3532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003533}