blob: 29de3f7ccbfc2fd37798788f2a51a5c1ed3f2d9a [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;
Romain Guy5c16f3e2010-01-12 17:24:58 -080021import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.ContentResolver;
23import android.content.ContentValues;
24import android.content.Intent;
25import android.content.Context;
26import android.content.pm.ActivityInfo;
27import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080028import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.ResolveInfo;
30import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Romain Guy5c16f3e2010-01-12 17:24:58 -080035import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040036import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040038import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039
Joe Onorato9c1289c2009-08-17 11:03:03 -040040import java.lang.ref.WeakReference;
41import java.net.URISyntaxException;
42import java.text.Collator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import java.util.Comparator;
45import java.util.Collections;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import java.util.HashMap;
47import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
49/**
50 * Maintains in-memory state of the Launcher. It is expected that there should be only one
51 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070052 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040054public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080055 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040056 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070057
Joe Onoratof99f8c12009-10-31 17:27:36 -040058 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040059 private final Object mLock = new Object();
60 private DeferredHandler mHandler = new DeferredHandler();
61 private Loader mLoader = new Loader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
Joe Onoratof99f8c12009-10-31 17:27:36 -040063 private boolean mBeforeFirstLoad = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -040064 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 private AllAppsList mAllAppsList = new AllAppsList();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Joe Onorato9c1289c2009-08-17 11:03:03 -040068 public interface Callbacks {
69 public int getCurrentWorkspaceScreen();
70 public void startBinding();
71 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -050072 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -040073 public void finishBindingItems();
74 public void bindAppWidget(LauncherAppWidgetInfo info);
75 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
76 public void bindPackageAdded(ArrayList<ApplicationInfo> apps);
77 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps);
78 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps);
79 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Joe Onoratof99f8c12009-10-31 17:27:36 -040081 LauncherModel(LauncherApplication app) {
82 mApp = app;
83 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Joe Onorato9c1289c2009-08-17 11:03:03 -040085 /**
86 * Adds an item to the DB if it was not created previously, or move it to a new
87 * <container, screen, cellX, cellY>
88 */
89 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
90 int screen, int cellX, int cellY) {
91 if (item.container == ItemInfo.NO_ID) {
92 // From all apps
93 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
94 } else {
95 // From somewhere else
96 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 }
98 }
99
100 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700102 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
104 int cellX, int cellY) {
105 item.container = container;
106 item.screen = screen;
107 item.cellX = cellX;
108 item.cellY = cellY;
109
110 final ContentValues values = new ContentValues();
111 final ContentResolver cr = context.getContentResolver();
112
113 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
114 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
115 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
116 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
117
118 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700119 }
120
121 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 * Returns true if the shortcuts already exists in the database.
123 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static boolean shortcutExists(Context context, String title, Intent intent) {
126 final ContentResolver cr = context.getContentResolver();
127 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
128 new String[] { "title", "intent" }, "title=? and intent=?",
129 new String[] { title, intent.toUri(0) }, null);
130 boolean result = false;
131 try {
132 result = c.moveToFirst();
133 } finally {
134 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400136 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700137 }
138
Joe Onorato9c1289c2009-08-17 11:03:03 -0400139 /**
140 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
141 */
142 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
143 final ContentResolver cr = context.getContentResolver();
144 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
145 "_id=? and (itemType=? or itemType=?)",
146 new String[] { String.valueOf(id),
147 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
148 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700149
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 try {
151 if (c.moveToFirst()) {
152 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
153 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
154 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
155 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
156 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
157 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 FolderInfo folderInfo = null;
160 switch (c.getInt(itemTypeIndex)) {
161 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
162 folderInfo = findOrMakeUserFolder(folderList, id);
163 break;
164 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
165 folderInfo = findOrMakeLiveFolder(folderList, id);
166 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700167 }
168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 folderInfo.title = c.getString(titleIndex);
170 folderInfo.id = id;
171 folderInfo.container = c.getInt(containerIndex);
172 folderInfo.screen = c.getInt(screenIndex);
173 folderInfo.cellX = c.getInt(cellXIndex);
174 folderInfo.cellY = c.getInt(cellYIndex);
175
176 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 } finally {
179 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700180 }
181
182 return null;
183 }
184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 /**
186 * Add an item to the database in a specified container. Sets the container, screen, cellX and
187 * cellY fields of the item. Also assigns an ID to the item.
188 */
189 static void addItemToDatabase(Context context, ItemInfo item, long container,
190 int screen, int cellX, int cellY, boolean notify) {
191 item.container = container;
192 item.screen = screen;
193 item.cellX = cellX;
194 item.cellY = cellY;
195
196 final ContentValues values = new ContentValues();
197 final ContentResolver cr = context.getContentResolver();
198
199 item.onAddToDatabase(values);
200
201 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
202 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
203
204 if (result != null) {
205 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700206 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700207 }
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 /**
210 * Update an item to the database in a specified container.
211 */
212 static void updateItemInDatabase(Context context, ItemInfo item) {
213 final ContentValues values = new ContentValues();
214 final ContentResolver cr = context.getContentResolver();
215
216 item.onAddToDatabase(values);
217
218 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
219 }
220
221 /**
222 * Removes the specified item from the database
223 * @param context
224 * @param item
225 */
226 static void deleteItemFromDatabase(Context context, ItemInfo item) {
227 final ContentResolver cr = context.getContentResolver();
228
229 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
230 }
231
232 /**
233 * Remove the contents of the specified folder from the database
234 */
235 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
236 final ContentResolver cr = context.getContentResolver();
237
238 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
239 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
240 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
241 }
242
243 /**
244 * Set this as the current Launcher activity object for the loader.
245 */
246 public void initialize(Callbacks callbacks) {
247 synchronized (mLock) {
248 mCallbacks = new WeakReference<Callbacks>(callbacks);
249 }
250 }
251
252 public void startLoader(Context context, boolean isLaunching) {
253 mLoader.startLoader(context, isLaunching);
254 }
255
256 public void stopLoader() {
257 mLoader.stopLoader();
258 }
259
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700260 /**
261 * We pick up most of the changes to all apps.
262 */
263 public void setAllAppsDirty() {
264 mLoader.setAllAppsDirty();
265 }
266
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267 public void setWorkspaceDirty() {
268 mLoader.setWorkspaceDirty();
269 }
270
271 /**
272 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
273 * ACTION_PACKAGE_CHANGED.
274 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400275 public void onReceive(Context context, Intent intent) {
276 // Use the app as the context.
277 context = mApp;
278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 final String packageName = intent.getData().getSchemeSpecificPart();
280
281 ArrayList<ApplicationInfo> added = null;
282 ArrayList<ApplicationInfo> removed = null;
283 ArrayList<ApplicationInfo> modified = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284
285 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400286 if (mBeforeFirstLoad) {
287 // If we haven't even loaded yet, don't bother, since we'll just pick
288 // up the changes.
289 return;
290 }
291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 final String action = intent.getAction();
293 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
294
295 if (packageName == null || packageName.length() == 0) {
296 // they sent us a bad intent
297 return;
298 }
299
300 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
301 mAllAppsList.updatePackage(context, packageName);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
303 if (!replacing) {
304 mAllAppsList.removePackage(packageName);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 }
306 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
307 // later, we will update the package at this time
308 } else {
309 if (!replacing) {
310 mAllAppsList.addPackage(context, packageName);
311 } else {
312 mAllAppsList.updatePackage(context, packageName);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400313 }
314 }
315
316 if (mAllAppsList.added.size() > 0) {
317 added = mAllAppsList.added;
Romain Guy84f296c2009-11-04 15:00:44 -0800318 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400319 }
320 if (mAllAppsList.removed.size() > 0) {
321 removed = mAllAppsList.removed;
Romain Guy84f296c2009-11-04 15:00:44 -0800322 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400323 for (ApplicationInfo info: removed) {
324 AppInfoCache.remove(info.intent.getComponent());
325 }
326 }
327 if (mAllAppsList.modified.size() > 0) {
328 modified = mAllAppsList.modified;
Romain Guy84f296c2009-11-04 15:00:44 -0800329 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400330 }
331
Marco Nelissen3c8b90d2009-09-11 14:49:50 -0700332 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400333 if (callbacks == null) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800334 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400335 return;
336 }
337
338 if (added != null) {
339 final ArrayList<ApplicationInfo> addedFinal = added;
340 mHandler.post(new Runnable() {
341 public void run() {
342 callbacks.bindPackageAdded(addedFinal);
343 }
344 });
345 }
Joe Onorato418928e2009-11-19 18:05:36 -0800346 if (modified != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400347 final ArrayList<ApplicationInfo> modifiedFinal = modified;
348 mHandler.post(new Runnable() {
349 public void run() {
350 callbacks.bindPackageUpdated(packageName, modifiedFinal);
351 }
352 });
353 }
Joe Onorato418928e2009-11-19 18:05:36 -0800354 if (removed != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 final ArrayList<ApplicationInfo> removedFinal = removed;
356 mHandler.post(new Runnable() {
357 public void run() {
358 callbacks.bindPackageRemoved(packageName, removedFinal);
359 }
360 });
361 }
362 }
363 }
364
365 public class Loader {
366 private static final int ITEMS_CHUNK = 6;
367
368 private LoaderThread mLoaderThread;
369
370 private int mLastWorkspaceSeq = 0;
371 private int mWorkspaceSeq = 1;
372
373 private int mLastAllAppsSeq = 0;
374 private int mAllAppsSeq = 1;
375
Romain Guy84f296c2009-11-04 15:00:44 -0800376 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
377 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
Joe Onoratoad72e172009-11-06 16:25:04 -0500378 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379
380 /**
381 * Call this from the ui thread so the handler is initialized on the correct thread.
382 */
383 public Loader() {
384 }
385
386 public void startLoader(Context context, boolean isLaunching) {
387 synchronized (mLock) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800388 if (DEBUG_LOADERS) {
389 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 // Don't bother to start the thread if we know it's not going to do anything
392 if (mCallbacks.get() != null) {
393 LoaderThread oldThread = mLoaderThread;
394 if (oldThread != null) {
395 if (oldThread.isLaunching()) {
396 // don't downgrade isLaunching if we're already running
397 isLaunching = true;
398 }
399 oldThread.stopLocked();
400 }
401 mLoaderThread = new LoaderThread(context, oldThread, isLaunching);
402 mLoaderThread.start();
403 }
404 }
405 }
406
407 public void stopLoader() {
408 synchronized (mLock) {
409 if (mLoaderThread != null) {
410 mLoaderThread.stopLocked();
411 }
412 }
413 }
414
415 public void setWorkspaceDirty() {
416 synchronized (mLock) {
417 mWorkspaceSeq++;
418 }
419 }
420
421 public void setAllAppsDirty() {
422 synchronized (mLock) {
423 mAllAppsSeq++;
424 }
425 }
426
427 /**
428 * Runnable for the thread that loads the contents of the launcher:
429 * - workspace icons
430 * - widgets
431 * - all apps icons
432 */
433 private class LoaderThread extends Thread {
434 private Context mContext;
435 private Thread mWaitThread;
436 private boolean mIsLaunching;
437 private boolean mStopped;
438 private boolean mWorkspaceDoneBinding;
439
440 LoaderThread(Context context, Thread waitThread, boolean isLaunching) {
441 mContext = context;
442 mWaitThread = waitThread;
443 mIsLaunching = isLaunching;
444 }
445
446 boolean isLaunching() {
447 return mIsLaunching;
448 }
449
450 /**
451 * If another LoaderThread was supplied, we need to wait for that to finish before
452 * we start our processing. This keeps the ordering of the setting and clearing
453 * of the dirty flags correct by making sure we don't start processing stuff until
454 * they've had a chance to re-set them. We do this waiting the worker thread, not
455 * the ui thread to avoid ANRs.
456 */
457 private void waitForOtherThread() {
458 if (mWaitThread != null) {
459 boolean done = false;
460 while (!done) {
461 try {
462 mWaitThread.join();
Joe Onoratoefabe002009-08-28 09:38:18 -0700463 done = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400464 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800465 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400466 }
467 }
468 mWaitThread = null;
469 }
470 }
471
472 public void run() {
473 waitForOtherThread();
474
475 // Elevate priority when Home launches for the first time to avoid
476 // starving at boot time. Staring at a blank home is not cool.
477 synchronized (mLock) {
478 android.os.Process.setThreadPriority(mIsLaunching
479 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
480 }
481
482 // Load the workspace only if it's dirty.
483 int workspaceSeq;
484 boolean workspaceDirty;
485 synchronized (mLock) {
486 workspaceSeq = mWorkspaceSeq;
487 workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq;
488 }
489 if (workspaceDirty) {
490 loadWorkspace();
491 }
492 synchronized (mLock) {
493 // If we're not stopped, and nobody has incremented mWorkspaceSeq.
494 if (mStopped) {
495 return;
496 }
497 if (workspaceSeq == mWorkspaceSeq) {
498 mLastWorkspaceSeq = mWorkspaceSeq;
499 }
500 }
501
502 // Bind the workspace
503 bindWorkspace();
504
505 // Wait until the either we're stopped or the other threads are done.
506 // This way we don't start loading all apps until the workspace has settled
507 // down.
508 synchronized (LoaderThread.this) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500509 mHandler.postIdle(new Runnable() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400510 public void run() {
511 synchronized (LoaderThread.this) {
512 mWorkspaceDoneBinding = true;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800513 if (DEBUG_LOADERS) {
514 Log.d(TAG, "done with workspace");
515 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 LoaderThread.this.notify();
517 }
518 }
519 });
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800520 if (DEBUG_LOADERS) {
521 Log.d(TAG, "waiting to be done with workspace");
522 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400523 while (!mStopped && !mWorkspaceDoneBinding) {
524 try {
525 this.wait();
526 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800527 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400528 }
529 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800530 if (DEBUG_LOADERS) {
531 Log.d(TAG, "done waiting to be done with workspace");
532 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 }
534
535 // Load all apps if they're dirty
536 int allAppsSeq;
537 boolean allAppsDirty;
538 synchronized (mLock) {
539 allAppsSeq = mAllAppsSeq;
540 allAppsDirty = mAllAppsSeq != mLastAllAppsSeq;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800541 if (DEBUG_LOADERS) {
542 Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq
543 + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty");
544 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400545 }
546 if (allAppsDirty) {
547 loadAllApps();
548 }
549 synchronized (mLock) {
550 // If we're not stopped, and nobody has incremented mAllAppsSeq.
551 if (mStopped) {
552 return;
553 }
554 if (allAppsSeq == mAllAppsSeq) {
555 mLastAllAppsSeq = mAllAppsSeq;
556 }
557 }
558
559 // Bind all apps
Joe Onorato34b02492009-10-14 11:13:48 -0700560 if (allAppsDirty) {
561 bindAllApps();
562 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563
564 // Clear out this reference, otherwise we end up holding it until all of the
565 // callback runnables are done.
566 mContext = null;
567
568 synchronized (mLock) {
569 // Setting the reference is atomic, but we can't do it inside the other critical
570 // sections.
571 mLoaderThread = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572 }
573 }
574
575 public void stopLocked() {
576 synchronized (LoaderThread.this) {
577 mStopped = true;
578 this.notify();
579 }
580 }
581
582 /**
583 * Gets the callbacks object. If we've been stopped, or if the launcher object
584 * has somehow been garbage collected, return null instead.
585 */
586 Callbacks tryGetCallbacks() {
587 synchronized (mLock) {
588 if (mStopped) {
589 return null;
590 }
591
592 final Callbacks callbacks = mCallbacks.get();
593 if (callbacks == null) {
594 Log.w(TAG, "no mCallbacks");
595 return null;
596 }
597
598 return callbacks;
599 }
600 }
601
602 private void loadWorkspace() {
603 long t = SystemClock.uptimeMillis();
604
605 final Context context = mContext;
606 final ContentResolver contentResolver = context.getContentResolver();
607 final PackageManager manager = context.getPackageManager();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800608 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400609
610 /* TODO
611 if (mLocaleChanged) {
612 updateShortcutLabels(contentResolver, manager);
613 }
614 */
615
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400616 mItems.clear();
Joe Onorato511ab642009-11-08 14:14:07 -0500617 mAppWidgets.clear();
Joe Onorato1db7a972009-11-16 18:32:22 -0800618 mFolders.clear();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400619
Romain Guy5c16f3e2010-01-12 17:24:58 -0800620 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
621
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622 final Cursor c = contentResolver.query(
623 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
624
625 try {
626 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
627 final int intentIndex = c.getColumnIndexOrThrow
628 (LauncherSettings.Favorites.INTENT);
629 final int titleIndex = c.getColumnIndexOrThrow
630 (LauncherSettings.Favorites.TITLE);
631 final int iconTypeIndex = c.getColumnIndexOrThrow(
632 LauncherSettings.Favorites.ICON_TYPE);
633 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
634 final int iconPackageIndex = c.getColumnIndexOrThrow(
635 LauncherSettings.Favorites.ICON_PACKAGE);
636 final int iconResourceIndex = c.getColumnIndexOrThrow(
637 LauncherSettings.Favorites.ICON_RESOURCE);
638 final int containerIndex = c.getColumnIndexOrThrow(
639 LauncherSettings.Favorites.CONTAINER);
640 final int itemTypeIndex = c.getColumnIndexOrThrow(
641 LauncherSettings.Favorites.ITEM_TYPE);
642 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
643 LauncherSettings.Favorites.APPWIDGET_ID);
644 final int screenIndex = c.getColumnIndexOrThrow(
645 LauncherSettings.Favorites.SCREEN);
646 final int cellXIndex = c.getColumnIndexOrThrow
647 (LauncherSettings.Favorites.CELLX);
648 final int cellYIndex = c.getColumnIndexOrThrow
649 (LauncherSettings.Favorites.CELLY);
650 final int spanXIndex = c.getColumnIndexOrThrow
651 (LauncherSettings.Favorites.SPANX);
652 final int spanYIndex = c.getColumnIndexOrThrow(
653 LauncherSettings.Favorites.SPANY);
654 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
655 final int displayModeIndex = c.getColumnIndexOrThrow(
656 LauncherSettings.Favorites.DISPLAY_MODE);
657
658 ApplicationInfo info;
659 String intentDescription;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 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:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 id = c.getLong(idIndex);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800743 Uri uri = Uri.parse(c.getString(uriIndex));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744
Romain Guy5c16f3e2010-01-12 17:24:58 -0800745 // Make sure the live folder exists
746 final ProviderInfo providerInfo =
747 context.getPackageManager().resolveContentProvider(
748 uri.getAuthority(), 0);
749
750 if (providerInfo == null && !isSafeMode) {
751 itemsToRemove.add(id);
752 } else {
753 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
754
755 intentDescription = c.getString(intentIndex);
756 intent = null;
757 if (intentDescription != null) {
758 try {
759 intent = Intent.parseUri(intentDescription, 0);
760 } catch (URISyntaxException e) {
761 // Ignore, a live folder might not have a base intent
762 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400763 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800764
765 liveFolderInfo.title = c.getString(titleIndex);
766 liveFolderInfo.id = id;
767 liveFolderInfo.uri = uri;
768 container = c.getInt(containerIndex);
769 liveFolderInfo.container = container;
770 liveFolderInfo.screen = c.getInt(screenIndex);
771 liveFolderInfo.cellX = c.getInt(cellXIndex);
772 liveFolderInfo.cellY = c.getInt(cellYIndex);
773 liveFolderInfo.baseIntent = intent;
774 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
775
776 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
777 iconResourceIndex, liveFolderInfo);
778
779 switch (container) {
780 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
781 mItems.add(liveFolderInfo);
782 break;
783 }
784 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500787
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
789 // Read all Launcher-specific widget details
790 int appWidgetId = c.getInt(appWidgetIdIndex);
791 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
792 appWidgetInfo.id = c.getLong(idIndex);
793 appWidgetInfo.screen = c.getInt(screenIndex);
794 appWidgetInfo.cellX = c.getInt(cellXIndex);
795 appWidgetInfo.cellY = c.getInt(cellYIndex);
796 appWidgetInfo.spanX = c.getInt(spanXIndex);
797 appWidgetInfo.spanY = c.getInt(spanYIndex);
798
799 container = c.getInt(containerIndex);
800 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
801 Log.e(TAG, "Widget found where container "
802 + "!= CONTAINER_DESKTOP -- ignoring!");
803 continue;
804 }
805 appWidgetInfo.container = c.getInt(containerIndex);
806
807 mAppWidgets.add(appWidgetInfo);
808 break;
809 }
810 } catch (Exception e) {
811 Log.w(TAG, "Desktop items loading interrupted:", e);
812 }
813 }
814 } finally {
815 c.close();
816 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800817
818 if (itemsToRemove.size() > 0) {
819 ContentProviderClient client = contentResolver.acquireContentProviderClient(
820 LauncherSettings.Favorites.CONTENT_URI);
821 // Remove dead items
822 for (long id : itemsToRemove) {
823 if (DEBUG_LOADERS) {
824 Log.d(TAG, "Removed id = " + id);
825 }
826 // Don't notify content observers
827 try {
828 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
829 null, null);
830 } catch (RemoteException e) {
831 Log.w(TAG, "Could not remove id = " + id);
832 }
833 }
834 }
835
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800836 if (DEBUG_LOADERS) {
837 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839 }
840
841 /**
842 * Read everything out of our database.
843 */
844 private void bindWorkspace() {
845 final long t = SystemClock.uptimeMillis();
846
847 // Don't use these two variables in any of the callback runnables.
848 // Otherwise we hold a reference to them.
849 Callbacks callbacks = mCallbacks.get();
850 if (callbacks == null) {
851 // This launcher has exited and nobody bothered to tell us. Just bail.
852 Log.w(TAG, "LoaderThread running with no launcher");
853 return;
854 }
855
856 int N;
857 // Tell the workspace that we're about to start firing items at it
858 mHandler.post(new Runnable() {
859 public void run() {
860 Callbacks callbacks = tryGetCallbacks();
861 if (callbacks != null) {
862 callbacks.startBinding();
863 }
864 }
865 });
866 // Add the items to the workspace.
867 N = mItems.size();
868 for (int i=0; i<N; i+=ITEMS_CHUNK) {
869 final int start = i;
870 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
871 mHandler.post(new Runnable() {
872 public void run() {
873 Callbacks callbacks = tryGetCallbacks();
874 if (callbacks != null) {
875 callbacks.bindItems(mItems, start, start+chunkSize);
876 }
877 }
878 });
879 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500880 mHandler.post(new Runnable() {
881 public void run() {
882 Callbacks callbacks = tryGetCallbacks();
883 if (callbacks != null) {
884 callbacks.bindFolders(mFolders);
885 }
886 }
887 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888 // Wait until the queue goes empty.
889 mHandler.postIdle(new Runnable() {
890 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800891 if (DEBUG_LOADERS) {
892 Log.d(TAG, "Going to start binding widgets soon.");
893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894 }
895 });
896 // Bind the widgets, one at a time.
897 // WARNING: this is calling into the workspace from the background thread,
898 // but since getCurrentScreen() just returns the int, we should be okay. This
899 // is just a hint for the order, and if it's wrong, we'll be okay.
900 // TODO: instead, we should have that push the current screen into here.
901 final int currentScreen = callbacks.getCurrentWorkspaceScreen();
902 N = mAppWidgets.size();
903 // once for the current screen
904 for (int i=0; i<N; i++) {
905 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
906 if (widget.screen == currentScreen) {
907 mHandler.post(new Runnable() {
908 public void run() {
909 Callbacks callbacks = tryGetCallbacks();
910 if (callbacks != null) {
911 callbacks.bindAppWidget(widget);
912 }
913 }
914 });
915 }
916 }
917 // once for the other screens
918 for (int i=0; i<N; i++) {
919 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
920 if (widget.screen != currentScreen) {
921 mHandler.post(new Runnable() {
922 public void run() {
923 Callbacks callbacks = tryGetCallbacks();
924 if (callbacks != null) {
925 callbacks.bindAppWidget(widget);
926 }
927 }
928 });
929 }
930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 // Tell the workspace that we're done.
932 mHandler.post(new Runnable() {
933 public void run() {
934 Callbacks callbacks = tryGetCallbacks();
935 if (callbacks != null) {
936 callbacks.finishBindingItems();
937 }
938 }
939 });
940 // If we're profiling, this is the last thing in the queue.
941 mHandler.post(new Runnable() {
942 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800943 if (DEBUG_LOADERS) {
944 Log.d(TAG, "bound workspace in "
945 + (SystemClock.uptimeMillis()-t) + "ms");
946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 if (Launcher.PROFILE_ROTATE) {
948 android.os.Debug.stopMethodTracing();
949 }
950 }
951 });
952 }
953
954 private void loadAllApps() {
955 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
956 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
957
958 final Callbacks callbacks = tryGetCallbacks();
959 if (callbacks == null) {
960 return;
961 }
962
963 final Context context = mContext;
964 final PackageManager packageManager = context.getPackageManager();
965
966 final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
967
968 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400969 mBeforeFirstLoad = false;
970
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 mAllAppsList.clear();
972 if (apps != null) {
973 long t = SystemClock.uptimeMillis();
974
975 int N = apps.size();
976 Utilities.BubbleText bubble = new Utilities.BubbleText(context);
977 for (int i=0; i<N && !mStopped; i++) {
978 // This builds the icon bitmaps.
979 mAllAppsList.add(AppInfoCache.cache(apps.get(i), context, bubble));
980 }
Joe Onoratob0c27f22009-12-01 16:19:38 -0800981 Collections.sort(mAllAppsList.data, APP_NAME_COMPARATOR);
982 Collections.sort(mAllAppsList.added, APP_NAME_COMPARATOR);
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800983 if (DEBUG_LOADERS) {
984 Log.d(TAG, "cached app icons in "
985 + (SystemClock.uptimeMillis()-t) + "ms");
986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 }
988 }
989 }
990
991 private void bindAllApps() {
992 synchronized (mLock) {
Joe Onorato0c4513e2009-11-19 12:24:48 -0800993 final ArrayList<ApplicationInfo> results
Romain Guy5c16f3e2010-01-12 17:24:58 -0800994 = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone();
Joe Onorato0c4513e2009-11-19 12:24:48 -0800995 // We're adding this now, so clear out this so we don't re-send them.
Romain Guy84f296c2009-11-04 15:00:44 -0800996 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 mHandler.post(new Runnable() {
998 public void run() {
Joe Onorato34b02492009-10-14 11:13:48 -0700999 final long t = SystemClock.uptimeMillis();
1000 final int count = results.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001
1002 Callbacks callbacks = tryGetCallbacks();
1003 if (callbacks != null) {
1004 callbacks.bindAllApplications(results);
1005 }
1006
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001007 if (DEBUG_LOADERS) {
1008 Log.d(TAG, "bound app " + count + " icons in "
Romain Guy5c16f3e2010-01-12 17:24:58 -08001009 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001010 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 }
1012 });
1013 }
1014 }
Joe Onoratobe386092009-11-17 17:32:16 -08001015
1016 public void dumpState() {
1017 Log.d(TAG, "mLoader.mLoaderThread.mContext=" + mContext);
1018 Log.d(TAG, "mLoader.mLoaderThread.mWaitThread=" + mWaitThread);
1019 Log.d(TAG, "mLoader.mLoaderThread.mIsLaunching=" + mIsLaunching);
1020 Log.d(TAG, "mLoader.mLoaderThread.mStopped=" + mStopped);
1021 Log.d(TAG, "mLoader.mLoaderThread.mWorkspaceDoneBinding=" + mWorkspaceDoneBinding);
1022 }
1023 }
1024
1025 public void dumpState() {
1026 Log.d(TAG, "mLoader.mLastWorkspaceSeq=" + mLoader.mLastWorkspaceSeq);
1027 Log.d(TAG, "mLoader.mWorkspaceSeq=" + mLoader.mWorkspaceSeq);
1028 Log.d(TAG, "mLoader.mLastAllAppsSeq=" + mLoader.mLastAllAppsSeq);
1029 Log.d(TAG, "mLoader.mAllAppsSeq=" + mLoader.mAllAppsSeq);
1030 Log.d(TAG, "mLoader.mItems size=" + mLoader.mItems.size());
1031 if (mLoaderThread != null) {
1032 mLoaderThread.dumpState();
1033 } else {
1034 Log.d(TAG, "mLoader.mLoaderThread=null");
1035 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 }
1037 }
1038
1039 /**
1040 * Make an ApplicationInfo object for an application.
1041 */
1042 private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent,
1043 Context context) {
1044 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1045
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001046 if (resolveInfo == null) {
1047 return null;
1048 }
1049
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 final ApplicationInfo info = new ApplicationInfo();
1051 final ActivityInfo activityInfo = resolveInfo.activityInfo;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001052 info.icon = Utilities.createIconThumbnail(activityInfo.loadIcon(manager), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 if (info.title == null || info.title.length() == 0) {
1054 info.title = activityInfo.loadLabel(manager);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 if (info.title == null) {
1057 info.title = "";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1060 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 * Make an ApplicationInfo object for a sortcut
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001066 private static ApplicationInfo getApplicationInfoShortcut(Cursor c, Context context,
1067 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069 final ApplicationInfo info = new ApplicationInfo();
1070 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 int iconType = c.getInt(iconTypeIndex);
1073 switch (iconType) {
1074 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1075 String packageName = c.getString(iconPackageIndex);
1076 String resourceName = c.getString(iconResourceIndex);
1077 PackageManager packageManager = context.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079 Resources resources = packageManager.getResourcesForApplication(packageName);
1080 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001081 info.icon = Utilities.createIconThumbnail(resources.getDrawable(id), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 } catch (Exception e) {
1083 info.icon = packageManager.getDefaultActivityIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 info.iconResource = new Intent.ShortcutIconResource();
1086 info.iconResource.packageName = packageName;
1087 info.iconResource.resourceName = resourceName;
1088 info.customIcon = false;
1089 break;
1090 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
1091 byte[] data = c.getBlob(iconIndex);
1092 try {
1093 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
1094 info.icon = new FastBitmapDrawable(
1095 Utilities.createBitmapThumbnail(bitmap, context));
1096 } catch (Exception e) {
1097 packageManager = context.getPackageManager();
1098 info.icon = packageManager.getDefaultActivityIcon();
1099 }
1100 info.filtered = true;
1101 info.customIcon = true;
1102 break;
1103 default:
1104 info.icon = context.getPackageManager().getDefaultActivityIcon();
1105 info.customIcon = false;
1106 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 return info;
1109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1112 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1113
1114 int iconType = c.getInt(iconTypeIndex);
1115 switch (iconType) {
1116 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1117 String packageName = c.getString(iconPackageIndex);
1118 String resourceName = c.getString(iconResourceIndex);
1119 PackageManager packageManager = context.getPackageManager();
1120 try {
1121 Resources resources = packageManager.getResourcesForApplication(packageName);
1122 final int id = resources.getIdentifier(resourceName, null, null);
1123 liveFolderInfo.icon = resources.getDrawable(id);
1124 } catch (Exception e) {
1125 liveFolderInfo.icon =
1126 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1127 }
1128 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1129 liveFolderInfo.iconResource.packageName = packageName;
1130 liveFolderInfo.iconResource.resourceName = resourceName;
1131 break;
1132 default:
1133 liveFolderInfo.icon =
1134 context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1135 }
1136 }
1137
1138 /**
1139 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1140 * or make a new one.
1141 */
1142 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1143 // See if a placeholder was created for us already
1144 FolderInfo folderInfo = folders.get(id);
1145 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1146 // No placeholder -- create a new instance
1147 folderInfo = new UserFolderInfo();
1148 folders.put(id, folderInfo);
1149 }
1150 return (UserFolderInfo) folderInfo;
1151 }
1152
1153 /**
1154 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1155 * new one.
1156 */
1157 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1158 // See if a placeholder was created for us already
1159 FolderInfo folderInfo = folders.get(id);
1160 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1161 // No placeholder -- create a new instance
1162 folderInfo = new LiveFolderInfo();
1163 folders.put(id, folderInfo);
1164 }
1165 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167
1168 private static void updateShortcutLabels(ContentResolver resolver, PackageManager manager) {
1169 final Cursor c = resolver.query(LauncherSettings.Favorites.CONTENT_URI,
Romain Guy73b979d2009-06-09 12:57:21 -07001170 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.TITLE,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 LauncherSettings.Favorites.INTENT, LauncherSettings.Favorites.ITEM_TYPE },
1172 null, null, null);
1173
Romain Guy73b979d2009-06-09 12:57:21 -07001174 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1176 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1177 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1178
1179 // boolean changed = false;
1180
1181 try {
1182 while (c.moveToNext()) {
1183 try {
1184 if (c.getInt(itemTypeIndex) !=
1185 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1186 continue;
1187 }
1188
1189 final String intentUri = c.getString(intentIndex);
1190 if (intentUri != null) {
Romain Guy1ce1a242009-06-23 17:34:54 -07001191 final Intent shortcut = Intent.parseUri(intentUri, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 if (Intent.ACTION_MAIN.equals(shortcut.getAction())) {
1193 final ComponentName name = shortcut.getComponent();
1194 if (name != null) {
1195 final ActivityInfo activityInfo = manager.getActivityInfo(name, 0);
1196 final String title = c.getString(titleIndex);
1197 String label = getLabel(manager, activityInfo);
1198
1199 if (title == null || !title.equals(label)) {
1200 final ContentValues values = new ContentValues();
1201 values.put(LauncherSettings.Favorites.TITLE, label);
1202
Romain Guyfedc4fc2009-03-27 20:48:20 -07001203 resolver.update(
1204 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 values, "_id=?",
1206 new String[] { String.valueOf(c.getLong(idIndex)) });
1207
1208 // changed = true;
1209 }
1210 }
1211 }
1212 }
1213 } catch (URISyntaxException e) {
1214 // Ignore
1215 } catch (PackageManager.NameNotFoundException e) {
1216 // Ignore
1217 }
1218 }
1219 } finally {
1220 c.close();
1221 }
1222
1223 // if (changed) resolver.notifyChange(Settings.Favorites.CONTENT_URI, null);
1224 }
1225
1226 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1227 String label = activityInfo.loadLabel(manager).toString();
1228 if (label == null) {
1229 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1230 if (label == null) {
1231 label = activityInfo.name;
1232 }
1233 }
1234 return label;
1235 }
1236
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001238 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 = new Comparator<ApplicationInfo>() {
1240 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1241 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 };
Joe Onoratobe386092009-11-17 17:32:16 -08001244
1245 public void dumpState() {
1246 Log.d(TAG, "mBeforeFirstLoad=" + mBeforeFirstLoad);
1247 Log.d(TAG, "mCallbacks=" + mCallbacks);
1248 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1249 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1250 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1251 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
1252 mLoader.dumpState();
1253 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254}