blob: 159ddb016b40bb0c72a35541431cc9f323f64d4c [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070031import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070035import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Resources;
37import android.database.Cursor;
38import android.graphics.Bitmap;
39import android.graphics.BitmapFactory;
40import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080041import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040042import android.os.Handler;
43import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Winson Chung68846fd2010-10-29 11:00:27 -070050import com.android.launcher.R;
51import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080052
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.lang.ref.WeakReference;
54import java.net.URISyntaxException;
55import java.text.Collator;
56import java.util.ArrayList;
57import java.util.Collections;
58import java.util.Comparator;
59import java.util.HashMap;
60import java.util.List;
Michael Jurkac2f801e2011-07-12 14:19:46 -070061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062/**
63 * Maintains in-memory state of the Launcher. It is expected that there should be only one
64 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070065 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040067public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080068 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040069 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070070
Joe Onorato36115782010-06-17 13:28:48 -040071 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080072 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040073 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040074 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040075
Joe Onoratof99f8c12009-10-31 17:27:36 -040076 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040077 private final Object mLock = new Object();
78 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040079 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070081 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
82 static {
83 sWorkerThread.start();
84 }
85 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
86
Joe Onoratocc67f472010-06-08 10:54:30 -070087 // We start off with everything not loaded. After that, we assume that
88 // our monitoring of the package manager provides all updates and we never
89 // need to do a requery. These are only ever touched from the loader thread.
90 private boolean mWorkspaceLoaded;
91 private boolean mAllAppsLoaded;
92
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Michael Jurkaa8c760d2011-04-28 14:59:33 -070095 // < only access in worker thread >
96 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080097
Michael Jurkaa8c760d2011-04-28 14:59:33 -070098 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
99 // LauncherModel to their ids
100 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
101
102 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
103 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
104 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700105 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700106
107 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
108 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
109 new ArrayList<LauncherAppWidgetInfo>();
110
111 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
112 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700113
114 // sDbIconCache is the set of ItemInfos that need to have their icons updated in the database
115 static final HashMap<Object, byte[]> sDbIconCache = new HashMap<Object, byte[]>();
116
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700117 // </ only access in worker thread >
118
119 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800120 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Adam Cohend22015c2010-07-26 22:02:18 -0700122 private static int mCellCountX;
123 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700124
Reena Lee99a73f32011-10-24 17:27:37 -0700125 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700126
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700128 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400129 public int getCurrentWorkspaceScreen();
130 public void startBinding();
131 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500132 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400133 public void finishBindingItems();
134 public void bindAppWidget(LauncherAppWidgetInfo info);
135 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500136 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
137 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400138 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700139 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400140 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100142 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800146 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400147 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 mAllAppsList = new AllAppsList(iconCache);
149 mIconCache = iconCache;
150
151 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700152 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400153
Reena Lee93f824a2011-09-23 17:20:28 -0700154 final Resources res = app.getResources();
155 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
156 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700157 Configuration config = res.getConfiguration();
158 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800159 }
160
Joe Onorato56d82912010-03-07 14:32:10 -0500161 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800162 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chung603bcb92011-09-02 11:45:39 -0700165 public void unbindWorkspaceItems() {
166 sWorker.post(new Runnable() {
167 @Override
168 public void run() {
169 unbindWorkspaceItemsOnMainThread();
170 }
171 });
172 }
173
174 /** Unbinds all the sWorkspaceItems on the main thread, and return a copy of sWorkspaceItems
175 * that is save to reference from the main thread. */
176 private ArrayList<ItemInfo> unbindWorkspaceItemsOnMainThread() {
177 // Ensure that we don't use the same workspace items data structure on the main thread
178 // by making a copy of workspace items first.
179 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(sWorkspaceItems);
Michael Jurka05bf644e2011-11-30 20:28:53 -0800180 final ArrayList<ItemInfo> appWidgets = new ArrayList<ItemInfo>(sAppWidgets);
Winson Chung603bcb92011-09-02 11:45:39 -0700181 mHandler.post(new Runnable() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700182 @Override
Winson Chung603bcb92011-09-02 11:45:39 -0700183 public void run() {
184 for (ItemInfo item : workspaceItems) {
185 item.unbind();
186 }
Michael Jurka05bf644e2011-11-30 20:28:53 -0800187 for (ItemInfo item : appWidgets) {
188 item.unbind();
189 }
Winson Chung603bcb92011-09-02 11:45:39 -0700190 }
191 });
192
193 return workspaceItems;
Adam Cohen4eac29a2011-07-11 17:53:37 -0700194 }
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 /**
197 * Adds an item to the DB if it was not created previously, or move it to a new
198 * <container, screen, cellX, cellY>
199 */
200 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
201 int screen, int cellX, int cellY) {
202 if (item.container == ItemInfo.NO_ID) {
203 // From all apps
204 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
205 } else {
206 // From somewhere else
207 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 }
209 }
210
Michael Jurkac9d95c52011-08-29 14:03:34 -0700211 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
212 final ItemInfo item, final String callingFunction) {
213 final long itemId = item.id;
214 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
215 final ContentResolver cr = context.getContentResolver();
216
217 Runnable r = new Runnable() {
218 public void run() {
219 cr.update(uri, values, null, null);
220
221 ItemInfo modelItem = sItemsIdMap.get(itemId);
222 if (item != modelItem) {
223 // the modelItem needs to match up perfectly with item if our model is to be
224 // consistent with the database-- for now, just require modelItem == item
225 String msg = "item: " + ((item != null) ? item.toString() : "null") +
226 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
227 "Error: ItemInfo passed to " + callingFunction + " doesn't match original";
228 throw new RuntimeException(msg);
229 }
230
231 // Items are added/removed from the corresponding FolderInfo elsewhere, such
232 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
233 // that are on the desktop, as appropriate
234 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
235 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
236 if (!sWorkspaceItems.contains(modelItem)) {
237 sWorkspaceItems.add(modelItem);
238 }
239 } else {
240 sWorkspaceItems.remove(modelItem);
241 }
242 }
243 };
244
245 if (sWorkerThread.getThreadId() == Process.myTid()) {
246 r.run();
247 } else {
248 sWorker.post(r);
249 }
250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700253 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700254 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
255 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257 item.cellX = cellX;
258 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700259
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 // We store hotseat items in canonical form which is this orientation invariant position
261 // in the hotseat
262 if (context instanceof Launcher && screen < 0 &&
263 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
264 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
265 } else {
266 item.screen = screen;
267 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268
269 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700271 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
272 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
274
Michael Jurkac9d95c52011-08-29 14:03:34 -0700275 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700276 }
277
278 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800279 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800280 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700281 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
282 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800283 item.spanX = spanX;
284 item.spanY = spanY;
285 item.cellX = cellX;
286 item.cellY = cellY;
287
Adam Cohend4844c32011-02-18 19:25:06 -0800288 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800289 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
290 values.put(LauncherSettings.Favorites.SPANX, spanX);
291 values.put(LauncherSettings.Favorites.SPANY, spanY);
292 values.put(LauncherSettings.Favorites.CELLX, cellX);
293 values.put(LauncherSettings.Favorites.CELLY, cellY);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700294 updateItemInDatabaseHelper(context, values, item, "resizeItemInDatabase");
295 }
Adam Cohend4844c32011-02-18 19:25:06 -0800296
Michael Jurkac9d95c52011-08-29 14:03:34 -0700297
298 /**
299 * Update an item to the database in a specified container.
300 */
301 static void updateItemInDatabase(Context context, final ItemInfo item) {
302 final ContentValues values = new ContentValues();
303 item.onAddToDatabase(values);
304 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
305 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800306 }
307
308 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 * Returns true if the shortcuts already exists in the database.
310 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312 static boolean shortcutExists(Context context, String title, Intent intent) {
313 final ContentResolver cr = context.getContentResolver();
314 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
315 new String[] { "title", "intent" }, "title=? and intent=?",
316 new String[] { title, intent.toUri(0) }, null);
317 boolean result = false;
318 try {
319 result = c.moveToFirst();
320 } finally {
321 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400323 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700324 }
325
Joe Onorato9c1289c2009-08-17 11:03:03 -0400326 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700327 * Returns an ItemInfo array containing all the items in the LauncherModel.
328 * The ItemInfo.id is not set through this function.
329 */
330 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
331 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
332 final ContentResolver cr = context.getContentResolver();
333 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
334 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
335 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
336 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
337
338 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
339 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
340 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
341 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
342 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
343 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
344 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
345
346 try {
347 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700348 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700349 item.cellX = c.getInt(cellXIndex);
350 item.cellY = c.getInt(cellYIndex);
351 item.spanX = c.getInt(spanXIndex);
352 item.spanY = c.getInt(spanYIndex);
353 item.container = c.getInt(containerIndex);
354 item.itemType = c.getInt(itemTypeIndex);
355 item.screen = c.getInt(screenIndex);
356
357 items.add(item);
358 }
359 } catch (Exception e) {
360 items.clear();
361 } finally {
362 c.close();
363 }
364
365 return items;
366 }
367
368 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
370 */
371 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
372 final ContentResolver cr = context.getContentResolver();
373 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
374 "_id=? and (itemType=? or itemType=?)",
375 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700376 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700377
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 try {
379 if (c.moveToFirst()) {
380 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
381 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
382 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
383 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
384 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
385 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 FolderInfo folderInfo = null;
388 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700389 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
390 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700392 }
393
Joe Onorato9c1289c2009-08-17 11:03:03 -0400394 folderInfo.title = c.getString(titleIndex);
395 folderInfo.id = id;
396 folderInfo.container = c.getInt(containerIndex);
397 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700398 folderInfo.cellX = c.getInt(cellXIndex);
399 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400
401 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700402 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 } finally {
404 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700405 }
406
407 return null;
408 }
409
Joe Onorato9c1289c2009-08-17 11:03:03 -0400410 /**
411 * Add an item to the database in a specified container. Sets the container, screen, cellX and
412 * cellY fields of the item. Also assigns an ID to the item.
413 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700414 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
415 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400416 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400417 item.cellX = cellX;
418 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700419 // We store hotseat items in canonical form which is this orientation invariant position
420 // in the hotseat
421 if (context instanceof Launcher && screen < 0 &&
422 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
423 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
424 } else {
425 item.screen = screen;
426 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427
428 final ContentValues values = new ContentValues();
429 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 item.onAddToDatabase(values);
431
Michael Jurka7578ec62011-08-03 14:11:54 -0700432 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700433 item.id = app.getLauncherProvider().generateNewId();
434 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700435 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700436
Michael Jurkac9d95c52011-08-29 14:03:34 -0700437 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700438 public void run() {
439 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
440 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441
Winson Chungb1094bd2011-08-24 16:14:08 -0700442 if (sItemsIdMap.containsKey(item.id)) {
443 // we should not be adding new items in the db with the same id
444 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
445 "addItemToDatabase already exists." + item.toString());
446 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700447 sItemsIdMap.put(item.id, item);
448 switch (item.itemType) {
449 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
450 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700451 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700452 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
453 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700454 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
455 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700456 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700457 }
458 break;
459 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
460 sAppWidgets.add((LauncherAppWidgetInfo) item);
461 break;
462 }
463 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700464 };
465
466 if (sWorkerThread.getThreadId() == Process.myTid()) {
467 r.run();
468 } else {
469 sWorker.post(r);
470 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700471 }
472
Joe Onorato9c1289c2009-08-17 11:03:03 -0400473 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700474 * Creates a new unique child id, for a given cell span across all layouts.
475 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700476 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700477 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
478 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700479 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700480 }
481
Adam Cohend22015c2010-07-26 22:02:18 -0700482 static int getCellCountX() {
483 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700484 }
485
Adam Cohend22015c2010-07-26 22:02:18 -0700486 static int getCellCountY() {
487 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700488 }
489
490 /**
491 * Updates the model orientation helper to take into account the current layout dimensions
492 * when performing local/canonical coordinate transformations.
493 */
494 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700495 mCellCountX = shortAxisCellCount;
496 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700497 }
498
499 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700500 * Removes the specified item from the database
501 * @param context
502 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400503 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700504 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400505 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700506 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Michael Jurka83df1882011-08-31 20:59:26 -0700507 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700508 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700509 cr.delete(uriToDelete, null, null);
510 switch (item.itemType) {
511 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
512 sFolders.remove(item.id);
513 sWorkspaceItems.remove(item);
514 break;
515 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
516 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
517 sWorkspaceItems.remove(item);
518 break;
519 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
520 sAppWidgets.remove((LauncherAppWidgetInfo) item);
521 break;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700522 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700523 sItemsIdMap.remove(item.id);
524 sDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700525 }
Michael Jurka83df1882011-08-31 20:59:26 -0700526 };
527 if (sWorkerThread.getThreadId() == Process.myTid()) {
528 r.run();
529 } else {
530 sWorker.post(r);
531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 }
533
534 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 * Remove the contents of the specified folder from the database
536 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700537 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 final ContentResolver cr = context.getContentResolver();
539
Michael Jurkac9d95c52011-08-29 14:03:34 -0700540 Runnable r = new Runnable() {
541 public void run() {
542 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
543 sItemsIdMap.remove(info.id);
544 sFolders.remove(info.id);
545 sDbIconCache.remove(info);
546 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700547
Michael Jurkac9d95c52011-08-29 14:03:34 -0700548 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
549 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
550 for (ItemInfo childInfo : info.contents) {
551 sItemsIdMap.remove(childInfo.id);
552 sDbIconCache.remove(childInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700553 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700554 }
555 };
556 if (sWorkerThread.getThreadId() == Process.myTid()) {
557 r.run();
558 } else {
559 sWorker.post(r);
560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400561 }
562
563 /**
564 * Set this as the current Launcher activity object for the loader.
565 */
566 public void initialize(Callbacks callbacks) {
567 synchronized (mLock) {
568 mCallbacks = new WeakReference<Callbacks>(callbacks);
569 }
570 }
571
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700572 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
574 * ACTION_PACKAGE_CHANGED.
575 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100576 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400577 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400578 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700579
Joe Onorato36115782010-06-17 13:28:48 -0400580 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400581
Joe Onorato36115782010-06-17 13:28:48 -0400582 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
583 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
584 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
585 final String packageName = intent.getData().getSchemeSpecificPart();
586 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400587
Joe Onorato36115782010-06-17 13:28:48 -0400588 int op = PackageUpdatedTask.OP_NONE;
589
590 if (packageName == null || packageName.length() == 0) {
591 // they sent us a bad intent
592 return;
593 }
594
595 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
596 op = PackageUpdatedTask.OP_UPDATE;
597 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
598 if (!replacing) {
599 op = PackageUpdatedTask.OP_REMOVE;
600 }
601 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
602 // later, we will update the package at this time
603 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
604 if (!replacing) {
605 op = PackageUpdatedTask.OP_ADD;
606 } else {
607 op = PackageUpdatedTask.OP_UPDATE;
608 }
609 }
610
611 if (op != PackageUpdatedTask.OP_NONE) {
612 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
613 }
614
615 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400616 // First, schedule to add these apps back in.
617 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
618 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
619 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700620 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400621 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
622 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
623 enqueuePackageUpdated(new PackageUpdatedTask(
624 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700625 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700626 // If we have changed locale we need to clear out the labels in all apps/workspace.
627 forceReload();
628 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
629 // Check if configuration change was an mcc/mnc change which would affect app resources
630 // and we would need to clear out the labels in all apps/workspace. Same handling as
631 // above for ACTION_LOCALE_CHANGED
632 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700633 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700634 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700635 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700636 forceReload();
637 }
638 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700639 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700640 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
641 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700642 if (mCallbacks != null) {
643 Callbacks callbacks = mCallbacks.get();
644 if (callbacks != null) {
645 callbacks.bindSearchablesChanged();
646 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700647 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700648 }
649 }
650
Reena Lee93f824a2011-09-23 17:20:28 -0700651 private void forceReload() {
652 synchronized (mLock) {
653 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
654 // mWorkspaceLoaded to true later
655 stopLoaderLocked();
656 mAllAppsLoaded = false;
657 mWorkspaceLoaded = false;
658 }
659 // Do this here because if the launcher activity is running it will be restarted.
660 // If it's not running startLoaderFromBackground will merely tell it that it needs
661 // to reload.
662 startLoaderFromBackground();
663 }
664
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700665 /**
666 * When the launcher is in the background, it's possible for it to miss paired
667 * configuration changes. So whenever we trigger the loader from the background
668 * tell the launcher that it needs to re-run the loader when it comes back instead
669 * of doing it now.
670 */
671 public void startLoaderFromBackground() {
672 boolean runLoader = false;
673 if (mCallbacks != null) {
674 Callbacks callbacks = mCallbacks.get();
675 if (callbacks != null) {
676 // Only actually run the loader if they're not paused.
677 if (!callbacks.setLoadOnResume()) {
678 runLoader = true;
679 }
680 }
681 }
682 if (runLoader) {
683 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700684 }
Joe Onorato36115782010-06-17 13:28:48 -0400685 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400686
Reena Lee93f824a2011-09-23 17:20:28 -0700687 // If there is already a loader task running, tell it to stop.
688 // returns true if isLaunching() was true on the old task
689 private boolean stopLoaderLocked() {
690 boolean isLaunching = false;
691 LoaderTask oldTask = mLoaderTask;
692 if (oldTask != null) {
693 if (oldTask.isLaunching()) {
694 isLaunching = true;
695 }
696 oldTask.stopLocked();
697 }
698 return isLaunching;
699 }
700
Joe Onorato36115782010-06-17 13:28:48 -0400701 public void startLoader(Context context, boolean isLaunching) {
702 synchronized (mLock) {
703 if (DEBUG_LOADERS) {
704 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
705 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706
Joe Onorato36115782010-06-17 13:28:48 -0400707 // Don't bother to start the thread if we know it's not going to do anything
708 if (mCallbacks != null && mCallbacks.get() != null) {
709 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700710 // also, don't downgrade isLaunching if we're already running
711 isLaunching = isLaunching || stopLoaderLocked();
Joe Onorato36115782010-06-17 13:28:48 -0400712 mLoaderTask = new LoaderTask(context, isLaunching);
Winson Chung7ed37742011-09-08 15:45:51 -0700713 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700714 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 }
717 }
718
Joe Onorato36115782010-06-17 13:28:48 -0400719 public void stopLoader() {
720 synchronized (mLock) {
721 if (mLoaderTask != null) {
722 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 }
724 }
Joe Onorato36115782010-06-17 13:28:48 -0400725 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400726
Michael Jurkac57b7a82011-08-09 22:02:20 -0700727 public boolean isAllAppsLoaded() {
728 return mAllAppsLoaded;
729 }
730
Joe Onorato36115782010-06-17 13:28:48 -0400731 /**
732 * Runnable for the thread that loads the contents of the launcher:
733 * - workspace icons
734 * - widgets
735 * - all apps icons
736 */
737 private class LoaderTask implements Runnable {
738 private Context mContext;
739 private Thread mWaitThread;
740 private boolean mIsLaunching;
741 private boolean mStopped;
742 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700743 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400744
745 LoaderTask(Context context, boolean isLaunching) {
746 mContext = context;
747 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700748 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 }
750
Joe Onorato36115782010-06-17 13:28:48 -0400751 boolean isLaunching() {
752 return mIsLaunching;
753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754
Joe Onorato36115782010-06-17 13:28:48 -0400755 private void loadAndBindWorkspace() {
756 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400757 if (DEBUG_LOADERS) {
758 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700760
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700761 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400762 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700763 synchronized (LoaderTask.this) {
764 if (mStopped) {
765 return;
766 }
767 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 }
769 }
770
Joe Onorato36115782010-06-17 13:28:48 -0400771 // Bind the workspace
772 bindWorkspace();
773 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400774
Joe Onorato36115782010-06-17 13:28:48 -0400775 private void waitForIdle() {
776 // Wait until the either we're stopped or the other threads are done.
777 // This way we don't start loading all apps until the workspace has settled
778 // down.
779 synchronized (LoaderTask.this) {
780 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700781
Joe Onorato36115782010-06-17 13:28:48 -0400782 mHandler.postIdle(new Runnable() {
783 public void run() {
784 synchronized (LoaderTask.this) {
785 mLoadAndBindStepFinished = true;
786 if (DEBUG_LOADERS) {
787 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400788 }
Joe Onorato36115782010-06-17 13:28:48 -0400789 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400790 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400791 }
Joe Onorato36115782010-06-17 13:28:48 -0400792 });
793
794 while (!mStopped && !mLoadAndBindStepFinished) {
795 try {
796 this.wait();
797 } catch (InterruptedException ex) {
798 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400799 }
800 }
Joe Onorato36115782010-06-17 13:28:48 -0400801 if (DEBUG_LOADERS) {
802 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700803 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400804 + "ms for previous step to finish binding");
805 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400806 }
Joe Onorato36115782010-06-17 13:28:48 -0400807 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400808
Joe Onorato36115782010-06-17 13:28:48 -0400809 public void run() {
810 // Optimize for end-user experience: if the Launcher is up and // running with the
811 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
812 // workspace first (default).
813 final Callbacks cbk = mCallbacks.get();
814 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400815
Joe Onorato36115782010-06-17 13:28:48 -0400816 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400817 // Elevate priority when Home launches for the first time to avoid
818 // starving at boot time. Staring at a blank home is not cool.
819 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700820 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
821 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400822 android.os.Process.setThreadPriority(mIsLaunching
823 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
824 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400825 if (loadWorkspaceFirst) {
826 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
827 loadAndBindWorkspace();
828 } else {
829 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700830 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400831 }
832
Joe Onorato36115782010-06-17 13:28:48 -0400833 if (mStopped) {
834 break keep_running;
835 }
836
837 // Whew! Hard work done. Slow us down, and wait until the UI thread has
838 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400839 synchronized (mLock) {
840 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700841 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400842 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
843 }
844 }
Joe Onorato36115782010-06-17 13:28:48 -0400845 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400846
847 // second step
848 if (loadWorkspaceFirst) {
849 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700850 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400851 } else {
852 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
853 loadAndBindWorkspace();
854 }
Winson Chung7ed37742011-09-08 15:45:51 -0700855
856 // Restore the default thread priority after we are done loading items
857 synchronized (mLock) {
858 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
859 }
Joe Onorato36115782010-06-17 13:28:48 -0400860 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861
Winson Chungaac01e12011-08-17 10:37:13 -0700862
863 // Update the saved icons if necessary
864 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700865 for (Object key : sDbIconCache.keySet()) {
866 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700867 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700868 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700869
Joe Onorato36115782010-06-17 13:28:48 -0400870 // Clear out this reference, otherwise we end up holding it until all of the
871 // callback runnables are done.
872 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873
Joe Onorato36115782010-06-17 13:28:48 -0400874 synchronized (mLock) {
875 // If we are still the last one to be scheduled, remove ourselves.
876 if (mLoaderTask == this) {
877 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 }
Joe Onorato36115782010-06-17 13:28:48 -0400879 }
Joe Onorato36115782010-06-17 13:28:48 -0400880 }
881
882 public void stopLocked() {
883 synchronized (LoaderTask.this) {
884 mStopped = true;
885 this.notify();
886 }
887 }
888
889 /**
890 * Gets the callbacks object. If we've been stopped, or if the launcher object
891 * has somehow been garbage collected, return null instead. Pass in the Callbacks
892 * object that was around when the deferred message was scheduled, and if there's
893 * a new Callbacks object around then also return null. This will save us from
894 * calling onto it with data that will be ignored.
895 */
896 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
897 synchronized (mLock) {
898 if (mStopped) {
899 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400900 }
Joe Onorato36115782010-06-17 13:28:48 -0400901
902 if (mCallbacks == null) {
903 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400904 }
Joe Onorato36115782010-06-17 13:28:48 -0400905
906 final Callbacks callbacks = mCallbacks.get();
907 if (callbacks != oldCallbacks) {
908 return null;
909 }
910 if (callbacks == null) {
911 Log.w(TAG, "no mCallbacks");
912 return null;
913 }
914
915 return callbacks;
916 }
917 }
918
919 // check & update map of what's occupied; used to discard overlapping/invalid items
920 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700921 int containerIndex = item.screen;
922 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700923 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800924 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700925 return false;
926 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700927
928 // We use the last index to refer to the hotseat and the screen as the rank, so
929 // test and update the occupied state accordingly
930 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
931 Log.e(TAG, "Error loading shortcut into hotseat " + item
932 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
933 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
934 return false;
935 } else {
936 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
937 return true;
938 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700939 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
940 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400941 return true;
942 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700943
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700944 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -0400945 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
946 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700947 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400948 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700949 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400950 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700951 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700952 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400953 return false;
954 }
955 }
956 }
957 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
958 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700959 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400960 }
961 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700962
Joe Onorato36115782010-06-17 13:28:48 -0400963 return true;
964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965
Joe Onorato36115782010-06-17 13:28:48 -0400966 private void loadWorkspace() {
967 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968
Joe Onorato36115782010-06-17 13:28:48 -0400969 final Context context = mContext;
970 final ContentResolver contentResolver = context.getContentResolver();
971 final PackageManager manager = context.getPackageManager();
972 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
973 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400974
Adam Cohen4eac29a2011-07-11 17:53:37 -0700975 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700976 sAppWidgets.clear();
977 sFolders.clear();
978 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700979 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800980
Joe Onorato36115782010-06-17 13:28:48 -0400981 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982
Joe Onorato36115782010-06-17 13:28:48 -0400983 final Cursor c = contentResolver.query(
Winson Chunge61e93e2011-09-12 10:59:49 -0700984 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400985
Winson Chungf30ad5f2011-08-08 10:55:42 -0700986 // +1 for the hotseat (it can be larger than the workspace)
Winson Chung36f97362011-09-07 11:20:10 -0700987 // Load workspace in reverse order to ensure that latest items are loaded first (and
988 // before any earlier duplicates)
Adam Cohend22015c2010-07-26 22:02:18 -0700989 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700990 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991
Joe Onorato36115782010-06-17 13:28:48 -0400992 try {
993 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
994 final int intentIndex = c.getColumnIndexOrThrow
995 (LauncherSettings.Favorites.INTENT);
996 final int titleIndex = c.getColumnIndexOrThrow
997 (LauncherSettings.Favorites.TITLE);
998 final int iconTypeIndex = c.getColumnIndexOrThrow(
999 LauncherSettings.Favorites.ICON_TYPE);
1000 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1001 final int iconPackageIndex = c.getColumnIndexOrThrow(
1002 LauncherSettings.Favorites.ICON_PACKAGE);
1003 final int iconResourceIndex = c.getColumnIndexOrThrow(
1004 LauncherSettings.Favorites.ICON_RESOURCE);
1005 final int containerIndex = c.getColumnIndexOrThrow(
1006 LauncherSettings.Favorites.CONTAINER);
1007 final int itemTypeIndex = c.getColumnIndexOrThrow(
1008 LauncherSettings.Favorites.ITEM_TYPE);
1009 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1010 LauncherSettings.Favorites.APPWIDGET_ID);
1011 final int screenIndex = c.getColumnIndexOrThrow(
1012 LauncherSettings.Favorites.SCREEN);
1013 final int cellXIndex = c.getColumnIndexOrThrow
1014 (LauncherSettings.Favorites.CELLX);
1015 final int cellYIndex = c.getColumnIndexOrThrow
1016 (LauncherSettings.Favorites.CELLY);
1017 final int spanXIndex = c.getColumnIndexOrThrow
1018 (LauncherSettings.Favorites.SPANX);
1019 final int spanYIndex = c.getColumnIndexOrThrow(
1020 LauncherSettings.Favorites.SPANY);
1021 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1022 final int displayModeIndex = c.getColumnIndexOrThrow(
1023 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024
Joe Onorato36115782010-06-17 13:28:48 -04001025 ShortcutInfo info;
1026 String intentDescription;
1027 LauncherAppWidgetInfo appWidgetInfo;
1028 int container;
1029 long id;
1030 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001031
Joe Onorato36115782010-06-17 13:28:48 -04001032 while (!mStopped && c.moveToNext()) {
1033 try {
1034 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035
Joe Onorato36115782010-06-17 13:28:48 -04001036 switch (itemType) {
1037 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1038 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1039 intentDescription = c.getString(intentIndex);
1040 try {
1041 intent = Intent.parseUri(intentDescription, 0);
1042 } catch (URISyntaxException e) {
1043 continue;
1044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1047 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001048 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001049 } else {
1050 info = getShortcutInfo(c, context, iconTypeIndex,
1051 iconPackageIndex, iconResourceIndex, iconIndex,
1052 titleIndex);
1053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001056 info.intent = intent;
1057 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001059 info.container = container;
1060 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001061 info.cellX = c.getInt(cellXIndex);
1062 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063
Daniel Sandler8802e962010-05-26 16:28:16 -04001064 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001065 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001066 break;
1067 }
1068
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001070 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001072 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001073 break;
1074 default:
1075 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001076 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001077 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001078 folderInfo.add(info);
1079 break;
1080 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001081 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001082
1083 // now that we've loaded everthing re-save it with the
1084 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001085 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001086 } else {
1087 // Failed to load the shortcut, probably because the
1088 // activity manager couldn't resolve it (maybe the app
1089 // was uninstalled), or the db row was somehow screwed up.
1090 // Delete it.
1091 id = c.getLong(idIndex);
1092 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1093 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1094 id, false), null, null);
1095 }
1096 break;
1097
Adam Cohendf2cc412011-04-27 16:56:57 -07001098 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001099 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001100 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001101
Winson Chungaafa03c2010-06-11 17:34:16 -07001102 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001103 folderInfo.id = id;
1104 container = c.getInt(containerIndex);
1105 folderInfo.container = container;
1106 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001107 folderInfo.cellX = c.getInt(cellXIndex);
1108 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001109
1110 // check & update map of what's occupied
1111 if (!checkItemPlacement(occupied, folderInfo)) {
1112 break;
1113 }
Joe Onorato36115782010-06-17 13:28:48 -04001114 switch (container) {
1115 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001117 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001118 break;
1119 }
1120
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121 sItemsIdMap.put(folderInfo.id, folderInfo);
1122 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001123 break;
1124
Joe Onorato36115782010-06-17 13:28:48 -04001125 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1126 // Read all Launcher-specific widget details
1127 int appWidgetId = c.getInt(appWidgetIdIndex);
1128 id = c.getLong(idIndex);
1129
1130 final AppWidgetProviderInfo provider =
1131 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001132
Joe Onorato36115782010-06-17 13:28:48 -04001133 if (!isSafeMode && (provider == null || provider.provider == null ||
1134 provider.provider.getPackageName() == null)) {
Adam Cohen16d7ffc2011-10-05 17:49:14 -07001135 String log = "Deleting widget that isn't installed anymore: id="
1136 + id + " appWidgetId=" + appWidgetId;
1137 Log.e(TAG, log);
1138 Launcher.sDumpLogs.add(log);
Joe Onorato36115782010-06-17 13:28:48 -04001139 itemsToRemove.add(id);
1140 } else {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001141 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001142 appWidgetInfo.id = id;
1143 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001144 appWidgetInfo.cellX = c.getInt(cellXIndex);
1145 appWidgetInfo.cellY = c.getInt(cellYIndex);
1146 appWidgetInfo.spanX = c.getInt(spanXIndex);
1147 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001148
1149 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001150 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1151 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001152 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001153 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001154 continue;
1155 }
1156 appWidgetInfo.container = c.getInt(containerIndex);
1157
1158 // check & update map of what's occupied
1159 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1160 break;
1161 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001162 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1163 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001164 }
1165 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001166 }
Joe Onorato36115782010-06-17 13:28:48 -04001167 } catch (Exception e) {
1168 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001169 }
1170 }
Joe Onorato36115782010-06-17 13:28:48 -04001171 } finally {
1172 c.close();
1173 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001174
Joe Onorato36115782010-06-17 13:28:48 -04001175 if (itemsToRemove.size() > 0) {
1176 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1177 LauncherSettings.Favorites.CONTENT_URI);
1178 // Remove dead items
1179 for (long id : itemsToRemove) {
1180 if (DEBUG_LOADERS) {
1181 Log.d(TAG, "Removed id = " + id);
1182 }
1183 // Don't notify content observers
1184 try {
1185 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1186 null, null);
1187 } catch (RemoteException e) {
1188 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001189 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 }
1192
Joe Onorato36115782010-06-17 13:28:48 -04001193 if (DEBUG_LOADERS) {
1194 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1195 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001196 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001197 String line = "";
1198 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1199 if (s > 0) {
1200 line += " | ";
1201 }
Adam Cohend22015c2010-07-26 22:02:18 -07001202 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001203 line += ((occupied[s][x][y] != null) ? "#" : ".");
1204 }
1205 }
1206 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 }
Joe Onorato36115782010-06-17 13:28:48 -04001208 }
1209 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210
Joe Onorato36115782010-06-17 13:28:48 -04001211 /**
1212 * Read everything out of our database.
1213 */
1214 private void bindWorkspace() {
1215 final long t = SystemClock.uptimeMillis();
1216
1217 // Don't use these two variables in any of the callback runnables.
1218 // Otherwise we hold a reference to them.
1219 final Callbacks oldCallbacks = mCallbacks.get();
1220 if (oldCallbacks == null) {
1221 // This launcher has exited and nobody bothered to tell us. Just bail.
1222 Log.w(TAG, "LoaderTask running with no launcher");
1223 return;
1224 }
1225
1226 int N;
1227 // Tell the workspace that we're about to start firing items at it
1228 mHandler.post(new Runnable() {
1229 public void run() {
1230 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1231 if (callbacks != null) {
1232 callbacks.startBinding();
1233 }
1234 }
1235 });
Winson Chung603bcb92011-09-02 11:45:39 -07001236
Winson Chung603bcb92011-09-02 11:45:39 -07001237 final ArrayList<ItemInfo> workspaceItems = unbindWorkspaceItemsOnMainThread();
1238
Joe Onorato36115782010-06-17 13:28:48 -04001239 // Add the items to the workspace.
Winson Chung603bcb92011-09-02 11:45:39 -07001240 N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001241 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1242 final int start = i;
1243 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 mHandler.post(new Runnable() {
1245 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001246 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001248 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249 }
1250 }
1251 });
Joe Onorato36115782010-06-17 13:28:48 -04001252 }
Winson Chung603bcb92011-09-02 11:45:39 -07001253 // Ensure that we don't use the same folders data structure on the main thread
1254 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001255 mHandler.post(new Runnable() {
1256 public void run() {
1257 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1258 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001259 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001260 }
1261 }
1262 });
1263 // Wait until the queue goes empty.
1264 mHandler.post(new Runnable() {
1265 public void run() {
1266 if (DEBUG_LOADERS) {
1267 Log.d(TAG, "Going to start binding widgets soon.");
1268 }
1269 }
1270 });
1271 // Bind the widgets, one at a time.
1272 // WARNING: this is calling into the workspace from the background thread,
1273 // but since getCurrentScreen() just returns the int, we should be okay. This
1274 // is just a hint for the order, and if it's wrong, we'll be okay.
1275 // TODO: instead, we should have that push the current screen into here.
1276 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001277 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001278 // once for the current screen
1279 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001280 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001281 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282 mHandler.post(new Runnable() {
1283 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001284 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001286 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001287 }
1288 }
1289 });
1290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291 }
Joe Onorato36115782010-06-17 13:28:48 -04001292 // once for the other screens
1293 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001294 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001295 if (widget.screen != currentScreen) {
1296 mHandler.post(new Runnable() {
1297 public void run() {
1298 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1299 if (callbacks != null) {
1300 callbacks.bindAppWidget(widget);
1301 }
1302 }
1303 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001304 }
1305 }
Joe Onorato36115782010-06-17 13:28:48 -04001306 // Tell the workspace that we're done.
1307 mHandler.post(new Runnable() {
1308 public void run() {
1309 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1310 if (callbacks != null) {
1311 callbacks.finishBindingItems();
1312 }
1313 }
1314 });
1315 // If we're profiling, this is the last thing in the queue.
1316 mHandler.post(new Runnable() {
1317 public void run() {
1318 if (DEBUG_LOADERS) {
1319 Log.d(TAG, "bound workspace in "
1320 + (SystemClock.uptimeMillis()-t) + "ms");
1321 }
1322 }
1323 });
1324 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001325
Joe Onorato36115782010-06-17 13:28:48 -04001326 private void loadAndBindAllApps() {
1327 if (DEBUG_LOADERS) {
1328 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1329 }
1330 if (!mAllAppsLoaded) {
1331 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001332 synchronized (LoaderTask.this) {
1333 if (mStopped) {
1334 return;
1335 }
1336 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001337 }
Joe Onorato36115782010-06-17 13:28:48 -04001338 } else {
1339 onlyBindAllApps();
1340 }
1341 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001342
Joe Onorato36115782010-06-17 13:28:48 -04001343 private void onlyBindAllApps() {
1344 final Callbacks oldCallbacks = mCallbacks.get();
1345 if (oldCallbacks == null) {
1346 // This launcher has exited and nobody bothered to tell us. Just bail.
1347 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1348 return;
1349 }
1350
1351 // shallow copy
1352 final ArrayList<ApplicationInfo> list
Michael Jurka92f3d462011-11-22 21:02:29 -08001353 = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone();
Joe Onorato36115782010-06-17 13:28:48 -04001354 mHandler.post(new Runnable() {
1355 public void run() {
1356 final long t = SystemClock.uptimeMillis();
1357 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1358 if (callbacks != null) {
1359 callbacks.bindAllApplications(list);
1360 }
1361 if (DEBUG_LOADERS) {
1362 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1363 + (SystemClock.uptimeMillis()-t) + "ms");
1364 }
1365 }
1366 });
1367
1368 }
1369
1370 private void loadAllAppsByBatch() {
1371 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1372
1373 // Don't use these two variables in any of the callback runnables.
1374 // Otherwise we hold a reference to them.
1375 final Callbacks oldCallbacks = mCallbacks.get();
1376 if (oldCallbacks == null) {
1377 // This launcher has exited and nobody bothered to tell us. Just bail.
1378 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1379 return;
1380 }
1381
1382 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1383 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1384
1385 final PackageManager packageManager = mContext.getPackageManager();
1386 List<ResolveInfo> apps = null;
1387
1388 int N = Integer.MAX_VALUE;
1389
1390 int startIndex;
1391 int i=0;
1392 int batchSize = -1;
1393 while (i < N && !mStopped) {
1394 if (i == 0) {
1395 mAllAppsList.clear();
1396 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1397 apps = packageManager.queryIntentActivities(mainIntent, 0);
1398 if (DEBUG_LOADERS) {
1399 Log.d(TAG, "queryIntentActivities took "
1400 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1401 }
1402 if (apps == null) {
1403 return;
1404 }
1405 N = apps.size();
1406 if (DEBUG_LOADERS) {
1407 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1408 }
1409 if (N == 0) {
1410 // There are no apps?!?
1411 return;
1412 }
1413 if (mBatchSize == 0) {
1414 batchSize = N;
1415 } else {
1416 batchSize = mBatchSize;
1417 }
1418
1419 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1420 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001421 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001422 if (DEBUG_LOADERS) {
1423 Log.d(TAG, "sort took "
1424 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1425 }
1426 }
1427
1428 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1429
1430 startIndex = i;
1431 for (int j=0; i<N && j<batchSize; j++) {
1432 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001433 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001434 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001435 i++;
1436 }
1437
1438 final boolean first = i <= batchSize;
1439 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1440 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1441 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1442
Joe Onoratocc67f472010-06-08 10:54:30 -07001443 mHandler.post(new Runnable() {
1444 public void run() {
1445 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001446 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001447 if (first) {
1448 callbacks.bindAllApplications(added);
1449 } else {
1450 callbacks.bindAppsAdded(added);
1451 }
1452 if (DEBUG_LOADERS) {
1453 Log.d(TAG, "bound " + added.size() + " apps in "
1454 + (SystemClock.uptimeMillis() - t) + "ms");
1455 }
1456 } else {
1457 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001458 }
1459 }
1460 });
1461
Daniel Sandlerdca66122010-04-13 16:23:58 -04001462 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001463 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1464 + (SystemClock.uptimeMillis()-t2) + "ms");
1465 }
1466
1467 if (mAllAppsLoadDelay > 0 && i < N) {
1468 try {
1469 if (DEBUG_LOADERS) {
1470 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1471 }
1472 Thread.sleep(mAllAppsLoadDelay);
1473 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 }
1475 }
1476
Joe Onorato36115782010-06-17 13:28:48 -04001477 if (DEBUG_LOADERS) {
1478 Log.d(TAG, "cached all " + N + " apps in "
1479 + (SystemClock.uptimeMillis()-t) + "ms"
1480 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001481 }
1482 }
1483
1484 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001485 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1486 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1487 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1488 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1489 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001490 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
1492 }
1493
1494 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001495 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
1497
1498 private class PackageUpdatedTask implements Runnable {
1499 int mOp;
1500 String[] mPackages;
1501
1502 public static final int OP_NONE = 0;
1503 public static final int OP_ADD = 1;
1504 public static final int OP_UPDATE = 2;
1505 public static final int OP_REMOVE = 3; // uninstlled
1506 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1507
1508
1509 public PackageUpdatedTask(int op, String[] packages) {
1510 mOp = op;
1511 mPackages = packages;
1512 }
1513
1514 public void run() {
1515 final Context context = mApp;
1516
1517 final String[] packages = mPackages;
1518 final int N = packages.length;
1519 switch (mOp) {
1520 case OP_ADD:
1521 for (int i=0; i<N; i++) {
1522 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1523 mAllAppsList.addPackage(context, packages[i]);
1524 }
1525 break;
1526 case OP_UPDATE:
1527 for (int i=0; i<N; i++) {
1528 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1529 mAllAppsList.updatePackage(context, packages[i]);
1530 }
1531 break;
1532 case OP_REMOVE:
1533 case OP_UNAVAILABLE:
1534 for (int i=0; i<N; i++) {
1535 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1536 mAllAppsList.removePackage(packages[i]);
1537 }
1538 break;
1539 }
1540
1541 ArrayList<ApplicationInfo> added = null;
1542 ArrayList<ApplicationInfo> removed = null;
1543 ArrayList<ApplicationInfo> modified = null;
1544
1545 if (mAllAppsList.added.size() > 0) {
1546 added = mAllAppsList.added;
1547 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1548 }
1549 if (mAllAppsList.removed.size() > 0) {
1550 removed = mAllAppsList.removed;
1551 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1552 for (ApplicationInfo info: removed) {
1553 mIconCache.remove(info.intent.getComponent());
1554 }
1555 }
1556 if (mAllAppsList.modified.size() > 0) {
1557 modified = mAllAppsList.modified;
1558 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1559 }
1560
1561 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1562 if (callbacks == null) {
1563 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1564 return;
1565 }
1566
1567 if (added != null) {
1568 final ArrayList<ApplicationInfo> addedFinal = added;
1569 mHandler.post(new Runnable() {
1570 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001571 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1572 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001573 callbacks.bindAppsAdded(addedFinal);
1574 }
1575 }
1576 });
1577 }
1578 if (modified != null) {
1579 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1580 mHandler.post(new Runnable() {
1581 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001582 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1583 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001584 callbacks.bindAppsUpdated(modifiedFinal);
1585 }
1586 }
1587 });
1588 }
1589 if (removed != null) {
1590 final boolean permanent = mOp != OP_UNAVAILABLE;
1591 final ArrayList<ApplicationInfo> removedFinal = removed;
1592 mHandler.post(new Runnable() {
1593 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001594 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1595 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001596 callbacks.bindAppsRemoved(removedFinal, permanent);
1597 }
1598 }
1599 });
Joe Onoratobe386092009-11-17 17:32:16 -08001600 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001601
1602 mHandler.post(new Runnable() {
1603 @Override
1604 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001605 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1606 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001607 callbacks.bindPackagesUpdated();
1608 }
1609 }
1610 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001611 }
1612 }
1613
1614 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001615 * This is called from the code that adds shortcuts from the intent receiver. This
1616 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 */
Joe Onorato56d82912010-03-07 14:32:10 -05001618 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001619 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001620 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001621
Joe Onorato56d82912010-03-07 14:32:10 -05001622 /**
1623 * Make an ShortcutInfo object for a shortcut that is an application.
1624 *
1625 * If c is not null, then it will be used to fill in missing data like the title and icon.
1626 */
1627 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001628 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001629 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001630 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001631
1632 ComponentName componentName = intent.getComponent();
1633 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001634 return null;
1635 }
1636
Adam Cohen00fcb492011-11-02 21:53:47 -07001637 try {
1638 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
1639 if (!pi.applicationInfo.enabled) {
1640 // If we return null here, the corresponding item will be removed from the launcher
1641 // db and will not appear in the workspace.
1642 return null;
1643 }
1644 } catch (NameNotFoundException e) {
1645 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
1646 }
1647
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001648 // TODO: See if the PackageManager knows about this case. If it doesn't
1649 // then return null & delete this.
1650
Joe Onorato56d82912010-03-07 14:32:10 -05001651 // the resource -- This may implicitly give us back the fallback icon,
1652 // but don't worry about that. All we're doing with usingFallbackIcon is
1653 // to avoid saving lots of copies of that in the database, and most apps
1654 // have icons anyway.
1655 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1656 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001657 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001658 }
Joe Onorato56d82912010-03-07 14:32:10 -05001659 // the db
1660 if (icon == null) {
1661 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001662 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001663 }
1664 }
1665 // the fallback icon
1666 if (icon == null) {
1667 icon = getFallbackIcon();
1668 info.usingFallbackIcon = true;
1669 }
1670 info.setIcon(icon);
1671
1672 // from the resource
1673 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001674 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1675 if (labelCache != null && labelCache.containsKey(key)) {
1676 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001677 } else {
1678 info.title = resolveInfo.activityInfo.loadLabel(manager);
1679 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001680 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001681 }
1682 }
Joe Onorato56d82912010-03-07 14:32:10 -05001683 }
1684 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001686 if (c != null) {
1687 info.title = c.getString(titleIndex);
1688 }
1689 }
1690 // fall back to the class name of the activity
1691 if (info.title == null) {
1692 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001693 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1695 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001697
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001699 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001701 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001702 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1703 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704
Joe Onorato56d82912010-03-07 14:32:10 -05001705 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001706 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001709 // TODO: If there's an explicit component and we can't install that, delete it.
1710
Joe Onorato56d82912010-03-07 14:32:10 -05001711 info.title = c.getString(titleIndex);
1712
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713 int iconType = c.getInt(iconTypeIndex);
1714 switch (iconType) {
1715 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1716 String packageName = c.getString(iconPackageIndex);
1717 String resourceName = c.getString(iconResourceIndex);
1718 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001719 info.customIcon = false;
1720 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001723 if (resources != null) {
1724 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001725 icon = Utilities.createIconBitmap(
1726 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001729 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 }
Joe Onorato56d82912010-03-07 14:32:10 -05001731 // the db
1732 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001733 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001734 }
1735 // the fallback icon
1736 if (icon == null) {
1737 icon = getFallbackIcon();
1738 info.usingFallbackIcon = true;
1739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001740 break;
1741 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001742 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001743 if (icon == null) {
1744 icon = getFallbackIcon();
1745 info.customIcon = false;
1746 info.usingFallbackIcon = true;
1747 } else {
1748 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 break;
1751 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001752 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001753 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 info.customIcon = false;
1755 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001757 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 return info;
1759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760
Michael Jurka931dc972011-08-05 15:08:15 -07001761 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001762 if (false) {
1763 Log.d(TAG, "getIconFromCursor app="
1764 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1765 }
1766 byte[] data = c.getBlob(iconIndex);
1767 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001768 return Utilities.createIconBitmap(
1769 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001770 } catch (Exception e) {
1771 return null;
1772 }
1773 }
1774
Winson Chung3d503fb2011-07-13 17:25:49 -07001775 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1776 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001777 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001778 if (info == null) {
1779 return null;
1780 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001781 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001782
1783 return info;
1784 }
1785
Winson Chunga9abd0e2010-10-27 17:18:37 -07001786 /**
Winson Chung55cef262010-10-28 14:14:18 -07001787 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1788 */
1789 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1790 ComponentName component) {
1791 List<AppWidgetProviderInfo> widgets =
1792 AppWidgetManager.getInstance(context).getInstalledProviders();
1793 for (AppWidgetProviderInfo info : widgets) {
1794 if (info.provider.equals(component)) {
1795 return info;
1796 }
1797 }
1798 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001799 }
1800
Winson Chung68846fd2010-10-29 11:00:27 -07001801 /**
1802 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1803 */
1804 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1805 final PackageManager packageManager = context.getPackageManager();
1806 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1807 new ArrayList<WidgetMimeTypeHandlerData>();
1808
1809 final Intent supportsIntent =
1810 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1811 supportsIntent.setType(mimeType);
1812
1813 // Create a set of widget configuration components that we can test against
1814 final List<AppWidgetProviderInfo> widgets =
1815 AppWidgetManager.getInstance(context).getInstalledProviders();
1816 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1817 new HashMap<ComponentName, AppWidgetProviderInfo>();
1818 for (AppWidgetProviderInfo info : widgets) {
1819 configurationComponentToWidget.put(info.configure, info);
1820 }
1821
1822 // Run through each of the intents that can handle this type of clip data, and cross
1823 // reference them with the components that are actual configuration components
1824 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1825 PackageManager.MATCH_DEFAULT_ONLY);
1826 for (ResolveInfo info : activities) {
1827 final ActivityInfo activityInfo = info.activityInfo;
1828 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1829 activityInfo.name);
1830 if (configurationComponentToWidget.containsKey(infoComponent)) {
1831 supportedConfigurationActivities.add(
1832 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1833 configurationComponentToWidget.get(infoComponent)));
1834 }
1835 }
1836 return supportedConfigurationActivities;
1837 }
1838
Winson Chunga9abd0e2010-10-27 17:18:37 -07001839 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001840 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1841 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1842 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1843
Adam Cohend9198822011-11-22 16:42:47 -08001844 if (intent == null) {
1845 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
1846 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
1847 return null;
1848 }
1849
Joe Onorato0589f0f2010-02-08 13:44:00 -08001850 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001851 boolean customIcon = false;
1852 ShortcutIconResource iconResource = null;
1853
1854 if (bitmap != null && bitmap instanceof Bitmap) {
1855 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001856 customIcon = true;
1857 } else {
1858 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1859 if (extra != null && extra instanceof ShortcutIconResource) {
1860 try {
1861 iconResource = (ShortcutIconResource) extra;
1862 final PackageManager packageManager = context.getPackageManager();
1863 Resources resources = packageManager.getResourcesForApplication(
1864 iconResource.packageName);
1865 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001866 icon = Utilities.createIconBitmap(
1867 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001868 } catch (Exception e) {
1869 Log.w(TAG, "Could not load shortcut icon: " + extra);
1870 }
1871 }
1872 }
1873
Michael Jurkac9d95c52011-08-29 14:03:34 -07001874 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001875
1876 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001877 if (fallbackIcon != null) {
1878 icon = fallbackIcon;
1879 } else {
1880 icon = getFallbackIcon();
1881 info.usingFallbackIcon = true;
1882 }
Joe Onorato56d82912010-03-07 14:32:10 -05001883 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001884 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001885
Joe Onorato0589f0f2010-02-08 13:44:00 -08001886 info.title = name;
1887 info.intent = intent;
1888 info.customIcon = customIcon;
1889 info.iconResource = iconResource;
1890
1891 return info;
1892 }
1893
Winson Chungaac01e12011-08-17 10:37:13 -07001894 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1895 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001896 // If apps can't be on SD, don't even bother.
1897 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001898 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001899 }
Joe Onorato56d82912010-03-07 14:32:10 -05001900 // If this icon doesn't have a custom icon, check to see
1901 // what's stored in the DB, and if it doesn't match what
1902 // we're going to show, store what we are going to show back
1903 // into the DB. We do this so when we're loading, if the
1904 // package manager can't find an icon (for example because
1905 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001906 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001907 cache.put(info, c.getBlob(iconIndex));
1908 return true;
1909 }
1910 return false;
1911 }
1912 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1913 boolean needSave = false;
1914 try {
1915 if (data != null) {
1916 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1917 Bitmap loaded = info.getIcon(mIconCache);
1918 needSave = !saved.sameAs(loaded);
1919 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001920 needSave = true;
1921 }
Winson Chungaac01e12011-08-17 10:37:13 -07001922 } catch (Exception e) {
1923 needSave = true;
1924 }
1925 if (needSave) {
1926 Log.d(TAG, "going to save icon bitmap for info=" + info);
1927 // This is slower than is ideal, but this only happens once
1928 // or when the app is updated with a new icon.
1929 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001930 }
1931 }
1932
Joe Onorato9c1289c2009-08-17 11:03:03 -04001933 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001934 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001935 * or make a new one.
1936 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001937 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 // See if a placeholder was created for us already
1939 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001940 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07001942 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001943 folders.put(id, folderInfo);
1944 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001945 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001949 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950 = new Comparator<ApplicationInfo>() {
1951 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001952 int result = sCollator.compare(a.title.toString(), b.title.toString());
1953 if (result == 0) {
1954 result = a.componentName.compareTo(b.componentName);
1955 }
1956 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001958 };
Winson Chung78403fe2011-01-21 15:38:02 -08001959 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1960 = new Comparator<ApplicationInfo>() {
1961 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1962 if (a.firstInstallTime < b.firstInstallTime) return 1;
1963 if (a.firstInstallTime > b.firstInstallTime) return -1;
1964 return 0;
1965 }
1966 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001967 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1968 = new Comparator<AppWidgetProviderInfo>() {
1969 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1970 return sCollator.compare(a.label.toString(), b.label.toString());
1971 }
1972 };
Winson Chung5308f242011-08-18 12:12:41 -07001973 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1974 if (info.activityInfo != null) {
1975 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1976 } else {
1977 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1978 }
1979 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001980 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1981 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001982 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001983 ShortcutNameComparator(PackageManager pm) {
1984 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001985 mLabelCache = new HashMap<Object, CharSequence>();
1986 }
1987 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1988 mPackageManager = pm;
1989 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001990 }
1991 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001992 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001993 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1994 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1995 if (mLabelCache.containsKey(keyA)) {
1996 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001997 } else {
1998 labelA = a.loadLabel(mPackageManager).toString();
1999
Winson Chung5308f242011-08-18 12:12:41 -07002000 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002001 }
Winson Chung5308f242011-08-18 12:12:41 -07002002 if (mLabelCache.containsKey(keyB)) {
2003 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002004 } else {
2005 labelB = b.loadLabel(mPackageManager).toString();
2006
Winson Chung5308f242011-08-18 12:12:41 -07002007 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002008 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002009 return sCollator.compare(labelA, labelB);
2010 }
2011 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002012 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
2013 private PackageManager mPackageManager;
2014 private HashMap<Object, String> mLabelCache;
2015 WidgetAndShortcutNameComparator(PackageManager pm) {
2016 mPackageManager = pm;
2017 mLabelCache = new HashMap<Object, String>();
2018 }
2019 public final int compare(Object a, Object b) {
2020 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002021 if (mLabelCache.containsKey(a)) {
2022 labelA = mLabelCache.get(a);
2023 } else {
2024 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002025 ((AppWidgetProviderInfo) a).label :
2026 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002027 mLabelCache.put(a, labelA);
2028 }
2029 if (mLabelCache.containsKey(b)) {
2030 labelB = mLabelCache.get(b);
2031 } else {
2032 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002033 ((AppWidgetProviderInfo) b).label :
2034 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002035 mLabelCache.put(b, labelB);
2036 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002037 return sCollator.compare(labelA, labelB);
2038 }
2039 };
Joe Onoratobe386092009-11-17 17:32:16 -08002040
2041 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002042 Log.d(TAG, "mCallbacks=" + mCallbacks);
2043 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
2044 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
2045 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
2046 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002047 if (mLoaderTask != null) {
2048 mLoaderTask.dumpState();
2049 } else {
2050 Log.d(TAG, "mLoaderTask=null");
2051 }
Joe Onoratobe386092009-11-17 17:32:16 -08002052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053}