blob: 30eb86c5a5d4a5d66a8c246f087b8b870416e197 [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() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700652 resetLoadedState(true, true);
653
Reena Lee93f824a2011-09-23 17:20:28 -0700654 // Do this here because if the launcher activity is running it will be restarted.
655 // If it's not running startLoaderFromBackground will merely tell it that it needs
656 // to reload.
657 startLoaderFromBackground();
658 }
659
Winson Chungf0c6ae02012-03-21 16:10:31 -0700660 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
661 synchronized (mLock) {
662 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
663 // mWorkspaceLoaded to true later
664 stopLoaderLocked();
665 if (resetAllAppsLoaded) mAllAppsLoaded = false;
666 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
667 }
668 }
669
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700670 /**
671 * When the launcher is in the background, it's possible for it to miss paired
672 * configuration changes. So whenever we trigger the loader from the background
673 * tell the launcher that it needs to re-run the loader when it comes back instead
674 * of doing it now.
675 */
676 public void startLoaderFromBackground() {
677 boolean runLoader = false;
678 if (mCallbacks != null) {
679 Callbacks callbacks = mCallbacks.get();
680 if (callbacks != null) {
681 // Only actually run the loader if they're not paused.
682 if (!callbacks.setLoadOnResume()) {
683 runLoader = true;
684 }
685 }
686 }
687 if (runLoader) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700688 startLoader(false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700689 }
Joe Onorato36115782010-06-17 13:28:48 -0400690 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400691
Reena Lee93f824a2011-09-23 17:20:28 -0700692 // If there is already a loader task running, tell it to stop.
693 // returns true if isLaunching() was true on the old task
694 private boolean stopLoaderLocked() {
695 boolean isLaunching = false;
696 LoaderTask oldTask = mLoaderTask;
697 if (oldTask != null) {
698 if (oldTask.isLaunching()) {
699 isLaunching = true;
700 }
701 oldTask.stopLocked();
702 }
703 return isLaunching;
704 }
705
Winson Chungf0c6ae02012-03-21 16:10:31 -0700706 public void startLoader(boolean isLaunching) {
Joe Onorato36115782010-06-17 13:28:48 -0400707 synchronized (mLock) {
708 if (DEBUG_LOADERS) {
709 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
710 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711
Joe Onorato36115782010-06-17 13:28:48 -0400712 // Don't bother to start the thread if we know it's not going to do anything
713 if (mCallbacks != null && mCallbacks.get() != null) {
714 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700715 // also, don't downgrade isLaunching if we're already running
716 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700717 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chung7ed37742011-09-08 15:45:51 -0700718 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700719 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 }
722 }
723
Joe Onorato36115782010-06-17 13:28:48 -0400724 public void stopLoader() {
725 synchronized (mLock) {
726 if (mLoaderTask != null) {
727 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 }
729 }
Joe Onorato36115782010-06-17 13:28:48 -0400730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731
Michael Jurkac57b7a82011-08-09 22:02:20 -0700732 public boolean isAllAppsLoaded() {
733 return mAllAppsLoaded;
734 }
735
Joe Onorato36115782010-06-17 13:28:48 -0400736 /**
737 * Runnable for the thread that loads the contents of the launcher:
738 * - workspace icons
739 * - widgets
740 * - all apps icons
741 */
742 private class LoaderTask implements Runnable {
743 private Context mContext;
744 private Thread mWaitThread;
745 private boolean mIsLaunching;
746 private boolean mStopped;
747 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700748 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400749
750 LoaderTask(Context context, boolean isLaunching) {
751 mContext = context;
752 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700753 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 }
755
Joe Onorato36115782010-06-17 13:28:48 -0400756 boolean isLaunching() {
757 return mIsLaunching;
758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759
Joe Onorato36115782010-06-17 13:28:48 -0400760 private void loadAndBindWorkspace() {
761 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400762 if (DEBUG_LOADERS) {
763 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700765
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700766 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400767 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700768 synchronized (LoaderTask.this) {
769 if (mStopped) {
770 return;
771 }
772 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773 }
774 }
775
Joe Onorato36115782010-06-17 13:28:48 -0400776 // Bind the workspace
777 bindWorkspace();
778 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400779
Joe Onorato36115782010-06-17 13:28:48 -0400780 private void waitForIdle() {
781 // Wait until the either we're stopped or the other threads are done.
782 // This way we don't start loading all apps until the workspace has settled
783 // down.
784 synchronized (LoaderTask.this) {
785 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700786
Joe Onorato36115782010-06-17 13:28:48 -0400787 mHandler.postIdle(new Runnable() {
788 public void run() {
789 synchronized (LoaderTask.this) {
790 mLoadAndBindStepFinished = true;
791 if (DEBUG_LOADERS) {
792 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400793 }
Joe Onorato36115782010-06-17 13:28:48 -0400794 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400795 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400796 }
Joe Onorato36115782010-06-17 13:28:48 -0400797 });
798
799 while (!mStopped && !mLoadAndBindStepFinished) {
800 try {
801 this.wait();
802 } catch (InterruptedException ex) {
803 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400804 }
805 }
Joe Onorato36115782010-06-17 13:28:48 -0400806 if (DEBUG_LOADERS) {
807 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700808 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400809 + "ms for previous step to finish binding");
810 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400811 }
Joe Onorato36115782010-06-17 13:28:48 -0400812 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400813
Joe Onorato36115782010-06-17 13:28:48 -0400814 public void run() {
815 // Optimize for end-user experience: if the Launcher is up and // running with the
816 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
817 // workspace first (default).
818 final Callbacks cbk = mCallbacks.get();
819 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400820
Joe Onorato36115782010-06-17 13:28:48 -0400821 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400822 // Elevate priority when Home launches for the first time to avoid
823 // starving at boot time. Staring at a blank home is not cool.
824 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700825 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
826 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400827 android.os.Process.setThreadPriority(mIsLaunching
828 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
829 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400830 if (loadWorkspaceFirst) {
831 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
832 loadAndBindWorkspace();
833 } else {
834 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700835 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400836 }
837
Joe Onorato36115782010-06-17 13:28:48 -0400838 if (mStopped) {
839 break keep_running;
840 }
841
842 // Whew! Hard work done. Slow us down, and wait until the UI thread has
843 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400844 synchronized (mLock) {
845 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700846 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400847 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
848 }
849 }
Joe Onorato36115782010-06-17 13:28:48 -0400850 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400851
852 // second step
853 if (loadWorkspaceFirst) {
854 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700855 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400856 } else {
857 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
858 loadAndBindWorkspace();
859 }
Winson Chung7ed37742011-09-08 15:45:51 -0700860
861 // Restore the default thread priority after we are done loading items
862 synchronized (mLock) {
863 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
864 }
Joe Onorato36115782010-06-17 13:28:48 -0400865 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
Winson Chungaac01e12011-08-17 10:37:13 -0700867
868 // Update the saved icons if necessary
869 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700870 for (Object key : sDbIconCache.keySet()) {
871 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700872 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700873 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700874
Joe Onorato36115782010-06-17 13:28:48 -0400875 // Clear out this reference, otherwise we end up holding it until all of the
876 // callback runnables are done.
877 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878
Joe Onorato36115782010-06-17 13:28:48 -0400879 synchronized (mLock) {
880 // If we are still the last one to be scheduled, remove ourselves.
881 if (mLoaderTask == this) {
882 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 }
Joe Onorato36115782010-06-17 13:28:48 -0400884 }
Joe Onorato36115782010-06-17 13:28:48 -0400885 }
886
887 public void stopLocked() {
888 synchronized (LoaderTask.this) {
889 mStopped = true;
890 this.notify();
891 }
892 }
893
894 /**
895 * Gets the callbacks object. If we've been stopped, or if the launcher object
896 * has somehow been garbage collected, return null instead. Pass in the Callbacks
897 * object that was around when the deferred message was scheduled, and if there's
898 * a new Callbacks object around then also return null. This will save us from
899 * calling onto it with data that will be ignored.
900 */
901 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
902 synchronized (mLock) {
903 if (mStopped) {
904 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400905 }
Joe Onorato36115782010-06-17 13:28:48 -0400906
907 if (mCallbacks == null) {
908 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400909 }
Joe Onorato36115782010-06-17 13:28:48 -0400910
911 final Callbacks callbacks = mCallbacks.get();
912 if (callbacks != oldCallbacks) {
913 return null;
914 }
915 if (callbacks == null) {
916 Log.w(TAG, "no mCallbacks");
917 return null;
918 }
919
920 return callbacks;
921 }
922 }
923
924 // check & update map of what's occupied; used to discard overlapping/invalid items
925 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700926 int containerIndex = item.screen;
927 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700928 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800929 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700930 return false;
931 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700932
933 // We use the last index to refer to the hotseat and the screen as the rank, so
934 // test and update the occupied state accordingly
935 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
936 Log.e(TAG, "Error loading shortcut into hotseat " + item
937 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
938 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
939 return false;
940 } else {
941 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
942 return true;
943 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700944 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
945 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400946 return true;
947 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700948
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700949 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -0400950 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
951 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700952 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400953 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700954 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400955 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700956 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700957 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400958 return false;
959 }
960 }
961 }
962 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
963 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700964 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400965 }
966 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700967
Joe Onorato36115782010-06-17 13:28:48 -0400968 return true;
969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970
Joe Onorato36115782010-06-17 13:28:48 -0400971 private void loadWorkspace() {
972 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973
Joe Onorato36115782010-06-17 13:28:48 -0400974 final Context context = mContext;
975 final ContentResolver contentResolver = context.getContentResolver();
976 final PackageManager manager = context.getPackageManager();
977 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
978 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400979
Adam Cohen4eac29a2011-07-11 17:53:37 -0700980 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 sAppWidgets.clear();
982 sFolders.clear();
983 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700984 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800985
Joe Onorato36115782010-06-17 13:28:48 -0400986 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987
Joe Onorato36115782010-06-17 13:28:48 -0400988 final Cursor c = contentResolver.query(
Winson Chunge61e93e2011-09-12 10:59:49 -0700989 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400990
Winson Chungf30ad5f2011-08-08 10:55:42 -0700991 // +1 for the hotseat (it can be larger than the workspace)
Winson Chung36f97362011-09-07 11:20:10 -0700992 // Load workspace in reverse order to ensure that latest items are loaded first (and
993 // before any earlier duplicates)
Adam Cohend22015c2010-07-26 22:02:18 -0700994 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700995 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996
Joe Onorato36115782010-06-17 13:28:48 -0400997 try {
998 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
999 final int intentIndex = c.getColumnIndexOrThrow
1000 (LauncherSettings.Favorites.INTENT);
1001 final int titleIndex = c.getColumnIndexOrThrow
1002 (LauncherSettings.Favorites.TITLE);
1003 final int iconTypeIndex = c.getColumnIndexOrThrow(
1004 LauncherSettings.Favorites.ICON_TYPE);
1005 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1006 final int iconPackageIndex = c.getColumnIndexOrThrow(
1007 LauncherSettings.Favorites.ICON_PACKAGE);
1008 final int iconResourceIndex = c.getColumnIndexOrThrow(
1009 LauncherSettings.Favorites.ICON_RESOURCE);
1010 final int containerIndex = c.getColumnIndexOrThrow(
1011 LauncherSettings.Favorites.CONTAINER);
1012 final int itemTypeIndex = c.getColumnIndexOrThrow(
1013 LauncherSettings.Favorites.ITEM_TYPE);
1014 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1015 LauncherSettings.Favorites.APPWIDGET_ID);
1016 final int screenIndex = c.getColumnIndexOrThrow(
1017 LauncherSettings.Favorites.SCREEN);
1018 final int cellXIndex = c.getColumnIndexOrThrow
1019 (LauncherSettings.Favorites.CELLX);
1020 final int cellYIndex = c.getColumnIndexOrThrow
1021 (LauncherSettings.Favorites.CELLY);
1022 final int spanXIndex = c.getColumnIndexOrThrow
1023 (LauncherSettings.Favorites.SPANX);
1024 final int spanYIndex = c.getColumnIndexOrThrow(
1025 LauncherSettings.Favorites.SPANY);
1026 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1027 final int displayModeIndex = c.getColumnIndexOrThrow(
1028 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029
Joe Onorato36115782010-06-17 13:28:48 -04001030 ShortcutInfo info;
1031 String intentDescription;
1032 LauncherAppWidgetInfo appWidgetInfo;
1033 int container;
1034 long id;
1035 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036
Joe Onorato36115782010-06-17 13:28:48 -04001037 while (!mStopped && c.moveToNext()) {
1038 try {
1039 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040
Joe Onorato36115782010-06-17 13:28:48 -04001041 switch (itemType) {
1042 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1043 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1044 intentDescription = c.getString(intentIndex);
1045 try {
1046 intent = Intent.parseUri(intentDescription, 0);
1047 } catch (URISyntaxException e) {
1048 continue;
1049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050
Joe Onorato36115782010-06-17 13:28:48 -04001051 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1052 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001053 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001054 } else {
1055 info = getShortcutInfo(c, context, iconTypeIndex,
1056 iconPackageIndex, iconResourceIndex, iconIndex,
1057 titleIndex);
1058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059
Joe Onorato36115782010-06-17 13:28:48 -04001060 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001061 info.intent = intent;
1062 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001064 info.container = container;
1065 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001066 info.cellX = c.getInt(cellXIndex);
1067 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068
Daniel Sandler8802e962010-05-26 16:28:16 -04001069 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001070 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001071 break;
1072 }
1073
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001075 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001076 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001077 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001078 break;
1079 default:
1080 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001081 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001082 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001083 folderInfo.add(info);
1084 break;
1085 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001086 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001087
1088 // now that we've loaded everthing re-save it with the
1089 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001090 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001091 } else {
1092 // Failed to load the shortcut, probably because the
1093 // activity manager couldn't resolve it (maybe the app
1094 // was uninstalled), or the db row was somehow screwed up.
1095 // Delete it.
1096 id = c.getLong(idIndex);
1097 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1098 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1099 id, false), null, null);
1100 }
1101 break;
1102
Adam Cohendf2cc412011-04-27 16:56:57 -07001103 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001104 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001105 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001106
Winson Chungaafa03c2010-06-11 17:34:16 -07001107 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001108 folderInfo.id = id;
1109 container = c.getInt(containerIndex);
1110 folderInfo.container = container;
1111 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001112 folderInfo.cellX = c.getInt(cellXIndex);
1113 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001114
1115 // check & update map of what's occupied
1116 if (!checkItemPlacement(occupied, folderInfo)) {
1117 break;
1118 }
Joe Onorato36115782010-06-17 13:28:48 -04001119 switch (container) {
1120 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001122 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001123 break;
1124 }
1125
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001126 sItemsIdMap.put(folderInfo.id, folderInfo);
1127 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001128 break;
1129
Joe Onorato36115782010-06-17 13:28:48 -04001130 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1131 // Read all Launcher-specific widget details
1132 int appWidgetId = c.getInt(appWidgetIdIndex);
1133 id = c.getLong(idIndex);
1134
1135 final AppWidgetProviderInfo provider =
1136 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001137
Joe Onorato36115782010-06-17 13:28:48 -04001138 if (!isSafeMode && (provider == null || provider.provider == null ||
1139 provider.provider.getPackageName() == null)) {
Adam Cohen16d7ffc2011-10-05 17:49:14 -07001140 String log = "Deleting widget that isn't installed anymore: id="
1141 + id + " appWidgetId=" + appWidgetId;
1142 Log.e(TAG, log);
1143 Launcher.sDumpLogs.add(log);
Joe Onorato36115782010-06-17 13:28:48 -04001144 itemsToRemove.add(id);
1145 } else {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001146 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001147 appWidgetInfo.id = id;
1148 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001149 appWidgetInfo.cellX = c.getInt(cellXIndex);
1150 appWidgetInfo.cellY = c.getInt(cellYIndex);
1151 appWidgetInfo.spanX = c.getInt(spanXIndex);
1152 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001153
1154 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001155 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1156 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001157 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001158 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001159 continue;
1160 }
1161 appWidgetInfo.container = c.getInt(containerIndex);
1162
1163 // check & update map of what's occupied
1164 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1165 break;
1166 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001167 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1168 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001169 }
1170 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001171 }
Joe Onorato36115782010-06-17 13:28:48 -04001172 } catch (Exception e) {
1173 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001174 }
1175 }
Joe Onorato36115782010-06-17 13:28:48 -04001176 } finally {
1177 c.close();
1178 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001179
Joe Onorato36115782010-06-17 13:28:48 -04001180 if (itemsToRemove.size() > 0) {
1181 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1182 LauncherSettings.Favorites.CONTENT_URI);
1183 // Remove dead items
1184 for (long id : itemsToRemove) {
1185 if (DEBUG_LOADERS) {
1186 Log.d(TAG, "Removed id = " + id);
1187 }
1188 // Don't notify content observers
1189 try {
1190 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1191 null, null);
1192 } catch (RemoteException e) {
1193 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001194 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001195 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 }
1197
Joe Onorato36115782010-06-17 13:28:48 -04001198 if (DEBUG_LOADERS) {
1199 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1200 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001201 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001202 String line = "";
1203 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1204 if (s > 0) {
1205 line += " | ";
1206 }
Adam Cohend22015c2010-07-26 22:02:18 -07001207 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001208 line += ((occupied[s][x][y] != null) ? "#" : ".");
1209 }
1210 }
1211 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 }
Joe Onorato36115782010-06-17 13:28:48 -04001213 }
1214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215
Joe Onorato36115782010-06-17 13:28:48 -04001216 /**
1217 * Read everything out of our database.
1218 */
1219 private void bindWorkspace() {
1220 final long t = SystemClock.uptimeMillis();
1221
1222 // Don't use these two variables in any of the callback runnables.
1223 // Otherwise we hold a reference to them.
1224 final Callbacks oldCallbacks = mCallbacks.get();
1225 if (oldCallbacks == null) {
1226 // This launcher has exited and nobody bothered to tell us. Just bail.
1227 Log.w(TAG, "LoaderTask running with no launcher");
1228 return;
1229 }
1230
1231 int N;
1232 // Tell the workspace that we're about to start firing items at it
1233 mHandler.post(new Runnable() {
1234 public void run() {
1235 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1236 if (callbacks != null) {
1237 callbacks.startBinding();
1238 }
1239 }
1240 });
Winson Chung603bcb92011-09-02 11:45:39 -07001241
Winson Chung603bcb92011-09-02 11:45:39 -07001242 final ArrayList<ItemInfo> workspaceItems = unbindWorkspaceItemsOnMainThread();
1243
Joe Onorato36115782010-06-17 13:28:48 -04001244 // Add the items to the workspace.
Winson Chung603bcb92011-09-02 11:45:39 -07001245 N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001246 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1247 final int start = i;
1248 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249 mHandler.post(new Runnable() {
1250 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001251 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001253 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 }
1255 }
1256 });
Joe Onorato36115782010-06-17 13:28:48 -04001257 }
Winson Chung603bcb92011-09-02 11:45:39 -07001258 // Ensure that we don't use the same folders data structure on the main thread
1259 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001260 mHandler.post(new Runnable() {
1261 public void run() {
1262 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1263 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001264 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001265 }
1266 }
1267 });
1268 // Wait until the queue goes empty.
1269 mHandler.post(new Runnable() {
1270 public void run() {
1271 if (DEBUG_LOADERS) {
1272 Log.d(TAG, "Going to start binding widgets soon.");
1273 }
1274 }
1275 });
1276 // Bind the widgets, one at a time.
1277 // WARNING: this is calling into the workspace from the background thread,
1278 // but since getCurrentScreen() just returns the int, we should be okay. This
1279 // is just a hint for the order, and if it's wrong, we'll be okay.
1280 // TODO: instead, we should have that push the current screen into here.
1281 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001282 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001283 // once for the current screen
1284 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001285 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001286 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001287 mHandler.post(new Runnable() {
1288 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001289 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001290 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001291 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292 }
1293 }
1294 });
1295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001296 }
Joe Onorato36115782010-06-17 13:28:48 -04001297 // once for the other screens
1298 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001299 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001300 if (widget.screen != currentScreen) {
1301 mHandler.post(new Runnable() {
1302 public void run() {
1303 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1304 if (callbacks != null) {
1305 callbacks.bindAppWidget(widget);
1306 }
1307 }
1308 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001309 }
1310 }
Joe Onorato36115782010-06-17 13:28:48 -04001311 // Tell the workspace that we're done.
1312 mHandler.post(new Runnable() {
1313 public void run() {
1314 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1315 if (callbacks != null) {
1316 callbacks.finishBindingItems();
1317 }
1318 }
1319 });
1320 // If we're profiling, this is the last thing in the queue.
1321 mHandler.post(new Runnable() {
1322 public void run() {
1323 if (DEBUG_LOADERS) {
1324 Log.d(TAG, "bound workspace in "
1325 + (SystemClock.uptimeMillis()-t) + "ms");
1326 }
1327 }
1328 });
1329 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001330
Joe Onorato36115782010-06-17 13:28:48 -04001331 private void loadAndBindAllApps() {
1332 if (DEBUG_LOADERS) {
1333 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1334 }
1335 if (!mAllAppsLoaded) {
1336 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001337 synchronized (LoaderTask.this) {
1338 if (mStopped) {
1339 return;
1340 }
1341 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001342 }
Joe Onorato36115782010-06-17 13:28:48 -04001343 } else {
1344 onlyBindAllApps();
1345 }
1346 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001347
Joe Onorato36115782010-06-17 13:28:48 -04001348 private void onlyBindAllApps() {
1349 final Callbacks oldCallbacks = mCallbacks.get();
1350 if (oldCallbacks == null) {
1351 // This launcher has exited and nobody bothered to tell us. Just bail.
1352 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1353 return;
1354 }
1355
1356 // shallow copy
1357 final ArrayList<ApplicationInfo> list
Michael Jurka92f3d462011-11-22 21:02:29 -08001358 = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone();
Joe Onorato36115782010-06-17 13:28:48 -04001359 mHandler.post(new Runnable() {
1360 public void run() {
1361 final long t = SystemClock.uptimeMillis();
1362 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1363 if (callbacks != null) {
1364 callbacks.bindAllApplications(list);
1365 }
1366 if (DEBUG_LOADERS) {
1367 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1368 + (SystemClock.uptimeMillis()-t) + "ms");
1369 }
1370 }
1371 });
1372
1373 }
1374
1375 private void loadAllAppsByBatch() {
1376 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1377
1378 // Don't use these two variables in any of the callback runnables.
1379 // Otherwise we hold a reference to them.
1380 final Callbacks oldCallbacks = mCallbacks.get();
1381 if (oldCallbacks == null) {
1382 // This launcher has exited and nobody bothered to tell us. Just bail.
1383 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1384 return;
1385 }
1386
1387 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1388 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1389
1390 final PackageManager packageManager = mContext.getPackageManager();
1391 List<ResolveInfo> apps = null;
1392
1393 int N = Integer.MAX_VALUE;
1394
1395 int startIndex;
1396 int i=0;
1397 int batchSize = -1;
1398 while (i < N && !mStopped) {
1399 if (i == 0) {
1400 mAllAppsList.clear();
1401 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1402 apps = packageManager.queryIntentActivities(mainIntent, 0);
1403 if (DEBUG_LOADERS) {
1404 Log.d(TAG, "queryIntentActivities took "
1405 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1406 }
1407 if (apps == null) {
1408 return;
1409 }
1410 N = apps.size();
1411 if (DEBUG_LOADERS) {
1412 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1413 }
1414 if (N == 0) {
1415 // There are no apps?!?
1416 return;
1417 }
1418 if (mBatchSize == 0) {
1419 batchSize = N;
1420 } else {
1421 batchSize = mBatchSize;
1422 }
1423
1424 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1425 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001426 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001427 if (DEBUG_LOADERS) {
1428 Log.d(TAG, "sort took "
1429 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1430 }
1431 }
1432
1433 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1434
1435 startIndex = i;
1436 for (int j=0; i<N && j<batchSize; j++) {
1437 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001438 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001439 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001440 i++;
1441 }
1442
1443 final boolean first = i <= batchSize;
1444 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1445 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1446 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1447
Joe Onoratocc67f472010-06-08 10:54:30 -07001448 mHandler.post(new Runnable() {
1449 public void run() {
1450 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001451 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001452 if (first) {
1453 callbacks.bindAllApplications(added);
1454 } else {
1455 callbacks.bindAppsAdded(added);
1456 }
1457 if (DEBUG_LOADERS) {
1458 Log.d(TAG, "bound " + added.size() + " apps in "
1459 + (SystemClock.uptimeMillis() - t) + "ms");
1460 }
1461 } else {
1462 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001463 }
1464 }
1465 });
1466
Daniel Sandlerdca66122010-04-13 16:23:58 -04001467 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001468 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1469 + (SystemClock.uptimeMillis()-t2) + "ms");
1470 }
1471
1472 if (mAllAppsLoadDelay > 0 && i < N) {
1473 try {
1474 if (DEBUG_LOADERS) {
1475 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1476 }
1477 Thread.sleep(mAllAppsLoadDelay);
1478 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 }
1480 }
1481
Joe Onorato36115782010-06-17 13:28:48 -04001482 if (DEBUG_LOADERS) {
1483 Log.d(TAG, "cached all " + N + " apps in "
1484 + (SystemClock.uptimeMillis()-t) + "ms"
1485 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001486 }
1487 }
1488
1489 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001490 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1491 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1492 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1493 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1494 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001495 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
1497 }
1498
1499 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001500 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001501 }
1502
1503 private class PackageUpdatedTask implements Runnable {
1504 int mOp;
1505 String[] mPackages;
1506
1507 public static final int OP_NONE = 0;
1508 public static final int OP_ADD = 1;
1509 public static final int OP_UPDATE = 2;
1510 public static final int OP_REMOVE = 3; // uninstlled
1511 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1512
1513
1514 public PackageUpdatedTask(int op, String[] packages) {
1515 mOp = op;
1516 mPackages = packages;
1517 }
1518
1519 public void run() {
1520 final Context context = mApp;
1521
1522 final String[] packages = mPackages;
1523 final int N = packages.length;
1524 switch (mOp) {
1525 case OP_ADD:
1526 for (int i=0; i<N; i++) {
1527 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1528 mAllAppsList.addPackage(context, packages[i]);
1529 }
1530 break;
1531 case OP_UPDATE:
1532 for (int i=0; i<N; i++) {
1533 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1534 mAllAppsList.updatePackage(context, packages[i]);
1535 }
1536 break;
1537 case OP_REMOVE:
1538 case OP_UNAVAILABLE:
1539 for (int i=0; i<N; i++) {
1540 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1541 mAllAppsList.removePackage(packages[i]);
1542 }
1543 break;
1544 }
1545
1546 ArrayList<ApplicationInfo> added = null;
1547 ArrayList<ApplicationInfo> removed = null;
1548 ArrayList<ApplicationInfo> modified = null;
1549
1550 if (mAllAppsList.added.size() > 0) {
1551 added = mAllAppsList.added;
1552 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1553 }
1554 if (mAllAppsList.removed.size() > 0) {
1555 removed = mAllAppsList.removed;
1556 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1557 for (ApplicationInfo info: removed) {
1558 mIconCache.remove(info.intent.getComponent());
1559 }
1560 }
1561 if (mAllAppsList.modified.size() > 0) {
1562 modified = mAllAppsList.modified;
1563 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1564 }
1565
1566 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1567 if (callbacks == null) {
1568 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1569 return;
1570 }
1571
1572 if (added != null) {
1573 final ArrayList<ApplicationInfo> addedFinal = added;
1574 mHandler.post(new Runnable() {
1575 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001576 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1577 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001578 callbacks.bindAppsAdded(addedFinal);
1579 }
1580 }
1581 });
1582 }
1583 if (modified != null) {
1584 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1585 mHandler.post(new Runnable() {
1586 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001587 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1588 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001589 callbacks.bindAppsUpdated(modifiedFinal);
1590 }
1591 }
1592 });
1593 }
1594 if (removed != null) {
1595 final boolean permanent = mOp != OP_UNAVAILABLE;
1596 final ArrayList<ApplicationInfo> removedFinal = removed;
1597 mHandler.post(new Runnable() {
1598 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001599 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1600 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001601 callbacks.bindAppsRemoved(removedFinal, permanent);
1602 }
1603 }
1604 });
Joe Onoratobe386092009-11-17 17:32:16 -08001605 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001606
1607 mHandler.post(new Runnable() {
1608 @Override
1609 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001610 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1611 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001612 callbacks.bindPackagesUpdated();
1613 }
1614 }
1615 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 }
1617 }
1618
1619 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001620 * This is called from the code that adds shortcuts from the intent receiver. This
1621 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622 */
Joe Onorato56d82912010-03-07 14:32:10 -05001623 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001624 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626
Joe Onorato56d82912010-03-07 14:32:10 -05001627 /**
1628 * Make an ShortcutInfo object for a shortcut that is an application.
1629 *
1630 * If c is not null, then it will be used to fill in missing data like the title and icon.
1631 */
1632 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001633 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001634 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001635 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001636
1637 ComponentName componentName = intent.getComponent();
1638 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001639 return null;
1640 }
1641
Adam Cohen00fcb492011-11-02 21:53:47 -07001642 try {
1643 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
1644 if (!pi.applicationInfo.enabled) {
1645 // If we return null here, the corresponding item will be removed from the launcher
1646 // db and will not appear in the workspace.
1647 return null;
1648 }
1649 } catch (NameNotFoundException e) {
1650 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
1651 }
1652
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001653 // TODO: See if the PackageManager knows about this case. If it doesn't
1654 // then return null & delete this.
1655
Joe Onorato56d82912010-03-07 14:32:10 -05001656 // the resource -- This may implicitly give us back the fallback icon,
1657 // but don't worry about that. All we're doing with usingFallbackIcon is
1658 // to avoid saving lots of copies of that in the database, and most apps
1659 // have icons anyway.
1660 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1661 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001662 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001663 }
Joe Onorato56d82912010-03-07 14:32:10 -05001664 // the db
1665 if (icon == null) {
1666 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001667 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001668 }
1669 }
1670 // the fallback icon
1671 if (icon == null) {
1672 icon = getFallbackIcon();
1673 info.usingFallbackIcon = true;
1674 }
1675 info.setIcon(icon);
1676
1677 // from the resource
1678 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001679 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1680 if (labelCache != null && labelCache.containsKey(key)) {
1681 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001682 } else {
1683 info.title = resolveInfo.activityInfo.loadLabel(manager);
1684 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001685 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001686 }
1687 }
Joe Onorato56d82912010-03-07 14:32:10 -05001688 }
1689 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001691 if (c != null) {
1692 info.title = c.getString(titleIndex);
1693 }
1694 }
1695 // fall back to the class name of the activity
1696 if (info.title == null) {
1697 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001698 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001699 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1700 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001704 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001706 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001707 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1708 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709
Joe Onorato56d82912010-03-07 14:32:10 -05001710 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001711 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001714 // TODO: If there's an explicit component and we can't install that, delete it.
1715
Joe Onorato56d82912010-03-07 14:32:10 -05001716 info.title = c.getString(titleIndex);
1717
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718 int iconType = c.getInt(iconTypeIndex);
1719 switch (iconType) {
1720 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1721 String packageName = c.getString(iconPackageIndex);
1722 String resourceName = c.getString(iconResourceIndex);
1723 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001724 info.customIcon = false;
1725 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001728 if (resources != null) {
1729 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001730 icon = Utilities.createIconBitmap(
1731 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001734 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
Joe Onorato56d82912010-03-07 14:32:10 -05001736 // the db
1737 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001738 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001739 }
1740 // the fallback icon
1741 if (icon == null) {
1742 icon = getFallbackIcon();
1743 info.usingFallbackIcon = true;
1744 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 break;
1746 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001747 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001748 if (icon == null) {
1749 icon = getFallbackIcon();
1750 info.customIcon = false;
1751 info.usingFallbackIcon = true;
1752 } else {
1753 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001755 break;
1756 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001757 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001758 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 info.customIcon = false;
1760 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001762 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 return info;
1764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765
Michael Jurka931dc972011-08-05 15:08:15 -07001766 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001767 if (false) {
1768 Log.d(TAG, "getIconFromCursor app="
1769 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1770 }
1771 byte[] data = c.getBlob(iconIndex);
1772 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001773 return Utilities.createIconBitmap(
1774 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001775 } catch (Exception e) {
1776 return null;
1777 }
1778 }
1779
Winson Chung3d503fb2011-07-13 17:25:49 -07001780 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1781 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001782 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001783 if (info == null) {
1784 return null;
1785 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001786 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001787
1788 return info;
1789 }
1790
Winson Chunga9abd0e2010-10-27 17:18:37 -07001791 /**
Winson Chung55cef262010-10-28 14:14:18 -07001792 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1793 */
1794 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1795 ComponentName component) {
1796 List<AppWidgetProviderInfo> widgets =
1797 AppWidgetManager.getInstance(context).getInstalledProviders();
1798 for (AppWidgetProviderInfo info : widgets) {
1799 if (info.provider.equals(component)) {
1800 return info;
1801 }
1802 }
1803 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001804 }
1805
Winson Chung68846fd2010-10-29 11:00:27 -07001806 /**
1807 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1808 */
1809 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1810 final PackageManager packageManager = context.getPackageManager();
1811 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1812 new ArrayList<WidgetMimeTypeHandlerData>();
1813
1814 final Intent supportsIntent =
1815 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1816 supportsIntent.setType(mimeType);
1817
1818 // Create a set of widget configuration components that we can test against
1819 final List<AppWidgetProviderInfo> widgets =
1820 AppWidgetManager.getInstance(context).getInstalledProviders();
1821 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1822 new HashMap<ComponentName, AppWidgetProviderInfo>();
1823 for (AppWidgetProviderInfo info : widgets) {
1824 configurationComponentToWidget.put(info.configure, info);
1825 }
1826
1827 // Run through each of the intents that can handle this type of clip data, and cross
1828 // reference them with the components that are actual configuration components
1829 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1830 PackageManager.MATCH_DEFAULT_ONLY);
1831 for (ResolveInfo info : activities) {
1832 final ActivityInfo activityInfo = info.activityInfo;
1833 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1834 activityInfo.name);
1835 if (configurationComponentToWidget.containsKey(infoComponent)) {
1836 supportedConfigurationActivities.add(
1837 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1838 configurationComponentToWidget.get(infoComponent)));
1839 }
1840 }
1841 return supportedConfigurationActivities;
1842 }
1843
Winson Chunga9abd0e2010-10-27 17:18:37 -07001844 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001845 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1846 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1847 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1848
Adam Cohend9198822011-11-22 16:42:47 -08001849 if (intent == null) {
1850 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
1851 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
1852 return null;
1853 }
1854
Joe Onorato0589f0f2010-02-08 13:44:00 -08001855 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001856 boolean customIcon = false;
1857 ShortcutIconResource iconResource = null;
1858
1859 if (bitmap != null && bitmap instanceof Bitmap) {
1860 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001861 customIcon = true;
1862 } else {
1863 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1864 if (extra != null && extra instanceof ShortcutIconResource) {
1865 try {
1866 iconResource = (ShortcutIconResource) extra;
1867 final PackageManager packageManager = context.getPackageManager();
1868 Resources resources = packageManager.getResourcesForApplication(
1869 iconResource.packageName);
1870 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001871 icon = Utilities.createIconBitmap(
1872 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001873 } catch (Exception e) {
1874 Log.w(TAG, "Could not load shortcut icon: " + extra);
1875 }
1876 }
1877 }
1878
Michael Jurkac9d95c52011-08-29 14:03:34 -07001879 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001880
1881 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001882 if (fallbackIcon != null) {
1883 icon = fallbackIcon;
1884 } else {
1885 icon = getFallbackIcon();
1886 info.usingFallbackIcon = true;
1887 }
Joe Onorato56d82912010-03-07 14:32:10 -05001888 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001889 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001890
Joe Onorato0589f0f2010-02-08 13:44:00 -08001891 info.title = name;
1892 info.intent = intent;
1893 info.customIcon = customIcon;
1894 info.iconResource = iconResource;
1895
1896 return info;
1897 }
1898
Winson Chungaac01e12011-08-17 10:37:13 -07001899 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1900 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001901 // If apps can't be on SD, don't even bother.
1902 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001903 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001904 }
Joe Onorato56d82912010-03-07 14:32:10 -05001905 // If this icon doesn't have a custom icon, check to see
1906 // what's stored in the DB, and if it doesn't match what
1907 // we're going to show, store what we are going to show back
1908 // into the DB. We do this so when we're loading, if the
1909 // package manager can't find an icon (for example because
1910 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001911 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001912 cache.put(info, c.getBlob(iconIndex));
1913 return true;
1914 }
1915 return false;
1916 }
1917 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1918 boolean needSave = false;
1919 try {
1920 if (data != null) {
1921 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1922 Bitmap loaded = info.getIcon(mIconCache);
1923 needSave = !saved.sameAs(loaded);
1924 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001925 needSave = true;
1926 }
Winson Chungaac01e12011-08-17 10:37:13 -07001927 } catch (Exception e) {
1928 needSave = true;
1929 }
1930 if (needSave) {
1931 Log.d(TAG, "going to save icon bitmap for info=" + info);
1932 // This is slower than is ideal, but this only happens once
1933 // or when the app is updated with a new icon.
1934 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001935 }
1936 }
1937
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001939 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001940 * or make a new one.
1941 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001942 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001943 // See if a placeholder was created for us already
1944 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001945 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001946 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07001947 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 folders.put(id, folderInfo);
1949 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001950 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001954 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 = new Comparator<ApplicationInfo>() {
1956 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001957 int result = sCollator.compare(a.title.toString(), b.title.toString());
1958 if (result == 0) {
1959 result = a.componentName.compareTo(b.componentName);
1960 }
1961 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001963 };
Winson Chung78403fe2011-01-21 15:38:02 -08001964 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1965 = new Comparator<ApplicationInfo>() {
1966 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1967 if (a.firstInstallTime < b.firstInstallTime) return 1;
1968 if (a.firstInstallTime > b.firstInstallTime) return -1;
1969 return 0;
1970 }
1971 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001972 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1973 = new Comparator<AppWidgetProviderInfo>() {
1974 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1975 return sCollator.compare(a.label.toString(), b.label.toString());
1976 }
1977 };
Winson Chung5308f242011-08-18 12:12:41 -07001978 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1979 if (info.activityInfo != null) {
1980 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1981 } else {
1982 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1983 }
1984 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001985 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1986 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001987 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001988 ShortcutNameComparator(PackageManager pm) {
1989 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001990 mLabelCache = new HashMap<Object, CharSequence>();
1991 }
1992 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1993 mPackageManager = pm;
1994 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001995 }
1996 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001997 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001998 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1999 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2000 if (mLabelCache.containsKey(keyA)) {
2001 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002002 } else {
2003 labelA = a.loadLabel(mPackageManager).toString();
2004
Winson Chung5308f242011-08-18 12:12:41 -07002005 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002006 }
Winson Chung5308f242011-08-18 12:12:41 -07002007 if (mLabelCache.containsKey(keyB)) {
2008 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002009 } else {
2010 labelB = b.loadLabel(mPackageManager).toString();
2011
Winson Chung5308f242011-08-18 12:12:41 -07002012 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002013 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002014 return sCollator.compare(labelA, labelB);
2015 }
2016 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002017 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
2018 private PackageManager mPackageManager;
2019 private HashMap<Object, String> mLabelCache;
2020 WidgetAndShortcutNameComparator(PackageManager pm) {
2021 mPackageManager = pm;
2022 mLabelCache = new HashMap<Object, String>();
2023 }
2024 public final int compare(Object a, Object b) {
2025 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002026 if (mLabelCache.containsKey(a)) {
2027 labelA = mLabelCache.get(a);
2028 } else {
2029 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002030 ((AppWidgetProviderInfo) a).label :
2031 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002032 mLabelCache.put(a, labelA);
2033 }
2034 if (mLabelCache.containsKey(b)) {
2035 labelB = mLabelCache.get(b);
2036 } else {
2037 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002038 ((AppWidgetProviderInfo) b).label :
2039 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002040 mLabelCache.put(b, labelB);
2041 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002042 return sCollator.compare(labelA, labelB);
2043 }
2044 };
Joe Onoratobe386092009-11-17 17:32:16 -08002045
2046 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002047 Log.d(TAG, "mCallbacks=" + mCallbacks);
2048 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
2049 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
2050 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
2051 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002052 if (mLoaderTask != null) {
2053 mLoaderTask.dumpState();
2054 } else {
2055 Log.d(TAG, "mLoaderTask=null");
2056 }
Joe Onoratobe386092009-11-17 17:32:16 -08002057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058}