blob: a19eb4c3b444969c52da4ff4d1d22a32c9a07620 [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
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetManager;
20import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040021import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080023import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
29import android.content.pm.ActivityInfo;
30import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.net.Uri;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
Romain Guy5c16f3e2010-01-12 17:24:58 -080039import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import java.lang.ref.WeakReference;
45import java.net.URISyntaxException;
46import java.text.Collator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import java.util.ArrayList;
Joe Onorato56d82912010-03-07 14:32:10 -050048import java.util.Arrays;
Joe Onorato9c1289c2009-08-17 11:03:03 -040049import java.util.Comparator;
50import java.util.Collections;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import java.util.HashMap;
52import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Romain Guyedcce092010-03-04 13:03:17 -080054import com.android.launcher.R;
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056/**
57 * Maintains in-memory state of the Launcher. It is expected that there should be only one
58 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070059 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040061public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080062 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040063 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070064
Joe Onoratod65d08e2010-04-20 15:43:37 -040065 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040066 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040067
Joe Onoratof99f8c12009-10-31 17:27:36 -040068 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040069 private final Object mLock = new Object();
70 private DeferredHandler mHandler = new DeferredHandler();
71 private Loader mLoader = new Loader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Joe Onoratod65d08e2010-04-20 15:43:37 -040073 private boolean mBeforeFirstLoad = true; // only access this from main thread
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
Joe Onorato0589f0f2010-02-08 13:44:00 -080076 private AllAppsList mAllAppsList;
77 private IconCache mIconCache;
78
79 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 public interface Callbacks {
82 public int getCurrentWorkspaceScreen();
83 public void startBinding();
84 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -050085 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -040086 public void finishBindingItems();
87 public void bindAppWidget(LauncherAppWidgetInfo info);
88 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -050089 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
90 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
91 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093
Joe Onorato0589f0f2010-02-08 13:44:00 -080094 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040095 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -080096 mAllAppsList = new AllAppsList(iconCache);
97 mIconCache = iconCache;
98
99 mDefaultIcon = Utilities.createIconBitmap(
100 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400101
102 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400103
104 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800105 }
106
Joe Onorato56d82912010-03-07 14:32:10 -0500107 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800108 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 /**
112 * Adds an item to the DB if it was not created previously, or move it to a new
113 * <container, screen, cellX, cellY>
114 */
115 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
116 int screen, int cellX, int cellY) {
117 if (item.container == ItemInfo.NO_ID) {
118 // From all apps
119 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
120 } else {
121 // From somewhere else
122 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 }
124 }
125
126 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700128 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400129 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
130 int cellX, int cellY) {
131 item.container = container;
132 item.screen = screen;
133 item.cellX = cellX;
134 item.cellY = cellY;
135
136 final ContentValues values = new ContentValues();
137 final ContentResolver cr = context.getContentResolver();
138
139 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
140 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
141 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
142 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
143
144 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700145 }
146
147 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 * Returns true if the shortcuts already exists in the database.
149 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 static boolean shortcutExists(Context context, String title, Intent intent) {
152 final ContentResolver cr = context.getContentResolver();
153 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
154 new String[] { "title", "intent" }, "title=? and intent=?",
155 new String[] { title, intent.toUri(0) }, null);
156 boolean result = false;
157 try {
158 result = c.moveToFirst();
159 } finally {
160 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700163 }
164
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 /**
166 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
167 */
168 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
169 final ContentResolver cr = context.getContentResolver();
170 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
171 "_id=? and (itemType=? or itemType=?)",
172 new String[] { String.valueOf(id),
173 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
174 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700175
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 try {
177 if (c.moveToFirst()) {
178 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
179 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
180 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
181 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
182 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
183 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 FolderInfo folderInfo = null;
186 switch (c.getInt(itemTypeIndex)) {
187 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
188 folderInfo = findOrMakeUserFolder(folderList, id);
189 break;
190 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
191 folderInfo = findOrMakeLiveFolder(folderList, id);
192 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700193 }
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 folderInfo.title = c.getString(titleIndex);
196 folderInfo.id = id;
197 folderInfo.container = c.getInt(containerIndex);
198 folderInfo.screen = c.getInt(screenIndex);
199 folderInfo.cellX = c.getInt(cellXIndex);
200 folderInfo.cellY = c.getInt(cellYIndex);
201
202 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 } finally {
205 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700206 }
207
208 return null;
209 }
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 /**
212 * Add an item to the database in a specified container. Sets the container, screen, cellX and
213 * cellY fields of the item. Also assigns an ID to the item.
214 */
215 static void addItemToDatabase(Context context, ItemInfo item, long container,
216 int screen, int cellX, int cellY, boolean notify) {
217 item.container = container;
218 item.screen = screen;
219 item.cellX = cellX;
220 item.cellY = cellY;
221
222 final ContentValues values = new ContentValues();
223 final ContentResolver cr = context.getContentResolver();
224
225 item.onAddToDatabase(values);
226
227 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
228 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
229
230 if (result != null) {
231 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700232 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700233 }
234
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 /**
236 * Update an item to the database in a specified container.
237 */
238 static void updateItemInDatabase(Context context, ItemInfo item) {
239 final ContentValues values = new ContentValues();
240 final ContentResolver cr = context.getContentResolver();
241
242 item.onAddToDatabase(values);
243
244 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
245 }
246
247 /**
248 * Removes the specified item from the database
249 * @param context
250 * @param item
251 */
252 static void deleteItemFromDatabase(Context context, ItemInfo item) {
253 final ContentResolver cr = context.getContentResolver();
254
255 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
256 }
257
258 /**
259 * Remove the contents of the specified folder from the database
260 */
261 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
262 final ContentResolver cr = context.getContentResolver();
263
264 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
265 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
266 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
267 }
268
269 /**
270 * Set this as the current Launcher activity object for the loader.
271 */
272 public void initialize(Callbacks callbacks) {
273 synchronized (mLock) {
274 mCallbacks = new WeakReference<Callbacks>(callbacks);
275 }
276 }
277
278 public void startLoader(Context context, boolean isLaunching) {
279 mLoader.startLoader(context, isLaunching);
280 }
281
282 public void stopLoader() {
283 mLoader.stopLoader();
284 }
285
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700286 /**
287 * We pick up most of the changes to all apps.
288 */
289 public void setAllAppsDirty() {
290 mLoader.setAllAppsDirty();
291 }
292
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 public void setWorkspaceDirty() {
294 mLoader.setWorkspaceDirty();
295 }
296
297 /**
298 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
299 * ACTION_PACKAGE_CHANGED.
300 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400301 public void onReceive(Context context, Intent intent) {
302 // Use the app as the context.
303 context = mApp;
304
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 ArrayList<ApplicationInfo> added = null;
306 ArrayList<ApplicationInfo> removed = null;
307 ArrayList<ApplicationInfo> modified = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308
309 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400310 if (mBeforeFirstLoad) {
311 // If we haven't even loaded yet, don't bother, since we'll just pick
312 // up the changes.
313 return;
314 }
315
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316 final String action = intent.getAction();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400317
Joe Onorato64e6be72010-03-05 15:05:52 -0500318 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
319 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
320 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
321 final String packageName = intent.getData().getSchemeSpecificPart();
322 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400323
Joe Onorato64e6be72010-03-05 15:05:52 -0500324 if (packageName == null || packageName.length() == 0) {
325 // they sent us a bad intent
326 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400327 }
Joe Onorato64e6be72010-03-05 15:05:52 -0500328
329 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400330 mAllAppsList.updatePackage(context, packageName);
Joe Onorato64e6be72010-03-05 15:05:52 -0500331 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
332 if (!replacing) {
333 mAllAppsList.removePackage(packageName);
334 }
335 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
336 // later, we will update the package at this time
337 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
338 if (!replacing) {
339 mAllAppsList.addPackage(context, packageName);
340 } else {
341 mAllAppsList.updatePackage(context, packageName);
342 }
343 }
Joe Onorato56d82912010-03-07 14:32:10 -0500344
345 if (mAllAppsList.added.size() > 0) {
346 added = mAllAppsList.added;
347 mAllAppsList.added = new ArrayList<ApplicationInfo>();
348 }
349 if (mAllAppsList.removed.size() > 0) {
350 removed = mAllAppsList.removed;
351 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
352 for (ApplicationInfo info: removed) {
353 mIconCache.remove(info.intent.getComponent());
354 }
355 }
356 if (mAllAppsList.modified.size() > 0) {
357 modified = mAllAppsList.modified;
358 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
359 }
360
361 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
362 if (callbacks == null) {
363 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
364 return;
365 }
366
367 if (added != null) {
368 final ArrayList<ApplicationInfo> addedFinal = added;
369 mHandler.post(new Runnable() {
370 public void run() {
371 callbacks.bindAppsAdded(addedFinal);
372 }
373 });
374 }
375 if (modified != null) {
376 final ArrayList<ApplicationInfo> modifiedFinal = modified;
377 mHandler.post(new Runnable() {
378 public void run() {
379 callbacks.bindAppsUpdated(modifiedFinal);
380 }
381 });
382 }
383 if (removed != null) {
384 final ArrayList<ApplicationInfo> removedFinal = removed;
385 mHandler.post(new Runnable() {
386 public void run() {
387 callbacks.bindAppsRemoved(removedFinal);
388 }
389 });
390 }
Joe Onorato64e6be72010-03-05 15:05:52 -0500391 } else {
392 if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
393 String packages[] = intent.getStringArrayExtra(
394 Intent.EXTRA_CHANGED_PACKAGE_LIST);
395 if (packages == null || packages.length == 0) {
396 return;
397 }
Joe Onorato56d82912010-03-07 14:32:10 -0500398 setAllAppsDirty();
399 setWorkspaceDirty();
400 startLoader(context, false);
Joe Onorato64e6be72010-03-05 15:05:52 -0500401 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
402 String packages[] = intent.getStringArrayExtra(
403 Intent.EXTRA_CHANGED_PACKAGE_LIST);
404 if (packages == null || packages.length == 0) {
405 return;
406 }
Joe Onorato56d82912010-03-07 14:32:10 -0500407 setAllAppsDirty();
408 setWorkspaceDirty();
409 startLoader(context, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400410 }
411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400412 }
413 }
414
415 public class Loader {
416 private static final int ITEMS_CHUNK = 6;
417
418 private LoaderThread mLoaderThread;
419
420 private int mLastWorkspaceSeq = 0;
421 private int mWorkspaceSeq = 1;
422
423 private int mLastAllAppsSeq = 0;
424 private int mAllAppsSeq = 1;
425
Romain Guy84f296c2009-11-04 15:00:44 -0800426 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
427 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
Joe Onoratoad72e172009-11-06 16:25:04 -0500428 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429
430 /**
431 * Call this from the ui thread so the handler is initialized on the correct thread.
432 */
433 public Loader() {
434 }
435
436 public void startLoader(Context context, boolean isLaunching) {
437 synchronized (mLock) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800438 if (DEBUG_LOADERS) {
439 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 // Don't bother to start the thread if we know it's not going to do anything
Joe Onoratoac033302010-04-13 17:19:18 -0700442 if (mCallbacks != null && mCallbacks.get() != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 LoaderThread oldThread = mLoaderThread;
444 if (oldThread != null) {
445 if (oldThread.isLaunching()) {
446 // don't downgrade isLaunching if we're already running
447 isLaunching = true;
448 }
449 oldThread.stopLocked();
450 }
451 mLoaderThread = new LoaderThread(context, oldThread, isLaunching);
452 mLoaderThread.start();
453 }
454 }
455 }
456
457 public void stopLoader() {
458 synchronized (mLock) {
459 if (mLoaderThread != null) {
460 mLoaderThread.stopLocked();
461 }
462 }
463 }
464
465 public void setWorkspaceDirty() {
466 synchronized (mLock) {
467 mWorkspaceSeq++;
468 }
469 }
470
471 public void setAllAppsDirty() {
472 synchronized (mLock) {
473 mAllAppsSeq++;
474 }
475 }
476
477 /**
478 * Runnable for the thread that loads the contents of the launcher:
479 * - workspace icons
480 * - widgets
481 * - all apps icons
482 */
483 private class LoaderThread extends Thread {
484 private Context mContext;
485 private Thread mWaitThread;
486 private boolean mIsLaunching;
487 private boolean mStopped;
488 private boolean mWorkspaceDoneBinding;
489
490 LoaderThread(Context context, Thread waitThread, boolean isLaunching) {
491 mContext = context;
492 mWaitThread = waitThread;
493 mIsLaunching = isLaunching;
494 }
495
496 boolean isLaunching() {
497 return mIsLaunching;
498 }
499
500 /**
501 * If another LoaderThread was supplied, we need to wait for that to finish before
502 * we start our processing. This keeps the ordering of the setting and clearing
503 * of the dirty flags correct by making sure we don't start processing stuff until
504 * they've had a chance to re-set them. We do this waiting the worker thread, not
505 * the ui thread to avoid ANRs.
506 */
507 private void waitForOtherThread() {
508 if (mWaitThread != null) {
509 boolean done = false;
510 while (!done) {
511 try {
512 mWaitThread.join();
Joe Onoratoefabe002009-08-28 09:38:18 -0700513 done = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800515 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 }
517 }
518 mWaitThread = null;
519 }
520 }
521
522 public void run() {
523 waitForOtherThread();
524
525 // Elevate priority when Home launches for the first time to avoid
526 // starving at boot time. Staring at a blank home is not cool.
527 synchronized (mLock) {
528 android.os.Process.setThreadPriority(mIsLaunching
529 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
530 }
531
532 // Load the workspace only if it's dirty.
533 int workspaceSeq;
534 boolean workspaceDirty;
535 synchronized (mLock) {
536 workspaceSeq = mWorkspaceSeq;
537 workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq;
538 }
539 if (workspaceDirty) {
540 loadWorkspace();
541 }
542 synchronized (mLock) {
543 // If we're not stopped, and nobody has incremented mWorkspaceSeq.
544 if (mStopped) {
545 return;
546 }
547 if (workspaceSeq == mWorkspaceSeq) {
548 mLastWorkspaceSeq = mWorkspaceSeq;
549 }
550 }
551
552 // Bind the workspace
553 bindWorkspace();
554
555 // Wait until the either we're stopped or the other threads are done.
556 // This way we don't start loading all apps until the workspace has settled
557 // down.
558 synchronized (LoaderThread.this) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500559 mHandler.postIdle(new Runnable() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 public void run() {
561 synchronized (LoaderThread.this) {
562 mWorkspaceDoneBinding = true;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800563 if (DEBUG_LOADERS) {
564 Log.d(TAG, "done with workspace");
565 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 LoaderThread.this.notify();
567 }
568 }
569 });
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800570 if (DEBUG_LOADERS) {
571 Log.d(TAG, "waiting to be done with workspace");
572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 while (!mStopped && !mWorkspaceDoneBinding) {
574 try {
575 this.wait();
576 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800577 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400578 }
579 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800580 if (DEBUG_LOADERS) {
581 Log.d(TAG, "done waiting to be done with workspace");
582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400583 }
584
Daniel Sandlerdca66122010-04-13 16:23:58 -0400585 // Whew! Hard work done.
586 synchronized (mLock) {
587 if (mIsLaunching) {
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400588 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Daniel Sandlerdca66122010-04-13 16:23:58 -0400589 }
590 }
591
Joe Onorato9c1289c2009-08-17 11:03:03 -0400592 // Load all apps if they're dirty
593 int allAppsSeq;
594 boolean allAppsDirty;
595 synchronized (mLock) {
596 allAppsSeq = mAllAppsSeq;
597 allAppsDirty = mAllAppsSeq != mLastAllAppsSeq;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800598 if (DEBUG_LOADERS) {
599 Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq
600 + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty");
601 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400602 }
603 if (allAppsDirty) {
Daniel Sandlerdca66122010-04-13 16:23:58 -0400604 loadAndBindAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400605 }
606 synchronized (mLock) {
607 // If we're not stopped, and nobody has incremented mAllAppsSeq.
608 if (mStopped) {
609 return;
610 }
611 if (allAppsSeq == mAllAppsSeq) {
612 mLastAllAppsSeq = mAllAppsSeq;
613 }
614 }
615
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 // Clear out this reference, otherwise we end up holding it until all of the
617 // callback runnables are done.
618 mContext = null;
619
620 synchronized (mLock) {
621 // Setting the reference is atomic, but we can't do it inside the other critical
622 // sections.
623 mLoaderThread = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624 }
625 }
626
627 public void stopLocked() {
628 synchronized (LoaderThread.this) {
629 mStopped = true;
630 this.notify();
631 }
632 }
633
634 /**
635 * Gets the callbacks object. If we've been stopped, or if the launcher object
Joe Onoratoc131b742010-03-11 15:45:05 -0800636 * has somehow been garbage collected, return null instead. Pass in the Callbacks
637 * object that was around when the deferred message was scheduled, and if there's
638 * a new Callbacks object around then also return null. This will save us from
639 * calling onto it with data that will be ignored.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 */
Joe Onoratoc131b742010-03-11 15:45:05 -0800641 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 synchronized (mLock) {
643 if (mStopped) {
644 return null;
645 }
646
Joe Onoratoac033302010-04-13 17:19:18 -0700647 if (mCallbacks == null) {
648 return null;
649 }
650
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 final Callbacks callbacks = mCallbacks.get();
Joe Onoratoc131b742010-03-11 15:45:05 -0800652 if (callbacks != oldCallbacks) {
653 return null;
654 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 if (callbacks == null) {
656 Log.w(TAG, "no mCallbacks");
657 return null;
658 }
659
660 return callbacks;
661 }
662 }
663
664 private void loadWorkspace() {
665 long t = SystemClock.uptimeMillis();
666
667 final Context context = mContext;
668 final ContentResolver contentResolver = context.getContentResolver();
669 final PackageManager manager = context.getPackageManager();
Romain Guy629de3e2010-01-13 12:20:59 -0800670 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800671 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400673 mItems.clear();
Joe Onorato511ab642009-11-08 14:14:07 -0500674 mAppWidgets.clear();
Joe Onorato1db7a972009-11-16 18:32:22 -0800675 mFolders.clear();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400676
Romain Guy5c16f3e2010-01-12 17:24:58 -0800677 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
678
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 final Cursor c = contentResolver.query(
680 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
681
682 try {
683 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
684 final int intentIndex = c.getColumnIndexOrThrow
685 (LauncherSettings.Favorites.INTENT);
686 final int titleIndex = c.getColumnIndexOrThrow
687 (LauncherSettings.Favorites.TITLE);
688 final int iconTypeIndex = c.getColumnIndexOrThrow(
689 LauncherSettings.Favorites.ICON_TYPE);
690 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
691 final int iconPackageIndex = c.getColumnIndexOrThrow(
692 LauncherSettings.Favorites.ICON_PACKAGE);
693 final int iconResourceIndex = c.getColumnIndexOrThrow(
694 LauncherSettings.Favorites.ICON_RESOURCE);
695 final int containerIndex = c.getColumnIndexOrThrow(
696 LauncherSettings.Favorites.CONTAINER);
697 final int itemTypeIndex = c.getColumnIndexOrThrow(
698 LauncherSettings.Favorites.ITEM_TYPE);
699 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
700 LauncherSettings.Favorites.APPWIDGET_ID);
701 final int screenIndex = c.getColumnIndexOrThrow(
702 LauncherSettings.Favorites.SCREEN);
703 final int cellXIndex = c.getColumnIndexOrThrow
704 (LauncherSettings.Favorites.CELLX);
705 final int cellYIndex = c.getColumnIndexOrThrow
706 (LauncherSettings.Favorites.CELLY);
707 final int spanXIndex = c.getColumnIndexOrThrow
708 (LauncherSettings.Favorites.SPANX);
709 final int spanYIndex = c.getColumnIndexOrThrow(
710 LauncherSettings.Favorites.SPANY);
711 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
712 final int displayModeIndex = c.getColumnIndexOrThrow(
713 LauncherSettings.Favorites.DISPLAY_MODE);
714
Joe Onorato0589f0f2010-02-08 13:44:00 -0800715 ShortcutInfo info;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 String intentDescription;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 LauncherAppWidgetInfo appWidgetInfo;
718 int container;
719 long id;
720 Intent intent;
721
722 while (!mStopped && c.moveToNext()) {
723 try {
724 int itemType = c.getInt(itemTypeIndex);
725
726 switch (itemType) {
727 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
728 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
729 intentDescription = c.getString(intentIndex);
730 try {
731 intent = Intent.parseUri(intentDescription, 0);
732 } catch (URISyntaxException e) {
733 continue;
734 }
735
736 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Joe Onorato56d82912010-03-07 14:32:10 -0500737 info = getShortcutInfo(manager, intent, context, c, iconIndex,
738 titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 } else {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800740 info = getShortcutInfo(c, context, iconTypeIndex,
Joe Onorato56d82912010-03-07 14:32:10 -0500741 iconPackageIndex, iconResourceIndex, iconIndex,
742 titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 }
744
745 if (info != null) {
Joe Onorato56d82912010-03-07 14:32:10 -0500746 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747
Joe Onorato56d82912010-03-07 14:32:10 -0500748 info.intent = intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 info.id = c.getLong(idIndex);
750 container = c.getInt(containerIndex);
751 info.container = container;
752 info.screen = c.getInt(screenIndex);
753 info.cellX = c.getInt(cellXIndex);
754 info.cellY = c.getInt(cellYIndex);
755
756 switch (container) {
757 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
758 mItems.add(info);
759 break;
760 default:
761 // Item is in a user folder
762 UserFolderInfo folderInfo =
Joe Onoratoad72e172009-11-06 16:25:04 -0500763 findOrMakeUserFolder(mFolders, container);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 folderInfo.add(info);
765 break;
766 }
Joe Onorato56d82912010-03-07 14:32:10 -0500767 } else {
768 // Failed to load the shortcut, probably because the
769 // activity manager couldn't resolve it (maybe the app
770 // was uninstalled), or the db row was somehow screwed up.
771 // Delete it.
772 id = c.getLong(idIndex);
773 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
774 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
775 id, false), null, null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 }
777 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778
Joe Onoratoad72e172009-11-06 16:25:04 -0500779 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 id = c.getLong(idIndex);
Joe Onoratoad72e172009-11-06 16:25:04 -0500781 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782
783 folderInfo.title = c.getString(titleIndex);
784
785 folderInfo.id = id;
786 container = c.getInt(containerIndex);
787 folderInfo.container = container;
788 folderInfo.screen = c.getInt(screenIndex);
789 folderInfo.cellX = c.getInt(cellXIndex);
790 folderInfo.cellY = c.getInt(cellYIndex);
791
792 switch (container) {
793 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
794 mItems.add(folderInfo);
795 break;
796 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500797
798 mFolders.put(folderInfo.id, folderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500800
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 id = c.getLong(idIndex);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800803 Uri uri = Uri.parse(c.getString(uriIndex));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804
Romain Guy5c16f3e2010-01-12 17:24:58 -0800805 // Make sure the live folder exists
806 final ProviderInfo providerInfo =
807 context.getPackageManager().resolveContentProvider(
808 uri.getAuthority(), 0);
809
810 if (providerInfo == null && !isSafeMode) {
811 itemsToRemove.add(id);
812 } else {
813 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
814
815 intentDescription = c.getString(intentIndex);
816 intent = null;
817 if (intentDescription != null) {
818 try {
819 intent = Intent.parseUri(intentDescription, 0);
820 } catch (URISyntaxException e) {
821 // Ignore, a live folder might not have a base intent
822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800824
825 liveFolderInfo.title = c.getString(titleIndex);
826 liveFolderInfo.id = id;
827 liveFolderInfo.uri = uri;
828 container = c.getInt(containerIndex);
829 liveFolderInfo.container = container;
830 liveFolderInfo.screen = c.getInt(screenIndex);
831 liveFolderInfo.cellX = c.getInt(cellXIndex);
832 liveFolderInfo.cellY = c.getInt(cellYIndex);
833 liveFolderInfo.baseIntent = intent;
834 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
835
836 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
837 iconResourceIndex, liveFolderInfo);
838
839 switch (container) {
840 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
841 mItems.add(liveFolderInfo);
842 break;
843 }
844 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500847
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
849 // Read all Launcher-specific widget details
850 int appWidgetId = c.getInt(appWidgetIdIndex);
Romain Guy629de3e2010-01-13 12:20:59 -0800851 id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852
Romain Guy629de3e2010-01-13 12:20:59 -0800853 final AppWidgetProviderInfo provider =
854 widgets.getAppWidgetInfo(appWidgetId);
855
856 if (!isSafeMode && (provider == null || provider.provider == null ||
857 provider.provider.getPackageName() == null)) {
Joe Onorato8ddc4fd2010-03-17 09:14:50 -0700858 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
859 + id + " appWidgetId=" + appWidgetId);
Romain Guy629de3e2010-01-13 12:20:59 -0800860 itemsToRemove.add(id);
861 } else {
862 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
863 appWidgetInfo.id = id;
864 appWidgetInfo.screen = c.getInt(screenIndex);
865 appWidgetInfo.cellX = c.getInt(cellXIndex);
866 appWidgetInfo.cellY = c.getInt(cellYIndex);
867 appWidgetInfo.spanX = c.getInt(spanXIndex);
868 appWidgetInfo.spanY = c.getInt(spanYIndex);
869
870 container = c.getInt(containerIndex);
871 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
872 Log.e(TAG, "Widget found where container "
873 + "!= CONTAINER_DESKTOP -- ignoring!");
874 continue;
875 }
876 appWidgetInfo.container = c.getInt(containerIndex);
877
878 mAppWidgets.add(appWidgetInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 break;
881 }
882 } catch (Exception e) {
883 Log.w(TAG, "Desktop items loading interrupted:", e);
884 }
885 }
886 } finally {
887 c.close();
888 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800889
890 if (itemsToRemove.size() > 0) {
891 ContentProviderClient client = contentResolver.acquireContentProviderClient(
892 LauncherSettings.Favorites.CONTENT_URI);
893 // Remove dead items
894 for (long id : itemsToRemove) {
895 if (DEBUG_LOADERS) {
896 Log.d(TAG, "Removed id = " + id);
897 }
898 // Don't notify content observers
899 try {
900 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
901 null, null);
902 } catch (RemoteException e) {
903 Log.w(TAG, "Could not remove id = " + id);
904 }
905 }
906 }
907
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800908 if (DEBUG_LOADERS) {
909 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911 }
912
913 /**
914 * Read everything out of our database.
915 */
916 private void bindWorkspace() {
917 final long t = SystemClock.uptimeMillis();
918
919 // Don't use these two variables in any of the callback runnables.
920 // Otherwise we hold a reference to them.
Joe Onoratoc131b742010-03-11 15:45:05 -0800921 final Callbacks oldCallbacks = mCallbacks.get();
922 if (oldCallbacks == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 // This launcher has exited and nobody bothered to tell us. Just bail.
924 Log.w(TAG, "LoaderThread running with no launcher");
925 return;
926 }
927
928 int N;
929 // Tell the workspace that we're about to start firing items at it
930 mHandler.post(new Runnable() {
931 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800932 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 if (callbacks != null) {
934 callbacks.startBinding();
935 }
936 }
937 });
938 // Add the items to the workspace.
939 N = mItems.size();
940 for (int i=0; i<N; i+=ITEMS_CHUNK) {
941 final int start = i;
942 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
943 mHandler.post(new Runnable() {
944 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800945 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 if (callbacks != null) {
947 callbacks.bindItems(mItems, start, start+chunkSize);
948 }
949 }
950 });
951 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500952 mHandler.post(new Runnable() {
953 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800954 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onoratoad72e172009-11-06 16:25:04 -0500955 if (callbacks != null) {
956 callbacks.bindFolders(mFolders);
957 }
958 }
959 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960 // Wait until the queue goes empty.
961 mHandler.postIdle(new Runnable() {
962 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800963 if (DEBUG_LOADERS) {
964 Log.d(TAG, "Going to start binding widgets soon.");
965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 }
967 });
968 // Bind the widgets, one at a time.
969 // WARNING: this is calling into the workspace from the background thread,
970 // but since getCurrentScreen() just returns the int, we should be okay. This
971 // is just a hint for the order, and if it's wrong, we'll be okay.
972 // TODO: instead, we should have that push the current screen into here.
Joe Onoratoc131b742010-03-11 15:45:05 -0800973 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 N = mAppWidgets.size();
975 // once for the current screen
976 for (int i=0; i<N; i++) {
977 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
978 if (widget.screen == currentScreen) {
979 mHandler.post(new Runnable() {
980 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800981 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 if (callbacks != null) {
983 callbacks.bindAppWidget(widget);
984 }
985 }
986 });
987 }
988 }
989 // once for the other screens
990 for (int i=0; i<N; i++) {
991 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
992 if (widget.screen != currentScreen) {
993 mHandler.post(new Runnable() {
994 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800995 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 if (callbacks != null) {
997 callbacks.bindAppWidget(widget);
998 }
999 }
1000 });
1001 }
1002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 // Tell the workspace that we're done.
1004 mHandler.post(new Runnable() {
1005 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001006 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 if (callbacks != null) {
1008 callbacks.finishBindingItems();
1009 }
1010 }
1011 });
1012 // If we're profiling, this is the last thing in the queue.
1013 mHandler.post(new Runnable() {
1014 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001015 if (DEBUG_LOADERS) {
1016 Log.d(TAG, "bound workspace in "
1017 + (SystemClock.uptimeMillis()-t) + "ms");
1018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 if (Launcher.PROFILE_ROTATE) {
1020 android.os.Debug.stopMethodTracing();
1021 }
1022 }
1023 });
1024 }
1025
Daniel Sandlerdca66122010-04-13 16:23:58 -04001026 private void loadAndBindAllApps() {
1027 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1028
Joe Onoratod65d08e2010-04-20 15:43:37 -04001029 // Don't use these two variables in any of the callback runnables.
1030 // Otherwise we hold a reference to them.
1031 final Callbacks oldCallbacks = mCallbacks.get();
1032 if (oldCallbacks == null) {
1033 // This launcher has exited and nobody bothered to tell us. Just bail.
1034 Log.w(TAG, "LoaderThread running with no launcher (loadAndBindAllApps)");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 return;
1036 }
1037
Joe Onoratod65d08e2010-04-20 15:43:37 -04001038 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1039 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1040
Joe Onorato0589f0f2010-02-08 13:44:00 -08001041 final PackageManager packageManager = mContext.getPackageManager();
Joe Onoratod65d08e2010-04-20 15:43:37 -04001042 List<ResolveInfo> apps = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043
Joe Onoratod65d08e2010-04-20 15:43:37 -04001044 int N = Integer.MAX_VALUE;
Daniel Sandlerdca66122010-04-13 16:23:58 -04001045
Joe Onoratod65d08e2010-04-20 15:43:37 -04001046 int startIndex;
Daniel Sandlerdca66122010-04-13 16:23:58 -04001047 int i=0;
Joe Onoratod65d08e2010-04-20 15:43:37 -04001048 int batchSize = -1;
Daniel Sandlerdca66122010-04-13 16:23:58 -04001049 while (i < N && !mStopped) {
1050 synchronized (mLock) {
Joe Onoratod65d08e2010-04-20 15:43:37 -04001051 if (i == 0) {
1052 // This needs to happen inside the same lock block as when we
1053 // prepare the first batch for bindAllApplications. Otherwise
1054 // the package changed receiver can come in and double-add
1055 // (or miss one?).
1056 mAllAppsList.clear();
1057 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1058 apps = packageManager.queryIntentActivities(mainIntent, 0);
1059 if (DEBUG_LOADERS) {
1060 Log.d(TAG, "queryIntentActivities took "
1061 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1062 }
1063 if (apps == null) {
1064 return;
1065 }
1066 N = apps.size();
1067 if (DEBUG_LOADERS) {
1068 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1069 }
1070 if (N == 0) {
1071 // There are no apps?!?
1072 return;
1073 }
1074 if (mBatchSize == 0) {
1075 batchSize = N;
1076 } else {
1077 batchSize = mBatchSize;
1078 }
1079
1080 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1081 Collections.sort(apps,
1082 new ResolveInfo.DisplayNameComparator(packageManager));
1083 if (DEBUG_LOADERS) {
1084 Log.d(TAG, "sort took "
1085 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1086 }
1087 }
1088
Daniel Sandlerdca66122010-04-13 16:23:58 -04001089 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1090
Joe Onoratod65d08e2010-04-20 15:43:37 -04001091 startIndex = i;
Daniel Sandlerdca66122010-04-13 16:23:58 -04001092 for (int j=0; i<N && j<batchSize; j++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 // This builds the icon bitmaps.
Joe Onorato0589f0f2010-02-08 13:44:00 -08001094 mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
Daniel Sandlerdca66122010-04-13 16:23:58 -04001095 i++;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 }
Joe Onoratod65d08e2010-04-20 15:43:37 -04001097
1098 final boolean first = i <= batchSize;
1099 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1100 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1101
1102 mHandler.post(new Runnable() {
1103 public void run() {
1104 final long t = SystemClock.uptimeMillis();
1105 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1106 if (first) {
1107 mBeforeFirstLoad = false;
1108 callbacks.bindAllApplications(added);
1109 } else {
1110 callbacks.bindAppsAdded(added);
1111 }
1112 if (DEBUG_LOADERS) {
1113 Log.d(TAG, "bound " + added.size() + " apps in "
1114 + (SystemClock.uptimeMillis() - t) + "ms");
1115 }
1116 }
1117 });
1118
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001119 if (DEBUG_LOADERS) {
Joe Onoratod65d08e2010-04-20 15:43:37 -04001120 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
Daniel Sandlerdca66122010-04-13 16:23:58 -04001121 + (SystemClock.uptimeMillis()-t2) + "ms");
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 }
Daniel Sandlerdca66122010-04-13 16:23:58 -04001124
Daniel Sandler2ff10b32010-04-16 15:06:06 -04001125 if (mAllAppsLoadDelay > 0 && i < N) {
Daniel Sandlerdca66122010-04-13 16:23:58 -04001126 try {
Joe Onoratod65d08e2010-04-20 15:43:37 -04001127 if (DEBUG_LOADERS) {
1128 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1129 }
Daniel Sandler2ff10b32010-04-16 15:06:06 -04001130 Thread.sleep(mAllAppsLoadDelay);
Daniel Sandlerdca66122010-04-13 16:23:58 -04001131 } catch (InterruptedException exc) { }
1132 }
1133 }
1134
1135 if (DEBUG_LOADERS) {
1136 Log.d(TAG, "cached all " + N + " apps in "
Daniel Sandler2ff10b32010-04-16 15:06:06 -04001137 + (SystemClock.uptimeMillis()-t) + "ms"
1138 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 }
1140 }
1141
Joe Onoratobe386092009-11-17 17:32:16 -08001142 public void dumpState() {
1143 Log.d(TAG, "mLoader.mLoaderThread.mContext=" + mContext);
1144 Log.d(TAG, "mLoader.mLoaderThread.mWaitThread=" + mWaitThread);
1145 Log.d(TAG, "mLoader.mLoaderThread.mIsLaunching=" + mIsLaunching);
1146 Log.d(TAG, "mLoader.mLoaderThread.mStopped=" + mStopped);
1147 Log.d(TAG, "mLoader.mLoaderThread.mWorkspaceDoneBinding=" + mWorkspaceDoneBinding);
1148 }
1149 }
1150
1151 public void dumpState() {
1152 Log.d(TAG, "mLoader.mLastWorkspaceSeq=" + mLoader.mLastWorkspaceSeq);
1153 Log.d(TAG, "mLoader.mWorkspaceSeq=" + mLoader.mWorkspaceSeq);
1154 Log.d(TAG, "mLoader.mLastAllAppsSeq=" + mLoader.mLastAllAppsSeq);
1155 Log.d(TAG, "mLoader.mAllAppsSeq=" + mLoader.mAllAppsSeq);
1156 Log.d(TAG, "mLoader.mItems size=" + mLoader.mItems.size());
1157 if (mLoaderThread != null) {
1158 mLoaderThread.dumpState();
1159 } else {
1160 Log.d(TAG, "mLoader.mLoaderThread=null");
1161 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 }
1163 }
1164
1165 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001166 * This is called from the code that adds shortcuts from the intent receiver. This
1167 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 */
Joe Onorato56d82912010-03-07 14:32:10 -05001169 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001170 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172
Joe Onorato56d82912010-03-07 14:32:10 -05001173 /**
1174 * Make an ShortcutInfo object for a shortcut that is an application.
1175 *
1176 * If c is not null, then it will be used to fill in missing data like the title and icon.
1177 */
1178 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1179 Cursor c, int iconIndex, int titleIndex) {
1180 Bitmap icon = null;
1181 final ShortcutInfo info = new ShortcutInfo();
1182
1183 ComponentName componentName = intent.getComponent();
1184 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001185 return null;
1186 }
1187
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001188 // TODO: See if the PackageManager knows about this case. If it doesn't
1189 // then return null & delete this.
1190
Joe Onorato56d82912010-03-07 14:32:10 -05001191 // the resource -- This may implicitly give us back the fallback icon,
1192 // but don't worry about that. All we're doing with usingFallbackIcon is
1193 // to avoid saving lots of copies of that in the database, and most apps
1194 // have icons anyway.
1195 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1196 if (resolveInfo != null) {
1197 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001198 }
Joe Onorato56d82912010-03-07 14:32:10 -05001199 // the db
1200 if (icon == null) {
1201 if (c != null) {
1202 icon = getIconFromCursor(c, iconIndex);
1203 }
1204 }
1205 // the fallback icon
1206 if (icon == null) {
1207 icon = getFallbackIcon();
1208 info.usingFallbackIcon = true;
1209 }
1210 info.setIcon(icon);
1211
1212 // from the resource
1213 if (resolveInfo != null) {
1214 info.title = resolveInfo.activityInfo.loadLabel(manager);
1215 }
1216 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001218 if (c != null) {
1219 info.title = c.getString(titleIndex);
1220 }
1221 }
1222 // fall back to the class name of the activity
1223 if (info.title == null) {
1224 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001226 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1227 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001231 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001233 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001234 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1235 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236
Joe Onorato56d82912010-03-07 14:32:10 -05001237 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001238 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001241 // TODO: If there's an explicit component and we can't install that, delete it.
1242
Joe Onorato56d82912010-03-07 14:32:10 -05001243 info.title = c.getString(titleIndex);
1244
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 int iconType = c.getInt(iconTypeIndex);
1246 switch (iconType) {
1247 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1248 String packageName = c.getString(iconPackageIndex);
1249 String resourceName = c.getString(iconResourceIndex);
1250 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001251 info.customIcon = false;
1252 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001255 if (resources != null) {
1256 final int id = resources.getIdentifier(resourceName, null, null);
1257 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001259 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001260 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
Joe Onorato56d82912010-03-07 14:32:10 -05001262 // the db
1263 if (icon == null) {
1264 icon = getIconFromCursor(c, iconIndex);
1265 }
1266 // the fallback icon
1267 if (icon == null) {
1268 icon = getFallbackIcon();
1269 info.usingFallbackIcon = true;
1270 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 break;
1272 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001273 icon = getIconFromCursor(c, iconIndex);
1274 if (icon == null) {
1275 icon = getFallbackIcon();
1276 info.customIcon = false;
1277 info.usingFallbackIcon = true;
1278 } else {
1279 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281 break;
1282 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001283 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001284 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 info.customIcon = false;
1286 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001288 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 return info;
1290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291
Joe Onorato56d82912010-03-07 14:32:10 -05001292 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1293 if (false) {
1294 Log.d(TAG, "getIconFromCursor app="
1295 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1296 }
1297 byte[] data = c.getBlob(iconIndex);
1298 try {
1299 return BitmapFactory.decodeByteArray(data, 0, data.length);
1300 } catch (Exception e) {
1301 return null;
1302 }
1303 }
1304
Joe Onorato0589f0f2010-02-08 13:44:00 -08001305 ShortcutInfo addShortcut(Context context, Intent data,
1306 CellLayout.CellInfo cellInfo, boolean notify) {
1307
1308 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1309 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1310 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1311
1312 return info;
1313 }
1314
1315 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1316 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1317 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1318 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1319
1320 Bitmap icon = null;
1321 boolean filtered = false;
1322 boolean customIcon = false;
1323 ShortcutIconResource iconResource = null;
1324
1325 if (bitmap != null && bitmap instanceof Bitmap) {
1326 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1327 filtered = true;
1328 customIcon = true;
1329 } else {
1330 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1331 if (extra != null && extra instanceof ShortcutIconResource) {
1332 try {
1333 iconResource = (ShortcutIconResource) extra;
1334 final PackageManager packageManager = context.getPackageManager();
1335 Resources resources = packageManager.getResourcesForApplication(
1336 iconResource.packageName);
1337 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1338 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1339 } catch (Exception e) {
1340 Log.w(TAG, "Could not load shortcut icon: " + extra);
1341 }
1342 }
1343 }
1344
Joe Onorato0589f0f2010-02-08 13:44:00 -08001345 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001346
1347 if (icon == null) {
1348 icon = getFallbackIcon();
1349 info.usingFallbackIcon = true;
1350 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001351 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001352
Joe Onorato0589f0f2010-02-08 13:44:00 -08001353 info.title = name;
1354 info.intent = intent;
1355 info.customIcon = customIcon;
1356 info.iconResource = iconResource;
1357
1358 return info;
1359 }
1360
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1362 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1363
1364 int iconType = c.getInt(iconTypeIndex);
1365 switch (iconType) {
1366 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1367 String packageName = c.getString(iconPackageIndex);
1368 String resourceName = c.getString(iconResourceIndex);
1369 PackageManager packageManager = context.getPackageManager();
1370 try {
1371 Resources resources = packageManager.getResourcesForApplication(packageName);
1372 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001373 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1374 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001376 liveFolderInfo.icon = Utilities.createIconBitmap(
1377 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1378 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 }
1380 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1381 liveFolderInfo.iconResource.packageName = packageName;
1382 liveFolderInfo.iconResource.resourceName = resourceName;
1383 break;
1384 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001385 liveFolderInfo.icon = Utilities.createIconBitmap(
1386 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1387 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 }
1389 }
1390
Joe Onorato56d82912010-03-07 14:32:10 -05001391 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1392 // If this icon doesn't have a custom icon, check to see
1393 // what's stored in the DB, and if it doesn't match what
1394 // we're going to show, store what we are going to show back
1395 // into the DB. We do this so when we're loading, if the
1396 // package manager can't find an icon (for example because
1397 // the app is on SD) then we can use that instead.
1398 if (info.onExternalStorage && !info.customIcon && !info.usingFallbackIcon) {
1399 boolean needSave;
1400 byte[] data = c.getBlob(iconIndex);
1401 try {
1402 if (data != null) {
1403 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1404 Bitmap loaded = info.getIcon(mIconCache);
1405 needSave = !saved.sameAs(loaded);
1406 } else {
1407 needSave = true;
1408 }
1409 } catch (Exception e) {
1410 needSave = true;
1411 }
1412 if (needSave) {
1413 Log.d(TAG, "going to save icon bitmap for info=" + info);
1414 // This is slower than is ideal, but this only happens either
1415 // after the froyo OTA or when the app is updated with a new
1416 // icon.
1417 updateItemInDatabase(context, info);
1418 }
1419 }
1420 }
1421
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422 /**
1423 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1424 * or make a new one.
1425 */
1426 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1427 // See if a placeholder was created for us already
1428 FolderInfo folderInfo = folders.get(id);
1429 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1430 // No placeholder -- create a new instance
1431 folderInfo = new UserFolderInfo();
1432 folders.put(id, folderInfo);
1433 }
1434 return (UserFolderInfo) folderInfo;
1435 }
1436
1437 /**
1438 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1439 * new one.
1440 */
1441 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1442 // See if a placeholder was created for us already
1443 FolderInfo folderInfo = folders.get(id);
1444 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1445 // No placeholder -- create a new instance
1446 folderInfo = new LiveFolderInfo();
1447 folders.put(id, folderInfo);
1448 }
1449 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1453 String label = activityInfo.loadLabel(manager).toString();
1454 if (label == null) {
1455 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1456 if (label == null) {
1457 label = activityInfo.name;
1458 }
1459 }
1460 return label;
1461 }
1462
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001464 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001465 = new Comparator<ApplicationInfo>() {
1466 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1467 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 };
Joe Onoratobe386092009-11-17 17:32:16 -08001470
1471 public void dumpState() {
1472 Log.d(TAG, "mBeforeFirstLoad=" + mBeforeFirstLoad);
1473 Log.d(TAG, "mCallbacks=" + mCallbacks);
1474 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1475 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1476 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1477 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
1478 mLoader.dumpState();
1479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480}