blob: e10396123a7131a925528d8ff0e39a57bb77d514 [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;
32import android.net.Uri;
Joe Onorato9c1289c2009-08-17 11:03:03 -040033import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036
Joe Onorato9c1289c2009-08-17 11:03:03 -040037import java.lang.ref.WeakReference;
38import java.net.URISyntaxException;
39import java.text.Collator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import java.util.Comparator;
42import java.util.Collections;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import java.util.HashMap;
44import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045
46/**
47 * Maintains in-memory state of the Launcher. It is expected that there should be only one
48 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070049 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040051public class LauncherModel extends BroadcastReceiver {
Romain Guy829f56a2009-03-27 16:58:13 -070052 static final boolean DEBUG_LOADERS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -040053 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054
Joe Onoratof99f8c12009-10-31 17:27:36 -040055 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040056 private final Object mLock = new Object();
57 private DeferredHandler mHandler = new DeferredHandler();
58 private Loader mLoader = new Loader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
Joe Onoratof99f8c12009-10-31 17:27:36 -040060 private boolean mBeforeFirstLoad = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -040061 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
Joe Onorato9c1289c2009-08-17 11:03:03 -040063 private AllAppsList mAllAppsList = new AllAppsList();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 public interface Callbacks {
66 public int getCurrentWorkspaceScreen();
67 public void startBinding();
68 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
69 public void finishBindingItems();
70 public void bindAppWidget(LauncherAppWidgetInfo info);
71 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
72 public void bindPackageAdded(ArrayList<ApplicationInfo> apps);
73 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps);
74 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps);
75 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Joe Onoratof99f8c12009-10-31 17:27:36 -040077 LauncherModel(LauncherApplication app) {
78 mApp = app;
79 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 /**
82 * Adds an item to the DB if it was not created previously, or move it to a new
83 * <container, screen, cellX, cellY>
84 */
85 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
86 int screen, int cellX, int cellY) {
87 if (item.container == ItemInfo.NO_ID) {
88 // From all apps
89 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
90 } else {
91 // From somewhere else
92 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
94 }
95
96 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040097 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070098 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
100 int cellX, int cellY) {
101 item.container = container;
102 item.screen = screen;
103 item.cellX = cellX;
104 item.cellY = cellY;
105
106 final ContentValues values = new ContentValues();
107 final ContentResolver cr = context.getContentResolver();
108
109 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
110 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
111 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
112 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
113
114 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700115 }
116
117 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 * Returns true if the shortcuts already exists in the database.
119 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static boolean shortcutExists(Context context, String title, Intent intent) {
122 final ContentResolver cr = context.getContentResolver();
123 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
124 new String[] { "title", "intent" }, "title=? and intent=?",
125 new String[] { title, intent.toUri(0) }, null);
126 boolean result = false;
127 try {
128 result = c.moveToFirst();
129 } finally {
130 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400132 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700133 }
134
Joe Onorato9c1289c2009-08-17 11:03:03 -0400135 /**
136 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
137 */
138 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
139 final ContentResolver cr = context.getContentResolver();
140 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
141 "_id=? and (itemType=? or itemType=?)",
142 new String[] { String.valueOf(id),
143 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
144 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700145
Joe Onorato9c1289c2009-08-17 11:03:03 -0400146 try {
147 if (c.moveToFirst()) {
148 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
149 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
150 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
151 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
152 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
153 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 FolderInfo folderInfo = null;
156 switch (c.getInt(itemTypeIndex)) {
157 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
158 folderInfo = findOrMakeUserFolder(folderList, id);
159 break;
160 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
161 folderInfo = findOrMakeLiveFolder(folderList, id);
162 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700163 }
164
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 folderInfo.title = c.getString(titleIndex);
166 folderInfo.id = id;
167 folderInfo.container = c.getInt(containerIndex);
168 folderInfo.screen = c.getInt(screenIndex);
169 folderInfo.cellX = c.getInt(cellXIndex);
170 folderInfo.cellY = c.getInt(cellYIndex);
171
172 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700173 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 } finally {
175 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700176 }
177
178 return null;
179 }
180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 /**
182 * Add an item to the database in a specified container. Sets the container, screen, cellX and
183 * cellY fields of the item. Also assigns an ID to the item.
184 */
185 static void addItemToDatabase(Context context, ItemInfo item, long container,
186 int screen, int cellX, int cellY, boolean notify) {
187 item.container = container;
188 item.screen = screen;
189 item.cellX = cellX;
190 item.cellY = cellY;
191
192 final ContentValues values = new ContentValues();
193 final ContentResolver cr = context.getContentResolver();
194
195 item.onAddToDatabase(values);
196
197 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
198 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
199
200 if (result != null) {
201 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700202 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700203 }
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 /**
206 * Update an item to the database in a specified container.
207 */
208 static void updateItemInDatabase(Context context, ItemInfo item) {
209 final ContentValues values = new ContentValues();
210 final ContentResolver cr = context.getContentResolver();
211
212 item.onAddToDatabase(values);
213
214 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
215 }
216
217 /**
218 * Removes the specified item from the database
219 * @param context
220 * @param item
221 */
222 static void deleteItemFromDatabase(Context context, ItemInfo item) {
223 final ContentResolver cr = context.getContentResolver();
224
225 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
226 }
227
228 /**
229 * Remove the contents of the specified folder from the database
230 */
231 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
232 final ContentResolver cr = context.getContentResolver();
233
234 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
235 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
236 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
237 }
238
239 /**
240 * Set this as the current Launcher activity object for the loader.
241 */
242 public void initialize(Callbacks callbacks) {
243 synchronized (mLock) {
244 mCallbacks = new WeakReference<Callbacks>(callbacks);
245 }
246 }
247
248 public void startLoader(Context context, boolean isLaunching) {
249 mLoader.startLoader(context, isLaunching);
250 }
251
252 public void stopLoader() {
253 mLoader.stopLoader();
254 }
255
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700256 /**
257 * We pick up most of the changes to all apps.
258 */
259 public void setAllAppsDirty() {
260 mLoader.setAllAppsDirty();
261 }
262
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 public void setWorkspaceDirty() {
264 mLoader.setWorkspaceDirty();
265 }
266
267 /**
268 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
269 * ACTION_PACKAGE_CHANGED.
270 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400271 public void onReceive(Context context, Intent intent) {
272 // Use the app as the context.
273 context = mApp;
274
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 final String packageName = intent.getData().getSchemeSpecificPart();
276
277 ArrayList<ApplicationInfo> added = null;
278 ArrayList<ApplicationInfo> removed = null;
279 ArrayList<ApplicationInfo> modified = null;
280 boolean update = false;
281 boolean remove = false;
282
283 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400284 if (mBeforeFirstLoad) {
285 // If we haven't even loaded yet, don't bother, since we'll just pick
286 // up the changes.
287 return;
288 }
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 final String action = intent.getAction();
291 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
292
293 if (packageName == null || packageName.length() == 0) {
294 // they sent us a bad intent
295 return;
296 }
297
298 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
299 mAllAppsList.updatePackage(context, packageName);
300 update = true;
301 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
302 if (!replacing) {
303 mAllAppsList.removePackage(packageName);
304 remove = true;
305 }
306 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
307 // later, we will update the package at this time
308 } else {
309 if (!replacing) {
310 mAllAppsList.addPackage(context, packageName);
311 } else {
312 mAllAppsList.updatePackage(context, packageName);
313 update = true;
314 }
315 }
316
317 if (mAllAppsList.added.size() > 0) {
318 added = mAllAppsList.added;
Romain Guy84f296c2009-11-04 15:00:44 -0800319 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 }
321 if (mAllAppsList.removed.size() > 0) {
322 removed = mAllAppsList.removed;
Romain Guy84f296c2009-11-04 15:00:44 -0800323 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400324 for (ApplicationInfo info: removed) {
325 AppInfoCache.remove(info.intent.getComponent());
326 }
327 }
328 if (mAllAppsList.modified.size() > 0) {
329 modified = mAllAppsList.modified;
Romain Guy84f296c2009-11-04 15:00:44 -0800330 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400331 }
332
Marco Nelissen3c8b90d2009-09-11 14:49:50 -0700333 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400334 if (callbacks == null) {
335 return;
336 }
337
338 if (added != null) {
339 final ArrayList<ApplicationInfo> addedFinal = added;
340 mHandler.post(new Runnable() {
341 public void run() {
342 callbacks.bindPackageAdded(addedFinal);
343 }
344 });
345 }
346 if (update || modified != null) {
347 final ArrayList<ApplicationInfo> modifiedFinal = modified;
348 mHandler.post(new Runnable() {
349 public void run() {
350 callbacks.bindPackageUpdated(packageName, modifiedFinal);
351 }
352 });
353 }
354 if (remove || removed != null) {
355 final ArrayList<ApplicationInfo> removedFinal = removed;
356 mHandler.post(new Runnable() {
357 public void run() {
358 callbacks.bindPackageRemoved(packageName, removedFinal);
359 }
360 });
361 }
362 }
363 }
364
365 public class Loader {
366 private static final int ITEMS_CHUNK = 6;
367
368 private LoaderThread mLoaderThread;
369
370 private int mLastWorkspaceSeq = 0;
371 private int mWorkspaceSeq = 1;
372
373 private int mLastAllAppsSeq = 0;
374 private int mAllAppsSeq = 1;
375
Romain Guy84f296c2009-11-04 15:00:44 -0800376 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
377 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
378 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379
380 /**
381 * Call this from the ui thread so the handler is initialized on the correct thread.
382 */
383 public Loader() {
384 }
385
386 public void startLoader(Context context, boolean isLaunching) {
387 synchronized (mLock) {
388 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
389 // Don't bother to start the thread if we know it's not going to do anything
390 if (mCallbacks.get() != null) {
391 LoaderThread oldThread = mLoaderThread;
392 if (oldThread != null) {
393 if (oldThread.isLaunching()) {
394 // don't downgrade isLaunching if we're already running
395 isLaunching = true;
396 }
397 oldThread.stopLocked();
398 }
399 mLoaderThread = new LoaderThread(context, oldThread, isLaunching);
400 mLoaderThread.start();
401 }
402 }
403 }
404
405 public void stopLoader() {
406 synchronized (mLock) {
407 if (mLoaderThread != null) {
408 mLoaderThread.stopLocked();
409 }
410 }
411 }
412
413 public void setWorkspaceDirty() {
414 synchronized (mLock) {
415 mWorkspaceSeq++;
416 }
417 }
418
419 public void setAllAppsDirty() {
420 synchronized (mLock) {
421 mAllAppsSeq++;
422 }
423 }
424
425 /**
426 * Runnable for the thread that loads the contents of the launcher:
427 * - workspace icons
428 * - widgets
429 * - all apps icons
430 */
431 private class LoaderThread extends Thread {
432 private Context mContext;
433 private Thread mWaitThread;
434 private boolean mIsLaunching;
435 private boolean mStopped;
436 private boolean mWorkspaceDoneBinding;
437
438 LoaderThread(Context context, Thread waitThread, boolean isLaunching) {
439 mContext = context;
440 mWaitThread = waitThread;
441 mIsLaunching = isLaunching;
442 }
443
444 boolean isLaunching() {
445 return mIsLaunching;
446 }
447
448 /**
449 * If another LoaderThread was supplied, we need to wait for that to finish before
450 * we start our processing. This keeps the ordering of the setting and clearing
451 * of the dirty flags correct by making sure we don't start processing stuff until
452 * they've had a chance to re-set them. We do this waiting the worker thread, not
453 * the ui thread to avoid ANRs.
454 */
455 private void waitForOtherThread() {
456 if (mWaitThread != null) {
457 boolean done = false;
458 while (!done) {
459 try {
460 mWaitThread.join();
Joe Onoratoefabe002009-08-28 09:38:18 -0700461 done = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400462 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800463 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400464 }
465 }
466 mWaitThread = null;
467 }
468 }
469
470 public void run() {
471 waitForOtherThread();
472
473 // Elevate priority when Home launches for the first time to avoid
474 // starving at boot time. Staring at a blank home is not cool.
475 synchronized (mLock) {
476 android.os.Process.setThreadPriority(mIsLaunching
477 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
478 }
479
480 // Load the workspace only if it's dirty.
481 int workspaceSeq;
482 boolean workspaceDirty;
483 synchronized (mLock) {
484 workspaceSeq = mWorkspaceSeq;
485 workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq;
486 }
487 if (workspaceDirty) {
488 loadWorkspace();
489 }
490 synchronized (mLock) {
491 // If we're not stopped, and nobody has incremented mWorkspaceSeq.
492 if (mStopped) {
493 return;
494 }
495 if (workspaceSeq == mWorkspaceSeq) {
496 mLastWorkspaceSeq = mWorkspaceSeq;
497 }
498 }
499
500 // Bind the workspace
501 bindWorkspace();
502
503 // Wait until the either we're stopped or the other threads are done.
504 // This way we don't start loading all apps until the workspace has settled
505 // down.
506 synchronized (LoaderThread.this) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500507 mHandler.postIdle(new Runnable() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400508 public void run() {
509 synchronized (LoaderThread.this) {
510 mWorkspaceDoneBinding = true;
511 Log.d(TAG, "done with workspace");
512 LoaderThread.this.notify();
513 }
514 }
515 });
516 Log.d(TAG, "waiting to be done with workspace");
517 while (!mStopped && !mWorkspaceDoneBinding) {
518 try {
519 this.wait();
520 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800521 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400522 }
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;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 }
563 }
564
565 public void stopLocked() {
566 synchronized (LoaderThread.this) {
567 mStopped = true;
568 this.notify();
569 }
570 }
571
572 /**
573 * Gets the callbacks object. If we've been stopped, or if the launcher object
574 * has somehow been garbage collected, return null instead.
575 */
576 Callbacks tryGetCallbacks() {
577 synchronized (mLock) {
578 if (mStopped) {
579 return null;
580 }
581
582 final Callbacks callbacks = mCallbacks.get();
583 if (callbacks == null) {
584 Log.w(TAG, "no mCallbacks");
585 return null;
586 }
587
588 return callbacks;
589 }
590 }
591
592 private void loadWorkspace() {
593 long t = SystemClock.uptimeMillis();
594
595 final Context context = mContext;
596 final ContentResolver contentResolver = context.getContentResolver();
597 final PackageManager manager = context.getPackageManager();
598
599 /* TODO
600 if (mLocaleChanged) {
601 updateShortcutLabels(contentResolver, manager);
602 }
603 */
604
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400605 mItems.clear();
606
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 final Cursor c = contentResolver.query(
608 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
609
610 try {
611 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
612 final int intentIndex = c.getColumnIndexOrThrow
613 (LauncherSettings.Favorites.INTENT);
614 final int titleIndex = c.getColumnIndexOrThrow
615 (LauncherSettings.Favorites.TITLE);
616 final int iconTypeIndex = c.getColumnIndexOrThrow(
617 LauncherSettings.Favorites.ICON_TYPE);
618 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
619 final int iconPackageIndex = c.getColumnIndexOrThrow(
620 LauncherSettings.Favorites.ICON_PACKAGE);
621 final int iconResourceIndex = c.getColumnIndexOrThrow(
622 LauncherSettings.Favorites.ICON_RESOURCE);
623 final int containerIndex = c.getColumnIndexOrThrow(
624 LauncherSettings.Favorites.CONTAINER);
625 final int itemTypeIndex = c.getColumnIndexOrThrow(
626 LauncherSettings.Favorites.ITEM_TYPE);
627 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
628 LauncherSettings.Favorites.APPWIDGET_ID);
629 final int screenIndex = c.getColumnIndexOrThrow(
630 LauncherSettings.Favorites.SCREEN);
631 final int cellXIndex = c.getColumnIndexOrThrow
632 (LauncherSettings.Favorites.CELLX);
633 final int cellYIndex = c.getColumnIndexOrThrow
634 (LauncherSettings.Favorites.CELLY);
635 final int spanXIndex = c.getColumnIndexOrThrow
636 (LauncherSettings.Favorites.SPANX);
637 final int spanYIndex = c.getColumnIndexOrThrow(
638 LauncherSettings.Favorites.SPANY);
639 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
640 final int displayModeIndex = c.getColumnIndexOrThrow(
641 LauncherSettings.Favorites.DISPLAY_MODE);
642
643 ApplicationInfo info;
644 String intentDescription;
645 Widget widgetInfo;
646 LauncherAppWidgetInfo appWidgetInfo;
647 int container;
648 long id;
649 Intent intent;
650
651 while (!mStopped && c.moveToNext()) {
652 try {
653 int itemType = c.getInt(itemTypeIndex);
654
655 switch (itemType) {
656 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
657 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
658 intentDescription = c.getString(intentIndex);
659 try {
660 intent = Intent.parseUri(intentDescription, 0);
661 } catch (URISyntaxException e) {
662 continue;
663 }
664
665 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
666 info = getApplicationInfo(manager, intent, context);
667 } else {
668 info = getApplicationInfoShortcut(c, context, iconTypeIndex,
669 iconPackageIndex, iconResourceIndex, iconIndex);
670 }
671
672 if (info == null) {
673 info = new ApplicationInfo();
674 info.icon = manager.getDefaultActivityIcon();
675 }
676
677 if (info != null) {
678 info.title = c.getString(titleIndex);
679 info.intent = intent;
680
681 info.id = c.getLong(idIndex);
682 container = c.getInt(containerIndex);
683 info.container = container;
684 info.screen = c.getInt(screenIndex);
685 info.cellX = c.getInt(cellXIndex);
686 info.cellY = c.getInt(cellYIndex);
687
688 switch (container) {
689 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
690 mItems.add(info);
691 break;
692 default:
693 // Item is in a user folder
694 UserFolderInfo folderInfo =
695 findOrMakeUserFolder(folders, container);
696 folderInfo.add(info);
697 break;
698 }
699 }
700 break;
701 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
702
703 id = c.getLong(idIndex);
704 UserFolderInfo folderInfo = findOrMakeUserFolder(folders, id);
705
706 folderInfo.title = c.getString(titleIndex);
707
708 folderInfo.id = id;
709 container = c.getInt(containerIndex);
710 folderInfo.container = container;
711 folderInfo.screen = c.getInt(screenIndex);
712 folderInfo.cellX = c.getInt(cellXIndex);
713 folderInfo.cellY = c.getInt(cellYIndex);
714
715 switch (container) {
716 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
717 mItems.add(folderInfo);
718 break;
719 }
720 break;
721 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
722
723 id = c.getLong(idIndex);
724 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(folders, id);
725
726 intentDescription = c.getString(intentIndex);
727 intent = null;
728 if (intentDescription != null) {
729 try {
730 intent = Intent.parseUri(intentDescription, 0);
731 } catch (URISyntaxException e) {
732 // Ignore, a live folder might not have a base intent
733 }
734 }
735
736 liveFolderInfo.title = c.getString(titleIndex);
737 liveFolderInfo.id = id;
738 container = c.getInt(containerIndex);
739 liveFolderInfo.container = container;
740 liveFolderInfo.screen = c.getInt(screenIndex);
741 liveFolderInfo.cellX = c.getInt(cellXIndex);
742 liveFolderInfo.cellY = c.getInt(cellYIndex);
743 liveFolderInfo.uri = Uri.parse(c.getString(uriIndex));
744 liveFolderInfo.baseIntent = intent;
745 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
746
747 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
748 iconResourceIndex, liveFolderInfo);
749
750 switch (container) {
751 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
752 mItems.add(liveFolderInfo);
753 break;
754 }
755 break;
756 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
757 widgetInfo = Widget.makeSearch();
758
759 container = c.getInt(containerIndex);
760 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
761 Log.e(TAG, "Widget found where container "
762 + "!= CONTAINER_DESKTOP ignoring!");
763 continue;
764 }
765
766 widgetInfo.id = c.getLong(idIndex);
767 widgetInfo.screen = c.getInt(screenIndex);
768 widgetInfo.container = container;
769 widgetInfo.cellX = c.getInt(cellXIndex);
770 widgetInfo.cellY = c.getInt(cellYIndex);
771
772 mItems.add(widgetInfo);
773 break;
774 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
775 // Read all Launcher-specific widget details
776 int appWidgetId = c.getInt(appWidgetIdIndex);
777 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
778 appWidgetInfo.id = c.getLong(idIndex);
779 appWidgetInfo.screen = c.getInt(screenIndex);
780 appWidgetInfo.cellX = c.getInt(cellXIndex);
781 appWidgetInfo.cellY = c.getInt(cellYIndex);
782 appWidgetInfo.spanX = c.getInt(spanXIndex);
783 appWidgetInfo.spanY = c.getInt(spanYIndex);
784
785 container = c.getInt(containerIndex);
786 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
787 Log.e(TAG, "Widget found where container "
788 + "!= CONTAINER_DESKTOP -- ignoring!");
789 continue;
790 }
791 appWidgetInfo.container = c.getInt(containerIndex);
792
793 mAppWidgets.add(appWidgetInfo);
794 break;
795 }
796 } catch (Exception e) {
797 Log.w(TAG, "Desktop items loading interrupted:", e);
798 }
799 }
800 } finally {
801 c.close();
802 }
803 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
804 }
805
806 /**
807 * Read everything out of our database.
808 */
809 private void bindWorkspace() {
810 final long t = SystemClock.uptimeMillis();
811
812 // Don't use these two variables in any of the callback runnables.
813 // Otherwise we hold a reference to them.
814 Callbacks callbacks = mCallbacks.get();
815 if (callbacks == null) {
816 // This launcher has exited and nobody bothered to tell us. Just bail.
817 Log.w(TAG, "LoaderThread running with no launcher");
818 return;
819 }
820
821 int N;
822 // Tell the workspace that we're about to start firing items at it
823 mHandler.post(new Runnable() {
824 public void run() {
825 Callbacks callbacks = tryGetCallbacks();
826 if (callbacks != null) {
827 callbacks.startBinding();
828 }
829 }
830 });
831 // Add the items to the workspace.
832 N = mItems.size();
833 for (int i=0; i<N; i+=ITEMS_CHUNK) {
834 final int start = i;
835 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
836 mHandler.post(new Runnable() {
837 public void run() {
838 Callbacks callbacks = tryGetCallbacks();
839 if (callbacks != null) {
840 callbacks.bindItems(mItems, start, start+chunkSize);
841 }
842 }
843 });
844 }
845 // Wait until the queue goes empty.
846 mHandler.postIdle(new Runnable() {
847 public void run() {
848 Log.d(TAG, "Going to start binding widgets soon.");
849 }
850 });
851 // Bind the widgets, one at a time.
852 // WARNING: this is calling into the workspace from the background thread,
853 // but since getCurrentScreen() just returns the int, we should be okay. This
854 // is just a hint for the order, and if it's wrong, we'll be okay.
855 // TODO: instead, we should have that push the current screen into here.
856 final int currentScreen = callbacks.getCurrentWorkspaceScreen();
857 N = mAppWidgets.size();
858 // once for the current screen
859 for (int i=0; i<N; i++) {
860 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
861 if (widget.screen == currentScreen) {
862 mHandler.post(new Runnable() {
863 public void run() {
864 Callbacks callbacks = tryGetCallbacks();
865 if (callbacks != null) {
866 callbacks.bindAppWidget(widget);
867 }
868 }
869 });
870 }
871 }
872 // once for the other screens
873 for (int i=0; i<N; i++) {
874 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
875 if (widget.screen != currentScreen) {
876 mHandler.post(new Runnable() {
877 public void run() {
878 Callbacks callbacks = tryGetCallbacks();
879 if (callbacks != null) {
880 callbacks.bindAppWidget(widget);
881 }
882 }
883 });
884 }
885 }
886 // TODO: Bind the folders
887 // Tell the workspace that we're done.
888 mHandler.post(new Runnable() {
889 public void run() {
890 Callbacks callbacks = tryGetCallbacks();
891 if (callbacks != null) {
892 callbacks.finishBindingItems();
893 }
894 }
895 });
896 // If we're profiling, this is the last thing in the queue.
897 mHandler.post(new Runnable() {
898 public void run() {
899 Log.d(TAG, "bound workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
900 if (Launcher.PROFILE_ROTATE) {
901 android.os.Debug.stopMethodTracing();
902 }
903 }
904 });
905 }
906
907 private void loadAllApps() {
908 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
909 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
910
911 final Callbacks callbacks = tryGetCallbacks();
912 if (callbacks == null) {
913 return;
914 }
915
916 final Context context = mContext;
917 final PackageManager packageManager = context.getPackageManager();
918
919 final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
920
921 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400922 mBeforeFirstLoad = false;
923
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 mAllAppsList.clear();
925 if (apps != null) {
926 long t = SystemClock.uptimeMillis();
927
928 int N = apps.size();
929 Utilities.BubbleText bubble = new Utilities.BubbleText(context);
930 for (int i=0; i<N && !mStopped; i++) {
931 // This builds the icon bitmaps.
932 mAllAppsList.add(AppInfoCache.cache(apps.get(i), context, bubble));
933 }
934 Collections.sort(mAllAppsList.data, sComparator);
935 Collections.sort(mAllAppsList.added, sComparator);
936 Log.d(TAG, "cached app icons in " + (SystemClock.uptimeMillis()-t) + "ms");
937 }
938 }
939 }
940
941 private void bindAllApps() {
942 synchronized (mLock) {
943 final ArrayList<ApplicationInfo> results = mAllAppsList.added;
Romain Guy84f296c2009-11-04 15:00:44 -0800944 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 mHandler.post(new Runnable() {
946 public void run() {
Joe Onorato34b02492009-10-14 11:13:48 -0700947 final long t = SystemClock.uptimeMillis();
948 final int count = results.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949
950 Callbacks callbacks = tryGetCallbacks();
951 if (callbacks != null) {
952 callbacks.bindAllApplications(results);
953 }
954
Joe Onorato34b02492009-10-14 11:13:48 -0700955 Log.d(TAG, "bound app " + count + " icons in "
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 + (SystemClock.uptimeMillis()-t) + "ms");
957 }
958 });
959 }
960 }
961 }
962 }
963
964 /**
965 * Make an ApplicationInfo object for an application.
966 */
967 private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent,
968 Context context) {
969 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
970
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700971 if (resolveInfo == null) {
972 return null;
973 }
974
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 final ApplicationInfo info = new ApplicationInfo();
976 final ActivityInfo activityInfo = resolveInfo.activityInfo;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700977 info.icon = Utilities.createIconThumbnail(activityInfo.loadIcon(manager), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 if (info.title == null || info.title.length() == 0) {
979 info.title = activityInfo.loadLabel(manager);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 if (info.title == null) {
982 info.title = "";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
985 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700987
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 * Make an ApplicationInfo object for a sortcut
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 private static ApplicationInfo getApplicationInfoShortcut(Cursor c, Context context,
992 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 final ApplicationInfo info = new ApplicationInfo();
995 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 int iconType = c.getInt(iconTypeIndex);
998 switch (iconType) {
999 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1000 String packageName = c.getString(iconPackageIndex);
1001 String resourceName = c.getString(iconResourceIndex);
1002 PackageManager packageManager = context.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004 Resources resources = packageManager.getResourcesForApplication(packageName);
1005 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001006 info.icon = Utilities.createIconThumbnail(resources.getDrawable(id), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 } catch (Exception e) {
1008 info.icon = packageManager.getDefaultActivityIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010 info.iconResource = new Intent.ShortcutIconResource();
1011 info.iconResource.packageName = packageName;
1012 info.iconResource.resourceName = resourceName;
1013 info.customIcon = false;
1014 break;
1015 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
1016 byte[] data = c.getBlob(iconIndex);
1017 try {
1018 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
1019 info.icon = new FastBitmapDrawable(
1020 Utilities.createBitmapThumbnail(bitmap, context));
1021 } catch (Exception e) {
1022 packageManager = context.getPackageManager();
1023 info.icon = packageManager.getDefaultActivityIcon();
1024 }
1025 info.filtered = true;
1026 info.customIcon = true;
1027 break;
1028 default:
1029 info.icon = context.getPackageManager().getDefaultActivityIcon();
1030 info.customIcon = false;
1031 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 return info;
1034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1037 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1038
1039 int iconType = c.getInt(iconTypeIndex);
1040 switch (iconType) {
1041 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1042 String packageName = c.getString(iconPackageIndex);
1043 String resourceName = c.getString(iconResourceIndex);
1044 PackageManager packageManager = context.getPackageManager();
1045 try {
1046 Resources resources = packageManager.getResourcesForApplication(packageName);
1047 final int id = resources.getIdentifier(resourceName, null, null);
1048 liveFolderInfo.icon = resources.getDrawable(id);
1049 } catch (Exception e) {
1050 liveFolderInfo.icon =
1051 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1052 }
1053 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1054 liveFolderInfo.iconResource.packageName = packageName;
1055 liveFolderInfo.iconResource.resourceName = resourceName;
1056 break;
1057 default:
1058 liveFolderInfo.icon =
1059 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1060 }
1061 }
1062
1063 /**
1064 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1065 * or make a new one.
1066 */
1067 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1068 // See if a placeholder was created for us already
1069 FolderInfo folderInfo = folders.get(id);
1070 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1071 // No placeholder -- create a new instance
1072 folderInfo = new UserFolderInfo();
1073 folders.put(id, folderInfo);
1074 }
1075 return (UserFolderInfo) folderInfo;
1076 }
1077
1078 /**
1079 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1080 * new one.
1081 */
1082 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1083 // See if a placeholder was created for us already
1084 FolderInfo folderInfo = folders.get(id);
1085 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1086 // No placeholder -- create a new instance
1087 folderInfo = new LiveFolderInfo();
1088 folders.put(id, folderInfo);
1089 }
1090 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 }
1092
1093 private static void updateShortcutLabels(ContentResolver resolver, PackageManager manager) {
1094 final Cursor c = resolver.query(LauncherSettings.Favorites.CONTENT_URI,
Romain Guy73b979d2009-06-09 12:57:21 -07001095 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.TITLE,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 LauncherSettings.Favorites.INTENT, LauncherSettings.Favorites.ITEM_TYPE },
1097 null, null, null);
1098
Romain Guy73b979d2009-06-09 12:57:21 -07001099 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1101 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1102 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1103
1104 // boolean changed = false;
1105
1106 try {
1107 while (c.moveToNext()) {
1108 try {
1109 if (c.getInt(itemTypeIndex) !=
1110 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1111 continue;
1112 }
1113
1114 final String intentUri = c.getString(intentIndex);
1115 if (intentUri != null) {
Romain Guy1ce1a242009-06-23 17:34:54 -07001116 final Intent shortcut = Intent.parseUri(intentUri, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 if (Intent.ACTION_MAIN.equals(shortcut.getAction())) {
1118 final ComponentName name = shortcut.getComponent();
1119 if (name != null) {
1120 final ActivityInfo activityInfo = manager.getActivityInfo(name, 0);
1121 final String title = c.getString(titleIndex);
1122 String label = getLabel(manager, activityInfo);
1123
1124 if (title == null || !title.equals(label)) {
1125 final ContentValues values = new ContentValues();
1126 values.put(LauncherSettings.Favorites.TITLE, label);
1127
Romain Guyfedc4fc2009-03-27 20:48:20 -07001128 resolver.update(
1129 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 values, "_id=?",
1131 new String[] { String.valueOf(c.getLong(idIndex)) });
1132
1133 // changed = true;
1134 }
1135 }
1136 }
1137 }
1138 } catch (URISyntaxException e) {
1139 // Ignore
1140 } catch (PackageManager.NameNotFoundException e) {
1141 // Ignore
1142 }
1143 }
1144 } finally {
1145 c.close();
1146 }
1147
1148 // if (changed) resolver.notifyChange(Settings.Favorites.CONTENT_URI, null);
1149 }
1150
1151 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1152 String label = activityInfo.loadLabel(manager).toString();
1153 if (label == null) {
1154 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1155 if (label == null) {
1156 label = activityInfo.name;
1157 }
1158 }
1159 return label;
1160 }
1161
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 private static final Collator sCollator = Collator.getInstance();
1163 private static final Comparator<ApplicationInfo> sComparator
1164 = new Comparator<ApplicationInfo>() {
1165 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1166 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169}