blob: 68a9c7e7fdfe53b1174bd2cac8f4f2336c8fe647 [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;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Sunny Goyalffe83f12014-08-14 17:39:34 -070047import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.LauncherActivityInfoCompat;
49import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070050import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070051import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.UserHandleCompat;
53import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070056import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000057import com.android.launcher3.folder.Folder;
58import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070059import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080060import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070062import com.android.launcher3.provider.LauncherDbUtils;
Robin Lee26ace122015-03-16 19:41:43 +000063import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070064import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070065import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070066import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070067import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070068import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyald09c3702016-04-06 16:18:20 -070069import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070070import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070071import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070072import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070073import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080074
Michael Jurkac2f801e2011-07-12 14:19:46 -070075import java.lang.ref.WeakReference;
76import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070077import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070079import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070080import java.util.Collections;
81import java.util.Comparator;
82import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070083import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070084import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070086import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070087import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070088import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090/**
91 * Maintains in-memory state of the Launcher. It is expected that there should be only one
92 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070093 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 */
Kenny Guyed131872014-04-30 03:02:21 +010095public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010096 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080097 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040098 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070099 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400100
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700102
Joe Onorato36115782010-06-17 13:28:48 -0400103 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500104 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final LauncherAppState mApp;
107 @Thunk final Object mLock = new Object();
108 @Thunk DeferredHandler mHandler = new DeferredHandler();
109 @Thunk LoaderTask mLoaderTask;
110 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700111 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700114 static {
115 sWorkerThread.start();
116 }
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700118
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 // We start off with everything not loaded. After that, we assume that
120 // our monitoring of the package manager provides all updates and we never
121 // need to do a requery. These are only ever touched from the loader thread.
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk boolean mWorkspaceLoaded;
123 @Thunk boolean mAllAppsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Sunny Goyal756a28a2015-04-23 17:07:55 -0700125 /**
126 * Set of runnables to be called on the background thread after the workspace binding
127 * is complete.
128 */
129 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
130
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800134 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700135 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800136 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800137
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700138 // The lock that must be acquired before referencing any static bg data structures. Unlike
139 // other locks, this one can generally be held long-term because we never expect any of these
140 // static data structures to be referenced outside of the worker thread except on the first
141 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700142 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700143
Adam Cohen487f7dd2012-06-28 18:12:10 -0700144 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700146 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700147
Adam Cohen487f7dd2012-06-28 18:12:10 -0700148 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
149 // created by LauncherModel that are directly on the home screen (however, no widgets or
150 // shortcuts within folders).
151 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
154 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155 new ArrayList<LauncherAppWidgetInfo>();
156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700158 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700159
Adam Cohendcd297f2013-06-18 13:13:40 -0700160 // sBgWorkspaceScreens is the ordered set of workspace screens.
161 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
162
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700163 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700164 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
165 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700166
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700167 // </ only access in worker thread >
168
Adam Cohen091440a2015-03-18 14:16:05 -0700169 @Thunk IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Adam Cohen091440a2015-03-18 14:16:05 -0700171 @Thunk final LauncherAppsCompat mLauncherApps;
172 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100173
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700175 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700177 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700179 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
180 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700181 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800182 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200184 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700185 public void bindAppsAdded(ArrayList<Long> newScreens,
186 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700187 ArrayList<ItemInfo> addAnimated,
188 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200189 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700190 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
191 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
192 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700193 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700194 public void bindWorkspaceComponentsRemoved(
195 HashSet<String> packageNames, HashSet<ComponentName> components,
196 UserHandleCompat user);
197 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800198 public void notifyWidgetProvidersChanged();
199 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700200 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700201 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Winson Chung64359a52013-07-08 17:17:08 -0700204 public interface ItemInfoFilter {
205 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
206 }
207
Bjorn Bringert1307f632013-10-03 22:31:03 +0100208 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
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;
214
Kenny Guyed131872014-04-30 03:02:21 +0100215 mLauncherApps = LauncherAppsCompat.getInstance(context);
216 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 }
218
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700219 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
220 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700222 if (sWorkerThread.getThreadId() == Process.myTid()) {
223 // If we are on the worker thread, post onto the main handler
224 mHandler.post(r);
225 } else {
226 r.run();
227 }
228 }
229
230 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
231 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700232 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700233 if (sWorkerThread.getThreadId() == Process.myTid()) {
234 r.run();
235 } else {
236 // If we are not on the worker thread, then post to the worker handler
237 sWorker.post(r);
238 }
239 }
240
Sunny Goyal756adbc2015-04-16 15:20:51 -0700241 public void setPackageState(final PackageInstallInfo installInfo) {
242 Runnable updateRunnable = new Runnable() {
243
244 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500245 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700246 synchronized (sBgLock) {
247 final HashSet<ItemInfo> updates = new HashSet<>();
248
249 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
250 // Ignore install success events as they are handled by Package add events.
251 return;
252 }
253
Sunny Goyale2df0622015-04-24 11:27:00 -0700254 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700255 if (info instanceof ShortcutInfo) {
256 ShortcutInfo si = (ShortcutInfo) info;
257 ComponentName cn = si.getTargetComponent();
258 if (si.isPromise() && (cn != null)
259 && installInfo.packageName.equals(cn.getPackageName())) {
260 si.setInstallProgress(installInfo.progress);
261
262 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
263 // Mark this info as broken.
264 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
265 }
266 updates.add(si);
267 }
268 }
269 }
270
271 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
272 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
273 widget.installProgress = installInfo.progress;
274 updates.add(widget);
275 }
276 }
277
278 if (!updates.isEmpty()) {
279 // Push changes to the callback.
280 Runnable r = new Runnable() {
281 public void run() {
282 Callbacks callbacks = getCallback();
283 if (callbacks != null) {
284 callbacks.bindRestoreItemsChange(updates);
285 }
286 }
287 };
288 mHandler.post(r);
289 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500290 }
291 }
292 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700293 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500294 }
295
Sunny Goyal756adbc2015-04-16 15:20:51 -0700296 /**
297 * Updates the icons and label of all pending icons for the provided package name.
298 */
299 public void updateSessionDisplayInfo(final String packageName) {
300 Runnable updateRunnable = new Runnable() {
301
302 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700303 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700304 synchronized (sBgLock) {
305 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
306 final UserHandleCompat user = UserHandleCompat.myUserHandle();
307
Sunny Goyale2df0622015-04-24 11:27:00 -0700308 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700309 if (info instanceof ShortcutInfo) {
310 ShortcutInfo si = (ShortcutInfo) info;
311 ComponentName cn = si.getTargetComponent();
312 if (si.isPromise() && (cn != null)
313 && packageName.equals(cn.getPackageName())) {
314 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
315 // For auto install apps update the icon as well as label.
316 mIconCache.getTitleAndIcon(si,
317 si.promisedIntent, user,
318 si.shouldUseLowResIcon());
319 } else {
320 // Only update the icon for restored apps.
321 si.updateIcon(mIconCache);
322 }
323 updates.add(si);
324 }
325 }
326 }
327
328 if (!updates.isEmpty()) {
329 // Push changes to the callback.
330 Runnable r = new Runnable() {
331 public void run() {
332 Callbacks callbacks = getCallback();
333 if (callbacks != null) {
334 callbacks.bindShortcutsChanged(updates,
335 new ArrayList<ShortcutInfo>(), user);
336 }
337 }
338 };
339 mHandler.post(r);
340 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700341 }
342 }
343 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700344 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700345 }
346
Adam Cohen76a47a12014-02-05 11:47:43 -0800347 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800348 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800349
350 if (allAppsApps == null) {
351 throw new RuntimeException("allAppsApps must not be null");
352 }
353 if (allAppsApps.isEmpty()) {
354 return;
355 }
356
357 // Process the newly added applications and add them to the database first
358 Runnable r = new Runnable() {
359 public void run() {
360 runOnMainThread(new Runnable() {
361 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800362 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800363 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500364 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800365 }
366 }
367 });
368 }
369 };
370 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700371 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800372
Sunny Goyala9116722015-04-29 13:55:58 -0700373 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800374 int[] xy, int spanX, int spanY) {
375 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700376 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700377
378 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800379 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700380 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700381 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800382 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800383 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700384 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800385 }
386
387 /**
388 * Find a position on the screen for the given size or adds a new screen.
389 * @return screenId and the coordinates for the item.
390 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700391 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700392 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 ArrayList<Long> workspaceScreens,
394 ArrayList<Long> addedWorkspaceScreensFinal,
395 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700396 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800397
Sunny Goyala9116722015-04-29 13:55:58 -0700398 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700399 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700400 synchronized (sBgLock) {
401 for (ItemInfo info : sBgItemsIdMap) {
402 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
403 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
404 if (items == null) {
405 items = new ArrayList<>();
406 screenItems.put(info.screenId, items);
407 }
408 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800409 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800410 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 }
412
413 // Find appropriate space for the item.
414 long screenId = 0;
415 int[] cordinates = new int[2];
416 boolean found = false;
417
418 int screenCount = workspaceScreens.size();
419 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700420 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800421 if (preferredScreenIndex < screenCount) {
422 screenId = workspaceScreens.get(preferredScreenIndex);
423 found = findNextAvailableIconSpaceInScreen(
424 screenItems.get(screenId), cordinates, spanX, spanY);
425 }
426
427 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700428 // Search on any of the screens starting from the first screen.
429 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 screenId = workspaceScreens.get(screen);
431 if (findNextAvailableIconSpaceInScreen(
432 screenItems.get(screenId), cordinates, spanX, spanY)) {
433 // We found a space for it
434 found = true;
435 break;
436 }
437 }
438 }
439
440 if (!found) {
441 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700442 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
443 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
444 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800445
446 // Save the screen id for binding in the workspace
447 workspaceScreens.add(screenId);
448 addedWorkspaceScreensFinal.add(screenId);
449
450 // If we still can't find an empty space, then God help us all!!!
451 if (!findNextAvailableIconSpaceInScreen(
452 screenItems.get(screenId), cordinates, spanX, spanY)) {
453 throw new RuntimeException("Can't find space to add the item");
454 }
455 }
456 return Pair.create(screenId, cordinates);
457 }
458
459 /**
460 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800461 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700462 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700463 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800465 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700466 return;
Winson Chung997a9232013-07-24 15:33:46 -0700467 }
Winson Chung64359a52013-07-08 17:17:08 -0700468 // Process the newly added applications and add them to the database first
469 Runnable r = new Runnable() {
470 public void run() {
471 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
472 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
473
Winson Chung76828c82013-08-19 15:43:29 -0700474 // Get the list of workspace screens. We need to append to this list and
475 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
476 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800477 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700478 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800479 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700480 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800481 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700482 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700484 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800485 }
Winson Chung76828c82013-08-19 15:43:29 -0700486
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800487 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700488 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700489 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800490 long screenId = coords.first;
491 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700492
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700493 ItemInfo itemInfo;
494 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
495 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800496 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700497 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700498 } else {
499 throw new RuntimeException("Unexpected info type");
500 }
Winson Chung94d67682013-09-25 16:29:40 -0700501
Winson Chung64359a52013-07-08 17:17:08 -0700502 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700503 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700504 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700505 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700506 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700507 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700508 }
509 }
510
Winson Chung76828c82013-08-19 15:43:29 -0700511 // Update the workspace screens
512 updateWorkspaceScreenOrder(context, workspaceScreens);
513
Adam Cohen76a47a12014-02-05 11:47:43 -0800514 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700515 runOnMainThread(new Runnable() {
516 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800517 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700518 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700519 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
520 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700521 if (!addedShortcutsFinal.isEmpty()) {
522 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
523 long lastScreenId = info.screenId;
524 for (ItemInfo i : addedShortcutsFinal) {
525 if (i.screenId == lastScreenId) {
526 addAnimated.add(i);
527 } else {
528 addNotAnimated.add(i);
529 }
Winson Chung997a9232013-07-24 15:33:46 -0700530 }
531 }
Winson Chungd64d1762013-08-20 14:37:16 -0700532 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800533 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700534 }
Winson Chung64359a52013-07-08 17:17:08 -0700535 }
Winson Chung997a9232013-07-24 15:33:46 -0700536 });
537 }
Winson Chung64359a52013-07-08 17:17:08 -0700538 }
539 };
540 runOnWorkerThread(r);
541 }
542
Joe Onorato9c1289c2009-08-17 11:03:03 -0400543 /**
544 * Adds an item to the DB if it was not created previously, or move it to a new
545 * <container, screen, cellX, cellY>
546 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800547 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700548 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549 if (item.container == ItemInfo.NO_ID) {
550 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700551 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 } else {
553 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700554 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556 }
557
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700558 static void checkItemInfoLocked(
559 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
560 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
561 if (modelItem != null && item != modelItem) {
562 // check all the data is consistent
563 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
564 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
565 ShortcutInfo shortcut = (ShortcutInfo) item;
566 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
567 modelShortcut.intent.filterEquals(shortcut.intent) &&
568 modelShortcut.id == shortcut.id &&
569 modelShortcut.itemType == shortcut.itemType &&
570 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700571 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700572 modelShortcut.cellX == shortcut.cellX &&
573 modelShortcut.cellY == shortcut.cellY &&
574 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700575 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700576 // For all intents and purposes, this is the same object
577 return;
578 }
579 }
580
581 // the modelItem needs to match up perfectly with item if our model is
582 // to be consistent with the database-- for now, just require
583 // modelItem == item or the equality check above
584 String msg = "item: " + ((item != null) ? item.toString() : "null") +
585 "modelItem: " +
586 ((modelItem != null) ? modelItem.toString() : "null") +
587 "Error: ItemInfo passed to checkItemInfo doesn't match original";
588 RuntimeException e = new RuntimeException(msg);
589 if (stackTrace != null) {
590 e.setStackTrace(stackTrace);
591 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800592 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700593 }
594 }
595
Michael Jurka816474f2012-06-25 14:49:02 -0700596 static void checkItemInfo(final ItemInfo item) {
597 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
598 final long itemId = item.id;
599 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700600 public void run() {
601 synchronized (sBgLock) {
602 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700603 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700604 }
605 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700606 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700607 }
608
Michael Jurkac9d95c52011-08-29 14:03:34 -0700609 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
610 final ItemInfo item, final String callingFunction) {
611 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700612 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700613 final ContentResolver cr = context.getContentResolver();
614
Adam Cohen487f7dd2012-06-28 18:12:10 -0700615 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700616 Runnable r = new Runnable() {
617 public void run() {
618 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700619 updateItemArrays(item, itemId, stackTrace);
620 }
621 };
622 runOnWorkerThread(r);
623 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700625 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
626 final ArrayList<ItemInfo> items, final String callingFunction) {
627 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700628
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700629 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
630 Runnable r = new Runnable() {
631 public void run() {
632 ArrayList<ContentProviderOperation> ops =
633 new ArrayList<ContentProviderOperation>();
634 int count = items.size();
635 for (int i = 0; i < count; i++) {
636 ItemInfo item = items.get(i);
637 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700638 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700639 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700640
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700641 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
642 updateItemArrays(item, itemId, stackTrace);
643
644 }
645 try {
646 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
647 } catch (Exception e) {
648 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700649 }
650 }
651 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700652 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700653 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700654
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700655 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
656 // Lock on mBgLock *after* the db operation
657 synchronized (sBgLock) {
658 checkItemInfoLocked(itemId, item, stackTrace);
659
660 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
661 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
662 // Item is in a folder, make sure this folder exists
663 if (!sBgFolders.containsKey(item.container)) {
664 // An items container is being set to a that of an item which is not in
665 // the list of Folders.
666 String msg = "item: " + item + " container being set to: " +
667 item.container + ", not in the list of folders";
668 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700669 }
670 }
671
672 // Items are added/removed from the corresponding FolderInfo elsewhere, such
673 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
674 // that are on the desktop, as appropriate
675 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800676 if (modelItem != null &&
677 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
678 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 switch (modelItem.itemType) {
680 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
681 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
682 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
683 if (!sBgWorkspaceItems.contains(modelItem)) {
684 sBgWorkspaceItems.add(modelItem);
685 }
686 break;
687 default:
688 break;
689 }
690 } else {
691 sBgWorkspaceItems.remove(modelItem);
692 }
693 }
694 }
695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700698 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700699 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700700 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 item.cellX = cellX;
703 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700704
Winson Chung3d503fb2011-07-13 17:25:49 -0700705 // We store hotseat items in canonical form which is this orientation invariant position
706 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700708 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700710 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700712 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713
714 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700716 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
717 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800718 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720
Michael Jurkac9d95c52011-08-29 14:03:34 -0700721 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700722 }
723
724 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700725 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
726 * cellX, cellY have already been updated on the ItemInfos.
727 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800728 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 final long container, final int screen) {
730
731 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
732 int count = items.size();
733
734 for (int i = 0; i < count; i++) {
735 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700736 item.container = container;
737
738 // We store hotseat items in canonical form which is this orientation invariant position
739 // in the hotseat
740 if (context instanceof Launcher && screen < 0 &&
741 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 item.cellY);
744 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700745 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700746 }
747
748 final ContentValues values = new ContentValues();
749 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
750 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
751 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800752 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700753 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754
755 contentValues.add(values);
756 }
757 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
758 }
759
760 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700761 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800762 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700763 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700765 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800766 item.cellX = cellX;
767 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700768 item.spanX = spanX;
769 item.spanY = spanY;
770
771 // We store hotseat items in canonical form which is this orientation invariant position
772 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700774 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700776 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700778 }
Adam Cohend4844c32011-02-18 19:25:06 -0800779
Adam Cohend4844c32011-02-18 19:25:06 -0800780 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800781 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700782 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
783 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800784 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
786 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800788
Michael Jurka816474f2012-06-25 14:49:02 -0700789 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700790 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700791
792 /**
793 * Update an item to the database in a specified container.
794 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700795 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700796 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100797 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700798 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800799 }
800
Sunny Goyal756a28a2015-04-23 17:07:55 -0700801 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700802 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700803 synchronized (mLock) {
804 if (!mHasLoaderCompletedOnce ||
805 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
806 throw new RuntimeException("Trying to add shortcut while loader is running");
807 }
808 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700809 }
810 }
811
Adam Cohend4844c32011-02-18 19:25:06 -0800812 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700813 * Returns true if the shortcuts already exists on the workspace. This must be called after
814 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700816 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
817 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700818 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700819 if (intent.getComponent() != null) {
820 // If component is not null, an intent with null package will produce
821 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700822 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700823 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700824 intentWithPkg = intent.toUri(0);
825 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700826 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700827 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
828 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700829 }
830 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700831 intentWithPkg = intent.toUri(0);
832 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700833 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700834
835 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700836 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700837 if (item instanceof ShortcutInfo) {
838 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700839 Intent targetIntent = info.promisedIntent == null
840 ? info.intent : info.promisedIntent;
841 if (targetIntent != null && info.user.equals(user)) {
842 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700843 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
844 return true;
845 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700846 }
847 }
848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700850 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700851 }
852
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 * Add an item to the database in a specified container. Sets the container, screen, cellX and
855 * cellY fields of the item. Also assigns an ID to the item.
856 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700857 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700858 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860 item.cellX = cellX;
861 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 // We store hotseat items in canonical form which is this orientation invariant position
863 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700867 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700868 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870
871 final ContentValues values = new ContentValues();
872 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100873 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874
Sunny Goyald2497482015-09-22 18:24:19 -0700875 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
876 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
877
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700878 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700879
Jason Monk8e19cf22014-03-20 15:06:57 -0400880 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700881 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700882 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700883 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700885 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700886 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400887 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700888 sBgItemsIdMap.put(item.id, item);
889 switch (item.itemType) {
890 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
891 sBgFolders.put(item.id, (FolderInfo) item);
892 // Fall through
893 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
894 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
895 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
896 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
897 sBgWorkspaceItems.add(item);
898 } else {
899 if (!sBgFolders.containsKey(item.container)) {
900 // Adding an item to a folder that doesn't exist.
901 String msg = "adding item: " + item + " to a folder that " +
902 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700903 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700904 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 break;
907 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
908 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
909 break;
910 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700911 }
912 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700915 }
916
Sunny Goyal34942622014-08-29 17:20:55 -0700917 private static ArrayList<ItemInfo> getItemsByPackageName(
918 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700919 ItemInfoFilter filter = new ItemInfoFilter() {
920 @Override
921 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
922 return cn.getPackageName().equals(pn) && info.user.equals(user);
923 }
924 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700925 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700926 }
927
928 /**
929 * Removes all the items from the database corresponding to the specified package.
930 */
931 static void deletePackageFromDatabase(Context context, final String pn,
932 final UserHandleCompat user) {
933 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700934 }
935
936 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700937 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700939 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700940 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
941 items.add(item);
942 deleteItemsFromDatabase(context, items);
943 }
944
945 /**
946 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700947 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700948 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700950 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700952 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700953 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700954 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700956 // Lock on mBgLock *after* the db operation
957 synchronized (sBgLock) {
958 switch (item.itemType) {
959 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
960 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700961 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700962 if (info.container == item.id) {
963 // We are deleting a folder which still contains items that
964 // think they are contained by that folder.
965 String msg = "deleting a folder (" + item + ") which still " +
966 "contains items (" + info + ")";
967 Log.e(TAG, msg);
968 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700969 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700970 sBgWorkspaceItems.remove(item);
971 break;
972 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
973 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
974 sBgWorkspaceItems.remove(item);
975 break;
976 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
977 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
978 break;
979 }
980 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700981 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700982 }
983 }
Michael Jurka83df1882011-08-31 20:59:26 -0700984 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 }
987
988 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 * Update the order of the workspace screens in the database. The array list contains
990 * a list of screen ids in the order that they should appear.
991 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700992 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700993 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 final ContentResolver cr = context.getContentResolver();
995 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
996
997 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700998 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700999 while (iter.hasNext()) {
1000 long id = iter.next();
1001 if (id < 0) {
1002 iter.remove();
1003 }
1004 }
1005
1006 Runnable r = new Runnable() {
1007 @Override
1008 public void run() {
Yura085c8532014-02-11 15:15:29 +00001009 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001010 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001011 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001012 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001013 for (int i = 0; i < count; i++) {
1014 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001015 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001016 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1017 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001018 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001019 }
Yura085c8532014-02-11 15:15:29 +00001020
1021 try {
1022 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1023 } catch (Exception ex) {
1024 throw new RuntimeException(ex);
1025 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001026
Winson Chungba9c37f2013-08-30 14:11:37 -07001027 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001028 sBgWorkspaceScreens.clear();
1029 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001030 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001031 }
1032 };
1033 runOnWorkerThread(r);
1034 }
1035
1036 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001037 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 */
Winsonc0b52fe2015-09-09 16:38:15 -07001039 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 final ContentResolver cr = context.getContentResolver();
1041
Michael Jurkac9d95c52011-08-29 14:03:34 -07001042 Runnable r = new Runnable() {
1043 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001044 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001046 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 sBgItemsIdMap.remove(info.id);
1048 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 sBgWorkspaceItems.remove(info);
1050 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001051
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001052 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001053 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001055 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 for (ItemInfo childInfo : info.contents) {
1057 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001059 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001060 }
1061 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001062 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 }
1064
1065 /**
1066 * Set this as the current Launcher activity object for the loader.
1067 */
1068 public void initialize(Callbacks callbacks) {
1069 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001070 Preconditions.assertUIThread();
1071 // Remove any queued UI runnables
1072 mHandler.cancelAll();
1073 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 }
1075 }
1076
Kenny Guyed131872014-04-30 03:02:21 +01001077 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001078 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001079 int op = PackageUpdatedTask.OP_UPDATE;
1080 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1081 user));
1082 }
1083
1084 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001085 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001086 int op = PackageUpdatedTask.OP_REMOVE;
1087 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1088 user));
1089 }
1090
1091 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001092 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001093 int op = PackageUpdatedTask.OP_ADD;
1094 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1095 user));
1096 }
1097
1098 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001099 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001100 boolean replacing) {
Sunny Goyal144154d2016-03-30 16:47:03 -07001101 enqueuePackageUpdated(
1102 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001103 }
1104
1105 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001106 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001107 boolean replacing) {
1108 if (!replacing) {
1109 enqueuePackageUpdated(new PackageUpdatedTask(
1110 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1111 user));
1112 }
Kenny Guyed131872014-04-30 03:02:21 +01001113 }
1114
Kenny Guy44cba692016-01-21 19:50:02 +00001115 @Override
1116 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
1117 enqueuePackageUpdated(new PackageUpdatedTask(
1118 PackageUpdatedTask.OP_SUSPEND, packageNames,
1119 user));
1120 }
1121
1122 @Override
1123 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
1124 enqueuePackageUpdated(new PackageUpdatedTask(
1125 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1126 user));
1127 }
1128
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001129 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1131 * ACTION_PACKAGE_CHANGED.
1132 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001133 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001134 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001135 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001136
Joe Onorato36115782010-06-17 13:28:48 -04001137 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001138 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001139 // If we have changed locale we need to clear out the labels in all apps/workspace.
1140 forceReload();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001141 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001142 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001143 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001144 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001145 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1146 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001147 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1148 if (user != null) {
1149 enqueuePackageUpdated(new PackageUpdatedTask(
1150 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1151 new String[0], user));
1152 }
Tony Wickham827cef22016-03-17 15:39:39 -07001153 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1154 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001155 }
1156 }
1157
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001158 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001159 resetLoadedState(true, true);
1160
Reena Lee93f824a2011-09-23 17:20:28 -07001161 // Do this here because if the launcher activity is running it will be restarted.
1162 // If it's not running startLoaderFromBackground will merely tell it that it needs
1163 // to reload.
1164 startLoaderFromBackground();
1165 }
1166
Winson Chungf0c6ae02012-03-21 16:10:31 -07001167 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1168 synchronized (mLock) {
1169 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1170 // mWorkspaceLoaded to true later
1171 stopLoaderLocked();
1172 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1173 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1174 }
1175 }
1176
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001177 /**
1178 * When the launcher is in the background, it's possible for it to miss paired
1179 * configuration changes. So whenever we trigger the loader from the background
1180 * tell the launcher that it needs to re-run the loader when it comes back instead
1181 * of doing it now.
1182 */
1183 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001184 Callbacks callbacks = getCallback();
1185 if (callbacks != null) {
1186 // Only actually run the loader if they're not paused.
1187 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001188 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001189 }
1190 }
Joe Onorato36115782010-06-17 13:28:48 -04001191 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001192
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001193 /**
1194 * If there is already a loader task running, tell it to stop.
1195 */
1196 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001197 LoaderTask oldTask = mLoaderTask;
1198 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001199 oldTask.stopLocked();
1200 }
Reena Lee93f824a2011-09-23 17:20:28 -07001201 }
1202
Adam Cohen1a85c582014-09-30 09:48:49 -07001203 public boolean isCurrentCallbacks(Callbacks callbacks) {
1204 return (mCallbacks != null && mCallbacks.get() == callbacks);
1205 }
1206
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001207 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001208 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1209 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001210 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001211 // Don't bother to start the thread if we know it's not going to do anything
1212 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001213 final Callbacks oldCallbacks = mCallbacks.get();
1214 // Clear any pending bind-runnables from the synchronized load process.
1215 runOnMainThread(new Runnable() {
1216 public void run() {
1217 oldCallbacks.clearPendingBinds();
1218 }
1219 });
1220
Joe Onorato36115782010-06-17 13:28:48 -04001221 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001222 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001223 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001224 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001225 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001226 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1227 } else {
1228 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1229 sWorker.post(mLoaderTask);
1230 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001231 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 }
1233 }
1234
Joe Onorato36115782010-06-17 13:28:48 -04001235 public void stopLoader() {
1236 synchronized (mLock) {
1237 if (mLoaderTask != null) {
1238 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 }
1240 }
Joe Onorato36115782010-06-17 13:28:48 -04001241 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001242
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001243 /**
1244 * Loads the workspace screen ids in an ordered list.
1245 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001246 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001247 final ContentResolver contentResolver = context.getContentResolver();
1248 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001249
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001250 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001251 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1252 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001253 }
1254
Joe Onorato36115782010-06-17 13:28:48 -04001255 /**
1256 * Runnable for the thread that loads the contents of the launcher:
1257 * - workspace icons
1258 * - widgets
1259 * - all apps icons
1260 */
1261 private class LoaderTask implements Runnable {
1262 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001263 private int mPageToBindFirst;
1264
Adam Cohen091440a2015-03-18 14:16:05 -07001265 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001266 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001267 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001268
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001269 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001270 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001271 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001272 }
1273
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001274 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001275 mIsLoadingAndBindingWorkspace = true;
1276
Joe Onorato36115782010-06-17 13:28:48 -04001277 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001278 if (DEBUG_LOADERS) {
1279 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001281
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001282 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001283 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001284 synchronized (LoaderTask.this) {
1285 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001286 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001287 }
1288 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 }
1290 }
1291
Joe Onorato36115782010-06-17 13:28:48 -04001292 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001293 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001294 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001295
Joe Onorato36115782010-06-17 13:28:48 -04001296 private void waitForIdle() {
1297 // Wait until the either we're stopped or the other threads are done.
1298 // This way we don't start loading all apps until the workspace has settled
1299 // down.
1300 synchronized (LoaderTask.this) {
1301 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001302
Joe Onorato36115782010-06-17 13:28:48 -04001303 mHandler.postIdle(new Runnable() {
1304 public void run() {
1305 synchronized (LoaderTask.this) {
1306 mLoadAndBindStepFinished = true;
1307 if (DEBUG_LOADERS) {
1308 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001309 }
Joe Onorato36115782010-06-17 13:28:48 -04001310 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001311 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001312 }
Joe Onorato36115782010-06-17 13:28:48 -04001313 });
1314
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001315 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001316 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001317 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1318 // wait no longer than 1sec at a time
1319 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001320 } catch (InterruptedException ex) {
1321 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001322 }
1323 }
Joe Onorato36115782010-06-17 13:28:48 -04001324 if (DEBUG_LOADERS) {
1325 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001326 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001327 + "ms for previous step to finish binding");
1328 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001329 }
Joe Onorato36115782010-06-17 13:28:48 -04001330 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001331
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001332 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001333 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001334 // Ensure that we have a valid page index to load synchronously
1335 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1336 "valid page index");
1337 }
1338 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1339 // Ensure that we don't try and bind a specified page when the pages have not been
1340 // loaded already (we should load everything asynchronously in that case)
1341 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1342 }
1343 synchronized (mLock) {
1344 if (mIsLoaderTaskRunning) {
1345 // Ensure that we are never running the background loading at this point since
1346 // we also touch the background collections
1347 throw new RuntimeException("Error! Background loading is already running");
1348 }
1349 }
1350
1351 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1352 // data structures, we can't allow any other thread to touch that data, but because
1353 // this call is synchronous, we can get away with not locking).
1354
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001355 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001356 // operations from the previous activity. We need to ensure that all queued operations
1357 // are executed before any synchronous binding work is done.
1358 mHandler.flush();
1359
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001360 // Divide the set of loaded items into those that we are binding synchronously, and
1361 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001362 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001363 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1364 // arise from that.
1365 onlyBindAllApps();
1366 }
1367
Joe Onorato36115782010-06-17 13:28:48 -04001368 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001370 if (mStopped) {
1371 return;
1372 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 mIsLoaderTaskRunning = true;
1374 }
Joe Onorato36115782010-06-17 13:28:48 -04001375 // Optimize for end-user experience: if the Launcher is up and // running with the
1376 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1377 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001378 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001379 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001380 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001381
Joe Onorato36115782010-06-17 13:28:48 -04001382 if (mStopped) {
1383 break keep_running;
1384 }
1385
Joe Onorato36115782010-06-17 13:28:48 -04001386 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001387
1388 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001389 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1390 loadAndBindAllApps();
Joe Onorato36115782010-06-17 13:28:48 -04001391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392
Joe Onorato36115782010-06-17 13:28:48 -04001393 // Clear out this reference, otherwise we end up holding it until all of the
1394 // callback runnables are done.
1395 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001396
Joe Onorato36115782010-06-17 13:28:48 -04001397 synchronized (mLock) {
1398 // If we are still the last one to be scheduled, remove ourselves.
1399 if (mLoaderTask == this) {
1400 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001401 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001402 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001403 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001404 }
Joe Onorato36115782010-06-17 13:28:48 -04001405 }
1406
1407 public void stopLocked() {
1408 synchronized (LoaderTask.this) {
1409 mStopped = true;
1410 this.notify();
1411 }
1412 }
1413
1414 /**
1415 * Gets the callbacks object. If we've been stopped, or if the launcher object
1416 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1417 * object that was around when the deferred message was scheduled, and if there's
1418 * a new Callbacks object around then also return null. This will save us from
1419 * calling onto it with data that will be ignored.
1420 */
1421 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1422 synchronized (mLock) {
1423 if (mStopped) {
1424 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001425 }
Joe Onorato36115782010-06-17 13:28:48 -04001426
1427 if (mCallbacks == null) {
1428 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001429 }
Joe Onorato36115782010-06-17 13:28:48 -04001430
1431 final Callbacks callbacks = mCallbacks.get();
1432 if (callbacks != oldCallbacks) {
1433 return null;
1434 }
1435 if (callbacks == null) {
1436 Log.w(TAG, "no mCallbacks");
1437 return null;
1438 }
1439
1440 return callbacks;
1441 }
1442 }
1443
1444 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001445 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001446 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001447 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001448 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001449
Adam Cohendcd297f2013-06-18 13:13:40 -07001450 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001451 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001452 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001453 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1454 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001455 Log.e(TAG, "Error loading shortcut into hotseat " + item
1456 + " into position (" + item.screenId + ":" + item.cellX + ","
1457 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001458 return false;
1459 }
1460
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001461 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001462 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1463
Adam Cohen2e6da152015-05-06 11:42:25 -07001464 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001465 Log.e(TAG, "Error loading shortcut " + item
1466 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001467 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001468 + ")");
1469 return false;
1470 }
1471
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001472 if (hotseatOccupancy != null) {
1473 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001474 Log.e(TAG, "Error loading shortcut into hotseat " + item
1475 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001476 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001478 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001479 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001480 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001481 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001482 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001483 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1484 occupancy.cells[(int) item.screenId][0] = true;
1485 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001486 return true;
1487 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001488 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1489 if (!workspaceScreens.contains((Long) item.screenId)) {
1490 // The item has an invalid screen id.
1491 return false;
1492 }
1493 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001494 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001495 return true;
1496 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001497
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001498 final int countX = profile.numColumns;
1499 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001500 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1501 item.cellX < 0 || item.cellY < 0 ||
1502 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1503 Log.e(TAG, "Error loading shortcut " + item
1504 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1505 + item.cellX + "," + item.cellY
1506 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1507 return false;
1508 }
1509
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001510 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001511 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1512 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001513 // Mark the first row as occupied (if the feature is enabled)
1514 // in order to account for the QSB.
1515 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001516 }
1517 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001518 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001519 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001520
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001521 // Check if any workspace icons overlap with each other
1522 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1523 occupancy.markCells(item, true);
1524 return true;
1525 } else {
1526 Log.e(TAG, "Error loading shortcut " + item
1527 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1528 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1529 + ") already occupied");
1530 return false;
1531 }
Joe Onorato36115782010-06-17 13:28:48 -04001532 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533
Winson Chungba9c37f2013-08-30 14:11:37 -07001534 /** Clears all the sBg data structures */
1535 private void clearSBgDataStructures() {
1536 synchronized (sBgLock) {
1537 sBgWorkspaceItems.clear();
1538 sBgAppWidgets.clear();
1539 sBgFolders.clear();
1540 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001541 sBgWorkspaceScreens.clear();
1542 }
1543 }
1544
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001545 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001546 if (LauncherAppState.PROFILE_STARTUP) {
1547 Trace.beginSection("Loading Workspace");
1548 }
Joe Onorato36115782010-06-17 13:28:48 -04001549 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550
Joe Onorato36115782010-06-17 13:28:48 -04001551 final Context context = mContext;
1552 final ContentResolver contentResolver = context.getContentResolver();
1553 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001554 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001555 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001556 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001557
Winson Chung892c74d2013-08-22 16:15:50 -07001558 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001559 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001560 int countX = profile.numColumns;
1561 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001562
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001563 boolean clearDb = false;
Sunny Goyalf076eae2016-01-11 12:25:10 -08001564 if (GridSizeMigrationTask.ENABLED &&
1565 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1566 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001567 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001568 }
1569
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001570 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001571 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001572 LauncherSettings.Settings.call(contentResolver,
1573 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001574 }
1575
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001576 Log.d(TAG, "loadWorkspace: loading default favorites");
1577 LauncherSettings.Settings.call(contentResolver,
1578 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001579
Winson Chung2abf94d2012-07-18 18:16:38 -07001580 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001581 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001582 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001583 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001584 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001585
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001586 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1587 final ArrayList<Long> restoredRows = new ArrayList<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001588 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001589 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001590 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001591
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001592 // +1 for the hotseat (it can be larger than the workspace)
1593 // Load workspace in reverse order to ensure that latest items are loaded first (and
1594 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001595 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001596 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001598 try {
1599 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1600 final int intentIndex = c.getColumnIndexOrThrow
1601 (LauncherSettings.Favorites.INTENT);
1602 final int titleIndex = c.getColumnIndexOrThrow
1603 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001604 final int containerIndex = c.getColumnIndexOrThrow(
1605 LauncherSettings.Favorites.CONTAINER);
1606 final int itemTypeIndex = c.getColumnIndexOrThrow(
1607 LauncherSettings.Favorites.ITEM_TYPE);
1608 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1609 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001610 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1611 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001612 final int screenIndex = c.getColumnIndexOrThrow(
1613 LauncherSettings.Favorites.SCREEN);
1614 final int cellXIndex = c.getColumnIndexOrThrow
1615 (LauncherSettings.Favorites.CELLX);
1616 final int cellYIndex = c.getColumnIndexOrThrow
1617 (LauncherSettings.Favorites.CELLY);
1618 final int spanXIndex = c.getColumnIndexOrThrow
1619 (LauncherSettings.Favorites.SPANX);
1620 final int spanYIndex = c.getColumnIndexOrThrow(
1621 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001622 final int rankIndex = c.getColumnIndexOrThrow(
1623 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001624 final int restoredIndex = c.getColumnIndexOrThrow(
1625 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001626 final int profileIdIndex = c.getColumnIndexOrThrow(
1627 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001628 final int optionsIndex = c.getColumnIndexOrThrow(
1629 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001630 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631
Sunny Goyal7f834d22015-04-21 10:10:23 -07001632 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001633 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001634 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001635 long serialNo = mUserManager.getSerialNumberForUser(user);
1636 allUsers.put(serialNo, user);
1637 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Sunny Goyal7f834d22015-04-21 10:10:23 -07001638 }
1639
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001640 ShortcutInfo info;
1641 String intentDescription;
1642 LauncherAppWidgetInfo appWidgetInfo;
1643 int container;
1644 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001645 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001646 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001647 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001648 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001649
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001650 while (!mStopped && c.moveToNext()) {
1651 try {
1652 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001653 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001654 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001655 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001656
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001657 switch (itemType) {
1658 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1659 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001660 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001661 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001662 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001663 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001664 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001665 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001666 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001667 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001668 if (user == null) {
1669 // User has been deleted remove the item.
1670 itemsToRemove.add(id);
1671 continue;
1672 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001673 try {
1674 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001675 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001676 if (cn != null && cn.getPackageName() != null) {
1677 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1678 cn.getPackageName(), user);
1679 boolean validComponent = validPkg &&
1680 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001681 if (validPkg) {
1682 targetPackage = cn.getPackageName();
1683 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001684
1685 if (validComponent) {
1686 if (restored) {
1687 // no special handling necessary for this item
1688 restoredRows.add(id);
1689 restored = false;
1690 }
Kenny Guyff05f432016-01-22 17:48:29 +00001691 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001692 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001693 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001694 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001695 intent = null;
1696 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1697 // We allow auto install apps to have their intent
1698 // updated after an install.
1699 intent = manager.getLaunchIntentForPackage(
1700 cn.getPackageName());
1701 if (intent != null) {
1702 ContentValues values = new ContentValues();
1703 values.put(LauncherSettings.Favorites.INTENT,
1704 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001705 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001706 }
1707 }
1708
1709 if (intent == null) {
1710 // The app is installed but the component is no
1711 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001712 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001713 itemsToRemove.add(id);
1714 continue;
1715 } else {
1716 // no special handling necessary for this item
1717 restoredRows.add(id);
1718 restored = false;
1719 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001720 } else if (restored) {
1721 // Package is not yet available but might be
1722 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001723 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001724
1725 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1726 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001727 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001728 // App restore has started. Update the flag
1729 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1730 ContentValues values = new ContentValues();
1731 values.put(LauncherSettings.Favorites.RESTORED,
1732 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001733 updateItem(id, values);
1734 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1735 // This is a common app. Try to replace this.
1736 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1737 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1738 if (parser.findDefaultApp()) {
1739 // Default app found. Replace it.
1740 intent = parser.parsedIntent;
1741 cn = intent.getComponent();
1742 ContentValues values = parser.parsedValues;
1743 values.put(LauncherSettings.Favorites.RESTORED, 0);
1744 updateItem(id, values);
1745 restored = false;
1746 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001747
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001748 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001749 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001750 itemsToRemove.add(id);
1751 continue;
1752 }
Sunny Goyal94485362014-09-18 16:13:58 -07001753 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001754 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001755 itemsToRemove.add(id);
1756 continue;
1757 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001758 } else if (PackageManagerHelper.isAppOnSdcard(
1759 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001760 // Package is present but not available.
1761 allowMissingTarget = true;
1762 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1763 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001764 // SdCard is not ready yet. Package might get available,
1765 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001766 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001767 HashSet<String> pkgs = sPendingPackages.get(user);
1768 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001769 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001770 sPendingPackages.put(user, pkgs);
1771 }
1772 pkgs.add(cn.getPackageName());
1773 allowMissingTarget = true;
1774 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001775
1776 } else {
1777 // Do not wait for external media load anymore.
1778 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001779 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001780 itemsToRemove.add(id);
1781 continue;
Winson Chungee055712013-07-30 14:46:24 -07001782 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001783 } else if (cn == null) {
1784 // For shortcuts with no component, keep them as they are
1785 restoredRows.add(id);
1786 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001787 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001789 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001790 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 continue;
1792 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793
Sunny Goyal34b65272015-03-11 16:56:52 -07001794 boolean useLowResIcon = container >= 0 &&
1795 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1796
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001797 if (itemReplaced) {
1798 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001799 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001800 cursorIconInfo.iconIndex, titleIndex,
1801 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001802 } else {
1803 // Don't replace items for other profiles.
1804 itemsToRemove.add(id);
1805 continue;
1806 }
1807 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001808 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001809 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001810 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001811 intent = getRestoredItemIntent(c, context, intent);
1812 } else {
1813 // Don't restore items for other profiles.
1814 itemsToRemove.add(id);
1815 continue;
1816 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001817 } else if (itemType ==
1818 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001819 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001820 cursorIconInfo.iconIndex, titleIndex,
1821 allowMissingTarget, useLowResIcon);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001822 } else {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001823 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001824
Sunny Goyald09c3702016-04-06 16:18:20 -07001825 // Shortcuts are only available on the primary profile
1826 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1827 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1828 }
1829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 // App shortcuts that used to be automatically added to Launcher
1831 // didn't always have the correct intent flags set, so do that
1832 // here
1833 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001834 intent.getCategories() != null &&
1835 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001836 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 intent.addFlags(
1838 Intent.FLAG_ACTIVITY_NEW_TASK |
1839 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1840 }
Michael Jurka96879562012-03-22 05:54:33 -07001841 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001844 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001847 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001848 info.cellX = c.getInt(cellXIndex);
1849 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001850 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001851 info.spanX = 1;
1852 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001853 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001854 if (info.promisedIntent != null) {
1855 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1856 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001857 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001858 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1859 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1860 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001861
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001863 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001864 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 break;
1866 }
1867
Sunny Goyal756adbc2015-04-16 15:20:51 -07001868 if (restored) {
1869 ComponentName cn = info.getTargetComponent();
1870 if (cn != null) {
1871 Integer progress = installingPkgs.get(cn.getPackageName());
1872 if (progress != null) {
1873 info.setInstallProgress(progress);
1874 } else {
1875 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1876 }
1877 }
1878 }
1879
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 switch (container) {
1881 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1882 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1883 sBgWorkspaceItems.add(info);
1884 break;
1885 default:
1886 // Item is in a user folder
1887 FolderInfo folderInfo =
1888 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07001889 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 break;
1891 }
1892 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07001893 } else {
1894 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895 }
1896 break;
1897
1898 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1899 id = c.getLong(idIndex);
1900 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1901
Sunny Goyala508e4f2015-05-21 09:33:57 -07001902 // Do not trim the folder label, as is was set by the user.
1903 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001906 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 folderInfo.cellX = c.getInt(cellXIndex);
1908 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001909 folderInfo.spanX = 1;
1910 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001911 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912
Daniel Sandler8802e962010-05-26 16:28:16 -04001913 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001914 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001915 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001916 break;
1917 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001918
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001920 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1921 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1922 sBgWorkspaceItems.add(folderInfo);
1923 break;
Joe Onorato36115782010-06-17 13:28:48 -04001924 }
Joe Onorato17a89222011-02-08 17:26:11 -08001925
Chris Wrenf4d08112014-01-16 18:13:56 -05001926 if (restored) {
1927 // no special handling required for restored folders
1928 restoredRows.add(id);
1929 }
1930
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1932 sBgFolders.put(folderInfo.id, folderInfo);
1933 break;
1934
1935 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001936 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001938 boolean customWidget = itemType ==
1939 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1940
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001941 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001942 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001943 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001944 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001945 user = allUsers.get(serialNumber);
1946 if (user == null) {
1947 itemsToRemove.add(id);
1948 continue;
1949 }
1950
Sunny Goyalff572272014-07-23 13:58:07 -07001951 final ComponentName component =
1952 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001953
Sunny Goyal651077b2014-06-30 14:15:31 -07001954 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001955 final boolean isIdValid = (restoreStatus &
1956 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001957 final boolean wasProviderReady = (restoreStatus &
1958 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001959
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001960 if (widgetProvidersMap == null) {
1961 widgetProvidersMap = AppWidgetManagerCompat
1962 .getInstance(mContext).getAllProvidersMap();
1963 }
1964 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1965 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001966 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001967 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001968
1969 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001970 if (!isSafeMode && !customWidget &&
1971 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001972 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001973 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 itemsToRemove.add(id);
1975 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001976 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001977 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1978 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001979
Sunny Goyal53f96722015-07-13 19:54:53 -07001980 // The provider is available. So the widget is either
1981 // available or not available. We do not need to track
1982 // any future restore updates.
1983 int status = restoreStatus &
1984 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001985 if (!wasProviderReady) {
1986 // If provider was not previously ready, update the
1987 // status and UI flag.
1988
1989 // Id would be valid only if the widget restore broadcast was received.
1990 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07001991 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001992 } else {
1993 status &= ~LauncherAppWidgetInfo
1994 .FLAG_PROVIDER_NOT_READY;
1995 }
1996 }
1997 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001998 } else {
1999 Log.v(TAG, "Widget restore pending id=" + id
2000 + " appWidgetId=" + appWidgetId
2001 + " status =" + restoreStatus);
2002 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002003 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002004 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002005 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002006
2007 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2008 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002009 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002010 // App restore has started. Update the flag
2011 appWidgetInfo.restoreStatus |=
2012 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002013 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002014 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002015 itemsToRemove.add(id);
2016 continue;
2017 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002018
2019 appWidgetInfo.installProgress =
2020 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002021 }
Sunny Goyalff572272014-07-23 13:58:07 -07002022
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002024 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 appWidgetInfo.cellX = c.getInt(cellXIndex);
2026 appWidgetInfo.cellY = c.getInt(cellYIndex);
2027 appWidgetInfo.spanX = c.getInt(spanXIndex);
2028 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002029 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002030
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2032 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2033 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002034 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2035 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 continue;
2037 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002039 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002041 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002042 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 break;
2044 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002045
Adam Cohen59400422014-03-05 18:07:04 -08002046 if (!customWidget) {
2047 String providerName =
2048 appWidgetInfo.providerName.flattenToString();
2049 if (!providerName.equals(savedProvider) ||
2050 (appWidgetInfo.restoreStatus != restoreStatus)) {
2051 ContentValues values = new ContentValues();
2052 values.put(
2053 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2054 providerName);
2055 values.put(LauncherSettings.Favorites.RESTORED,
2056 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002057 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002058 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002059 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2061 sBgAppWidgets.add(appWidgetInfo);
2062 }
Joe Onorato36115782010-06-17 13:28:48 -04002063 break;
2064 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002066 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002067 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 }
2069 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002070 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 }
2072
Winson Chungba9c37f2013-08-30 14:11:37 -07002073 // Break early if we've stopped loading
2074 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002075 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002076 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002077 }
2078
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002081 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2082 Utilities.createDbSelectionQuery(
2083 LauncherSettings.Favorites._ID, itemsToRemove), null);
2084 if (DEBUG_LOADERS) {
2085 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2086 LauncherSettings.Favorites._ID, itemsToRemove));
2087 }
2088
2089 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002090 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2091 .call(contentResolver,
2092 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2093 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2094 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002095 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2096 sBgFolders.remove(folderId);
2097 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002098 }
2099 }
2100
Sunny Goyal317698b2015-07-29 11:45:41 -07002101 // Sort all the folder items and make sure the first 3 items are high resolution.
2102 for (FolderInfo folder : sBgFolders) {
2103 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2104 int pos = 0;
2105 for (ShortcutInfo info : folder.contents) {
2106 if (info.usingLowResIcon) {
2107 info.updateIcon(mIconCache, false);
2108 }
2109 pos ++;
2110 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2111 break;
2112 }
2113 }
2114 }
2115
Chris Wrenf4d08112014-01-16 18:13:56 -05002116 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002117 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002118 ContentValues values = new ContentValues();
2119 values.put(LauncherSettings.Favorites.RESTORED, 0);
2120 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2121 Utilities.createDbSelectionQuery(
2122 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002123 }
2124
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002125 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2126 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002127 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002128 null, sWorker);
2129 }
2130
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002131 // Remove any empty screens
2132 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002133 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002134 long screenId = item.screenId;
2135 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2136 unusedScreens.contains(screenId)) {
2137 unusedScreens.remove(screenId);
2138 }
2139 }
2140
2141 // If there are any empty screens remove them, and update.
2142 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002143 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002145 }
2146
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147 if (DEBUG_LOADERS) {
2148 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2149 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002150 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002151 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002153
Sunny Goyale2df0622015-04-24 11:27:00 -07002154 for (int i = 0; i < nScreens; i++) {
2155 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002156 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 line += " | ";
2158 }
Joe Onorato36115782010-06-17 13:28:48 -04002159 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002161 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002162 }
Joe Onorato36115782010-06-17 13:28:48 -04002163 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002164 if (LauncherAppState.PROFILE_STARTUP) {
2165 Trace.endSection();
2166 }
Adam Cohene25af792013-06-06 23:08:25 -07002167 }
2168
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002169 /**
2170 * Partially updates the item without any notification. Must be called on the worker thread.
2171 */
2172 private void updateItem(long itemId, ContentValues update) {
2173 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002174 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002175 update,
2176 BaseColumns._ID + "= ?",
2177 new String[]{Long.toString(itemId)});
2178 }
2179
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 /** Filters the set of items who are directly or indirectly (via another container) on the
2181 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002182 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 ArrayList<ItemInfo> allWorkspaceItems,
2184 ArrayList<ItemInfo> currentScreenItems,
2185 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002186 // Purge any null ItemInfos
2187 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2188 while (iter.hasNext()) {
2189 ItemInfo i = iter.next();
2190 if (i == null) {
2191 iter.remove();
2192 }
2193 }
2194
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 // Order the set of items by their containers first, this allows use to walk through the
2196 // list sequentially, build up a list of containers that are in the specified screen,
2197 // as well as all items in those containers.
2198 Set<Long> itemsOnScreen = new HashSet<Long>();
2199 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2200 @Override
2201 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002202 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 }
2204 });
2205 for (ItemInfo info : allWorkspaceItems) {
2206 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002207 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 currentScreenItems.add(info);
2209 itemsOnScreen.add(info.id);
2210 } else {
2211 otherScreenItems.add(info);
2212 }
2213 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2214 currentScreenItems.add(info);
2215 itemsOnScreen.add(info.id);
2216 } else {
2217 if (itemsOnScreen.contains(info.container)) {
2218 currentScreenItems.add(info);
2219 itemsOnScreen.add(info.id);
2220 } else {
2221 otherScreenItems.add(info);
2222 }
2223 }
2224 }
2225 }
2226
2227 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002228 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 ArrayList<LauncherAppWidgetInfo> appWidgets,
2230 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2231 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232
2233 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002234 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002236 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 currentScreenWidgets.add(widget);
2238 } else {
2239 otherScreenWidgets.add(widget);
2240 }
2241 }
2242 }
2243
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2245 * right) */
2246 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002247 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002248 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002249 final int screenCols = profile.numColumns;
2250 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002252 @Override
2253 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002254 if (lhs.container == rhs.container) {
2255 // Within containers, order by their spatial position in that container
2256 switch ((int) lhs.container) {
2257 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2258 long lr = (lhs.screenId * screenCellCount +
2259 lhs.cellY * screenCols + lhs.cellX);
2260 long rr = (rhs.screenId * screenCellCount +
2261 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002262 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002263 }
2264 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2265 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002266 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002267 }
2268 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002269 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002270 throw new RuntimeException("Unexpected container type when " +
2271 "sorting workspace items.");
2272 }
2273 return 0;
2274 }
2275 } else {
2276 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002277 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002278 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002279 }
2280 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002282
Adam Cohendcd297f2013-06-18 13:13:40 -07002283 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2284 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002285 final Runnable r = new Runnable() {
2286 @Override
2287 public void run() {
2288 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2289 if (callbacks != null) {
2290 callbacks.bindScreens(orderedScreens);
2291 }
2292 }
2293 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002294 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 }
2296
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2298 final ArrayList<ItemInfo> workspaceItems,
2299 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002300 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301
2302 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002303 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002305 final int start = i;
2306 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002307 final Runnable r = new Runnable() {
2308 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002309 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002310 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002312 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2313 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002314 }
2315 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002317 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002318 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002319
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 // Bind the widgets, one at a time
2321 N = appWidgets.size();
2322 for (int i = 0; i < N; i++) {
2323 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2324 final Runnable r = new Runnable() {
2325 public void run() {
2326 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2327 if (callbacks != null) {
2328 callbacks.bindAppWidget(widget);
2329 }
2330 }
2331 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002332 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 }
2334 }
2335
2336 /**
2337 * Binds all loaded data to actual views on the main thread.
2338 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002339 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 final long t = SystemClock.uptimeMillis();
2341 Runnable r;
2342
2343 // Don't use these two variables in any of the callback runnables.
2344 // Otherwise we hold a reference to them.
2345 final Callbacks oldCallbacks = mCallbacks.get();
2346 if (oldCallbacks == null) {
2347 // This launcher has exited and nobody bothered to tell us. Just bail.
2348 Log.w(TAG, "LoaderTask running with no launcher");
2349 return;
2350 }
2351
Winson Chung9b9fb962013-11-15 15:39:34 -08002352 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002353 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2354 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2355 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002356
Winson Chung2abf94d2012-07-18 18:16:38 -07002357 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 workspaceItems.addAll(sBgWorkspaceItems);
2359 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002360 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361 }
2362
Derek Prothro7aff3992013-12-10 14:00:37 -05002363 final boolean isLoadingSynchronously =
2364 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002365 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002366 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002367 if (currScreen >= orderedScreenIds.size()) {
2368 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002369 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002370 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002371 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002372 final long currentScreenId = currentScreen < 0
2373 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002374
Winson Chung9b9fb962013-11-15 15:39:34 -08002375 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002376 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2377 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2378 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2379 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002380
Winson Chung9b9fb962013-11-15 15:39:34 -08002381 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002383 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2386 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2387
2388 // Tell the workspace that we're about to start binding items
2389 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002390 public void run() {
2391 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2392 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002393 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002394 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002395 }
2396 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002397 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002398 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399
Adam Cohendcd297f2013-06-18 13:13:40 -07002400 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2401
Sunny Goyal527c7d32015-08-28 15:19:36 -07002402 Executor mainExecutor = new DeferredMainThreadExecutor();
2403 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002404 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405
Sunny Goyal527c7d32015-08-28 15:19:36 -07002406 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2407 // remaining screens after first onDraw is called. This ensures that the first screen
2408 // is immediately visible (eg. during rotation)
2409 // In case of !isLoadingSynchronously, bind all pages one after other.
2410 final Executor deferredExecutor = isLoadingSynchronously ?
2411 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2412
Sunny Goyal44c06432016-04-02 10:56:02 -07002413 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002414
2415 // Tell the workspace that we're done binding items
2416 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002417 public void run() {
2418 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2419 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002420 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002421 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422
Sunny Goyal639e9062015-08-19 19:17:06 -07002423 mIsLoadingAndBindingWorkspace = false;
2424
2425 // Run all the bind complete runnables after workspace is bound.
2426 if (!mBindCompleteRunnables.isEmpty()) {
2427 synchronized (mBindCompleteRunnables) {
2428 for (final Runnable r : mBindCompleteRunnables) {
2429 runOnWorkerThread(r);
2430 }
2431 mBindCompleteRunnables.clear();
2432 }
2433 }
2434
Winson Chung98e030b2012-05-07 16:01:11 -07002435 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002436 if (DEBUG_LOADERS) {
2437 Log.d(TAG, "bound workspace in "
2438 + (SystemClock.uptimeMillis()-t) + "ms");
2439 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002440
Joe Onorato36115782010-06-17 13:28:48 -04002441 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002443 deferredExecutor.execute(r);
2444
Winson Chung4a2afa32012-07-19 14:53:05 -07002445 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002446 r = new Runnable() {
2447 public void run() {
2448 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2449 if (callbacks != null) {
2450 // We are loading synchronously, which means, some of the pages will be
2451 // bound after first draw. Inform the callbacks that page binding is
2452 // not complete, and schedule the remaining pages.
2453 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2454 callbacks.onPageBoundSynchronously(currentScreen);
2455 }
2456 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2457 }
2458 }
2459 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002460 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002461 }
Joe Onorato36115782010-06-17 13:28:48 -04002462 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002463
Joe Onorato36115782010-06-17 13:28:48 -04002464 private void loadAndBindAllApps() {
2465 if (DEBUG_LOADERS) {
2466 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2467 }
2468 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002469 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002470 synchronized (LoaderTask.this) {
2471 if (mStopped) {
2472 return;
2473 }
2474 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002475 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002476 synchronized (LoaderTask.this) {
2477 if (mStopped) {
2478 return;
2479 }
2480 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002481 }
Joe Onorato36115782010-06-17 13:28:48 -04002482 } else {
2483 onlyBindAllApps();
2484 }
2485 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002486
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002487 private void updateIconCache() {
2488 // Ignore packages which have a promise icon.
2489 HashSet<String> packagesToIgnore = new HashSet<>();
2490 synchronized (sBgLock) {
2491 for (ItemInfo info : sBgItemsIdMap) {
2492 if (info instanceof ShortcutInfo) {
2493 ShortcutInfo si = (ShortcutInfo) info;
2494 if (si.isPromise() && si.getTargetComponent() != null) {
2495 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2496 }
2497 } else if (info instanceof LauncherAppWidgetInfo) {
2498 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2499 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2500 packagesToIgnore.add(lawi.providerName.getPackageName());
2501 }
2502 }
2503 }
2504 }
2505 mIconCache.updateDbIcons(packagesToIgnore);
2506 }
2507
Joe Onorato36115782010-06-17 13:28:48 -04002508 private void onlyBindAllApps() {
2509 final Callbacks oldCallbacks = mCallbacks.get();
2510 if (oldCallbacks == null) {
2511 // This launcher has exited and nobody bothered to tell us. Just bail.
2512 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2513 return;
2514 }
2515
2516 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002517 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002518 final ArrayList<AppInfo> list
2519 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002520 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002521 public void run() {
2522 final long t = SystemClock.uptimeMillis();
2523 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2524 if (callbacks != null) {
2525 callbacks.bindAllApplications(list);
2526 }
2527 if (DEBUG_LOADERS) {
2528 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002529 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002530 }
2531 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002532 };
2533 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002534 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002535 r.run();
2536 } else {
2537 mHandler.post(r);
2538 }
Joe Onorato36115782010-06-17 13:28:48 -04002539 }
2540
Winson Chung64359a52013-07-08 17:17:08 -07002541 private void loadAllApps() {
2542 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002543
Joe Onorato36115782010-06-17 13:28:48 -04002544 final Callbacks oldCallbacks = mCallbacks.get();
2545 if (oldCallbacks == null) {
2546 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002547 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002548 return;
2549 }
2550
Kenny Guyed131872014-04-30 03:02:21 +01002551 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2552
Winson Chung64359a52013-07-08 17:17:08 -07002553 // Clear the list of apps
2554 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002555 for (UserHandleCompat user : profiles) {
2556 // Query for the set of apps
2557 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002558 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002559 if (DEBUG_LOADERS) {
2560 Log.d(TAG, "getActivityList took "
2561 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2562 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2563 }
2564 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002565 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002566 if (apps == null || apps.isEmpty()) {
2567 return;
2568 }
Kenny Guyff05f432016-01-22 17:48:29 +00002569 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002570 // Create the ApplicationInfos
2571 for (int i = 0; i < apps.size(); i++) {
2572 LauncherActivityInfoCompat app = apps.get(i);
2573 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002574 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002575 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002576
Sunny Goyal756a28a2015-04-23 17:07:55 -07002577 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2578 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002579 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002580
2581 @Override
2582 public void run() {
2583 heuristic.processUserApps(apps);
2584 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002585 };
2586 runOnMainThread(new Runnable() {
2587
2588 @Override
2589 public void run() {
2590 // Check isLoadingWorkspace on the UI thread, as it is updated on
2591 // the UI thread.
2592 if (mIsLoadingAndBindingWorkspace) {
2593 synchronized (mBindCompleteRunnables) {
2594 mBindCompleteRunnables.add(r);
2595 }
2596 } else {
2597 runOnWorkerThread(r);
2598 }
2599 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002600 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002601 }
Winson Chung64359a52013-07-08 17:17:08 -07002602 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002603 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002604 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2605 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002606
2607 // Post callback on main thread
2608 mHandler.post(new Runnable() {
2609 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002610
Winson Chung64359a52013-07-08 17:17:08 -07002611 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002612 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002613 if (callbacks != null) {
2614 callbacks.bindAllApplications(added);
2615 if (DEBUG_LOADERS) {
2616 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002617 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002618 }
2619 } else {
2620 Log.i(TAG, "not binding apps: no Launcher activity");
2621 }
2622 }
2623 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002624 // Cleanup any data stored for a deleted user.
2625 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002626 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002627 Log.d(TAG, "Icons processed in "
2628 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002629 }
2630 }
2631
2632 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002633 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002634 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2636 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2637 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2638 }
Joe Onorato36115782010-06-17 13:28:48 -04002639 }
2640 }
2641
Sunny Goyal75b0f552015-05-20 21:57:06 -07002642 /**
2643 * Called when the icons for packages have been updated in the icon cache.
2644 */
2645 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2646 final Callbacks callbacks = getCallback();
2647 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2648 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2649
2650 // If any package icon has changed (app was updated while launcher was dead),
2651 // update the corresponding shortcuts.
2652 synchronized (sBgLock) {
2653 for (ItemInfo info : sBgItemsIdMap) {
2654 if (info instanceof ShortcutInfo && user.equals(info.user)
2655 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2656 ShortcutInfo si = (ShortcutInfo) info;
2657 ComponentName cn = si.getTargetComponent();
2658 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2659 si.updateIcon(mIconCache);
2660 updatedShortcuts.add(si);
2661 }
2662 }
2663 }
2664 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2665 }
2666
2667 if (!updatedShortcuts.isEmpty()) {
2668 final UserHandleCompat userFinal = user;
2669 mHandler.post(new Runnable() {
2670
2671 public void run() {
2672 Callbacks cb = getCallback();
2673 if (cb != null && callbacks == cb) {
2674 cb.bindShortcutsChanged(updatedShortcuts,
2675 new ArrayList<ShortcutInfo>(), userFinal);
2676 }
2677 }
2678 });
2679 }
2680
2681 if (!updatedApps.isEmpty()) {
2682 mHandler.post(new Runnable() {
2683
2684 public void run() {
2685 Callbacks cb = getCallback();
2686 if (cb != null && callbacks == cb) {
2687 cb.bindAppsUpdated(updatedApps);
2688 }
2689 }
2690 });
2691 }
2692 }
2693
Joe Onorato36115782010-06-17 13:28:48 -04002694 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002695 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002696 }
2697
Adam Cohen091440a2015-03-18 14:16:05 -07002698 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002699
2700 @Override
2701 public void onReceive(Context context, Intent intent) {
2702 synchronized (sBgLock) {
2703 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2704 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002705 final PackageManager manager = context.getPackageManager();
2706 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2707 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002708 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2709 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002710 packagesRemoved.clear();
2711 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002712 for (String pkg : entry.getValue()) {
2713 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002714 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002715 packagesUnavailable.add(pkg);
2716 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002717 packagesRemoved.add(pkg);
2718 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002719 }
2720 }
2721 if (!packagesRemoved.isEmpty()) {
2722 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2723 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2724 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002725 if (!packagesUnavailable.isEmpty()) {
2726 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2727 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2728 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002729 }
Sunny Goyal34942622014-08-29 17:20:55 -07002730 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002731 }
2732 }
2733 }
2734
Joe Onorato36115782010-06-17 13:28:48 -04002735 private class PackageUpdatedTask implements Runnable {
2736 int mOp;
2737 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002738 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002739
2740 public static final int OP_NONE = 0;
2741 public static final int OP_ADD = 1;
2742 public static final int OP_UPDATE = 2;
2743 public static final int OP_REMOVE = 3; // uninstlled
2744 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002745 public static final int OP_SUSPEND = 5; // package suspended
2746 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002747 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002748
Kenny Guyed131872014-04-30 03:02:21 +01002749 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002750 mOp = op;
2751 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002752 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002753 }
2754
2755 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002756 if (!mHasLoaderCompletedOnce) {
2757 // Loader has not yet run.
2758 return;
2759 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002760 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002761
2762 final String[] packages = mPackages;
2763 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002764 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002765 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002766 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002767 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002768 for (int i=0; i<N; i++) {
2769 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002770 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002771 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002772 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002773
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002774 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2775 if (heuristic != null) {
2776 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002777 }
Joe Onorato36115782010-06-17 13:28:48 -04002778 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002779 }
Joe Onorato36115782010-06-17 13:28:48 -04002780 case OP_UPDATE:
2781 for (int i=0; i<N; i++) {
2782 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002783 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002784 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002785 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002786 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002787 // Since package was just updated, the target must be available now.
2788 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002789 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002790 case OP_REMOVE: {
2791 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2792 if (heuristic != null) {
2793 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002794 }
Joe Onorato36115782010-06-17 13:28:48 -04002795 for (int i=0; i<N; i++) {
2796 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002797 mIconCache.removeIconsForPkg(packages[i], mUser);
2798 }
2799 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002800 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002801 case OP_UNAVAILABLE:
2802 for (int i=0; i<N; i++) {
2803 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2804 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002805 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002806 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002807 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002808 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002809 case OP_SUSPEND:
2810 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002811 flagOp = mOp == OP_SUSPEND ?
2812 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
2813 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07002814 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07002815 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
2816 break;
2817 case OP_USER_AVAILABILITY_CHANGE:
2818 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
2819 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
2820 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
2821 // We want to update all packages for this user.
2822 pkgFilter = StringFilter.matchesAll();
2823 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00002824 break;
Joe Onorato36115782010-06-17 13:28:48 -04002825 }
2826
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002827 ArrayList<AppInfo> added = null;
2828 ArrayList<AppInfo> modified = null;
2829 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002830
Adam Cohen487f7dd2012-06-28 18:12:10 -07002831 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002832 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002833 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002834 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002835 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002836 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002837 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002838 }
Winson Chung5d55f332012-07-16 20:45:03 -07002839 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002840 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002841 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002842 }
2843
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002844 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002845
Joe Onorato36115782010-06-17 13:28:48 -04002846 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08002847 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002848 for (AppInfo ai : added) {
2849 addedOrUpdatedApps.put(ai.componentName, ai);
2850 }
Joe Onorato36115782010-06-17 13:28:48 -04002851 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002852
Joe Onorato36115782010-06-17 13:28:48 -04002853 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002854 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002855 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002856 for (AppInfo ai : modified) {
2857 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07002858 }
2859
Joe Onorato36115782010-06-17 13:28:48 -04002860 mHandler.post(new Runnable() {
2861 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002862 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07002863 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002864 callbacks.bindAppsUpdated(modifiedFinal);
2865 }
2866 }
2867 });
2868 }
Winson Chung83892cc2013-05-01 16:53:33 -07002869
Sunny Goyal4390ace2014-10-13 11:33:11 -07002870 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002871 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002872 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
2873 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
2874 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
2875
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002876 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07002877 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002878 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
2879 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002880 boolean infoUpdated = false;
2881 boolean shortcutUpdated = false;
2882
2883 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002884 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07002885 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002886 Bitmap icon = Utilities.createIconBitmap(
2887 si.iconResource.packageName,
2888 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002889 if (icon != null) {
2890 si.setIcon(icon);
2891 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002892 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002893 }
2894 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002895
2896 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07002897 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002898 AppInfo appInfo = addedOrUpdatedApps.get(cn);
2899
2900 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002901 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
2902 // Auto install icon
2903 PackageManager pm = context.getPackageManager();
2904 ResolveInfo matched = pm.resolveActivity(
2905 new Intent(Intent.ACTION_MAIN)
2906 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
2907 PackageManager.MATCH_DEFAULT_ONLY);
2908 if (matched == null) {
2909 // Try to find the best match activity.
2910 Intent intent = pm.getLaunchIntentForPackage(
2911 cn.getPackageName());
2912 if (intent != null) {
2913 cn = intent.getComponent();
2914 appInfo = addedOrUpdatedApps.get(cn);
2915 }
2916
2917 if ((intent == null) || (appInfo == null)) {
2918 removedShortcuts.add(si);
2919 continue;
2920 }
2921 si.promisedIntent = intent;
2922 }
2923 }
2924
2925 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07002926 if (appInfo != null) {
2927 si.flags = appInfo.flags;
2928 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07002929
Sunny Goyal756adbc2015-04-16 15:20:51 -07002930 si.intent = si.promisedIntent;
2931 si.promisedIntent = null;
2932 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07002933 infoUpdated = true;
2934 si.updateIcon(mIconCache);
2935 }
2936
2937 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
2938 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2939 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07002940 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07002941 si.contentDescription = appInfo.contentDescription;
2942 infoUpdated = true;
2943 }
2944
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002945 int oldDisabledFlags = si.isDisabled;
2946 si.isDisabled = flagOp.apply(si.isDisabled);
2947 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002948 shortcutUpdated = true;
2949 }
2950 }
2951
2952 if (infoUpdated || shortcutUpdated) {
2953 updatedShortcuts.add(si);
2954 }
2955 if (infoUpdated) {
2956 updateItemInDatabase(context, si);
2957 }
Sunny Goyalda891c12016-03-18 18:29:24 -07002958 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07002959 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
2960 if (mUser.equals(widgetInfo.user)
2961 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07002962 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07002963 widgetInfo.restoreStatus &=
2964 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
2965 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002966
2967 // adding this flag ensures that launcher shows 'click to setup'
2968 // if the widget has a config activity. In case there is no config
2969 // activity, it will be marked as 'restored' during bind.
2970 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
2971
Sunny Goyal4390ace2014-10-13 11:33:11 -07002972 widgets.add(widgetInfo);
2973 updateItemInDatabase(context, widgetInfo);
2974 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002975 }
2976 }
2977 }
2978
Sunny Goyal4390ace2014-10-13 11:33:11 -07002979 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002980 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002981 mHandler.post(new Runnable() {
2982
2983 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002984 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07002985 if (callbacks == cb && cb != null) {
2986 callbacks.bindShortcutsChanged(
2987 updatedShortcuts, removedShortcuts, mUser);
2988 }
2989 }
2990 });
2991 if (!removedShortcuts.isEmpty()) {
2992 deleteItemsFromDatabase(context, removedShortcuts);
2993 }
2994 }
2995 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002996 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002997 mHandler.post(new Runnable() {
2998 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002999 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003000 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003001 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003002 }
3003 }
3004 });
3005 }
3006 }
3007
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003008 final HashSet<String> removedPackages = new HashSet<>();
3009 final HashSet<ComponentName> removedComponents = new HashSet<>();
3010 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003011 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003012 Collections.addAll(removedPackages, packages);
3013
3014 // No need to update the removedComponents as
3015 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003016 } else if (mOp == OP_UPDATE) {
3017 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003018 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003019 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003020 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003021 }
3022 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003023
3024 // Update removedComponents as some components can get removed during package update
3025 for (AppInfo info : removedApps) {
3026 removedComponents.add(info.componentName);
3027 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003028 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003029
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003030 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3031 for (String pn : removedPackages) {
3032 deletePackageFromDatabase(context, pn, mUser);
3033 }
3034 for (ComponentName cn : removedComponents) {
3035 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003036 }
3037
Winson Chungdf95eb12013-10-16 14:57:07 -07003038 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003039 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3040
Winson Chungdf95eb12013-10-16 14:57:07 -07003041 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003042 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003043 mHandler.post(new Runnable() {
3044 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003045 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003046 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003047 callbacks.bindWorkspaceComponentsRemoved(
3048 removedPackages, removedComponents, mUser);
3049 }
3050 }
3051 });
3052 }
3053
3054 if (!removedApps.isEmpty()) {
3055 // Remove corresponding apps from All-Apps
3056 final Callbacks callbacks = getCallback();
3057 mHandler.post(new Runnable() {
3058 public void run() {
3059 Callbacks cb = getCallback();
3060 if (callbacks == cb && cb != null) {
3061 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003062 }
3063 }
3064 });
Joe Onoratobe386092009-11-17 17:32:16 -08003065 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003066
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003067 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3068 // get widget update signals.
3069 if (!Utilities.ATLEAST_MARSHMALLOW &&
3070 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003071 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003072 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003073 public void run() {
3074 Callbacks cb = getCallback();
3075 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003076 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003077 }
3078 }
3079 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003080 }
3081 }
3082 }
3083
3084 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3085 mHandler.post(new Runnable() {
3086 @Override
3087 public void run() {
3088 Callbacks cb = getCallback();
3089 if (callbacks == cb && cb != null) {
3090 callbacks.bindWidgetsModel(model);
3091 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003092 }
3093 });
3094 }
3095
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003096 public void refreshAndBindWidgetsAndShortcuts(
3097 final Callbacks callbacks, final boolean bindFirst) {
3098 runOnWorkerThread(new Runnable() {
3099 @Override
3100 public void run() {
3101 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3102 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003103 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003104 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3105 bindWidgetsModel(callbacks, model);
3106 // update the Widget entries inside DB on the worker thread.
3107 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3108 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003109 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003110 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003111 }
3112
Adam Cohen091440a2015-03-18 14:16:05 -07003113 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003114 UserHandleCompat user) {
3115 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3116 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003117 }
Adam Cohen556f6132014-01-15 15:18:08 -08003118
Kenny Guyed131872014-04-30 03:02:21 +01003119 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3120 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003121 if (cn == null) {
3122 return false;
3123 }
Kenny Guyed131872014-04-30 03:02:21 +01003124 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3125 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003126 return false;
3127 }
Kenny Guyed131872014-04-30 03:02:21 +01003128 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003129 }
3130
Adam Cohena28b78e2014-05-20 17:03:04 -07003131 public static boolean isValidPackage(Context context, String packageName,
3132 UserHandleCompat user) {
3133 if (packageName == null) {
3134 return false;
3135 }
3136 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3137 return launcherApps.isPackageEnabledForProfile(packageName, user);
3138 }
3139
Joe Onorato9c1289c2009-08-17 11:03:03 -04003140 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003141 * Make an ShortcutInfo object for a restored application or shortcut item that points
3142 * to a package that is not yet installed on the system.
3143 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003144 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003145 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003146 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003147 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003148
3149 Bitmap icon = iconInfo.loadIcon(c, info, context);
3150 // the fallback icon
3151 if (icon == null) {
3152 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3153 } else {
3154 info.setIcon(icon);
3155 }
Sunny Goyal34942622014-08-29 17:20:55 -07003156
3157 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003158 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003159 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003160 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003161 }
Sunny Goyal34942622014-08-29 17:20:55 -07003162 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3163 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003164 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003165 }
Sunny Goyal34942622014-08-29 17:20:55 -07003166 } else {
3167 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3168 }
3169
Winson Chung82b016c2015-05-08 17:00:10 -07003170 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003171 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003172 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003173 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003174 return info;
3175 }
3176
3177 /**
3178 * Make an Intent object for a restored application or shortcut item that points
3179 * to the market page for the item.
3180 */
Adam Cohen091440a2015-03-18 14:16:05 -07003181 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003182 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003183 return getMarketIntent(componentName.getPackageName());
3184 }
3185
3186 static Intent getMarketIntent(String packageName) {
3187 return new Intent(Intent.ACTION_VIEW)
3188 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003189 .scheme("market")
3190 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003191 .appendQueryParameter("id", packageName)
3192 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003193 }
3194
3195 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003196 * Make an ShortcutInfo object for a shortcut that is an application.
3197 *
3198 * If c is not null, then it will be used to fill in missing data like the title and icon.
3199 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003200 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003201 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003202 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003203 if (user == null) {
3204 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003205 return null;
3206 }
3207
Kenny Guyed131872014-04-30 03:02:21 +01003208 ComponentName componentName = intent.getComponent();
3209 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003210 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003211 return null;
3212 }
3213
3214 Intent newIntent = new Intent(intent.getAction(), null);
3215 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3216 newIntent.setComponent(componentName);
3217 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003218 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003219 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3220 return null;
3221 }
3222
3223 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003224 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003225 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3226 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3227 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003228 }
3229
Sunny Goyald09c3702016-04-06 16:18:20 -07003230 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3231 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3232 }
3233
Joe Onorato56d82912010-03-07 14:32:10 -05003234 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003235 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003236 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003237 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003238
Joe Onorato56d82912010-03-07 14:32:10 -05003239 // fall back to the class name of the activity
3240 if (info.title == null) {
3241 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003242 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003243
Joe Onorato9c1289c2009-08-17 11:03:03 -04003244 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003245 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003246 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003247 if (lai != null) {
3248 info.flags = AppInfo.initFlags(lai);
3249 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003250 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003252
Sunny Goyale2df0622015-04-24 11:27:00 -07003253 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003254 ItemInfoFilter f) {
3255 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3256 for (ItemInfo i : infos) {
3257 if (i instanceof ShortcutInfo) {
3258 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003259 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003260 if (cn != null && f.filterItem(null, info, cn)) {
3261 filtered.add(info);
3262 }
3263 } else if (i instanceof FolderInfo) {
3264 FolderInfo info = (FolderInfo) i;
3265 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003266 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003267 if (cn != null && f.filterItem(info, s, cn)) {
3268 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003269 }
3270 }
Winson Chung64359a52013-07-08 17:17:08 -07003271 } else if (i instanceof LauncherAppWidgetInfo) {
3272 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3273 ComponentName cn = info.providerName;
3274 if (cn != null && f.filterItem(null, info, cn)) {
3275 filtered.add(info);
3276 }
Winson Chung8a435102012-08-30 17:16:53 -07003277 }
3278 }
Winson Chung64359a52013-07-08 17:17:08 -07003279 return new ArrayList<ItemInfo>(filtered);
3280 }
3281
Adam Cohen091440a2015-03-18 14:16:05 -07003282 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003283 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003284 ItemInfoFilter filter = new ItemInfoFilter() {
3285 @Override
3286 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003287 if (info.user == null) {
3288 return cn.equals(cname);
3289 } else {
3290 return cn.equals(cname) && info.user.equals(user);
3291 }
Winson Chung64359a52013-07-08 17:17:08 -07003292 }
3293 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003294 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003295 }
3296
Sunny Goyal1a745e82014-10-02 15:58:31 -07003297 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003298 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003299 */
Adam Cohen091440a2015-03-18 14:16:05 -07003300 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003301 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003302 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003303 // Non-app shortcuts are only supported for current user.
3304 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003305 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003306
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003307 // TODO: If there's an explicit component and we can't install that, delete it.
3308
Winson Chung82b016c2015-05-08 17:00:10 -07003309 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003310
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003311 Bitmap icon = iconInfo.loadIcon(c, info, context);
3312 // the fallback icon
3313 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003314 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003315 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003316 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003317 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003318 return info;
3319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003320
Sunny Goyal2350bc92014-10-14 16:42:54 -07003321 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003322 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3323 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3324 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3325
Adam Cohend9198822011-11-22 16:42:47 -08003326 if (intent == null) {
3327 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3328 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3329 return null;
3330 }
3331
Joe Onorato0589f0f2010-02-08 13:44:00 -08003332 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003333 boolean customIcon = false;
3334 ShortcutIconResource iconResource = null;
3335
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003336 if (bitmap instanceof Bitmap) {
3337 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003338 customIcon = true;
3339 } else {
3340 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003341 if (extra instanceof ShortcutIconResource) {
3342 iconResource = (ShortcutIconResource) extra;
3343 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003344 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003345 }
3346 }
3347
Michael Jurkac9d95c52011-08-29 14:03:34 -07003348 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003349
Kenny Guyed131872014-04-30 03:02:21 +01003350 // Only support intents for current user for now. Intents sent from other
3351 // users wouldn't get here without intent forwarding anyway.
3352 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003353 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003354 icon = mIconCache.getDefaultIcon(info.user);
3355 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003356 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003357 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003358
Winson Chung82b016c2015-05-08 17:00:10 -07003359 info.title = Utilities.trim(name);
3360 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003361 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003362 info.iconResource = iconResource;
3363
3364 return info;
3365 }
3366
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003368 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 * or make a new one.
3370 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003371 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 // See if a placeholder was created for us already
3373 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003374 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003376 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 folders.put(id, folderInfo);
3378 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003379 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003380 }
3381
Joe Onoratobe386092009-11-17 17:32:16 -08003382
Sunny Goyal651077b2014-06-30 14:15:31 -07003383 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3384 return (provider != null) && (provider.provider != null)
3385 && (provider.provider.getPackageName() != null);
3386 }
3387
Joe Onoratobe386092009-11-17 17:32:16 -08003388 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003389 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003390 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3391 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3392 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3393 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003394 if (mLoaderTask != null) {
3395 mLoaderTask.dumpState();
3396 } else {
3397 Log.d(TAG, "mLoaderTask=null");
3398 }
Joe Onoratobe386092009-11-17 17:32:16 -08003399 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003400
3401 public Callbacks getCallback() {
3402 return mCallbacks != null ? mCallbacks.get() : null;
3403 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003404
3405 /**
3406 * @return {@link FolderInfo} if its already loaded.
3407 */
3408 public FolderInfo findFolderById(Long folderId) {
3409 synchronized (sBgLock) {
3410 return sBgFolders.get(folderId);
3411 }
3412 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003413
Sunny Goyal527c7d32015-08-28 15:19:36 -07003414 @Thunk class DeferredMainThreadExecutor implements Executor {
3415
3416 @Override
3417 public void execute(Runnable command) {
3418 runOnMainThread(command);
3419 }
3420 }
3421
Sunny Goyal756adbc2015-04-16 15:20:51 -07003422 /**
3423 * @return the looper for the worker thread which can be used to start background tasks.
3424 */
3425 public static Looper getWorkerLooper() {
3426 return sWorkerThread.getLooper();
3427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003428}