blob: b04fc3fc24373c91e213134f5fca308067b7902b [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
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
24import android.content.ContentProviderClient;
25import android.content.ContentProviderOperation;
26import android.content.ContentResolver;
27import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080030import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070031import android.content.IntentFilter;
32import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040034import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070036import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Resources;
38import android.database.Cursor;
39import android.graphics.Bitmap;
40import android.graphics.BitmapFactory;
41import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080042import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040043import android.os.Handler;
44import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040048import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040049import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080050import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070052import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010053
Sunny Goyalffe83f12014-08-14 17:39:34 -070054import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010055import com.android.launcher3.compat.LauncherActivityInfoCompat;
56import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070057import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070058import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010059import com.android.launcher3.compat.UserHandleCompat;
60import com.android.launcher3.compat.UserManagerCompat;
Romain Guyedcce092010-03-04 13:03:17 -080061
Michael Jurkac2f801e2011-07-12 14:19:46 -070062import java.lang.ref.WeakReference;
63import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070064import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070065import java.text.Collator;
66import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070067import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070068import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070069import java.util.Collections;
70import java.util.Comparator;
71import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070072import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070073import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070074import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070075import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070076import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070077import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070078import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080/**
81 * Maintains in-memory state of the Launcher. It is expected that there should be only one
82 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070083 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 */
Kenny Guyed131872014-04-30 03:02:21 +010085public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010086 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080087 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040088 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070089 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -040090
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070092
Daniel Sandler8707e0f2013-08-15 15:54:18 -070093 // true = use a "More Apps" folder for non-workspace apps on upgrade
94 // false = strew non-workspace apps across the workspace on upgrade
95 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
Dan Sandlerd5024042014-01-09 15:01:33 -050096 public static final int LOADER_FLAG_NONE = 0;
97 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
98 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
99
Joe Onorato36115782010-06-17 13:28:48 -0400100 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500101 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800102
Winson Chungee055712013-07-30 14:46:24 -0700103 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800104 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400105
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400106 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 private final Object mLock = new Object();
108 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -0400109 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700110 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +0200111 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Winson Chung81b52252012-08-27 15:34:29 -0700113 // Specific runnable types that are run on the main thread deferred handler, this allows us to
114 // clear all queued binding runnables when the Launcher activity is destroyed.
115 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
116 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
117
Jason Monkbbe1e242014-05-16 17:37:34 -0400118 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700119
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
121 static {
122 sWorkerThread.start();
123 }
124 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
125
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700132 // When we are loading pages synchronously, we can't just post the binding of items on the side
133 // pages as this delays the rotation process. Instead, we wait for a callback from the first
134 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
135 // a normal load, we also clear this set of Runnables.
136 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
137
Joe Onorato9c1289c2009-08-17 11:03:03 -0400138 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700141 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700143 // The lock that must be acquired before referencing any static bg data structures. Unlike
144 // other locks, this one can generally be held long-term because we never expect any of these
145 // static data structures to be referenced outside of the worker thread except on the first
146 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700147 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
154 // created by LauncherModel that are directly on the home screen (however, no widgets or
155 // shortcuts within folders).
156 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
159 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 new ArrayList<LauncherAppWidgetInfo>();
161
Adam Cohen487f7dd2012-06-28 18:12:10 -0700162 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
163 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700164
Adam Cohen487f7dd2012-06-28 18:12:10 -0700165 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
166 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700167
168 // sBgWorkspaceScreens is the ordered set of workspace screens.
169 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
170
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700171 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700172 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
173 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
177 private IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Reena Lee99a73f32011-10-24 17:27:37 -0700179 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700180
Kenny Guyed131872014-04-30 03:02:21 +0100181 private final LauncherAppsCompat mLauncherApps;
182 private final UserManagerCompat mUserManager;
183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700185 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public int getCurrentWorkspaceScreen();
187 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700188 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
189 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700190 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700191 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500192 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700193 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700196 public void bindAppsAdded(ArrayList<Long> newScreens,
197 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700198 ArrayList<ItemInfo> addAnimated,
199 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700201 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
202 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
203 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyale755d462014-07-22 13:48:29 -0700204 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo);
Sunny Goyala22666f2014-09-18 13:25:15 -0700205 public void updatePackageBadge(String packageName);
Winson Chung83892cc2013-05-01 16:53:33 -0700206 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700207 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Michael Jurkac402cd92013-05-20 15:49:32 +0200208 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100209 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700210 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700211 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700212 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung64359a52013-07-08 17:17:08 -0700215 public interface ItemInfoFilter {
216 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
217 }
218
Bjorn Bringert1307f632013-10-03 22:31:03 +0100219 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800220 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221
Winson Chungee055712013-07-30 14:46:24 -0700222 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700223 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400224 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
225 // resource string.
226 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
227 ProviderInfo providerInfo =
228 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
229 ProviderInfo redirectProvider =
230 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700231
232 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400233 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700234
235 if (mOldContentProviderExists) {
236 Log.d(TAG, "Old launcher provider exists.");
237 } else {
238 Log.d(TAG, "Old launcher provider does not exist.");
239 }
240
Daniel Sandlere4f98912013-06-25 15:13:26 -0400241 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100242 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 mIconCache = iconCache;
244
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400245 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700246 Configuration config = res.getConfiguration();
247 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100248 mLauncherApps = LauncherAppsCompat.getInstance(context);
249 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 }
251
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
253 * posted on the main thread handler. */
254 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700255 runOnMainThread(r, 0);
256 }
257 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700258 if (sWorkerThread.getThreadId() == Process.myTid()) {
259 // If we are on the worker thread, post onto the main handler
260 mHandler.post(r);
261 } else {
262 r.run();
263 }
264 }
265
266 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
267 * posted on the worker thread handler. */
268 private static void runOnWorkerThread(Runnable r) {
269 if (sWorkerThread.getThreadId() == Process.myTid()) {
270 r.run();
271 } else {
272 // If we are not on the worker thread, then post to the worker handler
273 sWorker.post(r);
274 }
275 }
276
Winson Chunge43a1e72014-01-15 10:33:02 -0800277 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
278 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800279 }
280
Winson Chungc9168342013-06-26 14:54:55 -0700281 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
282 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700283 LauncherAppState app = LauncherAppState.getInstance();
284 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
285 final int xCount = (int) grid.numColumns;
286 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700287 boolean[][] occupied = new boolean[xCount][yCount];
288
289 int cellX, cellY, spanX, spanY;
290 for (int i = 0; i < items.size(); ++i) {
291 final ItemInfo item = items.get(i);
292 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
293 if (item.screenId == screen) {
294 cellX = item.cellX;
295 cellY = item.cellY;
296 spanX = item.spanX;
297 spanY = item.spanY;
298 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
299 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
300 occupied[x][y] = true;
301 }
302 }
303 }
304 }
305 }
306
307 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
308 }
309 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700310 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700311 int firstScreenIndex,
312 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700313 // Lock on the app so that we don't try and get the items while apps are being added
314 LauncherAppState app = LauncherAppState.getInstance();
315 LauncherModel model = app.getModel();
316 boolean found = false;
317 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700318 if (sWorkerThread.getThreadId() != Process.myTid()) {
319 // Flush the LauncherModel worker thread, so that if we just did another
320 // processInstallShortcut, we give it time for its shortcut to get added to the
321 // database (getItemsInLocalCoordinates reads the database)
322 model.flushWorkerThread();
323 }
Winson Chungc9168342013-06-26 14:54:55 -0700324 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700325
326 // Try adding to the workspace screens incrementally, starting at the default or center
327 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700328 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
329 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700330 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700331 int[] tmpCoordinates = new int[2];
332 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700333 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700334 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700335 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700336 }
337 }
338 }
Winson Chungc9168342013-06-26 14:54:55 -0700339 return null;
340 }
341
Sunny Goyale755d462014-07-22 13:48:29 -0700342 public void setPackageState(final ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500343 // Process the updated package state
344 Runnable r = new Runnable() {
345 public void run() {
346 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
347 if (callbacks != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700348 callbacks.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500349 }
350 }
351 };
352 mHandler.post(r);
353 }
354
Sunny Goyala22666f2014-09-18 13:25:15 -0700355 public void updatePackageBadge(final String packageName) {
356 // Process the updated package badge
357 Runnable r = new Runnable() {
358 public void run() {
359 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
360 if (callbacks != null) {
361 callbacks.updatePackageBadge(packageName);
362 }
363 }
364 };
365 mHandler.post(r);
366 }
367
Adam Cohen76a47a12014-02-05 11:47:43 -0800368 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
369 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
370
371 if (allAppsApps == null) {
372 throw new RuntimeException("allAppsApps must not be null");
373 }
374 if (allAppsApps.isEmpty()) {
375 return;
376 }
377
378 // Process the newly added applications and add them to the database first
379 Runnable r = new Runnable() {
380 public void run() {
381 runOnMainThread(new Runnable() {
382 public void run() {
383 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
384 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500385 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800386 }
387 }
388 });
389 }
390 };
391 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700392 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800393
394 public void addAndBindAddedWorkspaceApps(final Context context,
395 final ArrayList<ItemInfo> workspaceApps) {
396 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
397
398 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800399 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
400 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800401 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700402 return;
Winson Chung997a9232013-07-24 15:33:46 -0700403 }
Winson Chung64359a52013-07-08 17:17:08 -0700404 // Process the newly added applications and add them to the database first
405 Runnable r = new Runnable() {
406 public void run() {
407 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
408 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
409
Winson Chung76828c82013-08-19 15:43:29 -0700410 // Get the list of workspace screens. We need to append to this list and
411 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
412 // called.
413 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
414 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
415 for (Integer i : orderedScreens.keySet()) {
416 long screenId = orderedScreens.get(i);
417 workspaceScreens.add(screenId);
418 }
419
Winson Chung64359a52013-07-08 17:17:08 -0700420 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700421 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700422 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700423 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700424 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700425 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700426
427 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal4390ace2014-10-13 11:33:11 -0700428 if (shortcutExists(context, name, launchIntent)) {
Winson Chung64359a52013-07-08 17:17:08 -0700429 continue;
430 }
431
Winson Chung87412982013-10-03 18:34:14 -0700432 // Add this icon to the db, creating a new page if necessary. If there
433 // is only the empty page then we just add items to the first page.
434 // Otherwise, we add them to the next pages.
435 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700436 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700437 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700438 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200439 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700440
441 // If we can't find a valid position, then just add a new screen.
442 // This takes time so we need to re-queue the add until the new
443 // page is added. Create as many screens as necessary to satisfy
444 // the startSearchPageIndex.
445 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700446 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700447 while (numPagesToAdd > 0) {
448 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700449 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700450 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700451 addedWorkspaceScreensFinal.add(screenId);
452 numPagesToAdd--;
453 }
Winson Chung76828c82013-08-19 15:43:29 -0700454
Winson Chung64359a52013-07-08 17:17:08 -0700455 // Find the coordinate again
456 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700457 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700458 }
459 if (coords == null) {
460 throw new RuntimeException("Coordinates should not be null");
461 }
462
Winson Chung997a9232013-07-24 15:33:46 -0700463 ShortcutInfo shortcutInfo;
464 if (a instanceof ShortcutInfo) {
465 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200466 } else if (a instanceof AppInfo) {
467 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700468 } else {
469 throw new RuntimeException("Unexpected info type");
470 }
Winson Chung94d67682013-09-25 16:29:40 -0700471
Winson Chung64359a52013-07-08 17:17:08 -0700472 // Add the shortcut to the db
473 addItemToDatabase(context, shortcutInfo,
474 LauncherSettings.Favorites.CONTAINER_DESKTOP,
475 coords.first, coords.second[0], coords.second[1], false);
476 // Save the ShortcutInfo for binding in the workspace
477 addedShortcutsFinal.add(shortcutInfo);
478 }
479 }
480
Winson Chung76828c82013-08-19 15:43:29 -0700481 // Update the workspace screens
482 updateWorkspaceScreenOrder(context, workspaceScreens);
483
Adam Cohen76a47a12014-02-05 11:47:43 -0800484 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700485 runOnMainThread(new Runnable() {
486 public void run() {
487 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
488 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700489 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
490 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700491 if (!addedShortcutsFinal.isEmpty()) {
492 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
493 long lastScreenId = info.screenId;
494 for (ItemInfo i : addedShortcutsFinal) {
495 if (i.screenId == lastScreenId) {
496 addAnimated.add(i);
497 } else {
498 addNotAnimated.add(i);
499 }
Winson Chung997a9232013-07-24 15:33:46 -0700500 }
501 }
Winson Chungd64d1762013-08-20 14:37:16 -0700502 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800503 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700504 }
Winson Chung64359a52013-07-08 17:17:08 -0700505 }
Winson Chung997a9232013-07-24 15:33:46 -0700506 });
507 }
Winson Chung64359a52013-07-08 17:17:08 -0700508 }
509 };
510 runOnWorkerThread(r);
511 }
512
Winson Chung81b52252012-08-27 15:34:29 -0700513 public void unbindItemInfosAndClearQueuedBindRunnables() {
514 if (sWorkerThread.getThreadId() == Process.myTid()) {
515 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
516 "main thread");
517 }
518
519 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400520 synchronized (mDeferredBindRunnables) {
521 mDeferredBindRunnables.clear();
522 }
Winson Chung81b52252012-08-27 15:34:29 -0700523 // Remove any queued bind runnables
524 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
525 // Unbind all the workspace items
526 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700527 }
528
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700529 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700530 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700531 // Ensure that we don't use the same workspace items data structure on the main thread
532 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700533 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
534 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700535 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700536 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
537 tmpAppWidgets.addAll(sBgAppWidgets);
538 }
539 Runnable r = new Runnable() {
540 @Override
541 public void run() {
542 for (ItemInfo item : tmpWorkspaceItems) {
543 item.unbind();
544 }
545 for (ItemInfo item : tmpAppWidgets) {
546 item.unbind();
547 }
548 }
549 };
550 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700551 }
552
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553 /**
554 * Adds an item to the DB if it was not created previously, or move it to a new
555 * <container, screen, cellX, cellY>
556 */
557 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700558 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400559 if (item.container == ItemInfo.NO_ID) {
560 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700561 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 } else {
563 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700564 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 }
566 }
567
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700568 static void checkItemInfoLocked(
569 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
570 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
571 if (modelItem != null && item != modelItem) {
572 // check all the data is consistent
573 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
574 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
575 ShortcutInfo shortcut = (ShortcutInfo) item;
576 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
577 modelShortcut.intent.filterEquals(shortcut.intent) &&
578 modelShortcut.id == shortcut.id &&
579 modelShortcut.itemType == shortcut.itemType &&
580 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700581 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700582 modelShortcut.cellX == shortcut.cellX &&
583 modelShortcut.cellY == shortcut.cellY &&
584 modelShortcut.spanX == shortcut.spanX &&
585 modelShortcut.spanY == shortcut.spanY &&
586 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
587 (modelShortcut.dropPos != null &&
588 shortcut.dropPos != null &&
589 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
590 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
591 // For all intents and purposes, this is the same object
592 return;
593 }
594 }
595
596 // the modelItem needs to match up perfectly with item if our model is
597 // to be consistent with the database-- for now, just require
598 // modelItem == item or the equality check above
599 String msg = "item: " + ((item != null) ? item.toString() : "null") +
600 "modelItem: " +
601 ((modelItem != null) ? modelItem.toString() : "null") +
602 "Error: ItemInfo passed to checkItemInfo doesn't match original";
603 RuntimeException e = new RuntimeException(msg);
604 if (stackTrace != null) {
605 e.setStackTrace(stackTrace);
606 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800607 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700608 }
609 }
610
Michael Jurka816474f2012-06-25 14:49:02 -0700611 static void checkItemInfo(final ItemInfo item) {
612 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
613 final long itemId = item.id;
614 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700615 public void run() {
616 synchronized (sBgLock) {
617 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700618 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700619 }
620 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700621 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700622 }
623
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
625 final ItemInfo item, final String callingFunction) {
626 final long itemId = item.id;
627 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
628 final ContentResolver cr = context.getContentResolver();
629
Adam Cohen487f7dd2012-06-28 18:12:10 -0700630 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631 Runnable r = new Runnable() {
632 public void run() {
633 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700634 updateItemArrays(item, itemId, stackTrace);
635 }
636 };
637 runOnWorkerThread(r);
638 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700639
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
641 final ArrayList<ItemInfo> items, final String callingFunction) {
642 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700643
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700644 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
645 Runnable r = new Runnable() {
646 public void run() {
647 ArrayList<ContentProviderOperation> ops =
648 new ArrayList<ContentProviderOperation>();
649 int count = items.size();
650 for (int i = 0; i < count; i++) {
651 ItemInfo item = items.get(i);
652 final long itemId = item.id;
653 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
654 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700655
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700656 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
657 updateItemArrays(item, itemId, stackTrace);
658
659 }
660 try {
661 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
662 } catch (Exception e) {
663 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700664 }
665 }
666 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700667 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700669
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700670 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
671 // Lock on mBgLock *after* the db operation
672 synchronized (sBgLock) {
673 checkItemInfoLocked(itemId, item, stackTrace);
674
675 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
676 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
677 // Item is in a folder, make sure this folder exists
678 if (!sBgFolders.containsKey(item.container)) {
679 // An items container is being set to a that of an item which is not in
680 // the list of Folders.
681 String msg = "item: " + item + " container being set to: " +
682 item.container + ", not in the list of folders";
683 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 }
685 }
686
687 // Items are added/removed from the corresponding FolderInfo elsewhere, such
688 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
689 // that are on the desktop, as appropriate
690 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800691 if (modelItem != null &&
692 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
693 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694 switch (modelItem.itemType) {
695 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
696 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
697 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
698 if (!sBgWorkspaceItems.contains(modelItem)) {
699 sBgWorkspaceItems.add(modelItem);
700 }
701 break;
702 default:
703 break;
704 }
705 } else {
706 sBgWorkspaceItems.remove(modelItem);
707 }
708 }
709 }
710
Michael Jurkac7700af2013-05-14 20:17:58 +0200711 public void flushWorkerThread() {
712 mFlushingWorkerThread = true;
713 Runnable waiter = new Runnable() {
714 public void run() {
715 synchronized (this) {
716 notifyAll();
717 mFlushingWorkerThread = false;
718 }
719 }
720 };
721
722 synchronized(waiter) {
723 runOnWorkerThread(waiter);
724 if (mLoaderTask != null) {
725 synchronized(mLoaderTask) {
726 mLoaderTask.notify();
727 }
728 }
729 boolean success = false;
730 while (!success) {
731 try {
732 waiter.wait();
733 success = true;
734 } catch (InterruptedException e) {
735 }
736 }
737 }
738 }
739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700742 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700743 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700744 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746 item.cellX = cellX;
747 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700748
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 // We store hotseat items in canonical form which is this orientation invariant position
750 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700751 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700752 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700753 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700754 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700755 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757
758 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700760 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
761 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700762 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400763
Michael Jurkac9d95c52011-08-29 14:03:34 -0700764 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700765 }
766
767 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
769 * cellX, cellY have already been updated on the ItemInfos.
770 */
771 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
772 final long container, final int screen) {
773
774 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
775 int count = items.size();
776
777 for (int i = 0; i < count; i++) {
778 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700779 item.container = container;
780
781 // We store hotseat items in canonical form which is this orientation invariant position
782 // in the hotseat
783 if (context instanceof Launcher && screen < 0 &&
784 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700786 item.cellY);
787 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700789 }
790
791 final ContentValues values = new ContentValues();
792 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
793 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
794 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700796
797 contentValues.add(values);
798 }
799 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
800 }
801
802 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700803 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800804 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700805 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700806 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700807 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800808 item.cellX = cellX;
809 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700810 item.spanX = spanX;
811 item.spanY = spanY;
812
813 // We store hotseat items in canonical form which is this orientation invariant position
814 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700816 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700817 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700818 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700820 }
Adam Cohend4844c32011-02-18 19:25:06 -0800821
Adam Cohend4844c32011-02-18 19:25:06 -0800822 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800823 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700824 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
825 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
826 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
827 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800829
Michael Jurka816474f2012-06-25 14:49:02 -0700830 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700831 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700832
833 /**
834 * Update an item to the database in a specified container.
835 */
836 static void updateItemInDatabase(Context context, final ItemInfo item) {
837 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100838 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700839 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
840 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800841 }
842
843 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 * Returns true if the shortcuts already exists in the database.
845 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400847 static boolean shortcutExists(Context context, String title, Intent intent) {
848 final ContentResolver cr = context.getContentResolver();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700849 final Intent intentWithPkg, intentWithoutPkg;
850
851 if (intent.getComponent() != null) {
852 // If component is not null, an intent with null package will produce
853 // the same result and should also be a match.
854 if (intent.getPackage() != null) {
855 intentWithPkg = intent;
856 intentWithoutPkg = new Intent(intent).setPackage(null);
857 } else {
858 intentWithPkg = new Intent(intent).setPackage(
859 intent.getComponent().getPackageName());
860 intentWithoutPkg = intent;
861 }
862 } else {
863 intentWithPkg = intent;
864 intentWithoutPkg = intent;
865 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700867 new String[] { "title", "intent" }, "title=? and (intent=? or intent=?)",
868 new String[] { title, intentWithPkg.toUri(0), intentWithoutPkg.toUri(0) }, null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 boolean result = false;
870 try {
871 result = c.moveToFirst();
872 } finally {
873 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700876 }
877
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700879 * Returns an ItemInfo array containing all the items in the LauncherModel.
880 * The ItemInfo.id is not set through this function.
881 */
882 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
883 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
884 final ContentResolver cr = context.getContentResolver();
885 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
886 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
Kenny Guyed131872014-04-30 03:02:21 +0100887 LauncherSettings.Favorites.SCREEN,
888 LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
889 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY,
890 LauncherSettings.Favorites.PROFILE_ID }, null, null, null);
Winson Chungaafa03c2010-06-11 17:34:16 -0700891
892 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
893 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
894 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
895 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
896 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
897 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
898 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
Kenny Guyed131872014-04-30 03:02:21 +0100899 final int profileIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.PROFILE_ID);
900 UserManagerCompat userManager = UserManagerCompat.getInstance(context);
Winson Chungaafa03c2010-06-11 17:34:16 -0700901 try {
902 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700903 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700904 item.cellX = c.getInt(cellXIndex);
905 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700906 item.spanX = Math.max(1, c.getInt(spanXIndex));
907 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700908 item.container = c.getInt(containerIndex);
909 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700910 item.screenId = c.getInt(screenIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100911 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +0100912 item.user = userManager.getUserForSerialNumber(serialNumber);
913 // Skip if user has been deleted.
914 if (item.user != null) {
915 items.add(item);
916 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700917 }
918 } catch (Exception e) {
919 items.clear();
920 } finally {
921 c.close();
922 }
923
924 return items;
925 }
926
927 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
929 */
930 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
931 final ContentResolver cr = context.getContentResolver();
932 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
933 "_id=? and (itemType=? or itemType=?)",
934 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700935 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700936
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 try {
938 if (c.moveToFirst()) {
939 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
940 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
941 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
942 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
943 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
944 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 FolderInfo folderInfo = null;
947 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700948 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
949 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700951 }
952
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953 folderInfo.title = c.getString(titleIndex);
954 folderInfo.id = id;
955 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700956 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700957 folderInfo.cellX = c.getInt(cellXIndex);
958 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400959
960 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962 } finally {
963 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700964 }
965
966 return null;
967 }
968
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 /**
970 * Add an item to the database in a specified container. Sets the container, screen, cellX and
971 * cellY fields of the item. Also assigns an ID to the item.
972 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700973 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 item.cellX = cellX;
977 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700978 // We store hotseat items in canonical form which is this orientation invariant position
979 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700980 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700981 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700983 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986
987 final ContentValues values = new ContentValues();
988 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100989 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990
Michael Jurka414300a2013-08-27 15:42:35 +0200991 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700992 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700994
Jason Monk8e19cf22014-03-20 15:06:57 -0400995 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700996 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700997 public void run() {
998 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
999 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001002 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001003 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001004 sBgItemsIdMap.put(item.id, item);
1005 switch (item.itemType) {
1006 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1007 sBgFolders.put(item.id, (FolderInfo) item);
1008 // Fall through
1009 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1010 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1011 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1012 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1013 sBgWorkspaceItems.add(item);
1014 } else {
1015 if (!sBgFolders.containsKey(item.container)) {
1016 // Adding an item to a folder that doesn't exist.
1017 String msg = "adding item: " + item + " to a folder that " +
1018 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001019 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001020 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001021 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 break;
1023 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1024 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1025 break;
1026 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001027 }
1028 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001029 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001030 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001031 }
1032
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001034 * Creates a new unique child id, for a given cell span across all layouts.
1035 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001036 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001037 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001039 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001040 }
1041
Sunny Goyal34942622014-08-29 17:20:55 -07001042 private static ArrayList<ItemInfo> getItemsByPackageName(
1043 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001044 ItemInfoFilter filter = new ItemInfoFilter() {
1045 @Override
1046 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1047 return cn.getPackageName().equals(pn) && info.user.equals(user);
1048 }
1049 };
Sunny Goyal34942622014-08-29 17:20:55 -07001050 return filterItemInfos(sBgItemsIdMap.values(), filter);
1051 }
1052
1053 /**
1054 * Removes all the items from the database corresponding to the specified package.
1055 */
1056 static void deletePackageFromDatabase(Context context, final String pn,
1057 final UserHandleCompat user) {
1058 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001059 }
1060
1061 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001062 * Removes the specified item from the database
1063 * @param context
1064 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001066 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001067 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1068 items.add(item);
1069 deleteItemsFromDatabase(context, items);
1070 }
1071
1072 /**
1073 * Removes the specified items from the database
1074 * @param context
1075 * @param item
1076 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001077 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001078 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -07001079
Michael Jurka83df1882011-08-31 20:59:26 -07001080 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001081 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001082 for (ItemInfo item : items) {
1083 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
1084 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001085
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001086 // Lock on mBgLock *after* the db operation
1087 synchronized (sBgLock) {
1088 switch (item.itemType) {
1089 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1090 sBgFolders.remove(item.id);
1091 for (ItemInfo info: sBgItemsIdMap.values()) {
1092 if (info.container == item.id) {
1093 // We are deleting a folder which still contains items that
1094 // think they are contained by that folder.
1095 String msg = "deleting a folder (" + item + ") which still " +
1096 "contains items (" + info + ")";
1097 Log.e(TAG, msg);
1098 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001100 sBgWorkspaceItems.remove(item);
1101 break;
1102 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1103 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1104 sBgWorkspaceItems.remove(item);
1105 break;
1106 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1107 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1108 break;
1109 }
1110 sBgItemsIdMap.remove(item.id);
1111 sBgDbIconCache.remove(item);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001113 }
1114 }
Michael Jurka83df1882011-08-31 20:59:26 -07001115 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 }
1118
1119 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001120 * Update the order of the workspace screens in the database. The array list contains
1121 * a list of screen ids in the order that they should appear.
1122 */
Winson Chungc9168342013-06-26 14:54:55 -07001123 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001124 // Log to disk
1125 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1126 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1127
Winson Chung64359a52013-07-08 17:17:08 -07001128 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001129 final ContentResolver cr = context.getContentResolver();
1130 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1131
1132 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001133 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001134 while (iter.hasNext()) {
1135 long id = iter.next();
1136 if (id < 0) {
1137 iter.remove();
1138 }
1139 }
1140
1141 Runnable r = new Runnable() {
1142 @Override
1143 public void run() {
Yura085c8532014-02-11 15:15:29 +00001144 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001146 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001147 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 for (int i = 0; i < count; i++) {
1149 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001150 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001151 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1152 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001153 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001154 }
Yura085c8532014-02-11 15:15:29 +00001155
1156 try {
1157 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1158 } catch (Exception ex) {
1159 throw new RuntimeException(ex);
1160 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001161
Winson Chungba9c37f2013-08-30 14:11:37 -07001162 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001163 sBgWorkspaceScreens.clear();
1164 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001165 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001166 }
1167 };
1168 runOnWorkerThread(r);
1169 }
1170
1171 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 * Remove the contents of the specified folder from the database
1173 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001174 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 final ContentResolver cr = context.getContentResolver();
1176
Michael Jurkac9d95c52011-08-29 14:03:34 -07001177 Runnable r = new Runnable() {
1178 public void run() {
1179 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001180 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001181 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001182 sBgItemsIdMap.remove(info.id);
1183 sBgFolders.remove(info.id);
1184 sBgDbIconCache.remove(info);
1185 sBgWorkspaceItems.remove(info);
1186 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001187
Michael Jurkac9d95c52011-08-29 14:03:34 -07001188 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1189 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001190 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001191 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 for (ItemInfo childInfo : info.contents) {
1193 sBgItemsIdMap.remove(childInfo.id);
1194 sBgDbIconCache.remove(childInfo);
1195 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001196 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001197 }
1198 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 }
1201
1202 /**
1203 * Set this as the current Launcher activity object for the loader.
1204 */
1205 public void initialize(Callbacks callbacks) {
1206 synchronized (mLock) {
1207 mCallbacks = new WeakReference<Callbacks>(callbacks);
1208 }
1209 }
1210
Kenny Guyed131872014-04-30 03:02:21 +01001211 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001212 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001213 int op = PackageUpdatedTask.OP_UPDATE;
1214 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1215 user));
1216 }
1217
1218 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001219 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001220 int op = PackageUpdatedTask.OP_REMOVE;
1221 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1222 user));
1223 }
1224
1225 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001226 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001227 int op = PackageUpdatedTask.OP_ADD;
1228 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1229 user));
1230 }
1231
1232 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001233 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001234 boolean replacing) {
1235 if (!replacing) {
1236 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1237 user));
1238 if (mAppsCanBeOnRemoveableStorage) {
1239 // Only rebind if we support removable storage. It catches the
1240 // case where
1241 // apps on the external sd card need to be reloaded
1242 startLoaderFromBackground();
1243 }
1244 } else {
1245 // If we are replacing then just update the packages in the list
1246 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1247 packageNames, user));
1248 }
1249 }
1250
1251 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001252 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001253 boolean replacing) {
1254 if (!replacing) {
1255 enqueuePackageUpdated(new PackageUpdatedTask(
1256 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1257 user));
1258 }
1259
1260 }
1261
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001262 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1264 * ACTION_PACKAGE_CHANGED.
1265 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001266 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001267 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001268 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001269
Joe Onorato36115782010-06-17 13:28:48 -04001270 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001271 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001272 // If we have changed locale we need to clear out the labels in all apps/workspace.
1273 forceReload();
1274 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1275 // Check if configuration change was an mcc/mnc change which would affect app resources
1276 // and we would need to clear out the labels in all apps/workspace. Same handling as
1277 // above for ACTION_LOCALE_CHANGED
1278 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001279 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001280 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001281 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001282 forceReload();
1283 }
1284 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001285 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001286 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1287 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001288 if (mCallbacks != null) {
1289 Callbacks callbacks = mCallbacks.get();
1290 if (callbacks != null) {
1291 callbacks.bindSearchablesChanged();
1292 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001293 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001294 }
1295 }
1296
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001297 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001298 resetLoadedState(true, true);
1299
Reena Lee93f824a2011-09-23 17:20:28 -07001300 // Do this here because if the launcher activity is running it will be restarted.
1301 // If it's not running startLoaderFromBackground will merely tell it that it needs
1302 // to reload.
1303 startLoaderFromBackground();
1304 }
1305
Winson Chungf0c6ae02012-03-21 16:10:31 -07001306 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1307 synchronized (mLock) {
1308 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1309 // mWorkspaceLoaded to true later
1310 stopLoaderLocked();
1311 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1312 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1313 }
1314 }
1315
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001316 /**
1317 * When the launcher is in the background, it's possible for it to miss paired
1318 * configuration changes. So whenever we trigger the loader from the background
1319 * tell the launcher that it needs to re-run the loader when it comes back instead
1320 * of doing it now.
1321 */
1322 public void startLoaderFromBackground() {
1323 boolean runLoader = false;
1324 if (mCallbacks != null) {
1325 Callbacks callbacks = mCallbacks.get();
1326 if (callbacks != null) {
1327 // Only actually run the loader if they're not paused.
1328 if (!callbacks.setLoadOnResume()) {
1329 runLoader = true;
1330 }
1331 }
1332 }
1333 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001334 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001335 }
Joe Onorato36115782010-06-17 13:28:48 -04001336 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001337
Reena Lee93f824a2011-09-23 17:20:28 -07001338 // If there is already a loader task running, tell it to stop.
1339 // returns true if isLaunching() was true on the old task
1340 private boolean stopLoaderLocked() {
1341 boolean isLaunching = false;
1342 LoaderTask oldTask = mLoaderTask;
1343 if (oldTask != null) {
1344 if (oldTask.isLaunching()) {
1345 isLaunching = true;
1346 }
1347 oldTask.stopLocked();
1348 }
1349 return isLaunching;
1350 }
1351
Adam Cohen1a85c582014-09-30 09:48:49 -07001352 public boolean isCurrentCallbacks(Callbacks callbacks) {
1353 return (mCallbacks != null && mCallbacks.get() == callbacks);
1354 }
1355
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001357 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1358 }
1359
1360 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001361 synchronized (mLock) {
1362 if (DEBUG_LOADERS) {
1363 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1364 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366 // Clear any deferred bind-runnables from the synchronized load process
1367 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001368 synchronized (mDeferredBindRunnables) {
1369 mDeferredBindRunnables.clear();
1370 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371
Joe Onorato36115782010-06-17 13:28:48 -04001372 // Don't bother to start the thread if we know it's not going to do anything
1373 if (mCallbacks != null && mCallbacks.get() != null) {
1374 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001375 // also, don't downgrade isLaunching if we're already running
1376 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001377 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001378 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1379 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1381 } else {
1382 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1383 sWorker.post(mLoaderTask);
1384 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 }
1387 }
1388
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 void bindRemainingSynchronousPages() {
1390 // Post the remaining side pages to be loaded
1391 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001392 Runnable[] deferredBindRunnables = null;
1393 synchronized (mDeferredBindRunnables) {
1394 deferredBindRunnables = mDeferredBindRunnables.toArray(
1395 new Runnable[mDeferredBindRunnables.size()]);
1396 mDeferredBindRunnables.clear();
1397 }
1398 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001399 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001400 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 }
1402 }
1403
Joe Onorato36115782010-06-17 13:28:48 -04001404 public void stopLoader() {
1405 synchronized (mLock) {
1406 if (mLoaderTask != null) {
1407 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001408 }
1409 }
Joe Onorato36115782010-06-17 13:28:48 -04001410 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001411
Winson Chung76828c82013-08-19 15:43:29 -07001412 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1413 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1414 final ContentResolver contentResolver = context.getContentResolver();
1415 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1416 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1417 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1418
1419 try {
1420 final int idIndex = sc.getColumnIndexOrThrow(
1421 LauncherSettings.WorkspaceScreens._ID);
1422 final int rankIndex = sc.getColumnIndexOrThrow(
1423 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1424 while (sc.moveToNext()) {
1425 try {
1426 long screenId = sc.getLong(idIndex);
1427 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001428 orderedScreens.put(rank, screenId);
1429 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001430 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001431 }
1432 }
1433 } finally {
1434 sc.close();
1435 }
Winson Chunga90303b2013-11-15 13:05:06 -08001436
1437 // Log to disk
1438 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1439 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1440 for (Integer i : orderedScreens.keySet()) {
1441 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1442 }
1443 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1444 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001445 return orderedScreens;
1446 }
1447
Michael Jurkac57b7a82011-08-09 22:02:20 -07001448 public boolean isAllAppsLoaded() {
1449 return mAllAppsLoaded;
1450 }
1451
Winson Chung36a62fe2012-05-06 18:04:42 -07001452 boolean isLoadingWorkspace() {
1453 synchronized (mLock) {
1454 if (mLoaderTask != null) {
1455 return mLoaderTask.isLoadingWorkspace();
1456 }
1457 }
1458 return false;
1459 }
1460
Joe Onorato36115782010-06-17 13:28:48 -04001461 /**
1462 * Runnable for the thread that loads the contents of the launcher:
1463 * - workspace icons
1464 * - widgets
1465 * - all apps icons
1466 */
1467 private class LoaderTask implements Runnable {
1468 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001469 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001470 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001471 private boolean mStopped;
1472 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001473 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001474
Winson Chungc3eecff2011-07-11 17:44:15 -07001475 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001476
Dan Sandlerd5024042014-01-09 15:01:33 -05001477 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001478 mContext = context;
1479 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001480 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001481 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001482 }
1483
Joe Onorato36115782010-06-17 13:28:48 -04001484 boolean isLaunching() {
1485 return mIsLaunching;
1486 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487
Winson Chung36a62fe2012-05-06 18:04:42 -07001488 boolean isLoadingWorkspace() {
1489 return mIsLoadingAndBindingWorkspace;
1490 }
1491
Winson Chungc763c4e2013-07-19 13:49:06 -07001492 /** Returns whether this is an upgrade path */
1493 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001494 mIsLoadingAndBindingWorkspace = true;
1495
Joe Onorato36115782010-06-17 13:28:48 -04001496 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001497 if (DEBUG_LOADERS) {
1498 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001500
Winson Chungc763c4e2013-07-19 13:49:06 -07001501 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001502 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001503 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001504 synchronized (LoaderTask.this) {
1505 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001506 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001507 }
1508 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 }
1510 }
1511
Joe Onorato36115782010-06-17 13:28:48 -04001512 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001513 bindWorkspace(-1, isUpgradePath);
1514 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001515 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001516
Joe Onorato36115782010-06-17 13:28:48 -04001517 private void waitForIdle() {
1518 // Wait until the either we're stopped or the other threads are done.
1519 // This way we don't start loading all apps until the workspace has settled
1520 // down.
1521 synchronized (LoaderTask.this) {
1522 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001523
Joe Onorato36115782010-06-17 13:28:48 -04001524 mHandler.postIdle(new Runnable() {
1525 public void run() {
1526 synchronized (LoaderTask.this) {
1527 mLoadAndBindStepFinished = true;
1528 if (DEBUG_LOADERS) {
1529 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001532 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001533 }
Joe Onorato36115782010-06-17 13:28:48 -04001534 });
1535
Michael Jurkac7700af2013-05-14 20:17:58 +02001536 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001537 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001538 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1539 // wait no longer than 1sec at a time
1540 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001541 } catch (InterruptedException ex) {
1542 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001543 }
1544 }
Joe Onorato36115782010-06-17 13:28:48 -04001545 if (DEBUG_LOADERS) {
1546 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001547 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001548 + "ms for previous step to finish binding");
1549 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001550 }
Joe Onorato36115782010-06-17 13:28:48 -04001551 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001552
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001553 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001554 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001555 // Ensure that we have a valid page index to load synchronously
1556 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1557 "valid page index");
1558 }
1559 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1560 // Ensure that we don't try and bind a specified page when the pages have not been
1561 // loaded already (we should load everything asynchronously in that case)
1562 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1563 }
1564 synchronized (mLock) {
1565 if (mIsLoaderTaskRunning) {
1566 // Ensure that we are never running the background loading at this point since
1567 // we also touch the background collections
1568 throw new RuntimeException("Error! Background loading is already running");
1569 }
1570 }
1571
1572 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1573 // data structures, we can't allow any other thread to touch that data, but because
1574 // this call is synchronous, we can get away with not locking).
1575
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001576 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001577 // operations from the previous activity. We need to ensure that all queued operations
1578 // are executed before any synchronous binding work is done.
1579 mHandler.flush();
1580
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001581 // Divide the set of loaded items into those that we are binding synchronously, and
1582 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001583 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001584 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1585 // arise from that.
1586 onlyBindAllApps();
1587 }
1588
Joe Onorato36115782010-06-17 13:28:48 -04001589 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001590 boolean isUpgrade = false;
1591
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001592 synchronized (mLock) {
1593 mIsLoaderTaskRunning = true;
1594 }
Joe Onorato36115782010-06-17 13:28:48 -04001595 // Optimize for end-user experience: if the Launcher is up and // running with the
1596 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1597 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001598 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001599 // Elevate priority when Home launches for the first time to avoid
1600 // starving at boot time. Staring at a blank home is not cool.
1601 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001602 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1603 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001604 android.os.Process.setThreadPriority(mIsLaunching
1605 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1606 }
Winson Chung64359a52013-07-08 17:17:08 -07001607 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001608 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001609
Joe Onorato36115782010-06-17 13:28:48 -04001610 if (mStopped) {
1611 break keep_running;
1612 }
1613
1614 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1615 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001616 synchronized (mLock) {
1617 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001618 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001619 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1620 }
1621 }
Joe Onorato36115782010-06-17 13:28:48 -04001622 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001623
1624 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001625 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1626 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001627
1628 // Restore the default thread priority after we are done loading items
1629 synchronized (mLock) {
1630 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1631 }
Joe Onorato36115782010-06-17 13:28:48 -04001632 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001633
Winson Chungaac01e12011-08-17 10:37:13 -07001634 // Update the saved icons if necessary
1635 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001636 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 for (Object key : sBgDbIconCache.keySet()) {
1638 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1639 }
1640 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001641 }
Winson Chungaac01e12011-08-17 10:37:13 -07001642
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001643 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001644 // Ensure that all the applications that are in the system are
1645 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001646 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001647 verifyApplications();
1648 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001649 }
1650
Joe Onorato36115782010-06-17 13:28:48 -04001651 // Clear out this reference, otherwise we end up holding it until all of the
1652 // callback runnables are done.
1653 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001654
Joe Onorato36115782010-06-17 13:28:48 -04001655 synchronized (mLock) {
1656 // If we are still the last one to be scheduled, remove ourselves.
1657 if (mLoaderTask == this) {
1658 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001660 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001661 }
Joe Onorato36115782010-06-17 13:28:48 -04001662 }
1663
1664 public void stopLocked() {
1665 synchronized (LoaderTask.this) {
1666 mStopped = true;
1667 this.notify();
1668 }
1669 }
1670
1671 /**
1672 * Gets the callbacks object. If we've been stopped, or if the launcher object
1673 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1674 * object that was around when the deferred message was scheduled, and if there's
1675 * a new Callbacks object around then also return null. This will save us from
1676 * calling onto it with data that will be ignored.
1677 */
1678 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1679 synchronized (mLock) {
1680 if (mStopped) {
1681 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001682 }
Joe Onorato36115782010-06-17 13:28:48 -04001683
1684 if (mCallbacks == null) {
1685 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001686 }
Joe Onorato36115782010-06-17 13:28:48 -04001687
1688 final Callbacks callbacks = mCallbacks.get();
1689 if (callbacks != oldCallbacks) {
1690 return null;
1691 }
1692 if (callbacks == null) {
1693 Log.w(TAG, "no mCallbacks");
1694 return null;
1695 }
1696
1697 return callbacks;
1698 }
1699 }
1700
Winson Chungc763c4e2013-07-19 13:49:06 -07001701 private void verifyApplications() {
1702 final Context context = mApp.getContext();
1703
1704 // Cross reference all the applications in our apps list with items in the workspace
1705 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001706 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001707 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001708 for (AppInfo app : mBgAllAppsList.data) {
Kenny Guyed131872014-04-30 03:02:21 +01001709 tmpInfos = getItemInfoForComponentName(app.componentName, app.user);
Winson Chungc763c4e2013-07-19 13:49:06 -07001710 if (tmpInfos.isEmpty()) {
1711 // We are missing an application icon, so add this to the workspace
1712 added.add(app);
1713 // This is a rare event, so lets log it
1714 Log.e(TAG, "Missing Application on load: " + app);
1715 }
1716 }
1717 }
1718 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001719 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001720 }
1721 }
1722
Joe Onorato36115782010-06-17 13:28:48 -04001723 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001724 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001725 LauncherAppState app = LauncherAppState.getInstance();
1726 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001727 final int countX = (int) grid.numColumns;
1728 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001729
Adam Cohendcd297f2013-06-18 13:13:40 -07001730 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001731 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001732 // Return early if we detect that an item is under the hotseat button
1733 if (mCallbacks == null ||
1734 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001735 Log.e(TAG, "Error loading shortcut into hotseat " + item
1736 + " into position (" + item.screenId + ":" + item.cellX + ","
1737 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001738 return false;
1739 }
1740
Dan Sandler295ae182013-12-10 16:05:47 -05001741 final ItemInfo[][] hotseatItems =
1742 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1743
Adam Cohenae4409d2013-11-26 10:34:59 -08001744 if (item.screenId >= grid.numHotseatIcons) {
1745 Log.e(TAG, "Error loading shortcut " + item
1746 + " into hotseat position " + item.screenId
1747 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1748 + ")");
1749 return false;
1750 }
1751
Dan Sandler295ae182013-12-10 16:05:47 -05001752 if (hotseatItems != null) {
1753 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001754 Log.e(TAG, "Error loading shortcut into hotseat " + item
1755 + " into position (" + item.screenId + ":" + item.cellX + ","
1756 + item.cellY + ") occupied by "
1757 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1758 [(int) item.screenId][0]);
1759 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001760 } else {
1761 hotseatItems[(int) item.screenId][0] = item;
1762 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001763 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001764 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001765 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001766 items[(int) item.screenId][0] = item;
1767 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001768 return true;
1769 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001770 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1771 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001772 return true;
1773 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001774
Adam Cohendcd297f2013-06-18 13:13:40 -07001775 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001776 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001777 occupied.put(item.screenId, items);
1778 }
1779
Dan Sandler295ae182013-12-10 16:05:47 -05001780 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001781 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1782 item.cellX < 0 || item.cellY < 0 ||
1783 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1784 Log.e(TAG, "Error loading shortcut " + item
1785 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1786 + item.cellX + "," + item.cellY
1787 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1788 return false;
1789 }
1790
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001791 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001792 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1793 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001795 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001796 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001797 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001798 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001799 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001800 return false;
1801 }
1802 }
1803 }
1804 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1805 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001806 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001807 }
1808 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001809
Joe Onorato36115782010-06-17 13:28:48 -04001810 return true;
1811 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812
Winson Chungba9c37f2013-08-30 14:11:37 -07001813 /** Clears all the sBg data structures */
1814 private void clearSBgDataStructures() {
1815 synchronized (sBgLock) {
1816 sBgWorkspaceItems.clear();
1817 sBgAppWidgets.clear();
1818 sBgFolders.clear();
1819 sBgItemsIdMap.clear();
1820 sBgDbIconCache.clear();
1821 sBgWorkspaceScreens.clear();
1822 }
1823 }
1824
Dan Sandlerd5024042014-01-09 15:01:33 -05001825 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001826 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001827 // Log to disk
1828 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1829
Joe Onorato36115782010-06-17 13:28:48 -04001830 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831
Joe Onorato36115782010-06-17 13:28:48 -04001832 final Context context = mContext;
1833 final ContentResolver contentResolver = context.getContentResolver();
1834 final PackageManager manager = context.getPackageManager();
1835 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1836 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001837 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1838 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001839 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001840
Winson Chung892c74d2013-08-22 16:15:50 -07001841 LauncherAppState app = LauncherAppState.getInstance();
1842 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1843 int countX = (int) grid.numColumns;
1844 int countY = (int) grid.numRows;
1845
Dan Sandlerd5024042014-01-09 15:01:33 -05001846 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1847 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1848 LauncherAppState.getLauncherProvider().deleteDatabase();
1849 }
1850
1851 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1852 // append the user's Launcher2 shortcuts
1853 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1854 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1855 } else {
1856 // Make sure the default workspace is loaded
1857 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001858 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001859 }
Adam Cohene25af792013-06-06 23:08:25 -07001860
Adam Cohen71483f42014-05-15 14:04:01 -07001861 // This code path is for our old migration code and should no longer be exercised
1862 boolean loadedOldDb = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001863
Winson Chung9f9f00b2013-11-15 13:27:00 -08001864 // Log to disk
1865 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001866
Winson Chung2abf94d2012-07-18 18:16:38 -07001867 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001868 clearSBgDataStructures();
Sunny Goyal94485362014-09-18 16:13:58 -07001869 final HashSet<String> installingPkgs = PackageInstallerCompat
1870 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001871
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001872 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001873 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07001874 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION;
Chris Wrene523e702013-10-09 10:36:55 -04001875 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001876 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001877
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 // +1 for the hotseat (it can be larger than the workspace)
1879 // Load workspace in reverse order to ensure that latest items are loaded first (and
1880 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001881 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 try {
1884 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1885 final int intentIndex = c.getColumnIndexOrThrow
1886 (LauncherSettings.Favorites.INTENT);
1887 final int titleIndex = c.getColumnIndexOrThrow
1888 (LauncherSettings.Favorites.TITLE);
1889 final int iconTypeIndex = c.getColumnIndexOrThrow(
1890 LauncherSettings.Favorites.ICON_TYPE);
1891 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1892 final int iconPackageIndex = c.getColumnIndexOrThrow(
1893 LauncherSettings.Favorites.ICON_PACKAGE);
1894 final int iconResourceIndex = c.getColumnIndexOrThrow(
1895 LauncherSettings.Favorites.ICON_RESOURCE);
1896 final int containerIndex = c.getColumnIndexOrThrow(
1897 LauncherSettings.Favorites.CONTAINER);
1898 final int itemTypeIndex = c.getColumnIndexOrThrow(
1899 LauncherSettings.Favorites.ITEM_TYPE);
1900 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1901 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001902 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1903 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 final int screenIndex = c.getColumnIndexOrThrow(
1905 LauncherSettings.Favorites.SCREEN);
1906 final int cellXIndex = c.getColumnIndexOrThrow
1907 (LauncherSettings.Favorites.CELLX);
1908 final int cellYIndex = c.getColumnIndexOrThrow
1909 (LauncherSettings.Favorites.CELLY);
1910 final int spanXIndex = c.getColumnIndexOrThrow
1911 (LauncherSettings.Favorites.SPANX);
1912 final int spanYIndex = c.getColumnIndexOrThrow(
1913 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001914 final int restoredIndex = c.getColumnIndexOrThrow(
1915 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001916 final int profileIdIndex = c.getColumnIndexOrThrow(
1917 LauncherSettings.Favorites.PROFILE_ID);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1919 //final int displayModeIndex = c.getColumnIndexOrThrow(
1920 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 ShortcutInfo info;
1923 String intentDescription;
1924 LauncherAppWidgetInfo appWidgetInfo;
1925 int container;
1926 long id;
1927 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001928 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001929
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001930 while (!mStopped && c.moveToNext()) {
1931 try {
1932 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001933 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001934 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001935
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001936 switch (itemType) {
1937 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1938 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001939 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001940 intentDescription = c.getString(intentIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001941 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001942 user = mUserManager.getUserForSerialNumber(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001943 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001944 int disabledState = 0;
Kenny Guyed131872014-04-30 03:02:21 +01001945 if (user == null) {
1946 // User has been deleted remove the item.
1947 itemsToRemove.add(id);
1948 continue;
1949 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 try {
1951 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001952 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001953 if (cn != null && cn.getPackageName() != null) {
1954 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1955 cn.getPackageName(), user);
1956 boolean validComponent = validPkg &&
1957 launcherApps.isActivityEnabledForProfile(cn, user);
1958
1959 if (validComponent) {
1960 if (restored) {
1961 // no special handling necessary for this item
1962 restoredRows.add(id);
1963 restored = false;
1964 }
1965 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001966 intent = null;
1967 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1968 // We allow auto install apps to have their intent
1969 // updated after an install.
1970 intent = manager.getLaunchIntentForPackage(
1971 cn.getPackageName());
1972 if (intent != null) {
1973 ContentValues values = new ContentValues();
1974 values.put(LauncherSettings.Favorites.INTENT,
1975 intent.toUri(0));
1976 String where = BaseColumns._ID + "= ?";
1977 String[] args = {Long.toString(id)};
1978 contentResolver.update(contentUri, values, where, args);
1979 }
1980 }
1981
1982 if (intent == null) {
1983 // The app is installed but the component is no
1984 // longer available.
1985 Launcher.addDumpLog(TAG,
1986 "Invalid component removed: " + cn, true);
1987 itemsToRemove.add(id);
1988 continue;
1989 } else {
1990 // no special handling necessary for this item
1991 restoredRows.add(id);
1992 restored = false;
1993 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001994 } else if (restored) {
1995 // Package is not yet available but might be
1996 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001997 Launcher.addDumpLog(TAG,
1998 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001999
2000 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
2001 // Restore has started once.
2002 } else if (installingPkgs.contains(cn.getPackageName())) {
2003 // App restore has started. Update the flag
2004 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
2005 ContentValues values = new ContentValues();
2006 values.put(LauncherSettings.Favorites.RESTORED,
2007 promiseType);
2008 String where = BaseColumns._ID + "= ?";
2009 String[] args = {Long.toString(id)};
2010 contentResolver.update(contentUri, values, where, args);
2011
2012 } else if (REMOVE_UNRESTORED_ICONS) {
2013 Launcher.addDumpLog(TAG,
2014 "Unrestored package removed: " + cn, true);
2015 itemsToRemove.add(id);
2016 continue;
2017 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002018 } else if (launcherApps.isAppEnabled(
2019 manager, cn.getPackageName(),
2020 PackageManager.GET_UNINSTALLED_PACKAGES)) {
2021 // Package is present but not available.
2022 allowMissingTarget = true;
2023 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
2024 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002025 // SdCard is not ready yet. Package might get available,
2026 // once it is ready.
2027 Launcher.addDumpLog(TAG, "Invalid package: " + cn
2028 + " (check again later)", true);
2029 HashSet<String> pkgs = sPendingPackages.get(user);
2030 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07002031 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002032 sPendingPackages.put(user, pkgs);
2033 }
2034 pkgs.add(cn.getPackageName());
2035 allowMissingTarget = true;
2036 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07002037
2038 } else {
2039 // Do not wait for external media load anymore.
2040 // Log the invalid package, and remove it
2041 Launcher.addDumpLog(TAG,
2042 "Invalid package removed: " + cn, true);
2043 itemsToRemove.add(id);
2044 continue;
Winson Chungee055712013-07-30 14:46:24 -07002045 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002046 } else if (cn == null) {
2047 // For shortcuts with no component, keep them as they are
2048 restoredRows.add(id);
2049 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002050 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002052 Launcher.addDumpLog(TAG,
2053 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 continue;
2055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056
Chris Wrenf4d08112014-01-16 18:13:56 -05002057 if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002058 if (user.equals(UserHandleCompat.myUserHandle())) {
2059 Launcher.addDumpLog(TAG,
2060 "constructing info for partially restored package",
2061 true);
Sunny Goyal34942622014-08-29 17:20:55 -07002062 info = getRestoredItemInfo(c, titleIndex, intent, promiseType);
Kenny Guyed131872014-04-30 03:02:21 +01002063 intent = getRestoredItemIntent(c, context, intent);
2064 } else {
2065 // Don't restore items for other profiles.
2066 itemsToRemove.add(id);
2067 continue;
2068 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002069 } else if (itemType ==
2070 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002071 info = getShortcutInfo(manager, intent, user, context, c,
2072 iconIndex, titleIndex, mLabelCache, allowMissingTarget);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 } else {
2074 info = getShortcutInfo(c, context, iconTypeIndex,
2075 iconPackageIndex, iconResourceIndex, iconIndex,
2076 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002077
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 // App shortcuts that used to be automatically added to Launcher
2079 // didn't always have the correct intent flags set, so do that
2080 // here
2081 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002082 intent.getCategories() != null &&
2083 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002084 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 intent.addFlags(
2086 Intent.FLAG_ACTIVITY_NEW_TASK |
2087 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2088 }
Michael Jurka96879562012-03-22 05:54:33 -07002089 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002090
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002092 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 container = c.getInt(containerIndex);
2095 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002096 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 info.cellX = c.getInt(cellXIndex);
2098 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002099 info.spanX = 1;
2100 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002101 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002102 info.isDisabled = disabledState;
2103 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2104 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2105 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002106
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002108 if (!checkItemPlacement(occupied, info)) {
2109 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 break;
2111 }
2112
2113 switch (container) {
2114 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2115 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2116 sBgWorkspaceItems.add(info);
2117 break;
2118 default:
2119 // Item is in a user folder
2120 FolderInfo folderInfo =
2121 findOrMakeFolder(sBgFolders, container);
2122 folderInfo.add(info);
2123 break;
2124 }
2125 sBgItemsIdMap.put(info.id, info);
2126
2127 // now that we've loaded everthing re-save it with the
2128 // icon in case it disappears somehow.
2129 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07002130 } else {
2131 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 }
2133 break;
2134
2135 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2136 id = c.getLong(idIndex);
2137 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2138
2139 folderInfo.title = c.getString(titleIndex);
2140 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002141 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002143 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 folderInfo.cellX = c.getInt(cellXIndex);
2145 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002146 folderInfo.spanX = 1;
2147 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148
Daniel Sandler8802e962010-05-26 16:28:16 -04002149 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002150 if (!checkItemPlacement(occupied, folderInfo)) {
2151 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002152 break;
2153 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002154
Joe Onorato9c1289c2009-08-17 11:03:03 -04002155 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2157 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2158 sBgWorkspaceItems.add(folderInfo);
2159 break;
Joe Onorato36115782010-06-17 13:28:48 -04002160 }
Joe Onorato17a89222011-02-08 17:26:11 -08002161
Chris Wrenf4d08112014-01-16 18:13:56 -05002162 if (restored) {
2163 // no special handling required for restored folders
2164 restoredRows.add(id);
2165 }
2166
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2168 sBgFolders.put(folderInfo.id, folderInfo);
2169 break;
2170
2171 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2172 // Read all Launcher-specific widget details
2173 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002174 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002175 id = c.getLong(idIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002176 final ComponentName component =
2177 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002178
Sunny Goyal651077b2014-06-30 14:15:31 -07002179 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002180 final boolean isIdValid = (restoreStatus &
2181 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Joe Onorato36115782010-06-17 13:28:48 -04002182
Sunny Goyalff572272014-07-23 13:58:07 -07002183 final boolean wasProviderReady = (restoreStatus &
2184 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002185
Sunny Goyalff572272014-07-23 13:58:07 -07002186 final AppWidgetProviderInfo provider = isIdValid
2187 ? widgets.getAppWidgetInfo(appWidgetId)
2188 : findAppWidgetProviderInfoWithComponent(context, component);
2189
2190 final boolean isProviderReady = isValidProvider(provider);
2191 if (!isSafeMode && wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002192 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002193 + "id=" + id + " appWidgetId=" + appWidgetId;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002195 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 itemsToRemove.add(id);
2197 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002198 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002199 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2200 provider.provider);
2201 int[] minSpan =
2202 Launcher.getMinSpanForWidget(context, provider);
2203 appWidgetInfo.minSpanX = minSpan[0];
2204 appWidgetInfo.minSpanY = minSpan[1];
Sunny Goyalff572272014-07-23 13:58:07 -07002205
2206 int status = restoreStatus;
2207 if (!wasProviderReady) {
2208 // If provider was not previously ready, update the
2209 // status and UI flag.
2210
2211 // Id would be valid only if the widget restore broadcast was received.
2212 if (isIdValid) {
2213 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2214 } else {
2215 status &= ~LauncherAppWidgetInfo
2216 .FLAG_PROVIDER_NOT_READY;
2217 }
2218 }
2219 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002220 } else {
2221 Log.v(TAG, "Widget restore pending id=" + id
2222 + " appWidgetId=" + appWidgetId
2223 + " status =" + restoreStatus);
2224 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002225 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002226 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal94485362014-09-18 16:13:58 -07002227
2228 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2229 // Restore has started once.
2230 } else if (installingPkgs.contains(component.getPackageName())) {
2231 // App restore has started. Update the flag
2232 appWidgetInfo.restoreStatus |=
2233 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002234 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002235 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002236 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002237 itemsToRemove.add(id);
2238 continue;
2239 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002240 }
Sunny Goyalff572272014-07-23 13:58:07 -07002241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 appWidgetInfo.cellX = c.getInt(cellXIndex);
2245 appWidgetInfo.cellY = c.getInt(cellYIndex);
2246 appWidgetInfo.spanX = c.getInt(spanXIndex);
2247 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002248
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 container = c.getInt(containerIndex);
2250 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2251 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2252 Log.e(TAG, "Widget found where container != " +
2253 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2254 continue;
2255 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256
Adam Cohene25af792013-06-06 23:08:25 -07002257 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002259 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2260 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 break;
2262 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002263
Sunny Goyal94485362014-09-18 16:13:58 -07002264 String providerName = appWidgetInfo.providerName.flattenToString();
2265 if (!providerName.equals(savedProvider) ||
2266 (appWidgetInfo.restoreStatus != restoreStatus)) {
2267 ContentValues values = new ContentValues();
2268 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2269 providerName);
2270 values.put(LauncherSettings.Favorites.RESTORED,
2271 appWidgetInfo.restoreStatus);
2272 String where = BaseColumns._ID + "= ?";
2273 String[] args = {Long.toString(id)};
2274 contentResolver.update(contentUri, values, where, args);
Chris Wrenc3919c02013-09-18 09:48:33 -04002275 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2277 sBgAppWidgets.add(appWidgetInfo);
2278 }
Joe Onorato36115782010-06-17 13:28:48 -04002279 break;
2280 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002282 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002283 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 }
2285 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002286 if (c != null) {
2287 c.close();
2288 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 }
2290
Winson Chungba9c37f2013-08-30 14:11:37 -07002291 // Break early if we've stopped loading
2292 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002293 clearSBgDataStructures();
2294 return false;
2295 }
2296
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 if (itemsToRemove.size() > 0) {
2298 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002299 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 // Remove dead items
2301 for (long id : itemsToRemove) {
2302 if (DEBUG_LOADERS) {
2303 Log.d(TAG, "Removed id = " + id);
2304 }
2305 // Don't notify content observers
2306 try {
2307 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2308 null, null);
2309 } catch (RemoteException e) {
2310 Log.w(TAG, "Could not remove id = " + id);
2311 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002312 }
2313 }
2314
Chris Wrenf4d08112014-01-16 18:13:56 -05002315 if (restoredRows.size() > 0) {
2316 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002317 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002318 // Update restored items that no longer require special handling
2319 try {
2320 StringBuilder selectionBuilder = new StringBuilder();
2321 selectionBuilder.append(LauncherSettings.Favorites._ID);
2322 selectionBuilder.append(" IN (");
2323 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2324 selectionBuilder.append(")");
2325 ContentValues values = new ContentValues();
2326 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal34942622014-08-29 17:20:55 -07002327 updater.update(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Chris Wrenf4d08112014-01-16 18:13:56 -05002328 values, selectionBuilder.toString(), null);
2329 } catch (RemoteException e) {
2330 Log.w(TAG, "Could not update restored rows");
2331 }
2332 }
2333
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002334 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2335 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002336 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002337 null, sWorker);
2338 }
2339
Winson Chungc763c4e2013-07-19 13:49:06 -07002340 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002341 long maxScreenId = 0;
2342 // If we're importing we use the old screen order.
2343 for (ItemInfo item: sBgItemsIdMap.values()) {
2344 long screenId = item.screenId;
2345 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2346 !sBgWorkspaceScreens.contains(screenId)) {
2347 sBgWorkspaceScreens.add(screenId);
2348 if (screenId > maxScreenId) {
2349 maxScreenId = screenId;
2350 }
2351 }
2352 }
2353 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002354 // Log to disk
2355 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2356 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2357 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002358
Michael Jurka414300a2013-08-27 15:42:35 +02002359 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002360 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002361
2362 // Update the max item id after we load an old db
2363 long maxItemId = 0;
2364 // If we're importing we use the old screen order.
2365 for (ItemInfo item: sBgItemsIdMap.values()) {
2366 maxItemId = Math.max(maxItemId, item.id);
2367 }
Michael Jurka414300a2013-08-27 15:42:35 +02002368 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002369 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002370 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2371 for (Integer i : orderedScreens.keySet()) {
2372 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002373 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002374 // Log to disk
2375 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2376 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002377
2378 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002379 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002380 for (ItemInfo item: sBgItemsIdMap.values()) {
2381 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002382 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2383 unusedScreens.contains(screenId)) {
2384 unusedScreens.remove(screenId);
2385 }
2386 }
2387
2388 // If there are any empty screens remove them, and update.
2389 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002390 // Log to disk
2391 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2392 TextUtils.join(", ", unusedScreens), true);
2393
Winson Chung933bae62013-08-29 11:42:30 -07002394 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002395 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2396 }
2397 }
2398
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399 if (DEBUG_LOADERS) {
2400 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2401 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002402 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002403 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002405
Daniel Sandler566da102013-06-25 23:43:45 -04002406 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002407 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002408 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002409 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410 line += " | ";
2411 }
Winson Chung892c74d2013-08-22 16:15:50 -07002412 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002413 ItemInfo[][] screen = occupied.get(screenId);
2414 if (x < screen.length && y < screen[x].length) {
2415 line += (screen[x][y] != null) ? "#" : ".";
2416 } else {
2417 line += "!";
2418 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002419 }
Joe Onorato36115782010-06-17 13:28:48 -04002420 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002423 }
Joe Onorato36115782010-06-17 13:28:48 -04002424 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002425 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002426 }
2427
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 /** Filters the set of items who are directly or indirectly (via another container) on the
2429 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002430 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431 ArrayList<ItemInfo> allWorkspaceItems,
2432 ArrayList<ItemInfo> currentScreenItems,
2433 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002434 // Purge any null ItemInfos
2435 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2436 while (iter.hasNext()) {
2437 ItemInfo i = iter.next();
2438 if (i == null) {
2439 iter.remove();
2440 }
2441 }
2442
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 // Order the set of items by their containers first, this allows use to walk through the
2444 // list sequentially, build up a list of containers that are in the specified screen,
2445 // as well as all items in those containers.
2446 Set<Long> itemsOnScreen = new HashSet<Long>();
2447 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2448 @Override
2449 public int compare(ItemInfo lhs, ItemInfo rhs) {
2450 return (int) (lhs.container - rhs.container);
2451 }
2452 });
2453 for (ItemInfo info : allWorkspaceItems) {
2454 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002455 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 currentScreenItems.add(info);
2457 itemsOnScreen.add(info.id);
2458 } else {
2459 otherScreenItems.add(info);
2460 }
2461 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2462 currentScreenItems.add(info);
2463 itemsOnScreen.add(info.id);
2464 } else {
2465 if (itemsOnScreen.contains(info.container)) {
2466 currentScreenItems.add(info);
2467 itemsOnScreen.add(info.id);
2468 } else {
2469 otherScreenItems.add(info);
2470 }
2471 }
2472 }
2473 }
2474
2475 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002476 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 ArrayList<LauncherAppWidgetInfo> appWidgets,
2478 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2479 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480
2481 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002482 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002484 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 currentScreenWidgets.add(widget);
2486 } else {
2487 otherScreenWidgets.add(widget);
2488 }
2489 }
2490 }
2491
2492 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002493 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494 HashMap<Long, ItemInfo> itemsIdMap,
2495 HashMap<Long, FolderInfo> folders,
2496 HashMap<Long, FolderInfo> currentScreenFolders,
2497 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498
2499 for (long id : folders.keySet()) {
2500 ItemInfo info = itemsIdMap.get(id);
2501 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002502 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002503 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002504 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 currentScreenFolders.put(id, folder);
2506 } else {
2507 otherScreenFolders.put(id, folder);
2508 }
2509 }
2510 }
2511
2512 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2513 * right) */
2514 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002515 final LauncherAppState app = LauncherAppState.getInstance();
2516 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002517 // XXX: review this
2518 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002519 @Override
2520 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002521 int cellCountX = (int) grid.numColumns;
2522 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002523 int screenOffset = cellCountX * cellCountY;
2524 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002525 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002526 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002527 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002528 rhs.cellY * cellCountX + rhs.cellX);
2529 return (int) (lr - rr);
2530 }
2531 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002532 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002533
Adam Cohendcd297f2013-06-18 13:13:40 -07002534 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2535 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002536 final Runnable r = new Runnable() {
2537 @Override
2538 public void run() {
2539 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2540 if (callbacks != null) {
2541 callbacks.bindScreens(orderedScreens);
2542 }
2543 }
2544 };
2545 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2546 }
2547
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2549 final ArrayList<ItemInfo> workspaceItems,
2550 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2551 final HashMap<Long, FolderInfo> folders,
2552 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002553
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 final boolean postOnMainThread = (deferredBindRunnables != null);
2555
2556 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002557 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002559 final int start = i;
2560 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561 final Runnable r = new Runnable() {
2562 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002563 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002564 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002566 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2567 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002568 }
2569 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002570 };
2571 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002572 synchronized (deferredBindRunnables) {
2573 deferredBindRunnables.add(r);
2574 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002576 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002577 }
Joe Onorato36115782010-06-17 13:28:48 -04002578 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002579
2580 // Bind the folders
2581 if (!folders.isEmpty()) {
2582 final Runnable r = new Runnable() {
2583 public void run() {
2584 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2585 if (callbacks != null) {
2586 callbacks.bindFolders(folders);
2587 }
2588 }
2589 };
2590 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002591 synchronized (deferredBindRunnables) {
2592 deferredBindRunnables.add(r);
2593 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002595 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002596 }
2597 }
2598
2599 // Bind the widgets, one at a time
2600 N = appWidgets.size();
2601 for (int i = 0; i < N; i++) {
2602 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2603 final Runnable r = new Runnable() {
2604 public void run() {
2605 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2606 if (callbacks != null) {
2607 callbacks.bindAppWidget(widget);
2608 }
2609 }
2610 };
2611 if (postOnMainThread) {
2612 deferredBindRunnables.add(r);
2613 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002614 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002615 }
2616 }
2617 }
2618
2619 /**
2620 * Binds all loaded data to actual views on the main thread.
2621 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002622 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 final long t = SystemClock.uptimeMillis();
2624 Runnable r;
2625
2626 // Don't use these two variables in any of the callback runnables.
2627 // Otherwise we hold a reference to them.
2628 final Callbacks oldCallbacks = mCallbacks.get();
2629 if (oldCallbacks == null) {
2630 // This launcher has exited and nobody bothered to tell us. Just bail.
2631 Log.w(TAG, "LoaderTask running with no launcher");
2632 return;
2633 }
2634
Winson Chung9b9fb962013-11-15 15:39:34 -08002635 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002636 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2637 ArrayList<LauncherAppWidgetInfo> appWidgets =
2638 new ArrayList<LauncherAppWidgetInfo>();
2639 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2640 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002641 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002642 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002643 workspaceItems.addAll(sBgWorkspaceItems);
2644 appWidgets.addAll(sBgAppWidgets);
2645 folders.putAll(sBgFolders);
2646 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002647 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002648 }
2649
Derek Prothro7aff3992013-12-10 14:00:37 -05002650 final boolean isLoadingSynchronously =
2651 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002652 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002653 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002654 if (currScreen >= orderedScreenIds.size()) {
2655 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002656 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002657 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002658 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002659 final long currentScreenId = currentScreen < 0
2660 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002661
2662 // Load all the items that are on the current page first (and in the process, unbind
2663 // all the existing workspace items before we call startBinding() below.
2664 unbindWorkspaceItemsOnMainThread();
2665
2666 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002667 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2668 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2669 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2670 new ArrayList<LauncherAppWidgetInfo>();
2671 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2672 new ArrayList<LauncherAppWidgetInfo>();
2673 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2674 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2675
Winson Chung9b9fb962013-11-15 15:39:34 -08002676 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002677 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002678 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002679 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002680 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002681 otherFolders);
2682 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2683 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2684
2685 // Tell the workspace that we're about to start binding items
2686 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002687 public void run() {
2688 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2689 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002690 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002691 }
2692 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002693 };
Winson Chung81b52252012-08-27 15:34:29 -07002694 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002695
Adam Cohendcd297f2013-06-18 13:13:40 -07002696 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2697
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002698 // Load items on the current page
2699 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2700 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002701 if (isLoadingSynchronously) {
2702 r = new Runnable() {
2703 public void run() {
2704 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002705 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002706 callbacks.onPageBoundSynchronously(currentScreen);
2707 }
2708 }
2709 };
Winson Chung81b52252012-08-27 15:34:29 -07002710 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002711 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002712
Winson Chung4a2afa32012-07-19 14:53:05 -07002713 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2714 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002715 synchronized (mDeferredBindRunnables) {
2716 mDeferredBindRunnables.clear();
2717 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002718 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002719 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002720
2721 // Tell the workspace that we're done binding items
2722 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002723 public void run() {
2724 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2725 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002726 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002727 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002728
Winson Chung98e030b2012-05-07 16:01:11 -07002729 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002730 if (DEBUG_LOADERS) {
2731 Log.d(TAG, "bound workspace in "
2732 + (SystemClock.uptimeMillis()-t) + "ms");
2733 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002734
2735 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002736 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002737 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002738 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002739 synchronized (mDeferredBindRunnables) {
2740 mDeferredBindRunnables.add(r);
2741 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002742 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002743 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002744 }
Joe Onorato36115782010-06-17 13:28:48 -04002745 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002746
Joe Onorato36115782010-06-17 13:28:48 -04002747 private void loadAndBindAllApps() {
2748 if (DEBUG_LOADERS) {
2749 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2750 }
2751 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002752 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002753 synchronized (LoaderTask.this) {
2754 if (mStopped) {
2755 return;
2756 }
2757 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002758 }
Joe Onorato36115782010-06-17 13:28:48 -04002759 } else {
2760 onlyBindAllApps();
2761 }
2762 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002763
Joe Onorato36115782010-06-17 13:28:48 -04002764 private void onlyBindAllApps() {
2765 final Callbacks oldCallbacks = mCallbacks.get();
2766 if (oldCallbacks == null) {
2767 // This launcher has exited and nobody bothered to tell us. Just bail.
2768 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2769 return;
2770 }
2771
2772 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002773 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002774 final ArrayList<AppInfo> list
2775 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002776 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002777 public void run() {
2778 final long t = SystemClock.uptimeMillis();
2779 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2780 if (callbacks != null) {
2781 callbacks.bindAllApplications(list);
2782 }
2783 if (DEBUG_LOADERS) {
2784 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2785 + (SystemClock.uptimeMillis()-t) + "ms");
2786 }
2787 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002788 };
2789 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002790 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002791 r.run();
2792 } else {
2793 mHandler.post(r);
2794 }
Joe Onorato36115782010-06-17 13:28:48 -04002795 }
2796
Winson Chung64359a52013-07-08 17:17:08 -07002797 private void loadAllApps() {
2798 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002799
Joe Onorato36115782010-06-17 13:28:48 -04002800 final Callbacks oldCallbacks = mCallbacks.get();
2801 if (oldCallbacks == null) {
2802 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002803 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002804 return;
2805 }
2806
2807 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2808 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2809
Kenny Guyed131872014-04-30 03:02:21 +01002810 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2811
Winson Chung64359a52013-07-08 17:17:08 -07002812 // Clear the list of apps
2813 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002814 for (UserHandleCompat user : profiles) {
2815 // Query for the set of apps
2816 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2817 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2818 if (DEBUG_LOADERS) {
2819 Log.d(TAG, "getActivityList took "
2820 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2821 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2822 }
2823 // Fail if we don't have any apps
2824 if (apps == null || apps.isEmpty()) {
2825 return;
2826 }
2827 // Sort the applications by name
2828 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2829 Collections.sort(apps,
2830 new LauncherModel.ShortcutNameComparator(mLabelCache));
2831 if (DEBUG_LOADERS) {
2832 Log.d(TAG, "sort took "
2833 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2834 }
Joe Onorato36115782010-06-17 13:28:48 -04002835
Kenny Guyed131872014-04-30 03:02:21 +01002836 // Create the ApplicationInfos
2837 for (int i = 0; i < apps.size(); i++) {
2838 LauncherActivityInfoCompat app = apps.get(i);
2839 // This builds the icon bitmaps.
2840 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, mLabelCache));
2841 }
Winson Chung64359a52013-07-08 17:17:08 -07002842 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002843 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002844 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2845 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002846
2847 // Post callback on main thread
2848 mHandler.post(new Runnable() {
2849 public void run() {
2850 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002851 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002852 if (callbacks != null) {
2853 callbacks.bindAllApplications(added);
2854 if (DEBUG_LOADERS) {
2855 Log.d(TAG, "bound " + added.size() + " apps in "
2856 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2857 }
2858 } else {
2859 Log.i(TAG, "not binding apps: no Launcher activity");
2860 }
2861 }
2862 });
2863
Joe Onorato36115782010-06-17 13:28:48 -04002864 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002865 Log.d(TAG, "Icons processed in "
2866 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002867 }
2868 }
2869
2870 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002871 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002872 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2873 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2874 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2875 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2876 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2877 }
Joe Onorato36115782010-06-17 13:28:48 -04002878 }
2879 }
2880
2881 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002882 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002883 }
2884
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002885 private class AppsAvailabilityCheck extends BroadcastReceiver {
2886
2887 @Override
2888 public void onReceive(Context context, Intent intent) {
2889 synchronized (sBgLock) {
2890 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2891 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002892 final PackageManager manager = context.getPackageManager();
2893 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2894 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002895 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2896 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002897 packagesRemoved.clear();
2898 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002899 for (String pkg : entry.getValue()) {
2900 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002901 boolean packageOnSdcard = launcherApps.isAppEnabled(
2902 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2903 if (packageOnSdcard) {
2904 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2905 packagesUnavailable.add(pkg);
2906 } else {
2907 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2908 packagesRemoved.add(pkg);
2909 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002910 }
2911 }
2912 if (!packagesRemoved.isEmpty()) {
2913 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2914 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2915 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002916 if (!packagesUnavailable.isEmpty()) {
2917 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2918 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2919 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002920 }
Sunny Goyal34942622014-08-29 17:20:55 -07002921 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002922 }
2923 }
2924 }
2925
Joe Onorato36115782010-06-17 13:28:48 -04002926 private class PackageUpdatedTask implements Runnable {
2927 int mOp;
2928 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002929 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002930
2931 public static final int OP_NONE = 0;
2932 public static final int OP_ADD = 1;
2933 public static final int OP_UPDATE = 2;
2934 public static final int OP_REMOVE = 3; // uninstlled
2935 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2936
2937
Kenny Guyed131872014-04-30 03:02:21 +01002938 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002939 mOp = op;
2940 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002941 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002942 }
2943
2944 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002945 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002946
2947 final String[] packages = mPackages;
2948 final int N = packages.length;
2949 switch (mOp) {
2950 case OP_ADD:
2951 for (int i=0; i<N; i++) {
2952 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002953 mIconCache.remove(packages[i], mUser);
2954 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002955 }
2956 break;
2957 case OP_UPDATE:
2958 for (int i=0; i<N; i++) {
2959 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002960 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002961 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002962 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002963 }
2964 break;
2965 case OP_REMOVE:
2966 case OP_UNAVAILABLE:
Sunny Goyal1a745e82014-10-02 15:58:31 -07002967 boolean clearCache = mOp == OP_REMOVE;
Joe Onorato36115782010-06-17 13:28:48 -04002968 for (int i=0; i<N; i++) {
2969 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002970 mBgAllAppsList.removePackage(packages[i], mUser, clearCache);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002971 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002972 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002973 }
2974 break;
2975 }
2976
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002977 ArrayList<AppInfo> added = null;
2978 ArrayList<AppInfo> modified = null;
2979 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002980
Adam Cohen487f7dd2012-06-28 18:12:10 -07002981 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002982 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002983 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002984 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002985 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002986 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002987 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002988 }
Winson Chung5d55f332012-07-16 20:45:03 -07002989 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002990 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002991 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002992 }
2993
Joe Onorato36115782010-06-17 13:28:48 -04002994 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2995 if (callbacks == null) {
2996 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2997 return;
2998 }
2999
Sunny Goyal4390ace2014-10-13 11:33:11 -07003000 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3001 new HashMap<ComponentName, AppInfo>();
3002
Joe Onorato36115782010-06-17 13:28:48 -04003003 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07003004 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08003005 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07003006 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08003007 addAndBindAddedWorkspaceApps(context, addedInfos);
3008 } else {
3009 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07003010 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003011 for (AppInfo ai : added) {
3012 addedOrUpdatedApps.put(ai.componentName, ai);
3013 }
Joe Onorato36115782010-06-17 13:28:48 -04003014 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003015
Joe Onorato36115782010-06-17 13:28:48 -04003016 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003017 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003018 for (AppInfo ai : modified) {
3019 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003020 }
3021
Joe Onorato36115782010-06-17 13:28:48 -04003022 mHandler.post(new Runnable() {
3023 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07003024 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
3025 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003026 callbacks.bindAppsUpdated(modifiedFinal);
3027 }
3028 }
3029 });
3030 }
Winson Chung83892cc2013-05-01 16:53:33 -07003031
Sunny Goyal4390ace2014-10-13 11:33:11 -07003032 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003033 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003034 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3035 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3036 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3037
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003038 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003039 synchronized (sBgLock) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003040 for (ItemInfo info : sBgItemsIdMap.values()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003041 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3042 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003043 boolean infoUpdated = false;
3044 boolean shortcutUpdated = false;
3045
3046 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003047 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003048 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003049 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3050 si.iconResource.resourceName, mIconCache, context);
3051 if (icon != null) {
3052 si.setIcon(icon);
3053 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003054 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003055 }
3056 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003057
3058 ComponentName cn = si.getTargetComponent();
3059 if (cn != null && packageSet.contains(cn.getPackageName())) {
3060 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3061
3062 if (si.isPromise()) {
3063 mIconCache.deletePreloadedIcon(cn, mUser);
3064 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3065 // Auto install icon
3066 PackageManager pm = context.getPackageManager();
3067 ResolveInfo matched = pm.resolveActivity(
3068 new Intent(Intent.ACTION_MAIN)
3069 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3070 PackageManager.MATCH_DEFAULT_ONLY);
3071 if (matched == null) {
3072 // Try to find the best match activity.
3073 Intent intent = pm.getLaunchIntentForPackage(
3074 cn.getPackageName());
3075 if (intent != null) {
3076 cn = intent.getComponent();
3077 appInfo = addedOrUpdatedApps.get(cn);
3078 }
3079
3080 if ((intent == null) || (appInfo == null)) {
3081 removedShortcuts.add(si);
3082 continue;
3083 }
3084 si.promisedIntent = intent;
3085 }
3086 }
3087
3088 // Restore the shortcut.
3089 si.intent = si.promisedIntent;
3090 si.promisedIntent = null;
3091 si.status &= ~ShortcutInfo.FLAG_RESTORED_ICON
3092 & ~ShortcutInfo.FLAG_AUTOINTALL_ICON
3093 & ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
3094
3095 infoUpdated = true;
3096 si.updateIcon(mIconCache);
3097 }
3098
3099 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3100 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3101 si.updateIcon(mIconCache);
3102 si.title = appInfo.title.toString();
3103 si.contentDescription = appInfo.contentDescription;
3104 infoUpdated = true;
3105 }
3106
3107 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3108 // Since package was just updated, the target must be available now.
3109 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3110 shortcutUpdated = true;
3111 }
3112 }
3113
3114 if (infoUpdated || shortcutUpdated) {
3115 updatedShortcuts.add(si);
3116 }
3117 if (infoUpdated) {
3118 updateItemInDatabase(context, si);
3119 }
3120 } else if (info instanceof LauncherAppWidgetInfo) {
3121 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3122 if (mUser.equals(widgetInfo.user)
3123 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3124 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3125 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3126 widgets.add(widgetInfo);
3127 updateItemInDatabase(context, widgetInfo);
3128 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003129 }
3130 }
3131 }
3132
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3134 mHandler.post(new Runnable() {
3135
3136 public void run() {
3137 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
3138 if (callbacks == cb && cb != null) {
3139 callbacks.bindShortcutsChanged(
3140 updatedShortcuts, removedShortcuts, mUser);
3141 }
3142 }
3143 });
3144 if (!removedShortcuts.isEmpty()) {
3145 deleteItemsFromDatabase(context, removedShortcuts);
3146 }
3147 }
3148 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003149 mHandler.post(new Runnable() {
3150 public void run() {
3151 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
3152 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003153 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003154 }
3155 }
3156 });
3157 }
3158 }
3159
Winson Chungdf95eb12013-10-16 14:57:07 -07003160 final ArrayList<String> removedPackageNames =
3161 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003162 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003163 // Mark all packages in the broadcast to be removed
3164 removedPackageNames.addAll(Arrays.asList(packages));
3165 } else if (mOp == OP_UPDATE) {
3166 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003167 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003168 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003169 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003170 }
3171 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003172 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003173
Winson Chungdf95eb12013-10-16 14:57:07 -07003174 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003175 final int removeReason;
3176 if (mOp == OP_UNAVAILABLE) {
3177 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3178 } else {
3179 // Remove all the components associated with this package
3180 for (String pn : removedPackageNames) {
3181 deletePackageFromDatabase(context, pn, mUser);
3182 }
3183 // Remove all the specific components
3184 for (AppInfo a : removedApps) {
3185 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3186 deleteItemsFromDatabase(context, infos);
3187 }
3188 removeReason = 0;
3189 }
3190
Winson Chungdf95eb12013-10-16 14:57:07 -07003191 // Remove any queued items from the install queue
3192 String spKey = LauncherAppState.getSharedPreferencesKey();
3193 SharedPreferences sp =
3194 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
3195 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
3196 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003197 mHandler.post(new Runnable() {
3198 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07003199 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
3200 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003201 callbacks.bindComponentsRemoved(
3202 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003203 }
3204 }
3205 });
Joe Onoratobe386092009-11-17 17:32:16 -08003206 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003207
Michael Jurkac402cd92013-05-20 15:49:32 +02003208 final ArrayList<Object> widgetsAndShortcuts =
Kenny Guyed131872014-04-30 03:02:21 +01003209 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07003210 mHandler.post(new Runnable() {
3211 @Override
3212 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07003213 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
3214 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003215 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07003216 }
3217 }
3218 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003219
3220 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003221 mHandler.post(new Runnable() {
3222 public void run() {
3223 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
3224 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003225 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003226 }
3227 }
3228 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003229 }
3230 }
3231
Michael Jurkac402cd92013-05-20 15:49:32 +02003232 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
3233 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
3234 PackageManager packageManager = context.getPackageManager();
3235 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003236 widgetsAndShortcuts.addAll(AppWidgetManagerCompat.getInstance(context).getAllProviders());
3237
Michael Jurkac402cd92013-05-20 15:49:32 +02003238 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3239 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Sunny Goyalffe83f12014-08-14 17:39:34 -07003240 Collections.sort(widgetsAndShortcuts, new WidgetAndShortcutNameComparator(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003241 return widgetsAndShortcuts;
3242 }
3243
Kenny Guyed131872014-04-30 03:02:21 +01003244 private static boolean isPackageDisabled(Context context, String packageName,
3245 UserHandleCompat user) {
3246 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3247 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 }
Adam Cohen556f6132014-01-15 15:18:08 -08003249
Kenny Guyed131872014-04-30 03:02:21 +01003250 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3251 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003252 if (cn == null) {
3253 return false;
3254 }
Kenny Guyed131872014-04-30 03:02:21 +01003255 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3256 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003257 return false;
3258 }
Kenny Guyed131872014-04-30 03:02:21 +01003259 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003260 }
3261
Adam Cohena28b78e2014-05-20 17:03:04 -07003262 public static boolean isValidPackage(Context context, String packageName,
3263 UserHandleCompat user) {
3264 if (packageName == null) {
3265 return false;
3266 }
3267 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3268 return launcherApps.isPackageEnabledForProfile(packageName, user);
3269 }
3270
Joe Onorato9c1289c2009-08-17 11:03:03 -04003271 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003272 * Make an ShortcutInfo object for a restored application or shortcut item that points
3273 * to a package that is not yet installed on the system.
3274 */
Sunny Goyal34942622014-08-29 17:20:55 -07003275 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
3276 int promiseType) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003277 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003278 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34942622014-08-29 17:20:55 -07003279 mIconCache.getTitleAndIcon(info, intent, info.user, true);
3280
3281 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3282 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3283 if (!TextUtils.isEmpty(title)) {
3284 info.title = title;
3285 }
3286 info.status = ShortcutInfo.FLAG_RESTORED_ICON;
3287 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3288 if (TextUtils.isEmpty(info.title)) {
3289 info.title = (cursor != null) ? cursor.getString(titleIndex) : "";
3290 }
3291 info.status = ShortcutInfo.FLAG_AUTOINTALL_ICON;
3292 } else {
3293 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3294 }
3295
Kenny Guyc2bd8102014-06-30 12:30:31 +01003296 info.contentDescription = mUserManager.getBadgedLabelForUser(
3297 info.title.toString(), info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003298 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003299 info.promisedIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05003300 return info;
3301 }
3302
3303 /**
3304 * Make an Intent object for a restored application or shortcut item that points
3305 * to the market page for the item.
3306 */
3307 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
3308 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003309 return getMarketIntent(componentName.getPackageName());
3310 }
3311
3312 static Intent getMarketIntent(String packageName) {
3313 return new Intent(Intent.ACTION_VIEW)
3314 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003315 .scheme("market")
3316 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003317 .appendQueryParameter("id", packageName)
3318 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003319 }
3320
3321 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003322 * This is called from the code that adds shortcuts from the intent receiver. This
3323 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 */
Kenny Guyed131872014-04-30 03:02:21 +01003325 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3326 UserHandleCompat user, Context context) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003327 return getShortcutInfo(manager, intent, user, context, null, -1, -1, null, false);
Joe Onorato56d82912010-03-07 14:32:10 -05003328 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329
Joe Onorato56d82912010-03-07 14:32:10 -05003330 /**
3331 * Make an ShortcutInfo object for a shortcut that is an application.
3332 *
3333 * If c is not null, then it will be used to fill in missing data like the title and icon.
3334 */
Kenny Guyed131872014-04-30 03:02:21 +01003335 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3336 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003337 HashMap<Object, CharSequence> labelCache, boolean allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003338 if (user == null) {
3339 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003340 return null;
3341 }
3342
Kenny Guyed131872014-04-30 03:02:21 +01003343 ComponentName componentName = intent.getComponent();
3344 if (componentName == null) {
3345 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3346 return null;
3347 }
3348
3349 Intent newIntent = new Intent(intent.getAction(), null);
3350 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3351 newIntent.setComponent(componentName);
3352 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003353 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003354 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3355 return null;
3356 }
3357
3358 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003359
Joe Onorato56d82912010-03-07 14:32:10 -05003360 // the resource -- This may implicitly give us back the fallback icon,
3361 // but don't worry about that. All we're doing with usingFallbackIcon is
3362 // to avoid saving lots of copies of that in the database, and most apps
3363 // have icons anyway.
Kenny Guyed131872014-04-30 03:02:21 +01003364 Bitmap icon = mIconCache.getIcon(componentName, lai, labelCache);
Winson Chungc208ff92012-03-29 17:37:41 -07003365
Joe Onorato56d82912010-03-07 14:32:10 -05003366 // the db
3367 if (icon == null) {
3368 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003369 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003370 }
3371 }
3372 // the fallback icon
3373 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003374 icon = mIconCache.getDefaultIcon(user);
Joe Onorato56d82912010-03-07 14:32:10 -05003375 info.usingFallbackIcon = true;
3376 }
3377 info.setIcon(icon);
3378
Kenny Guyed131872014-04-30 03:02:21 +01003379 // From the cache.
3380 if (labelCache != null) {
3381 info.title = labelCache.get(componentName);
3382 }
3383
Joe Onorato56d82912010-03-07 14:32:10 -05003384 // from the resource
Kenny Guyed131872014-04-30 03:02:21 +01003385 if (info.title == null && lai != null) {
3386 info.title = lai.getLabel();
3387 if (labelCache != null) {
3388 labelCache.put(componentName, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003389 }
Joe Onorato56d82912010-03-07 14:32:10 -05003390 }
3391 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003393 if (c != null) {
3394 info.title = c.getString(titleIndex);
3395 }
3396 }
3397 // fall back to the class name of the activity
3398 if (info.title == null) {
3399 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003402 info.user = user;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003403 info.contentDescription = mUserManager.getBadgedLabelForUser(
3404 info.title.toString(), info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003406 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003407
Winson Chung64359a52013-07-08 17:17:08 -07003408 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3409 ItemInfoFilter f) {
3410 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3411 for (ItemInfo i : infos) {
3412 if (i instanceof ShortcutInfo) {
3413 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003414 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003415 if (cn != null && f.filterItem(null, info, cn)) {
3416 filtered.add(info);
3417 }
3418 } else if (i instanceof FolderInfo) {
3419 FolderInfo info = (FolderInfo) i;
3420 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003421 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003422 if (cn != null && f.filterItem(info, s, cn)) {
3423 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003424 }
3425 }
Winson Chung64359a52013-07-08 17:17:08 -07003426 } else if (i instanceof LauncherAppWidgetInfo) {
3427 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3428 ComponentName cn = info.providerName;
3429 if (cn != null && f.filterItem(null, info, cn)) {
3430 filtered.add(info);
3431 }
Winson Chung8a435102012-08-30 17:16:53 -07003432 }
3433 }
Winson Chung64359a52013-07-08 17:17:08 -07003434 return new ArrayList<ItemInfo>(filtered);
3435 }
3436
Kenny Guyed131872014-04-30 03:02:21 +01003437 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
3438 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003439 ItemInfoFilter filter = new ItemInfoFilter() {
3440 @Override
3441 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003442 if (info.user == null) {
3443 return cn.equals(cname);
3444 } else {
3445 return cn.equals(cname) && info.user.equals(user);
3446 }
Winson Chung64359a52013-07-08 17:17:08 -07003447 }
3448 };
3449 return filterItemInfos(sBgItemsIdMap.values(), filter);
3450 }
3451
Sunny Goyal1a745e82014-10-02 15:58:31 -07003452 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003453 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003454 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003455 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003456 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3457 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003458
Joe Onorato56d82912010-03-07 14:32:10 -05003459 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003460 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003461 // Non-app shortcuts are only supported for current user.
3462 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003464
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003465 // TODO: If there's an explicit component and we can't install that, delete it.
3466
Joe Onorato56d82912010-03-07 14:32:10 -05003467 info.title = c.getString(titleIndex);
3468
Joe Onorato9c1289c2009-08-17 11:03:03 -04003469 int iconType = c.getInt(iconTypeIndex);
3470 switch (iconType) {
3471 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3472 String packageName = c.getString(iconPackageIndex);
3473 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003474 info.customIcon = false;
3475 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003476 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003477 // the db
3478 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003479 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003480 }
3481 // the fallback icon
3482 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003483 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003484 info.usingFallbackIcon = true;
3485 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 break;
3487 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003488 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003489 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003490 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003491 info.customIcon = false;
3492 info.usingFallbackIcon = true;
3493 } else {
3494 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 break;
3497 default:
Kenny Guyed131872014-04-30 03:02:21 +01003498 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003499 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 info.customIcon = false;
3501 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003502 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003503 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 return info;
3505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003506
Michael Jurka931dc972011-08-05 15:08:15 -07003507 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003508 @SuppressWarnings("all") // suppress dead code warning
3509 final boolean debug = false;
3510 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003511 Log.d(TAG, "getIconFromCursor app="
3512 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3513 }
3514 byte[] data = c.getBlob(iconIndex);
3515 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003516 return Utilities.createIconBitmap(
3517 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003518 } catch (Exception e) {
3519 return null;
3520 }
3521 }
3522
Winson Chunga9abd0e2010-10-27 17:18:37 -07003523 /**
Winson Chung55cef262010-10-28 14:14:18 -07003524 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3525 */
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003526 static AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
Winson Chung55cef262010-10-28 14:14:18 -07003527 ComponentName component) {
3528 List<AppWidgetProviderInfo> widgets =
3529 AppWidgetManager.getInstance(context).getInstalledProviders();
3530 for (AppWidgetProviderInfo info : widgets) {
3531 if (info.provider.equals(component)) {
3532 return info;
3533 }
3534 }
3535 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003536 }
3537
Sunny Goyal2350bc92014-10-14 16:42:54 -07003538 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003539 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3540 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3541 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3542
Adam Cohend9198822011-11-22 16:42:47 -08003543 if (intent == null) {
3544 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3545 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3546 return null;
3547 }
3548
Joe Onorato0589f0f2010-02-08 13:44:00 -08003549 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003550 boolean customIcon = false;
3551 ShortcutIconResource iconResource = null;
3552
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003553 if (bitmap instanceof Bitmap) {
3554 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003555 customIcon = true;
3556 } else {
3557 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003558 if (extra instanceof ShortcutIconResource) {
3559 iconResource = (ShortcutIconResource) extra;
3560 icon = Utilities.createIconBitmap(iconResource.packageName,
3561 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003562 }
3563 }
3564
Michael Jurkac9d95c52011-08-29 14:03:34 -07003565 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003566
Kenny Guyed131872014-04-30 03:02:21 +01003567 // Only support intents for current user for now. Intents sent from other
3568 // users wouldn't get here without intent forwarding anyway.
3569 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003570 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003571 icon = mIconCache.getDefaultIcon(info.user);
3572 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003573 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003574 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003575
Joe Onorato0589f0f2010-02-08 13:44:00 -08003576 info.title = name;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003577 info.contentDescription = mUserManager.getBadgedLabelForUser(
3578 info.title.toString(), info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003579 info.intent = intent;
3580 info.customIcon = customIcon;
3581 info.iconResource = iconResource;
3582
3583 return info;
3584 }
3585
Winson Chungaac01e12011-08-17 10:37:13 -07003586 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3587 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003588 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003589 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003590 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003591 }
Joe Onorato56d82912010-03-07 14:32:10 -05003592 // If this icon doesn't have a custom icon, check to see
3593 // what's stored in the DB, and if it doesn't match what
3594 // we're going to show, store what we are going to show back
3595 // into the DB. We do this so when we're loading, if the
3596 // package manager can't find an icon (for example because
3597 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003598 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003599 cache.put(info, c.getBlob(iconIndex));
3600 return true;
3601 }
3602 return false;
3603 }
3604 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3605 boolean needSave = false;
3606 try {
3607 if (data != null) {
3608 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3609 Bitmap loaded = info.getIcon(mIconCache);
3610 needSave = !saved.sameAs(loaded);
3611 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003612 needSave = true;
3613 }
Winson Chungaac01e12011-08-17 10:37:13 -07003614 } catch (Exception e) {
3615 needSave = true;
3616 }
3617 if (needSave) {
3618 Log.d(TAG, "going to save icon bitmap for info=" + info);
3619 // This is slower than is ideal, but this only happens once
3620 // or when the app is updated with a new icon.
3621 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003622 }
3623 }
3624
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003626 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 * or make a new one.
3628 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003629 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 // See if a placeholder was created for us already
3631 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003632 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003634 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003635 folders.put(id, folderInfo);
3636 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003637 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638 }
3639
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003640 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003641 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003642 return new Comparator<AppInfo>() {
3643 public final int compare(AppInfo a, AppInfo b) {
Kenny Guyed131872014-04-30 03:02:21 +01003644 if (a.user.equals(b.user)) {
3645 int result = collator.compare(a.title.toString().trim(),
3646 b.title.toString().trim());
3647 if (result == 0) {
3648 result = a.componentName.compareTo(b.componentName);
3649 }
3650 return result;
3651 } else {
3652 // TODO Need to figure out rules for sorting
3653 // profiles, this puts work second.
3654 return a.user.toString().compareTo(b.user.toString());
Winson Chung11904872012-09-17 16:58:46 -07003655 }
Michael Jurka5b1808d2011-07-11 19:59:46 -07003656 }
Winson Chung11904872012-09-17 16:58:46 -07003657 };
3658 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003659 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3660 = new Comparator<AppInfo>() {
3661 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003662 if (a.firstInstallTime < b.firstInstallTime) return 1;
3663 if (a.firstInstallTime > b.firstInstallTime) return -1;
3664 return 0;
3665 }
3666 };
Winson Chung5308f242011-08-18 12:12:41 -07003667 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3668 if (info.activityInfo != null) {
3669 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3670 } else {
3671 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3672 }
3673 }
Kenny Guyed131872014-04-30 03:02:21 +01003674 public static class ShortcutNameComparator implements Comparator<LauncherActivityInfoCompat> {
Winson Chung11904872012-09-17 16:58:46 -07003675 private Collator mCollator;
Winson Chungc3eecff2011-07-11 17:44:15 -07003676 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003677 ShortcutNameComparator(PackageManager pm) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003678 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003679 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003680 }
Kenny Guyed131872014-04-30 03:02:21 +01003681 ShortcutNameComparator(HashMap<Object, CharSequence> labelCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003682 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003683 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003684 }
Kenny Guyed131872014-04-30 03:02:21 +01003685 public final int compare(LauncherActivityInfoCompat a, LauncherActivityInfoCompat b) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003686 String labelA, labelB;
Kenny Guyed131872014-04-30 03:02:21 +01003687 ComponentName keyA = a.getComponentName();
3688 ComponentName keyB = b.getComponentName();
Winson Chung5308f242011-08-18 12:12:41 -07003689 if (mLabelCache.containsKey(keyA)) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003690 labelA = mLabelCache.get(keyA).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003691 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003692 labelA = a.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003693
Winson Chung5308f242011-08-18 12:12:41 -07003694 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003695 }
Winson Chung5308f242011-08-18 12:12:41 -07003696 if (mLabelCache.containsKey(keyB)) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003697 labelB = mLabelCache.get(keyB).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003698 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003699 labelB = b.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003700
Winson Chung5308f242011-08-18 12:12:41 -07003701 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003702 }
Winson Chung11904872012-09-17 16:58:46 -07003703 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003704 }
3705 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003706 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003707 private final AppWidgetManagerCompat mManager;
3708 private final PackageManager mPackageManager;
3709 private final HashMap<Object, String> mLabelCache;
3710 private final Collator mCollator;
3711
3712 WidgetAndShortcutNameComparator(Context context) {
3713 mManager = AppWidgetManagerCompat.getInstance(context);
3714 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003715 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003716 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003717 }
3718 public final int compare(Object a, Object b) {
3719 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003720 if (mLabelCache.containsKey(a)) {
3721 labelA = mLabelCache.get(a);
3722 } else {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003723 labelA = (a instanceof AppWidgetProviderInfo)
3724 ? mManager.loadLabel((AppWidgetProviderInfo) a)
3725 : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003726 mLabelCache.put(a, labelA);
3727 }
3728 if (mLabelCache.containsKey(b)) {
3729 labelB = mLabelCache.get(b);
3730 } else {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003731 labelB = (b instanceof AppWidgetProviderInfo)
3732 ? mManager.loadLabel((AppWidgetProviderInfo) b)
3733 : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003734 mLabelCache.put(b, labelB);
3735 }
Winson Chung11904872012-09-17 16:58:46 -07003736 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003737 }
3738 };
Joe Onoratobe386092009-11-17 17:32:16 -08003739
Sunny Goyal651077b2014-06-30 14:15:31 -07003740 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3741 return (provider != null) && (provider.provider != null)
3742 && (provider.provider.getPackageName() != null);
3743 }
3744
Joe Onoratobe386092009-11-17 17:32:16 -08003745 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003746 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003747 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3748 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3749 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3750 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003751 if (mLoaderTask != null) {
3752 mLoaderTask.dumpState();
3753 } else {
3754 Log.d(TAG, "mLoaderTask=null");
3755 }
Joe Onoratobe386092009-11-17 17:32:16 -08003756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003757}