blob: caf8870c26db238dac59d3c166eb7f3f3752a784 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal10629b02016-09-01 12:50:11 -070060import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070061import com.android.launcher3.logging.FileLog;
Sunny Goyale9956a72016-09-01 17:24:47 -070062import com.android.launcher3.model.BgDataModel;
Sunny Goyalf862a262015-12-14 14:27:38 -080063import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalc2936bc2016-09-01 15:50:36 -070064import com.android.launcher3.model.SdCardAvailableReceiver;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070065import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070066import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070067import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070068import com.android.launcher3.shortcuts.DeepShortcutManager;
69import com.android.launcher3.shortcuts.ShortcutInfoCompat;
70import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000071import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070072import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070073import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070074import com.android.launcher3.util.GridOccupancy;
Sunny Goyal40452cf2016-09-01 15:17:46 -070075import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyale2df0622015-04-24 11:27:00 -070076import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070077import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070078import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070079import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070080import com.android.launcher3.util.Preconditions;
Adam Cohen091440a2015-03-18 14:16:05 -070081import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070082import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080083
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.lang.ref.WeakReference;
85import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070086import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070088import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089import java.util.Collections;
90import java.util.Comparator;
91import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070093import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070094import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070095import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070096import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070097import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Maintains in-memory state of the Launcher. It is expected that there should be only one
101 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700102 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 */
Kenny Guyed131872014-04-30 03:02:21 +0100104public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100105 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400107 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700110
Joe Onorato36115782010-06-17 13:28:48 -0400111 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500112 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk final LauncherAppState mApp;
115 @Thunk final Object mLock = new Object();
116 @Thunk DeferredHandler mHandler = new DeferredHandler();
117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700119 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122 static {
123 sWorkerThread.start();
124 }
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 // We start off with everything not loaded. After that, we assume that
128 // our monitoring of the package manager provides all updates and we never
129 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700130 private boolean mWorkspaceLoaded;
131 private boolean mAllAppsLoaded;
132 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700133
Sunny Goyal756a28a2015-04-23 17:07:55 -0700134 /**
135 * Set of runnables to be called on the background thread after the workspace binding
136 * is complete.
137 */
138 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
139
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700144 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800145 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700147 private boolean mHasShortcutHostPermission;
148 // Runnable to check if the shortcuts permission has changed.
149 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
150 @Override
151 public void run() {
152 if (mDeepShortcutsLoaded) {
153 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
154 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
155 mApp.reloadWorkspace();
156 }
157 }
158 }
159 };
160
Sunny Goyale9956a72016-09-01 17:24:47 -0700161 /**
162 * All the static data should be accessed on the background thread, A lock should be acquired
163 * on this object when accessing any data from this model.
164 */
165 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700166
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700167 // </ only access in worker thread >
168
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700169 private IconCache mIconCache;
170 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700172 private final LauncherAppsCompat mLauncherApps;
173 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700176 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700178 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700180 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
181 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700182 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700183 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800184 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200186 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700187 public void bindAppsAdded(ArrayList<Long> newScreens,
188 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700189 ArrayList<ItemInfo> addAnimated,
190 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200191 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700192 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
193 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
194 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700195 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700196 public void bindWorkspaceComponentsRemoved(
197 HashSet<String> packageNames, HashSet<ComponentName> components,
198 UserHandleCompat user);
199 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800200 public void notifyWidgetProvidersChanged();
201 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700202 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700203 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700204 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700207 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
208 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800209 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400210 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100211 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700212 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700214 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800215
Kenny Guyed131872014-04-30 03:02:21 +0100216 mLauncherApps = LauncherAppsCompat.getInstance(context);
217 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 }
219
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700220 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
221 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700222 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700223 if (sWorkerThread.getThreadId() == Process.myTid()) {
224 // If we are on the worker thread, post onto the main handler
225 mHandler.post(r);
226 } else {
227 r.run();
228 }
229 }
230
231 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
232 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700233 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700234 if (sWorkerThread.getThreadId() == Process.myTid()) {
235 r.run();
236 } else {
237 // If we are not on the worker thread, then post to the worker handler
238 sWorker.post(r);
239 }
240 }
241
Sunny Goyal756adbc2015-04-16 15:20:51 -0700242 public void setPackageState(final PackageInstallInfo installInfo) {
243 Runnable updateRunnable = new Runnable() {
244
245 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500246 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700247 synchronized (sBgDataModel) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700248 final HashSet<ItemInfo> updates = new HashSet<>();
249
250 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
251 // Ignore install success events as they are handled by Package add events.
252 return;
253 }
254
Sunny Goyale9956a72016-09-01 17:24:47 -0700255 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700256 if (info instanceof ShortcutInfo) {
257 ShortcutInfo si = (ShortcutInfo) info;
258 ComponentName cn = si.getTargetComponent();
259 if (si.isPromise() && (cn != null)
260 && installInfo.packageName.equals(cn.getPackageName())) {
261 si.setInstallProgress(installInfo.progress);
262
263 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
264 // Mark this info as broken.
265 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
266 }
267 updates.add(si);
268 }
269 }
270 }
271
Sunny Goyale9956a72016-09-01 17:24:47 -0700272 for (LauncherAppWidgetInfo widget : sBgDataModel.appWidgets) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700273 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
274 widget.installProgress = installInfo.progress;
275 updates.add(widget);
276 }
277 }
278
279 if (!updates.isEmpty()) {
280 // Push changes to the callback.
281 Runnable r = new Runnable() {
282 public void run() {
283 Callbacks callbacks = getCallback();
284 if (callbacks != null) {
285 callbacks.bindRestoreItemsChange(updates);
286 }
287 }
288 };
289 mHandler.post(r);
290 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500291 }
292 }
293 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700294 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500295 }
296
Sunny Goyal756adbc2015-04-16 15:20:51 -0700297 /**
298 * Updates the icons and label of all pending icons for the provided package name.
299 */
300 public void updateSessionDisplayInfo(final String packageName) {
301 Runnable updateRunnable = new Runnable() {
302
303 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700304 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700305 synchronized (sBgDataModel) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700306 ArrayList<ShortcutInfo> updates = new ArrayList<>();
307 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700308
Sunny Goyale9956a72016-09-01 17:24:47 -0700309 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700310 if (info instanceof ShortcutInfo) {
311 ShortcutInfo si = (ShortcutInfo) info;
312 ComponentName cn = si.getTargetComponent();
313 if (si.isPromise() && (cn != null)
314 && packageName.equals(cn.getPackageName())) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -0700315 si.updateIcon(mIconCache);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700316 updates.add(si);
317 }
318 }
319 }
320
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700321 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700322 }
323 }
324 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700325 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700326 }
327
Adam Cohen76a47a12014-02-05 11:47:43 -0800328 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800329 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800330
331 if (allAppsApps == null) {
332 throw new RuntimeException("allAppsApps must not be null");
333 }
334 if (allAppsApps.isEmpty()) {
335 return;
336 }
337
338 // Process the newly added applications and add them to the database first
339 Runnable r = new Runnable() {
340 public void run() {
341 runOnMainThread(new Runnable() {
342 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800343 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800344 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500345 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800346 }
347 }
348 });
349 }
350 };
351 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700352 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800353
Sunny Goyala9116722015-04-29 13:55:58 -0700354 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800355 int[] xy, int spanX, int spanY) {
356 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700357 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700358
359 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800360 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700361 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700362 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800363 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800364 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700365 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800366 }
367
368 /**
369 * Find a position on the screen for the given size or adds a new screen.
370 * @return screenId and the coordinates for the item.
371 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700372 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700373 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800374 ArrayList<Long> workspaceScreens,
375 ArrayList<Long> addedWorkspaceScreensFinal,
376 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700377 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800378
Sunny Goyala9116722015-04-29 13:55:58 -0700379 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700380 assertWorkspaceLoaded();
Sunny Goyale9956a72016-09-01 17:24:47 -0700381 synchronized (sBgDataModel) {
382 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyala9116722015-04-29 13:55:58 -0700383 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
384 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
385 if (items == null) {
386 items = new ArrayList<>();
387 screenItems.put(info.screenId, items);
388 }
389 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800390 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800391 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800392 }
393
394 // Find appropriate space for the item.
395 long screenId = 0;
396 int[] cordinates = new int[2];
397 boolean found = false;
398
399 int screenCount = workspaceScreens.size();
400 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700401 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800402 if (preferredScreenIndex < screenCount) {
403 screenId = workspaceScreens.get(preferredScreenIndex);
404 found = findNextAvailableIconSpaceInScreen(
405 screenItems.get(screenId), cordinates, spanX, spanY);
406 }
407
408 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700409 // Search on any of the screens starting from the first screen.
410 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 screenId = workspaceScreens.get(screen);
412 if (findNextAvailableIconSpaceInScreen(
413 screenItems.get(screenId), cordinates, spanX, spanY)) {
414 // We found a space for it
415 found = true;
416 break;
417 }
418 }
419 }
420
421 if (!found) {
422 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700423 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
424 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
425 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426
427 // Save the screen id for binding in the workspace
428 workspaceScreens.add(screenId);
429 addedWorkspaceScreensFinal.add(screenId);
430
431 // If we still can't find an empty space, then God help us all!!!
432 if (!findNextAvailableIconSpaceInScreen(
433 screenItems.get(screenId), cordinates, spanX, spanY)) {
434 throw new RuntimeException("Can't find space to add the item");
435 }
436 }
437 return Pair.create(screenId, cordinates);
438 }
439
440 /**
441 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800442 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700443 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700444 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800446 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700447 return;
Winson Chung997a9232013-07-24 15:33:46 -0700448 }
Winson Chung64359a52013-07-08 17:17:08 -0700449 // Process the newly added applications and add them to the database first
450 Runnable r = new Runnable() {
451 public void run() {
452 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
453 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
454
Winson Chung76828c82013-08-19 15:43:29 -0700455 // Get the list of workspace screens. We need to append to this list and
456 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
457 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800458 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Sunny Goyale9956a72016-09-01 17:24:47 -0700459 synchronized(sBgDataModel) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800460 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700461 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700463 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700465 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466 }
Winson Chung76828c82013-08-19 15:43:29 -0700467
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800468 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700469 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700470 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800471 long screenId = coords.first;
472 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700473
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700474 ItemInfo itemInfo;
475 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
476 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800477 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700478 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700479 } else {
480 throw new RuntimeException("Unexpected info type");
481 }
Winson Chung94d67682013-09-25 16:29:40 -0700482
Winson Chung64359a52013-07-08 17:17:08 -0700483 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700484 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700485 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700486 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700487 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700488 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700489 }
490 }
491
Winson Chung76828c82013-08-19 15:43:29 -0700492 // Update the workspace screens
493 updateWorkspaceScreenOrder(context, workspaceScreens);
494
Adam Cohen76a47a12014-02-05 11:47:43 -0800495 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700496 runOnMainThread(new Runnable() {
497 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800498 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700499 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700500 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
501 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700502 if (!addedShortcutsFinal.isEmpty()) {
503 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
504 long lastScreenId = info.screenId;
505 for (ItemInfo i : addedShortcutsFinal) {
506 if (i.screenId == lastScreenId) {
507 addAnimated.add(i);
508 } else {
509 addNotAnimated.add(i);
510 }
Winson Chung997a9232013-07-24 15:33:46 -0700511 }
512 }
Winson Chungd64d1762013-08-20 14:37:16 -0700513 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800514 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700515 }
Winson Chung64359a52013-07-08 17:17:08 -0700516 }
Winson Chung997a9232013-07-24 15:33:46 -0700517 });
518 }
Winson Chung64359a52013-07-08 17:17:08 -0700519 }
520 };
521 runOnWorkerThread(r);
522 }
523
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 /**
525 * Adds an item to the DB if it was not created previously, or move it to a new
526 * <container, screen, cellX, cellY>
527 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800528 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700529 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 if (item.container == ItemInfo.NO_ID) {
531 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700532 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 } else {
534 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700535 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
537 }
538
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700539 static void checkItemInfoLocked(
540 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700541 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700542 if (modelItem != null && item != modelItem) {
543 // check all the data is consistent
544 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
545 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
546 ShortcutInfo shortcut = (ShortcutInfo) item;
547 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
548 modelShortcut.intent.filterEquals(shortcut.intent) &&
549 modelShortcut.id == shortcut.id &&
550 modelShortcut.itemType == shortcut.itemType &&
551 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700552 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700553 modelShortcut.cellX == shortcut.cellX &&
554 modelShortcut.cellY == shortcut.cellY &&
555 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700556 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700557 // For all intents and purposes, this is the same object
558 return;
559 }
560 }
561
562 // the modelItem needs to match up perfectly with item if our model is
563 // to be consistent with the database-- for now, just require
564 // modelItem == item or the equality check above
565 String msg = "item: " + ((item != null) ? item.toString() : "null") +
566 "modelItem: " +
567 ((modelItem != null) ? modelItem.toString() : "null") +
568 "Error: ItemInfo passed to checkItemInfo doesn't match original";
569 RuntimeException e = new RuntimeException(msg);
570 if (stackTrace != null) {
571 e.setStackTrace(stackTrace);
572 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800573 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700574 }
575 }
576
Michael Jurka816474f2012-06-25 14:49:02 -0700577 static void checkItemInfo(final ItemInfo item) {
578 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
579 final long itemId = item.id;
580 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700581 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700582 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700583 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700584 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700585 }
586 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700587 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700588 }
589
Michael Jurkac9d95c52011-08-29 14:03:34 -0700590 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
591 final ItemInfo item, final String callingFunction) {
592 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700593 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700594 final ContentResolver cr = context.getContentResolver();
595
Adam Cohen487f7dd2012-06-28 18:12:10 -0700596 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700597 Runnable r = new Runnable() {
598 public void run() {
599 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700600 updateItemArrays(item, itemId, stackTrace);
601 }
602 };
603 runOnWorkerThread(r);
604 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700605
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700606 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
607 final ArrayList<ItemInfo> items, final String callingFunction) {
608 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700609
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700610 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
611 Runnable r = new Runnable() {
612 public void run() {
613 ArrayList<ContentProviderOperation> ops =
614 new ArrayList<ContentProviderOperation>();
615 int count = items.size();
616 for (int i = 0; i < count; i++) {
617 ItemInfo item = items.get(i);
618 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700619 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700620 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700621
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700622 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
623 updateItemArrays(item, itemId, stackTrace);
624
625 }
626 try {
627 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
628 } catch (Exception e) {
629 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700630 }
631 }
632 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700633 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700634 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700635
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700636 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
637 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700638 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700639 checkItemInfoLocked(itemId, item, stackTrace);
640
641 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
642 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
643 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700644 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700645 // An items container is being set to a that of an item which is not in
646 // the list of Folders.
647 String msg = "item: " + item + " container being set to: " +
648 item.container + ", not in the list of folders";
649 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700650 }
651 }
652
653 // Items are added/removed from the corresponding FolderInfo elsewhere, such
654 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
655 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700656 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800657 if (modelItem != null &&
658 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
659 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 switch (modelItem.itemType) {
661 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
662 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700663 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700664 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700665 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
666 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700667 }
668 break;
669 default:
670 break;
671 }
672 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700673 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 }
675 }
676 }
677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700680 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700681 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 item.cellX = cellX;
685 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700686
Winson Chung3d503fb2011-07-13 17:25:49 -0700687 // We store hotseat items in canonical form which is this orientation invariant position
688 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700689 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700690 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700691 item.screenId = Launcher.getLauncher(context).getHotseat()
692 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700693 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700695 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696
697 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700699 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
700 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800701 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700702 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703
Michael Jurkac9d95c52011-08-29 14:03:34 -0700704 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700705 }
706
707 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700708 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
709 * cellX, cellY have already been updated on the ItemInfos.
710 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800711 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700712 final long container, final int screen) {
713
714 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
715 int count = items.size();
716
717 for (int i = 0; i < count; i++) {
718 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700719 item.container = container;
720
721 // We store hotseat items in canonical form which is this orientation invariant position
722 // in the hotseat
723 if (context instanceof Launcher && screen < 0 &&
724 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700725 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700726 item.cellY);
727 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 }
730
731 final ContentValues values = new ContentValues();
732 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
733 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
734 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800735 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737
738 contentValues.add(values);
739 }
740 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
741 }
742
743 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700744 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800745 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700746 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700747 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700748 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800749 item.cellX = cellX;
750 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700751 item.spanX = spanX;
752 item.spanY = spanY;
753
754 // We store hotseat items in canonical form which is this orientation invariant position
755 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700756 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700757 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700758 item.screenId = Launcher.getLauncher(context).getHotseat()
759 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700760 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700761 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700762 }
Adam Cohend4844c32011-02-18 19:25:06 -0800763
Adam Cohend4844c32011-02-18 19:25:06 -0800764 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800765 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700766 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
767 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800768 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700769 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
770 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800772
Michael Jurka816474f2012-06-25 14:49:02 -0700773 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700774 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700775
776 /**
777 * Update an item to the database in a specified container.
778 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700779 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700780 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100781 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700782 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800783 }
784
Sunny Goyal756a28a2015-04-23 17:07:55 -0700785 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700786 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700787 synchronized (mLock) {
788 if (!mHasLoaderCompletedOnce ||
789 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
790 throw new RuntimeException("Trying to add shortcut while loader is running");
791 }
792 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700793 }
794 }
795
Adam Cohend4844c32011-02-18 19:25:06 -0800796 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700797 * Returns true if the shortcuts already exists on the workspace. This must be called after
798 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700800 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
801 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700802 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700803 if (intent.getComponent() != null) {
804 // If component is not null, an intent with null package will produce
805 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700806 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700807 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700808 intentWithPkg = intent.toUri(0);
809 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700810 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700811 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
812 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700813 }
814 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700815 intentWithPkg = intent.toUri(0);
816 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700817 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700818
Sunny Goyale9956a72016-09-01 17:24:47 -0700819 synchronized (sBgDataModel) {
820 for (ItemInfo item : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700821 if (item instanceof ShortcutInfo) {
822 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700823 Intent targetIntent = info.promisedIntent == null
824 ? info.intent : info.promisedIntent;
825 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700826 Intent copyIntent = new Intent(targetIntent);
827 copyIntent.setSourceBounds(intent.getSourceBounds());
828 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700829 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
830 return true;
831 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700832 }
833 }
834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700836 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700837 }
838
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 * Add an item to the database in a specified container. Sets the container, screen, cellX and
841 * cellY fields of the item. Also assigns an ID to the item.
842 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700843 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700844 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 item.cellX = cellX;
847 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 // We store hotseat items in canonical form which is this orientation invariant position
849 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700852 item.screenId = Launcher.getLauncher(context).getHotseat()
853 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857
858 final ContentValues values = new ContentValues();
859 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100860 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861
Sunny Goyald2497482015-09-22 18:24:19 -0700862 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
863 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
864
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700865 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700866
Jason Monk8e19cf22014-03-20 15:06:57 -0400867 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700869 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700870 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871
Sunny Goyale9956a72016-09-01 17:24:47 -0700872 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400873 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700874 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700875 }
876 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700877 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700878 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700879 }
880
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700881 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700882 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700884 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700885 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700886 items.add(item);
887 deleteItemsFromDatabase(context, items);
888 }
889
890 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700891 * Removes all the items from the database matching {@param matcher}.
892 */
893 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700894 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700895 }
896
897 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700898 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700899 */
Sunny Goyal40452cf2016-09-01 15:17:46 -0700900 static void deleteItemsFromDatabase(Context context, final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700902 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700903 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700904 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700905 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700906 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700907
Sunny Goyale9956a72016-09-01 17:24:47 -0700908 sBgDataModel.removeItem(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700909 }
910 }
Michael Jurka83df1882011-08-31 20:59:26 -0700911 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700912 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 }
914
915 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700916 * Update the order of the workspace screens in the database. The array list contains
917 * a list of screen ids in the order that they should appear.
918 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700919 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700920 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 final ContentResolver cr = context.getContentResolver();
922 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
923
924 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700925 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 while (iter.hasNext()) {
927 long id = iter.next();
928 if (id < 0) {
929 iter.remove();
930 }
931 }
932
933 Runnable r = new Runnable() {
934 @Override
935 public void run() {
Yura085c8532014-02-11 15:15:29 +0000936 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000938 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700939 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700940 for (int i = 0; i < count; i++) {
941 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700942 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
944 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000945 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700946 }
Yura085c8532014-02-11 15:15:29 +0000947
948 try {
949 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
950 } catch (Exception ex) {
951 throw new RuntimeException(ex);
952 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700953
Sunny Goyale9956a72016-09-01 17:24:47 -0700954 synchronized (sBgDataModel) {
955 sBgDataModel.workspaceScreens.clear();
956 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700957 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700958 }
959 };
960 runOnWorkerThread(r);
961 }
962
963 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700964 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 */
Winsonc0b52fe2015-09-09 16:38:15 -0700966 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 final ContentResolver cr = context.getContentResolver();
968
Michael Jurkac9d95c52011-08-29 14:03:34 -0700969 Runnable r = new Runnable() {
970 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700971 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700972 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700973 sBgDataModel.removeItem(info.contents);
974 info.contents.clear();
975
976 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
977 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700978 }
979 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700980 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 }
982
983 /**
984 * Set this as the current Launcher activity object for the loader.
985 */
986 public void initialize(Callbacks callbacks) {
987 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700988 Preconditions.assertUIThread();
989 // Remove any queued UI runnables
990 mHandler.cancelAll();
991 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 }
993 }
994
Kenny Guyed131872014-04-30 03:02:21 +0100995 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100996 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100997 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700998 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +0100999 user));
1000 }
1001
1002 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001003 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001004 onPackagesRemoved(user, packageName);
1005 }
1006
1007 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +01001008 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001009 enqueueItemUpdatedTask(new PackageUpdatedTask(op, packages, user));
Kenny Guyed131872014-04-30 03:02:21 +01001010 }
1011
1012 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001013 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001014 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001015 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001016 user));
1017 }
1018
1019 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001020 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001021 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001022 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001023 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001024 }
1025
1026 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001027 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001028 boolean replacing) {
1029 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001030 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001031 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1032 user));
1033 }
Kenny Guyed131872014-04-30 03:02:21 +01001034 }
1035
Kenny Guy44cba692016-01-21 19:50:02 +00001036 @Override
1037 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001038 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001039 PackageUpdatedTask.OP_SUSPEND, packageNames,
1040 user));
1041 }
1042
1043 @Override
1044 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001045 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001046 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1047 user));
1048 }
1049
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001050 @Override
1051 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1052 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001053 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1054 }
1055
1056 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1057 UserHandleCompat user) {
1058 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001059 }
1060
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001061 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1063 * ACTION_PACKAGE_CHANGED.
1064 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001065 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001066 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001067 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001068
Joe Onorato36115782010-06-17 13:28:48 -04001069 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001070 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001071 // If we have changed locale we need to clear out the labels in all apps/workspace.
1072 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001073 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1074 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001075 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001076 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001077 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1078 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1079 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001080 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1081 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001082 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1083 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1084 enqueueItemUpdatedTask(new PackageUpdatedTask(
1085 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1086 new String[0], user));
1087 }
1088
1089 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1090 // we need to run the state change task again.
1091 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1092 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1093 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1094 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001095 }
Tony Wickham827cef22016-03-17 15:39:39 -07001096 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1097 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001098 }
1099 }
1100
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001101 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001102 resetLoadedState(true, true);
1103
Reena Lee93f824a2011-09-23 17:20:28 -07001104 // Do this here because if the launcher activity is running it will be restarted.
1105 // If it's not running startLoaderFromBackground will merely tell it that it needs
1106 // to reload.
1107 startLoaderFromBackground();
1108 }
1109
Winson Chungf0c6ae02012-03-21 16:10:31 -07001110 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1111 synchronized (mLock) {
1112 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1113 // mWorkspaceLoaded to true later
1114 stopLoaderLocked();
1115 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1116 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001117 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001118 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001119 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001120 }
1121 }
1122
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001123 /**
1124 * When the launcher is in the background, it's possible for it to miss paired
1125 * configuration changes. So whenever we trigger the loader from the background
1126 * tell the launcher that it needs to re-run the loader when it comes back instead
1127 * of doing it now.
1128 */
1129 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001130 Callbacks callbacks = getCallback();
1131 if (callbacks != null) {
1132 // Only actually run the loader if they're not paused.
1133 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001134 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001135 }
1136 }
Joe Onorato36115782010-06-17 13:28:48 -04001137 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001138
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001139 /**
1140 * If there is already a loader task running, tell it to stop.
1141 */
1142 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001143 LoaderTask oldTask = mLoaderTask;
1144 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001145 oldTask.stopLocked();
1146 }
Reena Lee93f824a2011-09-23 17:20:28 -07001147 }
1148
Adam Cohen1a85c582014-09-30 09:48:49 -07001149 public boolean isCurrentCallbacks(Callbacks callbacks) {
1150 return (mCallbacks != null && mCallbacks.get() == callbacks);
1151 }
1152
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001153 /**
1154 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1155 * @return true if the page could be bound synchronously.
1156 */
1157 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001158 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1159 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001160 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001161 // Don't bother to start the thread if we know it's not going to do anything
1162 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001163 final Callbacks oldCallbacks = mCallbacks.get();
1164 // Clear any pending bind-runnables from the synchronized load process.
1165 runOnMainThread(new Runnable() {
1166 public void run() {
1167 oldCallbacks.clearPendingBinds();
1168 }
1169 });
1170
Joe Onorato36115782010-06-17 13:28:48 -04001171 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001172 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001173 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001174 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001175 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1176 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001177 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001178 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 } else {
1180 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1181 sWorker.post(mLoaderTask);
1182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001185 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 }
1187
Joe Onorato36115782010-06-17 13:28:48 -04001188 public void stopLoader() {
1189 synchronized (mLock) {
1190 if (mLoaderTask != null) {
1191 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 }
1193 }
Joe Onorato36115782010-06-17 13:28:48 -04001194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001196 /**
1197 * Loads the workspace screen ids in an ordered list.
1198 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001199 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001200 final ContentResolver contentResolver = context.getContentResolver();
1201 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001202
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001203 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001204 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1205 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001206 }
1207
Joe Onorato36115782010-06-17 13:28:48 -04001208 /**
1209 * Runnable for the thread that loads the contents of the launcher:
1210 * - workspace icons
1211 * - widgets
1212 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001213 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001214 */
1215 private class LoaderTask implements Runnable {
1216 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001217 private int mPageToBindFirst;
1218
Adam Cohen091440a2015-03-18 14:16:05 -07001219 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001220 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001221 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001223 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001224 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001225 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001226 }
1227
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001228 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001229 mIsLoadingAndBindingWorkspace = true;
1230
Joe Onorato36115782010-06-17 13:28:48 -04001231 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001232 if (DEBUG_LOADERS) {
1233 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001235
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001236 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001237 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001238 synchronized (LoaderTask.this) {
1239 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001240 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001241 }
1242 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 }
1244 }
1245
Joe Onorato36115782010-06-17 13:28:48 -04001246 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001247 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001248 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001249
Joe Onorato36115782010-06-17 13:28:48 -04001250 private void waitForIdle() {
1251 // Wait until the either we're stopped or the other threads are done.
1252 // This way we don't start loading all apps until the workspace has settled
1253 // down.
1254 synchronized (LoaderTask.this) {
1255 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001256
Joe Onorato36115782010-06-17 13:28:48 -04001257 mHandler.postIdle(new Runnable() {
1258 public void run() {
1259 synchronized (LoaderTask.this) {
1260 mLoadAndBindStepFinished = true;
1261 if (DEBUG_LOADERS) {
1262 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001263 }
Joe Onorato36115782010-06-17 13:28:48 -04001264 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001265 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001266 }
Joe Onorato36115782010-06-17 13:28:48 -04001267 });
1268
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001269 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001270 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001271 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1272 // wait no longer than 1sec at a time
1273 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001274 } catch (InterruptedException ex) {
1275 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001276 }
1277 }
Joe Onorato36115782010-06-17 13:28:48 -04001278 if (DEBUG_LOADERS) {
1279 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001280 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001281 + "ms for previous step to finish binding");
1282 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001283 }
Joe Onorato36115782010-06-17 13:28:48 -04001284 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001285
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001286 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001287 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001288 // Ensure that we have a valid page index to load synchronously
1289 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1290 "valid page index");
1291 }
1292 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1293 // Ensure that we don't try and bind a specified page when the pages have not been
1294 // loaded already (we should load everything asynchronously in that case)
1295 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1296 }
1297 synchronized (mLock) {
1298 if (mIsLoaderTaskRunning) {
1299 // Ensure that we are never running the background loading at this point since
1300 // we also touch the background collections
1301 throw new RuntimeException("Error! Background loading is already running");
1302 }
1303 }
1304
1305 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1306 // data structures, we can't allow any other thread to touch that data, but because
1307 // this call is synchronous, we can get away with not locking).
1308
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001309 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001310 // operations from the previous activity. We need to ensure that all queued operations
1311 // are executed before any synchronous binding work is done.
1312 mHandler.flush();
1313
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001314 // Divide the set of loaded items into those that we are binding synchronously, and
1315 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001316 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001317 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1318 // arise from that.
1319 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001320
1321 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001322 }
1323
Joe Onorato36115782010-06-17 13:28:48 -04001324 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001325 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001326 if (mStopped) {
1327 return;
1328 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001329 mIsLoaderTaskRunning = true;
1330 }
Joe Onorato36115782010-06-17 13:28:48 -04001331 // Optimize for end-user experience: if the Launcher is up and // running with the
1332 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1333 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001334 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001335 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001336 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001337
Joe Onorato36115782010-06-17 13:28:48 -04001338 if (mStopped) {
1339 break keep_running;
1340 }
1341
Joe Onorato36115782010-06-17 13:28:48 -04001342 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001343
1344 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001345 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1346 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001347
1348 waitForIdle();
1349
1350 // third step
1351 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1352 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354
Joe Onorato36115782010-06-17 13:28:48 -04001355 // Clear out this reference, otherwise we end up holding it until all of the
1356 // callback runnables are done.
1357 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358
Joe Onorato36115782010-06-17 13:28:48 -04001359 synchronized (mLock) {
1360 // If we are still the last one to be scheduled, remove ourselves.
1361 if (mLoaderTask == this) {
1362 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001365 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001366 }
Joe Onorato36115782010-06-17 13:28:48 -04001367 }
1368
1369 public void stopLocked() {
1370 synchronized (LoaderTask.this) {
1371 mStopped = true;
1372 this.notify();
1373 }
1374 }
1375
1376 /**
1377 * Gets the callbacks object. If we've been stopped, or if the launcher object
1378 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1379 * object that was around when the deferred message was scheduled, and if there's
1380 * a new Callbacks object around then also return null. This will save us from
1381 * calling onto it with data that will be ignored.
1382 */
1383 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1384 synchronized (mLock) {
1385 if (mStopped) {
1386 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001387 }
Joe Onorato36115782010-06-17 13:28:48 -04001388
1389 if (mCallbacks == null) {
1390 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001391 }
Joe Onorato36115782010-06-17 13:28:48 -04001392
1393 final Callbacks callbacks = mCallbacks.get();
1394 if (callbacks != oldCallbacks) {
1395 return null;
1396 }
1397 if (callbacks == null) {
1398 Log.w(TAG, "no mCallbacks");
1399 return null;
1400 }
1401
1402 return callbacks;
1403 }
1404 }
1405
1406 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001407 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001408 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001409 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001410 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001411
Adam Cohendcd297f2013-06-18 13:13:40 -07001412 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001413 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001414 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001415 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1416 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001417 Log.e(TAG, "Error loading shortcut into hotseat " + item
1418 + " into position (" + item.screenId + ":" + item.cellX + ","
1419 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001420 return false;
1421 }
1422
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001423 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001424 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1425
Adam Cohen2e6da152015-05-06 11:42:25 -07001426 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001427 Log.e(TAG, "Error loading shortcut " + item
1428 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001429 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001430 + ")");
1431 return false;
1432 }
1433
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001434 if (hotseatOccupancy != null) {
1435 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001436 Log.e(TAG, "Error loading shortcut into hotseat " + item
1437 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001438 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001439 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001440 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001441 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001442 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001443 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001444 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001445 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1446 occupancy.cells[(int) item.screenId][0] = true;
1447 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001448 return true;
1449 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001450 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1451 if (!workspaceScreens.contains((Long) item.screenId)) {
1452 // The item has an invalid screen id.
1453 return false;
1454 }
1455 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001456 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001457 return true;
1458 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001459
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001460 final int countX = profile.numColumns;
1461 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001462 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1463 item.cellX < 0 || item.cellY < 0 ||
1464 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1465 Log.e(TAG, "Error loading shortcut " + item
1466 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1467 + item.cellX + "," + item.cellY
1468 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1469 return false;
1470 }
1471
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001472 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001473 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1474 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001475 // Mark the first row as occupied (if the feature is enabled)
1476 // in order to account for the QSB.
1477 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001478 }
1479 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001480 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001481 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001482
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001483 // Check if any workspace icons overlap with each other
1484 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1485 occupancy.markCells(item, true);
1486 return true;
1487 } else {
1488 Log.e(TAG, "Error loading shortcut " + item
1489 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1490 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1491 + ") already occupied");
1492 return false;
1493 }
Joe Onorato36115782010-06-17 13:28:48 -04001494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001496 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001497 if (LauncherAppState.PROFILE_STARTUP) {
1498 Trace.beginSection("Loading Workspace");
1499 }
Joe Onorato36115782010-06-17 13:28:48 -04001500 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501
Joe Onorato36115782010-06-17 13:28:48 -04001502 final Context context = mContext;
1503 final ContentResolver contentResolver = context.getContentResolver();
1504 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001505 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001506 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001507 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001508 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001509
Winson Chung892c74d2013-08-22 16:15:50 -07001510 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001511 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001512 int countX = profile.numColumns;
1513 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001514
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001515 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001516 try {
1517 ImportDataTask.performImportIfPossible(context);
1518 } catch (Exception e) {
1519 // Migration failed. Clear workspace.
1520 clearDb = true;
1521 }
1522
1523 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001524 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1525 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001526 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001527 }
1528
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001529 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001530 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001531 LauncherSettings.Settings.call(contentResolver,
1532 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001533 }
1534
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001535 Log.d(TAG, "loadWorkspace: loading default favorites");
1536 LauncherSettings.Settings.call(contentResolver,
1537 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001538
Sunny Goyale9956a72016-09-01 17:24:47 -07001539 synchronized (sBgDataModel) {
1540 sBgDataModel.clear();
1541
Sunny Goyal756adbc2015-04-16 15:20:51 -07001542 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001543 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001544 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001545
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001546 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1547 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001548 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001549 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001550 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001551 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001552
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001553 // +1 for the hotseat (it can be larger than the workspace)
1554 // Load workspace in reverse order to ensure that latest items are loaded first (and
1555 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001556 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001557 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001559 try {
1560 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1561 final int intentIndex = c.getColumnIndexOrThrow
1562 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001563 final int containerIndex = c.getColumnIndexOrThrow(
1564 LauncherSettings.Favorites.CONTAINER);
1565 final int itemTypeIndex = c.getColumnIndexOrThrow(
1566 LauncherSettings.Favorites.ITEM_TYPE);
1567 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1568 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001569 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1570 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001571 final int screenIndex = c.getColumnIndexOrThrow(
1572 LauncherSettings.Favorites.SCREEN);
1573 final int cellXIndex = c.getColumnIndexOrThrow
1574 (LauncherSettings.Favorites.CELLX);
1575 final int cellYIndex = c.getColumnIndexOrThrow
1576 (LauncherSettings.Favorites.CELLY);
1577 final int spanXIndex = c.getColumnIndexOrThrow
1578 (LauncherSettings.Favorites.SPANX);
1579 final int spanYIndex = c.getColumnIndexOrThrow(
1580 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001581 final int rankIndex = c.getColumnIndexOrThrow(
1582 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001583 final int restoredIndex = c.getColumnIndexOrThrow(
1584 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001585 final int profileIdIndex = c.getColumnIndexOrThrow(
1586 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001587 final int optionsIndex = c.getColumnIndexOrThrow(
1588 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001589 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590
Sunny Goyal7f834d22015-04-21 10:10:23 -07001591 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001592 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001593 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001594 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001595 long serialNo = mUserManager.getSerialNumberForUser(user);
1596 allUsers.put(serialNo, user);
1597 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001598
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001599 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001600
1601 // We can only query for shortcuts when the user is unlocked.
1602 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001603 List<ShortcutInfoCompat> pinnedShortcuts =
1604 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1605 if (mDeepShortcutManager.wasLastCallSuccess()) {
1606 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1607 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1608 shortcut);
1609 }
1610 } else {
1611 // Shortcut manager can fail due to some race condition when the
1612 // lock state changes too frequently. For the purpose of the loading
1613 // shortcuts, consider the user is still locked.
1614 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001615 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001616 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001617 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001618 }
1619
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 ShortcutInfo info;
1621 String intentDescription;
1622 LauncherAppWidgetInfo appWidgetInfo;
1623 int container;
1624 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001625 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001626 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001627 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001628 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001629
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001630 while (!mStopped && c.moveToNext()) {
1631 try {
1632 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001633 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001634 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001635 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001636
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 switch (itemType) {
1638 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1639 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001640 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001641 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001642 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001643 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001644 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001645 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001646 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001647 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001648 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001649 if (user == null) {
1650 // User has been deleted remove the item.
1651 itemsToRemove.add(id);
1652 continue;
1653 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001654 try {
1655 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001656 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001657 if (cn != null && cn.getPackageName() != null) {
1658 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1659 cn.getPackageName(), user);
1660 boolean validComponent = validPkg &&
1661 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001662 if (validPkg) {
1663 targetPackage = cn.getPackageName();
1664 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001665
1666 if (validComponent) {
1667 if (restored) {
1668 // no special handling necessary for this item
1669 restoredRows.add(id);
1670 restored = false;
1671 }
Kenny Guyff05f432016-01-22 17:48:29 +00001672 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001673 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001674 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001675 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001676 intent = null;
1677 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1678 // We allow auto install apps to have their intent
1679 // updated after an install.
1680 intent = manager.getLaunchIntentForPackage(
1681 cn.getPackageName());
1682 if (intent != null) {
1683 ContentValues values = new ContentValues();
1684 values.put(LauncherSettings.Favorites.INTENT,
1685 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001686 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001687 }
1688 }
1689
1690 if (intent == null) {
1691 // The app is installed but the component is no
1692 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001693 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001694 itemsToRemove.add(id);
1695 continue;
1696 } else {
1697 // no special handling necessary for this item
1698 restoredRows.add(id);
1699 restored = false;
1700 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001701 } else if (restored) {
1702 // Package is not yet available but might be
1703 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001704 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001705
1706 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1707 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001708 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001709 // App restore has started. Update the flag
1710 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1711 ContentValues values = new ContentValues();
1712 values.put(LauncherSettings.Favorites.RESTORED,
1713 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001714 updateItem(id, values);
1715 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1716 // This is a common app. Try to replace this.
1717 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1718 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1719 if (parser.findDefaultApp()) {
1720 // Default app found. Replace it.
1721 intent = parser.parsedIntent;
1722 cn = intent.getComponent();
1723 ContentValues values = parser.parsedValues;
1724 values.put(LauncherSettings.Favorites.RESTORED, 0);
1725 updateItem(id, values);
1726 restored = false;
1727 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001728
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001729 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001730 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001731 itemsToRemove.add(id);
1732 continue;
1733 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001734 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001735 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001736 itemsToRemove.add(id);
1737 continue;
1738 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001739 } else if (PackageManagerHelper.isAppOnSdcard(
1740 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001741 // Package is present but not available.
1742 allowMissingTarget = true;
1743 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1744 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001745 // SdCard is not ready yet. Package might get available,
1746 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001747 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001748 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001749 allowMissingTarget = true;
1750 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001751
1752 } else {
1753 // Do not wait for external media load anymore.
1754 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001755 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001756 itemsToRemove.add(id);
1757 continue;
Winson Chungee055712013-07-30 14:46:24 -07001758 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001759 } else if (cn == null) {
1760 // For shortcuts with no component, keep them as they are
1761 restoredRows.add(id);
1762 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001763 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001765 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001766 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 continue;
1768 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769
Sunny Goyal34b65272015-03-11 16:56:52 -07001770 boolean useLowResIcon = container >= 0 &&
1771 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1772
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001773 if (itemReplaced) {
1774 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001775 info = getAppShortcutInfo(intent, user, null,
1776 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001777 } else {
1778 // Don't replace items for other profiles.
1779 itemsToRemove.add(id);
1780 continue;
1781 }
1782 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001783 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001784 info = getRestoredItemInfo(c, intent,
1785 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001786 intent = getRestoredItemIntent(c, context, intent);
1787 } else {
1788 // Don't restore items for other profiles.
1789 itemsToRemove.add(id);
1790 continue;
1791 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001792 } else if (itemType ==
1793 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001794 info = getAppShortcutInfo(intent, user, c,
1795 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001796 } else if (itemType ==
1797 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001798
1799 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1800 if (unlockedUsers.get(serialNumber)) {
1801 ShortcutInfoCompat pinnedShortcut =
1802 shortcutKeyToPinnedShortcuts.get(key);
1803 if (pinnedShortcut == null) {
1804 // The shortcut is no longer valid.
1805 itemsToRemove.add(id);
1806 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001807 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001808 info = new ShortcutInfo(pinnedShortcut, context);
1809 intent = info.intent;
1810 } else {
1811 // Create a shortcut info in disabled mode for now.
1812 info = new ShortcutInfo();
1813 info.user = user;
1814 info.itemType = itemType;
1815 loadInfoFromCursor(info, c, cursorIconInfo);
1816
1817 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001818 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001819 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001820 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001821
Sunny Goyald09c3702016-04-06 16:18:20 -07001822 // Shortcuts are only available on the primary profile
1823 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1824 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1825 }
1826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 // App shortcuts that used to be automatically added to Launcher
1828 // didn't always have the correct intent flags set, so do that
1829 // here
1830 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001831 intent.getCategories() != null &&
1832 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001833 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 intent.addFlags(
1835 Intent.FLAG_ACTIVITY_NEW_TASK |
1836 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1837 }
Michael Jurka96879562012-03-22 05:54:33 -07001838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001841 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001844 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 info.cellX = c.getInt(cellXIndex);
1846 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001847 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001848 info.spanX = 1;
1849 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001850 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001851 if (info.promisedIntent != null) {
1852 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1853 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001854 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001855 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1856 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1857 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001860 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001861 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 break;
1863 }
1864
Sunny Goyal756adbc2015-04-16 15:20:51 -07001865 if (restored) {
1866 ComponentName cn = info.getTargetComponent();
1867 if (cn != null) {
1868 Integer progress = installingPkgs.get(cn.getPackageName());
1869 if (progress != null) {
1870 info.setInstallProgress(progress);
1871 } else {
1872 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1873 }
1874 }
1875 }
1876
Sunny Goyale9956a72016-09-01 17:24:47 -07001877 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001878 } else {
1879 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 }
1881 break;
1882
1883 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1884 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001885 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886
Sunny Goyala508e4f2015-05-21 09:33:57 -07001887 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001888 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001891 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 folderInfo.cellX = c.getInt(cellXIndex);
1893 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001894 folderInfo.spanX = 1;
1895 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001896 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897
Daniel Sandler8802e962010-05-26 16:28:16 -04001898 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001899 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001900 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001901 break;
1902 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001903 if (restored) {
1904 // no special handling required for restored folders
1905 restoredRows.add(id);
1906 }
1907
Sunny Goyale9956a72016-09-01 17:24:47 -07001908 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 break;
1910
1911 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001912 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001913 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001914 boolean customWidget = itemType ==
1915 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1916
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001918 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001919 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001920 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001921 user = allUsers.get(serialNumber);
1922 if (user == null) {
1923 itemsToRemove.add(id);
1924 continue;
1925 }
1926
Sunny Goyalff572272014-07-23 13:58:07 -07001927 final ComponentName component =
1928 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001929
Sunny Goyal651077b2014-06-30 14:15:31 -07001930 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001931 final boolean isIdValid = (restoreStatus &
1932 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001933 final boolean wasProviderReady = (restoreStatus &
1934 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001935
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001936 if (widgetProvidersMap == null) {
1937 widgetProvidersMap = AppWidgetManagerCompat
1938 .getInstance(mContext).getAllProvidersMap();
1939 }
1940 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1941 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001942 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001943 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001944
1945 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001946 if (!isSafeMode && !customWidget &&
1947 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001948 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001949 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 itemsToRemove.add(id);
1951 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001952 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001953 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1954 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001955
Sunny Goyal53f96722015-07-13 19:54:53 -07001956 // The provider is available. So the widget is either
1957 // available or not available. We do not need to track
1958 // any future restore updates.
1959 int status = restoreStatus &
1960 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001961 if (!wasProviderReady) {
1962 // If provider was not previously ready, update the
1963 // status and UI flag.
1964
1965 // Id would be valid only if the widget restore broadcast was received.
1966 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001967 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001968 } else {
1969 status &= ~LauncherAppWidgetInfo
1970 .FLAG_PROVIDER_NOT_READY;
1971 }
1972 }
1973 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001974 } else {
1975 Log.v(TAG, "Widget restore pending id=" + id
1976 + " appWidgetId=" + appWidgetId
1977 + " status =" + restoreStatus);
1978 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001979 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001980 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001981 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001982
1983 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1984 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001985 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001986 // App restore has started. Update the flag
1987 appWidgetInfo.restoreStatus |=
1988 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001989 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001990 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001991 itemsToRemove.add(id);
1992 continue;
1993 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001994
1995 appWidgetInfo.installProgress =
1996 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001997 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001998 if (appWidgetInfo.hasRestoreFlag(
1999 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2000 intentDescription = c.getString(intentIndex);
2001 if (!TextUtils.isEmpty(intentDescription)) {
2002 appWidgetInfo.bindOptions =
2003 Intent.parseUri(intentDescription, 0);
2004 }
2005 }
Sunny Goyalff572272014-07-23 13:58:07 -07002006
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002008 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 appWidgetInfo.cellX = c.getInt(cellXIndex);
2010 appWidgetInfo.cellY = c.getInt(cellYIndex);
2011 appWidgetInfo.spanX = c.getInt(spanXIndex);
2012 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002013 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002014
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2016 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2017 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002018 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2019 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 continue;
2021 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002023 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07002025 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002026 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 break;
2028 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002029
Adam Cohen59400422014-03-05 18:07:04 -08002030 if (!customWidget) {
2031 String providerName =
2032 appWidgetInfo.providerName.flattenToString();
2033 if (!providerName.equals(savedProvider) ||
2034 (appWidgetInfo.restoreStatus != restoreStatus)) {
2035 ContentValues values = new ContentValues();
2036 values.put(
2037 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2038 providerName);
2039 values.put(LauncherSettings.Favorites.RESTORED,
2040 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002041 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002042 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002043 }
Sunny Goyale9956a72016-09-01 17:24:47 -07002044 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 }
Joe Onorato36115782010-06-17 13:28:48 -04002046 break;
2047 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002049 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002050 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 }
2052 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002053 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 }
2055
Winson Chungba9c37f2013-08-30 14:11:37 -07002056 // Break early if we've stopped loading
2057 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002058 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002059 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002060 }
2061
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002064 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2065 Utilities.createDbSelectionQuery(
2066 LauncherSettings.Favorites._ID, itemsToRemove), null);
2067 if (DEBUG_LOADERS) {
2068 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2069 LauncherSettings.Favorites._ID, itemsToRemove));
2070 }
2071
2072 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002073 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2074 .call(contentResolver,
2075 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2076 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2077 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002078 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
2079 sBgDataModel.folders.remove(folderId);
2080 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002081 }
2082 }
2083
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002084 // Unpin shortcuts that don't exist on the workspace.
2085 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002086 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002087 if (numTimesPinned == null || numTimesPinned.value == 0) {
2088 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2089 mDeepShortcutManager.unpinShortcut(key);
2090 }
2091 }
2092
Sunny Goyal317698b2015-07-29 11:45:41 -07002093 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07002094 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07002095 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2096 int pos = 0;
2097 for (ShortcutInfo info : folder.contents) {
2098 if (info.usingLowResIcon) {
2099 info.updateIcon(mIconCache, false);
2100 }
2101 pos ++;
2102 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2103 break;
2104 }
2105 }
2106 }
2107
Chris Wrenf4d08112014-01-16 18:13:56 -05002108 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002109 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002110 ContentValues values = new ContentValues();
2111 values.put(LauncherSettings.Favorites.RESTORED, 0);
2112 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2113 Utilities.createDbSelectionQuery(
2114 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002115 }
2116
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002117 if (!isSdCardReady && !pendingPackages.isEmpty()) {
2118 context.registerReceiver(
2119 new SdCardAvailableReceiver(
2120 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002121 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002122 null,
2123 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002124 }
2125
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002126 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07002127 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
2128 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002129 long screenId = item.screenId;
2130 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2131 unusedScreens.contains(screenId)) {
2132 unusedScreens.remove(screenId);
2133 }
2134 }
2135
2136 // If there are any empty screens remove them, and update.
2137 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002138 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
2139 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002140 }
2141
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 if (DEBUG_LOADERS) {
2143 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2144 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002145 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002146 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002148
Sunny Goyale2df0622015-04-24 11:27:00 -07002149 for (int i = 0; i < nScreens; i++) {
2150 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002151 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 line += " | ";
2153 }
Joe Onorato36115782010-06-17 13:28:48 -04002154 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002156 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 }
Joe Onorato36115782010-06-17 13:28:48 -04002158 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002159 if (LauncherAppState.PROFILE_STARTUP) {
2160 Trace.endSection();
2161 }
Adam Cohene25af792013-06-06 23:08:25 -07002162 }
2163
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002164 /**
2165 * Partially updates the item without any notification. Must be called on the worker thread.
2166 */
2167 private void updateItem(long itemId, ContentValues update) {
2168 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002169 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002170 update,
2171 BaseColumns._ID + "= ?",
2172 new String[]{Long.toString(itemId)});
2173 }
2174
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 /** Filters the set of items who are directly or indirectly (via another container) on the
2176 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002177 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 ArrayList<ItemInfo> allWorkspaceItems,
2179 ArrayList<ItemInfo> currentScreenItems,
2180 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002181 // Purge any null ItemInfos
2182 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2183 while (iter.hasNext()) {
2184 ItemInfo i = iter.next();
2185 if (i == null) {
2186 iter.remove();
2187 }
2188 }
2189
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 // Order the set of items by their containers first, this allows use to walk through the
2191 // list sequentially, build up a list of containers that are in the specified screen,
2192 // as well as all items in those containers.
2193 Set<Long> itemsOnScreen = new HashSet<Long>();
2194 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2195 @Override
2196 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002197 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002198 }
2199 });
2200 for (ItemInfo info : allWorkspaceItems) {
2201 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002202 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 currentScreenItems.add(info);
2204 itemsOnScreen.add(info.id);
2205 } else {
2206 otherScreenItems.add(info);
2207 }
2208 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2209 currentScreenItems.add(info);
2210 itemsOnScreen.add(info.id);
2211 } else {
2212 if (itemsOnScreen.contains(info.container)) {
2213 currentScreenItems.add(info);
2214 itemsOnScreen.add(info.id);
2215 } else {
2216 otherScreenItems.add(info);
2217 }
2218 }
2219 }
2220 }
2221
2222 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002223 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 ArrayList<LauncherAppWidgetInfo> appWidgets,
2225 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2226 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227
2228 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002229 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002231 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 currentScreenWidgets.add(widget);
2233 } else {
2234 otherScreenWidgets.add(widget);
2235 }
2236 }
2237 }
2238
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2240 * right) */
2241 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002242 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002243 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002244 final int screenCols = profile.numColumns;
2245 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002247 @Override
2248 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002249 if (lhs.container == rhs.container) {
2250 // Within containers, order by their spatial position in that container
2251 switch ((int) lhs.container) {
2252 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2253 long lr = (lhs.screenId * screenCellCount +
2254 lhs.cellY * screenCols + lhs.cellX);
2255 long rr = (rhs.screenId * screenCellCount +
2256 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002257 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002258 }
2259 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2260 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002261 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002262 }
2263 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002264 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002265 throw new RuntimeException("Unexpected container type when " +
2266 "sorting workspace items.");
2267 }
2268 return 0;
2269 }
2270 } else {
2271 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002272 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002273 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002274 }
2275 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002277
Adam Cohendcd297f2013-06-18 13:13:40 -07002278 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2279 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 final Runnable r = new Runnable() {
2281 @Override
2282 public void run() {
2283 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2284 if (callbacks != null) {
2285 callbacks.bindScreens(orderedScreens);
2286 }
2287 }
2288 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002289 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 }
2291
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2293 final ArrayList<ItemInfo> workspaceItems,
2294 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002295 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296
2297 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002298 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002299 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002300 final int start = i;
2301 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 final Runnable r = new Runnable() {
2303 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002304 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002305 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002306 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002307 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2308 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002309 }
2310 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002312 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002313 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002314
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 // Bind the widgets, one at a time
2316 N = appWidgets.size();
2317 for (int i = 0; i < N; i++) {
2318 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2319 final Runnable r = new Runnable() {
2320 public void run() {
2321 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2322 if (callbacks != null) {
2323 callbacks.bindAppWidget(widget);
2324 }
2325 }
2326 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002327 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 }
2329 }
2330
2331 /**
2332 * Binds all loaded data to actual views on the main thread.
2333 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002334 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 final long t = SystemClock.uptimeMillis();
2336 Runnable r;
2337
2338 // Don't use these two variables in any of the callback runnables.
2339 // Otherwise we hold a reference to them.
2340 final Callbacks oldCallbacks = mCallbacks.get();
2341 if (oldCallbacks == null) {
2342 // This launcher has exited and nobody bothered to tell us. Just bail.
2343 Log.w(TAG, "LoaderTask running with no launcher");
2344 return;
2345 }
2346
Winson Chung9b9fb962013-11-15 15:39:34 -08002347 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002348 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2349 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2350 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002351
Sunny Goyale9956a72016-09-01 17:24:47 -07002352 synchronized (sBgDataModel) {
2353 workspaceItems.addAll(sBgDataModel.workspaceItems);
2354 appWidgets.addAll(sBgDataModel.appWidgets);
2355 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 }
2357
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002358 final int currentScreen;
2359 {
2360 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2361 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2362 if (currScreen >= orderedScreenIds.size()) {
2363 // There may be no workspace screens (just hotseat items and an empty page).
2364 currScreen = PagedView.INVALID_RESTORE_PAGE;
2365 }
2366 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002367 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002368 final boolean validFirstPage = currentScreen >= 0;
2369 final long currentScreenId =
2370 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002371
Winson Chung9b9fb962013-11-15 15:39:34 -08002372 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002373 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2374 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2375 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2376 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377
Winson Chung9b9fb962013-11-15 15:39:34 -08002378 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002380 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2383 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2384
2385 // Tell the workspace that we're about to start binding items
2386 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002387 public void run() {
2388 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2389 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002390 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002392 }
2393 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002394 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002395 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396
Adam Cohendcd297f2013-06-18 13:13:40 -07002397 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2398
Sunny Goyal527c7d32015-08-28 15:19:36 -07002399 Executor mainExecutor = new DeferredMainThreadExecutor();
2400 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002401 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002403 // In case of validFirstPage, only bind the first screen, and defer binding the
2404 // remaining screens after first onDraw (and an optional the fade animation whichever
2405 // happens later).
2406 // This ensures that the first screen is immediately visible (eg. during rotation)
2407 // In case of !validFirstPage, bind all pages one after other.
2408 final Executor deferredExecutor =
2409 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2410
2411 mainExecutor.execute(new Runnable() {
2412 @Override
2413 public void run() {
2414 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2415 if (callbacks != null) {
2416 callbacks.finishFirstPageBind(
2417 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2418 }
2419 }
2420 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002421
Sunny Goyal44c06432016-04-02 10:56:02 -07002422 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423
2424 // Tell the workspace that we're done binding items
2425 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002426 public void run() {
2427 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2428 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002429 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002430 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431
Sunny Goyal639e9062015-08-19 19:17:06 -07002432 mIsLoadingAndBindingWorkspace = false;
2433
2434 // Run all the bind complete runnables after workspace is bound.
2435 if (!mBindCompleteRunnables.isEmpty()) {
2436 synchronized (mBindCompleteRunnables) {
2437 for (final Runnable r : mBindCompleteRunnables) {
2438 runOnWorkerThread(r);
2439 }
2440 mBindCompleteRunnables.clear();
2441 }
2442 }
2443
Winson Chung98e030b2012-05-07 16:01:11 -07002444 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002445 if (DEBUG_LOADERS) {
2446 Log.d(TAG, "bound workspace in "
2447 + (SystemClock.uptimeMillis()-t) + "ms");
2448 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002449
Joe Onorato36115782010-06-17 13:28:48 -04002450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002452 deferredExecutor.execute(r);
2453
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002454 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002455 r = new Runnable() {
2456 public void run() {
2457 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2458 if (callbacks != null) {
2459 // We are loading synchronously, which means, some of the pages will be
2460 // bound after first draw. Inform the callbacks that page binding is
2461 // not complete, and schedule the remaining pages.
2462 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2463 callbacks.onPageBoundSynchronously(currentScreen);
2464 }
2465 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2466 }
2467 }
2468 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002469 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002470 }
Joe Onorato36115782010-06-17 13:28:48 -04002471 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002472
Joe Onorato36115782010-06-17 13:28:48 -04002473 private void loadAndBindAllApps() {
2474 if (DEBUG_LOADERS) {
2475 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2476 }
2477 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002478 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002479 synchronized (LoaderTask.this) {
2480 if (mStopped) {
2481 return;
2482 }
2483 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002484 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002485 synchronized (LoaderTask.this) {
2486 if (mStopped) {
2487 return;
2488 }
2489 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002490 }
Joe Onorato36115782010-06-17 13:28:48 -04002491 } else {
2492 onlyBindAllApps();
2493 }
2494 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002495
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002496 private void updateIconCache() {
2497 // Ignore packages which have a promise icon.
2498 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002499 synchronized (sBgDataModel) {
2500 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002501 if (info instanceof ShortcutInfo) {
2502 ShortcutInfo si = (ShortcutInfo) info;
2503 if (si.isPromise() && si.getTargetComponent() != null) {
2504 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2505 }
2506 } else if (info instanceof LauncherAppWidgetInfo) {
2507 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2508 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2509 packagesToIgnore.add(lawi.providerName.getPackageName());
2510 }
2511 }
2512 }
2513 }
2514 mIconCache.updateDbIcons(packagesToIgnore);
2515 }
2516
Joe Onorato36115782010-06-17 13:28:48 -04002517 private void onlyBindAllApps() {
2518 final Callbacks oldCallbacks = mCallbacks.get();
2519 if (oldCallbacks == null) {
2520 // This launcher has exited and nobody bothered to tell us. Just bail.
2521 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2522 return;
2523 }
2524
2525 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002526 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002527 final ArrayList<AppInfo> list
2528 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002529 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002530 public void run() {
2531 final long t = SystemClock.uptimeMillis();
2532 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2533 if (callbacks != null) {
2534 callbacks.bindAllApplications(list);
2535 }
2536 if (DEBUG_LOADERS) {
2537 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002538 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002539 }
2540 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002541 };
Tony Wickham80f57872016-06-29 18:12:15 -07002542 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002543 }
2544
Winson Chung64359a52013-07-08 17:17:08 -07002545 private void loadAllApps() {
2546 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002547
Joe Onorato36115782010-06-17 13:28:48 -04002548 final Callbacks oldCallbacks = mCallbacks.get();
2549 if (oldCallbacks == null) {
2550 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002551 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002552 return;
2553 }
2554
Kenny Guyed131872014-04-30 03:02:21 +01002555 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2556
Winson Chung64359a52013-07-08 17:17:08 -07002557 // Clear the list of apps
2558 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002559 for (UserHandleCompat user : profiles) {
2560 // Query for the set of apps
2561 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002562 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002563 if (DEBUG_LOADERS) {
2564 Log.d(TAG, "getActivityList took "
2565 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2566 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2567 }
2568 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002569 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002570 if (apps == null || apps.isEmpty()) {
2571 return;
2572 }
Kenny Guyff05f432016-01-22 17:48:29 +00002573 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002574 // Create the ApplicationInfos
2575 for (int i = 0; i < apps.size(); i++) {
2576 LauncherActivityInfoCompat app = apps.get(i);
2577 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002578 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002579 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002580
Sunny Goyal756a28a2015-04-23 17:07:55 -07002581 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2582 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002583 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002584
2585 @Override
2586 public void run() {
2587 heuristic.processUserApps(apps);
2588 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002589 };
2590 runOnMainThread(new Runnable() {
2591
2592 @Override
2593 public void run() {
2594 // Check isLoadingWorkspace on the UI thread, as it is updated on
2595 // the UI thread.
2596 if (mIsLoadingAndBindingWorkspace) {
2597 synchronized (mBindCompleteRunnables) {
2598 mBindCompleteRunnables.add(r);
2599 }
2600 } else {
2601 runOnWorkerThread(r);
2602 }
2603 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002604 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002605 }
Winson Chung64359a52013-07-08 17:17:08 -07002606 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002607 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002608 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2609 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002610
2611 // Post callback on main thread
2612 mHandler.post(new Runnable() {
2613 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002614
Winson Chung64359a52013-07-08 17:17:08 -07002615 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002616 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002617 if (callbacks != null) {
2618 callbacks.bindAllApplications(added);
2619 if (DEBUG_LOADERS) {
2620 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002621 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002622 }
2623 } else {
2624 Log.i(TAG, "not binding apps: no Launcher activity");
2625 }
2626 }
2627 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002628 // Cleanup any data stored for a deleted user.
2629 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002630 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002631 Log.d(TAG, "Icons processed in "
2632 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002633 }
2634 }
2635
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002636 private void loadAndBindDeepShortcuts() {
2637 if (DEBUG_LOADERS) {
2638 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2639 }
2640 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002641 sBgDataModel.deepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002642 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2643 if (mHasShortcutHostPermission) {
2644 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2645 if (mUserManager.isUserUnlocked(user)) {
2646 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2647 .queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002648 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002649 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002650 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002651 }
2652 synchronized (LoaderTask.this) {
2653 if (mStopped) {
2654 return;
2655 }
2656 mDeepShortcutsLoaded = true;
2657 }
2658 }
Tony Wickham80f57872016-06-29 18:12:15 -07002659 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002660 }
2661
Joe Onoratobe386092009-11-17 17:32:16 -08002662 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002663 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002664 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002665 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2666 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002667 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668 }
Joe Onorato36115782010-06-17 13:28:48 -04002669 }
2670 }
2671
Tony Wickham80f57872016-06-29 18:12:15 -07002672 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002673 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2674 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002675 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002676 @Override
2677 public void run() {
2678 Callbacks callbacks = getCallback();
2679 if (callbacks != null) {
2680 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2681 }
2682 }
Tony Wickham80f57872016-06-29 18:12:15 -07002683 };
2684 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002685 }
2686
Sunny Goyal75b0f552015-05-20 21:57:06 -07002687 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002688 * Refreshes the cached shortcuts if the shortcut permission has changed.
2689 * Current implementation simply reloads the workspace, but it can be optimized to
2690 * use partial updates similar to {@link UserManagerCompat}
2691 */
2692 public void refreshShortcutsIfRequired() {
2693 if (Utilities.isNycMR1OrAbove()) {
2694 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2695 sWorker.post(mShortcutPermissionCheckRunnable);
2696 }
2697 }
2698
2699 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002700 * Called when the icons for packages have been updated in the icon cache.
2701 */
2702 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2703 final Callbacks callbacks = getCallback();
2704 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2705 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2706
2707 // If any package icon has changed (app was updated while launcher was dead),
2708 // update the corresponding shortcuts.
Sunny Goyale9956a72016-09-01 17:24:47 -07002709 synchronized (sBgDataModel) {
2710 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002711 if (info instanceof ShortcutInfo && user.equals(info.user)
2712 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2713 ShortcutInfo si = (ShortcutInfo) info;
2714 ComponentName cn = si.getTargetComponent();
2715 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2716 si.updateIcon(mIconCache);
2717 updatedShortcuts.add(si);
2718 }
2719 }
2720 }
2721 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2722 }
2723
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002724 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002725
2726 if (!updatedApps.isEmpty()) {
2727 mHandler.post(new Runnable() {
2728
2729 public void run() {
2730 Callbacks cb = getCallback();
2731 if (cb != null && callbacks == cb) {
2732 cb.bindAppsUpdated(updatedApps);
2733 }
2734 }
2735 });
2736 }
2737 }
2738
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002739 private void bindUpdatedShortcuts(
2740 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2741 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2742 }
2743
2744 private void bindUpdatedShortcuts(
2745 final ArrayList<ShortcutInfo> updatedShortcuts,
2746 final ArrayList<ShortcutInfo> removedShortcuts,
2747 final UserHandleCompat user) {
2748 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002749 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002750 mHandler.post(new Runnable() {
2751
2752 public void run() {
2753 Callbacks cb = getCallback();
2754 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002755 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002756 }
2757 }
2758 });
2759 }
2760 }
2761
2762 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002763 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002764 }
2765
2766 private class PackageUpdatedTask implements Runnable {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002767 final int mOp;
2768 final String[] mPackages;
2769 final UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002770
2771 public static final int OP_NONE = 0;
2772 public static final int OP_ADD = 1;
2773 public static final int OP_UPDATE = 2;
2774 public static final int OP_REMOVE = 3; // uninstlled
2775 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002776 public static final int OP_SUSPEND = 5; // package suspended
2777 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002778 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002779
Kenny Guyed131872014-04-30 03:02:21 +01002780 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002781 mOp = op;
2782 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002783 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002784 }
2785
2786 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002787 if (!mHasLoaderCompletedOnce) {
2788 // Loader has not yet run.
2789 return;
2790 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002791 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002792
2793 final String[] packages = mPackages;
2794 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002795 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyal40452cf2016-09-01 15:17:46 -07002796 final HashSet<String> packageSet = new HashSet<>(Arrays.asList(packages));
Joe Onorato36115782010-06-17 13:28:48 -04002797 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002798 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002799 for (int i=0; i<N; i++) {
2800 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002801 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002802 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002803 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002804
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002805 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2806 if (heuristic != null) {
2807 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002808 }
Joe Onorato36115782010-06-17 13:28:48 -04002809 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002810 }
Joe Onorato36115782010-06-17 13:28:48 -04002811 case OP_UPDATE:
2812 for (int i=0; i<N; i++) {
2813 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002814 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002815 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002816 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002817 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002818 // Since package was just updated, the target must be available now.
2819 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002820 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002821 case OP_REMOVE: {
2822 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2823 if (heuristic != null) {
2824 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002825 }
Joe Onorato36115782010-06-17 13:28:48 -04002826 for (int i=0; i<N; i++) {
2827 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002828 mIconCache.removeIconsForPkg(packages[i], mUser);
2829 }
2830 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002831 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002832 case OP_UNAVAILABLE:
2833 for (int i=0; i<N; i++) {
2834 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2835 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002836 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002837 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002838 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002839 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002840 case OP_SUSPEND:
2841 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002842 flagOp = mOp == OP_SUSPEND ?
2843 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2844 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002845 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyal40452cf2016-09-01 15:17:46 -07002846 mBgAllAppsList.updateDisabledFlags(
2847 ItemInfoMatcher.ofPackages(packageSet, mUser), flagOp);
Sunny Goyalda891c12016-03-18 18:29:24 -07002848 break;
2849 case OP_USER_AVAILABILITY_CHANGE:
2850 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2851 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2852 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2853 // We want to update all packages for this user.
Sunny Goyal40452cf2016-09-01 15:17:46 -07002854 mBgAllAppsList.updateDisabledFlags(ItemInfoMatcher.ofUser(mUser), flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002855 break;
Joe Onorato36115782010-06-17 13:28:48 -04002856 }
2857
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002858 ArrayList<AppInfo> added = null;
2859 ArrayList<AppInfo> modified = null;
2860 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002861
Adam Cohen487f7dd2012-06-28 18:12:10 -07002862 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002863 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002864 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002865 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002866 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002867 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002868 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002869 }
Winson Chung5d55f332012-07-16 20:45:03 -07002870 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002871 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002872 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002873 }
2874
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002875 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002876
Joe Onorato36115782010-06-17 13:28:48 -04002877 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002878 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002879 for (AppInfo ai : added) {
2880 addedOrUpdatedApps.put(ai.componentName, ai);
2881 }
Joe Onorato36115782010-06-17 13:28:48 -04002882 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002883
Joe Onorato36115782010-06-17 13:28:48 -04002884 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002885 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002886 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002887 for (AppInfo ai : modified) {
2888 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002889 }
2890
Joe Onorato36115782010-06-17 13:28:48 -04002891 mHandler.post(new Runnable() {
2892 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002893 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002894 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002895 callbacks.bindAppsUpdated(modifiedFinal);
2896 }
2897 }
2898 });
2899 }
Winson Chung83892cc2013-05-01 16:53:33 -07002900
Sunny Goyal4390ace2014-10-13 11:33:11 -07002901 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002902 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002903 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2904 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<>();
2905 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002906
Sunny Goyale9956a72016-09-01 17:24:47 -07002907 synchronized (sBgDataModel) {
2908 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002909 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2910 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002911 boolean infoUpdated = false;
2912 boolean shortcutUpdated = false;
2913
2914 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002915 if ((si.iconResource != null)
Sunny Goyal40452cf2016-09-01 15:17:46 -07002916 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal10629b02016-09-01 12:50:11 -07002917 Bitmap icon = LauncherIcons.createIconBitmap(
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002918 si.iconResource.packageName,
2919 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002920 if (icon != null) {
2921 si.setIcon(icon);
2922 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002923 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002924 }
2925 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002926
2927 ComponentName cn = si.getTargetComponent();
Sunny Goyal40452cf2016-09-01 15:17:46 -07002928 if (cn != null && packageSet.contains(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002929 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2930
2931 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002932 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2933 // Auto install icon
2934 PackageManager pm = context.getPackageManager();
2935 ResolveInfo matched = pm.resolveActivity(
2936 new Intent(Intent.ACTION_MAIN)
2937 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2938 PackageManager.MATCH_DEFAULT_ONLY);
2939 if (matched == null) {
2940 // Try to find the best match activity.
2941 Intent intent = pm.getLaunchIntentForPackage(
2942 cn.getPackageName());
2943 if (intent != null) {
2944 cn = intent.getComponent();
2945 appInfo = addedOrUpdatedApps.get(cn);
2946 }
2947
2948 if ((intent == null) || (appInfo == null)) {
2949 removedShortcuts.add(si);
2950 continue;
2951 }
2952 si.promisedIntent = intent;
2953 }
2954 }
2955
Sunny Goyal756adbc2015-04-16 15:20:51 -07002956 si.intent = si.promisedIntent;
2957 si.promisedIntent = null;
2958 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002959 infoUpdated = true;
2960 si.updateIcon(mIconCache);
2961 }
2962
2963 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2964 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2965 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002966 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002967 si.contentDescription = appInfo.contentDescription;
2968 infoUpdated = true;
2969 }
2970
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002971 int oldDisabledFlags = si.isDisabled;
2972 si.isDisabled = flagOp.apply(si.isDisabled);
2973 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002974 shortcutUpdated = true;
2975 }
2976 }
2977
2978 if (infoUpdated || shortcutUpdated) {
2979 updatedShortcuts.add(si);
2980 }
2981 if (infoUpdated) {
2982 updateItemInDatabase(context, si);
2983 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002984 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002985 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2986 if (mUser.equals(widgetInfo.user)
2987 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyal40452cf2016-09-01 15:17:46 -07002988 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002989 widgetInfo.restoreStatus &=
2990 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2991 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002992
2993 // adding this flag ensures that launcher shows 'click to setup'
2994 // if the widget has a config activity. In case there is no config
2995 // activity, it will be marked as 'restored' during bind.
2996 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2997
Sunny Goyal4390ace2014-10-13 11:33:11 -07002998 widgets.add(widgetInfo);
2999 updateItemInDatabase(context, widgetInfo);
3000 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003001 }
3002 }
3003 }
3004
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003005 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3006 if (!removedShortcuts.isEmpty()) {
3007 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003008 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003009
Sunny Goyal4390ace2014-10-13 11:33:11 -07003010 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003011 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003012 mHandler.post(new Runnable() {
3013 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003014 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003015 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003016 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003017 }
3018 }
3019 });
3020 }
3021 }
3022
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003023 final HashSet<String> removedPackages = new HashSet<>();
3024 final HashSet<ComponentName> removedComponents = new HashSet<>();
3025 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003026 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003027 Collections.addAll(removedPackages, packages);
3028
3029 // No need to update the removedComponents as
3030 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003031 } else if (mOp == OP_UPDATE) {
3032 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003033 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003034 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003035 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003036 }
3037 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003038
3039 // Update removedComponents as some components can get removed during package update
3040 for (AppInfo info : removedApps) {
3041 removedComponents.add(info.componentName);
3042 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003043 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003044
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003045 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07003046 deleteItemsFromDatabase(
3047 context, ItemInfoMatcher.ofPackages(removedPackages, mUser));
3048 deleteItemsFromDatabase(
3049 context, ItemInfoMatcher.ofComponents(removedComponents, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003050
Winson Chungdf95eb12013-10-16 14:57:07 -07003051 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003052 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3053
Winson Chungdf95eb12013-10-16 14:57:07 -07003054 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003055 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003056 mHandler.post(new Runnable() {
3057 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003058 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003059 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003060 callbacks.bindWorkspaceComponentsRemoved(
3061 removedPackages, removedComponents, mUser);
3062 }
3063 }
3064 });
3065 }
3066
3067 if (!removedApps.isEmpty()) {
3068 // Remove corresponding apps from All-Apps
3069 final Callbacks callbacks = getCallback();
3070 mHandler.post(new Runnable() {
3071 public void run() {
3072 Callbacks cb = getCallback();
3073 if (callbacks == cb && cb != null) {
3074 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003075 }
3076 }
3077 });
Joe Onoratobe386092009-11-17 17:32:16 -08003078 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003079
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003080 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3081 // get widget update signals.
3082 if (!Utilities.ATLEAST_MARSHMALLOW &&
3083 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003084 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003085 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003086 public void run() {
3087 Callbacks cb = getCallback();
3088 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003089 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003090 }
3091 }
3092 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003093 }
3094 }
3095 }
3096
Sunny Goyal10923b32016-07-20 15:42:44 -07003097 /**
3098 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3099 */
3100 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3101 enqueueItemUpdatedTask(new Runnable() {
3102 @Override
3103 public void run() {
3104 info.updateFromDeepShortcutInfo(
3105 fullDetail, LauncherAppState.getInstance().getContext());
3106 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3107 update.add(info);
3108 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3109 }
3110 });
3111 }
3112
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003113 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003114 private final String mPackageName;
3115 private final List<ShortcutInfoCompat> mShortcuts;
3116 private final UserHandleCompat mUser;
3117 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003118
3119 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003120 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003121 mPackageName = packageName;
3122 mShortcuts = shortcuts;
3123 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003124 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003125 }
3126
3127 @Override
3128 public void run() {
3129 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3130
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003131 // Find ShortcutInfo's that have changed on the workspace.
Tony Wickham7695e602016-08-05 12:44:51 -07003132 final ArrayList<ShortcutInfo> removedShortcutInfos = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003133 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003134 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003135 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3136 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003137 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3138 && si.user.equals(mUser)) {
Tony Wickham7695e602016-08-05 12:44:51 -07003139 idsToWorkspaceShortcutInfos.addToList(si.getDeepShortcutId(), si);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003140 }
3141 }
3142 }
3143
Tony Wickham7695e602016-08-05 12:44:51 -07003144 final Context context = LauncherAppState.getInstance().getContext();
3145 final ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3146 if (!idsToWorkspaceShortcutInfos.isEmpty()) {
3147 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3148 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3149 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3150 for (ShortcutInfoCompat fullDetails : shortcuts) {
3151 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3152 .remove(fullDetails.getId());
3153 if (!fullDetails.isPinned()) {
3154 // The shortcut was previously pinned but is no longer, so remove it from
3155 // the workspace and our pinned shortcut counts.
3156 // Note that we put this check here, after querying for full details,
3157 // because there's a possible race condition between pinning and
3158 // receiving this callback.
3159 removedShortcutInfos.addAll(shortcutInfos);
3160 continue;
3161 }
3162 for (ShortcutInfo shortcutInfo : shortcutInfos) {
3163 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
3164 updatedShortcutInfos.add(shortcutInfo);
3165 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003166 }
3167 }
Tony Wickham7695e602016-08-05 12:44:51 -07003168
3169 // If there are still entries in idsToWorkspaceShortcutInfos, that means that
3170 // the corresponding shortcuts weren't passed in onShortcutsChanged(). This
3171 // means they were cleared, so we remove and unpin them now.
3172 for (String id : idsToWorkspaceShortcutInfos.keySet()) {
3173 removedShortcutInfos.addAll(idsToWorkspaceShortcutInfos.get(id));
3174 }
3175
3176 bindUpdatedShortcuts(updatedShortcutInfos, removedShortcutInfos, mUser);
3177 if (!removedShortcutInfos.isEmpty()) {
3178 deleteItemsFromDatabase(context, removedShortcutInfos);
3179 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003180
Sunny Goyal50941fb2016-08-04 12:03:52 -07003181 if (mUpdateIdMap) {
3182 // Update the deep shortcut map if the list of ids has changed for an activity.
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003183 sBgDataModel.updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Sunny Goyal50941fb2016-08-04 12:03:52 -07003184 bindDeepShortcuts();
3185 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003186 }
3187 }
3188
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003189 /**
3190 * Task to handle changing of lock state of the user
3191 */
3192 private class UserLockStateChangedTask implements Runnable {
3193
3194 private final UserHandleCompat mUser;
3195
3196 public UserLockStateChangedTask(UserHandleCompat user) {
3197 mUser = user;
3198 }
3199
3200 @Override
3201 public void run() {
3202 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3203 Context context = mApp.getContext();
3204
3205 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3206 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003207 List<ShortcutInfoCompat> shortcuts =
3208 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3209 if (mDeepShortcutManager.wasLastCallSuccess()) {
3210 for (ShortcutInfoCompat shortcut : shortcuts) {
3211 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3212 }
3213 } else {
3214 // Shortcut manager can fail due to some race condition when the lock state
3215 // changes too frequently. For the purpose of the update,
3216 // consider it as still locked.
3217 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003218 }
3219 }
3220
3221 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3222 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3223 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003224 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003225 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3226 && mUser.equals(itemInfo.user)) {
3227 ShortcutInfo si = (ShortcutInfo) itemInfo;
3228 if (isUserUnlocked) {
3229 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003230 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003231 // We couldn't verify the shortcut during loader. If its no longer available
3232 // (probably due to clear data), delete the workspace item as well
3233 if (shortcut == null) {
3234 deletedShortcutInfos.add(si);
3235 continue;
3236 }
3237 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3238 si.updateFromDeepShortcutInfo(shortcut, context);
3239 } else {
3240 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3241 }
3242 updatedShortcutInfos.add(si);
3243 }
3244 }
3245 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3246 if (!deletedShortcutInfos.isEmpty()) {
3247 deleteItemsFromDatabase(context, deletedShortcutInfos);
3248 }
3249
3250 // Remove shortcut id map for that user
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003251 Iterator<ComponentKey> keysIter = sBgDataModel.deepShortcutMap.keySet().iterator();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003252 while (keysIter.hasNext()) {
3253 if (keysIter.next().user.equals(mUser)) {
3254 keysIter.remove();
3255 }
3256 }
3257
3258 if (isUserUnlocked) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003259 sBgDataModel.updateDeepShortcutMap(
3260 null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003261 }
3262 bindDeepShortcuts();
3263 }
3264 }
3265
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003266 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3267 mHandler.post(new Runnable() {
3268 @Override
3269 public void run() {
3270 Callbacks cb = getCallback();
3271 if (callbacks == cb && cb != null) {
3272 callbacks.bindWidgetsModel(model);
3273 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003274 }
3275 });
3276 }
3277
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003278 public void refreshAndBindWidgetsAndShortcuts(
3279 final Callbacks callbacks, final boolean bindFirst) {
3280 runOnWorkerThread(new Runnable() {
3281 @Override
3282 public void run() {
3283 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3284 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003285 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003286 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3287 bindWidgetsModel(callbacks, model);
3288 // update the Widget entries inside DB on the worker thread.
3289 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3290 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003291 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003292 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003293 }
3294
Adam Cohen091440a2015-03-18 14:16:05 -07003295 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003296 UserHandleCompat user) {
3297 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3298 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003299 }
Adam Cohen556f6132014-01-15 15:18:08 -08003300
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003302 * Make an ShortcutInfo object for a restored application or shortcut item that points
3303 * to a package that is not yet installed on the system.
3304 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003305 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3306 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003307 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003308 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003309
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003310 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003311 // the fallback icon
3312 if (icon == null) {
3313 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3314 } else {
3315 info.setIcon(icon);
3316 }
Sunny Goyal34942622014-08-29 17:20:55 -07003317
3318 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003319 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003320 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003321 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003322 }
Sunny Goyal34942622014-08-29 17:20:55 -07003323 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3324 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003325 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003326 }
Sunny Goyal34942622014-08-29 17:20:55 -07003327 } else {
3328 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3329 }
3330
Winson Chung82b016c2015-05-08 17:00:10 -07003331 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003332 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003333 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003334 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003335 return info;
3336 }
3337
3338 /**
3339 * Make an Intent object for a restored application or shortcut item that points
3340 * to the market page for the item.
3341 */
Adam Cohen091440a2015-03-18 14:16:05 -07003342 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003343 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003344 return getMarketIntent(componentName.getPackageName());
3345 }
3346
3347 static Intent getMarketIntent(String packageName) {
3348 return new Intent(Intent.ACTION_VIEW)
3349 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003350 .scheme("market")
3351 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003352 .appendQueryParameter("id", packageName)
3353 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003354 }
3355
3356 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003357 * Make an ShortcutInfo object for a shortcut that is an application.
3358 *
3359 * If c is not null, then it will be used to fill in missing data like the title and icon.
3360 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003361 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003362 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003363 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003364 if (user == null) {
3365 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003366 return null;
3367 }
3368
Kenny Guyed131872014-04-30 03:02:21 +01003369 ComponentName componentName = intent.getComponent();
3370 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003371 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003372 return null;
3373 }
3374
3375 Intent newIntent = new Intent(intent.getAction(), null);
3376 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3377 newIntent.setComponent(componentName);
3378 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003379 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003380 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3381 return null;
3382 }
3383
3384 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003385 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003386 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003387 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003388 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003389 }
3390
Sunny Goyald09c3702016-04-06 16:18:20 -07003391 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3392 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3393 }
3394
Joe Onorato56d82912010-03-07 14:32:10 -05003395 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003396 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003397 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003398 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003399
Joe Onorato56d82912010-03-07 14:32:10 -05003400 // fall back to the class name of the activity
3401 if (info.title == null) {
3402 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003403 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003404
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003406 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003407 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003409 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003410
Sunny Goyal1a745e82014-10-02 15:58:31 -07003411 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003412 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003413 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003414 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003415 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003416 // Non-app shortcuts are only supported for current user.
3417 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003419
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003420 // TODO: If there's an explicit component and we can't install that, delete it.
3421
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003422 loadInfoFromCursor(info, c, iconInfo);
3423 return info;
3424 }
Joe Onorato56d82912010-03-07 14:32:10 -05003425
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003426 /**
3427 * Make an ShortcutInfo object for a shortcut that isn't an application.
3428 */
3429 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3430 info.title = iconInfo.getTitle(c);
3431 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003432 // the fallback icon
3433 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003434 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003435 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003437 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003439
Sunny Goyal2350bc92014-10-14 16:42:54 -07003440 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003441 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3442 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3443 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3444
Adam Cohend9198822011-11-22 16:42:47 -08003445 if (intent == null) {
3446 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3447 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3448 return null;
3449 }
3450
Joe Onorato0589f0f2010-02-08 13:44:00 -08003451 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003452 ShortcutIconResource iconResource = null;
3453
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003454 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003455 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003456 } else {
3457 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003458 if (extra instanceof ShortcutIconResource) {
3459 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07003460 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003461 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003462 }
3463 }
3464
Michael Jurkac9d95c52011-08-29 14:03:34 -07003465 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003466
Kenny Guyed131872014-04-30 03:02:21 +01003467 // Only support intents for current user for now. Intents sent from other
3468 // users wouldn't get here without intent forwarding anyway.
3469 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003470 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003471 icon = mIconCache.getDefaultIcon(info.user);
3472 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003473 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003474 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003475
Winson Chung82b016c2015-05-08 17:00:10 -07003476 info.title = Utilities.trim(name);
3477 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003478 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003479 info.iconResource = iconResource;
3480
3481 return info;
3482 }
3483
Sunny Goyal651077b2014-06-30 14:15:31 -07003484 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3485 return (provider != null) && (provider.provider != null)
3486 && (provider.provider.getPackageName() != null);
3487 }
3488
Joe Onoratobe386092009-11-17 17:32:16 -08003489 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003490 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003491 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3492 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3493 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3494 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003495 if (mLoaderTask != null) {
3496 mLoaderTask.dumpState();
3497 } else {
3498 Log.d(TAG, "mLoaderTask=null");
3499 }
Joe Onoratobe386092009-11-17 17:32:16 -08003500 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003501
3502 public Callbacks getCallback() {
3503 return mCallbacks != null ? mCallbacks.get() : null;
3504 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003505
3506 /**
3507 * @return {@link FolderInfo} if its already loaded.
3508 */
3509 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07003510 synchronized (sBgDataModel) {
3511 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003512 }
3513 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003514
Sunny Goyal527c7d32015-08-28 15:19:36 -07003515 @Thunk class DeferredMainThreadExecutor implements Executor {
3516
3517 @Override
3518 public void execute(Runnable command) {
3519 runOnMainThread(command);
3520 }
3521 }
3522
Sunny Goyal756adbc2015-04-16 15:20:51 -07003523 /**
3524 * @return the looper for the worker thread which can be used to start background tasks.
3525 */
3526 public static Looper getWorkerLooper() {
3527 return sWorkerThread.getLooper();
3528 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003529}