blob: 5e1abe6c2c0bf06d5fd4a5549b48ecc2a795dac4 [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
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetManager;
20import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040021import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080023import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
29import android.content.pm.ActivityInfo;
30import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080031import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.net.Uri;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
Romain Guy5c16f3e2010-01-12 17:24:58 -080039import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import java.lang.ref.WeakReference;
45import java.net.URISyntaxException;
46import java.text.Collator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import java.util.ArrayList;
Joe Onorato56d82912010-03-07 14:32:10 -050048import java.util.Arrays;
Joe Onorato9c1289c2009-08-17 11:03:03 -040049import java.util.Comparator;
50import java.util.Collections;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import java.util.HashMap;
52import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Romain Guyedcce092010-03-04 13:03:17 -080054import com.android.launcher.R;
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056/**
57 * Maintains in-memory state of the Launcher. It is expected that there should be only one
58 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070059 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040061public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080062 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040063 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070064
Joe Onoratof99f8c12009-10-31 17:27:36 -040065 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 private final Object mLock = new Object();
67 private DeferredHandler mHandler = new DeferredHandler();
68 private Loader mLoader = new Loader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
Joe Onoratof99f8c12009-10-31 17:27:36 -040070 private boolean mBeforeFirstLoad = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Joe Onorato0589f0f2010-02-08 13:44:00 -080073 private AllAppsList mAllAppsList;
74 private IconCache mIconCache;
75
76 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Joe Onorato9c1289c2009-08-17 11:03:03 -040078 public interface Callbacks {
79 public int getCurrentWorkspaceScreen();
80 public void startBinding();
81 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -050082 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -040083 public void finishBindingItems();
84 public void bindAppWidget(LauncherAppWidgetInfo info);
85 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -050086 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
87 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
88 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Joe Onorato0589f0f2010-02-08 13:44:00 -080091 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -040092 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -080093 mAllAppsList = new AllAppsList(iconCache);
94 mIconCache = iconCache;
95
96 mDefaultIcon = Utilities.createIconBitmap(
97 app.getPackageManager().getDefaultActivityIcon(), app);
98 }
99
Joe Onorato56d82912010-03-07 14:32:10 -0500100 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800101 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
Joe Onorato9c1289c2009-08-17 11:03:03 -0400104 /**
105 * Adds an item to the DB if it was not created previously, or move it to a new
106 * <container, screen, cellX, cellY>
107 */
108 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
109 int screen, int cellX, int cellY) {
110 if (item.container == ItemInfo.NO_ID) {
111 // From all apps
112 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
113 } else {
114 // From somewhere else
115 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 }
117 }
118
119 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700121 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
123 int cellX, int cellY) {
124 item.container = container;
125 item.screen = screen;
126 item.cellX = cellX;
127 item.cellY = cellY;
128
129 final ContentValues values = new ContentValues();
130 final ContentResolver cr = context.getContentResolver();
131
132 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
133 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
134 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
135 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
136
137 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700138 }
139
140 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400141 * Returns true if the shortcuts already exists in the database.
142 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400144 static boolean shortcutExists(Context context, String title, Intent intent) {
145 final ContentResolver cr = context.getContentResolver();
146 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
147 new String[] { "title", "intent" }, "title=? and intent=?",
148 new String[] { title, intent.toUri(0) }, null);
149 boolean result = false;
150 try {
151 result = c.moveToFirst();
152 } finally {
153 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700156 }
157
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 /**
159 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
160 */
161 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
162 final ContentResolver cr = context.getContentResolver();
163 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
164 "_id=? and (itemType=? or itemType=?)",
165 new String[] { String.valueOf(id),
166 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
167 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 try {
170 if (c.moveToFirst()) {
171 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
172 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
173 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
174 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
175 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
176 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 FolderInfo folderInfo = null;
179 switch (c.getInt(itemTypeIndex)) {
180 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
181 folderInfo = findOrMakeUserFolder(folderList, id);
182 break;
183 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
184 folderInfo = findOrMakeLiveFolder(folderList, id);
185 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700186 }
187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 folderInfo.title = c.getString(titleIndex);
189 folderInfo.id = id;
190 folderInfo.container = c.getInt(containerIndex);
191 folderInfo.screen = c.getInt(screenIndex);
192 folderInfo.cellX = c.getInt(cellXIndex);
193 folderInfo.cellY = c.getInt(cellYIndex);
194
195 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 } finally {
198 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700199 }
200
201 return null;
202 }
203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 /**
205 * Add an item to the database in a specified container. Sets the container, screen, cellX and
206 * cellY fields of the item. Also assigns an ID to the item.
207 */
208 static void addItemToDatabase(Context context, ItemInfo item, long container,
209 int screen, int cellX, int cellY, boolean notify) {
210 item.container = container;
211 item.screen = screen;
212 item.cellX = cellX;
213 item.cellY = cellY;
214
215 final ContentValues values = new ContentValues();
216 final ContentResolver cr = context.getContentResolver();
217
218 item.onAddToDatabase(values);
219
220 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
221 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
222
223 if (result != null) {
224 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700225 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700226 }
227
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 /**
229 * Update an item to the database in a specified container.
230 */
231 static void updateItemInDatabase(Context context, ItemInfo item) {
232 final ContentValues values = new ContentValues();
233 final ContentResolver cr = context.getContentResolver();
234
235 item.onAddToDatabase(values);
236
237 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
238 }
239
240 /**
241 * Removes the specified item from the database
242 * @param context
243 * @param item
244 */
245 static void deleteItemFromDatabase(Context context, ItemInfo item) {
246 final ContentResolver cr = context.getContentResolver();
247
248 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
249 }
250
251 /**
252 * Remove the contents of the specified folder from the database
253 */
254 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
255 final ContentResolver cr = context.getContentResolver();
256
257 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
258 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
259 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
260 }
261
262 /**
263 * Set this as the current Launcher activity object for the loader.
264 */
265 public void initialize(Callbacks callbacks) {
266 synchronized (mLock) {
267 mCallbacks = new WeakReference<Callbacks>(callbacks);
268 }
269 }
270
271 public void startLoader(Context context, boolean isLaunching) {
272 mLoader.startLoader(context, isLaunching);
273 }
274
275 public void stopLoader() {
276 mLoader.stopLoader();
277 }
278
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700279 /**
280 * We pick up most of the changes to all apps.
281 */
282 public void setAllAppsDirty() {
283 mLoader.setAllAppsDirty();
284 }
285
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 public void setWorkspaceDirty() {
287 mLoader.setWorkspaceDirty();
288 }
289
290 /**
291 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
292 * ACTION_PACKAGE_CHANGED.
293 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400294 public void onReceive(Context context, Intent intent) {
295 // Use the app as the context.
296 context = mApp;
297
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 ArrayList<ApplicationInfo> added = null;
299 ArrayList<ApplicationInfo> removed = null;
300 ArrayList<ApplicationInfo> modified = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301
302 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400303 if (mBeforeFirstLoad) {
304 // If we haven't even loaded yet, don't bother, since we'll just pick
305 // up the changes.
306 return;
307 }
308
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 final String action = intent.getAction();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400310
Joe Onorato64e6be72010-03-05 15:05:52 -0500311 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
312 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
313 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
314 final String packageName = intent.getData().getSchemeSpecificPart();
315 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316
Joe Onorato64e6be72010-03-05 15:05:52 -0500317 if (packageName == null || packageName.length() == 0) {
318 // they sent us a bad intent
319 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 }
Joe Onorato64e6be72010-03-05 15:05:52 -0500321
322 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400323 mAllAppsList.updatePackage(context, packageName);
Joe Onorato64e6be72010-03-05 15:05:52 -0500324 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
325 if (!replacing) {
326 mAllAppsList.removePackage(packageName);
327 }
328 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
329 // later, we will update the package at this time
330 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
331 if (!replacing) {
332 mAllAppsList.addPackage(context, packageName);
333 } else {
334 mAllAppsList.updatePackage(context, packageName);
335 }
336 }
Joe Onorato56d82912010-03-07 14:32:10 -0500337
338 if (mAllAppsList.added.size() > 0) {
339 added = mAllAppsList.added;
340 mAllAppsList.added = new ArrayList<ApplicationInfo>();
341 }
342 if (mAllAppsList.removed.size() > 0) {
343 removed = mAllAppsList.removed;
344 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
345 for (ApplicationInfo info: removed) {
346 mIconCache.remove(info.intent.getComponent());
347 }
348 }
349 if (mAllAppsList.modified.size() > 0) {
350 modified = mAllAppsList.modified;
351 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
352 }
353
354 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
355 if (callbacks == null) {
356 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
357 return;
358 }
359
360 if (added != null) {
361 final ArrayList<ApplicationInfo> addedFinal = added;
362 mHandler.post(new Runnable() {
363 public void run() {
364 callbacks.bindAppsAdded(addedFinal);
365 }
366 });
367 }
368 if (modified != null) {
369 final ArrayList<ApplicationInfo> modifiedFinal = modified;
370 mHandler.post(new Runnable() {
371 public void run() {
372 callbacks.bindAppsUpdated(modifiedFinal);
373 }
374 });
375 }
376 if (removed != null) {
377 final ArrayList<ApplicationInfo> removedFinal = removed;
378 mHandler.post(new Runnable() {
379 public void run() {
380 callbacks.bindAppsRemoved(removedFinal);
381 }
382 });
383 }
Joe Onorato64e6be72010-03-05 15:05:52 -0500384 } else {
385 if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
386 String packages[] = intent.getStringArrayExtra(
387 Intent.EXTRA_CHANGED_PACKAGE_LIST);
388 if (packages == null || packages.length == 0) {
389 return;
390 }
Joe Onorato56d82912010-03-07 14:32:10 -0500391 setAllAppsDirty();
392 setWorkspaceDirty();
393 startLoader(context, false);
Joe Onorato64e6be72010-03-05 15:05:52 -0500394 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
395 String packages[] = intent.getStringArrayExtra(
396 Intent.EXTRA_CHANGED_PACKAGE_LIST);
397 if (packages == null || packages.length == 0) {
398 return;
399 }
Joe Onorato56d82912010-03-07 14:32:10 -0500400 setAllAppsDirty();
401 setWorkspaceDirty();
402 startLoader(context, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 }
404 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 }
406 }
407
408 public class Loader {
409 private static final int ITEMS_CHUNK = 6;
410
411 private LoaderThread mLoaderThread;
412
413 private int mLastWorkspaceSeq = 0;
414 private int mWorkspaceSeq = 1;
415
416 private int mLastAllAppsSeq = 0;
417 private int mAllAppsSeq = 1;
418
Romain Guy84f296c2009-11-04 15:00:44 -0800419 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
420 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
Joe Onoratoad72e172009-11-06 16:25:04 -0500421 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422
423 /**
424 * Call this from the ui thread so the handler is initialized on the correct thread.
425 */
426 public Loader() {
427 }
428
429 public void startLoader(Context context, boolean isLaunching) {
430 synchronized (mLock) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800431 if (DEBUG_LOADERS) {
432 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400434 // Don't bother to start the thread if we know it's not going to do anything
Joe Onoratoac033302010-04-13 17:19:18 -0700435 if (mCallbacks != null && mCallbacks.get() != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400436 LoaderThread oldThread = mLoaderThread;
437 if (oldThread != null) {
438 if (oldThread.isLaunching()) {
439 // don't downgrade isLaunching if we're already running
440 isLaunching = true;
441 }
442 oldThread.stopLocked();
443 }
444 mLoaderThread = new LoaderThread(context, oldThread, isLaunching);
445 mLoaderThread.start();
446 }
447 }
448 }
449
450 public void stopLoader() {
451 synchronized (mLock) {
452 if (mLoaderThread != null) {
453 mLoaderThread.stopLocked();
454 }
455 }
456 }
457
458 public void setWorkspaceDirty() {
459 synchronized (mLock) {
460 mWorkspaceSeq++;
461 }
462 }
463
464 public void setAllAppsDirty() {
465 synchronized (mLock) {
466 mAllAppsSeq++;
467 }
468 }
469
470 /**
471 * Runnable for the thread that loads the contents of the launcher:
472 * - workspace icons
473 * - widgets
474 * - all apps icons
475 */
476 private class LoaderThread extends Thread {
477 private Context mContext;
478 private Thread mWaitThread;
479 private boolean mIsLaunching;
480 private boolean mStopped;
481 private boolean mWorkspaceDoneBinding;
482
483 LoaderThread(Context context, Thread waitThread, boolean isLaunching) {
484 mContext = context;
485 mWaitThread = waitThread;
486 mIsLaunching = isLaunching;
487 }
488
489 boolean isLaunching() {
490 return mIsLaunching;
491 }
492
493 /**
494 * If another LoaderThread was supplied, we need to wait for that to finish before
495 * we start our processing. This keeps the ordering of the setting and clearing
496 * of the dirty flags correct by making sure we don't start processing stuff until
497 * they've had a chance to re-set them. We do this waiting the worker thread, not
498 * the ui thread to avoid ANRs.
499 */
500 private void waitForOtherThread() {
501 if (mWaitThread != null) {
502 boolean done = false;
503 while (!done) {
504 try {
505 mWaitThread.join();
Joe Onoratoefabe002009-08-28 09:38:18 -0700506 done = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400507 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800508 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509 }
510 }
511 mWaitThread = null;
512 }
513 }
514
515 public void run() {
516 waitForOtherThread();
517
518 // Elevate priority when Home launches for the first time to avoid
519 // starving at boot time. Staring at a blank home is not cool.
520 synchronized (mLock) {
521 android.os.Process.setThreadPriority(mIsLaunching
522 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
523 }
524
525 // Load the workspace only if it's dirty.
526 int workspaceSeq;
527 boolean workspaceDirty;
528 synchronized (mLock) {
529 workspaceSeq = mWorkspaceSeq;
530 workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq;
531 }
532 if (workspaceDirty) {
533 loadWorkspace();
534 }
535 synchronized (mLock) {
536 // If we're not stopped, and nobody has incremented mWorkspaceSeq.
537 if (mStopped) {
538 return;
539 }
540 if (workspaceSeq == mWorkspaceSeq) {
541 mLastWorkspaceSeq = mWorkspaceSeq;
542 }
543 }
544
545 // Bind the workspace
546 bindWorkspace();
547
548 // Wait until the either we're stopped or the other threads are done.
549 // This way we don't start loading all apps until the workspace has settled
550 // down.
551 synchronized (LoaderThread.this) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500552 mHandler.postIdle(new Runnable() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553 public void run() {
554 synchronized (LoaderThread.this) {
555 mWorkspaceDoneBinding = true;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800556 if (DEBUG_LOADERS) {
557 Log.d(TAG, "done with workspace");
558 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400559 LoaderThread.this.notify();
560 }
561 }
562 });
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800563 if (DEBUG_LOADERS) {
564 Log.d(TAG, "waiting to be done with workspace");
565 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 while (!mStopped && !mWorkspaceDoneBinding) {
567 try {
568 this.wait();
569 } catch (InterruptedException ex) {
Romain Guy84f296c2009-11-04 15:00:44 -0800570 // Ignore
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 }
572 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800573 if (DEBUG_LOADERS) {
574 Log.d(TAG, "done waiting to be done with workspace");
575 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576 }
577
578 // Load all apps if they're dirty
579 int allAppsSeq;
580 boolean allAppsDirty;
581 synchronized (mLock) {
582 allAppsSeq = mAllAppsSeq;
583 allAppsDirty = mAllAppsSeq != mLastAllAppsSeq;
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800584 if (DEBUG_LOADERS) {
585 Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq
586 + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty");
587 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400588 }
589 if (allAppsDirty) {
590 loadAllApps();
591 }
592 synchronized (mLock) {
593 // If we're not stopped, and nobody has incremented mAllAppsSeq.
594 if (mStopped) {
595 return;
596 }
597 if (allAppsSeq == mAllAppsSeq) {
598 mLastAllAppsSeq = mAllAppsSeq;
599 }
600 }
601
602 // Bind all apps
Joe Onorato34b02492009-10-14 11:13:48 -0700603 if (allAppsDirty) {
604 bindAllApps();
605 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400606
607 // Clear out this reference, otherwise we end up holding it until all of the
608 // callback runnables are done.
609 mContext = null;
610
611 synchronized (mLock) {
612 // Setting the reference is atomic, but we can't do it inside the other critical
613 // sections.
614 mLoaderThread = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 }
616 }
617
618 public void stopLocked() {
619 synchronized (LoaderThread.this) {
620 mStopped = true;
621 this.notify();
622 }
623 }
624
625 /**
626 * Gets the callbacks object. If we've been stopped, or if the launcher object
Joe Onoratoc131b742010-03-11 15:45:05 -0800627 * has somehow been garbage collected, return null instead. Pass in the Callbacks
628 * object that was around when the deferred message was scheduled, and if there's
629 * a new Callbacks object around then also return null. This will save us from
630 * calling onto it with data that will be ignored.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 */
Joe Onoratoc131b742010-03-11 15:45:05 -0800632 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 synchronized (mLock) {
634 if (mStopped) {
635 return null;
636 }
637
Joe Onoratoac033302010-04-13 17:19:18 -0700638 if (mCallbacks == null) {
639 return null;
640 }
641
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 final Callbacks callbacks = mCallbacks.get();
Joe Onoratoc131b742010-03-11 15:45:05 -0800643 if (callbacks != oldCallbacks) {
644 return null;
645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 if (callbacks == null) {
647 Log.w(TAG, "no mCallbacks");
648 return null;
649 }
650
651 return callbacks;
652 }
653 }
654
655 private void loadWorkspace() {
656 long t = SystemClock.uptimeMillis();
657
658 final Context context = mContext;
659 final ContentResolver contentResolver = context.getContentResolver();
660 final PackageManager manager = context.getPackageManager();
Romain Guy629de3e2010-01-13 12:20:59 -0800661 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800662 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400664 mItems.clear();
Joe Onorato511ab642009-11-08 14:14:07 -0500665 mAppWidgets.clear();
Joe Onorato1db7a972009-11-16 18:32:22 -0800666 mFolders.clear();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400667
Romain Guy5c16f3e2010-01-12 17:24:58 -0800668 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
669
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 final Cursor c = contentResolver.query(
671 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
672
673 try {
674 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
675 final int intentIndex = c.getColumnIndexOrThrow
676 (LauncherSettings.Favorites.INTENT);
677 final int titleIndex = c.getColumnIndexOrThrow
678 (LauncherSettings.Favorites.TITLE);
679 final int iconTypeIndex = c.getColumnIndexOrThrow(
680 LauncherSettings.Favorites.ICON_TYPE);
681 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
682 final int iconPackageIndex = c.getColumnIndexOrThrow(
683 LauncherSettings.Favorites.ICON_PACKAGE);
684 final int iconResourceIndex = c.getColumnIndexOrThrow(
685 LauncherSettings.Favorites.ICON_RESOURCE);
686 final int containerIndex = c.getColumnIndexOrThrow(
687 LauncherSettings.Favorites.CONTAINER);
688 final int itemTypeIndex = c.getColumnIndexOrThrow(
689 LauncherSettings.Favorites.ITEM_TYPE);
690 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
691 LauncherSettings.Favorites.APPWIDGET_ID);
692 final int screenIndex = c.getColumnIndexOrThrow(
693 LauncherSettings.Favorites.SCREEN);
694 final int cellXIndex = c.getColumnIndexOrThrow
695 (LauncherSettings.Favorites.CELLX);
696 final int cellYIndex = c.getColumnIndexOrThrow
697 (LauncherSettings.Favorites.CELLY);
698 final int spanXIndex = c.getColumnIndexOrThrow
699 (LauncherSettings.Favorites.SPANX);
700 final int spanYIndex = c.getColumnIndexOrThrow(
701 LauncherSettings.Favorites.SPANY);
702 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
703 final int displayModeIndex = c.getColumnIndexOrThrow(
704 LauncherSettings.Favorites.DISPLAY_MODE);
705
Joe Onorato0589f0f2010-02-08 13:44:00 -0800706 ShortcutInfo info;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 String intentDescription;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 LauncherAppWidgetInfo appWidgetInfo;
709 int container;
710 long id;
711 Intent intent;
712
713 while (!mStopped && c.moveToNext()) {
714 try {
715 int itemType = c.getInt(itemTypeIndex);
716
717 switch (itemType) {
718 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
719 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
720 intentDescription = c.getString(intentIndex);
721 try {
722 intent = Intent.parseUri(intentDescription, 0);
723 } catch (URISyntaxException e) {
724 continue;
725 }
726
727 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Joe Onorato56d82912010-03-07 14:32:10 -0500728 info = getShortcutInfo(manager, intent, context, c, iconIndex,
729 titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730 } else {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800731 info = getShortcutInfo(c, context, iconTypeIndex,
Joe Onorato56d82912010-03-07 14:32:10 -0500732 iconPackageIndex, iconResourceIndex, iconIndex,
733 titleIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 }
735
736 if (info != null) {
Joe Onorato56d82912010-03-07 14:32:10 -0500737 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738
Joe Onorato56d82912010-03-07 14:32:10 -0500739 info.intent = intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 info.id = c.getLong(idIndex);
741 container = c.getInt(containerIndex);
742 info.container = container;
743 info.screen = c.getInt(screenIndex);
744 info.cellX = c.getInt(cellXIndex);
745 info.cellY = c.getInt(cellYIndex);
746
747 switch (container) {
748 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
749 mItems.add(info);
750 break;
751 default:
752 // Item is in a user folder
753 UserFolderInfo folderInfo =
Joe Onoratoad72e172009-11-06 16:25:04 -0500754 findOrMakeUserFolder(mFolders, container);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 folderInfo.add(info);
756 break;
757 }
Joe Onorato56d82912010-03-07 14:32:10 -0500758 } else {
759 // Failed to load the shortcut, probably because the
760 // activity manager couldn't resolve it (maybe the app
761 // was uninstalled), or the db row was somehow screwed up.
762 // Delete it.
763 id = c.getLong(idIndex);
764 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
765 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
766 id, false), null, null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400767 }
768 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400769
Joe Onoratoad72e172009-11-06 16:25:04 -0500770 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 id = c.getLong(idIndex);
Joe Onoratoad72e172009-11-06 16:25:04 -0500772 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773
774 folderInfo.title = c.getString(titleIndex);
775
776 folderInfo.id = id;
777 container = c.getInt(containerIndex);
778 folderInfo.container = container;
779 folderInfo.screen = c.getInt(screenIndex);
780 folderInfo.cellX = c.getInt(cellXIndex);
781 folderInfo.cellY = c.getInt(cellYIndex);
782
783 switch (container) {
784 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
785 mItems.add(folderInfo);
786 break;
787 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500788
789 mFolders.put(folderInfo.id, folderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500791
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793 id = c.getLong(idIndex);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800794 Uri uri = Uri.parse(c.getString(uriIndex));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795
Romain Guy5c16f3e2010-01-12 17:24:58 -0800796 // Make sure the live folder exists
797 final ProviderInfo providerInfo =
798 context.getPackageManager().resolveContentProvider(
799 uri.getAuthority(), 0);
800
801 if (providerInfo == null && !isSafeMode) {
802 itemsToRemove.add(id);
803 } else {
804 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
805
806 intentDescription = c.getString(intentIndex);
807 intent = null;
808 if (intentDescription != null) {
809 try {
810 intent = Intent.parseUri(intentDescription, 0);
811 } catch (URISyntaxException e) {
812 // Ignore, a live folder might not have a base intent
813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800815
816 liveFolderInfo.title = c.getString(titleIndex);
817 liveFolderInfo.id = id;
818 liveFolderInfo.uri = uri;
819 container = c.getInt(containerIndex);
820 liveFolderInfo.container = container;
821 liveFolderInfo.screen = c.getInt(screenIndex);
822 liveFolderInfo.cellX = c.getInt(cellXIndex);
823 liveFolderInfo.cellY = c.getInt(cellYIndex);
824 liveFolderInfo.baseIntent = intent;
825 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
826
827 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
828 iconResourceIndex, liveFolderInfo);
829
830 switch (container) {
831 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
832 mItems.add(liveFolderInfo);
833 break;
834 }
835 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 break;
Joe Onoratoad72e172009-11-06 16:25:04 -0500838
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
840 // Read all Launcher-specific widget details
841 int appWidgetId = c.getInt(appWidgetIdIndex);
Romain Guy629de3e2010-01-13 12:20:59 -0800842 id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843
Romain Guy629de3e2010-01-13 12:20:59 -0800844 final AppWidgetProviderInfo provider =
845 widgets.getAppWidgetInfo(appWidgetId);
846
847 if (!isSafeMode && (provider == null || provider.provider == null ||
848 provider.provider.getPackageName() == null)) {
Joe Onorato8ddc4fd2010-03-17 09:14:50 -0700849 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
850 + id + " appWidgetId=" + appWidgetId);
Romain Guy629de3e2010-01-13 12:20:59 -0800851 itemsToRemove.add(id);
852 } else {
853 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
854 appWidgetInfo.id = id;
855 appWidgetInfo.screen = c.getInt(screenIndex);
856 appWidgetInfo.cellX = c.getInt(cellXIndex);
857 appWidgetInfo.cellY = c.getInt(cellYIndex);
858 appWidgetInfo.spanX = c.getInt(spanXIndex);
859 appWidgetInfo.spanY = c.getInt(spanYIndex);
860
861 container = c.getInt(containerIndex);
862 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
863 Log.e(TAG, "Widget found where container "
864 + "!= CONTAINER_DESKTOP -- ignoring!");
865 continue;
866 }
867 appWidgetInfo.container = c.getInt(containerIndex);
868
869 mAppWidgets.add(appWidgetInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 break;
872 }
873 } catch (Exception e) {
874 Log.w(TAG, "Desktop items loading interrupted:", e);
875 }
876 }
877 } finally {
878 c.close();
879 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800880
881 if (itemsToRemove.size() > 0) {
882 ContentProviderClient client = contentResolver.acquireContentProviderClient(
883 LauncherSettings.Favorites.CONTENT_URI);
884 // Remove dead items
885 for (long id : itemsToRemove) {
886 if (DEBUG_LOADERS) {
887 Log.d(TAG, "Removed id = " + id);
888 }
889 // Don't notify content observers
890 try {
891 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
892 null, null);
893 } catch (RemoteException e) {
894 Log.w(TAG, "Could not remove id = " + id);
895 }
896 }
897 }
898
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800899 if (DEBUG_LOADERS) {
900 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
901 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 }
903
904 /**
905 * Read everything out of our database.
906 */
907 private void bindWorkspace() {
908 final long t = SystemClock.uptimeMillis();
909
910 // Don't use these two variables in any of the callback runnables.
911 // Otherwise we hold a reference to them.
Joe Onoratoc131b742010-03-11 15:45:05 -0800912 final Callbacks oldCallbacks = mCallbacks.get();
913 if (oldCallbacks == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 // This launcher has exited and nobody bothered to tell us. Just bail.
915 Log.w(TAG, "LoaderThread running with no launcher");
916 return;
917 }
918
919 int N;
920 // Tell the workspace that we're about to start firing items at it
921 mHandler.post(new Runnable() {
922 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800923 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 if (callbacks != null) {
925 callbacks.startBinding();
926 }
927 }
928 });
929 // Add the items to the workspace.
930 N = mItems.size();
931 for (int i=0; i<N; i+=ITEMS_CHUNK) {
932 final int start = i;
933 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
934 mHandler.post(new Runnable() {
935 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800936 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 if (callbacks != null) {
938 callbacks.bindItems(mItems, start, start+chunkSize);
939 }
940 }
941 });
942 }
Joe Onoratoad72e172009-11-06 16:25:04 -0500943 mHandler.post(new Runnable() {
944 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800945 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onoratoad72e172009-11-06 16:25:04 -0500946 if (callbacks != null) {
947 callbacks.bindFolders(mFolders);
948 }
949 }
950 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 // Wait until the queue goes empty.
952 mHandler.postIdle(new Runnable() {
953 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800954 if (DEBUG_LOADERS) {
955 Log.d(TAG, "Going to start binding widgets soon.");
956 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 }
958 });
959 // Bind the widgets, one at a time.
960 // WARNING: this is calling into the workspace from the background thread,
961 // but since getCurrentScreen() just returns the int, we should be okay. This
962 // is just a hint for the order, and if it's wrong, we'll be okay.
963 // TODO: instead, we should have that push the current screen into here.
Joe Onoratoc131b742010-03-11 15:45:05 -0800964 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 N = mAppWidgets.size();
966 // once for the current screen
967 for (int i=0; i<N; i++) {
968 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
969 if (widget.screen == currentScreen) {
970 mHandler.post(new Runnable() {
971 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800972 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 if (callbacks != null) {
974 callbacks.bindAppWidget(widget);
975 }
976 }
977 });
978 }
979 }
980 // once for the other screens
981 for (int i=0; i<N; i++) {
982 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
983 if (widget.screen != currentScreen) {
984 mHandler.post(new Runnable() {
985 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800986 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 if (callbacks != null) {
988 callbacks.bindAppWidget(widget);
989 }
990 }
991 });
992 }
993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 // Tell the workspace that we're done.
995 mHandler.post(new Runnable() {
996 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800997 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 if (callbacks != null) {
999 callbacks.finishBindingItems();
1000 }
1001 }
1002 });
1003 // If we're profiling, this is the last thing in the queue.
1004 mHandler.post(new Runnable() {
1005 public void run() {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001006 if (DEBUG_LOADERS) {
1007 Log.d(TAG, "bound workspace in "
1008 + (SystemClock.uptimeMillis()-t) + "ms");
1009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010 if (Launcher.PROFILE_ROTATE) {
1011 android.os.Debug.stopMethodTracing();
1012 }
1013 }
1014 });
1015 }
1016
1017 private void loadAllApps() {
1018 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1019 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1020
Joe Onoratoc131b742010-03-11 15:45:05 -08001021 final Callbacks callbacks = mCallbacks.get();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 if (callbacks == null) {
1023 return;
1024 }
1025
Joe Onorato0589f0f2010-02-08 13:44:00 -08001026 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
1028
1029 synchronized (mLock) {
Joe Onoratof99f8c12009-10-31 17:27:36 -04001030 mBeforeFirstLoad = false;
1031
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 mAllAppsList.clear();
1033 if (apps != null) {
1034 long t = SystemClock.uptimeMillis();
1035
1036 int N = apps.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 for (int i=0; i<N && !mStopped; i++) {
1038 // This builds the icon bitmaps.
Joe Onorato0589f0f2010-02-08 13:44:00 -08001039 mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 }
Joe Onoratob0c27f22009-12-01 16:19:38 -08001041 Collections.sort(mAllAppsList.data, APP_NAME_COMPARATOR);
1042 Collections.sort(mAllAppsList.added, APP_NAME_COMPARATOR);
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001043 if (DEBUG_LOADERS) {
1044 Log.d(TAG, "cached app icons in "
1045 + (SystemClock.uptimeMillis()-t) + "ms");
1046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 }
1048 }
1049 }
1050
1051 private void bindAllApps() {
1052 synchronized (mLock) {
Joe Onorato0c4513e2009-11-19 12:24:48 -08001053 final ArrayList<ApplicationInfo> results
Romain Guy5c16f3e2010-01-12 17:24:58 -08001054 = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone();
Joe Onorato0c4513e2009-11-19 12:24:48 -08001055 // We're adding this now, so clear out this so we don't re-send them.
Romain Guy84f296c2009-11-04 15:00:44 -08001056 mAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onoratoc131b742010-03-11 15:45:05 -08001057 final Callbacks old = mCallbacks.get();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 mHandler.post(new Runnable() {
1059 public void run() {
Joe Onorato34b02492009-10-14 11:13:48 -07001060 final long t = SystemClock.uptimeMillis();
1061 final int count = results.size();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062
Joe Onoratoc131b742010-03-11 15:45:05 -08001063 Callbacks callbacks = tryGetCallbacks(old);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 if (callbacks != null) {
1065 callbacks.bindAllApplications(results);
1066 }
1067
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001068 if (DEBUG_LOADERS) {
1069 Log.d(TAG, "bound app " + count + " icons in "
Romain Guy5c16f3e2010-01-12 17:24:58 -08001070 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001071 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 }
1073 });
1074 }
1075 }
Joe Onoratobe386092009-11-17 17:32:16 -08001076
1077 public void dumpState() {
1078 Log.d(TAG, "mLoader.mLoaderThread.mContext=" + mContext);
1079 Log.d(TAG, "mLoader.mLoaderThread.mWaitThread=" + mWaitThread);
1080 Log.d(TAG, "mLoader.mLoaderThread.mIsLaunching=" + mIsLaunching);
1081 Log.d(TAG, "mLoader.mLoaderThread.mStopped=" + mStopped);
1082 Log.d(TAG, "mLoader.mLoaderThread.mWorkspaceDoneBinding=" + mWorkspaceDoneBinding);
1083 }
1084 }
1085
1086 public void dumpState() {
1087 Log.d(TAG, "mLoader.mLastWorkspaceSeq=" + mLoader.mLastWorkspaceSeq);
1088 Log.d(TAG, "mLoader.mWorkspaceSeq=" + mLoader.mWorkspaceSeq);
1089 Log.d(TAG, "mLoader.mLastAllAppsSeq=" + mLoader.mLastAllAppsSeq);
1090 Log.d(TAG, "mLoader.mAllAppsSeq=" + mLoader.mAllAppsSeq);
1091 Log.d(TAG, "mLoader.mItems size=" + mLoader.mItems.size());
1092 if (mLoaderThread != null) {
1093 mLoaderThread.dumpState();
1094 } else {
1095 Log.d(TAG, "mLoader.mLoaderThread=null");
1096 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097 }
1098 }
1099
1100 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001101 * This is called from the code that adds shortcuts from the intent receiver. This
1102 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001103 */
Joe Onorato56d82912010-03-07 14:32:10 -05001104 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001105 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107
Joe Onorato56d82912010-03-07 14:32:10 -05001108 /**
1109 * Make an ShortcutInfo object for a shortcut that is an application.
1110 *
1111 * If c is not null, then it will be used to fill in missing data like the title and icon.
1112 */
1113 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1114 Cursor c, int iconIndex, int titleIndex) {
1115 Bitmap icon = null;
1116 final ShortcutInfo info = new ShortcutInfo();
1117
1118 ComponentName componentName = intent.getComponent();
1119 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001120 return null;
1121 }
1122
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001123 // TODO: See if the PackageManager knows about this case. If it doesn't
1124 // then return null & delete this.
1125
Joe Onorato56d82912010-03-07 14:32:10 -05001126 // the resource -- This may implicitly give us back the fallback icon,
1127 // but don't worry about that. All we're doing with usingFallbackIcon is
1128 // to avoid saving lots of copies of that in the database, and most apps
1129 // have icons anyway.
1130 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1131 if (resolveInfo != null) {
1132 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001133 }
Joe Onorato56d82912010-03-07 14:32:10 -05001134 // the db
1135 if (icon == null) {
1136 if (c != null) {
1137 icon = getIconFromCursor(c, iconIndex);
1138 }
1139 }
1140 // the fallback icon
1141 if (icon == null) {
1142 icon = getFallbackIcon();
1143 info.usingFallbackIcon = true;
1144 }
1145 info.setIcon(icon);
1146
1147 // from the resource
1148 if (resolveInfo != null) {
1149 info.title = resolveInfo.activityInfo.loadLabel(manager);
1150 }
1151 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001153 if (c != null) {
1154 info.title = c.getString(titleIndex);
1155 }
1156 }
1157 // fall back to the class name of the activity
1158 if (info.title == null) {
1159 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001160 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1162 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001166 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001168 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001169 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1170 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171
Joe Onorato56d82912010-03-07 14:32:10 -05001172 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001173 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001176 // TODO: If there's an explicit component and we can't install that, delete it.
1177
Joe Onorato56d82912010-03-07 14:32:10 -05001178 info.title = c.getString(titleIndex);
1179
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180 int iconType = c.getInt(iconTypeIndex);
1181 switch (iconType) {
1182 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1183 String packageName = c.getString(iconPackageIndex);
1184 String resourceName = c.getString(iconResourceIndex);
1185 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001186 info.customIcon = false;
1187 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001190 if (resources != null) {
1191 final int id = resources.getIdentifier(resourceName, null, null);
1192 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001195 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
Joe Onorato56d82912010-03-07 14:32:10 -05001197 // the db
1198 if (icon == null) {
1199 icon = getIconFromCursor(c, iconIndex);
1200 }
1201 // the fallback icon
1202 if (icon == null) {
1203 icon = getFallbackIcon();
1204 info.usingFallbackIcon = true;
1205 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 break;
1207 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001208 icon = getIconFromCursor(c, iconIndex);
1209 if (icon == null) {
1210 icon = getFallbackIcon();
1211 info.customIcon = false;
1212 info.usingFallbackIcon = true;
1213 } else {
1214 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001216 break;
1217 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001218 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001219 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 info.customIcon = false;
1221 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001223 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 return info;
1225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226
Joe Onorato56d82912010-03-07 14:32:10 -05001227 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1228 if (false) {
1229 Log.d(TAG, "getIconFromCursor app="
1230 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1231 }
1232 byte[] data = c.getBlob(iconIndex);
1233 try {
1234 return BitmapFactory.decodeByteArray(data, 0, data.length);
1235 } catch (Exception e) {
1236 return null;
1237 }
1238 }
1239
Joe Onorato0589f0f2010-02-08 13:44:00 -08001240 ShortcutInfo addShortcut(Context context, Intent data,
1241 CellLayout.CellInfo cellInfo, boolean notify) {
1242
1243 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1244 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1245 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1246
1247 return info;
1248 }
1249
1250 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1251 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1252 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1253 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1254
1255 Bitmap icon = null;
1256 boolean filtered = false;
1257 boolean customIcon = false;
1258 ShortcutIconResource iconResource = null;
1259
1260 if (bitmap != null && bitmap instanceof Bitmap) {
1261 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1262 filtered = true;
1263 customIcon = true;
1264 } else {
1265 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1266 if (extra != null && extra instanceof ShortcutIconResource) {
1267 try {
1268 iconResource = (ShortcutIconResource) extra;
1269 final PackageManager packageManager = context.getPackageManager();
1270 Resources resources = packageManager.getResourcesForApplication(
1271 iconResource.packageName);
1272 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1273 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1274 } catch (Exception e) {
1275 Log.w(TAG, "Could not load shortcut icon: " + extra);
1276 }
1277 }
1278 }
1279
Joe Onorato0589f0f2010-02-08 13:44:00 -08001280 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001281
1282 if (icon == null) {
1283 icon = getFallbackIcon();
1284 info.usingFallbackIcon = true;
1285 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001286 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001287
Joe Onorato0589f0f2010-02-08 13:44:00 -08001288 info.title = name;
1289 info.intent = intent;
1290 info.customIcon = customIcon;
1291 info.iconResource = iconResource;
1292
1293 return info;
1294 }
1295
Joe Onorato9c1289c2009-08-17 11:03:03 -04001296 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1297 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1298
1299 int iconType = c.getInt(iconTypeIndex);
1300 switch (iconType) {
1301 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1302 String packageName = c.getString(iconPackageIndex);
1303 String resourceName = c.getString(iconResourceIndex);
1304 PackageManager packageManager = context.getPackageManager();
1305 try {
1306 Resources resources = packageManager.getResourcesForApplication(packageName);
1307 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001308 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1309 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001311 liveFolderInfo.icon = Utilities.createIconBitmap(
1312 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1313 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
1315 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1316 liveFolderInfo.iconResource.packageName = packageName;
1317 liveFolderInfo.iconResource.resourceName = resourceName;
1318 break;
1319 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001320 liveFolderInfo.icon = Utilities.createIconBitmap(
1321 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1322 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323 }
1324 }
1325
Joe Onorato56d82912010-03-07 14:32:10 -05001326 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1327 // If this icon doesn't have a custom icon, check to see
1328 // what's stored in the DB, and if it doesn't match what
1329 // we're going to show, store what we are going to show back
1330 // into the DB. We do this so when we're loading, if the
1331 // package manager can't find an icon (for example because
1332 // the app is on SD) then we can use that instead.
1333 if (info.onExternalStorage && !info.customIcon && !info.usingFallbackIcon) {
1334 boolean needSave;
1335 byte[] data = c.getBlob(iconIndex);
1336 try {
1337 if (data != null) {
1338 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1339 Bitmap loaded = info.getIcon(mIconCache);
1340 needSave = !saved.sameAs(loaded);
1341 } else {
1342 needSave = true;
1343 }
1344 } catch (Exception e) {
1345 needSave = true;
1346 }
1347 if (needSave) {
1348 Log.d(TAG, "going to save icon bitmap for info=" + info);
1349 // This is slower than is ideal, but this only happens either
1350 // after the froyo OTA or when the app is updated with a new
1351 // icon.
1352 updateItemInDatabase(context, info);
1353 }
1354 }
1355 }
1356
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 /**
1358 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1359 * or make a new one.
1360 */
1361 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1362 // See if a placeholder was created for us already
1363 FolderInfo folderInfo = folders.get(id);
1364 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1365 // No placeholder -- create a new instance
1366 folderInfo = new UserFolderInfo();
1367 folders.put(id, folderInfo);
1368 }
1369 return (UserFolderInfo) folderInfo;
1370 }
1371
1372 /**
1373 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1374 * new one.
1375 */
1376 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1377 // See if a placeholder was created for us already
1378 FolderInfo folderInfo = folders.get(id);
1379 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1380 // No placeholder -- create a new instance
1381 folderInfo = new LiveFolderInfo();
1382 folders.put(id, folderInfo);
1383 }
1384 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1388 String label = activityInfo.loadLabel(manager).toString();
1389 if (label == null) {
1390 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1391 if (label == null) {
1392 label = activityInfo.name;
1393 }
1394 }
1395 return label;
1396 }
1397
Joe Onorato9c1289c2009-08-17 11:03:03 -04001398 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001399 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 = new Comparator<ApplicationInfo>() {
1401 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1402 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 };
Joe Onoratobe386092009-11-17 17:32:16 -08001405
1406 public void dumpState() {
1407 Log.d(TAG, "mBeforeFirstLoad=" + mBeforeFirstLoad);
1408 Log.d(TAG, "mCallbacks=" + mCallbacks);
1409 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1410 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1411 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1412 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
1413 mLoader.dumpState();
1414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415}