blob: ac8d3f3d26a162ac0fd4a39564b26578eeada4fb [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
Joe Onoratof99f8c12009-10-31 17:27:36 -040019import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.ComponentName;
21import android.content.ContentResolver;
22import android.content.ContentValues;
23import android.content.Intent;
24import android.content.Context;
25import android.content.pm.ActivityInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
29import android.database.Cursor;
30import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070032import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070034import static android.util.Log.*;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040037import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import java.lang.ref.WeakReference;
40import java.net.URISyntaxException;
41import java.text.Collator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import java.util.Comparator;
44import java.util.Collections;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import java.util.HashMap;
46import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
48/**
49 * Maintains in-memory state of the Launcher. It is expected that there should be only one
50 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070051 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040053public class LauncherModel extends BroadcastReceiver {
Romain Guy829f56a2009-03-27 16:58:13 -070054 static final boolean DEBUG_LOADERS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -040055 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070056
Joe Onoratof99f8c12009-10-31 17:27:36 -040057 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040058 private final Object mLock = new Object();
59 private DeferredHandler mHandler = new DeferredHandler();
60 private Loader mLoader = new Loader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
Joe Onoratof99f8c12009-10-31 17:27:36 -040062 private boolean mBeforeFirstLoad = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -040063 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 private AllAppsList mAllAppsList = new AllAppsList();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 public interface Callbacks {
68 public int getCurrentWorkspaceScreen();
69 public void startBinding();
70 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
71 public void finishBindingItems();
72 public void bindAppWidget(LauncherAppWidgetInfo info);
73 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
74 public void bindPackageAdded(ArrayList<ApplicationInfo> apps);
75 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps);
76 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps);
77 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
Joe Onoratof99f8c12009-10-31 17:27:36 -040079 LauncherModel(LauncherApplication app) {
80 mApp = app;
81 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
Joe Onorato9c1289c2009-08-17 11:03:03 -040083 /**
84 * Adds an item to the DB if it was not created previously, or move it to a new
85 * <container, screen, cellX, cellY>
86 */
87 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
88 int screen, int cellX, int cellY) {
89 if (item.container == ItemInfo.NO_ID) {
90 // From all apps
91 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
92 } else {
93 // From somewhere else
94 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 }
96 }
97
98 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
102 int cellX, int cellY) {
103 item.container = container;
104 item.screen = screen;
105 item.cellX = cellX;
106 item.cellY = cellY;
107
108 final ContentValues values = new ContentValues();
109 final ContentResolver cr = context.getContentResolver();
110
111 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
112 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
113 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
114 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
115
116 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700117 }
118
119 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 * Returns true if the shortcuts already exists in the database.
121 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 static boolean shortcutExists(Context context, String title, Intent intent) {
124 final ContentResolver cr = context.getContentResolver();
125 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
126 new String[] { "title", "intent" }, "title=? and intent=?",
127 new String[] { title, intent.toUri(0) }, null);
128 boolean result = false;
129 try {
130 result = c.moveToFirst();
131 } finally {
132 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400134 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700135 }
136
Joe Onorato9c1289c2009-08-17 11:03:03 -0400137 /**
138 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
139 */
140 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
141 final ContentResolver cr = context.getContentResolver();
142 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
143 "_id=? and (itemType=? or itemType=?)",
144 new String[] { String.valueOf(id),
145 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
146 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 try {
149 if (c.moveToFirst()) {
150 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
151 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
152 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
153 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
154 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
155 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Joe Onorato9c1289c2009-08-17 11:03:03 -0400157 FolderInfo folderInfo = null;
158 switch (c.getInt(itemTypeIndex)) {
159 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
160 folderInfo = findOrMakeUserFolder(folderList, id);
161 break;
162 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
163 folderInfo = findOrMakeLiveFolder(folderList, id);
164 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700165 }
166
Joe Onorato9c1289c2009-08-17 11:03:03 -0400167 folderInfo.title = c.getString(titleIndex);
168 folderInfo.id = id;
169 folderInfo.container = c.getInt(containerIndex);
170 folderInfo.screen = c.getInt(screenIndex);
171 folderInfo.cellX = c.getInt(cellXIndex);
172 folderInfo.cellY = c.getInt(cellYIndex);
173
174 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700175 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 } finally {
177 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700178 }
179
180 return null;
181 }
182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 /**
184 * Add an item to the database in a specified container. Sets the container, screen, cellX and
185 * cellY fields of the item. Also assigns an ID to the item.
186 */
187 static void addItemToDatabase(Context context, ItemInfo item, long container,
188 int screen, int cellX, int cellY, boolean notify) {
189 item.container = container;
190 item.screen = screen;
191 item.cellX = cellX;
192 item.cellY = cellY;
193
194 final ContentValues values = new ContentValues();
195 final ContentResolver cr = context.getContentResolver();
196
197 item.onAddToDatabase(values);
198
199 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
200 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
201
202 if (result != null) {
203 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700204 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700205 }
206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 /**
208 * Update an item to the database in a specified container.
209 */
210 static void updateItemInDatabase(Context context, ItemInfo item) {
211 final ContentValues values = new ContentValues();
212 final ContentResolver cr = context.getContentResolver();
213
214 item.onAddToDatabase(values);
215
216 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
217 }
218
219 /**
220 * Removes the specified item from the database
221 * @param context
222 * @param item
223 */
224 static void deleteItemFromDatabase(Context context, ItemInfo item) {
225 final ContentResolver cr = context.getContentResolver();
226
227 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
228 }
229
230 /**
231 * Remove the contents of the specified folder from the database
232 */
233 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
234 final ContentResolver cr = context.getContentResolver();
235
236 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
237 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
238 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
239 }
240
241 /**
242 * Set this as the current Launcher activity object for the loader.
243 */
244 public void initialize(Callbacks callbacks) {
245 synchronized (mLock) {
246 mCallbacks = new WeakReference<Callbacks>(callbacks);
247 }
248 }
249
250 public void startLoader(Context context, boolean isLaunching) {
251 mLoader.startLoader(context, isLaunching);
252 }
253
254 public void stopLoader() {
255 mLoader.stopLoader();
256 }
257
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700258 /**
259 * We pick up most of the changes to all apps.
260 */
261 public void setAllAppsDirty() {
262 mLoader.setAllAppsDirty();
263 }
264
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 public void setWorkspaceDirty() {
266 mLoader.setWorkspaceDirty();
267 }
268
269 /**
270 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
271 * ACTION_PACKAGE_CHANGED.
272 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400273 public void onReceive(Context context, Intent intent) {
274 // Use the app as the context.
275 context = mApp;
276
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 final String packageName = intent.getData().getSchemeSpecificPart();
278
279 ArrayList<ApplicationInfo> added = null;
280 ArrayList<ApplicationInfo> removed = null;
281 ArrayList<ApplicationInfo> modified = null;
282 boolean update = false;
283 boolean remove = false;
284
285 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400286 if (mBeforeFirstLoad) {
287 // If we haven't even loaded yet, don't bother, since we'll just pick
288 // up the changes.
289 return;
290 }
291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 final String action = intent.getAction();
293 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
294
295 if (packageName == null || packageName.length() == 0) {
296 // they sent us a bad intent
297 return;
298 }
299
300 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
301 mAllAppsList.updatePackage(context, packageName);
302 update = true;
303 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
304 if (!replacing) {
305 mAllAppsList.removePackage(packageName);
306 remove = true;
307 }
308 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
309 // later, we will update the package at this time
310 } else {
311 if (!replacing) {
312 mAllAppsList.addPackage(context, packageName);
313 } else {
314 mAllAppsList.updatePackage(context, packageName);
315 update = true;
316 }
317 }
318
319 if (mAllAppsList.added.size() > 0) {
320 added = mAllAppsList.added;
Joe Onoratoefabe002009-08-28 09:38:18 -0700321 mAllAppsList.added = new ArrayList();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400322 }
323 if (mAllAppsList.removed.size() > 0) {
324 removed = mAllAppsList.removed;
Joe Onoratoefabe002009-08-28 09:38:18 -0700325 mAllAppsList.removed = new ArrayList();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400326 for (ApplicationInfo info: removed) {
327 AppInfoCache.remove(info.intent.getComponent());
328 }
329 }
330 if (mAllAppsList.modified.size() > 0) {
331 modified = mAllAppsList.modified;
Joe Onoratoefabe002009-08-28 09:38:18 -0700332 mAllAppsList.modified = new ArrayList();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400333 }
334
Marco Nelissen3c8b90d2009-09-11 14:49:50 -0700335 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400336 if (callbacks == null) {
337 return;
338 }
339
340 if (added != null) {
341 final ArrayList<ApplicationInfo> addedFinal = added;
342 mHandler.post(new Runnable() {
343 public void run() {
344 callbacks.bindPackageAdded(addedFinal);
345 }
346 });
347 }
348 if (update || modified != null) {
349 final ArrayList<ApplicationInfo> modifiedFinal = modified;
350 mHandler.post(new Runnable() {
351 public void run() {
352 callbacks.bindPackageUpdated(packageName, modifiedFinal);
353 }
354 });
355 }
356 if (remove || removed != null) {
357 final ArrayList<ApplicationInfo> removedFinal = removed;
358 mHandler.post(new Runnable() {
359 public void run() {
360 callbacks.bindPackageRemoved(packageName, removedFinal);
361 }
362 });
363 }
364 }
365 }
366
367 public class Loader {
368 private static final int ITEMS_CHUNK = 6;
369
370 private LoaderThread mLoaderThread;
371
372 private int mLastWorkspaceSeq = 0;
373 private int mWorkspaceSeq = 1;
374
375 private int mLastAllAppsSeq = 0;
376 private int mAllAppsSeq = 1;
377
378 final ArrayList<ItemInfo> mItems = new ArrayList();
379 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList();
380 final HashMap<Long, FolderInfo> folders = new HashMap();
381
382 /**
383 * Call this from the ui thread so the handler is initialized on the correct thread.
384 */
385 public Loader() {
386 }
387
388 public void startLoader(Context context, boolean isLaunching) {
389 synchronized (mLock) {
390 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
391 // Don't bother to start the thread if we know it's not going to do anything
392 if (mCallbacks.get() != null) {
393 LoaderThread oldThread = mLoaderThread;
394 if (oldThread != null) {
395 if (oldThread.isLaunching()) {
396 // don't downgrade isLaunching if we're already running
397 isLaunching = true;
398 }
399 oldThread.stopLocked();
400 }
401 mLoaderThread = new LoaderThread(context, oldThread, isLaunching);
402 mLoaderThread.start();
403 }
404 }
405 }
406
407 public void stopLoader() {
408 synchronized (mLock) {
409 if (mLoaderThread != null) {
410 mLoaderThread.stopLocked();
411 }
412 }
413 }
414
415 public void setWorkspaceDirty() {
416 synchronized (mLock) {
417 mWorkspaceSeq++;
418 }
419 }
420
421 public void setAllAppsDirty() {
422 synchronized (mLock) {
423 mAllAppsSeq++;
424 }
425 }
426
427 /**
428 * Runnable for the thread that loads the contents of the launcher:
429 * - workspace icons
430 * - widgets
431 * - all apps icons
432 */
433 private class LoaderThread extends Thread {
434 private Context mContext;
435 private Thread mWaitThread;
436 private boolean mIsLaunching;
437 private boolean mStopped;
438 private boolean mWorkspaceDoneBinding;
439
440 LoaderThread(Context context, Thread waitThread, boolean isLaunching) {
441 mContext = context;
442 mWaitThread = waitThread;
443 mIsLaunching = isLaunching;
444 }
445
446 boolean isLaunching() {
447 return mIsLaunching;
448 }
449
450 /**
451 * If another LoaderThread was supplied, we need to wait for that to finish before
452 * we start our processing. This keeps the ordering of the setting and clearing
453 * of the dirty flags correct by making sure we don't start processing stuff until
454 * they've had a chance to re-set them. We do this waiting the worker thread, not
455 * the ui thread to avoid ANRs.
456 */
457 private void waitForOtherThread() {
458 if (mWaitThread != null) {
459 boolean done = false;
460 while (!done) {
461 try {
462 mWaitThread.join();
Joe Onoratoefabe002009-08-28 09:38:18 -0700463 done = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400464 } catch (InterruptedException ex) {
465 }
466 }
467 mWaitThread = null;
468 }
469 }
470
471 public void run() {
472 waitForOtherThread();
473
474 // Elevate priority when Home launches for the first time to avoid
475 // starving at boot time. Staring at a blank home is not cool.
476 synchronized (mLock) {
477 android.os.Process.setThreadPriority(mIsLaunching
478 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
479 }
480
481 // Load the workspace only if it's dirty.
482 int workspaceSeq;
483 boolean workspaceDirty;
484 synchronized (mLock) {
485 workspaceSeq = mWorkspaceSeq;
486 workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq;
487 }
488 if (workspaceDirty) {
489 loadWorkspace();
490 }
491 synchronized (mLock) {
492 // If we're not stopped, and nobody has incremented mWorkspaceSeq.
493 if (mStopped) {
494 return;
495 }
496 if (workspaceSeq == mWorkspaceSeq) {
497 mLastWorkspaceSeq = mWorkspaceSeq;
498 }
499 }
500
501 // Bind the workspace
502 bindWorkspace();
503
504 // Wait until the either we're stopped or the other threads are done.
505 // This way we don't start loading all apps until the workspace has settled
506 // down.
507 synchronized (LoaderThread.this) {
508 mHandler.post(new Runnable() {
509 public void run() {
510 synchronized (LoaderThread.this) {
511 mWorkspaceDoneBinding = true;
512 Log.d(TAG, "done with workspace");
513 LoaderThread.this.notify();
514 }
515 }
516 });
517 Log.d(TAG, "waiting to be done with workspace");
518 while (!mStopped && !mWorkspaceDoneBinding) {
519 try {
520 this.wait();
521 } catch (InterruptedException ex) {
522 }
523 }
524 Log.d(TAG, "done waiting to be done with workspace");
525 }
526
527 // Load all apps if they're dirty
528 int allAppsSeq;
529 boolean allAppsDirty;
530 synchronized (mLock) {
531 allAppsSeq = mAllAppsSeq;
532 allAppsDirty = mAllAppsSeq != mLastAllAppsSeq;
Joe Onoratof99f8c12009-10-31 17:27:36 -0400533 //Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq
534 // + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 }
536 if (allAppsDirty) {
537 loadAllApps();
538 }
539 synchronized (mLock) {
540 // If we're not stopped, and nobody has incremented mAllAppsSeq.
541 if (mStopped) {
542 return;
543 }
544 if (allAppsSeq == mAllAppsSeq) {
545 mLastAllAppsSeq = mAllAppsSeq;
546 }
547 }
548
549 // Bind all apps
Joe Onorato34b02492009-10-14 11:13:48 -0700550 if (allAppsDirty) {
551 bindAllApps();
552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553
554 // Clear out this reference, otherwise we end up holding it until all of the
555 // callback runnables are done.
556 mContext = null;
557
558 synchronized (mLock) {
559 // Setting the reference is atomic, but we can't do it inside the other critical
560 // sections.
561 mLoaderThread = null;
562 return;
563 }
564 }
565
566 public void stopLocked() {
567 synchronized (LoaderThread.this) {
568 mStopped = true;
569 this.notify();
570 }
571 }
572
573 /**
574 * Gets the callbacks object. If we've been stopped, or if the launcher object
575 * has somehow been garbage collected, return null instead.
576 */
577 Callbacks tryGetCallbacks() {
578 synchronized (mLock) {
579 if (mStopped) {
580 return null;
581 }
582
583 final Callbacks callbacks = mCallbacks.get();
584 if (callbacks == null) {
585 Log.w(TAG, "no mCallbacks");
586 return null;
587 }
588
589 return callbacks;
590 }
591 }
592
593 private void loadWorkspace() {
594 long t = SystemClock.uptimeMillis();
595
596 final Context context = mContext;
597 final ContentResolver contentResolver = context.getContentResolver();
598 final PackageManager manager = context.getPackageManager();
599
600 /* TODO
601 if (mLocaleChanged) {
602 updateShortcutLabels(contentResolver, manager);
603 }
604 */
605
606 final Cursor c = contentResolver.query(
607 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
608
609 try {
610 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
611 final int intentIndex = c.getColumnIndexOrThrow
612 (LauncherSettings.Favorites.INTENT);
613 final int titleIndex = c.getColumnIndexOrThrow
614 (LauncherSettings.Favorites.TITLE);
615 final int iconTypeIndex = c.getColumnIndexOrThrow(
616 LauncherSettings.Favorites.ICON_TYPE);
617 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
618 final int iconPackageIndex = c.getColumnIndexOrThrow(
619 LauncherSettings.Favorites.ICON_PACKAGE);
620 final int iconResourceIndex = c.getColumnIndexOrThrow(
621 LauncherSettings.Favorites.ICON_RESOURCE);
622 final int containerIndex = c.getColumnIndexOrThrow(
623 LauncherSettings.Favorites.CONTAINER);
624 final int itemTypeIndex = c.getColumnIndexOrThrow(
625 LauncherSettings.Favorites.ITEM_TYPE);
626 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
627 LauncherSettings.Favorites.APPWIDGET_ID);
628 final int screenIndex = c.getColumnIndexOrThrow(
629 LauncherSettings.Favorites.SCREEN);
630 final int cellXIndex = c.getColumnIndexOrThrow
631 (LauncherSettings.Favorites.CELLX);
632 final int cellYIndex = c.getColumnIndexOrThrow
633 (LauncherSettings.Favorites.CELLY);
634 final int spanXIndex = c.getColumnIndexOrThrow
635 (LauncherSettings.Favorites.SPANX);
636 final int spanYIndex = c.getColumnIndexOrThrow(
637 LauncherSettings.Favorites.SPANY);
638 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
639 final int displayModeIndex = c.getColumnIndexOrThrow(
640 LauncherSettings.Favorites.DISPLAY_MODE);
641
642 ApplicationInfo info;
643 String intentDescription;
644 Widget widgetInfo;
645 LauncherAppWidgetInfo appWidgetInfo;
646 int container;
647 long id;
648 Intent intent;
649
650 while (!mStopped && c.moveToNext()) {
651 try {
652 int itemType = c.getInt(itemTypeIndex);
653
654 switch (itemType) {
655 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
656 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
657 intentDescription = c.getString(intentIndex);
658 try {
659 intent = Intent.parseUri(intentDescription, 0);
660 } catch (URISyntaxException e) {
661 continue;
662 }
663
664 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
665 info = getApplicationInfo(manager, intent, context);
666 } else {
667 info = getApplicationInfoShortcut(c, context, iconTypeIndex,
668 iconPackageIndex, iconResourceIndex, iconIndex);
669 }
670
671 if (info == null) {
672 info = new ApplicationInfo();
673 info.icon = manager.getDefaultActivityIcon();
674 }
675
676 if (info != null) {
677 info.title = c.getString(titleIndex);
678 info.intent = intent;
679
680 info.id = c.getLong(idIndex);
681 container = c.getInt(containerIndex);
682 info.container = container;
683 info.screen = c.getInt(screenIndex);
684 info.cellX = c.getInt(cellXIndex);
685 info.cellY = c.getInt(cellYIndex);
686
687 switch (container) {
688 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
689 mItems.add(info);
690 break;
691 default:
692 // Item is in a user folder
693 UserFolderInfo folderInfo =
694 findOrMakeUserFolder(folders, container);
695 folderInfo.add(info);
696 break;
697 }
698 }
699 break;
700 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
701
702 id = c.getLong(idIndex);
703 UserFolderInfo folderInfo = findOrMakeUserFolder(folders, id);
704
705 folderInfo.title = c.getString(titleIndex);
706
707 folderInfo.id = id;
708 container = c.getInt(containerIndex);
709 folderInfo.container = container;
710 folderInfo.screen = c.getInt(screenIndex);
711 folderInfo.cellX = c.getInt(cellXIndex);
712 folderInfo.cellY = c.getInt(cellYIndex);
713
714 switch (container) {
715 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
716 mItems.add(folderInfo);
717 break;
718 }
719 break;
720 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
721
722 id = c.getLong(idIndex);
723 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(folders, id);
724
725 intentDescription = c.getString(intentIndex);
726 intent = null;
727 if (intentDescription != null) {
728 try {
729 intent = Intent.parseUri(intentDescription, 0);
730 } catch (URISyntaxException e) {
731 // Ignore, a live folder might not have a base intent
732 }
733 }
734
735 liveFolderInfo.title = c.getString(titleIndex);
736 liveFolderInfo.id = id;
737 container = c.getInt(containerIndex);
738 liveFolderInfo.container = container;
739 liveFolderInfo.screen = c.getInt(screenIndex);
740 liveFolderInfo.cellX = c.getInt(cellXIndex);
741 liveFolderInfo.cellY = c.getInt(cellYIndex);
742 liveFolderInfo.uri = Uri.parse(c.getString(uriIndex));
743 liveFolderInfo.baseIntent = intent;
744 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
745
746 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
747 iconResourceIndex, liveFolderInfo);
748
749 switch (container) {
750 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
751 mItems.add(liveFolderInfo);
752 break;
753 }
754 break;
755 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
756 widgetInfo = Widget.makeSearch();
757
758 container = c.getInt(containerIndex);
759 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
760 Log.e(TAG, "Widget found where container "
761 + "!= CONTAINER_DESKTOP ignoring!");
762 continue;
763 }
764
765 widgetInfo.id = c.getLong(idIndex);
766 widgetInfo.screen = c.getInt(screenIndex);
767 widgetInfo.container = container;
768 widgetInfo.cellX = c.getInt(cellXIndex);
769 widgetInfo.cellY = c.getInt(cellYIndex);
770
771 mItems.add(widgetInfo);
772 break;
773 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
774 // Read all Launcher-specific widget details
775 int appWidgetId = c.getInt(appWidgetIdIndex);
776 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
777 appWidgetInfo.id = c.getLong(idIndex);
778 appWidgetInfo.screen = c.getInt(screenIndex);
779 appWidgetInfo.cellX = c.getInt(cellXIndex);
780 appWidgetInfo.cellY = c.getInt(cellYIndex);
781 appWidgetInfo.spanX = c.getInt(spanXIndex);
782 appWidgetInfo.spanY = c.getInt(spanYIndex);
783
784 container = c.getInt(containerIndex);
785 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
786 Log.e(TAG, "Widget found where container "
787 + "!= CONTAINER_DESKTOP -- ignoring!");
788 continue;
789 }
790 appWidgetInfo.container = c.getInt(containerIndex);
791
792 mAppWidgets.add(appWidgetInfo);
793 break;
794 }
795 } catch (Exception e) {
796 Log.w(TAG, "Desktop items loading interrupted:", e);
797 }
798 }
799 } finally {
800 c.close();
801 }
802 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
803 }
804
805 /**
806 * Read everything out of our database.
807 */
808 private void bindWorkspace() {
809 final long t = SystemClock.uptimeMillis();
810
811 // Don't use these two variables in any of the callback runnables.
812 // Otherwise we hold a reference to them.
813 Callbacks callbacks = mCallbacks.get();
814 if (callbacks == null) {
815 // This launcher has exited and nobody bothered to tell us. Just bail.
816 Log.w(TAG, "LoaderThread running with no launcher");
817 return;
818 }
819
820 int N;
821 // Tell the workspace that we're about to start firing items at it
822 mHandler.post(new Runnable() {
823 public void run() {
824 Callbacks callbacks = tryGetCallbacks();
825 if (callbacks != null) {
826 callbacks.startBinding();
827 }
828 }
829 });
830 // Add the items to the workspace.
831 N = mItems.size();
832 for (int i=0; i<N; i+=ITEMS_CHUNK) {
833 final int start = i;
834 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
835 mHandler.post(new Runnable() {
836 public void run() {
837 Callbacks callbacks = tryGetCallbacks();
838 if (callbacks != null) {
839 callbacks.bindItems(mItems, start, start+chunkSize);
840 }
841 }
842 });
843 }
844 // Wait until the queue goes empty.
845 mHandler.postIdle(new Runnable() {
846 public void run() {
847 Log.d(TAG, "Going to start binding widgets soon.");
848 }
849 });
850 // Bind the widgets, one at a time.
851 // WARNING: this is calling into the workspace from the background thread,
852 // but since getCurrentScreen() just returns the int, we should be okay. This
853 // is just a hint for the order, and if it's wrong, we'll be okay.
854 // TODO: instead, we should have that push the current screen into here.
855 final int currentScreen = callbacks.getCurrentWorkspaceScreen();
856 N = mAppWidgets.size();
857 // once for the current screen
858 for (int i=0; i<N; i++) {
859 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
860 if (widget.screen == currentScreen) {
861 mHandler.post(new Runnable() {
862 public void run() {
863 Callbacks callbacks = tryGetCallbacks();
864 if (callbacks != null) {
865 callbacks.bindAppWidget(widget);
866 }
867 }
868 });
869 }
870 }
871 // once for the other screens
872 for (int i=0; i<N; i++) {
873 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
874 if (widget.screen != currentScreen) {
875 mHandler.post(new Runnable() {
876 public void run() {
877 Callbacks callbacks = tryGetCallbacks();
878 if (callbacks != null) {
879 callbacks.bindAppWidget(widget);
880 }
881 }
882 });
883 }
884 }
885 // TODO: Bind the folders
886 // Tell the workspace that we're done.
887 mHandler.post(new Runnable() {
888 public void run() {
889 Callbacks callbacks = tryGetCallbacks();
890 if (callbacks != null) {
891 callbacks.finishBindingItems();
892 }
893 }
894 });
895 // If we're profiling, this is the last thing in the queue.
896 mHandler.post(new Runnable() {
897 public void run() {
898 Log.d(TAG, "bound workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
899 if (Launcher.PROFILE_ROTATE) {
900 android.os.Debug.stopMethodTracing();
901 }
902 }
903 });
904 }
905
906 private void loadAllApps() {
907 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
908 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
909
910 final Callbacks callbacks = tryGetCallbacks();
911 if (callbacks == null) {
912 return;
913 }
914
915 final Context context = mContext;
916 final PackageManager packageManager = context.getPackageManager();
917
918 final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
919
920 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400921 mBeforeFirstLoad = false;
922
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 mAllAppsList.clear();
924 if (apps != null) {
925 long t = SystemClock.uptimeMillis();
926
927 int N = apps.size();
928 Utilities.BubbleText bubble = new Utilities.BubbleText(context);
929 for (int i=0; i<N && !mStopped; i++) {
930 // This builds the icon bitmaps.
931 mAllAppsList.add(AppInfoCache.cache(apps.get(i), context, bubble));
932 }
933 Collections.sort(mAllAppsList.data, sComparator);
934 Collections.sort(mAllAppsList.added, sComparator);
935 Log.d(TAG, "cached app icons in " + (SystemClock.uptimeMillis()-t) + "ms");
936 }
937 }
938 }
939
940 private void bindAllApps() {
941 synchronized (mLock) {
942 final ArrayList<ApplicationInfo> results = mAllAppsList.added;
943 mAllAppsList.added = new ArrayList();
944 mHandler.post(new Runnable() {
945 public void run() {
Joe Onorato34b02492009-10-14 11:13:48 -0700946 final long t = SystemClock.uptimeMillis();
947 final int count = results.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948
949 Callbacks callbacks = tryGetCallbacks();
950 if (callbacks != null) {
951 callbacks.bindAllApplications(results);
952 }
953
Joe Onorato34b02492009-10-14 11:13:48 -0700954 Log.d(TAG, "bound app " + count + " icons in "
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 + (SystemClock.uptimeMillis()-t) + "ms");
956 }
957 });
958 }
959 }
960 }
961 }
962
963 /**
964 * Make an ApplicationInfo object for an application.
965 */
966 private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent,
967 Context context) {
968 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
969
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700970 if (resolveInfo == null) {
971 return null;
972 }
973
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 final ApplicationInfo info = new ApplicationInfo();
975 final ActivityInfo activityInfo = resolveInfo.activityInfo;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700976 info.icon = Utilities.createIconThumbnail(activityInfo.loadIcon(manager), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 if (info.title == null || info.title.length() == 0) {
978 info.title = activityInfo.loadLabel(manager);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700979 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 if (info.title == null) {
981 info.title = "";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
984 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700986
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 * Make an ApplicationInfo object for a sortcut
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 private static ApplicationInfo getApplicationInfoShortcut(Cursor c, Context context,
991 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 final ApplicationInfo info = new ApplicationInfo();
994 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 int iconType = c.getInt(iconTypeIndex);
997 switch (iconType) {
998 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
999 String packageName = c.getString(iconPackageIndex);
1000 String resourceName = c.getString(iconResourceIndex);
1001 PackageManager packageManager = context.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 Resources resources = packageManager.getResourcesForApplication(packageName);
1004 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001005 info.icon = Utilities.createIconThumbnail(resources.getDrawable(id), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006 } catch (Exception e) {
1007 info.icon = packageManager.getDefaultActivityIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009 info.iconResource = new Intent.ShortcutIconResource();
1010 info.iconResource.packageName = packageName;
1011 info.iconResource.resourceName = resourceName;
1012 info.customIcon = false;
1013 break;
1014 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
1015 byte[] data = c.getBlob(iconIndex);
1016 try {
1017 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
1018 info.icon = new FastBitmapDrawable(
1019 Utilities.createBitmapThumbnail(bitmap, context));
1020 } catch (Exception e) {
1021 packageManager = context.getPackageManager();
1022 info.icon = packageManager.getDefaultActivityIcon();
1023 }
1024 info.filtered = true;
1025 info.customIcon = true;
1026 break;
1027 default:
1028 info.icon = context.getPackageManager().getDefaultActivityIcon();
1029 info.customIcon = false;
1030 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 return info;
1033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1036 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1037
1038 int iconType = c.getInt(iconTypeIndex);
1039 switch (iconType) {
1040 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1041 String packageName = c.getString(iconPackageIndex);
1042 String resourceName = c.getString(iconResourceIndex);
1043 PackageManager packageManager = context.getPackageManager();
1044 try {
1045 Resources resources = packageManager.getResourcesForApplication(packageName);
1046 final int id = resources.getIdentifier(resourceName, null, null);
1047 liveFolderInfo.icon = resources.getDrawable(id);
1048 } catch (Exception e) {
1049 liveFolderInfo.icon =
1050 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1051 }
1052 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1053 liveFolderInfo.iconResource.packageName = packageName;
1054 liveFolderInfo.iconResource.resourceName = resourceName;
1055 break;
1056 default:
1057 liveFolderInfo.icon =
1058 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1059 }
1060 }
1061
1062 /**
1063 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1064 * or make a new one.
1065 */
1066 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1067 // See if a placeholder was created for us already
1068 FolderInfo folderInfo = folders.get(id);
1069 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1070 // No placeholder -- create a new instance
1071 folderInfo = new UserFolderInfo();
1072 folders.put(id, folderInfo);
1073 }
1074 return (UserFolderInfo) folderInfo;
1075 }
1076
1077 /**
1078 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1079 * new one.
1080 */
1081 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1082 // See if a placeholder was created for us already
1083 FolderInfo folderInfo = folders.get(id);
1084 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1085 // No placeholder -- create a new instance
1086 folderInfo = new LiveFolderInfo();
1087 folders.put(id, folderInfo);
1088 }
1089 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
1092 private static void updateShortcutLabels(ContentResolver resolver, PackageManager manager) {
1093 final Cursor c = resolver.query(LauncherSettings.Favorites.CONTENT_URI,
Romain Guy73b979d2009-06-09 12:57:21 -07001094 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.TITLE,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 LauncherSettings.Favorites.INTENT, LauncherSettings.Favorites.ITEM_TYPE },
1096 null, null, null);
1097
Romain Guy73b979d2009-06-09 12:57:21 -07001098 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1100 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1101 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1102
1103 // boolean changed = false;
1104
1105 try {
1106 while (c.moveToNext()) {
1107 try {
1108 if (c.getInt(itemTypeIndex) !=
1109 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1110 continue;
1111 }
1112
1113 final String intentUri = c.getString(intentIndex);
1114 if (intentUri != null) {
Romain Guy1ce1a242009-06-23 17:34:54 -07001115 final Intent shortcut = Intent.parseUri(intentUri, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 if (Intent.ACTION_MAIN.equals(shortcut.getAction())) {
1117 final ComponentName name = shortcut.getComponent();
1118 if (name != null) {
1119 final ActivityInfo activityInfo = manager.getActivityInfo(name, 0);
1120 final String title = c.getString(titleIndex);
1121 String label = getLabel(manager, activityInfo);
1122
1123 if (title == null || !title.equals(label)) {
1124 final ContentValues values = new ContentValues();
1125 values.put(LauncherSettings.Favorites.TITLE, label);
1126
Romain Guyfedc4fc2009-03-27 20:48:20 -07001127 resolver.update(
1128 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 values, "_id=?",
1130 new String[] { String.valueOf(c.getLong(idIndex)) });
1131
1132 // changed = true;
1133 }
1134 }
1135 }
1136 }
1137 } catch (URISyntaxException e) {
1138 // Ignore
1139 } catch (PackageManager.NameNotFoundException e) {
1140 // Ignore
1141 }
1142 }
1143 } finally {
1144 c.close();
1145 }
1146
1147 // if (changed) resolver.notifyChange(Settings.Favorites.CONTENT_URI, null);
1148 }
1149
1150 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1151 String label = activityInfo.loadLabel(manager).toString();
1152 if (label == null) {
1153 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1154 if (label == null) {
1155 label = activityInfo.name;
1156 }
1157 }
1158 return label;
1159 }
1160
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 private static final Collator sCollator = Collator.getInstance();
1162 private static final Comparator<ApplicationInfo> sComparator
1163 = new Comparator<ApplicationInfo>() {
1164 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1165 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168}