blob: 85d47a916fd148b036edb8a363a6db5564c49d4e [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 {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080052 static final boolean DEBUG_LOADERS = false;
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);
Joe Onoratoad72e172009-11-06 16:25:04 -050069 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -040070 public void finishBindingItems();
71 public void bindAppWidget(LauncherAppWidgetInfo info);
72 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
73 public void bindPackageAdded(ArrayList<ApplicationInfo> apps);
74 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps);
75 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps);
76 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Joe Onoratof99f8c12009-10-31 17:27:36 -040078 LauncherModel(LauncherApplication app) {
79 mApp = app;
80 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 /**
83 * Adds an item to the DB if it was not created previously, or move it to a new
84 * <container, screen, cellX, cellY>
85 */
86 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
87 int screen, int cellX, int cellY) {
88 if (item.container == ItemInfo.NO_ID) {
89 // From all apps
90 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
91 } else {
92 // From somewhere else
93 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 }
95 }
96
97 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070099 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
101 int cellX, int cellY) {
102 item.container = container;
103 item.screen = screen;
104 item.cellX = cellX;
105 item.cellY = cellY;
106
107 final ContentValues values = new ContentValues();
108 final ContentResolver cr = context.getContentResolver();
109
110 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
111 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
112 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
113 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
114
115 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700116 }
117
118 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 * Returns true if the shortcuts already exists in the database.
120 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static boolean shortcutExists(Context context, String title, Intent intent) {
123 final ContentResolver cr = context.getContentResolver();
124 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
125 new String[] { "title", "intent" }, "title=? and intent=?",
126 new String[] { title, intent.toUri(0) }, null);
127 boolean result = false;
128 try {
129 result = c.moveToFirst();
130 } finally {
131 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400133 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700134 }
135
Joe Onorato9c1289c2009-08-17 11:03:03 -0400136 /**
137 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
138 */
139 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
140 final ContentResolver cr = context.getContentResolver();
141 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
142 "_id=? and (itemType=? or itemType=?)",
143 new String[] { String.valueOf(id),
144 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
145 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700146
Joe Onorato9c1289c2009-08-17 11:03:03 -0400147 try {
148 if (c.moveToFirst()) {
149 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
150 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
151 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
152 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
153 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
154 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 FolderInfo folderInfo = null;
157 switch (c.getInt(itemTypeIndex)) {
158 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
159 folderInfo = findOrMakeUserFolder(folderList, id);
160 break;
161 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
162 folderInfo = findOrMakeLiveFolder(folderList, id);
163 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700164 }
165
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 folderInfo.title = c.getString(titleIndex);
167 folderInfo.id = id;
168 folderInfo.container = c.getInt(containerIndex);
169 folderInfo.screen = c.getInt(screenIndex);
170 folderInfo.cellX = c.getInt(cellXIndex);
171 folderInfo.cellY = c.getInt(cellYIndex);
172
173 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 } finally {
176 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700177 }
178
179 return null;
180 }
181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 /**
183 * Add an item to the database in a specified container. Sets the container, screen, cellX and
184 * cellY fields of the item. Also assigns an ID to the item.
185 */
186 static void addItemToDatabase(Context context, ItemInfo item, long container,
187 int screen, int cellX, int cellY, boolean notify) {
188 item.container = container;
189 item.screen = screen;
190 item.cellX = cellX;
191 item.cellY = cellY;
192
193 final ContentValues values = new ContentValues();
194 final ContentResolver cr = context.getContentResolver();
195
196 item.onAddToDatabase(values);
197
198 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
199 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
200
201 if (result != null) {
202 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700203 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700204 }
205
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 /**
207 * Update an item to the database in a specified container.
208 */
209 static void updateItemInDatabase(Context context, ItemInfo item) {
210 final ContentValues values = new ContentValues();
211 final ContentResolver cr = context.getContentResolver();
212
213 item.onAddToDatabase(values);
214
215 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
216 }
217
218 /**
219 * Removes the specified item from the database
220 * @param context
221 * @param item
222 */
223 static void deleteItemFromDatabase(Context context, ItemInfo item) {
224 final ContentResolver cr = context.getContentResolver();
225
226 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
227 }
228
229 /**
230 * Remove the contents of the specified folder from the database
231 */
232 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
233 final ContentResolver cr = context.getContentResolver();
234
235 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
236 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
237 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
238 }
239
240 /**
241 * Set this as the current Launcher activity object for the loader.
242 */
243 public void initialize(Callbacks callbacks) {
244 synchronized (mLock) {
245 mCallbacks = new WeakReference<Callbacks>(callbacks);
246 }
247 }
248
249 public void startLoader(Context context, boolean isLaunching) {
250 mLoader.startLoader(context, isLaunching);
251 }
252
253 public void stopLoader() {
254 mLoader.stopLoader();
255 }
256
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700257 /**
258 * We pick up most of the changes to all apps.
259 */
260 public void setAllAppsDirty() {
261 mLoader.setAllAppsDirty();
262 }
263
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264 public void setWorkspaceDirty() {
265 mLoader.setWorkspaceDirty();
266 }
267
268 /**
269 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
270 * ACTION_PACKAGE_CHANGED.
271 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400272 public void onReceive(Context context, Intent intent) {
273 // Use the app as the context.
274 context = mApp;
275
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 final String packageName = intent.getData().getSchemeSpecificPart();
277
278 ArrayList<ApplicationInfo> added = null;
279 ArrayList<ApplicationInfo> removed = null;
280 ArrayList<ApplicationInfo> modified = null;
281 boolean update = false;
282 boolean remove = false;
283
284 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400285 if (mBeforeFirstLoad) {
286 // If we haven't even loaded yet, don't bother, since we'll just pick
287 // up the changes.
288 return;
289 }
290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 final String action = intent.getAction();
292 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
293
294 if (packageName == null || packageName.length() == 0) {
295 // they sent us a bad intent
296 return;
297 }
298
299 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
300 mAllAppsList.updatePackage(context, packageName);
301 update = true;
302 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
303 if (!replacing) {
304 mAllAppsList.removePackage(packageName);
305 remove = true;
306 }
307 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
308 // later, we will update the package at this time
309 } else {
310 if (!replacing) {
311 mAllAppsList.addPackage(context, packageName);
312 } else {
313 mAllAppsList.updatePackage(context, packageName);
314 update = true;
315 }
316 }
317
318 if (mAllAppsList.added.size() > 0) {
319 added = mAllAppsList.added;
Romain Guy84f296c2009-11-04 15:00:44 -0800320 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 }
322 if (mAllAppsList.removed.size() > 0) {
323 removed = mAllAppsList.removed;
Romain Guy84f296c2009-11-04 15:00:44 -0800324 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400325 for (ApplicationInfo info: removed) {
326 AppInfoCache.remove(info.intent.getComponent());
327 }
328 }
329 if (mAllAppsList.modified.size() > 0) {
330 modified = mAllAppsList.modified;
Romain Guy84f296c2009-11-04 15:00:44 -0800331 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400332 }
333
Marco Nelissen3c8b90d2009-09-11 14:49:50 -0700334 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400335 if (callbacks == null) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800336 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400337 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
Romain Guy84f296c2009-11-04 15:00:44 -0800378 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
379 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
Joe Onoratoad72e172009-11-06 16:25:04 -0500380 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381
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) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800390 if (DEBUG_LOADERS) {
391 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
392 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400393 // Don't bother to start the thread if we know it's not going to do anything
394 if (mCallbacks.get() != null) {
395 LoaderThread oldThread = mLoaderThread;
396 if (oldThread != null) {
397 if (oldThread.isLaunching()) {
398 // don't downgrade isLaunching if we're already running
399 isLaunching = true;
400 }
401 oldThread.stopLocked();
402 }
403 mLoaderThread = new LoaderThread(context, oldThread, isLaunching);
404 mLoaderThread.start();
405 }
406 }
407 }
408
409 public void stopLoader() {
410 synchronized (mLock) {
411 if (mLoaderThread != null) {
412 mLoaderThread.stopLocked();
413 }
414 }
415 }
416
417 public void setWorkspaceDirty() {
418 synchronized (mLock) {
419 mWorkspaceSeq++;
420 }
421 }
422
423 public void setAllAppsDirty() {
424 synchronized (mLock) {
425 mAllAppsSeq++;
426 }
427 }
428
429 /**
430 * Runnable for the thread that loads the contents of the launcher:
431 * - workspace icons
432 * - widgets
433 * - all apps icons
434 */
435 private class LoaderThread extends Thread {
436 private Context mContext;
437 private Thread mWaitThread;
438 private boolean mIsLaunching;
439 private boolean mStopped;
440 private boolean mWorkspaceDoneBinding;
441
442 LoaderThread(Context context, Thread waitThread, boolean isLaunching) {
443 mContext = context;
444 mWaitThread = waitThread;
445 mIsLaunching = isLaunching;
446 }
447
448 boolean isLaunching() {
449 return mIsLaunching;
450 }
451
452 /**
453 * If another LoaderThread was supplied, we need to wait for that to finish before
454 * we start our processing. This keeps the ordering of the setting and clearing
455 * of the dirty flags correct by making sure we don't start processing stuff until
456 * they've had a chance to re-set them. We do this waiting the worker thread, not
457 * the ui thread to avoid ANRs.
458 */
459 private void waitForOtherThread() {
460 if (mWaitThread != null) {
461 boolean done = false;
462 while (!done) {
463 try {
464 mWaitThread.join();
Joe Onoratoefabe002009-08-28 09:38:18 -0700465 done = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400466 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800467 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400468 }
469 }
470 mWaitThread = null;
471 }
472 }
473
474 public void run() {
475 waitForOtherThread();
476
477 // Elevate priority when Home launches for the first time to avoid
478 // starving at boot time. Staring at a blank home is not cool.
479 synchronized (mLock) {
480 android.os.Process.setThreadPriority(mIsLaunching
481 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
482 }
483
484 // Load the workspace only if it's dirty.
485 int workspaceSeq;
486 boolean workspaceDirty;
487 synchronized (mLock) {
488 workspaceSeq = mWorkspaceSeq;
489 workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq;
490 }
491 if (workspaceDirty) {
492 loadWorkspace();
493 }
494 synchronized (mLock) {
495 // If we're not stopped, and nobody has incremented mWorkspaceSeq.
496 if (mStopped) {
497 return;
498 }
499 if (workspaceSeq == mWorkspaceSeq) {
500 mLastWorkspaceSeq = mWorkspaceSeq;
501 }
502 }
503
504 // Bind the workspace
505 bindWorkspace();
506
507 // Wait until the either we're stopped or the other threads are done.
508 // This way we don't start loading all apps until the workspace has settled
509 // down.
510 synchronized (LoaderThread.this) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500511 mHandler.postIdle(new Runnable() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 public void run() {
513 synchronized (LoaderThread.this) {
514 mWorkspaceDoneBinding = true;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800515 if (DEBUG_LOADERS) {
516 Log.d(TAG, "done with workspace");
517 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400518 LoaderThread.this.notify();
519 }
520 }
521 });
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800522 if (DEBUG_LOADERS) {
523 Log.d(TAG, "waiting to be done with workspace");
524 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525 while (!mStopped && !mWorkspaceDoneBinding) {
526 try {
527 this.wait();
528 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800529 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 }
531 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800532 if (DEBUG_LOADERS) {
533 Log.d(TAG, "done waiting to be done with workspace");
534 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 }
536
537 // Load all apps if they're dirty
538 int allAppsSeq;
539 boolean allAppsDirty;
540 synchronized (mLock) {
541 allAppsSeq = mAllAppsSeq;
542 allAppsDirty = mAllAppsSeq != mLastAllAppsSeq;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800543 if (DEBUG_LOADERS) {
544 Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq
545 + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty");
546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 }
548 if (allAppsDirty) {
549 loadAllApps();
550 }
551 synchronized (mLock) {
552 // If we're not stopped, and nobody has incremented mAllAppsSeq.
553 if (mStopped) {
554 return;
555 }
556 if (allAppsSeq == mAllAppsSeq) {
557 mLastAllAppsSeq = mAllAppsSeq;
558 }
559 }
560
561 // Bind all apps
Joe Onorato34b02492009-10-14 11:13:48 -0700562 if (allAppsDirty) {
563 bindAllApps();
564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565
566 // Clear out this reference, otherwise we end up holding it until all of the
567 // callback runnables are done.
568 mContext = null;
569
570 synchronized (mLock) {
571 // Setting the reference is atomic, but we can't do it inside the other critical
572 // sections.
573 mLoaderThread = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 }
575 }
576
577 public void stopLocked() {
578 synchronized (LoaderThread.this) {
579 mStopped = true;
580 this.notify();
581 }
582 }
583
584 /**
585 * Gets the callbacks object. If we've been stopped, or if the launcher object
586 * has somehow been garbage collected, return null instead.
587 */
588 Callbacks tryGetCallbacks() {
589 synchronized (mLock) {
590 if (mStopped) {
591 return null;
592 }
593
594 final Callbacks callbacks = mCallbacks.get();
595 if (callbacks == null) {
596 Log.w(TAG, "no mCallbacks");
597 return null;
598 }
599
600 return callbacks;
601 }
602 }
603
604 private void loadWorkspace() {
605 long t = SystemClock.uptimeMillis();
606
607 final Context context = mContext;
608 final ContentResolver contentResolver = context.getContentResolver();
609 final PackageManager manager = context.getPackageManager();
610
611 /* TODO
612 if (mLocaleChanged) {
613 updateShortcutLabels(contentResolver, manager);
614 }
615 */
616
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400617 mItems.clear();
Joe Onorato511ab642009-11-08 14:14:07 -0500618 mAppWidgets.clear();
Joe Onorato1db7a972009-11-16 18:32:22 -0800619 mFolders.clear();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400620
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 final Cursor c = contentResolver.query(
622 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
623
624 try {
625 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
626 final int intentIndex = c.getColumnIndexOrThrow
627 (LauncherSettings.Favorites.INTENT);
628 final int titleIndex = c.getColumnIndexOrThrow
629 (LauncherSettings.Favorites.TITLE);
630 final int iconTypeIndex = c.getColumnIndexOrThrow(
631 LauncherSettings.Favorites.ICON_TYPE);
632 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
633 final int iconPackageIndex = c.getColumnIndexOrThrow(
634 LauncherSettings.Favorites.ICON_PACKAGE);
635 final int iconResourceIndex = c.getColumnIndexOrThrow(
636 LauncherSettings.Favorites.ICON_RESOURCE);
637 final int containerIndex = c.getColumnIndexOrThrow(
638 LauncherSettings.Favorites.CONTAINER);
639 final int itemTypeIndex = c.getColumnIndexOrThrow(
640 LauncherSettings.Favorites.ITEM_TYPE);
641 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
642 LauncherSettings.Favorites.APPWIDGET_ID);
643 final int screenIndex = c.getColumnIndexOrThrow(
644 LauncherSettings.Favorites.SCREEN);
645 final int cellXIndex = c.getColumnIndexOrThrow
646 (LauncherSettings.Favorites.CELLX);
647 final int cellYIndex = c.getColumnIndexOrThrow
648 (LauncherSettings.Favorites.CELLY);
649 final int spanXIndex = c.getColumnIndexOrThrow
650 (LauncherSettings.Favorites.SPANX);
651 final int spanYIndex = c.getColumnIndexOrThrow(
652 LauncherSettings.Favorites.SPANY);
653 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
654 final int displayModeIndex = c.getColumnIndexOrThrow(
655 LauncherSettings.Favorites.DISPLAY_MODE);
656
657 ApplicationInfo info;
658 String intentDescription;
659 Widget widgetInfo;
660 LauncherAppWidgetInfo appWidgetInfo;
661 int container;
662 long id;
663 Intent intent;
664
665 while (!mStopped && c.moveToNext()) {
666 try {
667 int itemType = c.getInt(itemTypeIndex);
668
669 switch (itemType) {
670 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
671 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
672 intentDescription = c.getString(intentIndex);
673 try {
674 intent = Intent.parseUri(intentDescription, 0);
675 } catch (URISyntaxException e) {
676 continue;
677 }
678
679 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
680 info = getApplicationInfo(manager, intent, context);
681 } else {
682 info = getApplicationInfoShortcut(c, context, iconTypeIndex,
683 iconPackageIndex, iconResourceIndex, iconIndex);
684 }
685
686 if (info == null) {
687 info = new ApplicationInfo();
688 info.icon = manager.getDefaultActivityIcon();
689 }
690
691 if (info != null) {
Joe Onorato028b6242009-11-10 18:26:13 -0800692 if (itemType
693 != LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
694 info.title = c.getString(titleIndex);
695 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 info.intent = intent;
697
698 info.id = c.getLong(idIndex);
699 container = c.getInt(containerIndex);
700 info.container = container;
701 info.screen = c.getInt(screenIndex);
702 info.cellX = c.getInt(cellXIndex);
703 info.cellY = c.getInt(cellYIndex);
704
705 switch (container) {
706 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
707 mItems.add(info);
708 break;
709 default:
710 // Item is in a user folder
711 UserFolderInfo folderInfo =
Joe Onoratoad72e172009-11-06 16:25:04 -0500712 findOrMakeUserFolder(mFolders, container);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 folderInfo.add(info);
714 break;
715 }
716 }
717 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718
Joe Onoratoad72e172009-11-06 16:25:04 -0500719 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 id = c.getLong(idIndex);
Joe Onoratoad72e172009-11-06 16:25:04 -0500721 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722
723 folderInfo.title = c.getString(titleIndex);
724
725 folderInfo.id = id;
726 container = c.getInt(containerIndex);
727 folderInfo.container = container;
728 folderInfo.screen = c.getInt(screenIndex);
729 folderInfo.cellX = c.getInt(cellXIndex);
730 folderInfo.cellY = c.getInt(cellYIndex);
731
732 switch (container) {
733 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
734 mItems.add(folderInfo);
735 break;
736 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500737
738 mFolders.put(folderInfo.id, folderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500740
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
742
743 id = c.getLong(idIndex);
Joe Onoratoad72e172009-11-06 16:25:04 -0500744 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745
746 intentDescription = c.getString(intentIndex);
747 intent = null;
748 if (intentDescription != null) {
749 try {
750 intent = Intent.parseUri(intentDescription, 0);
751 } catch (URISyntaxException e) {
752 // Ignore, a live folder might not have a base intent
753 }
754 }
755
756 liveFolderInfo.title = c.getString(titleIndex);
757 liveFolderInfo.id = id;
758 container = c.getInt(containerIndex);
759 liveFolderInfo.container = container;
760 liveFolderInfo.screen = c.getInt(screenIndex);
761 liveFolderInfo.cellX = c.getInt(cellXIndex);
762 liveFolderInfo.cellY = c.getInt(cellYIndex);
763 liveFolderInfo.uri = Uri.parse(c.getString(uriIndex));
764 liveFolderInfo.baseIntent = intent;
765 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
766
767 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
768 iconResourceIndex, liveFolderInfo);
769
770 switch (container) {
771 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
772 mItems.add(liveFolderInfo);
773 break;
774 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500775 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500777
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
779 widgetInfo = Widget.makeSearch();
780
781 container = c.getInt(containerIndex);
782 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
783 Log.e(TAG, "Widget found where container "
784 + "!= CONTAINER_DESKTOP ignoring!");
785 continue;
786 }
787
788 widgetInfo.id = c.getLong(idIndex);
789 widgetInfo.screen = c.getInt(screenIndex);
790 widgetInfo.container = container;
791 widgetInfo.cellX = c.getInt(cellXIndex);
792 widgetInfo.cellY = c.getInt(cellYIndex);
793
794 mItems.add(widgetInfo);
795 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500796
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
798 // Read all Launcher-specific widget details
799 int appWidgetId = c.getInt(appWidgetIdIndex);
800 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
801 appWidgetInfo.id = c.getLong(idIndex);
802 appWidgetInfo.screen = c.getInt(screenIndex);
803 appWidgetInfo.cellX = c.getInt(cellXIndex);
804 appWidgetInfo.cellY = c.getInt(cellYIndex);
805 appWidgetInfo.spanX = c.getInt(spanXIndex);
806 appWidgetInfo.spanY = c.getInt(spanYIndex);
807
808 container = c.getInt(containerIndex);
809 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
810 Log.e(TAG, "Widget found where container "
811 + "!= CONTAINER_DESKTOP -- ignoring!");
812 continue;
813 }
814 appWidgetInfo.container = c.getInt(containerIndex);
815
816 mAppWidgets.add(appWidgetInfo);
817 break;
818 }
819 } catch (Exception e) {
820 Log.w(TAG, "Desktop items loading interrupted:", e);
821 }
822 }
823 } finally {
824 c.close();
825 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800826 if (DEBUG_LOADERS) {
827 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 }
830
831 /**
832 * Read everything out of our database.
833 */
834 private void bindWorkspace() {
835 final long t = SystemClock.uptimeMillis();
836
837 // Don't use these two variables in any of the callback runnables.
838 // Otherwise we hold a reference to them.
839 Callbacks callbacks = mCallbacks.get();
840 if (callbacks == null) {
841 // This launcher has exited and nobody bothered to tell us. Just bail.
842 Log.w(TAG, "LoaderThread running with no launcher");
843 return;
844 }
845
846 int N;
847 // Tell the workspace that we're about to start firing items at it
848 mHandler.post(new Runnable() {
849 public void run() {
850 Callbacks callbacks = tryGetCallbacks();
851 if (callbacks != null) {
852 callbacks.startBinding();
853 }
854 }
855 });
856 // Add the items to the workspace.
857 N = mItems.size();
858 for (int i=0; i<N; i+=ITEMS_CHUNK) {
859 final int start = i;
860 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
861 mHandler.post(new Runnable() {
862 public void run() {
863 Callbacks callbacks = tryGetCallbacks();
864 if (callbacks != null) {
865 callbacks.bindItems(mItems, start, start+chunkSize);
866 }
867 }
868 });
869 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500870 mHandler.post(new Runnable() {
871 public void run() {
872 Callbacks callbacks = tryGetCallbacks();
873 if (callbacks != null) {
874 callbacks.bindFolders(mFolders);
875 }
876 }
877 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 // Wait until the queue goes empty.
879 mHandler.postIdle(new Runnable() {
880 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800881 if (DEBUG_LOADERS) {
882 Log.d(TAG, "Going to start binding widgets soon.");
883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 }
885 });
886 // Bind the widgets, one at a time.
887 // WARNING: this is calling into the workspace from the background thread,
888 // but since getCurrentScreen() just returns the int, we should be okay. This
889 // is just a hint for the order, and if it's wrong, we'll be okay.
890 // TODO: instead, we should have that push the current screen into here.
891 final int currentScreen = callbacks.getCurrentWorkspaceScreen();
892 N = mAppWidgets.size();
893 // once for the current screen
894 for (int i=0; i<N; i++) {
895 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
896 if (widget.screen == currentScreen) {
897 mHandler.post(new Runnable() {
898 public void run() {
899 Callbacks callbacks = tryGetCallbacks();
900 if (callbacks != null) {
901 callbacks.bindAppWidget(widget);
902 }
903 }
904 });
905 }
906 }
907 // once for the other screens
908 for (int i=0; i<N; i++) {
909 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
910 if (widget.screen != currentScreen) {
911 mHandler.post(new Runnable() {
912 public void run() {
913 Callbacks callbacks = tryGetCallbacks();
914 if (callbacks != null) {
915 callbacks.bindAppWidget(widget);
916 }
917 }
918 });
919 }
920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 // Tell the workspace that we're done.
922 mHandler.post(new Runnable() {
923 public void run() {
924 Callbacks callbacks = tryGetCallbacks();
925 if (callbacks != null) {
926 callbacks.finishBindingItems();
927 }
928 }
929 });
930 // If we're profiling, this is the last thing in the queue.
931 mHandler.post(new Runnable() {
932 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800933 if (DEBUG_LOADERS) {
934 Log.d(TAG, "bound workspace in "
935 + (SystemClock.uptimeMillis()-t) + "ms");
936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 if (Launcher.PROFILE_ROTATE) {
938 android.os.Debug.stopMethodTracing();
939 }
940 }
941 });
942 }
943
944 private void loadAllApps() {
945 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
946 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
947
948 final Callbacks callbacks = tryGetCallbacks();
949 if (callbacks == null) {
950 return;
951 }
952
953 final Context context = mContext;
954 final PackageManager packageManager = context.getPackageManager();
955
956 final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
957
958 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400959 mBeforeFirstLoad = false;
960
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961 mAllAppsList.clear();
962 if (apps != null) {
963 long t = SystemClock.uptimeMillis();
964
965 int N = apps.size();
966 Utilities.BubbleText bubble = new Utilities.BubbleText(context);
967 for (int i=0; i<N && !mStopped; i++) {
968 // This builds the icon bitmaps.
969 mAllAppsList.add(AppInfoCache.cache(apps.get(i), context, bubble));
970 }
971 Collections.sort(mAllAppsList.data, sComparator);
972 Collections.sort(mAllAppsList.added, sComparator);
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800973 if (DEBUG_LOADERS) {
974 Log.d(TAG, "cached app icons in "
975 + (SystemClock.uptimeMillis()-t) + "ms");
976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 }
978 }
979 }
980
981 private void bindAllApps() {
982 synchronized (mLock) {
983 final ArrayList<ApplicationInfo> results = mAllAppsList.added;
Romain Guy84f296c2009-11-04 15:00:44 -0800984 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 mHandler.post(new Runnable() {
986 public void run() {
Joe Onorato34b02492009-10-14 11:13:48 -0700987 final long t = SystemClock.uptimeMillis();
988 final int count = results.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989
990 Callbacks callbacks = tryGetCallbacks();
991 if (callbacks != null) {
992 callbacks.bindAllApplications(results);
993 }
994
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800995 if (DEBUG_LOADERS) {
996 Log.d(TAG, "bound app " + count + " icons in "
997 + (SystemClock.uptimeMillis()-t) + "ms");
998 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 }
1000 });
1001 }
1002 }
Joe Onoratobe386092009-11-17 17:32:16 -08001003
1004 public void dumpState() {
1005 Log.d(TAG, "mLoader.mLoaderThread.mContext=" + mContext);
1006 Log.d(TAG, "mLoader.mLoaderThread.mWaitThread=" + mWaitThread);
1007 Log.d(TAG, "mLoader.mLoaderThread.mIsLaunching=" + mIsLaunching);
1008 Log.d(TAG, "mLoader.mLoaderThread.mStopped=" + mStopped);
1009 Log.d(TAG, "mLoader.mLoaderThread.mWorkspaceDoneBinding=" + mWorkspaceDoneBinding);
1010 }
1011 }
1012
1013 public void dumpState() {
1014 Log.d(TAG, "mLoader.mLastWorkspaceSeq=" + mLoader.mLastWorkspaceSeq);
1015 Log.d(TAG, "mLoader.mWorkspaceSeq=" + mLoader.mWorkspaceSeq);
1016 Log.d(TAG, "mLoader.mLastAllAppsSeq=" + mLoader.mLastAllAppsSeq);
1017 Log.d(TAG, "mLoader.mAllAppsSeq=" + mLoader.mAllAppsSeq);
1018 Log.d(TAG, "mLoader.mItems size=" + mLoader.mItems.size());
1019 if (mLoaderThread != null) {
1020 mLoaderThread.dumpState();
1021 } else {
1022 Log.d(TAG, "mLoader.mLoaderThread=null");
1023 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 }
1025 }
1026
1027 /**
1028 * Make an ApplicationInfo object for an application.
1029 */
1030 private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent,
1031 Context context) {
1032 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1033
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001034 if (resolveInfo == null) {
1035 return null;
1036 }
1037
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 final ApplicationInfo info = new ApplicationInfo();
1039 final ActivityInfo activityInfo = resolveInfo.activityInfo;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001040 info.icon = Utilities.createIconThumbnail(activityInfo.loadIcon(manager), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 if (info.title == null || info.title.length() == 0) {
1042 info.title = activityInfo.loadLabel(manager);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 if (info.title == null) {
1045 info.title = "";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1048 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 * Make an ApplicationInfo object for a sortcut
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 private static ApplicationInfo getApplicationInfoShortcut(Cursor c, Context context,
1055 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 final ApplicationInfo info = new ApplicationInfo();
1058 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060 int iconType = c.getInt(iconTypeIndex);
1061 switch (iconType) {
1062 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1063 String packageName = c.getString(iconPackageIndex);
1064 String resourceName = c.getString(iconResourceIndex);
1065 PackageManager packageManager = context.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067 Resources resources = packageManager.getResourcesForApplication(packageName);
1068 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001069 info.icon = Utilities.createIconThumbnail(resources.getDrawable(id), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 } catch (Exception e) {
1071 info.icon = packageManager.getDefaultActivityIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 info.iconResource = new Intent.ShortcutIconResource();
1074 info.iconResource.packageName = packageName;
1075 info.iconResource.resourceName = resourceName;
1076 info.customIcon = false;
1077 break;
1078 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
1079 byte[] data = c.getBlob(iconIndex);
1080 try {
1081 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
1082 info.icon = new FastBitmapDrawable(
1083 Utilities.createBitmapThumbnail(bitmap, context));
1084 } catch (Exception e) {
1085 packageManager = context.getPackageManager();
1086 info.icon = packageManager.getDefaultActivityIcon();
1087 }
1088 info.filtered = true;
1089 info.customIcon = true;
1090 break;
1091 default:
1092 info.icon = context.getPackageManager().getDefaultActivityIcon();
1093 info.customIcon = false;
1094 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 return info;
1097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1100 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1101
1102 int iconType = c.getInt(iconTypeIndex);
1103 switch (iconType) {
1104 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1105 String packageName = c.getString(iconPackageIndex);
1106 String resourceName = c.getString(iconResourceIndex);
1107 PackageManager packageManager = context.getPackageManager();
1108 try {
1109 Resources resources = packageManager.getResourcesForApplication(packageName);
1110 final int id = resources.getIdentifier(resourceName, null, null);
1111 liveFolderInfo.icon = resources.getDrawable(id);
1112 } catch (Exception e) {
1113 liveFolderInfo.icon =
1114 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1115 }
1116 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1117 liveFolderInfo.iconResource.packageName = packageName;
1118 liveFolderInfo.iconResource.resourceName = resourceName;
1119 break;
1120 default:
1121 liveFolderInfo.icon =
1122 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1123 }
1124 }
1125
1126 /**
1127 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1128 * or make a new one.
1129 */
1130 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1131 // See if a placeholder was created for us already
1132 FolderInfo folderInfo = folders.get(id);
1133 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1134 // No placeholder -- create a new instance
1135 folderInfo = new UserFolderInfo();
1136 folders.put(id, folderInfo);
1137 }
1138 return (UserFolderInfo) folderInfo;
1139 }
1140
1141 /**
1142 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1143 * new one.
1144 */
1145 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1146 // See if a placeholder was created for us already
1147 FolderInfo folderInfo = folders.get(id);
1148 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1149 // No placeholder -- create a new instance
1150 folderInfo = new LiveFolderInfo();
1151 folders.put(id, folderInfo);
1152 }
1153 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155
1156 private static void updateShortcutLabels(ContentResolver resolver, PackageManager manager) {
1157 final Cursor c = resolver.query(LauncherSettings.Favorites.CONTENT_URI,
Romain Guy73b979d2009-06-09 12:57:21 -07001158 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.TITLE,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 LauncherSettings.Favorites.INTENT, LauncherSettings.Favorites.ITEM_TYPE },
1160 null, null, null);
1161
Romain Guy73b979d2009-06-09 12:57:21 -07001162 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1164 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1165 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1166
1167 // boolean changed = false;
1168
1169 try {
1170 while (c.moveToNext()) {
1171 try {
1172 if (c.getInt(itemTypeIndex) !=
1173 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1174 continue;
1175 }
1176
1177 final String intentUri = c.getString(intentIndex);
1178 if (intentUri != null) {
Romain Guy1ce1a242009-06-23 17:34:54 -07001179 final Intent shortcut = Intent.parseUri(intentUri, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 if (Intent.ACTION_MAIN.equals(shortcut.getAction())) {
1181 final ComponentName name = shortcut.getComponent();
1182 if (name != null) {
1183 final ActivityInfo activityInfo = manager.getActivityInfo(name, 0);
1184 final String title = c.getString(titleIndex);
1185 String label = getLabel(manager, activityInfo);
1186
1187 if (title == null || !title.equals(label)) {
1188 final ContentValues values = new ContentValues();
1189 values.put(LauncherSettings.Favorites.TITLE, label);
1190
Romain Guyfedc4fc2009-03-27 20:48:20 -07001191 resolver.update(
1192 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 values, "_id=?",
1194 new String[] { String.valueOf(c.getLong(idIndex)) });
1195
1196 // changed = true;
1197 }
1198 }
1199 }
1200 }
1201 } catch (URISyntaxException e) {
1202 // Ignore
1203 } catch (PackageManager.NameNotFoundException e) {
1204 // Ignore
1205 }
1206 }
1207 } finally {
1208 c.close();
1209 }
1210
1211 // if (changed) resolver.notifyChange(Settings.Favorites.CONTENT_URI, null);
1212 }
1213
1214 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1215 String label = activityInfo.loadLabel(manager).toString();
1216 if (label == null) {
1217 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1218 if (label == null) {
1219 label = activityInfo.name;
1220 }
1221 }
1222 return label;
1223 }
1224
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 private static final Collator sCollator = Collator.getInstance();
1226 private static final Comparator<ApplicationInfo> sComparator
1227 = new Comparator<ApplicationInfo>() {
1228 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1229 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001231 };
Joe Onoratobe386092009-11-17 17:32:16 -08001232
1233 public void dumpState() {
1234 Log.d(TAG, "mBeforeFirstLoad=" + mBeforeFirstLoad);
1235 Log.d(TAG, "mCallbacks=" + mCallbacks);
1236 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1237 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1238 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1239 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
1240 mLoader.dumpState();
1241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242}