blob: ff8a3564777cdb7b8759c5c3615288480b0ccfe9 [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;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Daniel Sandler325dc232013-06-05 22:57:57 -040047import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070053import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070054import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070055import java.util.Collections;
56import java.util.Comparator;
57import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070059import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070062import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070063import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Maintains in-memory state of the Launcher. It is expected that there should be only one
67 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070068 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040070public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080071 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070073
Daniel Sandler8707e0f2013-08-15 15:54:18 -070074 // true = use a "More Apps" folder for non-workspace apps on upgrade
75 // false = strew non-workspace apps across the workspace on upgrade
76 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
77
Dan Sandlerd5024042014-01-09 15:01:33 -050078 public static final int LOADER_FLAG_NONE = 0;
79 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
80 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
81
Joe Onorato36115782010-06-17 13:28:48 -040082 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050083 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080084
Winson Chungee055712013-07-30 14:46:24 -070085 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -080086 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -040087
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private final Object mLock = new Object();
90 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040091 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020093 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Winson Chung81b52252012-08-27 15:34:29 -070095 // Specific runnable types that are run on the main thread deferred handler, this allows us to
96 // clear all queued binding runnables when the Launcher activity is destroyed.
97 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
98 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
99
100
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700101 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
102 static {
103 sWorkerThread.start();
104 }
105 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
106
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 // We start off with everything not loaded. After that, we assume that
108 // our monitoring of the package manager provides all updates and we never
109 // need to do a requery. These are only ever touched from the loader thread.
110 private boolean mWorkspaceLoaded;
111 private boolean mAllAppsLoaded;
112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // When we are loading pages synchronously, we can't just post the binding of items on the side
114 // pages as this delays the rotation process. Instead, we wait for a callback from the first
115 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
116 // a normal load, we also clear this set of Runnables.
117 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700121 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700122 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800123
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700124 // The lock that must be acquired before referencing any static bg data structures. Unlike
125 // other locks, this one can generally be held long-term because we never expect any of these
126 // static data structures to be referenced outside of the worker thread except on the first
127 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700128 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
135 // created by LauncherModel that are directly on the home screen (however, no widgets or
136 // shortcuts within folders).
137 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138
Adam Cohen487f7dd2012-06-28 18:12:10 -0700139 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
140 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 new ArrayList<LauncherAppWidgetInfo>();
142
Adam Cohen487f7dd2012-06-28 18:12:10 -0700143 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
144 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
147 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700148
149 // sBgWorkspaceScreens is the ordered set of workspace screens.
150 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
151
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // </ only access in worker thread >
153
154 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800155 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Reena Lee99a73f32011-10-24 17:27:37 -0700157 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700160 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public int getCurrentWorkspaceScreen();
162 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700163 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
164 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700166 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500167 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700168 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200170 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700171 public void bindAppsAdded(ArrayList<Long> newScreens,
172 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700173 ArrayList<ItemInfo> addAnimated,
174 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200175 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700176 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700177 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200178 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100179 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700180 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700181 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700182 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Winson Chung64359a52013-07-08 17:17:08 -0700185 public interface ItemInfoFilter {
186 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
187 }
188
Bjorn Bringert1307f632013-10-03 22:31:03 +0100189 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800190 Context context = app.getContext();
191 ContentResolver contentResolver = context.getContentResolver();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400192
Winson Chungee055712013-07-30 14:46:24 -0700193 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Winson Chunga6945242014-01-08 14:04:34 -0800194 mOldContentProviderExists = (contentResolver.acquireContentProviderClient(
195 LauncherSettings.Favorites.OLD_CONTENT_URI) != null);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400196 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100197 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800198 mIconCache = iconCache;
199
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400200 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700201 Configuration config = res.getConfiguration();
202 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 }
204
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700205 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
206 * posted on the main thread handler. */
207 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700208 runOnMainThread(r, 0);
209 }
210 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700211 if (sWorkerThread.getThreadId() == Process.myTid()) {
212 // If we are on the worker thread, post onto the main handler
213 mHandler.post(r);
214 } else {
215 r.run();
216 }
217 }
218
219 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
220 * posted on the worker thread handler. */
221 private static void runOnWorkerThread(Runnable r) {
222 if (sWorkerThread.getThreadId() == Process.myTid()) {
223 r.run();
224 } else {
225 // If we are not on the worker thread, then post to the worker handler
226 sWorker.post(r);
227 }
228 }
229
Winson Chunge43a1e72014-01-15 10:33:02 -0800230 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
231 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800232 }
233
Winson Chungc9168342013-06-26 14:54:55 -0700234 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
235 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700236 LauncherAppState app = LauncherAppState.getInstance();
237 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
238 final int xCount = (int) grid.numColumns;
239 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700240 boolean[][] occupied = new boolean[xCount][yCount];
241
242 int cellX, cellY, spanX, spanY;
243 for (int i = 0; i < items.size(); ++i) {
244 final ItemInfo item = items.get(i);
245 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
246 if (item.screenId == screen) {
247 cellX = item.cellX;
248 cellY = item.cellY;
249 spanX = item.spanX;
250 spanY = item.spanY;
251 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
252 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
253 occupied[x][y] = true;
254 }
255 }
256 }
257 }
258 }
259
260 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
261 }
262 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700263 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700264 int firstScreenIndex,
265 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700266 // Lock on the app so that we don't try and get the items while apps are being added
267 LauncherAppState app = LauncherAppState.getInstance();
268 LauncherModel model = app.getModel();
269 boolean found = false;
270 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700271 if (sWorkerThread.getThreadId() != Process.myTid()) {
272 // Flush the LauncherModel worker thread, so that if we just did another
273 // processInstallShortcut, we give it time for its shortcut to get added to the
274 // database (getItemsInLocalCoordinates reads the database)
275 model.flushWorkerThread();
276 }
Winson Chungc9168342013-06-26 14:54:55 -0700277 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700278
279 // Try adding to the workspace screens incrementally, starting at the default or center
280 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700281 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
282 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700283 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700284 int[] tmpCoordinates = new int[2];
285 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700286 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700287 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700288 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700289 }
290 }
291 }
Winson Chungc9168342013-06-26 14:54:55 -0700292 return null;
293 }
294
Adam Cohen76a47a12014-02-05 11:47:43 -0800295 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
296 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
297
298 if (allAppsApps == null) {
299 throw new RuntimeException("allAppsApps must not be null");
300 }
301 if (allAppsApps.isEmpty()) {
302 return;
303 }
304
Chris Wrenb6d4c282014-01-27 14:17:08 -0500305 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
306 Iterator<AppInfo> iter = allAppsApps.iterator();
307 while (iter.hasNext()) {
308 ItemInfo a = iter.next();
309 if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
310 restoredAppsFinal.add((AppInfo) a);
311 }
312 }
313
Adam Cohen76a47a12014-02-05 11:47:43 -0800314 // Process the newly added applications and add them to the database first
315 Runnable r = new Runnable() {
316 public void run() {
317 runOnMainThread(new Runnable() {
318 public void run() {
319 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
320 if (callbacks == cb && cb != null) {
321 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500322 if (!restoredAppsFinal.isEmpty()) {
323 callbacks.bindAppsUpdated(restoredAppsFinal);
324 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800325 }
326 }
327 });
328 }
329 };
330 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700331 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800332
333 public void addAndBindAddedWorkspaceApps(final Context context,
334 final ArrayList<ItemInfo> workspaceApps) {
335 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
336
337 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800338 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
339 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800340 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700341 return;
Winson Chung997a9232013-07-24 15:33:46 -0700342 }
Winson Chung64359a52013-07-08 17:17:08 -0700343 // Process the newly added applications and add them to the database first
344 Runnable r = new Runnable() {
345 public void run() {
346 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
347 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500348 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700349
Winson Chung76828c82013-08-19 15:43:29 -0700350 // Get the list of workspace screens. We need to append to this list and
351 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
352 // called.
353 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
354 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
355 for (Integer i : orderedScreens.keySet()) {
356 long screenId = orderedScreens.get(i);
357 workspaceScreens.add(screenId);
358 }
359
Winson Chung64359a52013-07-08 17:17:08 -0700360 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700361 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700362 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700363 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700364 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700365 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700366
367 // Short-circuit this logic if the icon exists somewhere on the workspace
368 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500369 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
370 if (a instanceof AppInfo &&
371 LauncherModel.appWasRestored(context, launchIntent)) {
372 restoredAppsFinal.add((AppInfo) a);
373 }
Winson Chung64359a52013-07-08 17:17:08 -0700374 continue;
375 }
376
Winson Chung87412982013-10-03 18:34:14 -0700377 // Add this icon to the db, creating a new page if necessary. If there
378 // is only the empty page then we just add items to the first page.
379 // Otherwise, we add them to the next pages.
380 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700381 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700382 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700383 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200384 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700385
386 // If we can't find a valid position, then just add a new screen.
387 // This takes time so we need to re-queue the add until the new
388 // page is added. Create as many screens as necessary to satisfy
389 // the startSearchPageIndex.
390 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700391 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700392 while (numPagesToAdd > 0) {
393 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700394 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700395 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700396 addedWorkspaceScreensFinal.add(screenId);
397 numPagesToAdd--;
398 }
Winson Chung76828c82013-08-19 15:43:29 -0700399
Winson Chung64359a52013-07-08 17:17:08 -0700400 // Find the coordinate again
401 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700402 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700403 }
404 if (coords == null) {
405 throw new RuntimeException("Coordinates should not be null");
406 }
407
Winson Chung997a9232013-07-24 15:33:46 -0700408 ShortcutInfo shortcutInfo;
409 if (a instanceof ShortcutInfo) {
410 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200411 } else if (a instanceof AppInfo) {
412 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700413 } else {
414 throw new RuntimeException("Unexpected info type");
415 }
Winson Chung94d67682013-09-25 16:29:40 -0700416
Winson Chung64359a52013-07-08 17:17:08 -0700417 // Add the shortcut to the db
418 addItemToDatabase(context, shortcutInfo,
419 LauncherSettings.Favorites.CONTAINER_DESKTOP,
420 coords.first, coords.second[0], coords.second[1], false);
421 // Save the ShortcutInfo for binding in the workspace
422 addedShortcutsFinal.add(shortcutInfo);
423 }
424 }
425
Winson Chung76828c82013-08-19 15:43:29 -0700426 // Update the workspace screens
427 updateWorkspaceScreenOrder(context, workspaceScreens);
428
Adam Cohen76a47a12014-02-05 11:47:43 -0800429 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700430 runOnMainThread(new Runnable() {
431 public void run() {
432 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
433 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700434 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
435 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700436 if (!addedShortcutsFinal.isEmpty()) {
437 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
438 long lastScreenId = info.screenId;
439 for (ItemInfo i : addedShortcutsFinal) {
440 if (i.screenId == lastScreenId) {
441 addAnimated.add(i);
442 } else {
443 addNotAnimated.add(i);
444 }
Winson Chung997a9232013-07-24 15:33:46 -0700445 }
446 }
Winson Chungd64d1762013-08-20 14:37:16 -0700447 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800448 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500449 if (!restoredAppsFinal.isEmpty()) {
450 callbacks.bindAppsUpdated(restoredAppsFinal);
451 }
Winson Chung997a9232013-07-24 15:33:46 -0700452 }
Winson Chung64359a52013-07-08 17:17:08 -0700453 }
Winson Chung997a9232013-07-24 15:33:46 -0700454 });
455 }
Winson Chung64359a52013-07-08 17:17:08 -0700456 }
457 };
458 runOnWorkerThread(r);
459 }
460
Joe Onorato56d82912010-03-07 14:32:10 -0500461 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700462 if (mDefaultIcon == null) {
463 final Context context = LauncherAppState.getInstance().getContext();
464 mDefaultIcon = Utilities.createIconBitmap(
465 mIconCache.getFullResDefaultActivityIcon(), context);
466 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800467 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469
Winson Chung81b52252012-08-27 15:34:29 -0700470 public void unbindItemInfosAndClearQueuedBindRunnables() {
471 if (sWorkerThread.getThreadId() == Process.myTid()) {
472 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
473 "main thread");
474 }
475
476 // Clear any deferred bind runnables
477 mDeferredBindRunnables.clear();
478 // Remove any queued bind runnables
479 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
480 // Unbind all the workspace items
481 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700482 }
483
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700484 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700485 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700486 // Ensure that we don't use the same workspace items data structure on the main thread
487 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700488 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
489 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700490 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700491 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
492 tmpAppWidgets.addAll(sBgAppWidgets);
493 }
494 Runnable r = new Runnable() {
495 @Override
496 public void run() {
497 for (ItemInfo item : tmpWorkspaceItems) {
498 item.unbind();
499 }
500 for (ItemInfo item : tmpAppWidgets) {
501 item.unbind();
502 }
503 }
504 };
505 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700506 }
507
Joe Onorato9c1289c2009-08-17 11:03:03 -0400508 /**
509 * Adds an item to the DB if it was not created previously, or move it to a new
510 * <container, screen, cellX, cellY>
511 */
512 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700513 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 if (item.container == ItemInfo.NO_ID) {
515 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700516 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 } else {
518 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700519 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 }
521 }
522
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700523 static void checkItemInfoLocked(
524 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
525 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
526 if (modelItem != null && item != modelItem) {
527 // check all the data is consistent
528 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
529 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
530 ShortcutInfo shortcut = (ShortcutInfo) item;
531 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
532 modelShortcut.intent.filterEquals(shortcut.intent) &&
533 modelShortcut.id == shortcut.id &&
534 modelShortcut.itemType == shortcut.itemType &&
535 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700536 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700537 modelShortcut.cellX == shortcut.cellX &&
538 modelShortcut.cellY == shortcut.cellY &&
539 modelShortcut.spanX == shortcut.spanX &&
540 modelShortcut.spanY == shortcut.spanY &&
541 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
542 (modelShortcut.dropPos != null &&
543 shortcut.dropPos != null &&
544 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
545 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
546 // For all intents and purposes, this is the same object
547 return;
548 }
549 }
550
551 // the modelItem needs to match up perfectly with item if our model is
552 // to be consistent with the database-- for now, just require
553 // modelItem == item or the equality check above
554 String msg = "item: " + ((item != null) ? item.toString() : "null") +
555 "modelItem: " +
556 ((modelItem != null) ? modelItem.toString() : "null") +
557 "Error: ItemInfo passed to checkItemInfo doesn't match original";
558 RuntimeException e = new RuntimeException(msg);
559 if (stackTrace != null) {
560 e.setStackTrace(stackTrace);
561 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800562 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700563 }
564 }
565
Michael Jurka816474f2012-06-25 14:49:02 -0700566 static void checkItemInfo(final ItemInfo item) {
567 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
568 final long itemId = item.id;
569 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700570 public void run() {
571 synchronized (sBgLock) {
572 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700573 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700574 }
575 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700576 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700577 }
578
Michael Jurkac9d95c52011-08-29 14:03:34 -0700579 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
580 final ItemInfo item, final String callingFunction) {
581 final long itemId = item.id;
582 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
583 final ContentResolver cr = context.getContentResolver();
584
Adam Cohen487f7dd2012-06-28 18:12:10 -0700585 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700586 Runnable r = new Runnable() {
587 public void run() {
588 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700589 updateItemArrays(item, itemId, stackTrace);
590 }
591 };
592 runOnWorkerThread(r);
593 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700594
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700595 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
596 final ArrayList<ItemInfo> items, final String callingFunction) {
597 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700598
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700599 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
600 Runnable r = new Runnable() {
601 public void run() {
602 ArrayList<ContentProviderOperation> ops =
603 new ArrayList<ContentProviderOperation>();
604 int count = items.size();
605 for (int i = 0; i < count; i++) {
606 ItemInfo item = items.get(i);
607 final long itemId = item.id;
608 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
609 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700610
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700611 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
612 updateItemArrays(item, itemId, stackTrace);
613
614 }
615 try {
616 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
617 } catch (Exception e) {
618 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700619 }
620 }
621 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700622 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700623 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700624
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700625 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
626 // Lock on mBgLock *after* the db operation
627 synchronized (sBgLock) {
628 checkItemInfoLocked(itemId, item, stackTrace);
629
630 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
631 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
632 // Item is in a folder, make sure this folder exists
633 if (!sBgFolders.containsKey(item.container)) {
634 // An items container is being set to a that of an item which is not in
635 // the list of Folders.
636 String msg = "item: " + item + " container being set to: " +
637 item.container + ", not in the list of folders";
638 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700639 }
640 }
641
642 // Items are added/removed from the corresponding FolderInfo elsewhere, such
643 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
644 // that are on the desktop, as appropriate
645 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800646 if (modelItem != null &&
647 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
648 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 switch (modelItem.itemType) {
650 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
651 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
652 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
653 if (!sBgWorkspaceItems.contains(modelItem)) {
654 sBgWorkspaceItems.add(modelItem);
655 }
656 break;
657 default:
658 break;
659 }
660 } else {
661 sBgWorkspaceItems.remove(modelItem);
662 }
663 }
664 }
665
Michael Jurkac7700af2013-05-14 20:17:58 +0200666 public void flushWorkerThread() {
667 mFlushingWorkerThread = true;
668 Runnable waiter = new Runnable() {
669 public void run() {
670 synchronized (this) {
671 notifyAll();
672 mFlushingWorkerThread = false;
673 }
674 }
675 };
676
677 synchronized(waiter) {
678 runOnWorkerThread(waiter);
679 if (mLoaderTask != null) {
680 synchronized(mLoaderTask) {
681 mLoaderTask.notify();
682 }
683 }
684 boolean success = false;
685 while (!success) {
686 try {
687 waiter.wait();
688 success = true;
689 } catch (InterruptedException e) {
690 }
691 }
692 }
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700697 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700698 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700699 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 item.cellX = cellX;
702 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700703
Winson Chung3d503fb2011-07-13 17:25:49 -0700704 // We store hotseat items in canonical form which is this orientation invariant position
705 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700707 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700709 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700711 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712
713 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700715 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
716 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718
Michael Jurkac9d95c52011-08-29 14:03:34 -0700719 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700720 }
721
722 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700723 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
724 * cellX, cellY have already been updated on the ItemInfos.
725 */
726 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
727 final long container, final int screen) {
728
729 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
730 int count = items.size();
731
732 for (int i = 0; i < count; i++) {
733 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700734 item.container = container;
735
736 // We store hotseat items in canonical form which is this orientation invariant position
737 // in the hotseat
738 if (context instanceof Launcher && screen < 0 &&
739 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700740 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700741 item.cellY);
742 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 }
745
746 final ContentValues values = new ContentValues();
747 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
748 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
749 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751
752 contentValues.add(values);
753 }
754 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
755 }
756
757 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700758 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800759 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700760 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700761 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700762 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800763 item.cellX = cellX;
764 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700765 item.spanX = spanX;
766 item.spanY = spanY;
767
768 // We store hotseat items in canonical form which is this orientation invariant position
769 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700770 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700771 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700772 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700773 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700775 }
Adam Cohend4844c32011-02-18 19:25:06 -0800776
Adam Cohend4844c32011-02-18 19:25:06 -0800777 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800778 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
780 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
781 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
782 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800784
Michael Jurka816474f2012-06-25 14:49:02 -0700785 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700787
788 /**
789 * Update an item to the database in a specified container.
790 */
791 static void updateItemInDatabase(Context context, final ItemInfo item) {
792 final ContentValues values = new ContentValues();
793 item.onAddToDatabase(values);
794 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
795 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800796 }
797
798 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 * Returns true if the shortcuts already exists in the database.
800 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 static boolean shortcutExists(Context context, String title, Intent intent) {
803 final ContentResolver cr = context.getContentResolver();
804 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
805 new String[] { "title", "intent" }, "title=? and intent=?",
806 new String[] { title, intent.toUri(0) }, null);
807 boolean result = false;
808 try {
809 result = c.moveToFirst();
810 } finally {
811 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700814 }
815
Joe Onorato9c1289c2009-08-17 11:03:03 -0400816 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500817 * Returns true if the shortcuts already exists in the database.
818 * we identify a shortcut by the component name of the intent.
819 */
820 static boolean appWasRestored(Context context, Intent intent) {
821 final ContentResolver cr = context.getContentResolver();
822 final ComponentName component = intent.getComponent();
823 if (component == null) {
824 return false;
825 }
826 String componentName = component.flattenToString();
827 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
828 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
829 new String[]{"intent", "restored"}, where, null, null);
830 boolean result = false;
831 try {
832 result = c.moveToFirst();
833 } finally {
834 c.close();
835 }
836 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
837 return result;
838 }
839
840 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700841 * Returns an ItemInfo array containing all the items in the LauncherModel.
842 * The ItemInfo.id is not set through this function.
843 */
844 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
845 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
846 final ContentResolver cr = context.getContentResolver();
847 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
848 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
849 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
850 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
851
852 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
853 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
854 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
855 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
856 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
857 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
858 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
859
860 try {
861 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700862 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700863 item.cellX = c.getInt(cellXIndex);
864 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700865 item.spanX = Math.max(1, c.getInt(spanXIndex));
866 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700867 item.container = c.getInt(containerIndex);
868 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700870
871 items.add(item);
872 }
873 } catch (Exception e) {
874 items.clear();
875 } finally {
876 c.close();
877 }
878
879 return items;
880 }
881
882 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
884 */
885 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
886 final ContentResolver cr = context.getContentResolver();
887 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
888 "_id=? and (itemType=? or itemType=?)",
889 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700890 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700891
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892 try {
893 if (c.moveToFirst()) {
894 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
895 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
896 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
897 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
898 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
899 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901 FolderInfo folderInfo = null;
902 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700903 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
904 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700906 }
907
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 folderInfo.title = c.getString(titleIndex);
909 folderInfo.id = id;
910 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700911 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700912 folderInfo.cellX = c.getInt(cellXIndex);
913 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914
915 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700916 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 } finally {
918 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700919 }
920
921 return null;
922 }
923
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 /**
925 * Add an item to the database in a specified container. Sets the container, screen, cellX and
926 * cellY fields of the item. Also assigns an ID to the item.
927 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700928 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700929 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 item.cellX = cellX;
932 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 // We store hotseat items in canonical form which is this orientation invariant position
934 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941
942 final ContentValues values = new ContentValues();
943 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 item.onAddToDatabase(values);
945
Michael Jurka414300a2013-08-27 15:42:35 +0200946 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700947 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700949
Michael Jurkac9d95c52011-08-29 14:03:34 -0700950 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 public void run() {
952 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
953 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700956 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700957 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700958 sBgItemsIdMap.put(item.id, item);
959 switch (item.itemType) {
960 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
961 sBgFolders.put(item.id, (FolderInfo) item);
962 // Fall through
963 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
964 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
965 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
966 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
967 sBgWorkspaceItems.add(item);
968 } else {
969 if (!sBgFolders.containsKey(item.container)) {
970 // Adding an item to a folder that doesn't exist.
971 String msg = "adding item: " + item + " to a folder that " +
972 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700973 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700975 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700976 break;
977 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
978 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
979 break;
980 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 }
982 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700983 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700985 }
986
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700988 * Creates a new unique child id, for a given cell span across all layouts.
989 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700990 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700994 }
995
Winson Chungaafa03c2010-06-11 17:34:16 -0700996 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700997 * Removes the specified item from the database
998 * @param context
999 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001001 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001003 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001004
Michael Jurka83df1882011-08-31 20:59:26 -07001005 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008
1009 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001010 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 switch (item.itemType) {
1012 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1013 sBgFolders.remove(item.id);
1014 for (ItemInfo info: sBgItemsIdMap.values()) {
1015 if (info.container == item.id) {
1016 // We are deleting a folder which still contains items that
1017 // think they are contained by that folder.
1018 String msg = "deleting a folder (" + item + ") which still " +
1019 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001020 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001022 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 sBgWorkspaceItems.remove(item);
1024 break;
1025 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1026 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1027 sBgWorkspaceItems.remove(item);
1028 break;
1029 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1030 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1031 break;
1032 }
1033 sBgItemsIdMap.remove(item.id);
1034 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001035 }
1036 }
Michael Jurka83df1882011-08-31 20:59:26 -07001037 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 }
1040
1041 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001042 * Update the order of the workspace screens in the database. The array list contains
1043 * a list of screen ids in the order that they should appear.
1044 */
Winson Chungc9168342013-06-26 14:54:55 -07001045 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001046 // Log to disk
1047 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1048 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1049
Winson Chung64359a52013-07-08 17:17:08 -07001050 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001051 final ContentResolver cr = context.getContentResolver();
1052 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1053
1054 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001055 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 while (iter.hasNext()) {
1057 long id = iter.next();
1058 if (id < 0) {
1059 iter.remove();
1060 }
1061 }
1062
1063 Runnable r = new Runnable() {
1064 @Override
1065 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 // Clear the table
1067 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001068 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 ContentValues[] values = new ContentValues[count];
1070 for (int i = 0; i < count; i++) {
1071 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001072 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1074 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001075 values[i] = v;
1076 }
1077 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001078
Winson Chungba9c37f2013-08-30 14:11:37 -07001079 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001080 sBgWorkspaceScreens.clear();
1081 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001082 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 }
1084 };
1085 runOnWorkerThread(r);
1086 }
1087
1088 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089 * Remove the contents of the specified folder from the database
1090 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001091 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 final ContentResolver cr = context.getContentResolver();
1093
Michael Jurkac9d95c52011-08-29 14:03:34 -07001094 Runnable r = new Runnable() {
1095 public void run() {
1096 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001097 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001098 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099 sBgItemsIdMap.remove(info.id);
1100 sBgFolders.remove(info.id);
1101 sBgDbIconCache.remove(info);
1102 sBgWorkspaceItems.remove(info);
1103 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001104
Michael Jurkac9d95c52011-08-29 14:03:34 -07001105 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1106 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001107 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001108 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001109 for (ItemInfo childInfo : info.contents) {
1110 sBgItemsIdMap.remove(childInfo.id);
1111 sBgDbIconCache.remove(childInfo);
1112 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001113 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001114 }
1115 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 }
1118
1119 /**
1120 * Set this as the current Launcher activity object for the loader.
1121 */
1122 public void initialize(Callbacks callbacks) {
1123 synchronized (mLock) {
1124 mCallbacks = new WeakReference<Callbacks>(callbacks);
1125 }
1126 }
1127
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001128 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1130 * ACTION_PACKAGE_CHANGED.
1131 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001132 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001133 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001134 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001135
Joe Onorato36115782010-06-17 13:28:48 -04001136 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001137
Joe Onorato36115782010-06-17 13:28:48 -04001138 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1139 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1140 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1141 final String packageName = intent.getData().getSchemeSpecificPart();
1142 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143
Joe Onorato36115782010-06-17 13:28:48 -04001144 int op = PackageUpdatedTask.OP_NONE;
1145
1146 if (packageName == null || packageName.length() == 0) {
1147 // they sent us a bad intent
1148 return;
1149 }
1150
1151 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1152 op = PackageUpdatedTask.OP_UPDATE;
1153 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1154 if (!replacing) {
1155 op = PackageUpdatedTask.OP_REMOVE;
1156 }
1157 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1158 // later, we will update the package at this time
1159 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1160 if (!replacing) {
1161 op = PackageUpdatedTask.OP_ADD;
1162 } else {
1163 op = PackageUpdatedTask.OP_UPDATE;
1164 }
1165 }
1166
1167 if (op != PackageUpdatedTask.OP_NONE) {
1168 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1169 }
1170
1171 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001172 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001173 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001174 if (!replacing) {
1175 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1176 if (mAppsCanBeOnRemoveableStorage) {
1177 // Only rebind if we support removable storage. It catches the case where
1178 // apps on the external sd card need to be reloaded
1179 startLoaderFromBackground();
1180 }
1181 } else {
1182 // If we are replacing then just update the packages in the list
1183 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1184 packages));
1185 }
Joe Onorato36115782010-06-17 13:28:48 -04001186 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001187 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1188 if (!replacing) {
1189 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1190 enqueuePackageUpdated(new PackageUpdatedTask(
1191 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1192 }
1193 // else, we are replacing the packages, so ignore this event and wait for
1194 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001195 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001196 // If we have changed locale we need to clear out the labels in all apps/workspace.
1197 forceReload();
1198 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1199 // Check if configuration change was an mcc/mnc change which would affect app resources
1200 // and we would need to clear out the labels in all apps/workspace. Same handling as
1201 // above for ACTION_LOCALE_CHANGED
1202 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001203 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001204 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001205 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001206 forceReload();
1207 }
1208 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001209 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001210 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1211 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001212 if (mCallbacks != null) {
1213 Callbacks callbacks = mCallbacks.get();
1214 if (callbacks != null) {
1215 callbacks.bindSearchablesChanged();
1216 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001217 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001218 }
1219 }
1220
Reena Lee93f824a2011-09-23 17:20:28 -07001221 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001222 resetLoadedState(true, true);
1223
Reena Lee93f824a2011-09-23 17:20:28 -07001224 // Do this here because if the launcher activity is running it will be restarted.
1225 // If it's not running startLoaderFromBackground will merely tell it that it needs
1226 // to reload.
1227 startLoaderFromBackground();
1228 }
1229
Winson Chungf0c6ae02012-03-21 16:10:31 -07001230 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1231 synchronized (mLock) {
1232 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1233 // mWorkspaceLoaded to true later
1234 stopLoaderLocked();
1235 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1236 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1237 }
1238 }
1239
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001240 /**
1241 * When the launcher is in the background, it's possible for it to miss paired
1242 * configuration changes. So whenever we trigger the loader from the background
1243 * tell the launcher that it needs to re-run the loader when it comes back instead
1244 * of doing it now.
1245 */
1246 public void startLoaderFromBackground() {
1247 boolean runLoader = false;
1248 if (mCallbacks != null) {
1249 Callbacks callbacks = mCallbacks.get();
1250 if (callbacks != null) {
1251 // Only actually run the loader if they're not paused.
1252 if (!callbacks.setLoadOnResume()) {
1253 runLoader = true;
1254 }
1255 }
1256 }
1257 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001258 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001259 }
Joe Onorato36115782010-06-17 13:28:48 -04001260 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001261
Reena Lee93f824a2011-09-23 17:20:28 -07001262 // If there is already a loader task running, tell it to stop.
1263 // returns true if isLaunching() was true on the old task
1264 private boolean stopLoaderLocked() {
1265 boolean isLaunching = false;
1266 LoaderTask oldTask = mLoaderTask;
1267 if (oldTask != null) {
1268 if (oldTask.isLaunching()) {
1269 isLaunching = true;
1270 }
1271 oldTask.stopLocked();
1272 }
1273 return isLaunching;
1274 }
1275
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001276 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001277 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1278 }
1279
1280 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001281 synchronized (mLock) {
1282 if (DEBUG_LOADERS) {
1283 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1284 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001286 // Clear any deferred bind-runnables from the synchronized load process
1287 // We must do this before any loading/binding is scheduled below.
1288 mDeferredBindRunnables.clear();
1289
Joe Onorato36115782010-06-17 13:28:48 -04001290 // Don't bother to start the thread if we know it's not going to do anything
1291 if (mCallbacks != null && mCallbacks.get() != null) {
1292 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001293 // also, don't downgrade isLaunching if we're already running
1294 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001295 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001296 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1297 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001298 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1299 } else {
1300 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1301 sWorker.post(mLoaderTask);
1302 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001304 }
1305 }
1306
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001307 void bindRemainingSynchronousPages() {
1308 // Post the remaining side pages to be loaded
1309 if (!mDeferredBindRunnables.isEmpty()) {
1310 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001311 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001312 }
1313 mDeferredBindRunnables.clear();
1314 }
1315 }
1316
Joe Onorato36115782010-06-17 13:28:48 -04001317 public void stopLoader() {
1318 synchronized (mLock) {
1319 if (mLoaderTask != null) {
1320 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 }
1322 }
Joe Onorato36115782010-06-17 13:28:48 -04001323 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324
Winson Chung76828c82013-08-19 15:43:29 -07001325 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1326 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1327 final ContentResolver contentResolver = context.getContentResolver();
1328 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1329 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1330 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1331
1332 try {
1333 final int idIndex = sc.getColumnIndexOrThrow(
1334 LauncherSettings.WorkspaceScreens._ID);
1335 final int rankIndex = sc.getColumnIndexOrThrow(
1336 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1337 while (sc.moveToNext()) {
1338 try {
1339 long screenId = sc.getLong(idIndex);
1340 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001341 orderedScreens.put(rank, screenId);
1342 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001343 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001344 }
1345 }
1346 } finally {
1347 sc.close();
1348 }
Winson Chunga90303b2013-11-15 13:05:06 -08001349
1350 // Log to disk
1351 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1352 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1353 for (Integer i : orderedScreens.keySet()) {
1354 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1355 }
1356 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1357 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001358 return orderedScreens;
1359 }
1360
Michael Jurkac57b7a82011-08-09 22:02:20 -07001361 public boolean isAllAppsLoaded() {
1362 return mAllAppsLoaded;
1363 }
1364
Winson Chung36a62fe2012-05-06 18:04:42 -07001365 boolean isLoadingWorkspace() {
1366 synchronized (mLock) {
1367 if (mLoaderTask != null) {
1368 return mLoaderTask.isLoadingWorkspace();
1369 }
1370 }
1371 return false;
1372 }
1373
Joe Onorato36115782010-06-17 13:28:48 -04001374 /**
1375 * Runnable for the thread that loads the contents of the launcher:
1376 * - workspace icons
1377 * - widgets
1378 * - all apps icons
1379 */
1380 private class LoaderTask implements Runnable {
1381 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001382 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001383 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001384 private boolean mStopped;
1385 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001386 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001387
Winson Chungc3eecff2011-07-11 17:44:15 -07001388 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001389
Dan Sandlerd5024042014-01-09 15:01:33 -05001390 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001391 mContext = context;
1392 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001393 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001394 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 }
1396
Joe Onorato36115782010-06-17 13:28:48 -04001397 boolean isLaunching() {
1398 return mIsLaunching;
1399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400
Winson Chung36a62fe2012-05-06 18:04:42 -07001401 boolean isLoadingWorkspace() {
1402 return mIsLoadingAndBindingWorkspace;
1403 }
1404
Winson Chungc763c4e2013-07-19 13:49:06 -07001405 /** Returns whether this is an upgrade path */
1406 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001407 mIsLoadingAndBindingWorkspace = true;
1408
Joe Onorato36115782010-06-17 13:28:48 -04001409 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001410 if (DEBUG_LOADERS) {
1411 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001413
Winson Chungc763c4e2013-07-19 13:49:06 -07001414 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001415 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001416 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001417 synchronized (LoaderTask.this) {
1418 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001419 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001420 }
1421 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422 }
1423 }
1424
Joe Onorato36115782010-06-17 13:28:48 -04001425 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001426 bindWorkspace(-1, isUpgradePath);
1427 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001428 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001429
Joe Onorato36115782010-06-17 13:28:48 -04001430 private void waitForIdle() {
1431 // Wait until the either we're stopped or the other threads are done.
1432 // This way we don't start loading all apps until the workspace has settled
1433 // down.
1434 synchronized (LoaderTask.this) {
1435 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001436
Joe Onorato36115782010-06-17 13:28:48 -04001437 mHandler.postIdle(new Runnable() {
1438 public void run() {
1439 synchronized (LoaderTask.this) {
1440 mLoadAndBindStepFinished = true;
1441 if (DEBUG_LOADERS) {
1442 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001443 }
Joe Onorato36115782010-06-17 13:28:48 -04001444 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001445 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001446 }
Joe Onorato36115782010-06-17 13:28:48 -04001447 });
1448
Michael Jurkac7700af2013-05-14 20:17:58 +02001449 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001450 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001451 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1452 // wait no longer than 1sec at a time
1453 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001454 } catch (InterruptedException ex) {
1455 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001456 }
1457 }
Joe Onorato36115782010-06-17 13:28:48 -04001458 if (DEBUG_LOADERS) {
1459 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001460 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001461 + "ms for previous step to finish binding");
1462 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001463 }
Joe Onorato36115782010-06-17 13:28:48 -04001464 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001465
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001467 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 // Ensure that we have a valid page index to load synchronously
1469 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1470 "valid page index");
1471 }
1472 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1473 // Ensure that we don't try and bind a specified page when the pages have not been
1474 // loaded already (we should load everything asynchronously in that case)
1475 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1476 }
1477 synchronized (mLock) {
1478 if (mIsLoaderTaskRunning) {
1479 // Ensure that we are never running the background loading at this point since
1480 // we also touch the background collections
1481 throw new RuntimeException("Error! Background loading is already running");
1482 }
1483 }
1484
1485 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1486 // data structures, we can't allow any other thread to touch that data, but because
1487 // this call is synchronous, we can get away with not locking).
1488
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001489 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001490 // operations from the previous activity. We need to ensure that all queued operations
1491 // are executed before any synchronous binding work is done.
1492 mHandler.flush();
1493
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001494 // Divide the set of loaded items into those that we are binding synchronously, and
1495 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001496 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001497 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1498 // arise from that.
1499 onlyBindAllApps();
1500 }
1501
Joe Onorato36115782010-06-17 13:28:48 -04001502 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001503 boolean isUpgrade = false;
1504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505 synchronized (mLock) {
1506 mIsLoaderTaskRunning = true;
1507 }
Joe Onorato36115782010-06-17 13:28:48 -04001508 // Optimize for end-user experience: if the Launcher is up and // running with the
1509 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1510 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001511 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001512 // Elevate priority when Home launches for the first time to avoid
1513 // starving at boot time. Staring at a blank home is not cool.
1514 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001515 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1516 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001517 android.os.Process.setThreadPriority(mIsLaunching
1518 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1519 }
Winson Chung64359a52013-07-08 17:17:08 -07001520 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001521 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001522
Joe Onorato36115782010-06-17 13:28:48 -04001523 if (mStopped) {
1524 break keep_running;
1525 }
1526
1527 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1528 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001529 synchronized (mLock) {
1530 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001531 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001532 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1533 }
1534 }
Joe Onorato36115782010-06-17 13:28:48 -04001535 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001536
1537 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001538 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1539 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001540
1541 // Restore the default thread priority after we are done loading items
1542 synchronized (mLock) {
1543 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1544 }
Joe Onorato36115782010-06-17 13:28:48 -04001545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546
Winson Chungaac01e12011-08-17 10:37:13 -07001547 // Update the saved icons if necessary
1548 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001549 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 for (Object key : sBgDbIconCache.keySet()) {
1551 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1552 }
1553 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001554 }
Winson Chungaac01e12011-08-17 10:37:13 -07001555
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001556 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001557 // Ensure that all the applications that are in the system are
1558 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001559 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001560 verifyApplications();
1561 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001562 }
1563
Joe Onorato36115782010-06-17 13:28:48 -04001564 // Clear out this reference, otherwise we end up holding it until all of the
1565 // callback runnables are done.
1566 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567
Joe Onorato36115782010-06-17 13:28:48 -04001568 synchronized (mLock) {
1569 // If we are still the last one to be scheduled, remove ourselves.
1570 if (mLoaderTask == this) {
1571 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001573 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001574 }
Joe Onorato36115782010-06-17 13:28:48 -04001575 }
1576
1577 public void stopLocked() {
1578 synchronized (LoaderTask.this) {
1579 mStopped = true;
1580 this.notify();
1581 }
1582 }
1583
1584 /**
1585 * Gets the callbacks object. If we've been stopped, or if the launcher object
1586 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1587 * object that was around when the deferred message was scheduled, and if there's
1588 * a new Callbacks object around then also return null. This will save us from
1589 * calling onto it with data that will be ignored.
1590 */
1591 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1592 synchronized (mLock) {
1593 if (mStopped) {
1594 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001595 }
Joe Onorato36115782010-06-17 13:28:48 -04001596
1597 if (mCallbacks == null) {
1598 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001599 }
Joe Onorato36115782010-06-17 13:28:48 -04001600
1601 final Callbacks callbacks = mCallbacks.get();
1602 if (callbacks != oldCallbacks) {
1603 return null;
1604 }
1605 if (callbacks == null) {
1606 Log.w(TAG, "no mCallbacks");
1607 return null;
1608 }
1609
1610 return callbacks;
1611 }
1612 }
1613
Winson Chungc763c4e2013-07-19 13:49:06 -07001614 private void verifyApplications() {
1615 final Context context = mApp.getContext();
1616
1617 // Cross reference all the applications in our apps list with items in the workspace
1618 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001619 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001620 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001621 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001622 tmpInfos = getItemInfoForComponentName(app.componentName);
1623 if (tmpInfos.isEmpty()) {
1624 // We are missing an application icon, so add this to the workspace
1625 added.add(app);
1626 // This is a rare event, so lets log it
1627 Log.e(TAG, "Missing Application on load: " + app);
1628 }
1629 }
1630 }
1631 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001632 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001633 }
1634 }
1635
Joe Onorato36115782010-06-17 13:28:48 -04001636 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001637 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001638 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001639 LauncherAppState app = LauncherAppState.getInstance();
1640 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001641 final int countX = (int) grid.numColumns;
1642 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001643
Adam Cohendcd297f2013-06-18 13:13:40 -07001644 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001645 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001646 // Return early if we detect that an item is under the hotseat button
1647 if (mCallbacks == null ||
1648 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001649 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001650 Log.e(TAG, "Error loading shortcut into hotseat " + item
1651 + " into position (" + item.screenId + ":" + item.cellX + ","
1652 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001653 return false;
1654 }
1655
Dan Sandler295ae182013-12-10 16:05:47 -05001656 final ItemInfo[][] hotseatItems =
1657 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1658
Adam Cohenae4409d2013-11-26 10:34:59 -08001659 if (item.screenId >= grid.numHotseatIcons) {
1660 Log.e(TAG, "Error loading shortcut " + item
1661 + " into hotseat position " + item.screenId
1662 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1663 + ")");
1664 return false;
1665 }
1666
Dan Sandler295ae182013-12-10 16:05:47 -05001667 if (hotseatItems != null) {
1668 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001669 Log.e(TAG, "Error loading shortcut into hotseat " + item
1670 + " into position (" + item.screenId + ":" + item.cellX + ","
1671 + item.cellY + ") occupied by "
1672 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1673 [(int) item.screenId][0]);
1674 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001675 } else {
1676 hotseatItems[(int) item.screenId][0] = item;
1677 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001678 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001679 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001680 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001681 items[(int) item.screenId][0] = item;
1682 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001683 return true;
1684 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001685 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1686 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001687 return true;
1688 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001689
Adam Cohendcd297f2013-06-18 13:13:40 -07001690 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001691 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 occupied.put(item.screenId, items);
1693 }
1694
Dan Sandler295ae182013-12-10 16:05:47 -05001695 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001696 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1697 item.cellX < 0 || item.cellY < 0 ||
1698 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1699 Log.e(TAG, "Error loading shortcut " + item
1700 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1701 + item.cellX + "," + item.cellY
1702 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1703 return false;
1704 }
1705
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001706 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001707 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1708 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001709 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001710 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001711 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001712 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001713 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001715 return false;
1716 }
1717 }
1718 }
1719 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1720 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001722 }
1723 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001724
Joe Onorato36115782010-06-17 13:28:48 -04001725 return true;
1726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Winson Chungba9c37f2013-08-30 14:11:37 -07001728 /** Clears all the sBg data structures */
1729 private void clearSBgDataStructures() {
1730 synchronized (sBgLock) {
1731 sBgWorkspaceItems.clear();
1732 sBgAppWidgets.clear();
1733 sBgFolders.clear();
1734 sBgItemsIdMap.clear();
1735 sBgDbIconCache.clear();
1736 sBgWorkspaceScreens.clear();
1737 }
1738 }
1739
Dan Sandlerd5024042014-01-09 15:01:33 -05001740 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001741 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001742 // Log to disk
1743 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1744
Joe Onorato36115782010-06-17 13:28:48 -04001745 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746
Joe Onorato36115782010-06-17 13:28:48 -04001747 final Context context = mContext;
1748 final ContentResolver contentResolver = context.getContentResolver();
1749 final PackageManager manager = context.getPackageManager();
1750 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1751 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001752
Winson Chung892c74d2013-08-22 16:15:50 -07001753 LauncherAppState app = LauncherAppState.getInstance();
1754 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1755 int countX = (int) grid.numColumns;
1756 int countY = (int) grid.numRows;
1757
Dan Sandlerd5024042014-01-09 15:01:33 -05001758 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1759 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1760 LauncherAppState.getLauncherProvider().deleteDatabase();
1761 }
1762
1763 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1764 // append the user's Launcher2 shortcuts
1765 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1766 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1767 } else {
1768 // Make sure the default workspace is loaded
1769 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1770 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1771 }
Adam Cohene25af792013-06-06 23:08:25 -07001772
Winson Chungc763c4e2013-07-19 13:49:06 -07001773 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001774 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001775 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001776
Winson Chung9f9f00b2013-11-15 13:27:00 -08001777 // Log to disk
1778 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001779
Winson Chung2abf94d2012-07-18 18:16:38 -07001780 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001781 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001782
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001784 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001785 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001786 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001787 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001788
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001789 // +1 for the hotseat (it can be larger than the workspace)
1790 // Load workspace in reverse order to ensure that latest items are loaded first (and
1791 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001792 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001794 try {
1795 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1796 final int intentIndex = c.getColumnIndexOrThrow
1797 (LauncherSettings.Favorites.INTENT);
1798 final int titleIndex = c.getColumnIndexOrThrow
1799 (LauncherSettings.Favorites.TITLE);
1800 final int iconTypeIndex = c.getColumnIndexOrThrow(
1801 LauncherSettings.Favorites.ICON_TYPE);
1802 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1803 final int iconPackageIndex = c.getColumnIndexOrThrow(
1804 LauncherSettings.Favorites.ICON_PACKAGE);
1805 final int iconResourceIndex = c.getColumnIndexOrThrow(
1806 LauncherSettings.Favorites.ICON_RESOURCE);
1807 final int containerIndex = c.getColumnIndexOrThrow(
1808 LauncherSettings.Favorites.CONTAINER);
1809 final int itemTypeIndex = c.getColumnIndexOrThrow(
1810 LauncherSettings.Favorites.ITEM_TYPE);
1811 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1812 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001813 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1814 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001815 final int screenIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.SCREEN);
1817 final int cellXIndex = c.getColumnIndexOrThrow
1818 (LauncherSettings.Favorites.CELLX);
1819 final int cellYIndex = c.getColumnIndexOrThrow
1820 (LauncherSettings.Favorites.CELLY);
1821 final int spanXIndex = c.getColumnIndexOrThrow
1822 (LauncherSettings.Favorites.SPANX);
1823 final int spanYIndex = c.getColumnIndexOrThrow(
1824 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001825 final int restoredIndex = c.getColumnIndexOrThrow(
1826 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1828 //final int displayModeIndex = c.getColumnIndexOrThrow(
1829 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001830
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 ShortcutInfo info;
1832 String intentDescription;
1833 LauncherAppWidgetInfo appWidgetInfo;
1834 int container;
1835 long id;
1836 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001839 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 try {
1841 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001842 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 switch (itemType) {
1845 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1846 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001847 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001848 intentDescription = c.getString(intentIndex);
1849 try {
1850 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001851 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001852 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001853 if (restored) {
1854 // might be installed later
1855 Launcher.addDumpLog(TAG,
1856 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001857 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001858 if (!mAppsCanBeOnRemoveableStorage) {
1859 // Log the invalid package, and remove it
1860 Launcher.addDumpLog(TAG,
1861 "Invalid package removed: " + cn, true);
1862 itemsToRemove.add(id);
1863 } else {
1864 // If apps can be on external storage, then we just
1865 // leave them for the user to remove (maybe add
1866 // visual treatment to it)
1867 Launcher.addDumpLog(TAG,
1868 "Invalid package found: " + cn, true);
1869 }
1870 continue;
Winson Chungee055712013-07-30 14:46:24 -07001871 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001872 } else if (restored) {
1873 // no special handling necessary for this restored item
1874 restoredRows.add(id);
1875 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001876 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001878 Launcher.addDumpLog(TAG,
1879 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 continue;
1881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882
Chris Wrenf4d08112014-01-16 18:13:56 -05001883 if (restored) {
1884 Launcher.addDumpLog(TAG,
1885 "constructing info for partially restored package",
1886 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001887 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001888 intent = getRestoredItemIntent(c, context, intent);
1889 } else if (itemType ==
1890 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1892 titleIndex, mLabelCache);
1893 } else {
1894 info = getShortcutInfo(c, context, iconTypeIndex,
1895 iconPackageIndex, iconResourceIndex, iconIndex,
1896 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001897
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 // App shortcuts that used to be automatically added to Launcher
1899 // didn't always have the correct intent flags set, so do that
1900 // here
1901 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001902 intent.getCategories() != null &&
1903 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001904 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 intent.addFlags(
1906 Intent.FLAG_ACTIVITY_NEW_TASK |
1907 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1908 }
Michael Jurka96879562012-03-22 05:54:33 -07001909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001911 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001912 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001913 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001914 container = c.getInt(containerIndex);
1915 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001916 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 info.cellX = c.getInt(cellXIndex);
1918 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001919 info.spanX = 1;
1920 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001921
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001923 deleteOnInvalidPlacement.set(false);
1924 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1925 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001926 itemsToRemove.add(id);
1927 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 break;
1929 }
1930
1931 switch (container) {
1932 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1933 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1934 sBgWorkspaceItems.add(info);
1935 break;
1936 default:
1937 // Item is in a user folder
1938 FolderInfo folderInfo =
1939 findOrMakeFolder(sBgFolders, container);
1940 folderInfo.add(info);
1941 break;
1942 }
1943 sBgItemsIdMap.put(info.id, info);
1944
1945 // now that we've loaded everthing re-save it with the
1946 // icon in case it disappears somehow.
1947 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001948 } else {
1949 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 }
1951 break;
1952
1953 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1954 id = c.getLong(idIndex);
1955 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1956
1957 folderInfo.title = c.getString(titleIndex);
1958 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001959 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001960 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001961 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001962 folderInfo.cellX = c.getInt(cellXIndex);
1963 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001964 folderInfo.spanX = 1;
1965 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001966
Daniel Sandler8802e962010-05-26 16:28:16 -04001967 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001968 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001969 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001970 deleteOnInvalidPlacement)) {
1971 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001972 itemsToRemove.add(id);
1973 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001974 break;
1975 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001976
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1979 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1980 sBgWorkspaceItems.add(folderInfo);
1981 break;
Joe Onorato36115782010-06-17 13:28:48 -04001982 }
Joe Onorato17a89222011-02-08 17:26:11 -08001983
Chris Wrenf4d08112014-01-16 18:13:56 -05001984 if (restored) {
1985 // no special handling required for restored folders
1986 restoredRows.add(id);
1987 }
1988
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1990 sBgFolders.put(folderInfo.id, folderInfo);
1991 break;
1992
1993 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1994 // Read all Launcher-specific widget details
1995 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001996 String savedProvider = c.getString(appWidgetProviderIndex);
1997
Joe Onorato36115782010-06-17 13:28:48 -04001998 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001999
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 final AppWidgetProviderInfo provider =
2001 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002002
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 if (!isSafeMode && (provider == null || provider.provider == null ||
2004 provider.provider.getPackageName() == null)) {
2005 String log = "Deleting widget that isn't installed anymore: id="
2006 + id + " appWidgetId=" + appWidgetId;
2007 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002008 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 itemsToRemove.add(id);
2010 } else {
2011 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2012 provider.provider);
2013 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002014 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 appWidgetInfo.cellX = c.getInt(cellXIndex);
2016 appWidgetInfo.cellY = c.getInt(cellYIndex);
2017 appWidgetInfo.spanX = c.getInt(spanXIndex);
2018 appWidgetInfo.spanY = c.getInt(spanYIndex);
2019 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2020 appWidgetInfo.minSpanX = minSpan[0];
2021 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002022
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 container = c.getInt(containerIndex);
2024 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2025 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2026 Log.e(TAG, "Widget found where container != " +
2027 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2028 continue;
2029 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030
Adam Cohene25af792013-06-06 23:08:25 -07002031 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002033 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002034 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002035 deleteOnInvalidPlacement)) {
2036 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002037 itemsToRemove.add(id);
2038 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 break;
2040 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002041 String providerName = provider.provider.flattenToString();
2042 if (!providerName.equals(savedProvider)) {
2043 ContentValues values = new ContentValues();
2044 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2045 providerName);
2046 String where = BaseColumns._ID + "= ?";
2047 String[] args = {Integer.toString(c.getInt(idIndex))};
2048 contentResolver.update(contentUri, values, where, args);
2049 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2051 sBgAppWidgets.add(appWidgetInfo);
2052 }
Joe Onorato36115782010-06-17 13:28:48 -04002053 break;
2054 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002056 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002057 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 }
2059 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002060 if (c != null) {
2061 c.close();
2062 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 }
2064
Winson Chungba9c37f2013-08-30 14:11:37 -07002065 // Break early if we've stopped loading
2066 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002067 clearSBgDataStructures();
2068 return false;
2069 }
2070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 if (itemsToRemove.size() > 0) {
2072 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002073 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 // Remove dead items
2075 for (long id : itemsToRemove) {
2076 if (DEBUG_LOADERS) {
2077 Log.d(TAG, "Removed id = " + id);
2078 }
2079 // Don't notify content observers
2080 try {
2081 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2082 null, null);
2083 } catch (RemoteException e) {
2084 Log.w(TAG, "Could not remove id = " + id);
2085 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002086 }
2087 }
2088
Chris Wrenf4d08112014-01-16 18:13:56 -05002089 if (restoredRows.size() > 0) {
2090 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2091 LauncherSettings.Favorites.CONTENT_URI);
2092 // Update restored items that no longer require special handling
2093 try {
2094 StringBuilder selectionBuilder = new StringBuilder();
2095 selectionBuilder.append(LauncherSettings.Favorites._ID);
2096 selectionBuilder.append(" IN (");
2097 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2098 selectionBuilder.append(")");
2099 ContentValues values = new ContentValues();
2100 values.put(LauncherSettings.Favorites.RESTORED, 0);
2101 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2102 values, selectionBuilder.toString(), null);
2103 } catch (RemoteException e) {
2104 Log.w(TAG, "Could not update restored rows");
2105 }
2106 }
2107
Winson Chungc763c4e2013-07-19 13:49:06 -07002108 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002109 long maxScreenId = 0;
2110 // If we're importing we use the old screen order.
2111 for (ItemInfo item: sBgItemsIdMap.values()) {
2112 long screenId = item.screenId;
2113 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2114 !sBgWorkspaceScreens.contains(screenId)) {
2115 sBgWorkspaceScreens.add(screenId);
2116 if (screenId > maxScreenId) {
2117 maxScreenId = screenId;
2118 }
2119 }
2120 }
2121 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002122 // Log to disk
2123 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2124 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2125 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002126
Michael Jurka414300a2013-08-27 15:42:35 +02002127 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002128 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002129
2130 // Update the max item id after we load an old db
2131 long maxItemId = 0;
2132 // If we're importing we use the old screen order.
2133 for (ItemInfo item: sBgItemsIdMap.values()) {
2134 maxItemId = Math.max(maxItemId, item.id);
2135 }
Michael Jurka414300a2013-08-27 15:42:35 +02002136 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002138 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2139 for (Integer i : orderedScreens.keySet()) {
2140 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002141 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002142 // Log to disk
2143 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2144 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002145
2146 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002147 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002148 for (ItemInfo item: sBgItemsIdMap.values()) {
2149 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002150 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2151 unusedScreens.contains(screenId)) {
2152 unusedScreens.remove(screenId);
2153 }
2154 }
2155
2156 // If there are any empty screens remove them, and update.
2157 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002158 // Log to disk
2159 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2160 TextUtils.join(", ", unusedScreens), true);
2161
Winson Chung933bae62013-08-29 11:42:30 -07002162 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002163 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2164 }
2165 }
2166
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 if (DEBUG_LOADERS) {
2168 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2169 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002170 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002171 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002173
Daniel Sandler566da102013-06-25 23:43:45 -04002174 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002175 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002176 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002177 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 line += " | ";
2179 }
Winson Chung892c74d2013-08-22 16:15:50 -07002180 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 }
Joe Onorato36115782010-06-17 13:28:48 -04002183 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 }
Joe Onorato36115782010-06-17 13:28:48 -04002187 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002188 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002189 }
2190
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 /** Filters the set of items who are directly or indirectly (via another container) on the
2192 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002193 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 ArrayList<ItemInfo> allWorkspaceItems,
2195 ArrayList<ItemInfo> currentScreenItems,
2196 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002197 // Purge any null ItemInfos
2198 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2199 while (iter.hasNext()) {
2200 ItemInfo i = iter.next();
2201 if (i == null) {
2202 iter.remove();
2203 }
2204 }
2205
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 // Order the set of items by their containers first, this allows use to walk through the
2207 // list sequentially, build up a list of containers that are in the specified screen,
2208 // as well as all items in those containers.
2209 Set<Long> itemsOnScreen = new HashSet<Long>();
2210 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2211 @Override
2212 public int compare(ItemInfo lhs, ItemInfo rhs) {
2213 return (int) (lhs.container - rhs.container);
2214 }
2215 });
2216 for (ItemInfo info : allWorkspaceItems) {
2217 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002218 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 currentScreenItems.add(info);
2220 itemsOnScreen.add(info.id);
2221 } else {
2222 otherScreenItems.add(info);
2223 }
2224 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2225 currentScreenItems.add(info);
2226 itemsOnScreen.add(info.id);
2227 } else {
2228 if (itemsOnScreen.contains(info.container)) {
2229 currentScreenItems.add(info);
2230 itemsOnScreen.add(info.id);
2231 } else {
2232 otherScreenItems.add(info);
2233 }
2234 }
2235 }
2236 }
2237
2238 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002239 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240 ArrayList<LauncherAppWidgetInfo> appWidgets,
2241 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2242 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243
2244 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002245 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002247 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 currentScreenWidgets.add(widget);
2249 } else {
2250 otherScreenWidgets.add(widget);
2251 }
2252 }
2253 }
2254
2255 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002256 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257 HashMap<Long, ItemInfo> itemsIdMap,
2258 HashMap<Long, FolderInfo> folders,
2259 HashMap<Long, FolderInfo> currentScreenFolders,
2260 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261
2262 for (long id : folders.keySet()) {
2263 ItemInfo info = itemsIdMap.get(id);
2264 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002265 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002267 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 currentScreenFolders.put(id, folder);
2269 } else {
2270 otherScreenFolders.put(id, folder);
2271 }
2272 }
2273 }
2274
2275 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2276 * right) */
2277 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002278 final LauncherAppState app = LauncherAppState.getInstance();
2279 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 // XXX: review this
2281 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002282 @Override
2283 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002284 int cellCountX = (int) grid.numColumns;
2285 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002286 int screenOffset = cellCountX * cellCountY;
2287 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002289 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002291 rhs.cellY * cellCountX + rhs.cellX);
2292 return (int) (lr - rr);
2293 }
2294 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002296
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2298 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 final Runnable r = new Runnable() {
2300 @Override
2301 public void run() {
2302 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2303 if (callbacks != null) {
2304 callbacks.bindScreens(orderedScreens);
2305 }
2306 }
2307 };
2308 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2309 }
2310
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2312 final ArrayList<ItemInfo> workspaceItems,
2313 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2314 final HashMap<Long, FolderInfo> folders,
2315 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002316
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 final boolean postOnMainThread = (deferredBindRunnables != null);
2318
2319 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002320 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002321 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002322 final int start = i;
2323 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 final Runnable r = new Runnable() {
2325 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002326 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002327 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002328 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002329 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2330 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002331 }
2332 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 };
2334 if (postOnMainThread) {
2335 deferredBindRunnables.add(r);
2336 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002337 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 }
Joe Onorato36115782010-06-17 13:28:48 -04002339 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340
2341 // Bind the folders
2342 if (!folders.isEmpty()) {
2343 final Runnable r = new Runnable() {
2344 public void run() {
2345 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2346 if (callbacks != null) {
2347 callbacks.bindFolders(folders);
2348 }
2349 }
2350 };
2351 if (postOnMainThread) {
2352 deferredBindRunnables.add(r);
2353 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002354 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 }
2356 }
2357
2358 // Bind the widgets, one at a time
2359 N = appWidgets.size();
2360 for (int i = 0; i < N; i++) {
2361 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2362 final Runnable r = new Runnable() {
2363 public void run() {
2364 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2365 if (callbacks != null) {
2366 callbacks.bindAppWidget(widget);
2367 }
2368 }
2369 };
2370 if (postOnMainThread) {
2371 deferredBindRunnables.add(r);
2372 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002373 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 }
2375 }
2376 }
2377
2378 /**
2379 * Binds all loaded data to actual views on the main thread.
2380 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002381 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 final long t = SystemClock.uptimeMillis();
2383 Runnable r;
2384
2385 // Don't use these two variables in any of the callback runnables.
2386 // Otherwise we hold a reference to them.
2387 final Callbacks oldCallbacks = mCallbacks.get();
2388 if (oldCallbacks == null) {
2389 // This launcher has exited and nobody bothered to tell us. Just bail.
2390 Log.w(TAG, "LoaderTask running with no launcher");
2391 return;
2392 }
2393
Winson Chung9b9fb962013-11-15 15:39:34 -08002394 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002395 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2396 ArrayList<LauncherAppWidgetInfo> appWidgets =
2397 new ArrayList<LauncherAppWidgetInfo>();
2398 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2399 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002400 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002401 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 workspaceItems.addAll(sBgWorkspaceItems);
2403 appWidgets.addAll(sBgAppWidgets);
2404 folders.putAll(sBgFolders);
2405 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002406 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 }
2408
Derek Prothro7aff3992013-12-10 14:00:37 -05002409 final boolean isLoadingSynchronously =
2410 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002411 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002412 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002413 if (currScreen >= orderedScreenIds.size()) {
2414 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002415 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002416 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002417 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002418 final long currentScreenId = currentScreen < 0
2419 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002420
2421 // Load all the items that are on the current page first (and in the process, unbind
2422 // all the existing workspace items before we call startBinding() below.
2423 unbindWorkspaceItemsOnMainThread();
2424
2425 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2427 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2428 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2429 new ArrayList<LauncherAppWidgetInfo>();
2430 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2431 new ArrayList<LauncherAppWidgetInfo>();
2432 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2433 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2434
Winson Chung9b9fb962013-11-15 15:39:34 -08002435 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002437 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002439 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002440 otherFolders);
2441 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2442 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2443
2444 // Tell the workspace that we're about to start binding items
2445 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002446 public void run() {
2447 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2448 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002450 }
2451 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452 };
Winson Chung81b52252012-08-27 15:34:29 -07002453 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454
Adam Cohendcd297f2013-06-18 13:13:40 -07002455 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2456
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 // Load items on the current page
2458 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2459 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002460 if (isLoadingSynchronously) {
2461 r = new Runnable() {
2462 public void run() {
2463 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002464 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002465 callbacks.onPageBoundSynchronously(currentScreen);
2466 }
2467 }
2468 };
Winson Chung81b52252012-08-27 15:34:29 -07002469 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002470 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471
Winson Chung4a2afa32012-07-19 14:53:05 -07002472 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2473 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474 mDeferredBindRunnables.clear();
2475 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002476 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477
2478 // Tell the workspace that we're done binding items
2479 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002480 public void run() {
2481 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2482 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002483 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002484 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485
Winson Chung98e030b2012-05-07 16:01:11 -07002486 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002487 if (DEBUG_LOADERS) {
2488 Log.d(TAG, "bound workspace in "
2489 + (SystemClock.uptimeMillis()-t) + "ms");
2490 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002491
2492 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002493 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002495 if (isLoadingSynchronously) {
2496 mDeferredBindRunnables.add(r);
2497 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002498 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002499 }
Joe Onorato36115782010-06-17 13:28:48 -04002500 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002501
Joe Onorato36115782010-06-17 13:28:48 -04002502 private void loadAndBindAllApps() {
2503 if (DEBUG_LOADERS) {
2504 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2505 }
2506 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002507 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002508 synchronized (LoaderTask.this) {
2509 if (mStopped) {
2510 return;
2511 }
2512 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002513 }
Joe Onorato36115782010-06-17 13:28:48 -04002514 } else {
2515 onlyBindAllApps();
2516 }
2517 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002518
Joe Onorato36115782010-06-17 13:28:48 -04002519 private void onlyBindAllApps() {
2520 final Callbacks oldCallbacks = mCallbacks.get();
2521 if (oldCallbacks == null) {
2522 // This launcher has exited and nobody bothered to tell us. Just bail.
2523 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2524 return;
2525 }
2526
2527 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002528 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002529 final ArrayList<AppInfo> list
2530 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002531 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002532 public void run() {
2533 final long t = SystemClock.uptimeMillis();
2534 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2535 if (callbacks != null) {
2536 callbacks.bindAllApplications(list);
2537 }
2538 if (DEBUG_LOADERS) {
2539 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2540 + (SystemClock.uptimeMillis()-t) + "ms");
2541 }
2542 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002543 };
2544 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002545 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002546 r.run();
2547 } else {
2548 mHandler.post(r);
2549 }
Joe Onorato36115782010-06-17 13:28:48 -04002550 }
2551
Winson Chung64359a52013-07-08 17:17:08 -07002552 private void loadAllApps() {
2553 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002554
Joe Onorato36115782010-06-17 13:28:48 -04002555 final Callbacks oldCallbacks = mCallbacks.get();
2556 if (oldCallbacks == null) {
2557 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002558 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002559 return;
2560 }
2561
Winson Chung64359a52013-07-08 17:17:08 -07002562 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002563 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2564 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2565
Winson Chung64359a52013-07-08 17:17:08 -07002566 // Clear the list of apps
2567 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002568
Winson Chung64359a52013-07-08 17:17:08 -07002569 // Query for the set of apps
2570 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2571 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2572 if (DEBUG_LOADERS) {
2573 Log.d(TAG, "queryIntentActivities took "
2574 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2575 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2576 }
2577 // Fail if we don't have any apps
2578 if (apps == null || apps.isEmpty()) {
2579 return;
2580 }
2581 // Sort the applications by name
2582 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2583 Collections.sort(apps,
2584 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2585 if (DEBUG_LOADERS) {
2586 Log.d(TAG, "sort took "
2587 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002588 }
2589
Winson Chung64359a52013-07-08 17:17:08 -07002590 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002591 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002592 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002593 // This builds the icon bitmaps.
2594 mBgAllAppsList.add(new AppInfo(packageManager, app,
2595 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002596 }
2597
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002598 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002599 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2600 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002601
2602 // Post callback on main thread
2603 mHandler.post(new Runnable() {
2604 public void run() {
2605 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002606 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002607 if (callbacks != null) {
2608 callbacks.bindAllApplications(added);
2609 if (DEBUG_LOADERS) {
2610 Log.d(TAG, "bound " + added.size() + " apps in "
2611 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2612 }
2613 } else {
2614 Log.i(TAG, "not binding apps: no Launcher activity");
2615 }
2616 }
2617 });
2618
Joe Onorato36115782010-06-17 13:28:48 -04002619 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002620 Log.d(TAG, "Icons processed in "
2621 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002622 }
2623 }
2624
2625 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002626 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002627 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2628 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2629 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2630 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2631 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2632 }
Joe Onorato36115782010-06-17 13:28:48 -04002633 }
2634 }
2635
2636 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002637 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002638 }
2639
2640 private class PackageUpdatedTask implements Runnable {
2641 int mOp;
2642 String[] mPackages;
2643
2644 public static final int OP_NONE = 0;
2645 public static final int OP_ADD = 1;
2646 public static final int OP_UPDATE = 2;
2647 public static final int OP_REMOVE = 3; // uninstlled
2648 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2649
2650
2651 public PackageUpdatedTask(int op, String[] packages) {
2652 mOp = op;
2653 mPackages = packages;
2654 }
2655
2656 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002657 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002658
2659 final String[] packages = mPackages;
2660 final int N = packages.length;
2661 switch (mOp) {
2662 case OP_ADD:
2663 for (int i=0; i<N; i++) {
2664 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002665 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002666 }
2667 break;
2668 case OP_UPDATE:
2669 for (int i=0; i<N; i++) {
2670 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002671 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002672 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002673 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002674 }
2675 break;
2676 case OP_REMOVE:
2677 case OP_UNAVAILABLE:
2678 for (int i=0; i<N; i++) {
2679 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002680 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002681 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002682 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002683 }
2684 break;
2685 }
2686
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002687 ArrayList<AppInfo> added = null;
2688 ArrayList<AppInfo> modified = null;
2689 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002690
Adam Cohen487f7dd2012-06-28 18:12:10 -07002691 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002692 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002693 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002694 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002695 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002696 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002697 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002698 }
Winson Chung5d55f332012-07-16 20:45:03 -07002699 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002700 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002701 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002702 }
2703
Joe Onorato36115782010-06-17 13:28:48 -04002704 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2705 if (callbacks == null) {
2706 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2707 return;
2708 }
2709
2710 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002711 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002712 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002713 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002714 addAndBindAddedWorkspaceApps(context, addedInfos);
2715 } else {
2716 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002717 }
Joe Onorato36115782010-06-17 13:28:48 -04002718 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002719
Joe Onorato36115782010-06-17 13:28:48 -04002720 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002721 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002722
2723 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002724 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002725 ArrayList<ItemInfo> infos =
2726 getItemInfoForComponentName(a.componentName);
2727 for (ItemInfo i : infos) {
2728 if (isShortcutInfoUpdateable(i)) {
2729 ShortcutInfo info = (ShortcutInfo) i;
2730 info.title = a.title.toString();
2731 updateItemInDatabase(context, info);
2732 }
2733 }
2734 }
2735
Joe Onorato36115782010-06-17 13:28:48 -04002736 mHandler.post(new Runnable() {
2737 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002738 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2739 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002740 callbacks.bindAppsUpdated(modifiedFinal);
2741 }
2742 }
2743 });
2744 }
Winson Chung83892cc2013-05-01 16:53:33 -07002745
Winson Chungdf95eb12013-10-16 14:57:07 -07002746 final ArrayList<String> removedPackageNames =
2747 new ArrayList<String>();
2748 if (mOp == OP_REMOVE) {
2749 // Mark all packages in the broadcast to be removed
2750 removedPackageNames.addAll(Arrays.asList(packages));
2751 } else if (mOp == OP_UPDATE) {
2752 // Mark disabled packages in the broadcast to be removed
2753 final PackageManager pm = context.getPackageManager();
2754 for (int i=0; i<N; i++) {
2755 if (isPackageDisabled(pm, packages[i])) {
2756 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002757 }
2758 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002759 }
2760 // Remove all the components associated with this package
2761 for (String pn : removedPackageNames) {
2762 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2763 for (ItemInfo i : infos) {
2764 deleteItemFromDatabase(context, i);
2765 }
2766 }
2767 // Remove all the specific components
2768 for (AppInfo a : removedApps) {
2769 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2770 for (ItemInfo i : infos) {
2771 deleteItemFromDatabase(context, i);
2772 }
2773 }
2774 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2775 // Remove any queued items from the install queue
2776 String spKey = LauncherAppState.getSharedPreferencesKey();
2777 SharedPreferences sp =
2778 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2779 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2780 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002781 mHandler.post(new Runnable() {
2782 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002783 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2784 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002785 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002786 }
2787 }
2788 });
Joe Onoratobe386092009-11-17 17:32:16 -08002789 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002790
Michael Jurkac402cd92013-05-20 15:49:32 +02002791 final ArrayList<Object> widgetsAndShortcuts =
2792 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002793 mHandler.post(new Runnable() {
2794 @Override
2795 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002796 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2797 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002798 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002799 }
2800 }
2801 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002802
2803 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002804 mHandler.post(new Runnable() {
2805 public void run() {
2806 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2807 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002808 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002809 }
2810 }
2811 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002812 }
2813 }
2814
Michael Jurkac402cd92013-05-20 15:49:32 +02002815 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2816 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2817 PackageManager packageManager = context.getPackageManager();
2818 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2819 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2820 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2821 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2822 Collections.sort(widgetsAndShortcuts,
2823 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2824 return widgetsAndShortcuts;
2825 }
2826
Adam Cohen556f6132014-01-15 15:18:08 -08002827 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002828 try {
2829 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2830 return !pi.applicationInfo.enabled;
2831 } catch (NameNotFoundException e) {
2832 // Fall through
2833 }
2834 return false;
2835 }
Adam Cohen556f6132014-01-15 15:18:08 -08002836
2837 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002838 if (cn == null) {
2839 return false;
2840 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002841 if (isPackageDisabled(pm, cn.getPackageName())) {
2842 return false;
2843 }
Winson Chungee055712013-07-30 14:46:24 -07002844
2845 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002846 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002847 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002848 return (pm.getActivityInfo(cn, 0) != null);
2849 } catch (NameNotFoundException e) {
2850 return false;
2851 }
2852 }
2853
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002855 * Make an ShortcutInfo object for a restored application or shortcut item that points
2856 * to a package that is not yet installed on the system.
2857 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002858 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002859 final ShortcutInfo info = new ShortcutInfo();
2860 info.usingFallbackIcon = true;
2861 info.setIcon(getFallbackIcon());
2862 if (cursor != null) {
2863 info.title = cursor.getString(titleIndex);
2864 } else {
2865 info.title = "";
2866 }
2867 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002868 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002869 return info;
2870 }
2871
2872 /**
2873 * Make an Intent object for a restored application or shortcut item that points
2874 * to the market page for the item.
2875 */
2876 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002877 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002878 ComponentName componentName = intent.getComponent();
2879 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2880 Uri marketUri = new Uri.Builder()
2881 .scheme("market")
2882 .authority("details")
2883 .appendQueryParameter("id", componentName.getPackageName())
2884 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002885 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002886 marketIntent.setData(marketUri);
2887 return marketIntent;
2888 }
2889
2890 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002891 * This is called from the code that adds shortcuts from the intent receiver. This
2892 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002893 */
Joe Onorato56d82912010-03-07 14:32:10 -05002894 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002895 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897
Joe Onorato56d82912010-03-07 14:32:10 -05002898 /**
2899 * Make an ShortcutInfo object for a shortcut that is an application.
2900 *
2901 * If c is not null, then it will be used to fill in missing data like the title and icon.
2902 */
2903 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002904 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002905 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002906 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002907 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002908 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002909 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002910 } else {
2911 try {
2912 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2913 info.initFlagsAndFirstInstallTime(pi);
2914 } catch (NameNotFoundException e) {
2915 Log.d(TAG, "getPackInfo failed for package " +
2916 componentName.getPackageName());
2917 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002918 }
2919
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002920 // TODO: See if the PackageManager knows about this case. If it doesn't
2921 // then return null & delete this.
2922
Joe Onorato56d82912010-03-07 14:32:10 -05002923 // the resource -- This may implicitly give us back the fallback icon,
2924 // but don't worry about that. All we're doing with usingFallbackIcon is
2925 // to avoid saving lots of copies of that in the database, and most apps
2926 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002927
2928 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2929 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2930 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002931 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002932 ResolveInfo resolveInfo = null;
2933 ComponentName oldComponent = intent.getComponent();
2934 Intent newIntent = new Intent(intent.getAction(), null);
2935 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2936 newIntent.setPackage(oldComponent.getPackageName());
2937 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2938 for (ResolveInfo i : infos) {
2939 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2940 i.activityInfo.name);
2941 if (cn.equals(oldComponent)) {
2942 resolveInfo = i;
2943 }
2944 }
2945 if (resolveInfo == null) {
2946 resolveInfo = manager.resolveActivity(intent, 0);
2947 }
Joe Onorato56d82912010-03-07 14:32:10 -05002948 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002949 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002950 }
Joe Onorato56d82912010-03-07 14:32:10 -05002951 // the db
2952 if (icon == null) {
2953 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002954 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002955 }
2956 }
2957 // the fallback icon
2958 if (icon == null) {
2959 icon = getFallbackIcon();
2960 info.usingFallbackIcon = true;
2961 }
2962 info.setIcon(icon);
2963
2964 // from the resource
2965 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002966 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2967 if (labelCache != null && labelCache.containsKey(key)) {
2968 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002969 } else {
2970 info.title = resolveInfo.activityInfo.loadLabel(manager);
2971 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002972 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002973 }
2974 }
Joe Onorato56d82912010-03-07 14:32:10 -05002975 }
2976 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002978 if (c != null) {
2979 info.title = c.getString(titleIndex);
2980 }
2981 }
2982 // fall back to the class name of the activity
2983 if (info.title == null) {
2984 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002986 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2987 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002988 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002989
Winson Chung64359a52013-07-08 17:17:08 -07002990 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2991 ItemInfoFilter f) {
2992 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2993 for (ItemInfo i : infos) {
2994 if (i instanceof ShortcutInfo) {
2995 ShortcutInfo info = (ShortcutInfo) i;
2996 ComponentName cn = info.intent.getComponent();
2997 if (cn != null && f.filterItem(null, info, cn)) {
2998 filtered.add(info);
2999 }
3000 } else if (i instanceof FolderInfo) {
3001 FolderInfo info = (FolderInfo) i;
3002 for (ShortcutInfo s : info.contents) {
3003 ComponentName cn = s.intent.getComponent();
3004 if (cn != null && f.filterItem(info, s, cn)) {
3005 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003006 }
3007 }
Winson Chung64359a52013-07-08 17:17:08 -07003008 } else if (i instanceof LauncherAppWidgetInfo) {
3009 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3010 ComponentName cn = info.providerName;
3011 if (cn != null && f.filterItem(null, info, cn)) {
3012 filtered.add(info);
3013 }
Winson Chung8a435102012-08-30 17:16:53 -07003014 }
3015 }
Winson Chung64359a52013-07-08 17:17:08 -07003016 return new ArrayList<ItemInfo>(filtered);
3017 }
3018
3019 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003020 ItemInfoFilter filter = new ItemInfoFilter() {
3021 @Override
3022 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3023 return cn.getPackageName().equals(pn);
3024 }
3025 };
3026 return filterItemInfos(sBgItemsIdMap.values(), filter);
3027 }
3028
3029 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003030 ItemInfoFilter filter = new ItemInfoFilter() {
3031 @Override
3032 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3033 return cn.equals(cname);
3034 }
3035 };
3036 return filterItemInfos(sBgItemsIdMap.values(), filter);
3037 }
3038
3039 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3040 if (i instanceof ShortcutInfo) {
3041 ShortcutInfo info = (ShortcutInfo) i;
3042 // We need to check for ACTION_MAIN otherwise getComponent() might
3043 // return null for some shortcuts (for instance, for shortcuts to
3044 // web pages.)
3045 Intent intent = info.intent;
3046 ComponentName name = intent.getComponent();
3047 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3048 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3049 return true;
3050 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003051 // placeholder shortcuts get special treatment, let them through too.
3052 if (info.getRestoredIntent() != null) {
3053 return true;
3054 }
Winson Chung64359a52013-07-08 17:17:08 -07003055 }
3056 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003057 }
3058
3059 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003060 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003062 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003063 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3064 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065
Joe Onorato56d82912010-03-07 14:32:10 -05003066 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003067 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003068 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003070 // TODO: If there's an explicit component and we can't install that, delete it.
3071
Joe Onorato56d82912010-03-07 14:32:10 -05003072 info.title = c.getString(titleIndex);
3073
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 int iconType = c.getInt(iconTypeIndex);
3075 switch (iconType) {
3076 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3077 String packageName = c.getString(iconPackageIndex);
3078 String resourceName = c.getString(iconResourceIndex);
3079 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003080 info.customIcon = false;
3081 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003084 if (resources != null) {
3085 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003086 icon = Utilities.createIconBitmap(
3087 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003088 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003089 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003090 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 }
Joe Onorato56d82912010-03-07 14:32:10 -05003092 // the db
3093 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003094 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003095 }
3096 // the fallback icon
3097 if (icon == null) {
3098 icon = getFallbackIcon();
3099 info.usingFallbackIcon = true;
3100 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 break;
3102 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003103 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003104 if (icon == null) {
3105 icon = getFallbackIcon();
3106 info.customIcon = false;
3107 info.usingFallbackIcon = true;
3108 } else {
3109 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 break;
3112 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003113 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003114 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 info.customIcon = false;
3116 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003118 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003119 return info;
3120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121
Michael Jurka931dc972011-08-05 15:08:15 -07003122 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003123 @SuppressWarnings("all") // suppress dead code warning
3124 final boolean debug = false;
3125 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003126 Log.d(TAG, "getIconFromCursor app="
3127 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3128 }
3129 byte[] data = c.getBlob(iconIndex);
3130 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003131 return Utilities.createIconBitmap(
3132 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003133 } catch (Exception e) {
3134 return null;
3135 }
3136 }
3137
Winson Chung3d503fb2011-07-13 17:25:49 -07003138 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3139 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003140 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003141 if (info == null) {
3142 return null;
3143 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003144 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003145
3146 return info;
3147 }
3148
Winson Chunga9abd0e2010-10-27 17:18:37 -07003149 /**
Winson Chung55cef262010-10-28 14:14:18 -07003150 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3151 */
3152 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3153 ComponentName component) {
3154 List<AppWidgetProviderInfo> widgets =
3155 AppWidgetManager.getInstance(context).getInstalledProviders();
3156 for (AppWidgetProviderInfo info : widgets) {
3157 if (info.provider.equals(component)) {
3158 return info;
3159 }
3160 }
3161 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003162 }
3163
Winson Chung68846fd2010-10-29 11:00:27 -07003164 /**
3165 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3166 */
3167 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3168 final PackageManager packageManager = context.getPackageManager();
3169 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3170 new ArrayList<WidgetMimeTypeHandlerData>();
3171
3172 final Intent supportsIntent =
3173 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3174 supportsIntent.setType(mimeType);
3175
3176 // Create a set of widget configuration components that we can test against
3177 final List<AppWidgetProviderInfo> widgets =
3178 AppWidgetManager.getInstance(context).getInstalledProviders();
3179 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3180 new HashMap<ComponentName, AppWidgetProviderInfo>();
3181 for (AppWidgetProviderInfo info : widgets) {
3182 configurationComponentToWidget.put(info.configure, info);
3183 }
3184
3185 // Run through each of the intents that can handle this type of clip data, and cross
3186 // reference them with the components that are actual configuration components
3187 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3188 PackageManager.MATCH_DEFAULT_ONLY);
3189 for (ResolveInfo info : activities) {
3190 final ActivityInfo activityInfo = info.activityInfo;
3191 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3192 activityInfo.name);
3193 if (configurationComponentToWidget.containsKey(infoComponent)) {
3194 supportedConfigurationActivities.add(
3195 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3196 configurationComponentToWidget.get(infoComponent)));
3197 }
3198 }
3199 return supportedConfigurationActivities;
3200 }
3201
Winson Chunga9abd0e2010-10-27 17:18:37 -07003202 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003203 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3204 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3205 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3206
Adam Cohend9198822011-11-22 16:42:47 -08003207 if (intent == null) {
3208 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3209 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3210 return null;
3211 }
3212
Joe Onorato0589f0f2010-02-08 13:44:00 -08003213 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003214 boolean customIcon = false;
3215 ShortcutIconResource iconResource = null;
3216
3217 if (bitmap != null && bitmap instanceof Bitmap) {
3218 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003219 customIcon = true;
3220 } else {
3221 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3222 if (extra != null && extra instanceof ShortcutIconResource) {
3223 try {
3224 iconResource = (ShortcutIconResource) extra;
3225 final PackageManager packageManager = context.getPackageManager();
3226 Resources resources = packageManager.getResourcesForApplication(
3227 iconResource.packageName);
3228 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003229 icon = Utilities.createIconBitmap(
3230 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003231 } catch (Exception e) {
3232 Log.w(TAG, "Could not load shortcut icon: " + extra);
3233 }
3234 }
3235 }
3236
Michael Jurkac9d95c52011-08-29 14:03:34 -07003237 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003238
3239 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003240 if (fallbackIcon != null) {
3241 icon = fallbackIcon;
3242 } else {
3243 icon = getFallbackIcon();
3244 info.usingFallbackIcon = true;
3245 }
Joe Onorato56d82912010-03-07 14:32:10 -05003246 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003247 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003248
Joe Onorato0589f0f2010-02-08 13:44:00 -08003249 info.title = name;
3250 info.intent = intent;
3251 info.customIcon = customIcon;
3252 info.iconResource = iconResource;
3253
3254 return info;
3255 }
3256
Winson Chungaac01e12011-08-17 10:37:13 -07003257 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3258 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003259 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003260 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003261 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003262 }
Joe Onorato56d82912010-03-07 14:32:10 -05003263 // If this icon doesn't have a custom icon, check to see
3264 // what's stored in the DB, and if it doesn't match what
3265 // we're going to show, store what we are going to show back
3266 // into the DB. We do this so when we're loading, if the
3267 // package manager can't find an icon (for example because
3268 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003269 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003270 cache.put(info, c.getBlob(iconIndex));
3271 return true;
3272 }
3273 return false;
3274 }
3275 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3276 boolean needSave = false;
3277 try {
3278 if (data != null) {
3279 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3280 Bitmap loaded = info.getIcon(mIconCache);
3281 needSave = !saved.sameAs(loaded);
3282 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003283 needSave = true;
3284 }
Winson Chungaac01e12011-08-17 10:37:13 -07003285 } catch (Exception e) {
3286 needSave = true;
3287 }
3288 if (needSave) {
3289 Log.d(TAG, "going to save icon bitmap for info=" + info);
3290 // This is slower than is ideal, but this only happens once
3291 // or when the app is updated with a new icon.
3292 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003293 }
3294 }
3295
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003297 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 * or make a new one.
3299 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003300 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 // See if a placeholder was created for us already
3302 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003303 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003305 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003306 folders.put(id, folderInfo);
3307 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003308 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003309 }
3310
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003311 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003312 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003313 return new Comparator<AppInfo>() {
3314 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003315 int result = collator.compare(a.title.toString().trim(),
3316 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003317 if (result == 0) {
3318 result = a.componentName.compareTo(b.componentName);
3319 }
3320 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003321 }
Winson Chung11904872012-09-17 16:58:46 -07003322 };
3323 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003324 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3325 = new Comparator<AppInfo>() {
3326 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003327 if (a.firstInstallTime < b.firstInstallTime) return 1;
3328 if (a.firstInstallTime > b.firstInstallTime) return -1;
3329 return 0;
3330 }
3331 };
Winson Chung11904872012-09-17 16:58:46 -07003332 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3333 final Collator collator = Collator.getInstance();
3334 return new Comparator<AppWidgetProviderInfo>() {
3335 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003336 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003337 }
3338 };
3339 }
Winson Chung5308f242011-08-18 12:12:41 -07003340 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3341 if (info.activityInfo != null) {
3342 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3343 } else {
3344 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3345 }
3346 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003347 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003348 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003349 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003350 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003351 ShortcutNameComparator(PackageManager pm) {
3352 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003353 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003354 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003355 }
3356 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3357 mPackageManager = pm;
3358 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003359 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003360 }
3361 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003362 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003363 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3364 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3365 if (mLabelCache.containsKey(keyA)) {
3366 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003367 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003368 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003369
Winson Chung5308f242011-08-18 12:12:41 -07003370 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003371 }
Winson Chung5308f242011-08-18 12:12:41 -07003372 if (mLabelCache.containsKey(keyB)) {
3373 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003374 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003375 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003376
Winson Chung5308f242011-08-18 12:12:41 -07003377 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003378 }
Winson Chung11904872012-09-17 16:58:46 -07003379 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003380 }
3381 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003382 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003383 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003384 private PackageManager mPackageManager;
3385 private HashMap<Object, String> mLabelCache;
3386 WidgetAndShortcutNameComparator(PackageManager pm) {
3387 mPackageManager = pm;
3388 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003389 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003390 }
3391 public final int compare(Object a, Object b) {
3392 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003393 if (mLabelCache.containsKey(a)) {
3394 labelA = mLabelCache.get(a);
3395 } else {
3396 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003397 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003398 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003399 mLabelCache.put(a, labelA);
3400 }
3401 if (mLabelCache.containsKey(b)) {
3402 labelB = mLabelCache.get(b);
3403 } else {
3404 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003405 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003406 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003407 mLabelCache.put(b, labelB);
3408 }
Winson Chung11904872012-09-17 16:58:46 -07003409 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003410 }
3411 };
Joe Onoratobe386092009-11-17 17:32:16 -08003412
3413 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003414 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003415 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3416 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3417 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3418 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003419 if (mLoaderTask != null) {
3420 mLoaderTask.dumpState();
3421 } else {
3422 Log.d(TAG, "mLoaderTask=null");
3423 }
Joe Onoratobe386092009-11-17 17:32:16 -08003424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003425}