blob: 06fcfb31945645ec7ea69f15f8766498292c26b2 [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)) {
343 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
344 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
345
346 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
347 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
348 enqueuePackageUpdated(new PackageUpdatedTask(
349 PackageUpdatedTask.OP_UNAVAILABLE, packages));
350
Joe Onorato790c2d92010-06-11 00:14:11 -0700351 }
Joe Onorato36115782010-06-17 13:28:48 -0400352 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400353
Joe Onorato36115782010-06-17 13:28:48 -0400354 public void startLoader(Context context, boolean isLaunching) {
355 synchronized (mLock) {
356 if (DEBUG_LOADERS) {
357 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
358 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359
Joe Onorato36115782010-06-17 13:28:48 -0400360 // Don't bother to start the thread if we know it's not going to do anything
361 if (mCallbacks != null && mCallbacks.get() != null) {
362 // If there is already one running, tell it to stop.
363 LoaderTask oldTask = mLoaderTask;
364 if (oldTask != null) {
365 if (oldTask.isLaunching()) {
366 // don't downgrade isLaunching if we're already running
367 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500368 }
Joe Onorato36115782010-06-17 13:28:48 -0400369 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500370 }
Joe Onorato36115782010-06-17 13:28:48 -0400371 mLoaderTask = new LoaderTask(context, isLaunching);
372 mWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 }
375 }
376
Joe Onorato36115782010-06-17 13:28:48 -0400377 public void stopLoader() {
378 synchronized (mLock) {
379 if (mLoaderTask != null) {
380 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 }
382 }
Joe Onorato36115782010-06-17 13:28:48 -0400383 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384
Joe Onorato36115782010-06-17 13:28:48 -0400385 /**
386 * Runnable for the thread that loads the contents of the launcher:
387 * - workspace icons
388 * - widgets
389 * - all apps icons
390 */
391 private class LoaderTask implements Runnable {
392 private Context mContext;
393 private Thread mWaitThread;
394 private boolean mIsLaunching;
395 private boolean mStopped;
396 private boolean mLoadAndBindStepFinished;
397
398 LoaderTask(Context context, boolean isLaunching) {
399 mContext = context;
400 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400401 }
402
Joe Onorato36115782010-06-17 13:28:48 -0400403 boolean isLaunching() {
404 return mIsLaunching;
405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406
Joe Onorato36115782010-06-17 13:28:48 -0400407 private void loadAndBindWorkspace() {
408 // Load the workspace
409
410 // For now, just always reload the workspace. It's ~100 ms vs. the
411 // binding which takes many hundreds of ms.
412 // We can reconsider.
413 if (DEBUG_LOADERS) {
414 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400415 }
Joe Onorato36115782010-06-17 13:28:48 -0400416 if (true || !mWorkspaceLoaded) {
417 loadWorkspace();
418 if (mStopped) {
419 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400420 }
Joe Onorato36115782010-06-17 13:28:48 -0400421 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422 }
423
Joe Onorato36115782010-06-17 13:28:48 -0400424 // Bind the workspace
425 bindWorkspace();
426 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400427
Joe Onorato36115782010-06-17 13:28:48 -0400428 private void waitForIdle() {
429 // Wait until the either we're stopped or the other threads are done.
430 // This way we don't start loading all apps until the workspace has settled
431 // down.
432 synchronized (LoaderTask.this) {
433 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700434
Joe Onorato36115782010-06-17 13:28:48 -0400435 mHandler.postIdle(new Runnable() {
436 public void run() {
437 synchronized (LoaderTask.this) {
438 mLoadAndBindStepFinished = true;
439 if (DEBUG_LOADERS) {
440 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400441 }
Joe Onorato36115782010-06-17 13:28:48 -0400442 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400443 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400444 }
Joe Onorato36115782010-06-17 13:28:48 -0400445 });
446
447 while (!mStopped && !mLoadAndBindStepFinished) {
448 try {
449 this.wait();
450 } catch (InterruptedException ex) {
451 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400452 }
453 }
Joe Onorato36115782010-06-17 13:28:48 -0400454 if (DEBUG_LOADERS) {
455 Log.d(TAG, "waited "
456 + (SystemClock.uptimeMillis()-workspaceWaitTime)
457 + "ms for previous step to finish binding");
458 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400459 }
Joe Onorato36115782010-06-17 13:28:48 -0400460 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400461
Joe Onorato36115782010-06-17 13:28:48 -0400462 public void run() {
463 // Optimize for end-user experience: if the Launcher is up and // running with the
464 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
465 // workspace first (default).
466 final Callbacks cbk = mCallbacks.get();
467 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400468
Joe Onorato36115782010-06-17 13:28:48 -0400469 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400470 // Elevate priority when Home launches for the first time to avoid
471 // starving at boot time. Staring at a blank home is not cool.
472 synchronized (mLock) {
473 android.os.Process.setThreadPriority(mIsLaunching
474 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
475 }
476
Daniel Sandler843e8602010-06-07 14:59:01 -0400477 if (loadWorkspaceFirst) {
478 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
479 loadAndBindWorkspace();
480 } else {
481 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700482 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400483 }
484
Joe Onorato36115782010-06-17 13:28:48 -0400485 if (mStopped) {
486 break keep_running;
487 }
488
489 // Whew! Hard work done. Slow us down, and wait until the UI thread has
490 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400491 synchronized (mLock) {
492 if (mIsLaunching) {
493 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
494 }
495 }
Joe Onorato36115782010-06-17 13:28:48 -0400496 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400497
498 // second step
499 if (loadWorkspaceFirst) {
500 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700501 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400502 } else {
503 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
504 loadAndBindWorkspace();
505 }
Joe Onorato36115782010-06-17 13:28:48 -0400506 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400507
Joe Onorato36115782010-06-17 13:28:48 -0400508 // Clear out this reference, otherwise we end up holding it until all of the
509 // callback runnables are done.
510 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400511
Joe Onorato36115782010-06-17 13:28:48 -0400512 synchronized (mLock) {
513 // If we are still the last one to be scheduled, remove ourselves.
514 if (mLoaderTask == this) {
515 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 }
Joe Onorato36115782010-06-17 13:28:48 -0400517 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700518
Joe Onorato36115782010-06-17 13:28:48 -0400519 // Trigger a gc to try to clean up after the stuff is done, since the
520 // renderscript allocations aren't charged to the java heap.
521 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700522 mHandler.post(new Runnable() {
523 public void run() {
524 System.gc();
525 }
526 });
Joe Onorato36115782010-06-17 13:28:48 -0400527 } else {
528 mHandler.postIdle(new Runnable() {
529 public void run() {
530 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400531 }
Joe Onorato36115782010-06-17 13:28:48 -0400532 });
533 }
534 }
535
536 public void stopLocked() {
537 synchronized (LoaderTask.this) {
538 mStopped = true;
539 this.notify();
540 }
541 }
542
543 /**
544 * Gets the callbacks object. If we've been stopped, or if the launcher object
545 * has somehow been garbage collected, return null instead. Pass in the Callbacks
546 * object that was around when the deferred message was scheduled, and if there's
547 * a new Callbacks object around then also return null. This will save us from
548 * calling onto it with data that will be ignored.
549 */
550 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
551 synchronized (mLock) {
552 if (mStopped) {
553 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400554 }
Joe Onorato36115782010-06-17 13:28:48 -0400555
556 if (mCallbacks == null) {
557 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400558 }
Joe Onorato36115782010-06-17 13:28:48 -0400559
560 final Callbacks callbacks = mCallbacks.get();
561 if (callbacks != oldCallbacks) {
562 return null;
563 }
564 if (callbacks == null) {
565 Log.w(TAG, "no mCallbacks");
566 return null;
567 }
568
569 return callbacks;
570 }
571 }
572
573 // check & update map of what's occupied; used to discard overlapping/invalid items
574 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
575 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400576 return true;
577 }
578
Joe Onorato36115782010-06-17 13:28:48 -0400579 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
580 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
581 if (occupied[item.screen][x][y] != null) {
582 Log.e(TAG, "Error loading shortcut " + item
583 + " into cell (" + item.screen + ":"
584 + x + "," + y
585 + ") occupied by "
586 + occupied[item.screen][x][y]);
587 return false;
588 }
589 }
590 }
591 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
592 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
593 occupied[item.screen][x][y] = item;
594 }
595 }
596 return true;
597 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400598
Joe Onorato36115782010-06-17 13:28:48 -0400599 private void loadWorkspace() {
600 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601
Joe Onorato36115782010-06-17 13:28:48 -0400602 final Context context = mContext;
603 final ContentResolver contentResolver = context.getContentResolver();
604 final PackageManager manager = context.getPackageManager();
605 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
606 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400607
Joe Onorato36115782010-06-17 13:28:48 -0400608 mItems.clear();
609 mAppWidgets.clear();
610 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800611
Joe Onorato36115782010-06-17 13:28:48 -0400612 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613
Joe Onorato36115782010-06-17 13:28:48 -0400614 final Cursor c = contentResolver.query(
615 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400616
Joe Onorato36115782010-06-17 13:28:48 -0400617 final ItemInfo occupied[][][] = new ItemInfo[Launcher.SCREEN_COUNT][Launcher.NUMBER_CELLS_X][Launcher.NUMBER_CELLS_Y];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618
Joe Onorato36115782010-06-17 13:28:48 -0400619 try {
620 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
621 final int intentIndex = c.getColumnIndexOrThrow
622 (LauncherSettings.Favorites.INTENT);
623 final int titleIndex = c.getColumnIndexOrThrow
624 (LauncherSettings.Favorites.TITLE);
625 final int iconTypeIndex = c.getColumnIndexOrThrow(
626 LauncherSettings.Favorites.ICON_TYPE);
627 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
628 final int iconPackageIndex = c.getColumnIndexOrThrow(
629 LauncherSettings.Favorites.ICON_PACKAGE);
630 final int iconResourceIndex = c.getColumnIndexOrThrow(
631 LauncherSettings.Favorites.ICON_RESOURCE);
632 final int containerIndex = c.getColumnIndexOrThrow(
633 LauncherSettings.Favorites.CONTAINER);
634 final int itemTypeIndex = c.getColumnIndexOrThrow(
635 LauncherSettings.Favorites.ITEM_TYPE);
636 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
637 LauncherSettings.Favorites.APPWIDGET_ID);
638 final int screenIndex = c.getColumnIndexOrThrow(
639 LauncherSettings.Favorites.SCREEN);
640 final int cellXIndex = c.getColumnIndexOrThrow
641 (LauncherSettings.Favorites.CELLX);
642 final int cellYIndex = c.getColumnIndexOrThrow
643 (LauncherSettings.Favorites.CELLY);
644 final int spanXIndex = c.getColumnIndexOrThrow
645 (LauncherSettings.Favorites.SPANX);
646 final int spanYIndex = c.getColumnIndexOrThrow(
647 LauncherSettings.Favorites.SPANY);
648 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
649 final int displayModeIndex = c.getColumnIndexOrThrow(
650 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651
Joe Onorato36115782010-06-17 13:28:48 -0400652 ShortcutInfo info;
653 String intentDescription;
654 LauncherAppWidgetInfo appWidgetInfo;
655 int container;
656 long id;
657 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658
Joe Onorato36115782010-06-17 13:28:48 -0400659 while (!mStopped && c.moveToNext()) {
660 try {
661 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662
Joe Onorato36115782010-06-17 13:28:48 -0400663 switch (itemType) {
664 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
665 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
666 intentDescription = c.getString(intentIndex);
667 try {
668 intent = Intent.parseUri(intentDescription, 0);
669 } catch (URISyntaxException e) {
670 continue;
671 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672
Joe Onorato36115782010-06-17 13:28:48 -0400673 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
674 info = getShortcutInfo(manager, intent, context, c, iconIndex,
675 titleIndex);
676 } else {
677 info = getShortcutInfo(c, context, iconTypeIndex,
678 iconPackageIndex, iconResourceIndex, iconIndex,
679 titleIndex);
680 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681
Joe Onorato36115782010-06-17 13:28:48 -0400682 if (info != null) {
683 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684
Joe Onorato36115782010-06-17 13:28:48 -0400685 info.intent = intent;
686 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400688 info.container = container;
689 info.screen = c.getInt(screenIndex);
690 info.cellX = c.getInt(cellXIndex);
691 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692
Daniel Sandler8802e962010-05-26 16:28:16 -0400693 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400694 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400695 break;
696 }
697
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400699 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
700 mItems.add(info);
701 break;
702 default:
703 // Item is in a user folder
704 UserFolderInfo folderInfo =
705 findOrMakeUserFolder(mFolders, container);
706 folderInfo.add(info);
707 break;
708 }
709 } else {
710 // Failed to load the shortcut, probably because the
711 // activity manager couldn't resolve it (maybe the app
712 // was uninstalled), or the db row was somehow screwed up.
713 // Delete it.
714 id = c.getLong(idIndex);
715 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
716 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
717 id, false), null, null);
718 }
719 break;
720
721 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
722 id = c.getLong(idIndex);
723 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
724
725 folderInfo.title = c.getString(titleIndex);
726
727 folderInfo.id = id;
728 container = c.getInt(containerIndex);
729 folderInfo.container = container;
730 folderInfo.screen = c.getInt(screenIndex);
731 folderInfo.cellX = c.getInt(cellXIndex);
732 folderInfo.cellY = c.getInt(cellYIndex);
733
734 // check & update map of what's occupied
735 if (!checkItemPlacement(occupied, folderInfo)) {
736 break;
737 }
738
739 switch (container) {
740 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
741 mItems.add(folderInfo);
742 break;
743 }
744
745 mFolders.put(folderInfo.id, folderInfo);
746 break;
747
748 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
749 id = c.getLong(idIndex);
750 Uri uri = Uri.parse(c.getString(uriIndex));
751
752 // Make sure the live folder exists
753 final ProviderInfo providerInfo =
754 context.getPackageManager().resolveContentProvider(
755 uri.getAuthority(), 0);
756
757 if (providerInfo == null && !isSafeMode) {
758 itemsToRemove.add(id);
759 } else {
760 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
761
762 intentDescription = c.getString(intentIndex);
763 intent = null;
764 if (intentDescription != null) {
765 try {
766 intent = Intent.parseUri(intentDescription, 0);
767 } catch (URISyntaxException e) {
768 // Ignore, a live folder might not have a base intent
769 }
770 }
771
772 liveFolderInfo.title = c.getString(titleIndex);
773 liveFolderInfo.id = id;
774 liveFolderInfo.uri = uri;
775 container = c.getInt(containerIndex);
776 liveFolderInfo.container = container;
777 liveFolderInfo.screen = c.getInt(screenIndex);
778 liveFolderInfo.cellX = c.getInt(cellXIndex);
779 liveFolderInfo.cellY = c.getInt(cellYIndex);
780 liveFolderInfo.baseIntent = intent;
781 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
782
783 // check & update map of what's occupied
784 if (!checkItemPlacement(occupied, liveFolderInfo)) {
785 break;
786 }
787
788 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
789 iconResourceIndex, liveFolderInfo);
790
791 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400793 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794 break;
795 }
Joe Onorato36115782010-06-17 13:28:48 -0400796 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 }
Joe Onorato36115782010-06-17 13:28:48 -0400798 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800799
Joe Onorato36115782010-06-17 13:28:48 -0400800 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
801 // Read all Launcher-specific widget details
802 int appWidgetId = c.getInt(appWidgetIdIndex);
803 id = c.getLong(idIndex);
804
805 final AppWidgetProviderInfo provider =
806 widgets.getAppWidgetInfo(appWidgetId);
807
808 if (!isSafeMode && (provider == null || provider.provider == null ||
809 provider.provider.getPackageName() == null)) {
810 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
811 + id + " appWidgetId=" + appWidgetId);
812 itemsToRemove.add(id);
813 } else {
814 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
815 appWidgetInfo.id = id;
816 appWidgetInfo.screen = c.getInt(screenIndex);
817 appWidgetInfo.cellX = c.getInt(cellXIndex);
818 appWidgetInfo.cellY = c.getInt(cellYIndex);
819 appWidgetInfo.spanX = c.getInt(spanXIndex);
820 appWidgetInfo.spanY = c.getInt(spanYIndex);
821
822 container = c.getInt(containerIndex);
823 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
824 Log.e(TAG, "Widget found where container "
825 + "!= CONTAINER_DESKTOP -- ignoring!");
826 continue;
827 }
828 appWidgetInfo.container = c.getInt(containerIndex);
829
830 // check & update map of what's occupied
831 if (!checkItemPlacement(occupied, appWidgetInfo)) {
832 break;
833 }
834
835 mAppWidgets.add(appWidgetInfo);
836 }
837 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800838 }
Joe Onorato36115782010-06-17 13:28:48 -0400839 } catch (Exception e) {
840 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800841 }
842 }
Joe Onorato36115782010-06-17 13:28:48 -0400843 } finally {
844 c.close();
845 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800846
Joe Onorato36115782010-06-17 13:28:48 -0400847 if (itemsToRemove.size() > 0) {
848 ContentProviderClient client = contentResolver.acquireContentProviderClient(
849 LauncherSettings.Favorites.CONTENT_URI);
850 // Remove dead items
851 for (long id : itemsToRemove) {
852 if (DEBUG_LOADERS) {
853 Log.d(TAG, "Removed id = " + id);
854 }
855 // Don't notify content observers
856 try {
857 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
858 null, null);
859 } catch (RemoteException e) {
860 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400861 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800862 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863 }
864
Joe Onorato36115782010-06-17 13:28:48 -0400865 if (DEBUG_LOADERS) {
866 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
867 Log.d(TAG, "workspace layout: ");
868 for (int y = 0; y < Launcher.NUMBER_CELLS_Y; y++) {
869 String line = "";
870 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
871 if (s > 0) {
872 line += " | ";
873 }
874 for (int x = 0; x < Launcher.NUMBER_CELLS_X; x++) {
875 line += ((occupied[s][x][y] != null) ? "#" : ".");
876 }
877 }
878 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 }
Joe Onorato36115782010-06-17 13:28:48 -0400880 }
881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Joe Onorato36115782010-06-17 13:28:48 -0400883 /**
884 * Read everything out of our database.
885 */
886 private void bindWorkspace() {
887 final long t = SystemClock.uptimeMillis();
888
889 // Don't use these two variables in any of the callback runnables.
890 // Otherwise we hold a reference to them.
891 final Callbacks oldCallbacks = mCallbacks.get();
892 if (oldCallbacks == null) {
893 // This launcher has exited and nobody bothered to tell us. Just bail.
894 Log.w(TAG, "LoaderTask running with no launcher");
895 return;
896 }
897
898 int N;
899 // Tell the workspace that we're about to start firing items at it
900 mHandler.post(new Runnable() {
901 public void run() {
902 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
903 if (callbacks != null) {
904 callbacks.startBinding();
905 }
906 }
907 });
908 // Add the items to the workspace.
909 N = mItems.size();
910 for (int i=0; i<N; i+=ITEMS_CHUNK) {
911 final int start = i;
912 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 mHandler.post(new Runnable() {
914 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800915 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400917 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 }
919 }
920 });
Joe Onorato36115782010-06-17 13:28:48 -0400921 }
922 mHandler.post(new Runnable() {
923 public void run() {
924 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
925 if (callbacks != null) {
926 callbacks.bindFolders(mFolders);
927 }
928 }
929 });
930 // Wait until the queue goes empty.
931 mHandler.post(new Runnable() {
932 public void run() {
933 if (DEBUG_LOADERS) {
934 Log.d(TAG, "Going to start binding widgets soon.");
935 }
936 }
937 });
938 // Bind the widgets, one at a time.
939 // WARNING: this is calling into the workspace from the background thread,
940 // but since getCurrentScreen() just returns the int, we should be okay. This
941 // is just a hint for the order, and if it's wrong, we'll be okay.
942 // TODO: instead, we should have that push the current screen into here.
943 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
944 N = mAppWidgets.size();
945 // once for the current screen
946 for (int i=0; i<N; i++) {
947 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
948 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 mHandler.post(new Runnable() {
950 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800951 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400953 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 }
955 }
956 });
957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 }
Joe Onorato36115782010-06-17 13:28:48 -0400959 // once for the other screens
960 for (int i=0; i<N; i++) {
961 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
962 if (widget.screen != currentScreen) {
963 mHandler.post(new Runnable() {
964 public void run() {
965 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
966 if (callbacks != null) {
967 callbacks.bindAppWidget(widget);
968 }
969 }
970 });
Joe Onoratocc67f472010-06-08 10:54:30 -0700971 }
972 }
Joe Onorato36115782010-06-17 13:28:48 -0400973 // Tell the workspace that we're done.
974 mHandler.post(new Runnable() {
975 public void run() {
976 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
977 if (callbacks != null) {
978 callbacks.finishBindingItems();
979 }
980 }
981 });
982 // If we're profiling, this is the last thing in the queue.
983 mHandler.post(new Runnable() {
984 public void run() {
985 if (DEBUG_LOADERS) {
986 Log.d(TAG, "bound workspace in "
987 + (SystemClock.uptimeMillis()-t) + "ms");
988 }
989 }
990 });
991 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700992
Joe Onorato36115782010-06-17 13:28:48 -0400993 private void loadAndBindAllApps() {
994 if (DEBUG_LOADERS) {
995 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
996 }
997 if (!mAllAppsLoaded) {
998 loadAllAppsByBatch();
999 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001000 return;
1001 }
Joe Onorato36115782010-06-17 13:28:48 -04001002 mAllAppsLoaded = true;
1003 } else {
1004 onlyBindAllApps();
1005 }
1006 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001007
Joe Onorato36115782010-06-17 13:28:48 -04001008 private void onlyBindAllApps() {
1009 final Callbacks oldCallbacks = mCallbacks.get();
1010 if (oldCallbacks == null) {
1011 // This launcher has exited and nobody bothered to tell us. Just bail.
1012 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1013 return;
1014 }
1015
1016 // shallow copy
1017 final ArrayList<ApplicationInfo> list
1018 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1019 mHandler.post(new Runnable() {
1020 public void run() {
1021 final long t = SystemClock.uptimeMillis();
1022 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1023 if (callbacks != null) {
1024 callbacks.bindAllApplications(list);
1025 }
1026 if (DEBUG_LOADERS) {
1027 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1028 + (SystemClock.uptimeMillis()-t) + "ms");
1029 }
1030 }
1031 });
1032
1033 }
1034
1035 private void loadAllAppsByBatch() {
1036 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1037
1038 // Don't use these two variables in any of the callback runnables.
1039 // Otherwise we hold a reference to them.
1040 final Callbacks oldCallbacks = mCallbacks.get();
1041 if (oldCallbacks == null) {
1042 // This launcher has exited and nobody bothered to tell us. Just bail.
1043 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1044 return;
1045 }
1046
1047 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1048 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1049
1050 final PackageManager packageManager = mContext.getPackageManager();
1051 List<ResolveInfo> apps = null;
1052
1053 int N = Integer.MAX_VALUE;
1054
1055 int startIndex;
1056 int i=0;
1057 int batchSize = -1;
1058 while (i < N && !mStopped) {
1059 if (i == 0) {
1060 mAllAppsList.clear();
1061 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1062 apps = packageManager.queryIntentActivities(mainIntent, 0);
1063 if (DEBUG_LOADERS) {
1064 Log.d(TAG, "queryIntentActivities took "
1065 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1066 }
1067 if (apps == null) {
1068 return;
1069 }
1070 N = apps.size();
1071 if (DEBUG_LOADERS) {
1072 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1073 }
1074 if (N == 0) {
1075 // There are no apps?!?
1076 return;
1077 }
1078 if (mBatchSize == 0) {
1079 batchSize = N;
1080 } else {
1081 batchSize = mBatchSize;
1082 }
1083
1084 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1085 Collections.sort(apps,
1086 new ResolveInfo.DisplayNameComparator(packageManager));
1087 if (DEBUG_LOADERS) {
1088 Log.d(TAG, "sort took "
1089 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1090 }
1091 }
1092
1093 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1094
1095 startIndex = i;
1096 for (int j=0; i<N && j<batchSize; j++) {
1097 // This builds the icon bitmaps.
1098 mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
1099 i++;
1100 }
1101
1102 final boolean first = i <= batchSize;
1103 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1104 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1105 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1106
Joe Onoratocc67f472010-06-08 10:54:30 -07001107 mHandler.post(new Runnable() {
1108 public void run() {
1109 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001110 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001111 if (first) {
1112 callbacks.bindAllApplications(added);
1113 } else {
1114 callbacks.bindAppsAdded(added);
1115 }
1116 if (DEBUG_LOADERS) {
1117 Log.d(TAG, "bound " + added.size() + " apps in "
1118 + (SystemClock.uptimeMillis() - t) + "ms");
1119 }
1120 } else {
1121 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001122 }
1123 }
1124 });
1125
Daniel Sandlerdca66122010-04-13 16:23:58 -04001126 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001127 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1128 + (SystemClock.uptimeMillis()-t2) + "ms");
1129 }
1130
1131 if (mAllAppsLoadDelay > 0 && i < N) {
1132 try {
1133 if (DEBUG_LOADERS) {
1134 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1135 }
1136 Thread.sleep(mAllAppsLoadDelay);
1137 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 }
1139 }
1140
Joe Onorato36115782010-06-17 13:28:48 -04001141 if (DEBUG_LOADERS) {
1142 Log.d(TAG, "cached all " + N + " apps in "
1143 + (SystemClock.uptimeMillis()-t) + "ms"
1144 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001145 }
1146 }
1147
1148 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001149 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1150 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1151 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1152 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1153 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1154 }
1155 }
1156
1157 void enqueuePackageUpdated(PackageUpdatedTask task) {
1158 mWorker.post(task);
1159 }
1160
1161 private class PackageUpdatedTask implements Runnable {
1162 int mOp;
1163 String[] mPackages;
1164
1165 public static final int OP_NONE = 0;
1166 public static final int OP_ADD = 1;
1167 public static final int OP_UPDATE = 2;
1168 public static final int OP_REMOVE = 3; // uninstlled
1169 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1170
1171
1172 public PackageUpdatedTask(int op, String[] packages) {
1173 mOp = op;
1174 mPackages = packages;
1175 }
1176
1177 public void run() {
1178 final Context context = mApp;
1179
1180 final String[] packages = mPackages;
1181 final int N = packages.length;
1182 switch (mOp) {
1183 case OP_ADD:
1184 for (int i=0; i<N; i++) {
1185 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1186 mAllAppsList.addPackage(context, packages[i]);
1187 }
1188 break;
1189 case OP_UPDATE:
1190 for (int i=0; i<N; i++) {
1191 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1192 mAllAppsList.updatePackage(context, packages[i]);
1193 }
1194 break;
1195 case OP_REMOVE:
1196 case OP_UNAVAILABLE:
1197 for (int i=0; i<N; i++) {
1198 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1199 mAllAppsList.removePackage(packages[i]);
1200 }
1201 break;
1202 }
1203
1204 ArrayList<ApplicationInfo> added = null;
1205 ArrayList<ApplicationInfo> removed = null;
1206 ArrayList<ApplicationInfo> modified = null;
1207
1208 if (mAllAppsList.added.size() > 0) {
1209 added = mAllAppsList.added;
1210 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1211 }
1212 if (mAllAppsList.removed.size() > 0) {
1213 removed = mAllAppsList.removed;
1214 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1215 for (ApplicationInfo info: removed) {
1216 mIconCache.remove(info.intent.getComponent());
1217 }
1218 }
1219 if (mAllAppsList.modified.size() > 0) {
1220 modified = mAllAppsList.modified;
1221 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1222 }
1223
1224 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1225 if (callbacks == null) {
1226 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1227 return;
1228 }
1229
1230 if (added != null) {
1231 final ArrayList<ApplicationInfo> addedFinal = added;
1232 mHandler.post(new Runnable() {
1233 public void run() {
1234 if (callbacks == mCallbacks.get()) {
1235 callbacks.bindAppsAdded(addedFinal);
1236 }
1237 }
1238 });
1239 }
1240 if (modified != null) {
1241 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1242 mHandler.post(new Runnable() {
1243 public void run() {
1244 if (callbacks == mCallbacks.get()) {
1245 callbacks.bindAppsUpdated(modifiedFinal);
1246 }
1247 }
1248 });
1249 }
1250 if (removed != null) {
1251 final boolean permanent = mOp != OP_UNAVAILABLE;
1252 final ArrayList<ApplicationInfo> removedFinal = removed;
1253 mHandler.post(new Runnable() {
1254 public void run() {
1255 if (callbacks == mCallbacks.get()) {
1256 callbacks.bindAppsRemoved(removedFinal, permanent);
1257 }
1258 }
1259 });
Joe Onoratobe386092009-11-17 17:32:16 -08001260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 }
1262 }
1263
1264 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001265 * This is called from the code that adds shortcuts from the intent receiver. This
1266 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 */
Joe Onorato56d82912010-03-07 14:32:10 -05001268 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001269 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001270 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271
Joe Onorato56d82912010-03-07 14:32:10 -05001272 /**
1273 * Make an ShortcutInfo object for a shortcut that is an application.
1274 *
1275 * If c is not null, then it will be used to fill in missing data like the title and icon.
1276 */
1277 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1278 Cursor c, int iconIndex, int titleIndex) {
1279 Bitmap icon = null;
1280 final ShortcutInfo info = new ShortcutInfo();
1281
1282 ComponentName componentName = intent.getComponent();
1283 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001284 return null;
1285 }
1286
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001287 // TODO: See if the PackageManager knows about this case. If it doesn't
1288 // then return null & delete this.
1289
Joe Onorato56d82912010-03-07 14:32:10 -05001290 // the resource -- This may implicitly give us back the fallback icon,
1291 // but don't worry about that. All we're doing with usingFallbackIcon is
1292 // to avoid saving lots of copies of that in the database, and most apps
1293 // have icons anyway.
1294 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1295 if (resolveInfo != null) {
1296 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001297 }
Joe Onorato56d82912010-03-07 14:32:10 -05001298 // the db
1299 if (icon == null) {
1300 if (c != null) {
1301 icon = getIconFromCursor(c, iconIndex);
1302 }
1303 }
1304 // the fallback icon
1305 if (icon == null) {
1306 icon = getFallbackIcon();
1307 info.usingFallbackIcon = true;
1308 }
1309 info.setIcon(icon);
1310
1311 // from the resource
1312 if (resolveInfo != null) {
1313 info.title = resolveInfo.activityInfo.loadLabel(manager);
1314 }
1315 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001317 if (c != null) {
1318 info.title = c.getString(titleIndex);
1319 }
1320 }
1321 // fall back to the class name of the activity
1322 if (info.title == null) {
1323 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1326 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001330 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001332 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001333 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1334 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
Joe Onorato56d82912010-03-07 14:32:10 -05001336 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001337 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001340 // TODO: If there's an explicit component and we can't install that, delete it.
1341
Joe Onorato56d82912010-03-07 14:32:10 -05001342 info.title = c.getString(titleIndex);
1343
Joe Onorato9c1289c2009-08-17 11:03:03 -04001344 int iconType = c.getInt(iconTypeIndex);
1345 switch (iconType) {
1346 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1347 String packageName = c.getString(iconPackageIndex);
1348 String resourceName = c.getString(iconResourceIndex);
1349 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001350 info.customIcon = false;
1351 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001354 if (resources != null) {
1355 final int id = resources.getIdentifier(resourceName, null, null);
1356 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001359 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
Joe Onorato56d82912010-03-07 14:32:10 -05001361 // the db
1362 if (icon == null) {
1363 icon = getIconFromCursor(c, iconIndex);
1364 }
1365 // the fallback icon
1366 if (icon == null) {
1367 icon = getFallbackIcon();
1368 info.usingFallbackIcon = true;
1369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 break;
1371 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001372 icon = getIconFromCursor(c, iconIndex);
1373 if (icon == null) {
1374 icon = getFallbackIcon();
1375 info.customIcon = false;
1376 info.usingFallbackIcon = true;
1377 } else {
1378 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 break;
1381 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001382 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001383 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001384 info.customIcon = false;
1385 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001387 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 return info;
1389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390
Joe Onorato56d82912010-03-07 14:32:10 -05001391 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1392 if (false) {
1393 Log.d(TAG, "getIconFromCursor app="
1394 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1395 }
1396 byte[] data = c.getBlob(iconIndex);
1397 try {
1398 return BitmapFactory.decodeByteArray(data, 0, data.length);
1399 } catch (Exception e) {
1400 return null;
1401 }
1402 }
1403
Joe Onorato0589f0f2010-02-08 13:44:00 -08001404 ShortcutInfo addShortcut(Context context, Intent data,
1405 CellLayout.CellInfo cellInfo, boolean notify) {
1406
1407 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1408 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1409 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1410
1411 return info;
1412 }
1413
1414 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1415 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1416 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1417 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1418
1419 Bitmap icon = null;
1420 boolean filtered = false;
1421 boolean customIcon = false;
1422 ShortcutIconResource iconResource = null;
1423
1424 if (bitmap != null && bitmap instanceof Bitmap) {
1425 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1426 filtered = true;
1427 customIcon = true;
1428 } else {
1429 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1430 if (extra != null && extra instanceof ShortcutIconResource) {
1431 try {
1432 iconResource = (ShortcutIconResource) extra;
1433 final PackageManager packageManager = context.getPackageManager();
1434 Resources resources = packageManager.getResourcesForApplication(
1435 iconResource.packageName);
1436 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1437 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1438 } catch (Exception e) {
1439 Log.w(TAG, "Could not load shortcut icon: " + extra);
1440 }
1441 }
1442 }
1443
Joe Onorato0589f0f2010-02-08 13:44:00 -08001444 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001445
1446 if (icon == null) {
1447 icon = getFallbackIcon();
1448 info.usingFallbackIcon = true;
1449 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001450 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001451
Joe Onorato0589f0f2010-02-08 13:44:00 -08001452 info.title = name;
1453 info.intent = intent;
1454 info.customIcon = customIcon;
1455 info.iconResource = iconResource;
1456
1457 return info;
1458 }
1459
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1461 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1462
1463 int iconType = c.getInt(iconTypeIndex);
1464 switch (iconType) {
1465 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1466 String packageName = c.getString(iconPackageIndex);
1467 String resourceName = c.getString(iconResourceIndex);
1468 PackageManager packageManager = context.getPackageManager();
1469 try {
1470 Resources resources = packageManager.getResourcesForApplication(packageName);
1471 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001472 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1473 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001475 liveFolderInfo.icon = Utilities.createIconBitmap(
1476 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1477 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 }
1479 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1480 liveFolderInfo.iconResource.packageName = packageName;
1481 liveFolderInfo.iconResource.resourceName = resourceName;
1482 break;
1483 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001484 liveFolderInfo.icon = Utilities.createIconBitmap(
1485 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1486 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487 }
1488 }
1489
Joe Onorato56d82912010-03-07 14:32:10 -05001490 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1491 // If this icon doesn't have a custom icon, check to see
1492 // what's stored in the DB, and if it doesn't match what
1493 // we're going to show, store what we are going to show back
1494 // into the DB. We do this so when we're loading, if the
1495 // package manager can't find an icon (for example because
1496 // the app is on SD) then we can use that instead.
Joe Onoratof5ed4f12010-08-30 18:30:15 -07001497 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001498 boolean needSave;
1499 byte[] data = c.getBlob(iconIndex);
1500 try {
1501 if (data != null) {
1502 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1503 Bitmap loaded = info.getIcon(mIconCache);
1504 needSave = !saved.sameAs(loaded);
1505 } else {
1506 needSave = true;
1507 }
1508 } catch (Exception e) {
1509 needSave = true;
1510 }
1511 if (needSave) {
1512 Log.d(TAG, "going to save icon bitmap for info=" + info);
1513 // This is slower than is ideal, but this only happens either
1514 // after the froyo OTA or when the app is updated with a new
1515 // icon.
1516 updateItemInDatabase(context, info);
1517 }
1518 }
1519 }
1520
Joe Onorato9c1289c2009-08-17 11:03:03 -04001521 /**
1522 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1523 * or make a new one.
1524 */
1525 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1526 // See if a placeholder was created for us already
1527 FolderInfo folderInfo = folders.get(id);
1528 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1529 // No placeholder -- create a new instance
1530 folderInfo = new UserFolderInfo();
1531 folders.put(id, folderInfo);
1532 }
1533 return (UserFolderInfo) folderInfo;
1534 }
1535
1536 /**
1537 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1538 * new one.
1539 */
1540 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1541 // See if a placeholder was created for us already
1542 FolderInfo folderInfo = folders.get(id);
1543 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1544 // No placeholder -- create a new instance
1545 folderInfo = new LiveFolderInfo();
1546 folders.put(id, folderInfo);
1547 }
1548 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1552 String label = activityInfo.loadLabel(manager).toString();
1553 if (label == null) {
1554 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1555 if (label == null) {
1556 label = activityInfo.name;
1557 }
1558 }
1559 return label;
1560 }
1561
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001563 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001564 = new Comparator<ApplicationInfo>() {
1565 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1566 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 };
Joe Onoratobe386092009-11-17 17:32:16 -08001569
1570 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001571 Log.d(TAG, "mCallbacks=" + mCallbacks);
1572 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1573 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1574 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1575 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001576 Log.d(TAG, "mItems size=" + mItems.size());
1577 if (mLoaderTask != null) {
1578 mLoaderTask.dumpState();
1579 } else {
1580 Log.d(TAG, "mLoaderTask=null");
1581 }
Joe Onoratobe386092009-11-17 17:32:16 -08001582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583}