blob: 3f9c2a32438d06d1c6437c217a0d5f12ffc279c9 [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) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700692 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700693 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695
696 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700698 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
699 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800700 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702
Michael Jurkac9d95c52011-08-29 14:03:34 -0700703 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700704 }
705
706 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
708 * cellX, cellY have already been updated on the ItemInfos.
709 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800710 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700711 final long container, final int screen) {
712
713 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
714 int count = items.size();
715
716 for (int i = 0; i < count; i++) {
717 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700718 item.container = container;
719
720 // We store hotseat items in canonical form which is this orientation invariant position
721 // in the hotseat
722 if (context instanceof Launcher && screen < 0 &&
723 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700724 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 item.cellY);
726 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700728 }
729
730 final ContentValues values = new ContentValues();
731 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
732 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
733 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800734 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700735 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736
737 contentValues.add(values);
738 }
739 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
740 }
741
742 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700743 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800744 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700745 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700746 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700747 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800748 item.cellX = cellX;
749 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700750 item.spanX = spanX;
751 item.spanY = spanY;
752
753 // We store hotseat items in canonical form which is this orientation invariant position
754 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700755 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700756 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700757 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700758 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700759 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700760 }
Adam Cohend4844c32011-02-18 19:25:06 -0800761
Adam Cohend4844c32011-02-18 19:25:06 -0800762 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800763 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700764 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
765 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800766 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700767 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
768 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700769 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800770
Michael Jurka816474f2012-06-25 14:49:02 -0700771 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700772 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700773
774 /**
775 * Update an item to the database in a specified container.
776 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700777 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700778 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100779 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700780 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800781 }
782
Sunny Goyal756a28a2015-04-23 17:07:55 -0700783 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700784 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700785 synchronized (mLock) {
786 if (!mHasLoaderCompletedOnce ||
787 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
788 throw new RuntimeException("Trying to add shortcut while loader is running");
789 }
790 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700791 }
792 }
793
Adam Cohend4844c32011-02-18 19:25:06 -0800794 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700795 * Returns true if the shortcuts already exists on the workspace. This must be called after
796 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700798 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
799 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700800 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700801 if (intent.getComponent() != null) {
802 // If component is not null, an intent with null package will produce
803 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700804 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700805 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700806 intentWithPkg = intent.toUri(0);
807 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700808 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700809 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
810 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700811 }
812 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700813 intentWithPkg = intent.toUri(0);
814 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700815 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700816
Sunny Goyale9956a72016-09-01 17:24:47 -0700817 synchronized (sBgDataModel) {
818 for (ItemInfo item : sBgDataModel.itemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700819 if (item instanceof ShortcutInfo) {
820 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700821 Intent targetIntent = info.promisedIntent == null
822 ? info.intent : info.promisedIntent;
823 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700824 Intent copyIntent = new Intent(targetIntent);
825 copyIntent.setSourceBounds(intent.getSourceBounds());
826 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700827 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
828 return true;
829 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700830 }
831 }
832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700834 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700835 }
836
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 * Add an item to the database in a specified container. Sets the container, screen, cellX and
839 * cellY fields of the item. Also assigns an ID to the item.
840 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700841 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700842 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 item.cellX = cellX;
845 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 // We store hotseat items in canonical form which is this orientation invariant position
847 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854
855 final ContentValues values = new ContentValues();
856 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100857 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858
Sunny Goyald2497482015-09-22 18:24:19 -0700859 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
860 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
861
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700862 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700863
Jason Monk8e19cf22014-03-20 15:06:57 -0400864 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700865 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700866 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700867 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868
Sunny Goyale9956a72016-09-01 17:24:47 -0700869 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400870 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700871 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700872 }
873 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700874 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700875 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700876 }
877
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700878 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700879 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700881 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700882 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700883 items.add(item);
884 deleteItemsFromDatabase(context, items);
885 }
886
887 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700888 * Removes all the items from the database matching {@param matcher}.
889 */
890 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700891 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700892 }
893
894 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700895 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700896 */
Sunny Goyal40452cf2016-09-01 15:17:46 -0700897 static void deleteItemsFromDatabase(Context context, final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700899 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700900 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700901 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700902 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700903 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700904
Sunny Goyale9956a72016-09-01 17:24:47 -0700905 sBgDataModel.removeItem(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700906 }
907 }
Michael Jurka83df1882011-08-31 20:59:26 -0700908 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700909 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 }
911
912 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700913 * Update the order of the workspace screens in the database. The array list contains
914 * a list of screen ids in the order that they should appear.
915 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700916 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700917 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700918 final ContentResolver cr = context.getContentResolver();
919 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
920
921 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700922 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 while (iter.hasNext()) {
924 long id = iter.next();
925 if (id < 0) {
926 iter.remove();
927 }
928 }
929
930 Runnable r = new Runnable() {
931 @Override
932 public void run() {
Yura085c8532014-02-11 15:15:29 +0000933 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000935 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700936 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 for (int i = 0; i < count; i++) {
938 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700939 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700940 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
941 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000942 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 }
Yura085c8532014-02-11 15:15:29 +0000944
945 try {
946 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
947 } catch (Exception ex) {
948 throw new RuntimeException(ex);
949 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700950
Sunny Goyale9956a72016-09-01 17:24:47 -0700951 synchronized (sBgDataModel) {
952 sBgDataModel.workspaceScreens.clear();
953 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700954 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700955 }
956 };
957 runOnWorkerThread(r);
958 }
959
960 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700961 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962 */
Winsonc0b52fe2015-09-09 16:38:15 -0700963 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964 final ContentResolver cr = context.getContentResolver();
965
Michael Jurkac9d95c52011-08-29 14:03:34 -0700966 Runnable r = new Runnable() {
967 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700968 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700969 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700970 sBgDataModel.removeItem(info.contents);
971 info.contents.clear();
972
973 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
974 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700975 }
976 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700977 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 }
979
980 /**
981 * Set this as the current Launcher activity object for the loader.
982 */
983 public void initialize(Callbacks callbacks) {
984 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700985 Preconditions.assertUIThread();
986 // Remove any queued UI runnables
987 mHandler.cancelAll();
988 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 }
990 }
991
Kenny Guyed131872014-04-30 03:02:21 +0100992 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100993 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100994 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700995 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +0100996 user));
997 }
998
999 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001000 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001001 onPackagesRemoved(user, packageName);
1002 }
1003
1004 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +01001005 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001006 enqueueItemUpdatedTask(new PackageUpdatedTask(op, packages, user));
Kenny Guyed131872014-04-30 03:02:21 +01001007 }
1008
1009 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001010 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001011 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001012 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001013 user));
1014 }
1015
1016 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001017 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001018 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001019 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001020 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001021 }
1022
1023 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001024 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001025 boolean replacing) {
1026 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001027 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001028 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1029 user));
1030 }
Kenny Guyed131872014-04-30 03:02:21 +01001031 }
1032
Kenny Guy44cba692016-01-21 19:50:02 +00001033 @Override
1034 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001035 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001036 PackageUpdatedTask.OP_SUSPEND, packageNames,
1037 user));
1038 }
1039
1040 @Override
1041 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001042 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001043 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1044 user));
1045 }
1046
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001047 @Override
1048 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1049 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001050 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1051 }
1052
1053 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1054 UserHandleCompat user) {
1055 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001056 }
1057
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001058 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1060 * ACTION_PACKAGE_CHANGED.
1061 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001062 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001063 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001064 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001065
Joe Onorato36115782010-06-17 13:28:48 -04001066 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001067 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001068 // If we have changed locale we need to clear out the labels in all apps/workspace.
1069 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001070 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1071 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001072 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001073 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001074 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1075 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1076 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001077 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1078 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001079 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1080 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1081 enqueueItemUpdatedTask(new PackageUpdatedTask(
1082 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1083 new String[0], user));
1084 }
1085
1086 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1087 // we need to run the state change task again.
1088 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1089 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1090 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1091 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001092 }
Tony Wickham827cef22016-03-17 15:39:39 -07001093 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1094 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001095 }
1096 }
1097
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001098 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001099 resetLoadedState(true, true);
1100
Reena Lee93f824a2011-09-23 17:20:28 -07001101 // Do this here because if the launcher activity is running it will be restarted.
1102 // If it's not running startLoaderFromBackground will merely tell it that it needs
1103 // to reload.
1104 startLoaderFromBackground();
1105 }
1106
Winson Chungf0c6ae02012-03-21 16:10:31 -07001107 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1108 synchronized (mLock) {
1109 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1110 // mWorkspaceLoaded to true later
1111 stopLoaderLocked();
1112 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1113 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001114 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001115 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001116 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001117 }
1118 }
1119
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001120 /**
1121 * When the launcher is in the background, it's possible for it to miss paired
1122 * configuration changes. So whenever we trigger the loader from the background
1123 * tell the launcher that it needs to re-run the loader when it comes back instead
1124 * of doing it now.
1125 */
1126 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001127 Callbacks callbacks = getCallback();
1128 if (callbacks != null) {
1129 // Only actually run the loader if they're not paused.
1130 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001131 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001132 }
1133 }
Joe Onorato36115782010-06-17 13:28:48 -04001134 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001135
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001136 /**
1137 * If there is already a loader task running, tell it to stop.
1138 */
1139 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001140 LoaderTask oldTask = mLoaderTask;
1141 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001142 oldTask.stopLocked();
1143 }
Reena Lee93f824a2011-09-23 17:20:28 -07001144 }
1145
Adam Cohen1a85c582014-09-30 09:48:49 -07001146 public boolean isCurrentCallbacks(Callbacks callbacks) {
1147 return (mCallbacks != null && mCallbacks.get() == callbacks);
1148 }
1149
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001150 /**
1151 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1152 * @return true if the page could be bound synchronously.
1153 */
1154 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001155 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1156 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001157 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001158 // Don't bother to start the thread if we know it's not going to do anything
1159 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001160 final Callbacks oldCallbacks = mCallbacks.get();
1161 // Clear any pending bind-runnables from the synchronized load process.
1162 runOnMainThread(new Runnable() {
1163 public void run() {
1164 oldCallbacks.clearPendingBinds();
1165 }
1166 });
1167
Joe Onorato36115782010-06-17 13:28:48 -04001168 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001169 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001170 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001171 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001172 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1173 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001175 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001176 } else {
1177 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1178 sWorker.post(mLoaderTask);
1179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001182 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 }
1184
Joe Onorato36115782010-06-17 13:28:48 -04001185 public void stopLoader() {
1186 synchronized (mLock) {
1187 if (mLoaderTask != null) {
1188 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 }
1190 }
Joe Onorato36115782010-06-17 13:28:48 -04001191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001193 /**
1194 * Loads the workspace screen ids in an ordered list.
1195 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001196 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001197 final ContentResolver contentResolver = context.getContentResolver();
1198 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001199
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001200 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001201 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1202 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001203 }
1204
Joe Onorato36115782010-06-17 13:28:48 -04001205 /**
1206 * Runnable for the thread that loads the contents of the launcher:
1207 * - workspace icons
1208 * - widgets
1209 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001210 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001211 */
1212 private class LoaderTask implements Runnable {
1213 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001214 private int mPageToBindFirst;
1215
Adam Cohen091440a2015-03-18 14:16:05 -07001216 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001217 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001218 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001219
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001220 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001221 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001222 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 }
1224
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001225 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001226 mIsLoadingAndBindingWorkspace = true;
1227
Joe Onorato36115782010-06-17 13:28:48 -04001228 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001229 if (DEBUG_LOADERS) {
1230 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001231 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001232
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001233 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001234 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001235 synchronized (LoaderTask.this) {
1236 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001237 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001238 }
1239 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001240 }
1241 }
1242
Joe Onorato36115782010-06-17 13:28:48 -04001243 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001244 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001245 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001246
Joe Onorato36115782010-06-17 13:28:48 -04001247 private void waitForIdle() {
1248 // Wait until the either we're stopped or the other threads are done.
1249 // This way we don't start loading all apps until the workspace has settled
1250 // down.
1251 synchronized (LoaderTask.this) {
1252 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001253
Joe Onorato36115782010-06-17 13:28:48 -04001254 mHandler.postIdle(new Runnable() {
1255 public void run() {
1256 synchronized (LoaderTask.this) {
1257 mLoadAndBindStepFinished = true;
1258 if (DEBUG_LOADERS) {
1259 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001260 }
Joe Onorato36115782010-06-17 13:28:48 -04001261 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001262 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001263 }
Joe Onorato36115782010-06-17 13:28:48 -04001264 });
1265
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001266 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001267 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001268 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1269 // wait no longer than 1sec at a time
1270 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001271 } catch (InterruptedException ex) {
1272 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001273 }
1274 }
Joe Onorato36115782010-06-17 13:28:48 -04001275 if (DEBUG_LOADERS) {
1276 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001277 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001278 + "ms for previous step to finish binding");
1279 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001280 }
Joe Onorato36115782010-06-17 13:28:48 -04001281 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001282
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001283 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001284 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001285 // Ensure that we have a valid page index to load synchronously
1286 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1287 "valid page index");
1288 }
1289 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1290 // Ensure that we don't try and bind a specified page when the pages have not been
1291 // loaded already (we should load everything asynchronously in that case)
1292 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1293 }
1294 synchronized (mLock) {
1295 if (mIsLoaderTaskRunning) {
1296 // Ensure that we are never running the background loading at this point since
1297 // we also touch the background collections
1298 throw new RuntimeException("Error! Background loading is already running");
1299 }
1300 }
1301
1302 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1303 // data structures, we can't allow any other thread to touch that data, but because
1304 // this call is synchronous, we can get away with not locking).
1305
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001306 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001307 // operations from the previous activity. We need to ensure that all queued operations
1308 // are executed before any synchronous binding work is done.
1309 mHandler.flush();
1310
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001311 // Divide the set of loaded items into those that we are binding synchronously, and
1312 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001313 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001314 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1315 // arise from that.
1316 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001317
1318 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 }
1320
Joe Onorato36115782010-06-17 13:28:48 -04001321 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001322 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001323 if (mStopped) {
1324 return;
1325 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 mIsLoaderTaskRunning = true;
1327 }
Joe Onorato36115782010-06-17 13:28:48 -04001328 // Optimize for end-user experience: if the Launcher is up and // running with the
1329 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1330 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001331 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001332 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001333 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001334
Joe Onorato36115782010-06-17 13:28:48 -04001335 if (mStopped) {
1336 break keep_running;
1337 }
1338
Joe Onorato36115782010-06-17 13:28:48 -04001339 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001340
1341 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001342 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1343 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001344
1345 waitForIdle();
1346
1347 // third step
1348 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1349 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001350 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001351
Joe Onorato36115782010-06-17 13:28:48 -04001352 // Clear out this reference, otherwise we end up holding it until all of the
1353 // callback runnables are done.
1354 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355
Joe Onorato36115782010-06-17 13:28:48 -04001356 synchronized (mLock) {
1357 // If we are still the last one to be scheduled, remove ourselves.
1358 if (mLoaderTask == this) {
1359 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001362 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001363 }
Joe Onorato36115782010-06-17 13:28:48 -04001364 }
1365
1366 public void stopLocked() {
1367 synchronized (LoaderTask.this) {
1368 mStopped = true;
1369 this.notify();
1370 }
1371 }
1372
1373 /**
1374 * Gets the callbacks object. If we've been stopped, or if the launcher object
1375 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1376 * object that was around when the deferred message was scheduled, and if there's
1377 * a new Callbacks object around then also return null. This will save us from
1378 * calling onto it with data that will be ignored.
1379 */
1380 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1381 synchronized (mLock) {
1382 if (mStopped) {
1383 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001384 }
Joe Onorato36115782010-06-17 13:28:48 -04001385
1386 if (mCallbacks == null) {
1387 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001388 }
Joe Onorato36115782010-06-17 13:28:48 -04001389
1390 final Callbacks callbacks = mCallbacks.get();
1391 if (callbacks != oldCallbacks) {
1392 return null;
1393 }
1394 if (callbacks == null) {
1395 Log.w(TAG, "no mCallbacks");
1396 return null;
1397 }
1398
1399 return callbacks;
1400 }
1401 }
1402
1403 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001404 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001405 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001406 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001407 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001408
Adam Cohendcd297f2013-06-18 13:13:40 -07001409 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001410 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001411 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001412 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1413 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001414 Log.e(TAG, "Error loading shortcut into hotseat " + item
1415 + " into position (" + item.screenId + ":" + item.cellX + ","
1416 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001417 return false;
1418 }
1419
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001420 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001421 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1422
Adam Cohen2e6da152015-05-06 11:42:25 -07001423 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001424 Log.e(TAG, "Error loading shortcut " + item
1425 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001426 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001427 + ")");
1428 return false;
1429 }
1430
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001431 if (hotseatOccupancy != null) {
1432 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001433 Log.e(TAG, "Error loading shortcut into hotseat " + item
1434 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001435 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001436 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001437 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001438 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001439 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001440 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001441 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001442 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1443 occupancy.cells[(int) item.screenId][0] = true;
1444 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001445 return true;
1446 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001447 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1448 if (!workspaceScreens.contains((Long) item.screenId)) {
1449 // The item has an invalid screen id.
1450 return false;
1451 }
1452 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001453 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001454 return true;
1455 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001456
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001457 final int countX = profile.numColumns;
1458 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001459 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1460 item.cellX < 0 || item.cellY < 0 ||
1461 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1462 Log.e(TAG, "Error loading shortcut " + item
1463 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1464 + item.cellX + "," + item.cellY
1465 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1466 return false;
1467 }
1468
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001469 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001470 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1471 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001472 // Mark the first row as occupied (if the feature is enabled)
1473 // in order to account for the QSB.
1474 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001475 }
1476 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001477 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001478 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001479
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001480 // Check if any workspace icons overlap with each other
1481 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1482 occupancy.markCells(item, true);
1483 return true;
1484 } else {
1485 Log.e(TAG, "Error loading shortcut " + item
1486 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1487 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1488 + ") already occupied");
1489 return false;
1490 }
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001492
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001493 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001494 if (LauncherAppState.PROFILE_STARTUP) {
1495 Trace.beginSection("Loading Workspace");
1496 }
Joe Onorato36115782010-06-17 13:28:48 -04001497 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498
Joe Onorato36115782010-06-17 13:28:48 -04001499 final Context context = mContext;
1500 final ContentResolver contentResolver = context.getContentResolver();
1501 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001502 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001503 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001504 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001505 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001506
Winson Chung892c74d2013-08-22 16:15:50 -07001507 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001508 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001509 int countX = profile.numColumns;
1510 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001511
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001512 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001513 try {
1514 ImportDataTask.performImportIfPossible(context);
1515 } catch (Exception e) {
1516 // Migration failed. Clear workspace.
1517 clearDb = true;
1518 }
1519
1520 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001521 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1522 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001523 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001524 }
1525
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001526 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001527 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001528 LauncherSettings.Settings.call(contentResolver,
1529 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001530 }
1531
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001532 Log.d(TAG, "loadWorkspace: loading default favorites");
1533 LauncherSettings.Settings.call(contentResolver,
1534 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001535
Sunny Goyale9956a72016-09-01 17:24:47 -07001536 synchronized (sBgDataModel) {
1537 sBgDataModel.clear();
1538
Sunny Goyal756adbc2015-04-16 15:20:51 -07001539 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001540 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001541 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001542
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001543 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1544 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001545 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001546 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001547 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001548 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001549
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 // +1 for the hotseat (it can be larger than the workspace)
1551 // Load workspace in reverse order to ensure that latest items are loaded first (and
1552 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001553 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001554 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001556 try {
1557 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1558 final int intentIndex = c.getColumnIndexOrThrow
1559 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001560 final int containerIndex = c.getColumnIndexOrThrow(
1561 LauncherSettings.Favorites.CONTAINER);
1562 final int itemTypeIndex = c.getColumnIndexOrThrow(
1563 LauncherSettings.Favorites.ITEM_TYPE);
1564 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1565 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001566 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1567 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001568 final int screenIndex = c.getColumnIndexOrThrow(
1569 LauncherSettings.Favorites.SCREEN);
1570 final int cellXIndex = c.getColumnIndexOrThrow
1571 (LauncherSettings.Favorites.CELLX);
1572 final int cellYIndex = c.getColumnIndexOrThrow
1573 (LauncherSettings.Favorites.CELLY);
1574 final int spanXIndex = c.getColumnIndexOrThrow
1575 (LauncherSettings.Favorites.SPANX);
1576 final int spanYIndex = c.getColumnIndexOrThrow(
1577 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001578 final int rankIndex = c.getColumnIndexOrThrow(
1579 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001580 final int restoredIndex = c.getColumnIndexOrThrow(
1581 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001582 final int profileIdIndex = c.getColumnIndexOrThrow(
1583 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001584 final int optionsIndex = c.getColumnIndexOrThrow(
1585 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001586 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587
Sunny Goyal7f834d22015-04-21 10:10:23 -07001588 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001589 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001590 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001591 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001592 long serialNo = mUserManager.getSerialNumberForUser(user);
1593 allUsers.put(serialNo, user);
1594 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001595
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001596 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001597
1598 // We can only query for shortcuts when the user is unlocked.
1599 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001600 List<ShortcutInfoCompat> pinnedShortcuts =
1601 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1602 if (mDeepShortcutManager.wasLastCallSuccess()) {
1603 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1604 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1605 shortcut);
1606 }
1607 } else {
1608 // Shortcut manager can fail due to some race condition when the
1609 // lock state changes too frequently. For the purpose of the loading
1610 // shortcuts, consider the user is still locked.
1611 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001612 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001613 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001614 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001615 }
1616
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001617 ShortcutInfo info;
1618 String intentDescription;
1619 LauncherAppWidgetInfo appWidgetInfo;
1620 int container;
1621 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001622 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001623 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001624 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001625 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001627 while (!mStopped && c.moveToNext()) {
1628 try {
1629 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001630 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001631 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001632 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001633
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001634 switch (itemType) {
1635 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1636 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001637 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001638 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001639 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001640 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001641 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001642 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001643 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001644 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001645 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001646 if (user == null) {
1647 // User has been deleted remove the item.
1648 itemsToRemove.add(id);
1649 continue;
1650 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001651 try {
1652 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001653 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001654 if (cn != null && cn.getPackageName() != null) {
1655 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1656 cn.getPackageName(), user);
1657 boolean validComponent = validPkg &&
1658 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001659 if (validPkg) {
1660 targetPackage = cn.getPackageName();
1661 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001662
1663 if (validComponent) {
1664 if (restored) {
1665 // no special handling necessary for this item
1666 restoredRows.add(id);
1667 restored = false;
1668 }
Kenny Guyff05f432016-01-22 17:48:29 +00001669 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001670 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001671 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001672 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001673 intent = null;
1674 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1675 // We allow auto install apps to have their intent
1676 // updated after an install.
1677 intent = manager.getLaunchIntentForPackage(
1678 cn.getPackageName());
1679 if (intent != null) {
1680 ContentValues values = new ContentValues();
1681 values.put(LauncherSettings.Favorites.INTENT,
1682 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001683 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001684 }
1685 }
1686
1687 if (intent == null) {
1688 // The app is installed but the component is no
1689 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001690 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001691 itemsToRemove.add(id);
1692 continue;
1693 } else {
1694 // no special handling necessary for this item
1695 restoredRows.add(id);
1696 restored = false;
1697 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001698 } else if (restored) {
1699 // Package is not yet available but might be
1700 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001701 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001702
1703 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1704 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001705 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001706 // App restore has started. Update the flag
1707 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1708 ContentValues values = new ContentValues();
1709 values.put(LauncherSettings.Favorites.RESTORED,
1710 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001711 updateItem(id, values);
1712 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1713 // This is a common app. Try to replace this.
1714 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1715 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1716 if (parser.findDefaultApp()) {
1717 // Default app found. Replace it.
1718 intent = parser.parsedIntent;
1719 cn = intent.getComponent();
1720 ContentValues values = parser.parsedValues;
1721 values.put(LauncherSettings.Favorites.RESTORED, 0);
1722 updateItem(id, values);
1723 restored = false;
1724 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001725
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001726 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001727 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001728 itemsToRemove.add(id);
1729 continue;
1730 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001731 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001732 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001733 itemsToRemove.add(id);
1734 continue;
1735 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001736 } else if (PackageManagerHelper.isAppOnSdcard(
1737 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001738 // Package is present but not available.
1739 allowMissingTarget = true;
1740 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1741 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001742 // SdCard is not ready yet. Package might get available,
1743 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001744 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001745 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001746 allowMissingTarget = true;
1747 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001748
1749 } else {
1750 // Do not wait for external media load anymore.
1751 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001752 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001753 itemsToRemove.add(id);
1754 continue;
Winson Chungee055712013-07-30 14:46:24 -07001755 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001756 } else if (cn == null) {
1757 // For shortcuts with no component, keep them as they are
1758 restoredRows.add(id);
1759 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001760 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001762 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001763 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 continue;
1765 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766
Sunny Goyal34b65272015-03-11 16:56:52 -07001767 boolean useLowResIcon = container >= 0 &&
1768 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1769
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001770 if (itemReplaced) {
1771 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001772 info = getAppShortcutInfo(intent, user, null,
1773 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001774 } else {
1775 // Don't replace items for other profiles.
1776 itemsToRemove.add(id);
1777 continue;
1778 }
1779 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001780 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001781 info = getRestoredItemInfo(c, intent,
1782 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001783 intent = getRestoredItemIntent(c, context, intent);
1784 } else {
1785 // Don't restore items for other profiles.
1786 itemsToRemove.add(id);
1787 continue;
1788 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001789 } else if (itemType ==
1790 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001791 info = getAppShortcutInfo(intent, user, c,
1792 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001793 } else if (itemType ==
1794 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001795
1796 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1797 if (unlockedUsers.get(serialNumber)) {
1798 ShortcutInfoCompat pinnedShortcut =
1799 shortcutKeyToPinnedShortcuts.get(key);
1800 if (pinnedShortcut == null) {
1801 // The shortcut is no longer valid.
1802 itemsToRemove.add(id);
1803 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001804 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001805 info = new ShortcutInfo(pinnedShortcut, context);
1806 intent = info.intent;
1807 } else {
1808 // Create a shortcut info in disabled mode for now.
1809 info = new ShortcutInfo();
1810 info.user = user;
1811 info.itemType = itemType;
1812 loadInfoFromCursor(info, c, cursorIconInfo);
1813
1814 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001815 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001816 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001817 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001818
Sunny Goyald09c3702016-04-06 16:18:20 -07001819 // Shortcuts are only available on the primary profile
1820 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1821 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1822 }
1823
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 // App shortcuts that used to be automatically added to Launcher
1825 // didn't always have the correct intent flags set, so do that
1826 // here
1827 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001828 intent.getCategories() != null &&
1829 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001830 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 intent.addFlags(
1832 Intent.FLAG_ACTIVITY_NEW_TASK |
1833 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1834 }
Michael Jurka96879562012-03-22 05:54:33 -07001835 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001838 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001841 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 info.cellX = c.getInt(cellXIndex);
1843 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001844 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001845 info.spanX = 1;
1846 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001847 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001848 if (info.promisedIntent != null) {
1849 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1850 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001851 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001852 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1853 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1854 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001855
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001857 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001858 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 break;
1860 }
1861
Sunny Goyal756adbc2015-04-16 15:20:51 -07001862 if (restored) {
1863 ComponentName cn = info.getTargetComponent();
1864 if (cn != null) {
1865 Integer progress = installingPkgs.get(cn.getPackageName());
1866 if (progress != null) {
1867 info.setInstallProgress(progress);
1868 } else {
1869 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1870 }
1871 }
1872 }
1873
Sunny Goyale9956a72016-09-01 17:24:47 -07001874 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001875 } else {
1876 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 }
1878 break;
1879
1880 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1881 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001882 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883
Sunny Goyala508e4f2015-05-21 09:33:57 -07001884 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001885 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001888 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 folderInfo.cellX = c.getInt(cellXIndex);
1890 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001891 folderInfo.spanX = 1;
1892 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001893 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001894
Daniel Sandler8802e962010-05-26 16:28:16 -04001895 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001896 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001897 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001898 break;
1899 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001900 if (restored) {
1901 // no special handling required for restored folders
1902 restoredRows.add(id);
1903 }
1904
Sunny Goyale9956a72016-09-01 17:24:47 -07001905 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 break;
1907
1908 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001909 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001910 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001911 boolean customWidget = itemType ==
1912 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1913
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001914 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001915 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001916 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001917 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001918 user = allUsers.get(serialNumber);
1919 if (user == null) {
1920 itemsToRemove.add(id);
1921 continue;
1922 }
1923
Sunny Goyalff572272014-07-23 13:58:07 -07001924 final ComponentName component =
1925 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001926
Sunny Goyal651077b2014-06-30 14:15:31 -07001927 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001928 final boolean isIdValid = (restoreStatus &
1929 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001930 final boolean wasProviderReady = (restoreStatus &
1931 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001932
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001933 if (widgetProvidersMap == null) {
1934 widgetProvidersMap = AppWidgetManagerCompat
1935 .getInstance(mContext).getAllProvidersMap();
1936 }
1937 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1938 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001939 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001940 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001941
1942 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001943 if (!isSafeMode && !customWidget &&
1944 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001945 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001946 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 itemsToRemove.add(id);
1948 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001949 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001950 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1951 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001952
Sunny Goyal53f96722015-07-13 19:54:53 -07001953 // The provider is available. So the widget is either
1954 // available or not available. We do not need to track
1955 // any future restore updates.
1956 int status = restoreStatus &
1957 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001958 if (!wasProviderReady) {
1959 // If provider was not previously ready, update the
1960 // status and UI flag.
1961
1962 // Id would be valid only if the widget restore broadcast was received.
1963 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001964 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001965 } else {
1966 status &= ~LauncherAppWidgetInfo
1967 .FLAG_PROVIDER_NOT_READY;
1968 }
1969 }
1970 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001971 } else {
1972 Log.v(TAG, "Widget restore pending id=" + id
1973 + " appWidgetId=" + appWidgetId
1974 + " status =" + restoreStatus);
1975 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001976 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001977 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001978 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001979
1980 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1981 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001982 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001983 // App restore has started. Update the flag
1984 appWidgetInfo.restoreStatus |=
1985 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001986 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001987 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001988 itemsToRemove.add(id);
1989 continue;
1990 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001991
1992 appWidgetInfo.installProgress =
1993 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001994 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001995 if (appWidgetInfo.hasRestoreFlag(
1996 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1997 intentDescription = c.getString(intentIndex);
1998 if (!TextUtils.isEmpty(intentDescription)) {
1999 appWidgetInfo.bindOptions =
2000 Intent.parseUri(intentDescription, 0);
2001 }
2002 }
Sunny Goyalff572272014-07-23 13:58:07 -07002003
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002005 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 appWidgetInfo.cellX = c.getInt(cellXIndex);
2007 appWidgetInfo.cellY = c.getInt(cellYIndex);
2008 appWidgetInfo.spanX = c.getInt(spanXIndex);
2009 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002010 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002011
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2013 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2014 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002015 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2016 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 continue;
2018 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002020 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07002022 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002023 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 break;
2025 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002026
Adam Cohen59400422014-03-05 18:07:04 -08002027 if (!customWidget) {
2028 String providerName =
2029 appWidgetInfo.providerName.flattenToString();
2030 if (!providerName.equals(savedProvider) ||
2031 (appWidgetInfo.restoreStatus != restoreStatus)) {
2032 ContentValues values = new ContentValues();
2033 values.put(
2034 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2035 providerName);
2036 values.put(LauncherSettings.Favorites.RESTORED,
2037 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002038 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002039 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002040 }
Sunny Goyale9956a72016-09-01 17:24:47 -07002041 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 }
Joe Onorato36115782010-06-17 13:28:48 -04002043 break;
2044 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002046 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002047 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 }
2049 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002050 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 }
2052
Winson Chungba9c37f2013-08-30 14:11:37 -07002053 // Break early if we've stopped loading
2054 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002055 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002056 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002057 }
2058
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002061 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2062 Utilities.createDbSelectionQuery(
2063 LauncherSettings.Favorites._ID, itemsToRemove), null);
2064 if (DEBUG_LOADERS) {
2065 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2066 LauncherSettings.Favorites._ID, itemsToRemove));
2067 }
2068
2069 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002070 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2071 .call(contentResolver,
2072 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2073 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2074 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002075 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
2076 sBgDataModel.folders.remove(folderId);
2077 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002078 }
2079 }
2080
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002081 // Unpin shortcuts that don't exist on the workspace.
2082 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002083 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002084 if (numTimesPinned == null || numTimesPinned.value == 0) {
2085 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2086 mDeepShortcutManager.unpinShortcut(key);
2087 }
2088 }
2089
Sunny Goyal317698b2015-07-29 11:45:41 -07002090 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07002091 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07002092 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2093 int pos = 0;
2094 for (ShortcutInfo info : folder.contents) {
2095 if (info.usingLowResIcon) {
2096 info.updateIcon(mIconCache, false);
2097 }
2098 pos ++;
2099 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2100 break;
2101 }
2102 }
2103 }
2104
Chris Wrenf4d08112014-01-16 18:13:56 -05002105 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002106 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002107 ContentValues values = new ContentValues();
2108 values.put(LauncherSettings.Favorites.RESTORED, 0);
2109 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2110 Utilities.createDbSelectionQuery(
2111 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002112 }
2113
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002114 if (!isSdCardReady && !pendingPackages.isEmpty()) {
2115 context.registerReceiver(
2116 new SdCardAvailableReceiver(
2117 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002118 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002119 null,
2120 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002121 }
2122
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002123 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07002124 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
2125 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002126 long screenId = item.screenId;
2127 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2128 unusedScreens.contains(screenId)) {
2129 unusedScreens.remove(screenId);
2130 }
2131 }
2132
2133 // If there are any empty screens remove them, and update.
2134 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002135 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
2136 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 }
2138
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002139 if (DEBUG_LOADERS) {
2140 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2141 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002143 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002145
Sunny Goyale2df0622015-04-24 11:27:00 -07002146 for (int i = 0; i < nScreens; i++) {
2147 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002148 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 line += " | ";
2150 }
Joe Onorato36115782010-06-17 13:28:48 -04002151 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002153 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002154 }
Joe Onorato36115782010-06-17 13:28:48 -04002155 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002156 if (LauncherAppState.PROFILE_STARTUP) {
2157 Trace.endSection();
2158 }
Adam Cohene25af792013-06-06 23:08:25 -07002159 }
2160
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002161 /**
2162 * Partially updates the item without any notification. Must be called on the worker thread.
2163 */
2164 private void updateItem(long itemId, ContentValues update) {
2165 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002166 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002167 update,
2168 BaseColumns._ID + "= ?",
2169 new String[]{Long.toString(itemId)});
2170 }
2171
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 /** Filters the set of items who are directly or indirectly (via another container) on the
2173 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002174 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 ArrayList<ItemInfo> allWorkspaceItems,
2176 ArrayList<ItemInfo> currentScreenItems,
2177 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002178 // Purge any null ItemInfos
2179 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2180 while (iter.hasNext()) {
2181 ItemInfo i = iter.next();
2182 if (i == null) {
2183 iter.remove();
2184 }
2185 }
2186
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 // Order the set of items by their containers first, this allows use to walk through the
2188 // list sequentially, build up a list of containers that are in the specified screen,
2189 // as well as all items in those containers.
2190 Set<Long> itemsOnScreen = new HashSet<Long>();
2191 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2192 @Override
2193 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002194 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 }
2196 });
2197 for (ItemInfo info : allWorkspaceItems) {
2198 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002199 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 currentScreenItems.add(info);
2201 itemsOnScreen.add(info.id);
2202 } else {
2203 otherScreenItems.add(info);
2204 }
2205 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2206 currentScreenItems.add(info);
2207 itemsOnScreen.add(info.id);
2208 } else {
2209 if (itemsOnScreen.contains(info.container)) {
2210 currentScreenItems.add(info);
2211 itemsOnScreen.add(info.id);
2212 } else {
2213 otherScreenItems.add(info);
2214 }
2215 }
2216 }
2217 }
2218
2219 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002220 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 ArrayList<LauncherAppWidgetInfo> appWidgets,
2222 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2223 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224
2225 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002226 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002228 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 currentScreenWidgets.add(widget);
2230 } else {
2231 otherScreenWidgets.add(widget);
2232 }
2233 }
2234 }
2235
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2237 * right) */
2238 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002239 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002240 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002241 final int screenCols = profile.numColumns;
2242 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002244 @Override
2245 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002246 if (lhs.container == rhs.container) {
2247 // Within containers, order by their spatial position in that container
2248 switch ((int) lhs.container) {
2249 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2250 long lr = (lhs.screenId * screenCellCount +
2251 lhs.cellY * screenCols + lhs.cellX);
2252 long rr = (rhs.screenId * screenCellCount +
2253 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002254 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002255 }
2256 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2257 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002258 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002259 }
2260 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002261 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002262 throw new RuntimeException("Unexpected container type when " +
2263 "sorting workspace items.");
2264 }
2265 return 0;
2266 }
2267 } else {
2268 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002269 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002270 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002271 }
2272 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002274
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2276 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002277 final Runnable r = new Runnable() {
2278 @Override
2279 public void run() {
2280 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2281 if (callbacks != null) {
2282 callbacks.bindScreens(orderedScreens);
2283 }
2284 }
2285 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002286 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 }
2288
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2290 final ArrayList<ItemInfo> workspaceItems,
2291 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002292 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293
2294 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002295 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002297 final int start = i;
2298 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002299 final Runnable r = new Runnable() {
2300 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002301 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002302 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002303 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002304 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2305 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002306 }
2307 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002309 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002310 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 // Bind the widgets, one at a time
2313 N = appWidgets.size();
2314 for (int i = 0; i < N; i++) {
2315 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2316 final Runnable r = new Runnable() {
2317 public void run() {
2318 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2319 if (callbacks != null) {
2320 callbacks.bindAppWidget(widget);
2321 }
2322 }
2323 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002324 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325 }
2326 }
2327
2328 /**
2329 * Binds all loaded data to actual views on the main thread.
2330 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002331 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 final long t = SystemClock.uptimeMillis();
2333 Runnable r;
2334
2335 // Don't use these two variables in any of the callback runnables.
2336 // Otherwise we hold a reference to them.
2337 final Callbacks oldCallbacks = mCallbacks.get();
2338 if (oldCallbacks == null) {
2339 // This launcher has exited and nobody bothered to tell us. Just bail.
2340 Log.w(TAG, "LoaderTask running with no launcher");
2341 return;
2342 }
2343
Winson Chung9b9fb962013-11-15 15:39:34 -08002344 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002345 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2346 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2347 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002348
Sunny Goyale9956a72016-09-01 17:24:47 -07002349 synchronized (sBgDataModel) {
2350 workspaceItems.addAll(sBgDataModel.workspaceItems);
2351 appWidgets.addAll(sBgDataModel.appWidgets);
2352 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 }
2354
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002355 final int currentScreen;
2356 {
2357 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2358 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2359 if (currScreen >= orderedScreenIds.size()) {
2360 // There may be no workspace screens (just hotseat items and an empty page).
2361 currScreen = PagedView.INVALID_RESTORE_PAGE;
2362 }
2363 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002364 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002365 final boolean validFirstPage = currentScreen >= 0;
2366 final long currentScreenId =
2367 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002368
Winson Chung9b9fb962013-11-15 15:39:34 -08002369 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002370 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2371 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2372 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2373 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374
Winson Chung9b9fb962013-11-15 15:39:34 -08002375 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002377 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2380 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2381
2382 // Tell the workspace that we're about to start binding items
2383 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002384 public void run() {
2385 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2386 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002387 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002389 }
2390 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002392 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393
Adam Cohendcd297f2013-06-18 13:13:40 -07002394 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2395
Sunny Goyal527c7d32015-08-28 15:19:36 -07002396 Executor mainExecutor = new DeferredMainThreadExecutor();
2397 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002398 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002400 // In case of validFirstPage, only bind the first screen, and defer binding the
2401 // remaining screens after first onDraw (and an optional the fade animation whichever
2402 // happens later).
2403 // This ensures that the first screen is immediately visible (eg. during rotation)
2404 // In case of !validFirstPage, bind all pages one after other.
2405 final Executor deferredExecutor =
2406 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2407
2408 mainExecutor.execute(new Runnable() {
2409 @Override
2410 public void run() {
2411 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2412 if (callbacks != null) {
2413 callbacks.finishFirstPageBind(
2414 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2415 }
2416 }
2417 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002418
Sunny Goyal44c06432016-04-02 10:56:02 -07002419 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002420
2421 // Tell the workspace that we're done binding items
2422 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002423 public void run() {
2424 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2425 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002426 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002427 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428
Sunny Goyal639e9062015-08-19 19:17:06 -07002429 mIsLoadingAndBindingWorkspace = false;
2430
2431 // Run all the bind complete runnables after workspace is bound.
2432 if (!mBindCompleteRunnables.isEmpty()) {
2433 synchronized (mBindCompleteRunnables) {
2434 for (final Runnable r : mBindCompleteRunnables) {
2435 runOnWorkerThread(r);
2436 }
2437 mBindCompleteRunnables.clear();
2438 }
2439 }
2440
Winson Chung98e030b2012-05-07 16:01:11 -07002441 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002442 if (DEBUG_LOADERS) {
2443 Log.d(TAG, "bound workspace in "
2444 + (SystemClock.uptimeMillis()-t) + "ms");
2445 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002446
Joe Onorato36115782010-06-17 13:28:48 -04002447 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002449 deferredExecutor.execute(r);
2450
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002451 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002452 r = new Runnable() {
2453 public void run() {
2454 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2455 if (callbacks != null) {
2456 // We are loading synchronously, which means, some of the pages will be
2457 // bound after first draw. Inform the callbacks that page binding is
2458 // not complete, and schedule the remaining pages.
2459 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2460 callbacks.onPageBoundSynchronously(currentScreen);
2461 }
2462 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2463 }
2464 }
2465 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002466 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002467 }
Joe Onorato36115782010-06-17 13:28:48 -04002468 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002469
Joe Onorato36115782010-06-17 13:28:48 -04002470 private void loadAndBindAllApps() {
2471 if (DEBUG_LOADERS) {
2472 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2473 }
2474 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002475 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002476 synchronized (LoaderTask.this) {
2477 if (mStopped) {
2478 return;
2479 }
2480 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002481 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002482 synchronized (LoaderTask.this) {
2483 if (mStopped) {
2484 return;
2485 }
2486 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002487 }
Joe Onorato36115782010-06-17 13:28:48 -04002488 } else {
2489 onlyBindAllApps();
2490 }
2491 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002492
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002493 private void updateIconCache() {
2494 // Ignore packages which have a promise icon.
2495 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002496 synchronized (sBgDataModel) {
2497 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002498 if (info instanceof ShortcutInfo) {
2499 ShortcutInfo si = (ShortcutInfo) info;
2500 if (si.isPromise() && si.getTargetComponent() != null) {
2501 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2502 }
2503 } else if (info instanceof LauncherAppWidgetInfo) {
2504 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2505 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2506 packagesToIgnore.add(lawi.providerName.getPackageName());
2507 }
2508 }
2509 }
2510 }
2511 mIconCache.updateDbIcons(packagesToIgnore);
2512 }
2513
Joe Onorato36115782010-06-17 13:28:48 -04002514 private void onlyBindAllApps() {
2515 final Callbacks oldCallbacks = mCallbacks.get();
2516 if (oldCallbacks == null) {
2517 // This launcher has exited and nobody bothered to tell us. Just bail.
2518 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2519 return;
2520 }
2521
2522 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002523 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002524 final ArrayList<AppInfo> list
2525 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002526 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002527 public void run() {
2528 final long t = SystemClock.uptimeMillis();
2529 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2530 if (callbacks != null) {
2531 callbacks.bindAllApplications(list);
2532 }
2533 if (DEBUG_LOADERS) {
2534 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002535 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002536 }
2537 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002538 };
Tony Wickham80f57872016-06-29 18:12:15 -07002539 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002540 }
2541
Winson Chung64359a52013-07-08 17:17:08 -07002542 private void loadAllApps() {
2543 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002544
Joe Onorato36115782010-06-17 13:28:48 -04002545 final Callbacks oldCallbacks = mCallbacks.get();
2546 if (oldCallbacks == null) {
2547 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002548 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002549 return;
2550 }
2551
Kenny Guyed131872014-04-30 03:02:21 +01002552 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2553
Winson Chung64359a52013-07-08 17:17:08 -07002554 // Clear the list of apps
2555 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002556 for (UserHandleCompat user : profiles) {
2557 // Query for the set of apps
2558 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002559 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002560 if (DEBUG_LOADERS) {
2561 Log.d(TAG, "getActivityList took "
2562 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2563 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2564 }
2565 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002566 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002567 if (apps == null || apps.isEmpty()) {
2568 return;
2569 }
Kenny Guyff05f432016-01-22 17:48:29 +00002570 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002571 // Create the ApplicationInfos
2572 for (int i = 0; i < apps.size(); i++) {
2573 LauncherActivityInfoCompat app = apps.get(i);
2574 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002575 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002576 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002577
Sunny Goyal756a28a2015-04-23 17:07:55 -07002578 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2579 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002580 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002581
2582 @Override
2583 public void run() {
2584 heuristic.processUserApps(apps);
2585 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002586 };
2587 runOnMainThread(new Runnable() {
2588
2589 @Override
2590 public void run() {
2591 // Check isLoadingWorkspace on the UI thread, as it is updated on
2592 // the UI thread.
2593 if (mIsLoadingAndBindingWorkspace) {
2594 synchronized (mBindCompleteRunnables) {
2595 mBindCompleteRunnables.add(r);
2596 }
2597 } else {
2598 runOnWorkerThread(r);
2599 }
2600 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002601 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002602 }
Winson Chung64359a52013-07-08 17:17:08 -07002603 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002604 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002605 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2606 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002607
2608 // Post callback on main thread
2609 mHandler.post(new Runnable() {
2610 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002611
Winson Chung64359a52013-07-08 17:17:08 -07002612 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002613 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002614 if (callbacks != null) {
2615 callbacks.bindAllApplications(added);
2616 if (DEBUG_LOADERS) {
2617 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002618 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002619 }
2620 } else {
2621 Log.i(TAG, "not binding apps: no Launcher activity");
2622 }
2623 }
2624 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002625 // Cleanup any data stored for a deleted user.
2626 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002627 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002628 Log.d(TAG, "Icons processed in "
2629 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002630 }
2631 }
2632
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002633 private void loadAndBindDeepShortcuts() {
2634 if (DEBUG_LOADERS) {
2635 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2636 }
2637 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002638 sBgDataModel.deepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002639 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2640 if (mHasShortcutHostPermission) {
2641 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2642 if (mUserManager.isUserUnlocked(user)) {
2643 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2644 .queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002645 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002646 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002647 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002648 }
2649 synchronized (LoaderTask.this) {
2650 if (mStopped) {
2651 return;
2652 }
2653 mDeepShortcutsLoaded = true;
2654 }
2655 }
Tony Wickham80f57872016-06-29 18:12:15 -07002656 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002657 }
2658
Joe Onoratobe386092009-11-17 17:32:16 -08002659 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002660 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002661 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2663 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002664 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002665 }
Joe Onorato36115782010-06-17 13:28:48 -04002666 }
2667 }
2668
Tony Wickham80f57872016-06-29 18:12:15 -07002669 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002670 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2671 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002672 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002673 @Override
2674 public void run() {
2675 Callbacks callbacks = getCallback();
2676 if (callbacks != null) {
2677 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2678 }
2679 }
Tony Wickham80f57872016-06-29 18:12:15 -07002680 };
2681 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002682 }
2683
Sunny Goyal75b0f552015-05-20 21:57:06 -07002684 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002685 * Refreshes the cached shortcuts if the shortcut permission has changed.
2686 * Current implementation simply reloads the workspace, but it can be optimized to
2687 * use partial updates similar to {@link UserManagerCompat}
2688 */
2689 public void refreshShortcutsIfRequired() {
2690 if (Utilities.isNycMR1OrAbove()) {
2691 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2692 sWorker.post(mShortcutPermissionCheckRunnable);
2693 }
2694 }
2695
2696 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002697 * Called when the icons for packages have been updated in the icon cache.
2698 */
2699 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2700 final Callbacks callbacks = getCallback();
2701 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2702 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2703
2704 // If any package icon has changed (app was updated while launcher was dead),
2705 // update the corresponding shortcuts.
Sunny Goyale9956a72016-09-01 17:24:47 -07002706 synchronized (sBgDataModel) {
2707 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002708 if (info instanceof ShortcutInfo && user.equals(info.user)
2709 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2710 ShortcutInfo si = (ShortcutInfo) info;
2711 ComponentName cn = si.getTargetComponent();
2712 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2713 si.updateIcon(mIconCache);
2714 updatedShortcuts.add(si);
2715 }
2716 }
2717 }
2718 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2719 }
2720
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002721 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002722
2723 if (!updatedApps.isEmpty()) {
2724 mHandler.post(new Runnable() {
2725
2726 public void run() {
2727 Callbacks cb = getCallback();
2728 if (cb != null && callbacks == cb) {
2729 cb.bindAppsUpdated(updatedApps);
2730 }
2731 }
2732 });
2733 }
2734 }
2735
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002736 private void bindUpdatedShortcuts(
2737 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2738 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2739 }
2740
2741 private void bindUpdatedShortcuts(
2742 final ArrayList<ShortcutInfo> updatedShortcuts,
2743 final ArrayList<ShortcutInfo> removedShortcuts,
2744 final UserHandleCompat user) {
2745 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002746 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002747 mHandler.post(new Runnable() {
2748
2749 public void run() {
2750 Callbacks cb = getCallback();
2751 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002752 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002753 }
2754 }
2755 });
2756 }
2757 }
2758
2759 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002760 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002761 }
2762
2763 private class PackageUpdatedTask implements Runnable {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002764 final int mOp;
2765 final String[] mPackages;
2766 final UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002767
2768 public static final int OP_NONE = 0;
2769 public static final int OP_ADD = 1;
2770 public static final int OP_UPDATE = 2;
2771 public static final int OP_REMOVE = 3; // uninstlled
2772 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002773 public static final int OP_SUSPEND = 5; // package suspended
2774 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002775 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002776
Kenny Guyed131872014-04-30 03:02:21 +01002777 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002778 mOp = op;
2779 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002780 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002781 }
2782
2783 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002784 if (!mHasLoaderCompletedOnce) {
2785 // Loader has not yet run.
2786 return;
2787 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002788 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002789
2790 final String[] packages = mPackages;
2791 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002792 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyal40452cf2016-09-01 15:17:46 -07002793 final HashSet<String> packageSet = new HashSet<>(Arrays.asList(packages));
Joe Onorato36115782010-06-17 13:28:48 -04002794 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002795 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002796 for (int i=0; i<N; i++) {
2797 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002798 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002799 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002800 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002801
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002802 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2803 if (heuristic != null) {
2804 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002805 }
Joe Onorato36115782010-06-17 13:28:48 -04002806 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002807 }
Joe Onorato36115782010-06-17 13:28:48 -04002808 case OP_UPDATE:
2809 for (int i=0; i<N; i++) {
2810 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002811 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002812 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002813 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002814 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002815 // Since package was just updated, the target must be available now.
2816 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002817 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002818 case OP_REMOVE: {
2819 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2820 if (heuristic != null) {
2821 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002822 }
Joe Onorato36115782010-06-17 13:28:48 -04002823 for (int i=0; i<N; i++) {
2824 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002825 mIconCache.removeIconsForPkg(packages[i], mUser);
2826 }
2827 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002828 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002829 case OP_UNAVAILABLE:
2830 for (int i=0; i<N; i++) {
2831 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2832 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002833 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002834 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002835 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002836 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002837 case OP_SUSPEND:
2838 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002839 flagOp = mOp == OP_SUSPEND ?
2840 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2841 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002842 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyal40452cf2016-09-01 15:17:46 -07002843 mBgAllAppsList.updateDisabledFlags(
2844 ItemInfoMatcher.ofPackages(packageSet, mUser), flagOp);
Sunny Goyalda891c12016-03-18 18:29:24 -07002845 break;
2846 case OP_USER_AVAILABILITY_CHANGE:
2847 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2848 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2849 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2850 // We want to update all packages for this user.
Sunny Goyal40452cf2016-09-01 15:17:46 -07002851 mBgAllAppsList.updateDisabledFlags(ItemInfoMatcher.ofUser(mUser), flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002852 break;
Joe Onorato36115782010-06-17 13:28:48 -04002853 }
2854
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002855 ArrayList<AppInfo> added = null;
2856 ArrayList<AppInfo> modified = null;
2857 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002858
Adam Cohen487f7dd2012-06-28 18:12:10 -07002859 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002860 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002861 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002862 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002863 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002864 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002865 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002866 }
Winson Chung5d55f332012-07-16 20:45:03 -07002867 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002868 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002869 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002870 }
2871
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002872 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002873
Joe Onorato36115782010-06-17 13:28:48 -04002874 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002875 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002876 for (AppInfo ai : added) {
2877 addedOrUpdatedApps.put(ai.componentName, ai);
2878 }
Joe Onorato36115782010-06-17 13:28:48 -04002879 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002880
Joe Onorato36115782010-06-17 13:28:48 -04002881 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002882 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002883 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002884 for (AppInfo ai : modified) {
2885 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002886 }
2887
Joe Onorato36115782010-06-17 13:28:48 -04002888 mHandler.post(new Runnable() {
2889 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002890 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002891 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002892 callbacks.bindAppsUpdated(modifiedFinal);
2893 }
2894 }
2895 });
2896 }
Winson Chung83892cc2013-05-01 16:53:33 -07002897
Sunny Goyal4390ace2014-10-13 11:33:11 -07002898 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002899 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07002900 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2901 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<>();
2902 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002903
Sunny Goyale9956a72016-09-01 17:24:47 -07002904 synchronized (sBgDataModel) {
2905 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002906 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2907 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002908 boolean infoUpdated = false;
2909 boolean shortcutUpdated = false;
2910
2911 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002912 if ((si.iconResource != null)
Sunny Goyal40452cf2016-09-01 15:17:46 -07002913 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyal10629b02016-09-01 12:50:11 -07002914 Bitmap icon = LauncherIcons.createIconBitmap(
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002915 si.iconResource.packageName,
2916 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002917 if (icon != null) {
2918 si.setIcon(icon);
2919 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002920 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002921 }
2922 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002923
2924 ComponentName cn = si.getTargetComponent();
Sunny Goyal40452cf2016-09-01 15:17:46 -07002925 if (cn != null && packageSet.contains(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002926 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2927
2928 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002929 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2930 // Auto install icon
2931 PackageManager pm = context.getPackageManager();
2932 ResolveInfo matched = pm.resolveActivity(
2933 new Intent(Intent.ACTION_MAIN)
2934 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2935 PackageManager.MATCH_DEFAULT_ONLY);
2936 if (matched == null) {
2937 // Try to find the best match activity.
2938 Intent intent = pm.getLaunchIntentForPackage(
2939 cn.getPackageName());
2940 if (intent != null) {
2941 cn = intent.getComponent();
2942 appInfo = addedOrUpdatedApps.get(cn);
2943 }
2944
2945 if ((intent == null) || (appInfo == null)) {
2946 removedShortcuts.add(si);
2947 continue;
2948 }
2949 si.promisedIntent = intent;
2950 }
2951 }
2952
Sunny Goyal756adbc2015-04-16 15:20:51 -07002953 si.intent = si.promisedIntent;
2954 si.promisedIntent = null;
2955 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002956 infoUpdated = true;
2957 si.updateIcon(mIconCache);
2958 }
2959
2960 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2961 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2962 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002963 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002964 si.contentDescription = appInfo.contentDescription;
2965 infoUpdated = true;
2966 }
2967
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002968 int oldDisabledFlags = si.isDisabled;
2969 si.isDisabled = flagOp.apply(si.isDisabled);
2970 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002971 shortcutUpdated = true;
2972 }
2973 }
2974
2975 if (infoUpdated || shortcutUpdated) {
2976 updatedShortcuts.add(si);
2977 }
2978 if (infoUpdated) {
2979 updateItemInDatabase(context, si);
2980 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002981 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002982 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2983 if (mUser.equals(widgetInfo.user)
2984 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyal40452cf2016-09-01 15:17:46 -07002985 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002986 widgetInfo.restoreStatus &=
2987 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2988 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002989
2990 // adding this flag ensures that launcher shows 'click to setup'
2991 // if the widget has a config activity. In case there is no config
2992 // activity, it will be marked as 'restored' during bind.
2993 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2994
Sunny Goyal4390ace2014-10-13 11:33:11 -07002995 widgets.add(widgetInfo);
2996 updateItemInDatabase(context, widgetInfo);
2997 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002998 }
2999 }
3000 }
3001
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003002 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3003 if (!removedShortcuts.isEmpty()) {
3004 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003005 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003006
Sunny Goyal4390ace2014-10-13 11:33:11 -07003007 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003008 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003009 mHandler.post(new Runnable() {
3010 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003011 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003012 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003013 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003014 }
3015 }
3016 });
3017 }
3018 }
3019
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003020 final HashSet<String> removedPackages = new HashSet<>();
3021 final HashSet<ComponentName> removedComponents = new HashSet<>();
3022 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003023 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003024 Collections.addAll(removedPackages, packages);
3025
3026 // No need to update the removedComponents as
3027 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003028 } else if (mOp == OP_UPDATE) {
3029 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003030 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003031 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003032 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003033 }
3034 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003035
3036 // Update removedComponents as some components can get removed during package update
3037 for (AppInfo info : removedApps) {
3038 removedComponents.add(info.componentName);
3039 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003040 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003041
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003042 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
Sunny Goyal40452cf2016-09-01 15:17:46 -07003043 deleteItemsFromDatabase(
3044 context, ItemInfoMatcher.ofPackages(removedPackages, mUser));
3045 deleteItemsFromDatabase(
3046 context, ItemInfoMatcher.ofComponents(removedComponents, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003047
Winson Chungdf95eb12013-10-16 14:57:07 -07003048 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003049 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3050
Winson Chungdf95eb12013-10-16 14:57:07 -07003051 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003052 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003053 mHandler.post(new Runnable() {
3054 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003055 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003056 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003057 callbacks.bindWorkspaceComponentsRemoved(
3058 removedPackages, removedComponents, mUser);
3059 }
3060 }
3061 });
3062 }
3063
3064 if (!removedApps.isEmpty()) {
3065 // Remove corresponding apps from All-Apps
3066 final Callbacks callbacks = getCallback();
3067 mHandler.post(new Runnable() {
3068 public void run() {
3069 Callbacks cb = getCallback();
3070 if (callbacks == cb && cb != null) {
3071 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003072 }
3073 }
3074 });
Joe Onoratobe386092009-11-17 17:32:16 -08003075 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003076
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003077 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3078 // get widget update signals.
3079 if (!Utilities.ATLEAST_MARSHMALLOW &&
3080 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003081 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003082 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003083 public void run() {
3084 Callbacks cb = getCallback();
3085 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003086 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003087 }
3088 }
3089 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003090 }
3091 }
3092 }
3093
Sunny Goyal10923b32016-07-20 15:42:44 -07003094 /**
3095 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3096 */
3097 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3098 enqueueItemUpdatedTask(new Runnable() {
3099 @Override
3100 public void run() {
3101 info.updateFromDeepShortcutInfo(
3102 fullDetail, LauncherAppState.getInstance().getContext());
3103 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3104 update.add(info);
3105 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3106 }
3107 });
3108 }
3109
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003110 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003111 private final String mPackageName;
3112 private final List<ShortcutInfoCompat> mShortcuts;
3113 private final UserHandleCompat mUser;
3114 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003115
3116 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003117 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003118 mPackageName = packageName;
3119 mShortcuts = shortcuts;
3120 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003121 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003122 }
3123
3124 @Override
3125 public void run() {
3126 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3127
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003128 // Find ShortcutInfo's that have changed on the workspace.
Tony Wickham7695e602016-08-05 12:44:51 -07003129 final ArrayList<ShortcutInfo> removedShortcutInfos = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003130 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003131 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003132 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3133 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003134 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3135 && si.user.equals(mUser)) {
Tony Wickham7695e602016-08-05 12:44:51 -07003136 idsToWorkspaceShortcutInfos.addToList(si.getDeepShortcutId(), si);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003137 }
3138 }
3139 }
3140
Tony Wickham7695e602016-08-05 12:44:51 -07003141 final Context context = LauncherAppState.getInstance().getContext();
3142 final ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3143 if (!idsToWorkspaceShortcutInfos.isEmpty()) {
3144 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3145 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3146 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3147 for (ShortcutInfoCompat fullDetails : shortcuts) {
3148 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3149 .remove(fullDetails.getId());
3150 if (!fullDetails.isPinned()) {
3151 // The shortcut was previously pinned but is no longer, so remove it from
3152 // the workspace and our pinned shortcut counts.
3153 // Note that we put this check here, after querying for full details,
3154 // because there's a possible race condition between pinning and
3155 // receiving this callback.
3156 removedShortcutInfos.addAll(shortcutInfos);
3157 continue;
3158 }
3159 for (ShortcutInfo shortcutInfo : shortcutInfos) {
3160 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
3161 updatedShortcutInfos.add(shortcutInfo);
3162 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003163 }
3164 }
Tony Wickham7695e602016-08-05 12:44:51 -07003165
3166 // If there are still entries in idsToWorkspaceShortcutInfos, that means that
3167 // the corresponding shortcuts weren't passed in onShortcutsChanged(). This
3168 // means they were cleared, so we remove and unpin them now.
3169 for (String id : idsToWorkspaceShortcutInfos.keySet()) {
3170 removedShortcutInfos.addAll(idsToWorkspaceShortcutInfos.get(id));
3171 }
3172
3173 bindUpdatedShortcuts(updatedShortcutInfos, removedShortcutInfos, mUser);
3174 if (!removedShortcutInfos.isEmpty()) {
3175 deleteItemsFromDatabase(context, removedShortcutInfos);
3176 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003177
Sunny Goyal50941fb2016-08-04 12:03:52 -07003178 if (mUpdateIdMap) {
3179 // Update the deep shortcut map if the list of ids has changed for an activity.
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003180 sBgDataModel.updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Sunny Goyal50941fb2016-08-04 12:03:52 -07003181 bindDeepShortcuts();
3182 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003183 }
3184 }
3185
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003186 /**
3187 * Task to handle changing of lock state of the user
3188 */
3189 private class UserLockStateChangedTask implements Runnable {
3190
3191 private final UserHandleCompat mUser;
3192
3193 public UserLockStateChangedTask(UserHandleCompat user) {
3194 mUser = user;
3195 }
3196
3197 @Override
3198 public void run() {
3199 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3200 Context context = mApp.getContext();
3201
3202 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3203 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003204 List<ShortcutInfoCompat> shortcuts =
3205 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3206 if (mDeepShortcutManager.wasLastCallSuccess()) {
3207 for (ShortcutInfoCompat shortcut : shortcuts) {
3208 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3209 }
3210 } else {
3211 // Shortcut manager can fail due to some race condition when the lock state
3212 // changes too frequently. For the purpose of the update,
3213 // consider it as still locked.
3214 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003215 }
3216 }
3217
3218 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3219 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3220 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07003221 for (ItemInfo itemInfo : sBgDataModel.itemsIdMap) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003222 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3223 && mUser.equals(itemInfo.user)) {
3224 ShortcutInfo si = (ShortcutInfo) itemInfo;
3225 if (isUserUnlocked) {
3226 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003227 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003228 // We couldn't verify the shortcut during loader. If its no longer available
3229 // (probably due to clear data), delete the workspace item as well
3230 if (shortcut == null) {
3231 deletedShortcutInfos.add(si);
3232 continue;
3233 }
3234 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3235 si.updateFromDeepShortcutInfo(shortcut, context);
3236 } else {
3237 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3238 }
3239 updatedShortcutInfos.add(si);
3240 }
3241 }
3242 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3243 if (!deletedShortcutInfos.isEmpty()) {
3244 deleteItemsFromDatabase(context, deletedShortcutInfos);
3245 }
3246
3247 // Remove shortcut id map for that user
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003248 Iterator<ComponentKey> keysIter = sBgDataModel.deepShortcutMap.keySet().iterator();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003249 while (keysIter.hasNext()) {
3250 if (keysIter.next().user.equals(mUser)) {
3251 keysIter.remove();
3252 }
3253 }
3254
3255 if (isUserUnlocked) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07003256 sBgDataModel.updateDeepShortcutMap(
3257 null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003258 }
3259 bindDeepShortcuts();
3260 }
3261 }
3262
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003263 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3264 mHandler.post(new Runnable() {
3265 @Override
3266 public void run() {
3267 Callbacks cb = getCallback();
3268 if (callbacks == cb && cb != null) {
3269 callbacks.bindWidgetsModel(model);
3270 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003271 }
3272 });
3273 }
3274
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003275 public void refreshAndBindWidgetsAndShortcuts(
3276 final Callbacks callbacks, final boolean bindFirst) {
3277 runOnWorkerThread(new Runnable() {
3278 @Override
3279 public void run() {
3280 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3281 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003282 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003283 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3284 bindWidgetsModel(callbacks, model);
3285 // update the Widget entries inside DB on the worker thread.
3286 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3287 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003288 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003289 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003290 }
3291
Adam Cohen091440a2015-03-18 14:16:05 -07003292 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003293 UserHandleCompat user) {
3294 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3295 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003296 }
Adam Cohen556f6132014-01-15 15:18:08 -08003297
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003299 * Make an ShortcutInfo object for a restored application or shortcut item that points
3300 * to a package that is not yet installed on the system.
3301 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003302 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3303 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003304 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003305 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003306
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003307 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003308 // the fallback icon
3309 if (icon == null) {
3310 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3311 } else {
3312 info.setIcon(icon);
3313 }
Sunny Goyal34942622014-08-29 17:20:55 -07003314
3315 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003316 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003317 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003318 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003319 }
Sunny Goyal34942622014-08-29 17:20:55 -07003320 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3321 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003322 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003323 }
Sunny Goyal34942622014-08-29 17:20:55 -07003324 } else {
3325 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3326 }
3327
Winson Chung82b016c2015-05-08 17:00:10 -07003328 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003329 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003330 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003331 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003332 return info;
3333 }
3334
3335 /**
3336 * Make an Intent object for a restored application or shortcut item that points
3337 * to the market page for the item.
3338 */
Adam Cohen091440a2015-03-18 14:16:05 -07003339 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003340 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003341 return getMarketIntent(componentName.getPackageName());
3342 }
3343
3344 static Intent getMarketIntent(String packageName) {
3345 return new Intent(Intent.ACTION_VIEW)
3346 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003347 .scheme("market")
3348 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003349 .appendQueryParameter("id", packageName)
3350 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003351 }
3352
3353 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003354 * Make an ShortcutInfo object for a shortcut that is an application.
3355 *
3356 * If c is not null, then it will be used to fill in missing data like the title and icon.
3357 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003358 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003359 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003360 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003361 if (user == null) {
3362 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003363 return null;
3364 }
3365
Kenny Guyed131872014-04-30 03:02:21 +01003366 ComponentName componentName = intent.getComponent();
3367 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003368 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003369 return null;
3370 }
3371
3372 Intent newIntent = new Intent(intent.getAction(), null);
3373 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3374 newIntent.setComponent(componentName);
3375 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003376 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003377 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3378 return null;
3379 }
3380
3381 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003382 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003383 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003384 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003385 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003386 }
3387
Sunny Goyald09c3702016-04-06 16:18:20 -07003388 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3389 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3390 }
3391
Joe Onorato56d82912010-03-07 14:32:10 -05003392 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003393 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003394 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003395 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003396
Joe Onorato56d82912010-03-07 14:32:10 -05003397 // fall back to the class name of the activity
3398 if (info.title == null) {
3399 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003400 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003401
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003403 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003404 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003406 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003407
Sunny Goyal1a745e82014-10-02 15:58:31 -07003408 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003409 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003410 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003411 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003412 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003413 // Non-app shortcuts are only supported for current user.
3414 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003416
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003417 // TODO: If there's an explicit component and we can't install that, delete it.
3418
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003419 loadInfoFromCursor(info, c, iconInfo);
3420 return info;
3421 }
Joe Onorato56d82912010-03-07 14:32:10 -05003422
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003423 /**
3424 * Make an ShortcutInfo object for a shortcut that isn't an application.
3425 */
3426 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3427 info.title = iconInfo.getTitle(c);
3428 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003429 // the fallback icon
3430 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003431 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003432 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003433 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003434 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436
Sunny Goyal2350bc92014-10-14 16:42:54 -07003437 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003438 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3439 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3440 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3441
Adam Cohend9198822011-11-22 16:42:47 -08003442 if (intent == null) {
3443 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3444 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3445 return null;
3446 }
3447
Joe Onorato0589f0f2010-02-08 13:44:00 -08003448 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003449 ShortcutIconResource iconResource = null;
3450
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003451 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003452 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003453 } else {
3454 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003455 if (extra instanceof ShortcutIconResource) {
3456 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07003457 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003458 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003459 }
3460 }
3461
Michael Jurkac9d95c52011-08-29 14:03:34 -07003462 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003463
Kenny Guyed131872014-04-30 03:02:21 +01003464 // Only support intents for current user for now. Intents sent from other
3465 // users wouldn't get here without intent forwarding anyway.
3466 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003467 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003468 icon = mIconCache.getDefaultIcon(info.user);
3469 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003470 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003471 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003472
Winson Chung82b016c2015-05-08 17:00:10 -07003473 info.title = Utilities.trim(name);
3474 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003475 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003476 info.iconResource = iconResource;
3477
3478 return info;
3479 }
3480
Sunny Goyal651077b2014-06-30 14:15:31 -07003481 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3482 return (provider != null) && (provider.provider != null)
3483 && (provider.provider.getPackageName() != null);
3484 }
3485
Joe Onoratobe386092009-11-17 17:32:16 -08003486 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003487 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003488 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3489 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3490 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3491 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003492 if (mLoaderTask != null) {
3493 mLoaderTask.dumpState();
3494 } else {
3495 Log.d(TAG, "mLoaderTask=null");
3496 }
Joe Onoratobe386092009-11-17 17:32:16 -08003497 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003498
3499 public Callbacks getCallback() {
3500 return mCallbacks != null ? mCallbacks.get() : null;
3501 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003502
3503 /**
3504 * @return {@link FolderInfo} if its already loaded.
3505 */
3506 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07003507 synchronized (sBgDataModel) {
3508 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003509 }
3510 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003511
Sunny Goyal527c7d32015-08-28 15:19:36 -07003512 @Thunk class DeferredMainThreadExecutor implements Executor {
3513
3514 @Override
3515 public void execute(Runnable command) {
3516 runOnMainThread(command);
3517 }
3518 }
3519
Sunny Goyal756adbc2015-04-16 15:20:51 -07003520 /**
3521 * @return the looper for the worker thread which can be used to start background tasks.
3522 */
3523 public static Looper getWorkerLooper() {
3524 return sWorkerThread.getLooper();
3525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003526}