blob: ee61a919ea95d9527c960c1ce10985e28d8628ea [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 Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040import android.os.Parcelable;
Romain Guy5c16f3e2010-01-12 17:24:58 -080041import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045
Joe Onorato9c1289c2009-08-17 11:03:03 -040046import java.lang.ref.WeakReference;
47import java.net.URISyntaxException;
48import java.text.Collator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import java.util.ArrayList;
Joe Onorato56d82912010-03-07 14:32:10 -050050import java.util.Arrays;
Joe Onorato9c1289c2009-08-17 11:03:03 -040051import java.util.Comparator;
52import java.util.Collections;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import java.util.HashMap;
54import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
Romain Guyedcce092010-03-04 13:03:17 -080056import com.android.launcher.R;
57
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * Maintains in-memory state of the Launcher. It is expected that there should be only one
60 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070061 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040063public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080064 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070066
Joe Onorato36115782010-06-17 13:28:48 -040067 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onoratod65d08e2010-04-20 15:43:37 -040068 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040069 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040070
Joe Onoratof99f8c12009-10-31 17:27:36 -040071 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 private final Object mLock = new Object();
73 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040074 private HandlerThread mWorkerThread;
75 private Handler mWorker;
76 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Joe Onoratocc67f472010-06-08 10:54:30 -070078 // We start off with everything not loaded. After that, we assume that
79 // our monitoring of the package manager provides all updates and we never
80 // need to do a requery. These are only ever touched from the loader thread.
81 private boolean mWorkspaceLoaded;
82 private boolean mAllAppsLoaded;
83
Joe Onorato9c1289c2009-08-17 11:03:03 -040084 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Joe Onorato36115782010-06-17 13:28:48 -040086 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080087 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -040088 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
89 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
90 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -080091
92 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093
Joe Onorato9c1289c2009-08-17 11:03:03 -040094 public interface Callbacks {
95 public int getCurrentWorkspaceScreen();
96 public void startBinding();
97 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -050098 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 public void finishBindingItems();
100 public void bindAppWidget(LauncherAppWidgetInfo info);
101 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500102 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
103 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400104 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Daniel Sandler843e8602010-06-07 14:59:01 -0400105 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Joe Onorato0589f0f2010-02-08 13:44:00 -0800108 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400109 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800110 mAllAppsList = new AllAppsList(iconCache);
111 mIconCache = iconCache;
112
113 mDefaultIcon = Utilities.createIconBitmap(
114 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400115
116 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400117
118 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato36115782010-06-17 13:28:48 -0400119
120 mWorkerThread = new HandlerThread("launcher-loader");
121 mWorkerThread.start();
122 mWorker = new Handler(mWorkerThread.getLooper());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800123 }
124
Joe Onorato56d82912010-03-07 14:32:10 -0500125 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800126 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Joe Onorato9c1289c2009-08-17 11:03:03 -0400129 /**
130 * Adds an item to the DB if it was not created previously, or move it to a new
131 * <container, screen, cellX, cellY>
132 */
133 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
134 int screen, int cellX, int cellY) {
135 if (item.container == ItemInfo.NO_ID) {
136 // From all apps
137 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
138 } else {
139 // From somewhere else
140 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 }
142 }
143
144 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400145 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700146 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400147 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
148 int cellX, int cellY) {
149 item.container = container;
150 item.screen = screen;
151 item.cellX = cellX;
152 item.cellY = cellY;
153
154 final ContentValues values = new ContentValues();
155 final ContentResolver cr = context.getContentResolver();
156
157 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
158 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
159 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
160 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
161
162 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700163 }
164
165 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 * Returns true if the shortcuts already exists in the database.
167 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static boolean shortcutExists(Context context, String title, Intent intent) {
170 final ContentResolver cr = context.getContentResolver();
171 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
172 new String[] { "title", "intent" }, "title=? and intent=?",
173 new String[] { title, intent.toUri(0) }, null);
174 boolean result = false;
175 try {
176 result = c.moveToFirst();
177 } finally {
178 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700181 }
182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 /**
184 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
185 */
186 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
187 final ContentResolver cr = context.getContentResolver();
188 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
189 "_id=? and (itemType=? or itemType=?)",
190 new String[] { String.valueOf(id),
191 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
192 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 try {
195 if (c.moveToFirst()) {
196 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
197 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
198 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
199 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
200 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
201 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 FolderInfo folderInfo = null;
204 switch (c.getInt(itemTypeIndex)) {
205 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
206 folderInfo = findOrMakeUserFolder(folderList, id);
207 break;
208 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
209 folderInfo = findOrMakeLiveFolder(folderList, id);
210 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700211 }
212
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 folderInfo.title = c.getString(titleIndex);
214 folderInfo.id = id;
215 folderInfo.container = c.getInt(containerIndex);
216 folderInfo.screen = c.getInt(screenIndex);
217 folderInfo.cellX = c.getInt(cellXIndex);
218 folderInfo.cellY = c.getInt(cellYIndex);
219
220 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 } finally {
223 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700224 }
225
226 return null;
227 }
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 /**
230 * Add an item to the database in a specified container. Sets the container, screen, cellX and
231 * cellY fields of the item. Also assigns an ID to the item.
232 */
233 static void addItemToDatabase(Context context, ItemInfo item, long container,
234 int screen, int cellX, int cellY, boolean notify) {
235 item.container = container;
236 item.screen = screen;
237 item.cellX = cellX;
238 item.cellY = cellY;
239
240 final ContentValues values = new ContentValues();
241 final ContentResolver cr = context.getContentResolver();
242
243 item.onAddToDatabase(values);
244
245 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
246 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
247
248 if (result != null) {
249 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700250 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700251 }
252
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 /**
254 * Update an item to the database in a specified container.
255 */
256 static void updateItemInDatabase(Context context, ItemInfo item) {
257 final ContentValues values = new ContentValues();
258 final ContentResolver cr = context.getContentResolver();
259
260 item.onAddToDatabase(values);
261
262 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
263 }
264
265 /**
266 * Removes the specified item from the database
267 * @param context
268 * @param item
269 */
270 static void deleteItemFromDatabase(Context context, ItemInfo item) {
271 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700272 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
273 new Thread("deleteItemFromDatabase") {
274 public void run() {
275 cr.delete(uriToDelete, null, null);
276 }
277 }.start();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 }
279
280 /**
281 * Remove the contents of the specified folder from the database
282 */
283 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
284 final ContentResolver cr = context.getContentResolver();
285
286 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
287 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
288 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
289 }
290
291 /**
292 * Set this as the current Launcher activity object for the loader.
293 */
294 public void initialize(Callbacks callbacks) {
295 synchronized (mLock) {
296 mCallbacks = new WeakReference<Callbacks>(callbacks);
297 }
298 }
299
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700300 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
302 * ACTION_PACKAGE_CHANGED.
303 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400304 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400305 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
306
307 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400308
Joe Onorato36115782010-06-17 13:28:48 -0400309 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
310 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
311 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
312 final String packageName = intent.getData().getSchemeSpecificPart();
313 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400314
Joe Onorato36115782010-06-17 13:28:48 -0400315 int op = PackageUpdatedTask.OP_NONE;
316
317 if (packageName == null || packageName.length() == 0) {
318 // they sent us a bad intent
319 return;
320 }
321
322 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
323 op = PackageUpdatedTask.OP_UPDATE;
324 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
325 if (!replacing) {
326 op = PackageUpdatedTask.OP_REMOVE;
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 op = PackageUpdatedTask.OP_ADD;
333 } else {
334 op = PackageUpdatedTask.OP_UPDATE;
335 }
336 }
337
338 if (op != PackageUpdatedTask.OP_NONE) {
339 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
340 }
341
342 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400343 // First, schedule to add these apps back in.
344 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
345 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
346 // Then, rebind everything.
Joe Onorato789065d2010-09-30 16:29:59 -0700347 startLoader(mApp, false);
Joe Onorato36115782010-06-17 13:28:48 -0400348
349 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
350 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
351 enqueuePackageUpdated(new PackageUpdatedTask(
352 PackageUpdatedTask.OP_UNAVAILABLE, packages));
353
Joe Onorato790c2d92010-06-11 00:14:11 -0700354 }
Joe Onorato36115782010-06-17 13:28:48 -0400355 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400356
Joe Onorato36115782010-06-17 13:28:48 -0400357 public void startLoader(Context context, boolean isLaunching) {
358 synchronized (mLock) {
359 if (DEBUG_LOADERS) {
360 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362
Joe Onorato36115782010-06-17 13:28:48 -0400363 // Don't bother to start the thread if we know it's not going to do anything
364 if (mCallbacks != null && mCallbacks.get() != null) {
365 // If there is already one running, tell it to stop.
366 LoaderTask oldTask = mLoaderTask;
367 if (oldTask != null) {
368 if (oldTask.isLaunching()) {
369 // don't downgrade isLaunching if we're already running
370 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500371 }
Joe Onorato36115782010-06-17 13:28:48 -0400372 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500373 }
Joe Onorato36115782010-06-17 13:28:48 -0400374 mLoaderTask = new LoaderTask(context, isLaunching);
375 mWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 }
378 }
379
Joe Onorato36115782010-06-17 13:28:48 -0400380 public void stopLoader() {
381 synchronized (mLock) {
382 if (mLoaderTask != null) {
383 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 }
385 }
Joe Onorato36115782010-06-17 13:28:48 -0400386 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387
Joe Onorato36115782010-06-17 13:28:48 -0400388 /**
389 * Runnable for the thread that loads the contents of the launcher:
390 * - workspace icons
391 * - widgets
392 * - all apps icons
393 */
394 private class LoaderTask implements Runnable {
395 private Context mContext;
396 private Thread mWaitThread;
397 private boolean mIsLaunching;
398 private boolean mStopped;
399 private boolean mLoadAndBindStepFinished;
400
401 LoaderTask(Context context, boolean isLaunching) {
402 mContext = context;
403 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404 }
405
Joe Onorato36115782010-06-17 13:28:48 -0400406 boolean isLaunching() {
407 return mIsLaunching;
408 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409
Joe Onorato36115782010-06-17 13:28:48 -0400410 private void loadAndBindWorkspace() {
411 // Load the workspace
412
413 // For now, just always reload the workspace. It's ~100 ms vs. the
414 // binding which takes many hundreds of ms.
415 // We can reconsider.
416 if (DEBUG_LOADERS) {
417 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400418 }
Joe Onorato36115782010-06-17 13:28:48 -0400419 if (true || !mWorkspaceLoaded) {
420 loadWorkspace();
421 if (mStopped) {
422 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400423 }
Joe Onorato36115782010-06-17 13:28:48 -0400424 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 }
426
Joe Onorato36115782010-06-17 13:28:48 -0400427 // Bind the workspace
428 bindWorkspace();
429 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400430
Joe Onorato36115782010-06-17 13:28:48 -0400431 private void waitForIdle() {
432 // Wait until the either we're stopped or the other threads are done.
433 // This way we don't start loading all apps until the workspace has settled
434 // down.
435 synchronized (LoaderTask.this) {
436 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700437
Joe Onorato36115782010-06-17 13:28:48 -0400438 mHandler.postIdle(new Runnable() {
439 public void run() {
440 synchronized (LoaderTask.this) {
441 mLoadAndBindStepFinished = true;
442 if (DEBUG_LOADERS) {
443 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400444 }
Joe Onorato36115782010-06-17 13:28:48 -0400445 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400446 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400447 }
Joe Onorato36115782010-06-17 13:28:48 -0400448 });
449
450 while (!mStopped && !mLoadAndBindStepFinished) {
451 try {
452 this.wait();
453 } catch (InterruptedException ex) {
454 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400455 }
456 }
Joe Onorato36115782010-06-17 13:28:48 -0400457 if (DEBUG_LOADERS) {
458 Log.d(TAG, "waited "
459 + (SystemClock.uptimeMillis()-workspaceWaitTime)
460 + "ms for previous step to finish binding");
461 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400462 }
Joe Onorato36115782010-06-17 13:28:48 -0400463 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400464
Joe Onorato36115782010-06-17 13:28:48 -0400465 public void run() {
466 // Optimize for end-user experience: if the Launcher is up and // running with the
467 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
468 // workspace first (default).
469 final Callbacks cbk = mCallbacks.get();
470 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400471
Joe Onorato36115782010-06-17 13:28:48 -0400472 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400473 // Elevate priority when Home launches for the first time to avoid
474 // starving at boot time. Staring at a blank home is not cool.
475 synchronized (mLock) {
476 android.os.Process.setThreadPriority(mIsLaunching
477 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
478 }
479
Daniel Sandler843e8602010-06-07 14:59:01 -0400480 if (loadWorkspaceFirst) {
481 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
482 loadAndBindWorkspace();
483 } else {
484 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700485 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400486 }
487
Joe Onorato36115782010-06-17 13:28:48 -0400488 if (mStopped) {
489 break keep_running;
490 }
491
492 // Whew! Hard work done. Slow us down, and wait until the UI thread has
493 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400494 synchronized (mLock) {
495 if (mIsLaunching) {
496 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
497 }
498 }
Joe Onorato36115782010-06-17 13:28:48 -0400499 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400500
501 // second step
502 if (loadWorkspaceFirst) {
503 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700504 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400505 } else {
506 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
507 loadAndBindWorkspace();
508 }
Joe Onorato36115782010-06-17 13:28:48 -0400509 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400510
Joe Onorato36115782010-06-17 13:28:48 -0400511 // Clear out this reference, otherwise we end up holding it until all of the
512 // callback runnables are done.
513 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514
Joe Onorato36115782010-06-17 13:28:48 -0400515 synchronized (mLock) {
516 // If we are still the last one to be scheduled, remove ourselves.
517 if (mLoaderTask == this) {
518 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 }
Joe Onorato36115782010-06-17 13:28:48 -0400520 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700521
Joe Onorato36115782010-06-17 13:28:48 -0400522 // Trigger a gc to try to clean up after the stuff is done, since the
523 // renderscript allocations aren't charged to the java heap.
524 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700525 mHandler.post(new Runnable() {
526 public void run() {
527 System.gc();
528 }
529 });
Joe Onorato36115782010-06-17 13:28:48 -0400530 } else {
531 mHandler.postIdle(new Runnable() {
532 public void run() {
533 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400534 }
Joe Onorato36115782010-06-17 13:28:48 -0400535 });
536 }
537 }
538
539 public void stopLocked() {
540 synchronized (LoaderTask.this) {
541 mStopped = true;
542 this.notify();
543 }
544 }
545
546 /**
547 * Gets the callbacks object. If we've been stopped, or if the launcher object
548 * has somehow been garbage collected, return null instead. Pass in the Callbacks
549 * object that was around when the deferred message was scheduled, and if there's
550 * a new Callbacks object around then also return null. This will save us from
551 * calling onto it with data that will be ignored.
552 */
553 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
554 synchronized (mLock) {
555 if (mStopped) {
556 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400557 }
Joe Onorato36115782010-06-17 13:28:48 -0400558
559 if (mCallbacks == null) {
560 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400561 }
Joe Onorato36115782010-06-17 13:28:48 -0400562
563 final Callbacks callbacks = mCallbacks.get();
564 if (callbacks != oldCallbacks) {
565 return null;
566 }
567 if (callbacks == null) {
568 Log.w(TAG, "no mCallbacks");
569 return null;
570 }
571
572 return callbacks;
573 }
574 }
575
576 // check & update map of what's occupied; used to discard overlapping/invalid items
577 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
578 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400579 return true;
580 }
581
Joe Onorato36115782010-06-17 13:28:48 -0400582 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
583 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
584 if (occupied[item.screen][x][y] != null) {
585 Log.e(TAG, "Error loading shortcut " + item
586 + " into cell (" + item.screen + ":"
587 + x + "," + y
588 + ") occupied by "
589 + occupied[item.screen][x][y]);
590 return false;
591 }
592 }
593 }
594 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
595 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
596 occupied[item.screen][x][y] = item;
597 }
598 }
599 return true;
600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601
Joe Onorato36115782010-06-17 13:28:48 -0400602 private void loadWorkspace() {
603 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400604
Joe Onorato36115782010-06-17 13:28:48 -0400605 final Context context = mContext;
606 final ContentResolver contentResolver = context.getContentResolver();
607 final PackageManager manager = context.getPackageManager();
608 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
609 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400610
Joe Onorato36115782010-06-17 13:28:48 -0400611 mItems.clear();
612 mAppWidgets.clear();
613 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800614
Joe Onorato36115782010-06-17 13:28:48 -0400615 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616
Joe Onorato36115782010-06-17 13:28:48 -0400617 final Cursor c = contentResolver.query(
618 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400619
Joe Onorato36115782010-06-17 13:28:48 -0400620 final ItemInfo occupied[][][] = new ItemInfo[Launcher.SCREEN_COUNT][Launcher.NUMBER_CELLS_X][Launcher.NUMBER_CELLS_Y];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621
Joe Onorato36115782010-06-17 13:28:48 -0400622 try {
623 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
624 final int intentIndex = c.getColumnIndexOrThrow
625 (LauncherSettings.Favorites.INTENT);
626 final int titleIndex = c.getColumnIndexOrThrow
627 (LauncherSettings.Favorites.TITLE);
628 final int iconTypeIndex = c.getColumnIndexOrThrow(
629 LauncherSettings.Favorites.ICON_TYPE);
630 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
631 final int iconPackageIndex = c.getColumnIndexOrThrow(
632 LauncherSettings.Favorites.ICON_PACKAGE);
633 final int iconResourceIndex = c.getColumnIndexOrThrow(
634 LauncherSettings.Favorites.ICON_RESOURCE);
635 final int containerIndex = c.getColumnIndexOrThrow(
636 LauncherSettings.Favorites.CONTAINER);
637 final int itemTypeIndex = c.getColumnIndexOrThrow(
638 LauncherSettings.Favorites.ITEM_TYPE);
639 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
640 LauncherSettings.Favorites.APPWIDGET_ID);
641 final int screenIndex = c.getColumnIndexOrThrow(
642 LauncherSettings.Favorites.SCREEN);
643 final int cellXIndex = c.getColumnIndexOrThrow
644 (LauncherSettings.Favorites.CELLX);
645 final int cellYIndex = c.getColumnIndexOrThrow
646 (LauncherSettings.Favorites.CELLY);
647 final int spanXIndex = c.getColumnIndexOrThrow
648 (LauncherSettings.Favorites.SPANX);
649 final int spanYIndex = c.getColumnIndexOrThrow(
650 LauncherSettings.Favorites.SPANY);
651 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
652 final int displayModeIndex = c.getColumnIndexOrThrow(
653 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654
Joe Onorato36115782010-06-17 13:28:48 -0400655 ShortcutInfo info;
656 String intentDescription;
657 LauncherAppWidgetInfo appWidgetInfo;
658 int container;
659 long id;
660 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661
Joe Onorato36115782010-06-17 13:28:48 -0400662 while (!mStopped && c.moveToNext()) {
663 try {
664 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665
Joe Onorato36115782010-06-17 13:28:48 -0400666 switch (itemType) {
667 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
668 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
669 intentDescription = c.getString(intentIndex);
670 try {
671 intent = Intent.parseUri(intentDescription, 0);
672 } catch (URISyntaxException e) {
673 continue;
674 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675
Joe Onorato36115782010-06-17 13:28:48 -0400676 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
677 info = getShortcutInfo(manager, intent, context, c, iconIndex,
678 titleIndex);
679 } else {
680 info = getShortcutInfo(c, context, iconTypeIndex,
681 iconPackageIndex, iconResourceIndex, iconIndex,
682 titleIndex);
683 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684
Joe Onorato36115782010-06-17 13:28:48 -0400685 if (info != null) {
686 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687
Joe Onorato36115782010-06-17 13:28:48 -0400688 info.intent = intent;
689 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400691 info.container = container;
692 info.screen = c.getInt(screenIndex);
693 info.cellX = c.getInt(cellXIndex);
694 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695
Daniel Sandler8802e962010-05-26 16:28:16 -0400696 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400697 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400698 break;
699 }
700
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400702 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
703 mItems.add(info);
704 break;
705 default:
706 // Item is in a user folder
707 UserFolderInfo folderInfo =
708 findOrMakeUserFolder(mFolders, container);
709 folderInfo.add(info);
710 break;
711 }
712 } else {
713 // Failed to load the shortcut, probably because the
714 // activity manager couldn't resolve it (maybe the app
715 // was uninstalled), or the db row was somehow screwed up.
716 // Delete it.
717 id = c.getLong(idIndex);
718 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
719 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
720 id, false), null, null);
721 }
722 break;
723
724 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
725 id = c.getLong(idIndex);
726 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
727
728 folderInfo.title = c.getString(titleIndex);
729
730 folderInfo.id = id;
731 container = c.getInt(containerIndex);
732 folderInfo.container = container;
733 folderInfo.screen = c.getInt(screenIndex);
734 folderInfo.cellX = c.getInt(cellXIndex);
735 folderInfo.cellY = c.getInt(cellYIndex);
736
737 // check & update map of what's occupied
738 if (!checkItemPlacement(occupied, folderInfo)) {
739 break;
740 }
741
742 switch (container) {
743 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
744 mItems.add(folderInfo);
745 break;
746 }
747
748 mFolders.put(folderInfo.id, folderInfo);
749 break;
750
751 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
752 id = c.getLong(idIndex);
753 Uri uri = Uri.parse(c.getString(uriIndex));
754
755 // Make sure the live folder exists
756 final ProviderInfo providerInfo =
757 context.getPackageManager().resolveContentProvider(
758 uri.getAuthority(), 0);
759
760 if (providerInfo == null && !isSafeMode) {
761 itemsToRemove.add(id);
762 } else {
763 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
764
765 intentDescription = c.getString(intentIndex);
766 intent = null;
767 if (intentDescription != null) {
768 try {
769 intent = Intent.parseUri(intentDescription, 0);
770 } catch (URISyntaxException e) {
771 // Ignore, a live folder might not have a base intent
772 }
773 }
774
775 liveFolderInfo.title = c.getString(titleIndex);
776 liveFolderInfo.id = id;
777 liveFolderInfo.uri = uri;
778 container = c.getInt(containerIndex);
779 liveFolderInfo.container = container;
780 liveFolderInfo.screen = c.getInt(screenIndex);
781 liveFolderInfo.cellX = c.getInt(cellXIndex);
782 liveFolderInfo.cellY = c.getInt(cellYIndex);
783 liveFolderInfo.baseIntent = intent;
784 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
785
786 // check & update map of what's occupied
787 if (!checkItemPlacement(occupied, liveFolderInfo)) {
788 break;
789 }
790
791 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
792 iconResourceIndex, liveFolderInfo);
793
794 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400796 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 break;
798 }
Joe Onorato36115782010-06-17 13:28:48 -0400799 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 }
Joe Onorato36115782010-06-17 13:28:48 -0400801 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800802
Joe Onorato36115782010-06-17 13:28:48 -0400803 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
804 // Read all Launcher-specific widget details
805 int appWidgetId = c.getInt(appWidgetIdIndex);
806 id = c.getLong(idIndex);
807
808 final AppWidgetProviderInfo provider =
809 widgets.getAppWidgetInfo(appWidgetId);
810
811 if (!isSafeMode && (provider == null || provider.provider == null ||
812 provider.provider.getPackageName() == null)) {
813 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
814 + id + " appWidgetId=" + appWidgetId);
815 itemsToRemove.add(id);
816 } else {
817 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
818 appWidgetInfo.id = id;
819 appWidgetInfo.screen = c.getInt(screenIndex);
820 appWidgetInfo.cellX = c.getInt(cellXIndex);
821 appWidgetInfo.cellY = c.getInt(cellYIndex);
822 appWidgetInfo.spanX = c.getInt(spanXIndex);
823 appWidgetInfo.spanY = c.getInt(spanYIndex);
824
825 container = c.getInt(containerIndex);
826 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
827 Log.e(TAG, "Widget found where container "
828 + "!= CONTAINER_DESKTOP -- ignoring!");
829 continue;
830 }
831 appWidgetInfo.container = c.getInt(containerIndex);
832
833 // check & update map of what's occupied
834 if (!checkItemPlacement(occupied, appWidgetInfo)) {
835 break;
836 }
837
838 mAppWidgets.add(appWidgetInfo);
839 }
840 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800841 }
Joe Onorato36115782010-06-17 13:28:48 -0400842 } catch (Exception e) {
843 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800844 }
845 }
Joe Onorato36115782010-06-17 13:28:48 -0400846 } finally {
847 c.close();
848 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800849
Joe Onorato36115782010-06-17 13:28:48 -0400850 if (itemsToRemove.size() > 0) {
851 ContentProviderClient client = contentResolver.acquireContentProviderClient(
852 LauncherSettings.Favorites.CONTENT_URI);
853 // Remove dead items
854 for (long id : itemsToRemove) {
855 if (DEBUG_LOADERS) {
856 Log.d(TAG, "Removed id = " + id);
857 }
858 // Don't notify content observers
859 try {
860 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
861 null, null);
862 } catch (RemoteException e) {
863 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400864 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800865 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866 }
867
Joe Onorato36115782010-06-17 13:28:48 -0400868 if (DEBUG_LOADERS) {
869 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
870 Log.d(TAG, "workspace layout: ");
871 for (int y = 0; y < Launcher.NUMBER_CELLS_Y; y++) {
872 String line = "";
873 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
874 if (s > 0) {
875 line += " | ";
876 }
877 for (int x = 0; x < Launcher.NUMBER_CELLS_X; x++) {
878 line += ((occupied[s][x][y] != null) ? "#" : ".");
879 }
880 }
881 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 }
Joe Onorato36115782010-06-17 13:28:48 -0400883 }
884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Joe Onorato36115782010-06-17 13:28:48 -0400886 /**
887 * Read everything out of our database.
888 */
889 private void bindWorkspace() {
890 final long t = SystemClock.uptimeMillis();
891
892 // Don't use these two variables in any of the callback runnables.
893 // Otherwise we hold a reference to them.
894 final Callbacks oldCallbacks = mCallbacks.get();
895 if (oldCallbacks == null) {
896 // This launcher has exited and nobody bothered to tell us. Just bail.
897 Log.w(TAG, "LoaderTask running with no launcher");
898 return;
899 }
900
901 int N;
902 // Tell the workspace that we're about to start firing items at it
903 mHandler.post(new Runnable() {
904 public void run() {
905 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
906 if (callbacks != null) {
907 callbacks.startBinding();
908 }
909 }
910 });
911 // Add the items to the workspace.
912 N = mItems.size();
913 for (int i=0; i<N; i+=ITEMS_CHUNK) {
914 final int start = i;
915 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 mHandler.post(new Runnable() {
917 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800918 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400920 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 }
922 }
923 });
Joe Onorato36115782010-06-17 13:28:48 -0400924 }
925 mHandler.post(new Runnable() {
926 public void run() {
927 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
928 if (callbacks != null) {
929 callbacks.bindFolders(mFolders);
930 }
931 }
932 });
933 // Wait until the queue goes empty.
934 mHandler.post(new Runnable() {
935 public void run() {
936 if (DEBUG_LOADERS) {
937 Log.d(TAG, "Going to start binding widgets soon.");
938 }
939 }
940 });
941 // Bind the widgets, one at a time.
942 // WARNING: this is calling into the workspace from the background thread,
943 // but since getCurrentScreen() just returns the int, we should be okay. This
944 // is just a hint for the order, and if it's wrong, we'll be okay.
945 // TODO: instead, we should have that push the current screen into here.
946 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
947 N = mAppWidgets.size();
948 // once for the current screen
949 for (int i=0; i<N; i++) {
950 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
951 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 mHandler.post(new Runnable() {
953 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800954 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400956 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 }
958 }
959 });
960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961 }
Joe Onorato36115782010-06-17 13:28:48 -0400962 // once for the other screens
963 for (int i=0; i<N; i++) {
964 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
965 if (widget.screen != currentScreen) {
966 mHandler.post(new Runnable() {
967 public void run() {
968 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
969 if (callbacks != null) {
970 callbacks.bindAppWidget(widget);
971 }
972 }
973 });
Joe Onoratocc67f472010-06-08 10:54:30 -0700974 }
975 }
Joe Onorato36115782010-06-17 13:28:48 -0400976 // Tell the workspace that we're done.
977 mHandler.post(new Runnable() {
978 public void run() {
979 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
980 if (callbacks != null) {
981 callbacks.finishBindingItems();
982 }
983 }
984 });
985 // If we're profiling, this is the last thing in the queue.
986 mHandler.post(new Runnable() {
987 public void run() {
988 if (DEBUG_LOADERS) {
989 Log.d(TAG, "bound workspace in "
990 + (SystemClock.uptimeMillis()-t) + "ms");
991 }
992 }
993 });
994 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700995
Joe Onorato36115782010-06-17 13:28:48 -0400996 private void loadAndBindAllApps() {
997 if (DEBUG_LOADERS) {
998 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
999 }
1000 if (!mAllAppsLoaded) {
1001 loadAllAppsByBatch();
1002 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001003 return;
1004 }
Joe Onorato36115782010-06-17 13:28:48 -04001005 mAllAppsLoaded = true;
1006 } else {
1007 onlyBindAllApps();
1008 }
1009 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001010
Joe Onorato36115782010-06-17 13:28:48 -04001011 private void onlyBindAllApps() {
1012 final Callbacks oldCallbacks = mCallbacks.get();
1013 if (oldCallbacks == null) {
1014 // This launcher has exited and nobody bothered to tell us. Just bail.
1015 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1016 return;
1017 }
1018
1019 // shallow copy
1020 final ArrayList<ApplicationInfo> list
1021 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1022 mHandler.post(new Runnable() {
1023 public void run() {
1024 final long t = SystemClock.uptimeMillis();
1025 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1026 if (callbacks != null) {
1027 callbacks.bindAllApplications(list);
1028 }
1029 if (DEBUG_LOADERS) {
1030 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1031 + (SystemClock.uptimeMillis()-t) + "ms");
1032 }
1033 }
1034 });
1035
1036 }
1037
1038 private void loadAllAppsByBatch() {
1039 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1040
1041 // Don't use these two variables in any of the callback runnables.
1042 // Otherwise we hold a reference to them.
1043 final Callbacks oldCallbacks = mCallbacks.get();
1044 if (oldCallbacks == null) {
1045 // This launcher has exited and nobody bothered to tell us. Just bail.
1046 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1047 return;
1048 }
1049
1050 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1051 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1052
1053 final PackageManager packageManager = mContext.getPackageManager();
1054 List<ResolveInfo> apps = null;
1055
1056 int N = Integer.MAX_VALUE;
1057
1058 int startIndex;
1059 int i=0;
1060 int batchSize = -1;
1061 while (i < N && !mStopped) {
1062 if (i == 0) {
1063 mAllAppsList.clear();
1064 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1065 apps = packageManager.queryIntentActivities(mainIntent, 0);
1066 if (DEBUG_LOADERS) {
1067 Log.d(TAG, "queryIntentActivities took "
1068 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1069 }
1070 if (apps == null) {
1071 return;
1072 }
1073 N = apps.size();
1074 if (DEBUG_LOADERS) {
1075 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1076 }
1077 if (N == 0) {
1078 // There are no apps?!?
1079 return;
1080 }
1081 if (mBatchSize == 0) {
1082 batchSize = N;
1083 } else {
1084 batchSize = mBatchSize;
1085 }
1086
1087 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1088 Collections.sort(apps,
1089 new ResolveInfo.DisplayNameComparator(packageManager));
1090 if (DEBUG_LOADERS) {
1091 Log.d(TAG, "sort took "
1092 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1093 }
1094 }
1095
1096 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1097
1098 startIndex = i;
1099 for (int j=0; i<N && j<batchSize; j++) {
1100 // This builds the icon bitmaps.
1101 mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
1102 i++;
1103 }
1104
1105 final boolean first = i <= batchSize;
1106 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1107 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1108 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1109
Joe Onoratocc67f472010-06-08 10:54:30 -07001110 mHandler.post(new Runnable() {
1111 public void run() {
1112 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001113 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001114 if (first) {
1115 callbacks.bindAllApplications(added);
1116 } else {
1117 callbacks.bindAppsAdded(added);
1118 }
1119 if (DEBUG_LOADERS) {
1120 Log.d(TAG, "bound " + added.size() + " apps in "
1121 + (SystemClock.uptimeMillis() - t) + "ms");
1122 }
1123 } else {
1124 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001125 }
1126 }
1127 });
1128
Daniel Sandlerdca66122010-04-13 16:23:58 -04001129 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001130 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1131 + (SystemClock.uptimeMillis()-t2) + "ms");
1132 }
1133
1134 if (mAllAppsLoadDelay > 0 && i < N) {
1135 try {
1136 if (DEBUG_LOADERS) {
1137 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1138 }
1139 Thread.sleep(mAllAppsLoadDelay);
1140 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 }
1142 }
1143
Joe Onorato36115782010-06-17 13:28:48 -04001144 if (DEBUG_LOADERS) {
1145 Log.d(TAG, "cached all " + N + " apps in "
1146 + (SystemClock.uptimeMillis()-t) + "ms"
1147 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001148 }
1149 }
1150
1151 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001152 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1153 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1154 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1155 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1156 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1157 }
1158 }
1159
1160 void enqueuePackageUpdated(PackageUpdatedTask task) {
1161 mWorker.post(task);
1162 }
1163
1164 private class PackageUpdatedTask implements Runnable {
1165 int mOp;
1166 String[] mPackages;
1167
1168 public static final int OP_NONE = 0;
1169 public static final int OP_ADD = 1;
1170 public static final int OP_UPDATE = 2;
1171 public static final int OP_REMOVE = 3; // uninstlled
1172 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1173
1174
1175 public PackageUpdatedTask(int op, String[] packages) {
1176 mOp = op;
1177 mPackages = packages;
1178 }
1179
1180 public void run() {
1181 final Context context = mApp;
1182
1183 final String[] packages = mPackages;
1184 final int N = packages.length;
1185 switch (mOp) {
1186 case OP_ADD:
1187 for (int i=0; i<N; i++) {
1188 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1189 mAllAppsList.addPackage(context, packages[i]);
1190 }
1191 break;
1192 case OP_UPDATE:
1193 for (int i=0; i<N; i++) {
1194 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1195 mAllAppsList.updatePackage(context, packages[i]);
1196 }
1197 break;
1198 case OP_REMOVE:
1199 case OP_UNAVAILABLE:
1200 for (int i=0; i<N; i++) {
1201 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1202 mAllAppsList.removePackage(packages[i]);
1203 }
1204 break;
1205 }
1206
1207 ArrayList<ApplicationInfo> added = null;
1208 ArrayList<ApplicationInfo> removed = null;
1209 ArrayList<ApplicationInfo> modified = null;
1210
1211 if (mAllAppsList.added.size() > 0) {
1212 added = mAllAppsList.added;
1213 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1214 }
1215 if (mAllAppsList.removed.size() > 0) {
1216 removed = mAllAppsList.removed;
1217 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1218 for (ApplicationInfo info: removed) {
1219 mIconCache.remove(info.intent.getComponent());
1220 }
1221 }
1222 if (mAllAppsList.modified.size() > 0) {
1223 modified = mAllAppsList.modified;
1224 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1225 }
1226
1227 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1228 if (callbacks == null) {
1229 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1230 return;
1231 }
1232
1233 if (added != null) {
1234 final ArrayList<ApplicationInfo> addedFinal = added;
1235 mHandler.post(new Runnable() {
1236 public void run() {
1237 if (callbacks == mCallbacks.get()) {
1238 callbacks.bindAppsAdded(addedFinal);
1239 }
1240 }
1241 });
1242 }
1243 if (modified != null) {
1244 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1245 mHandler.post(new Runnable() {
1246 public void run() {
1247 if (callbacks == mCallbacks.get()) {
1248 callbacks.bindAppsUpdated(modifiedFinal);
1249 }
1250 }
1251 });
1252 }
1253 if (removed != null) {
1254 final boolean permanent = mOp != OP_UNAVAILABLE;
1255 final ArrayList<ApplicationInfo> removedFinal = removed;
1256 mHandler.post(new Runnable() {
1257 public void run() {
1258 if (callbacks == mCallbacks.get()) {
1259 callbacks.bindAppsRemoved(removedFinal, permanent);
1260 }
1261 }
1262 });
Joe Onoratobe386092009-11-17 17:32:16 -08001263 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 }
1265 }
1266
1267 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001268 * This is called from the code that adds shortcuts from the intent receiver. This
1269 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001270 */
Joe Onorato56d82912010-03-07 14:32:10 -05001271 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001272 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001273 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001274
Joe Onorato56d82912010-03-07 14:32:10 -05001275 /**
1276 * Make an ShortcutInfo object for a shortcut that is an application.
1277 *
1278 * If c is not null, then it will be used to fill in missing data like the title and icon.
1279 */
1280 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1281 Cursor c, int iconIndex, int titleIndex) {
1282 Bitmap icon = null;
1283 final ShortcutInfo info = new ShortcutInfo();
1284
1285 ComponentName componentName = intent.getComponent();
1286 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001287 return null;
1288 }
1289
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001290 // TODO: See if the PackageManager knows about this case. If it doesn't
1291 // then return null & delete this.
1292
Joe Onorato56d82912010-03-07 14:32:10 -05001293 // the resource -- This may implicitly give us back the fallback icon,
1294 // but don't worry about that. All we're doing with usingFallbackIcon is
1295 // to avoid saving lots of copies of that in the database, and most apps
1296 // have icons anyway.
1297 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1298 if (resolveInfo != null) {
1299 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001300 }
Joe Onorato56d82912010-03-07 14:32:10 -05001301 // the db
1302 if (icon == null) {
1303 if (c != null) {
1304 icon = getIconFromCursor(c, iconIndex);
1305 }
1306 }
1307 // the fallback icon
1308 if (icon == null) {
1309 icon = getFallbackIcon();
1310 info.usingFallbackIcon = true;
1311 }
1312 info.setIcon(icon);
1313
1314 // from the resource
1315 if (resolveInfo != null) {
1316 info.title = resolveInfo.activityInfo.loadLabel(manager);
1317 }
1318 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001319 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001320 if (c != null) {
1321 info.title = c.getString(titleIndex);
1322 }
1323 }
1324 // fall back to the class name of the activity
1325 if (info.title == null) {
1326 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1329 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001333 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001335 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001336 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1337 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338
Joe Onorato56d82912010-03-07 14:32:10 -05001339 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001340 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001343 // TODO: If there's an explicit component and we can't install that, delete it.
1344
Joe Onorato56d82912010-03-07 14:32:10 -05001345 info.title = c.getString(titleIndex);
1346
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 int iconType = c.getInt(iconTypeIndex);
1348 switch (iconType) {
1349 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1350 String packageName = c.getString(iconPackageIndex);
1351 String resourceName = c.getString(iconResourceIndex);
1352 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001353 info.customIcon = false;
1354 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001357 if (resources != null) {
1358 final int id = resources.getIdentifier(resourceName, null, null);
1359 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1360 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001362 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
Joe Onorato56d82912010-03-07 14:32:10 -05001364 // the db
1365 if (icon == null) {
1366 icon = getIconFromCursor(c, iconIndex);
1367 }
1368 // the fallback icon
1369 if (icon == null) {
1370 icon = getFallbackIcon();
1371 info.usingFallbackIcon = true;
1372 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 break;
1374 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001375 icon = getIconFromCursor(c, iconIndex);
1376 if (icon == null) {
1377 icon = getFallbackIcon();
1378 info.customIcon = false;
1379 info.usingFallbackIcon = true;
1380 } else {
1381 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 break;
1384 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001385 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001386 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 info.customIcon = false;
1388 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001390 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391 return info;
1392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393
Joe Onorato56d82912010-03-07 14:32:10 -05001394 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1395 if (false) {
1396 Log.d(TAG, "getIconFromCursor app="
1397 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1398 }
1399 byte[] data = c.getBlob(iconIndex);
1400 try {
1401 return BitmapFactory.decodeByteArray(data, 0, data.length);
1402 } catch (Exception e) {
1403 return null;
1404 }
1405 }
1406
Joe Onorato0589f0f2010-02-08 13:44:00 -08001407 ShortcutInfo addShortcut(Context context, Intent data,
1408 CellLayout.CellInfo cellInfo, boolean notify) {
1409
1410 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1411 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1412 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1413
1414 return info;
1415 }
1416
1417 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1418 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1419 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1420 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1421
1422 Bitmap icon = null;
1423 boolean filtered = false;
1424 boolean customIcon = false;
1425 ShortcutIconResource iconResource = null;
1426
1427 if (bitmap != null && bitmap instanceof Bitmap) {
1428 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1429 filtered = true;
1430 customIcon = true;
1431 } else {
1432 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1433 if (extra != null && extra instanceof ShortcutIconResource) {
1434 try {
1435 iconResource = (ShortcutIconResource) extra;
1436 final PackageManager packageManager = context.getPackageManager();
1437 Resources resources = packageManager.getResourcesForApplication(
1438 iconResource.packageName);
1439 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1440 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1441 } catch (Exception e) {
1442 Log.w(TAG, "Could not load shortcut icon: " + extra);
1443 }
1444 }
1445 }
1446
Joe Onorato0589f0f2010-02-08 13:44:00 -08001447 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001448
1449 if (icon == null) {
1450 icon = getFallbackIcon();
1451 info.usingFallbackIcon = true;
1452 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001453 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001454
Joe Onorato0589f0f2010-02-08 13:44:00 -08001455 info.title = name;
1456 info.intent = intent;
1457 info.customIcon = customIcon;
1458 info.iconResource = iconResource;
1459
1460 return info;
1461 }
1462
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1464 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1465
1466 int iconType = c.getInt(iconTypeIndex);
1467 switch (iconType) {
1468 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1469 String packageName = c.getString(iconPackageIndex);
1470 String resourceName = c.getString(iconResourceIndex);
1471 PackageManager packageManager = context.getPackageManager();
1472 try {
1473 Resources resources = packageManager.getResourcesForApplication(packageName);
1474 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001475 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1476 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001477 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001478 liveFolderInfo.icon = Utilities.createIconBitmap(
1479 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1480 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481 }
1482 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1483 liveFolderInfo.iconResource.packageName = packageName;
1484 liveFolderInfo.iconResource.resourceName = resourceName;
1485 break;
1486 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001487 liveFolderInfo.icon = Utilities.createIconBitmap(
1488 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1489 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 }
1491 }
1492
Joe Onorato56d82912010-03-07 14:32:10 -05001493 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1494 // If this icon doesn't have a custom icon, check to see
1495 // what's stored in the DB, and if it doesn't match what
1496 // we're going to show, store what we are going to show back
1497 // into the DB. We do this so when we're loading, if the
1498 // package manager can't find an icon (for example because
1499 // the app is on SD) then we can use that instead.
Joe Onoratof5ed4f12010-08-30 18:30:15 -07001500 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001501 boolean needSave;
1502 byte[] data = c.getBlob(iconIndex);
1503 try {
1504 if (data != null) {
1505 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1506 Bitmap loaded = info.getIcon(mIconCache);
1507 needSave = !saved.sameAs(loaded);
1508 } else {
1509 needSave = true;
1510 }
1511 } catch (Exception e) {
1512 needSave = true;
1513 }
1514 if (needSave) {
1515 Log.d(TAG, "going to save icon bitmap for info=" + info);
1516 // This is slower than is ideal, but this only happens either
1517 // after the froyo OTA or when the app is updated with a new
1518 // icon.
1519 updateItemInDatabase(context, info);
1520 }
1521 }
1522 }
1523
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524 /**
1525 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1526 * or make a new one.
1527 */
1528 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1529 // See if a placeholder was created for us already
1530 FolderInfo folderInfo = folders.get(id);
1531 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1532 // No placeholder -- create a new instance
1533 folderInfo = new UserFolderInfo();
1534 folders.put(id, folderInfo);
1535 }
1536 return (UserFolderInfo) folderInfo;
1537 }
1538
1539 /**
1540 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1541 * new one.
1542 */
1543 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1544 // See if a placeholder was created for us already
1545 FolderInfo folderInfo = folders.get(id);
1546 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1547 // No placeholder -- create a new instance
1548 folderInfo = new LiveFolderInfo();
1549 folders.put(id, folderInfo);
1550 }
1551 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1555 String label = activityInfo.loadLabel(manager).toString();
1556 if (label == null) {
1557 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1558 if (label == null) {
1559 label = activityInfo.name;
1560 }
1561 }
1562 return label;
1563 }
1564
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001566 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567 = new Comparator<ApplicationInfo>() {
1568 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1569 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 };
Joe Onoratobe386092009-11-17 17:32:16 -08001572
1573 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001574 Log.d(TAG, "mCallbacks=" + mCallbacks);
1575 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1576 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1577 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1578 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001579 Log.d(TAG, "mItems size=" + mItems.size());
1580 if (mLoaderTask != null) {
1581 mLoaderTask.dumpState();
1582 } else {
1583 Log.d(TAG, "mLoaderTask=null");
1584 }
Joe Onoratobe386092009-11-17 17:32:16 -08001585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586}