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