blob: 9bf88381b43908d4fc5eca1b2ffa9092877ff2e4 [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
Winson Chung68846fd2010-10-29 11:00:27 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
Winson Chungaafa03c2010-06-11 17:34:16 -070027
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080040import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.pm.ResolveInfo;
42import android.content.res.Resources;
43import android.database.Cursor;
44import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080047import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040048import android.os.Handler;
49import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080050import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040053import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070054import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
Winson Chung68846fd2010-10-29 11:00:27 -070056import com.android.launcher.R;
57import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080058
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * Maintains in-memory state of the Launcher. It is expected that there should be only one
61 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070062 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040064public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080065 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070067
Joe Onorato36115782010-06-17 13:28:48 -040068 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080069 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040070 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040071 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040072
Joe Onoratof99f8c12009-10-31 17:27:36 -040073 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 private final Object mLock = new Object();
75 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040076 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070078 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
79 static {
80 sWorkerThread.start();
81 }
82 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
83
Joe Onoratocc67f472010-06-08 10:54:30 -070084 // We start off with everything not loaded. After that, we assume that
85 // our monitoring of the package manager provides all updates and we never
86 // need to do a requery. These are only ever touched from the loader thread.
87 private boolean mWorkspaceLoaded;
88 private boolean mAllAppsLoaded;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Joe Onorato36115782010-06-17 13:28:48 -040092 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080093 private IconCache mIconCache;
94
95 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Adam Cohend22015c2010-07-26 22:02:18 -070097 private static int mCellCountX;
98 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -070099
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700101 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400102 public int getCurrentWorkspaceScreen();
103 public void startBinding();
104 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500105 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 public void finishBindingItems();
107 public void bindAppWidget(LauncherAppWidgetInfo info);
108 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500109 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
110 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400111 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400113 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato0589f0f2010-02-08 13:44:00 -0800116 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800117 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400118 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800119 mAllAppsList = new AllAppsList(iconCache);
120 mIconCache = iconCache;
121
122 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700123 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400124
125 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400126
127 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800128 }
129
Joe Onorato56d82912010-03-07 14:32:10 -0500130 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800131 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato9c1289c2009-08-17 11:03:03 -0400134 /**
135 * Adds an item to the DB if it was not created previously, or move it to a new
136 * <container, screen, cellX, cellY>
137 */
138 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
139 int screen, int cellX, int cellY) {
140 if (item.container == ItemInfo.NO_ID) {
141 // From all apps
142 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
143 } else {
144 // From somewhere else
145 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147 }
148
149 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700151 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
153 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 item.container = container;
156 item.screen = screen;
157 item.cellX = cellX;
158 item.cellY = cellY;
159
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700160 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 final ContentValues values = new ContentValues();
162 final ContentResolver cr = context.getContentResolver();
163
164 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700165 values.put(LauncherSettings.Favorites.CELLX, cellX);
166 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400167 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
168
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700169 sWorker.post(new Runnable() {
170 public void run() {
171 cr.update(uri, values, null, null);
172 }
173 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700174 }
175
176 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800177 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800178 */
Adam Cohen1b607ed2011-03-03 17:26:50 -0800179 static void resizeItemInDatabase(Context context, ItemInfo item, int cellX, int cellY,
Adam Cohend4844c32011-02-18 19:25:06 -0800180 int spanX, int spanY) {
181 item.spanX = spanX;
182 item.spanY = spanY;
183 item.cellX = cellX;
184 item.cellY = cellY;
185
186 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
187 final ContentValues values = new ContentValues();
188 final ContentResolver cr = context.getContentResolver();
189
190 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
191 values.put(LauncherSettings.Favorites.SPANX, spanX);
192 values.put(LauncherSettings.Favorites.SPANY, spanY);
193 values.put(LauncherSettings.Favorites.CELLX, cellX);
194 values.put(LauncherSettings.Favorites.CELLY, cellY);
195
196 sWorker.post(new Runnable() {
197 public void run() {
198 cr.update(uri, values, null, null);
199 }
200 });
201 }
202
203 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 * Returns true if the shortcuts already exists in the database.
205 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 static boolean shortcutExists(Context context, String title, Intent intent) {
208 final ContentResolver cr = context.getContentResolver();
209 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
210 new String[] { "title", "intent" }, "title=? and intent=?",
211 new String[] { title, intent.toUri(0) }, null);
212 boolean result = false;
213 try {
214 result = c.moveToFirst();
215 } finally {
216 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700219 }
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700222 * Returns an ItemInfo array containing all the items in the LauncherModel.
223 * The ItemInfo.id is not set through this function.
224 */
225 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
226 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
227 final ContentResolver cr = context.getContentResolver();
228 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
229 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
230 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
231 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
232
233 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
234 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
235 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
236 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
237 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
238 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
239 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
240
241 try {
242 while (c.moveToNext()) {
243 ItemInfo item = new ItemInfo();
244 item.cellX = c.getInt(cellXIndex);
245 item.cellY = c.getInt(cellYIndex);
246 item.spanX = c.getInt(spanXIndex);
247 item.spanY = c.getInt(spanYIndex);
248 item.container = c.getInt(containerIndex);
249 item.itemType = c.getInt(itemTypeIndex);
250 item.screen = c.getInt(screenIndex);
251
252 items.add(item);
253 }
254 } catch (Exception e) {
255 items.clear();
256 } finally {
257 c.close();
258 }
259
260 return items;
261 }
262
263 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
265 */
266 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
267 final ContentResolver cr = context.getContentResolver();
268 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
269 "_id=? and (itemType=? or itemType=?)",
270 new String[] { String.valueOf(id),
271 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
272 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700273
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 try {
275 if (c.moveToFirst()) {
276 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
277 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
278 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
279 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
280 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
281 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 FolderInfo folderInfo = null;
284 switch (c.getInt(itemTypeIndex)) {
285 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
286 folderInfo = findOrMakeUserFolder(folderList, id);
287 break;
288 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
289 folderInfo = findOrMakeLiveFolder(folderList, id);
290 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700291 }
292
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 folderInfo.title = c.getString(titleIndex);
294 folderInfo.id = id;
295 folderInfo.container = c.getInt(containerIndex);
296 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700297 folderInfo.cellX = c.getInt(cellXIndex);
298 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299
300 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 } finally {
303 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700304 }
305
306 return null;
307 }
308
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 /**
310 * Add an item to the database in a specified container. Sets the container, screen, cellX and
311 * cellY fields of the item. Also assigns an ID to the item.
312 */
313 static void addItemToDatabase(Context context, ItemInfo item, long container,
314 int screen, int cellX, int cellY, boolean notify) {
315 item.container = container;
316 item.screen = screen;
317 item.cellX = cellX;
318 item.cellY = cellY;
319
320 final ContentValues values = new ContentValues();
321 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400322 item.onAddToDatabase(values);
323
Adam Cohend22015c2010-07-26 22:02:18 -0700324 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700325
Joe Onorato9c1289c2009-08-17 11:03:03 -0400326 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
327 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
328
329 if (result != null) {
330 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700331 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700332 }
333
Joe Onorato9c1289c2009-08-17 11:03:03 -0400334 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700335 * Creates a new unique child id, for a given cell span across all layouts.
336 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700337 static int getCellLayoutChildId(
338 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
339 return ((cellId & 0xFF) << 24)
340 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700341 }
342
Adam Cohend22015c2010-07-26 22:02:18 -0700343 static int getCellCountX() {
344 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700345 }
346
Adam Cohend22015c2010-07-26 22:02:18 -0700347 static int getCellCountY() {
348 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700349 }
350
351 /**
352 * Updates the model orientation helper to take into account the current layout dimensions
353 * when performing local/canonical coordinate transformations.
354 */
355 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700356 mCellCountX = shortAxisCellCount;
357 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700358 }
359
360 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 * Update an item to the database in a specified container.
362 */
363 static void updateItemInDatabase(Context context, ItemInfo item) {
364 final ContentValues values = new ContentValues();
365 final ContentResolver cr = context.getContentResolver();
366
367 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700368 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700369
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
371 }
372
373 /**
374 * Removes the specified item from the database
375 * @param context
376 * @param item
377 */
378 static void deleteItemFromDatabase(Context context, ItemInfo item) {
379 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700380 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700381 sWorker.post(new Runnable() {
382 public void run() {
383 cr.delete(uriToDelete, null, null);
384 }
385 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 }
387
388 /**
389 * Remove the contents of the specified folder from the database
390 */
391 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
392 final ContentResolver cr = context.getContentResolver();
393
394 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
395 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
396 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
397 }
398
399 /**
400 * Set this as the current Launcher activity object for the loader.
401 */
402 public void initialize(Callbacks callbacks) {
403 synchronized (mLock) {
404 mCallbacks = new WeakReference<Callbacks>(callbacks);
405 }
406 }
407
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700408 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
410 * ACTION_PACKAGE_CHANGED.
411 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400412 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400413 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700414
Joe Onorato36115782010-06-17 13:28:48 -0400415 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400416
Joe Onorato36115782010-06-17 13:28:48 -0400417 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
418 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
419 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
420 final String packageName = intent.getData().getSchemeSpecificPart();
421 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422
Joe Onorato36115782010-06-17 13:28:48 -0400423 int op = PackageUpdatedTask.OP_NONE;
424
425 if (packageName == null || packageName.length() == 0) {
426 // they sent us a bad intent
427 return;
428 }
429
430 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
431 op = PackageUpdatedTask.OP_UPDATE;
432 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
433 if (!replacing) {
434 op = PackageUpdatedTask.OP_REMOVE;
435 }
436 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
437 // later, we will update the package at this time
438 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
439 if (!replacing) {
440 op = PackageUpdatedTask.OP_ADD;
441 } else {
442 op = PackageUpdatedTask.OP_UPDATE;
443 }
444 }
445
446 if (op != PackageUpdatedTask.OP_NONE) {
447 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
448 }
449
450 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400451 // First, schedule to add these apps back in.
452 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
453 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
454 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700455 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400456 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
457 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
458 enqueuePackageUpdated(new PackageUpdatedTask(
459 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700460 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
461 // If we have changed locale we need to clear out the labels in all apps.
462 // Do this here because if the launcher activity is running it will be restarted.
463 // If it's not running startLoaderFromBackground will merely tell it that it needs
464 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
465 // next time.
466 mAllAppsLoaded = false;
467 startLoaderFromBackground();
468 }
469 }
470
471 /**
472 * When the launcher is in the background, it's possible for it to miss paired
473 * configuration changes. So whenever we trigger the loader from the background
474 * tell the launcher that it needs to re-run the loader when it comes back instead
475 * of doing it now.
476 */
477 public void startLoaderFromBackground() {
478 boolean runLoader = false;
479 if (mCallbacks != null) {
480 Callbacks callbacks = mCallbacks.get();
481 if (callbacks != null) {
482 // Only actually run the loader if they're not paused.
483 if (!callbacks.setLoadOnResume()) {
484 runLoader = true;
485 }
486 }
487 }
488 if (runLoader) {
489 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700490 }
Joe Onorato36115782010-06-17 13:28:48 -0400491 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400492
Joe Onorato36115782010-06-17 13:28:48 -0400493 public void startLoader(Context context, boolean isLaunching) {
494 synchronized (mLock) {
495 if (DEBUG_LOADERS) {
496 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
497 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400498
Joe Onorato36115782010-06-17 13:28:48 -0400499 // Don't bother to start the thread if we know it's not going to do anything
500 if (mCallbacks != null && mCallbacks.get() != null) {
501 // If there is already one running, tell it to stop.
502 LoaderTask oldTask = mLoaderTask;
503 if (oldTask != null) {
504 if (oldTask.isLaunching()) {
505 // don't downgrade isLaunching if we're already running
506 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500507 }
Joe Onorato36115782010-06-17 13:28:48 -0400508 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500509 }
Joe Onorato36115782010-06-17 13:28:48 -0400510 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700511 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 }
514 }
515
Joe Onorato36115782010-06-17 13:28:48 -0400516 public void stopLoader() {
517 synchronized (mLock) {
518 if (mLoaderTask != null) {
519 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400520 }
521 }
Joe Onorato36115782010-06-17 13:28:48 -0400522 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400523
Joe Onorato36115782010-06-17 13:28:48 -0400524 /**
525 * Runnable for the thread that loads the contents of the launcher:
526 * - workspace icons
527 * - widgets
528 * - all apps icons
529 */
530 private class LoaderTask implements Runnable {
531 private Context mContext;
532 private Thread mWaitThread;
533 private boolean mIsLaunching;
534 private boolean mStopped;
535 private boolean mLoadAndBindStepFinished;
536
Adam Cohen71a95462011-03-22 13:54:11 -0700537 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
538 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
539 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
540
Joe Onorato36115782010-06-17 13:28:48 -0400541 LoaderTask(Context context, boolean isLaunching) {
542 mContext = context;
543 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 }
545
Joe Onorato36115782010-06-17 13:28:48 -0400546 boolean isLaunching() {
547 return mIsLaunching;
548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549
Joe Onorato36115782010-06-17 13:28:48 -0400550 private void loadAndBindWorkspace() {
551 // Load the workspace
552
553 // For now, just always reload the workspace. It's ~100 ms vs. the
554 // binding which takes many hundreds of ms.
555 // We can reconsider.
556 if (DEBUG_LOADERS) {
557 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 }
Joe Onorato36115782010-06-17 13:28:48 -0400559 if (true || !mWorkspaceLoaded) {
560 loadWorkspace();
561 if (mStopped) {
562 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 }
Joe Onorato36115782010-06-17 13:28:48 -0400564 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565 }
566
Joe Onorato36115782010-06-17 13:28:48 -0400567 // Bind the workspace
568 bindWorkspace();
569 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400570
Joe Onorato36115782010-06-17 13:28:48 -0400571 private void waitForIdle() {
572 // Wait until the either we're stopped or the other threads are done.
573 // This way we don't start loading all apps until the workspace has settled
574 // down.
575 synchronized (LoaderTask.this) {
576 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700577
Joe Onorato36115782010-06-17 13:28:48 -0400578 mHandler.postIdle(new Runnable() {
579 public void run() {
580 synchronized (LoaderTask.this) {
581 mLoadAndBindStepFinished = true;
582 if (DEBUG_LOADERS) {
583 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400584 }
Joe Onorato36115782010-06-17 13:28:48 -0400585 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400586 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400587 }
Joe Onorato36115782010-06-17 13:28:48 -0400588 });
589
590 while (!mStopped && !mLoadAndBindStepFinished) {
591 try {
592 this.wait();
593 } catch (InterruptedException ex) {
594 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400595 }
596 }
Joe Onorato36115782010-06-17 13:28:48 -0400597 if (DEBUG_LOADERS) {
598 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400600 + "ms for previous step to finish binding");
601 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400602 }
Joe Onorato36115782010-06-17 13:28:48 -0400603 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400604
Joe Onorato36115782010-06-17 13:28:48 -0400605 public void run() {
606 // Optimize for end-user experience: if the Launcher is up and // running with the
607 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
608 // workspace first (default).
609 final Callbacks cbk = mCallbacks.get();
610 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400611
Joe Onorato36115782010-06-17 13:28:48 -0400612 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400613 // Elevate priority when Home launches for the first time to avoid
614 // starving at boot time. Staring at a blank home is not cool.
615 synchronized (mLock) {
616 android.os.Process.setThreadPriority(mIsLaunching
617 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
618 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400619 if (loadWorkspaceFirst) {
620 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
621 loadAndBindWorkspace();
622 } else {
623 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700624 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400625 }
626
Joe Onorato36115782010-06-17 13:28:48 -0400627 if (mStopped) {
628 break keep_running;
629 }
630
631 // Whew! Hard work done. Slow us down, and wait until the UI thread has
632 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400633 synchronized (mLock) {
634 if (mIsLaunching) {
635 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
636 }
637 }
Joe Onorato36115782010-06-17 13:28:48 -0400638 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400639
640 // second step
641 if (loadWorkspaceFirst) {
642 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700643 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400644 } else {
645 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
646 loadAndBindWorkspace();
647 }
Joe Onorato36115782010-06-17 13:28:48 -0400648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649
Joe Onorato36115782010-06-17 13:28:48 -0400650 // Clear out this reference, otherwise we end up holding it until all of the
651 // callback runnables are done.
652 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653
Joe Onorato36115782010-06-17 13:28:48 -0400654 synchronized (mLock) {
655 // If we are still the last one to be scheduled, remove ourselves.
656 if (mLoaderTask == this) {
657 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 }
Joe Onorato36115782010-06-17 13:28:48 -0400659 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700660
Joe Onorato36115782010-06-17 13:28:48 -0400661 // Trigger a gc to try to clean up after the stuff is done, since the
662 // renderscript allocations aren't charged to the java heap.
663 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700664 mHandler.post(new Runnable() {
665 public void run() {
666 System.gc();
667 }
668 });
Joe Onorato36115782010-06-17 13:28:48 -0400669 } else {
670 mHandler.postIdle(new Runnable() {
671 public void run() {
672 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400673 }
Joe Onorato36115782010-06-17 13:28:48 -0400674 });
675 }
676 }
677
678 public void stopLocked() {
679 synchronized (LoaderTask.this) {
680 mStopped = true;
681 this.notify();
682 }
683 }
684
685 /**
686 * Gets the callbacks object. If we've been stopped, or if the launcher object
687 * has somehow been garbage collected, return null instead. Pass in the Callbacks
688 * object that was around when the deferred message was scheduled, and if there's
689 * a new Callbacks object around then also return null. This will save us from
690 * calling onto it with data that will be ignored.
691 */
692 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
693 synchronized (mLock) {
694 if (mStopped) {
695 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400696 }
Joe Onorato36115782010-06-17 13:28:48 -0400697
698 if (mCallbacks == null) {
699 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400700 }
Joe Onorato36115782010-06-17 13:28:48 -0400701
702 final Callbacks callbacks = mCallbacks.get();
703 if (callbacks != oldCallbacks) {
704 return null;
705 }
706 if (callbacks == null) {
707 Log.w(TAG, "no mCallbacks");
708 return null;
709 }
710
711 return callbacks;
712 }
713 }
714
715 // check & update map of what's occupied; used to discard overlapping/invalid items
716 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
717 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400718 return true;
719 }
Joe Onorato36115782010-06-17 13:28:48 -0400720 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
721 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
722 if (occupied[item.screen][x][y] != null) {
723 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700724 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400725 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700726 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400727 + occupied[item.screen][x][y]);
728 return false;
729 }
730 }
731 }
732 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
733 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
734 occupied[item.screen][x][y] = item;
735 }
736 }
737 return true;
738 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739
Joe Onorato36115782010-06-17 13:28:48 -0400740 private void loadWorkspace() {
741 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742
Joe Onorato36115782010-06-17 13:28:48 -0400743 final Context context = mContext;
744 final ContentResolver contentResolver = context.getContentResolver();
745 final PackageManager manager = context.getPackageManager();
746 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
747 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400748
Joe Onorato36115782010-06-17 13:28:48 -0400749 mItems.clear();
750 mAppWidgets.clear();
751 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800752
Joe Onorato36115782010-06-17 13:28:48 -0400753 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754
Joe Onorato36115782010-06-17 13:28:48 -0400755 final Cursor c = contentResolver.query(
756 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400757
Adam Cohend22015c2010-07-26 22:02:18 -0700758 final ItemInfo occupied[][][] =
759 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760
Joe Onorato36115782010-06-17 13:28:48 -0400761 try {
762 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
763 final int intentIndex = c.getColumnIndexOrThrow
764 (LauncherSettings.Favorites.INTENT);
765 final int titleIndex = c.getColumnIndexOrThrow
766 (LauncherSettings.Favorites.TITLE);
767 final int iconTypeIndex = c.getColumnIndexOrThrow(
768 LauncherSettings.Favorites.ICON_TYPE);
769 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
770 final int iconPackageIndex = c.getColumnIndexOrThrow(
771 LauncherSettings.Favorites.ICON_PACKAGE);
772 final int iconResourceIndex = c.getColumnIndexOrThrow(
773 LauncherSettings.Favorites.ICON_RESOURCE);
774 final int containerIndex = c.getColumnIndexOrThrow(
775 LauncherSettings.Favorites.CONTAINER);
776 final int itemTypeIndex = c.getColumnIndexOrThrow(
777 LauncherSettings.Favorites.ITEM_TYPE);
778 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
779 LauncherSettings.Favorites.APPWIDGET_ID);
780 final int screenIndex = c.getColumnIndexOrThrow(
781 LauncherSettings.Favorites.SCREEN);
782 final int cellXIndex = c.getColumnIndexOrThrow
783 (LauncherSettings.Favorites.CELLX);
784 final int cellYIndex = c.getColumnIndexOrThrow
785 (LauncherSettings.Favorites.CELLY);
786 final int spanXIndex = c.getColumnIndexOrThrow
787 (LauncherSettings.Favorites.SPANX);
788 final int spanYIndex = c.getColumnIndexOrThrow(
789 LauncherSettings.Favorites.SPANY);
790 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
791 final int displayModeIndex = c.getColumnIndexOrThrow(
792 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793
Joe Onorato36115782010-06-17 13:28:48 -0400794 ShortcutInfo info;
795 String intentDescription;
796 LauncherAppWidgetInfo appWidgetInfo;
797 int container;
798 long id;
799 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800
Joe Onorato36115782010-06-17 13:28:48 -0400801 while (!mStopped && c.moveToNext()) {
802 try {
803 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804
Joe Onorato36115782010-06-17 13:28:48 -0400805 switch (itemType) {
806 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
807 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
808 intentDescription = c.getString(intentIndex);
809 try {
810 intent = Intent.parseUri(intentDescription, 0);
811 } catch (URISyntaxException e) {
812 continue;
813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814
Joe Onorato36115782010-06-17 13:28:48 -0400815 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
816 info = getShortcutInfo(manager, intent, context, c, iconIndex,
817 titleIndex);
818 } else {
819 info = getShortcutInfo(c, context, iconTypeIndex,
820 iconPackageIndex, iconResourceIndex, iconIndex,
821 titleIndex);
822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823
Joe Onorato36115782010-06-17 13:28:48 -0400824 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400825 info.intent = intent;
826 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400828 info.container = container;
829 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700830 info.cellX = c.getInt(cellXIndex);
831 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832
Daniel Sandler8802e962010-05-26 16:28:16 -0400833 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400834 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400835 break;
836 }
837
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400839 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
840 mItems.add(info);
841 break;
842 default:
843 // Item is in a user folder
844 UserFolderInfo folderInfo =
845 findOrMakeUserFolder(mFolders, container);
846 folderInfo.add(info);
847 break;
848 }
Joe Onorato17a89222011-02-08 17:26:11 -0800849
850 // now that we've loaded everthing re-save it with the
851 // icon in case it disappears somehow.
852 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400853 } else {
854 // Failed to load the shortcut, probably because the
855 // activity manager couldn't resolve it (maybe the app
856 // was uninstalled), or the db row was somehow screwed up.
857 // Delete it.
858 id = c.getLong(idIndex);
859 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
860 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
861 id, false), null, null);
862 }
863 break;
864
865 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
866 id = c.getLong(idIndex);
867 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
868
Winson Chungaafa03c2010-06-11 17:34:16 -0700869 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400870 folderInfo.id = id;
871 container = c.getInt(containerIndex);
872 folderInfo.container = container;
873 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700874 folderInfo.cellX = c.getInt(cellXIndex);
875 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400876
877 // check & update map of what's occupied
878 if (!checkItemPlacement(occupied, folderInfo)) {
879 break;
880 }
Joe Onorato36115782010-06-17 13:28:48 -0400881 switch (container) {
882 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
883 mItems.add(folderInfo);
884 break;
885 }
886
887 mFolders.put(folderInfo.id, folderInfo);
888 break;
889
890 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
891 id = c.getLong(idIndex);
892 Uri uri = Uri.parse(c.getString(uriIndex));
893
894 // Make sure the live folder exists
895 final ProviderInfo providerInfo =
896 context.getPackageManager().resolveContentProvider(
897 uri.getAuthority(), 0);
898
899 if (providerInfo == null && !isSafeMode) {
900 itemsToRemove.add(id);
901 } else {
902 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400903 intentDescription = c.getString(intentIndex);
904 intent = null;
905 if (intentDescription != null) {
906 try {
907 intent = Intent.parseUri(intentDescription, 0);
908 } catch (URISyntaxException e) {
909 // Ignore, a live folder might not have a base intent
910 }
911 }
912
913 liveFolderInfo.title = c.getString(titleIndex);
914 liveFolderInfo.id = id;
915 liveFolderInfo.uri = uri;
916 container = c.getInt(containerIndex);
917 liveFolderInfo.container = container;
918 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700919 liveFolderInfo.cellX = c.getInt(cellXIndex);
920 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400921 liveFolderInfo.baseIntent = intent;
922 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
923
924 // check & update map of what's occupied
925 if (!checkItemPlacement(occupied, liveFolderInfo)) {
926 break;
927 }
928
929 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
930 iconResourceIndex, liveFolderInfo);
931
932 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400934 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935 break;
936 }
Joe Onorato36115782010-06-17 13:28:48 -0400937 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 }
Joe Onorato36115782010-06-17 13:28:48 -0400939 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800940
Joe Onorato36115782010-06-17 13:28:48 -0400941 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
942 // Read all Launcher-specific widget details
943 int appWidgetId = c.getInt(appWidgetIdIndex);
944 id = c.getLong(idIndex);
945
946 final AppWidgetProviderInfo provider =
947 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700948
Joe Onorato36115782010-06-17 13:28:48 -0400949 if (!isSafeMode && (provider == null || provider.provider == null ||
950 provider.provider.getPackageName() == null)) {
951 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
952 + id + " appWidgetId=" + appWidgetId);
953 itemsToRemove.add(id);
954 } else {
955 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
956 appWidgetInfo.id = id;
957 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700958 appWidgetInfo.cellX = c.getInt(cellXIndex);
959 appWidgetInfo.cellY = c.getInt(cellYIndex);
960 appWidgetInfo.spanX = c.getInt(spanXIndex);
961 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400962
963 container = c.getInt(containerIndex);
964 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
965 Log.e(TAG, "Widget found where container "
966 + "!= CONTAINER_DESKTOP -- ignoring!");
967 continue;
968 }
969 appWidgetInfo.container = c.getInt(containerIndex);
970
971 // check & update map of what's occupied
972 if (!checkItemPlacement(occupied, appWidgetInfo)) {
973 break;
974 }
975
976 mAppWidgets.add(appWidgetInfo);
977 }
978 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800979 }
Joe Onorato36115782010-06-17 13:28:48 -0400980 } catch (Exception e) {
981 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800982 }
983 }
Joe Onorato36115782010-06-17 13:28:48 -0400984 } finally {
985 c.close();
986 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800987
Joe Onorato36115782010-06-17 13:28:48 -0400988 if (itemsToRemove.size() > 0) {
989 ContentProviderClient client = contentResolver.acquireContentProviderClient(
990 LauncherSettings.Favorites.CONTENT_URI);
991 // Remove dead items
992 for (long id : itemsToRemove) {
993 if (DEBUG_LOADERS) {
994 Log.d(TAG, "Removed id = " + id);
995 }
996 // Don't notify content observers
997 try {
998 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
999 null, null);
1000 } catch (RemoteException e) {
1001 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001002 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004 }
1005
Joe Onorato36115782010-06-17 13:28:48 -04001006 if (DEBUG_LOADERS) {
1007 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1008 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001009 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001010 String line = "";
1011 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1012 if (s > 0) {
1013 line += " | ";
1014 }
Adam Cohend22015c2010-07-26 22:02:18 -07001015 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001016 line += ((occupied[s][x][y] != null) ? "#" : ".");
1017 }
1018 }
1019 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 }
Joe Onorato36115782010-06-17 13:28:48 -04001021 }
1022 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023
Joe Onorato36115782010-06-17 13:28:48 -04001024 /**
1025 * Read everything out of our database.
1026 */
1027 private void bindWorkspace() {
1028 final long t = SystemClock.uptimeMillis();
1029
1030 // Don't use these two variables in any of the callback runnables.
1031 // Otherwise we hold a reference to them.
1032 final Callbacks oldCallbacks = mCallbacks.get();
1033 if (oldCallbacks == null) {
1034 // This launcher has exited and nobody bothered to tell us. Just bail.
1035 Log.w(TAG, "LoaderTask running with no launcher");
1036 return;
1037 }
1038
1039 int N;
1040 // Tell the workspace that we're about to start firing items at it
1041 mHandler.post(new Runnable() {
1042 public void run() {
1043 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1044 if (callbacks != null) {
1045 callbacks.startBinding();
1046 }
1047 }
1048 });
1049 // Add the items to the workspace.
1050 N = mItems.size();
1051 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1052 final int start = i;
1053 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 mHandler.post(new Runnable() {
1055 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001056 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001058 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 }
1060 }
1061 });
Joe Onorato36115782010-06-17 13:28:48 -04001062 }
1063 mHandler.post(new Runnable() {
1064 public void run() {
1065 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1066 if (callbacks != null) {
1067 callbacks.bindFolders(mFolders);
1068 }
1069 }
1070 });
1071 // Wait until the queue goes empty.
1072 mHandler.post(new Runnable() {
1073 public void run() {
1074 if (DEBUG_LOADERS) {
1075 Log.d(TAG, "Going to start binding widgets soon.");
1076 }
1077 }
1078 });
1079 // Bind the widgets, one at a time.
1080 // WARNING: this is calling into the workspace from the background thread,
1081 // but since getCurrentScreen() just returns the int, we should be okay. This
1082 // is just a hint for the order, and if it's wrong, we'll be okay.
1083 // TODO: instead, we should have that push the current screen into here.
1084 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1085 N = mAppWidgets.size();
1086 // once for the current screen
1087 for (int i=0; i<N; i++) {
1088 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1089 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 mHandler.post(new Runnable() {
1091 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001092 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001094 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 }
1096 }
1097 });
1098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099 }
Joe Onorato36115782010-06-17 13:28:48 -04001100 // once for the other screens
1101 for (int i=0; i<N; i++) {
1102 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1103 if (widget.screen != currentScreen) {
1104 mHandler.post(new Runnable() {
1105 public void run() {
1106 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1107 if (callbacks != null) {
1108 callbacks.bindAppWidget(widget);
1109 }
1110 }
1111 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001112 }
1113 }
Joe Onorato36115782010-06-17 13:28:48 -04001114 // Tell the workspace that we're done.
1115 mHandler.post(new Runnable() {
1116 public void run() {
1117 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1118 if (callbacks != null) {
1119 callbacks.finishBindingItems();
1120 }
1121 }
1122 });
1123 // If we're profiling, this is the last thing in the queue.
1124 mHandler.post(new Runnable() {
1125 public void run() {
1126 if (DEBUG_LOADERS) {
1127 Log.d(TAG, "bound workspace in "
1128 + (SystemClock.uptimeMillis()-t) + "ms");
1129 }
1130 }
1131 });
1132 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001133
Joe Onorato36115782010-06-17 13:28:48 -04001134 private void loadAndBindAllApps() {
1135 if (DEBUG_LOADERS) {
1136 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1137 }
1138 if (!mAllAppsLoaded) {
1139 loadAllAppsByBatch();
1140 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001141 return;
1142 }
Joe Onorato36115782010-06-17 13:28:48 -04001143 mAllAppsLoaded = true;
1144 } else {
1145 onlyBindAllApps();
1146 }
1147 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001148
Joe Onorato36115782010-06-17 13:28:48 -04001149 private void onlyBindAllApps() {
1150 final Callbacks oldCallbacks = mCallbacks.get();
1151 if (oldCallbacks == null) {
1152 // This launcher has exited and nobody bothered to tell us. Just bail.
1153 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1154 return;
1155 }
1156
1157 // shallow copy
1158 final ArrayList<ApplicationInfo> list
1159 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1160 mHandler.post(new Runnable() {
1161 public void run() {
1162 final long t = SystemClock.uptimeMillis();
1163 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1164 if (callbacks != null) {
1165 callbacks.bindAllApplications(list);
1166 }
1167 if (DEBUG_LOADERS) {
1168 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1169 + (SystemClock.uptimeMillis()-t) + "ms");
1170 }
1171 }
1172 });
1173
1174 }
1175
1176 private void loadAllAppsByBatch() {
1177 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1178
1179 // Don't use these two variables in any of the callback runnables.
1180 // Otherwise we hold a reference to them.
1181 final Callbacks oldCallbacks = mCallbacks.get();
1182 if (oldCallbacks == null) {
1183 // This launcher has exited and nobody bothered to tell us. Just bail.
1184 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1185 return;
1186 }
1187
1188 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1189 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1190
1191 final PackageManager packageManager = mContext.getPackageManager();
1192 List<ResolveInfo> apps = null;
1193
1194 int N = Integer.MAX_VALUE;
1195
1196 int startIndex;
1197 int i=0;
1198 int batchSize = -1;
1199 while (i < N && !mStopped) {
1200 if (i == 0) {
1201 mAllAppsList.clear();
1202 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1203 apps = packageManager.queryIntentActivities(mainIntent, 0);
1204 if (DEBUG_LOADERS) {
1205 Log.d(TAG, "queryIntentActivities took "
1206 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1207 }
1208 if (apps == null) {
1209 return;
1210 }
1211 N = apps.size();
1212 if (DEBUG_LOADERS) {
1213 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1214 }
1215 if (N == 0) {
1216 // There are no apps?!?
1217 return;
1218 }
1219 if (mBatchSize == 0) {
1220 batchSize = N;
1221 } else {
1222 batchSize = mBatchSize;
1223 }
1224
1225 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1226 Collections.sort(apps,
1227 new ResolveInfo.DisplayNameComparator(packageManager));
1228 if (DEBUG_LOADERS) {
1229 Log.d(TAG, "sort took "
1230 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1231 }
1232 }
1233
1234 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1235
1236 startIndex = i;
1237 for (int j=0; i<N && j<batchSize; j++) {
1238 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001239 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001240 i++;
1241 }
1242
1243 final boolean first = i <= batchSize;
1244 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1245 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1246 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1247
Joe Onoratocc67f472010-06-08 10:54:30 -07001248 mHandler.post(new Runnable() {
1249 public void run() {
1250 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001251 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001252 if (first) {
1253 callbacks.bindAllApplications(added);
1254 } else {
1255 callbacks.bindAppsAdded(added);
1256 }
1257 if (DEBUG_LOADERS) {
1258 Log.d(TAG, "bound " + added.size() + " apps in "
1259 + (SystemClock.uptimeMillis() - t) + "ms");
1260 }
1261 } else {
1262 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001263 }
1264 }
1265 });
1266
Daniel Sandlerdca66122010-04-13 16:23:58 -04001267 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001268 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1269 + (SystemClock.uptimeMillis()-t2) + "ms");
1270 }
1271
1272 if (mAllAppsLoadDelay > 0 && i < N) {
1273 try {
1274 if (DEBUG_LOADERS) {
1275 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1276 }
1277 Thread.sleep(mAllAppsLoadDelay);
1278 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 }
1280 }
1281
Joe Onorato36115782010-06-17 13:28:48 -04001282 if (DEBUG_LOADERS) {
1283 Log.d(TAG, "cached all " + N + " apps in "
1284 + (SystemClock.uptimeMillis()-t) + "ms"
1285 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001286 }
1287 }
1288
1289 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001290 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1291 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1292 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1293 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1294 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen71a95462011-03-22 13:54:11 -07001295 Log.d(TAG, "mItems size=" + mItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001296 }
1297 }
1298
1299 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001300 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001301 }
1302
1303 private class PackageUpdatedTask implements Runnable {
1304 int mOp;
1305 String[] mPackages;
1306
1307 public static final int OP_NONE = 0;
1308 public static final int OP_ADD = 1;
1309 public static final int OP_UPDATE = 2;
1310 public static final int OP_REMOVE = 3; // uninstlled
1311 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1312
1313
1314 public PackageUpdatedTask(int op, String[] packages) {
1315 mOp = op;
1316 mPackages = packages;
1317 }
1318
1319 public void run() {
1320 final Context context = mApp;
1321
1322 final String[] packages = mPackages;
1323 final int N = packages.length;
1324 switch (mOp) {
1325 case OP_ADD:
1326 for (int i=0; i<N; i++) {
1327 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1328 mAllAppsList.addPackage(context, packages[i]);
1329 }
1330 break;
1331 case OP_UPDATE:
1332 for (int i=0; i<N; i++) {
1333 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1334 mAllAppsList.updatePackage(context, packages[i]);
1335 }
1336 break;
1337 case OP_REMOVE:
1338 case OP_UNAVAILABLE:
1339 for (int i=0; i<N; i++) {
1340 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1341 mAllAppsList.removePackage(packages[i]);
1342 }
1343 break;
1344 }
1345
1346 ArrayList<ApplicationInfo> added = null;
1347 ArrayList<ApplicationInfo> removed = null;
1348 ArrayList<ApplicationInfo> modified = null;
1349
1350 if (mAllAppsList.added.size() > 0) {
1351 added = mAllAppsList.added;
1352 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1353 }
1354 if (mAllAppsList.removed.size() > 0) {
1355 removed = mAllAppsList.removed;
1356 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1357 for (ApplicationInfo info: removed) {
1358 mIconCache.remove(info.intent.getComponent());
1359 }
1360 }
1361 if (mAllAppsList.modified.size() > 0) {
1362 modified = mAllAppsList.modified;
1363 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1364 }
1365
1366 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1367 if (callbacks == null) {
1368 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1369 return;
1370 }
1371
1372 if (added != null) {
1373 final ArrayList<ApplicationInfo> addedFinal = added;
1374 mHandler.post(new Runnable() {
1375 public void run() {
1376 if (callbacks == mCallbacks.get()) {
1377 callbacks.bindAppsAdded(addedFinal);
1378 }
1379 }
1380 });
1381 }
1382 if (modified != null) {
1383 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1384 mHandler.post(new Runnable() {
1385 public void run() {
1386 if (callbacks == mCallbacks.get()) {
1387 callbacks.bindAppsUpdated(modifiedFinal);
1388 }
1389 }
1390 });
1391 }
1392 if (removed != null) {
1393 final boolean permanent = mOp != OP_UNAVAILABLE;
1394 final ArrayList<ApplicationInfo> removedFinal = removed;
1395 mHandler.post(new Runnable() {
1396 public void run() {
1397 if (callbacks == mCallbacks.get()) {
1398 callbacks.bindAppsRemoved(removedFinal, permanent);
1399 }
1400 }
1401 });
Joe Onoratobe386092009-11-17 17:32:16 -08001402 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001403
1404 mHandler.post(new Runnable() {
1405 @Override
1406 public void run() {
1407 if (callbacks == mCallbacks.get()) {
1408 callbacks.bindPackagesUpdated();
1409 }
1410 }
1411 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412 }
1413 }
1414
1415 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001416 * This is called from the code that adds shortcuts from the intent receiver. This
1417 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001418 */
Joe Onorato56d82912010-03-07 14:32:10 -05001419 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001420 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422
Joe Onorato56d82912010-03-07 14:32:10 -05001423 /**
1424 * Make an ShortcutInfo object for a shortcut that is an application.
1425 *
1426 * If c is not null, then it will be used to fill in missing data like the title and icon.
1427 */
1428 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1429 Cursor c, int iconIndex, int titleIndex) {
1430 Bitmap icon = null;
1431 final ShortcutInfo info = new ShortcutInfo();
1432
1433 ComponentName componentName = intent.getComponent();
1434 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001435 return null;
1436 }
1437
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001438 // TODO: See if the PackageManager knows about this case. If it doesn't
1439 // then return null & delete this.
1440
Joe Onorato56d82912010-03-07 14:32:10 -05001441 // the resource -- This may implicitly give us back the fallback icon,
1442 // but don't worry about that. All we're doing with usingFallbackIcon is
1443 // to avoid saving lots of copies of that in the database, and most apps
1444 // have icons anyway.
1445 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1446 if (resolveInfo != null) {
1447 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001448 }
Joe Onorato56d82912010-03-07 14:32:10 -05001449 // the db
1450 if (icon == null) {
1451 if (c != null) {
1452 icon = getIconFromCursor(c, iconIndex);
1453 }
1454 }
1455 // the fallback icon
1456 if (icon == null) {
1457 icon = getFallbackIcon();
1458 info.usingFallbackIcon = true;
1459 }
1460 info.setIcon(icon);
1461
1462 // from the resource
1463 if (resolveInfo != null) {
1464 info.title = resolveInfo.activityInfo.loadLabel(manager);
1465 }
1466 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001468 if (c != null) {
1469 info.title = c.getString(titleIndex);
1470 }
1471 }
1472 // fall back to the class name of the activity
1473 if (info.title == null) {
1474 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1477 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001481 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001483 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001484 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1485 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
Joe Onorato56d82912010-03-07 14:32:10 -05001487 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001488 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001491 // TODO: If there's an explicit component and we can't install that, delete it.
1492
Joe Onorato56d82912010-03-07 14:32:10 -05001493 info.title = c.getString(titleIndex);
1494
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 int iconType = c.getInt(iconTypeIndex);
1496 switch (iconType) {
1497 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1498 String packageName = c.getString(iconPackageIndex);
1499 String resourceName = c.getString(iconResourceIndex);
1500 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001501 info.customIcon = false;
1502 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001505 if (resources != null) {
1506 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001507 icon = Utilities.createIconBitmap(
1508 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001509 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001511 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
Joe Onorato56d82912010-03-07 14:32:10 -05001513 // the db
1514 if (icon == null) {
1515 icon = getIconFromCursor(c, iconIndex);
1516 }
1517 // the fallback icon
1518 if (icon == null) {
1519 icon = getFallbackIcon();
1520 info.usingFallbackIcon = true;
1521 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001522 break;
1523 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001524 icon = getIconFromCursor(c, iconIndex);
1525 if (icon == null) {
1526 icon = getFallbackIcon();
1527 info.customIcon = false;
1528 info.usingFallbackIcon = true;
1529 } else {
1530 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001532 break;
1533 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001534 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001535 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001536 info.customIcon = false;
1537 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001539 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001540 return info;
1541 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
Joe Onorato56d82912010-03-07 14:32:10 -05001543 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1544 if (false) {
1545 Log.d(TAG, "getIconFromCursor app="
1546 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1547 }
1548 byte[] data = c.getBlob(iconIndex);
1549 try {
1550 return BitmapFactory.decodeByteArray(data, 0, data.length);
1551 } catch (Exception e) {
1552 return null;
1553 }
1554 }
1555
Joe Onorato0589f0f2010-02-08 13:44:00 -08001556 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001558
Winson Chunga9abd0e2010-10-27 17:18:37 -07001559 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001560 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001562
1563 return info;
1564 }
1565
Winson Chunga9abd0e2010-10-27 17:18:37 -07001566 /**
Winson Chung55cef262010-10-28 14:14:18 -07001567 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1568 */
1569 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1570 ComponentName component) {
1571 List<AppWidgetProviderInfo> widgets =
1572 AppWidgetManager.getInstance(context).getInstalledProviders();
1573 for (AppWidgetProviderInfo info : widgets) {
1574 if (info.provider.equals(component)) {
1575 return info;
1576 }
1577 }
1578 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001579 }
1580
Winson Chung68846fd2010-10-29 11:00:27 -07001581 /**
1582 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1583 */
1584 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1585 final PackageManager packageManager = context.getPackageManager();
1586 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1587 new ArrayList<WidgetMimeTypeHandlerData>();
1588
1589 final Intent supportsIntent =
1590 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1591 supportsIntent.setType(mimeType);
1592
1593 // Create a set of widget configuration components that we can test against
1594 final List<AppWidgetProviderInfo> widgets =
1595 AppWidgetManager.getInstance(context).getInstalledProviders();
1596 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1597 new HashMap<ComponentName, AppWidgetProviderInfo>();
1598 for (AppWidgetProviderInfo info : widgets) {
1599 configurationComponentToWidget.put(info.configure, info);
1600 }
1601
1602 // Run through each of the intents that can handle this type of clip data, and cross
1603 // reference them with the components that are actual configuration components
1604 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1605 PackageManager.MATCH_DEFAULT_ONLY);
1606 for (ResolveInfo info : activities) {
1607 final ActivityInfo activityInfo = info.activityInfo;
1608 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1609 activityInfo.name);
1610 if (configurationComponentToWidget.containsKey(infoComponent)) {
1611 supportedConfigurationActivities.add(
1612 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1613 configurationComponentToWidget.get(infoComponent)));
1614 }
1615 }
1616 return supportedConfigurationActivities;
1617 }
1618
Winson Chunga9abd0e2010-10-27 17:18:37 -07001619 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001620 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1621 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1622 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1623
1624 Bitmap icon = null;
1625 boolean filtered = false;
1626 boolean customIcon = false;
1627 ShortcutIconResource iconResource = null;
1628
1629 if (bitmap != null && bitmap instanceof Bitmap) {
1630 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1631 filtered = true;
1632 customIcon = true;
1633 } else {
1634 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1635 if (extra != null && extra instanceof ShortcutIconResource) {
1636 try {
1637 iconResource = (ShortcutIconResource) extra;
1638 final PackageManager packageManager = context.getPackageManager();
1639 Resources resources = packageManager.getResourcesForApplication(
1640 iconResource.packageName);
1641 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001642 icon = Utilities.createIconBitmap(
1643 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001644 } catch (Exception e) {
1645 Log.w(TAG, "Could not load shortcut icon: " + extra);
1646 }
1647 }
1648 }
1649
Joe Onorato0589f0f2010-02-08 13:44:00 -08001650 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001651
1652 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001653 if (fallbackIcon != null) {
1654 icon = fallbackIcon;
1655 } else {
1656 icon = getFallbackIcon();
1657 info.usingFallbackIcon = true;
1658 }
Joe Onorato56d82912010-03-07 14:32:10 -05001659 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001660 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001661
Joe Onorato0589f0f2010-02-08 13:44:00 -08001662 info.title = name;
1663 info.intent = intent;
1664 info.customIcon = customIcon;
1665 info.iconResource = iconResource;
1666
1667 return info;
1668 }
1669
Michael Jurkac9a96192010-11-01 11:52:08 -07001670 private void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001671 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1672
1673 int iconType = c.getInt(iconTypeIndex);
1674 switch (iconType) {
1675 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1676 String packageName = c.getString(iconPackageIndex);
1677 String resourceName = c.getString(iconResourceIndex);
1678 PackageManager packageManager = context.getPackageManager();
1679 try {
Michael Jurkac9a96192010-11-01 11:52:08 -07001680 Resources appResources = packageManager.getResourcesForApplication(packageName);
1681 final int id = appResources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001682 liveFolderInfo.icon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -07001683 mIconCache.getFullResIcon(appResources, id), context);
1684 } catch (Exception e) {
1685 Resources resources = context.getResources();
1686 liveFolderInfo.icon = Utilities.createIconBitmap(
1687 mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001688 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001689 }
1690 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1691 liveFolderInfo.iconResource.packageName = packageName;
1692 liveFolderInfo.iconResource.resourceName = resourceName;
1693 break;
1694 default:
Michael Jurkac9a96192010-11-01 11:52:08 -07001695 Resources resources = context.getResources();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001696 liveFolderInfo.icon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -07001697 mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001698 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001699 }
1700 }
1701
Joe Onorato56d82912010-03-07 14:32:10 -05001702 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001703 // If apps can't be on SD, don't even bother.
1704 if (!mAppsCanBeOnExternalStorage) {
1705 return;
1706 }
Joe Onorato56d82912010-03-07 14:32:10 -05001707 // If this icon doesn't have a custom icon, check to see
1708 // what's stored in the DB, and if it doesn't match what
1709 // we're going to show, store what we are going to show back
1710 // into the DB. We do this so when we're loading, if the
1711 // package manager can't find an icon (for example because
1712 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001713 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001714 boolean needSave;
1715 byte[] data = c.getBlob(iconIndex);
1716 try {
1717 if (data != null) {
1718 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1719 Bitmap loaded = info.getIcon(mIconCache);
1720 needSave = !saved.sameAs(loaded);
1721 } else {
1722 needSave = true;
1723 }
1724 } catch (Exception e) {
1725 needSave = true;
1726 }
1727 if (needSave) {
1728 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001729 // This is slower than is ideal, but this only happens once
1730 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001731 updateItemInDatabase(context, info);
1732 }
1733 }
1734 }
1735
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 /**
1737 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1738 * or make a new one.
1739 */
1740 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1741 // See if a placeholder was created for us already
1742 FolderInfo folderInfo = folders.get(id);
1743 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1744 // No placeholder -- create a new instance
1745 folderInfo = new UserFolderInfo();
1746 folders.put(id, folderInfo);
1747 }
1748 return (UserFolderInfo) folderInfo;
1749 }
1750
1751 /**
1752 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1753 * new one.
1754 */
1755 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1756 // See if a placeholder was created for us already
1757 FolderInfo folderInfo = folders.get(id);
1758 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1759 // No placeholder -- create a new instance
1760 folderInfo = new LiveFolderInfo();
1761 folders.put(id, folderInfo);
1762 }
1763 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1767 String label = activityInfo.loadLabel(manager).toString();
1768 if (label == null) {
1769 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1770 if (label == null) {
1771 label = activityInfo.name;
1772 }
1773 }
1774 return label;
1775 }
1776
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001778 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001779 = new Comparator<ApplicationInfo>() {
1780 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1781 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 };
Winson Chung78403fe2011-01-21 15:38:02 -08001784 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1785 = new Comparator<ApplicationInfo>() {
1786 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1787 if (a.firstInstallTime < b.firstInstallTime) return 1;
1788 if (a.firstInstallTime > b.firstInstallTime) return -1;
1789 return 0;
1790 }
1791 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001792 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1793 = new Comparator<AppWidgetProviderInfo>() {
1794 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1795 return sCollator.compare(a.label.toString(), b.label.toString());
1796 }
1797 };
1798 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1799 private PackageManager mPackageManager;
1800 private HashMap<Object, String> mLabelCache;
1801 ShortcutNameComparator(PackageManager pm) {
1802 mPackageManager = pm;
1803 mLabelCache = new HashMap<Object, String>();
1804 }
1805 public final int compare(ResolveInfo a, ResolveInfo b) {
1806 String labelA, labelB;
1807 if (mLabelCache.containsKey(a)) labelA = mLabelCache.get(a);
1808 else labelA = a.loadLabel(mPackageManager).toString();
1809 if (mLabelCache.containsKey(b)) labelB = mLabelCache.get(b);
1810 else labelB = b.loadLabel(mPackageManager).toString();
1811 return sCollator.compare(labelA, labelB);
1812 }
1813 };
Joe Onoratobe386092009-11-17 17:32:16 -08001814
1815 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001816 Log.d(TAG, "mCallbacks=" + mCallbacks);
1817 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1818 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1819 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1820 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001821 if (mLoaderTask != null) {
1822 mLoaderTask.dumpState();
1823 } else {
1824 Log.d(TAG, "mLoaderTask=null");
1825 }
Joe Onoratobe386092009-11-17 17:32:16 -08001826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827}