The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 19 | import android.appwidget.AppWidgetManager; |
| 20 | import android.appwidget.AppWidgetProviderInfo; |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 23 | import android.content.ContentProviderClient; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
| 25 | import android.content.ContentValues; |
| 26 | import android.content.Intent; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 27 | import android.content.Intent.ShortcutIconResource; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.pm.ActivityInfo; |
| 30 | import android.content.pm.PackageManager; |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 31 | import android.content.pm.ProviderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.content.pm.ResolveInfo; |
| 33 | import android.content.res.Resources; |
| 34 | import android.database.Cursor; |
| 35 | import android.graphics.Bitmap; |
| 36 | import android.graphics.BitmapFactory; |
| 37 | import android.net.Uri; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 38 | import android.os.Parcelable; |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 39 | import android.os.RemoteException; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 40 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.os.Process; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 42 | import android.os.SystemClock; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 44 | import java.lang.ref.WeakReference; |
| 45 | import java.net.URISyntaxException; |
| 46 | import java.text.Collator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 48 | import java.util.Arrays; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 49 | import java.util.Comparator; |
| 50 | import java.util.Collections; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | import java.util.HashMap; |
| 52 | import java.util.List; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 54 | import com.android.launcher.R; |
| 55 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Maintains in-memory state of the Launcher. It is expected that there should be only one |
| 58 | * LauncherModel object held in a static. Also provide APIs for updating the database state |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 59 | * for the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | */ |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 61 | public class LauncherModel extends BroadcastReceiver { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 62 | static final boolean DEBUG_LOADERS = false; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 63 | static final String TAG = "Launcher.Model"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 64 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 65 | final int ALL_APPS_LOAD_DELAY = 150; // ms |
| 66 | |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 67 | private final LauncherApplication mApp; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 68 | private final Object mLock = new Object(); |
| 69 | private DeferredHandler mHandler = new DeferredHandler(); |
| 70 | private Loader mLoader = new Loader(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 72 | private boolean mBeforeFirstLoad = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 73 | private WeakReference<Callbacks> mCallbacks; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 75 | private AllAppsList mAllAppsList; |
| 76 | private IconCache mIconCache; |
| 77 | |
| 78 | private Bitmap mDefaultIcon; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 80 | public interface Callbacks { |
| 81 | public int getCurrentWorkspaceScreen(); |
| 82 | public void startBinding(); |
| 83 | public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end); |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 84 | public void bindFolders(HashMap<Long,FolderInfo> folders); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 85 | public void finishBindingItems(); |
| 86 | public void bindAppWidget(LauncherAppWidgetInfo info); |
| 87 | public void bindAllApplications(ArrayList<ApplicationInfo> apps); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 88 | public void bindAppsAdded(ArrayList<ApplicationInfo> apps); |
| 89 | public void bindAppsUpdated(ArrayList<ApplicationInfo> apps); |
| 90 | public void bindAppsRemoved(ArrayList<ApplicationInfo> apps); |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 91 | public int getAppBatchSize(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 92 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 94 | LauncherModel(LauncherApplication app, IconCache iconCache) { |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 95 | mApp = app; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 96 | mAllAppsList = new AllAppsList(iconCache); |
| 97 | mIconCache = iconCache; |
| 98 | |
| 99 | mDefaultIcon = Utilities.createIconBitmap( |
| 100 | app.getPackageManager().getDefaultActivityIcon(), app); |
| 101 | } |
| 102 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 103 | public Bitmap getFallbackIcon() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 104 | return Bitmap.createBitmap(mDefaultIcon); |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 105 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 107 | /** |
| 108 | * Adds an item to the DB if it was not created previously, or move it to a new |
| 109 | * <container, screen, cellX, cellY> |
| 110 | */ |
| 111 | static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container, |
| 112 | int screen, int cellX, int cellY) { |
| 113 | if (item.container == ItemInfo.NO_ID) { |
| 114 | // From all apps |
| 115 | addItemToDatabase(context, item, container, screen, cellX, cellY, false); |
| 116 | } else { |
| 117 | // From somewhere else |
| 118 | moveItemInDatabase(context, item, container, screen, cellX, cellY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 123 | * Move an item in the DB to a new <container, screen, cellX, cellY> |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 124 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 125 | static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen, |
| 126 | int cellX, int cellY) { |
| 127 | item.container = container; |
| 128 | item.screen = screen; |
| 129 | item.cellX = cellX; |
| 130 | item.cellY = cellY; |
| 131 | |
| 132 | final ContentValues values = new ContentValues(); |
| 133 | final ContentResolver cr = context.getContentResolver(); |
| 134 | |
| 135 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
| 136 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 137 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
| 138 | values.put(LauncherSettings.Favorites.SCREEN, item.screen); |
| 139 | |
| 140 | cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null); |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 144 | * Returns true if the shortcuts already exists in the database. |
| 145 | * we identify a shortcut by its title and intent. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 146 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 147 | static boolean shortcutExists(Context context, String title, Intent intent) { |
| 148 | final ContentResolver cr = context.getContentResolver(); |
| 149 | Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, |
| 150 | new String[] { "title", "intent" }, "title=? and intent=?", |
| 151 | new String[] { title, intent.toUri(0) }, null); |
| 152 | boolean result = false; |
| 153 | try { |
| 154 | result = c.moveToFirst(); |
| 155 | } finally { |
| 156 | c.close(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 157 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 158 | return result; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 161 | /** |
| 162 | * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList. |
| 163 | */ |
| 164 | FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) { |
| 165 | final ContentResolver cr = context.getContentResolver(); |
| 166 | Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null, |
| 167 | "_id=? and (itemType=? or itemType=?)", |
| 168 | new String[] { String.valueOf(id), |
| 169 | String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER), |
| 170 | String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null); |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 171 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 172 | try { |
| 173 | if (c.moveToFirst()) { |
| 174 | final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 175 | final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); |
| 176 | final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 177 | final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 178 | final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 179 | final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 180 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 181 | FolderInfo folderInfo = null; |
| 182 | switch (c.getInt(itemTypeIndex)) { |
| 183 | case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: |
| 184 | folderInfo = findOrMakeUserFolder(folderList, id); |
| 185 | break; |
| 186 | case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: |
| 187 | folderInfo = findOrMakeLiveFolder(folderList, id); |
| 188 | break; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 191 | folderInfo.title = c.getString(titleIndex); |
| 192 | folderInfo.id = id; |
| 193 | folderInfo.container = c.getInt(containerIndex); |
| 194 | folderInfo.screen = c.getInt(screenIndex); |
| 195 | folderInfo.cellX = c.getInt(cellXIndex); |
| 196 | folderInfo.cellY = c.getInt(cellYIndex); |
| 197 | |
| 198 | return folderInfo; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 199 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 200 | } finally { |
| 201 | c.close(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return null; |
| 205 | } |
| 206 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 207 | /** |
| 208 | * Add an item to the database in a specified container. Sets the container, screen, cellX and |
| 209 | * cellY fields of the item. Also assigns an ID to the item. |
| 210 | */ |
| 211 | static void addItemToDatabase(Context context, ItemInfo item, long container, |
| 212 | int screen, int cellX, int cellY, boolean notify) { |
| 213 | item.container = container; |
| 214 | item.screen = screen; |
| 215 | item.cellX = cellX; |
| 216 | item.cellY = cellY; |
| 217 | |
| 218 | final ContentValues values = new ContentValues(); |
| 219 | final ContentResolver cr = context.getContentResolver(); |
| 220 | |
| 221 | item.onAddToDatabase(values); |
| 222 | |
| 223 | Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI : |
| 224 | LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values); |
| 225 | |
| 226 | if (result != null) { |
| 227 | item.id = Integer.parseInt(result.getPathSegments().get(1)); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 228 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 231 | /** |
| 232 | * Update an item to the database in a specified container. |
| 233 | */ |
| 234 | static void updateItemInDatabase(Context context, ItemInfo item) { |
| 235 | final ContentValues values = new ContentValues(); |
| 236 | final ContentResolver cr = context.getContentResolver(); |
| 237 | |
| 238 | item.onAddToDatabase(values); |
| 239 | |
| 240 | cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Removes the specified item from the database |
| 245 | * @param context |
| 246 | * @param item |
| 247 | */ |
| 248 | static void deleteItemFromDatabase(Context context, ItemInfo item) { |
| 249 | final ContentResolver cr = context.getContentResolver(); |
| 250 | |
| 251 | cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Remove the contents of the specified folder from the database |
| 256 | */ |
| 257 | static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) { |
| 258 | final ContentResolver cr = context.getContentResolver(); |
| 259 | |
| 260 | cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null); |
| 261 | cr.delete(LauncherSettings.Favorites.CONTENT_URI, |
| 262 | LauncherSettings.Favorites.CONTAINER + "=" + info.id, null); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Set this as the current Launcher activity object for the loader. |
| 267 | */ |
| 268 | public void initialize(Callbacks callbacks) { |
| 269 | synchronized (mLock) { |
| 270 | mCallbacks = new WeakReference<Callbacks>(callbacks); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | public void startLoader(Context context, boolean isLaunching) { |
| 275 | mLoader.startLoader(context, isLaunching); |
| 276 | } |
| 277 | |
| 278 | public void stopLoader() { |
| 279 | mLoader.stopLoader(); |
| 280 | } |
| 281 | |
Joe Onorato | 1d8e7bb | 2009-10-15 19:49:43 -0700 | [diff] [blame] | 282 | /** |
| 283 | * We pick up most of the changes to all apps. |
| 284 | */ |
| 285 | public void setAllAppsDirty() { |
| 286 | mLoader.setAllAppsDirty(); |
| 287 | } |
| 288 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 289 | public void setWorkspaceDirty() { |
| 290 | mLoader.setWorkspaceDirty(); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and |
| 295 | * ACTION_PACKAGE_CHANGED. |
| 296 | */ |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 297 | public void onReceive(Context context, Intent intent) { |
| 298 | // Use the app as the context. |
| 299 | context = mApp; |
| 300 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 301 | ArrayList<ApplicationInfo> added = null; |
| 302 | ArrayList<ApplicationInfo> removed = null; |
| 303 | ArrayList<ApplicationInfo> modified = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 304 | |
| 305 | synchronized (mLock) { |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 306 | if (mBeforeFirstLoad) { |
| 307 | // If we haven't even loaded yet, don't bother, since we'll just pick |
| 308 | // up the changes. |
| 309 | return; |
| 310 | } |
| 311 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 312 | final String action = intent.getAction(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 313 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 314 | if (Intent.ACTION_PACKAGE_CHANGED.equals(action) |
| 315 | || Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 316 | || Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 317 | final String packageName = intent.getData().getSchemeSpecificPart(); |
| 318 | final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 319 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 320 | if (packageName == null || packageName.length() == 0) { |
| 321 | // they sent us a bad intent |
| 322 | return; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 323 | } |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 324 | |
| 325 | if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 326 | mAllAppsList.updatePackage(context, packageName); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 327 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 328 | if (!replacing) { |
| 329 | mAllAppsList.removePackage(packageName); |
| 330 | } |
| 331 | // else, we are replacing the package, so a PACKAGE_ADDED will be sent |
| 332 | // later, we will update the package at this time |
| 333 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 334 | if (!replacing) { |
| 335 | mAllAppsList.addPackage(context, packageName); |
| 336 | } else { |
| 337 | mAllAppsList.updatePackage(context, packageName); |
| 338 | } |
| 339 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 340 | |
| 341 | if (mAllAppsList.added.size() > 0) { |
| 342 | added = mAllAppsList.added; |
| 343 | mAllAppsList.added = new ArrayList<ApplicationInfo>(); |
| 344 | } |
| 345 | if (mAllAppsList.removed.size() > 0) { |
| 346 | removed = mAllAppsList.removed; |
| 347 | mAllAppsList.removed = new ArrayList<ApplicationInfo>(); |
| 348 | for (ApplicationInfo info: removed) { |
| 349 | mIconCache.remove(info.intent.getComponent()); |
| 350 | } |
| 351 | } |
| 352 | if (mAllAppsList.modified.size() > 0) { |
| 353 | modified = mAllAppsList.modified; |
| 354 | mAllAppsList.modified = new ArrayList<ApplicationInfo>(); |
| 355 | } |
| 356 | |
| 357 | final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null; |
| 358 | if (callbacks == null) { |
| 359 | Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading."); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | if (added != null) { |
| 364 | final ArrayList<ApplicationInfo> addedFinal = added; |
| 365 | mHandler.post(new Runnable() { |
| 366 | public void run() { |
| 367 | callbacks.bindAppsAdded(addedFinal); |
| 368 | } |
| 369 | }); |
| 370 | } |
| 371 | if (modified != null) { |
| 372 | final ArrayList<ApplicationInfo> modifiedFinal = modified; |
| 373 | mHandler.post(new Runnable() { |
| 374 | public void run() { |
| 375 | callbacks.bindAppsUpdated(modifiedFinal); |
| 376 | } |
| 377 | }); |
| 378 | } |
| 379 | if (removed != null) { |
| 380 | final ArrayList<ApplicationInfo> removedFinal = removed; |
| 381 | mHandler.post(new Runnable() { |
| 382 | public void run() { |
| 383 | callbacks.bindAppsRemoved(removedFinal); |
| 384 | } |
| 385 | }); |
| 386 | } |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 387 | } else { |
| 388 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| 389 | String packages[] = intent.getStringArrayExtra( |
| 390 | Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 391 | if (packages == null || packages.length == 0) { |
| 392 | return; |
| 393 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 394 | setAllAppsDirty(); |
| 395 | setWorkspaceDirty(); |
| 396 | startLoader(context, false); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 397 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| 398 | String packages[] = intent.getStringArrayExtra( |
| 399 | Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 400 | if (packages == null || packages.length == 0) { |
| 401 | return; |
| 402 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 403 | setAllAppsDirty(); |
| 404 | setWorkspaceDirty(); |
| 405 | startLoader(context, false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 406 | } |
| 407 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | public class Loader { |
| 412 | private static final int ITEMS_CHUNK = 6; |
| 413 | |
| 414 | private LoaderThread mLoaderThread; |
| 415 | |
| 416 | private int mLastWorkspaceSeq = 0; |
| 417 | private int mWorkspaceSeq = 1; |
| 418 | |
| 419 | private int mLastAllAppsSeq = 0; |
| 420 | private int mAllAppsSeq = 1; |
| 421 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 422 | final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>(); |
| 423 | final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>(); |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 424 | final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * Call this from the ui thread so the handler is initialized on the correct thread. |
| 428 | */ |
| 429 | public Loader() { |
| 430 | } |
| 431 | |
| 432 | public void startLoader(Context context, boolean isLaunching) { |
| 433 | synchronized (mLock) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 434 | if (DEBUG_LOADERS) { |
| 435 | Log.d(TAG, "startLoader isLaunching=" + isLaunching); |
| 436 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 437 | // Don't bother to start the thread if we know it's not going to do anything |
Joe Onorato | ac03330 | 2010-04-13 17:19:18 -0700 | [diff] [blame] | 438 | if (mCallbacks != null && mCallbacks.get() != null) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 439 | LoaderThread oldThread = mLoaderThread; |
| 440 | if (oldThread != null) { |
| 441 | if (oldThread.isLaunching()) { |
| 442 | // don't downgrade isLaunching if we're already running |
| 443 | isLaunching = true; |
| 444 | } |
| 445 | oldThread.stopLocked(); |
| 446 | } |
| 447 | mLoaderThread = new LoaderThread(context, oldThread, isLaunching); |
| 448 | mLoaderThread.start(); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | public void stopLoader() { |
| 454 | synchronized (mLock) { |
| 455 | if (mLoaderThread != null) { |
| 456 | mLoaderThread.stopLocked(); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | public void setWorkspaceDirty() { |
| 462 | synchronized (mLock) { |
| 463 | mWorkspaceSeq++; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | public void setAllAppsDirty() { |
| 468 | synchronized (mLock) { |
| 469 | mAllAppsSeq++; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Runnable for the thread that loads the contents of the launcher: |
| 475 | * - workspace icons |
| 476 | * - widgets |
| 477 | * - all apps icons |
| 478 | */ |
| 479 | private class LoaderThread extends Thread { |
| 480 | private Context mContext; |
| 481 | private Thread mWaitThread; |
| 482 | private boolean mIsLaunching; |
| 483 | private boolean mStopped; |
| 484 | private boolean mWorkspaceDoneBinding; |
| 485 | |
| 486 | LoaderThread(Context context, Thread waitThread, boolean isLaunching) { |
| 487 | mContext = context; |
| 488 | mWaitThread = waitThread; |
| 489 | mIsLaunching = isLaunching; |
| 490 | } |
| 491 | |
| 492 | boolean isLaunching() { |
| 493 | return mIsLaunching; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * If another LoaderThread was supplied, we need to wait for that to finish before |
| 498 | * we start our processing. This keeps the ordering of the setting and clearing |
| 499 | * of the dirty flags correct by making sure we don't start processing stuff until |
| 500 | * they've had a chance to re-set them. We do this waiting the worker thread, not |
| 501 | * the ui thread to avoid ANRs. |
| 502 | */ |
| 503 | private void waitForOtherThread() { |
| 504 | if (mWaitThread != null) { |
| 505 | boolean done = false; |
| 506 | while (!done) { |
| 507 | try { |
| 508 | mWaitThread.join(); |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 509 | done = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 510 | } catch (InterruptedException ex) { |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 511 | // Ignore |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | mWaitThread = null; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | public void run() { |
| 519 | waitForOtherThread(); |
| 520 | |
| 521 | // Elevate priority when Home launches for the first time to avoid |
| 522 | // starving at boot time. Staring at a blank home is not cool. |
| 523 | synchronized (mLock) { |
| 524 | android.os.Process.setThreadPriority(mIsLaunching |
| 525 | ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND); |
| 526 | } |
| 527 | |
| 528 | // Load the workspace only if it's dirty. |
| 529 | int workspaceSeq; |
| 530 | boolean workspaceDirty; |
| 531 | synchronized (mLock) { |
| 532 | workspaceSeq = mWorkspaceSeq; |
| 533 | workspaceDirty = mWorkspaceSeq != mLastWorkspaceSeq; |
| 534 | } |
| 535 | if (workspaceDirty) { |
| 536 | loadWorkspace(); |
| 537 | } |
| 538 | synchronized (mLock) { |
| 539 | // If we're not stopped, and nobody has incremented mWorkspaceSeq. |
| 540 | if (mStopped) { |
| 541 | return; |
| 542 | } |
| 543 | if (workspaceSeq == mWorkspaceSeq) { |
| 544 | mLastWorkspaceSeq = mWorkspaceSeq; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Bind the workspace |
| 549 | bindWorkspace(); |
| 550 | |
| 551 | // Wait until the either we're stopped or the other threads are done. |
| 552 | // This way we don't start loading all apps until the workspace has settled |
| 553 | // down. |
| 554 | synchronized (LoaderThread.this) { |
Joe Onorato | 080d9b6 | 2009-11-02 12:01:11 -0500 | [diff] [blame] | 555 | mHandler.postIdle(new Runnable() { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 556 | public void run() { |
| 557 | synchronized (LoaderThread.this) { |
| 558 | mWorkspaceDoneBinding = true; |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 559 | if (DEBUG_LOADERS) { |
| 560 | Log.d(TAG, "done with workspace"); |
| 561 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 562 | LoaderThread.this.notify(); |
| 563 | } |
| 564 | } |
| 565 | }); |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 566 | if (DEBUG_LOADERS) { |
| 567 | Log.d(TAG, "waiting to be done with workspace"); |
| 568 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 569 | while (!mStopped && !mWorkspaceDoneBinding) { |
| 570 | try { |
| 571 | this.wait(); |
| 572 | } catch (InterruptedException ex) { |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 573 | // Ignore |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 574 | } |
| 575 | } |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 576 | if (DEBUG_LOADERS) { |
| 577 | Log.d(TAG, "done waiting to be done with workspace"); |
| 578 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 581 | // Whew! Hard work done. |
| 582 | synchronized (mLock) { |
| 583 | if (mIsLaunching) { |
| 584 | android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 585 | } |
| 586 | } |
| 587 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 588 | // Load all apps if they're dirty |
| 589 | int allAppsSeq; |
| 590 | boolean allAppsDirty; |
| 591 | synchronized (mLock) { |
| 592 | allAppsSeq = mAllAppsSeq; |
| 593 | allAppsDirty = mAllAppsSeq != mLastAllAppsSeq; |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 594 | if (DEBUG_LOADERS) { |
| 595 | Log.d(TAG, "mAllAppsSeq=" + mAllAppsSeq |
| 596 | + " mLastAllAppsSeq=" + mLastAllAppsSeq + " allAppsDirty"); |
| 597 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 598 | } |
| 599 | if (allAppsDirty) { |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 600 | loadAndBindAllApps(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 601 | } |
| 602 | synchronized (mLock) { |
| 603 | // If we're not stopped, and nobody has incremented mAllAppsSeq. |
| 604 | if (mStopped) { |
| 605 | return; |
| 606 | } |
| 607 | if (allAppsSeq == mAllAppsSeq) { |
| 608 | mLastAllAppsSeq = mAllAppsSeq; |
| 609 | } |
| 610 | } |
| 611 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 612 | // Clear out this reference, otherwise we end up holding it until all of the |
| 613 | // callback runnables are done. |
| 614 | mContext = null; |
| 615 | |
| 616 | synchronized (mLock) { |
| 617 | // Setting the reference is atomic, but we can't do it inside the other critical |
| 618 | // sections. |
| 619 | mLoaderThread = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | public void stopLocked() { |
| 624 | synchronized (LoaderThread.this) { |
| 625 | mStopped = true; |
| 626 | this.notify(); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Gets the callbacks object. If we've been stopped, or if the launcher object |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 632 | * has somehow been garbage collected, return null instead. Pass in the Callbacks |
| 633 | * object that was around when the deferred message was scheduled, and if there's |
| 634 | * a new Callbacks object around then also return null. This will save us from |
| 635 | * calling onto it with data that will be ignored. |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 636 | */ |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 637 | Callbacks tryGetCallbacks(Callbacks oldCallbacks) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 638 | synchronized (mLock) { |
| 639 | if (mStopped) { |
| 640 | return null; |
| 641 | } |
| 642 | |
Joe Onorato | ac03330 | 2010-04-13 17:19:18 -0700 | [diff] [blame] | 643 | if (mCallbacks == null) { |
| 644 | return null; |
| 645 | } |
| 646 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 647 | final Callbacks callbacks = mCallbacks.get(); |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 648 | if (callbacks != oldCallbacks) { |
| 649 | return null; |
| 650 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 651 | if (callbacks == null) { |
| 652 | Log.w(TAG, "no mCallbacks"); |
| 653 | return null; |
| 654 | } |
| 655 | |
| 656 | return callbacks; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | private void loadWorkspace() { |
| 661 | long t = SystemClock.uptimeMillis(); |
| 662 | |
| 663 | final Context context = mContext; |
| 664 | final ContentResolver contentResolver = context.getContentResolver(); |
| 665 | final PackageManager manager = context.getPackageManager(); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 666 | final AppWidgetManager widgets = AppWidgetManager.getInstance(context); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 667 | final boolean isSafeMode = manager.isSafeMode(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 668 | |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 669 | mItems.clear(); |
Joe Onorato | 511ab64 | 2009-11-08 14:14:07 -0500 | [diff] [blame] | 670 | mAppWidgets.clear(); |
Joe Onorato | 1db7a97 | 2009-11-16 18:32:22 -0800 | [diff] [blame] | 671 | mFolders.clear(); |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 672 | |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 673 | final ArrayList<Long> itemsToRemove = new ArrayList<Long>(); |
| 674 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 675 | final Cursor c = contentResolver.query( |
| 676 | LauncherSettings.Favorites.CONTENT_URI, null, null, null, null); |
| 677 | |
| 678 | try { |
| 679 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); |
| 680 | final int intentIndex = c.getColumnIndexOrThrow |
| 681 | (LauncherSettings.Favorites.INTENT); |
| 682 | final int titleIndex = c.getColumnIndexOrThrow |
| 683 | (LauncherSettings.Favorites.TITLE); |
| 684 | final int iconTypeIndex = c.getColumnIndexOrThrow( |
| 685 | LauncherSettings.Favorites.ICON_TYPE); |
| 686 | final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); |
| 687 | final int iconPackageIndex = c.getColumnIndexOrThrow( |
| 688 | LauncherSettings.Favorites.ICON_PACKAGE); |
| 689 | final int iconResourceIndex = c.getColumnIndexOrThrow( |
| 690 | LauncherSettings.Favorites.ICON_RESOURCE); |
| 691 | final int containerIndex = c.getColumnIndexOrThrow( |
| 692 | LauncherSettings.Favorites.CONTAINER); |
| 693 | final int itemTypeIndex = c.getColumnIndexOrThrow( |
| 694 | LauncherSettings.Favorites.ITEM_TYPE); |
| 695 | final int appWidgetIdIndex = c.getColumnIndexOrThrow( |
| 696 | LauncherSettings.Favorites.APPWIDGET_ID); |
| 697 | final int screenIndex = c.getColumnIndexOrThrow( |
| 698 | LauncherSettings.Favorites.SCREEN); |
| 699 | final int cellXIndex = c.getColumnIndexOrThrow |
| 700 | (LauncherSettings.Favorites.CELLX); |
| 701 | final int cellYIndex = c.getColumnIndexOrThrow |
| 702 | (LauncherSettings.Favorites.CELLY); |
| 703 | final int spanXIndex = c.getColumnIndexOrThrow |
| 704 | (LauncherSettings.Favorites.SPANX); |
| 705 | final int spanYIndex = c.getColumnIndexOrThrow( |
| 706 | LauncherSettings.Favorites.SPANY); |
| 707 | final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); |
| 708 | final int displayModeIndex = c.getColumnIndexOrThrow( |
| 709 | LauncherSettings.Favorites.DISPLAY_MODE); |
| 710 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 711 | ShortcutInfo info; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 712 | String intentDescription; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 713 | LauncherAppWidgetInfo appWidgetInfo; |
| 714 | int container; |
| 715 | long id; |
| 716 | Intent intent; |
| 717 | |
| 718 | while (!mStopped && c.moveToNext()) { |
| 719 | try { |
| 720 | int itemType = c.getInt(itemTypeIndex); |
| 721 | |
| 722 | switch (itemType) { |
| 723 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 724 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 725 | intentDescription = c.getString(intentIndex); |
| 726 | try { |
| 727 | intent = Intent.parseUri(intentDescription, 0); |
| 728 | } catch (URISyntaxException e) { |
| 729 | continue; |
| 730 | } |
| 731 | |
| 732 | if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 733 | info = getShortcutInfo(manager, intent, context, c, iconIndex, |
| 734 | titleIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 735 | } else { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 736 | info = getShortcutInfo(c, context, iconTypeIndex, |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 737 | iconPackageIndex, iconResourceIndex, iconIndex, |
| 738 | titleIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | if (info != null) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 742 | updateSavedIcon(context, info, c, iconIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 743 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 744 | info.intent = intent; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 745 | info.id = c.getLong(idIndex); |
| 746 | container = c.getInt(containerIndex); |
| 747 | info.container = container; |
| 748 | info.screen = c.getInt(screenIndex); |
| 749 | info.cellX = c.getInt(cellXIndex); |
| 750 | info.cellY = c.getInt(cellYIndex); |
| 751 | |
| 752 | switch (container) { |
| 753 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: |
| 754 | mItems.add(info); |
| 755 | break; |
| 756 | default: |
| 757 | // Item is in a user folder |
| 758 | UserFolderInfo folderInfo = |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 759 | findOrMakeUserFolder(mFolders, container); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 760 | folderInfo.add(info); |
| 761 | break; |
| 762 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 763 | } else { |
| 764 | // Failed to load the shortcut, probably because the |
| 765 | // activity manager couldn't resolve it (maybe the app |
| 766 | // was uninstalled), or the db row was somehow screwed up. |
| 767 | // Delete it. |
| 768 | id = c.getLong(idIndex); |
| 769 | Log.e(TAG, "Error loading shortcut " + id + ", removing it"); |
| 770 | contentResolver.delete(LauncherSettings.Favorites.getContentUri( |
| 771 | id, false), null, null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 772 | } |
| 773 | break; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 774 | |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 775 | case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 776 | id = c.getLong(idIndex); |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 777 | UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 778 | |
| 779 | folderInfo.title = c.getString(titleIndex); |
| 780 | |
| 781 | folderInfo.id = id; |
| 782 | container = c.getInt(containerIndex); |
| 783 | folderInfo.container = container; |
| 784 | folderInfo.screen = c.getInt(screenIndex); |
| 785 | folderInfo.cellX = c.getInt(cellXIndex); |
| 786 | folderInfo.cellY = c.getInt(cellYIndex); |
| 787 | |
| 788 | switch (container) { |
| 789 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: |
| 790 | mItems.add(folderInfo); |
| 791 | break; |
| 792 | } |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 793 | |
| 794 | mFolders.put(folderInfo.id, folderInfo); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 795 | break; |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 796 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 797 | case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 798 | id = c.getLong(idIndex); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 799 | Uri uri = Uri.parse(c.getString(uriIndex)); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 800 | |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 801 | // Make sure the live folder exists |
| 802 | final ProviderInfo providerInfo = |
| 803 | context.getPackageManager().resolveContentProvider( |
| 804 | uri.getAuthority(), 0); |
| 805 | |
| 806 | if (providerInfo == null && !isSafeMode) { |
| 807 | itemsToRemove.add(id); |
| 808 | } else { |
| 809 | LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id); |
| 810 | |
| 811 | intentDescription = c.getString(intentIndex); |
| 812 | intent = null; |
| 813 | if (intentDescription != null) { |
| 814 | try { |
| 815 | intent = Intent.parseUri(intentDescription, 0); |
| 816 | } catch (URISyntaxException e) { |
| 817 | // Ignore, a live folder might not have a base intent |
| 818 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 819 | } |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 820 | |
| 821 | liveFolderInfo.title = c.getString(titleIndex); |
| 822 | liveFolderInfo.id = id; |
| 823 | liveFolderInfo.uri = uri; |
| 824 | container = c.getInt(containerIndex); |
| 825 | liveFolderInfo.container = container; |
| 826 | liveFolderInfo.screen = c.getInt(screenIndex); |
| 827 | liveFolderInfo.cellX = c.getInt(cellXIndex); |
| 828 | liveFolderInfo.cellY = c.getInt(cellYIndex); |
| 829 | liveFolderInfo.baseIntent = intent; |
| 830 | liveFolderInfo.displayMode = c.getInt(displayModeIndex); |
| 831 | |
| 832 | loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex, |
| 833 | iconResourceIndex, liveFolderInfo); |
| 834 | |
| 835 | switch (container) { |
| 836 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: |
| 837 | mItems.add(liveFolderInfo); |
| 838 | break; |
| 839 | } |
| 840 | mFolders.put(liveFolderInfo.id, liveFolderInfo); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 841 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 842 | break; |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 843 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 844 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 845 | // Read all Launcher-specific widget details |
| 846 | int appWidgetId = c.getInt(appWidgetIdIndex); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 847 | id = c.getLong(idIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 848 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 849 | final AppWidgetProviderInfo provider = |
| 850 | widgets.getAppWidgetInfo(appWidgetId); |
| 851 | |
| 852 | if (!isSafeMode && (provider == null || provider.provider == null || |
| 853 | provider.provider.getPackageName() == null)) { |
Joe Onorato | 8ddc4fd | 2010-03-17 09:14:50 -0700 | [diff] [blame] | 854 | Log.e(TAG, "Deleting widget that isn't installed anymore: id=" |
| 855 | + id + " appWidgetId=" + appWidgetId); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 856 | itemsToRemove.add(id); |
| 857 | } else { |
| 858 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId); |
| 859 | appWidgetInfo.id = id; |
| 860 | appWidgetInfo.screen = c.getInt(screenIndex); |
| 861 | appWidgetInfo.cellX = c.getInt(cellXIndex); |
| 862 | appWidgetInfo.cellY = c.getInt(cellYIndex); |
| 863 | appWidgetInfo.spanX = c.getInt(spanXIndex); |
| 864 | appWidgetInfo.spanY = c.getInt(spanYIndex); |
| 865 | |
| 866 | container = c.getInt(containerIndex); |
| 867 | if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 868 | Log.e(TAG, "Widget found where container " |
| 869 | + "!= CONTAINER_DESKTOP -- ignoring!"); |
| 870 | continue; |
| 871 | } |
| 872 | appWidgetInfo.container = c.getInt(containerIndex); |
| 873 | |
| 874 | mAppWidgets.add(appWidgetInfo); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 875 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | } catch (Exception e) { |
| 879 | Log.w(TAG, "Desktop items loading interrupted:", e); |
| 880 | } |
| 881 | } |
| 882 | } finally { |
| 883 | c.close(); |
| 884 | } |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 885 | |
| 886 | if (itemsToRemove.size() > 0) { |
| 887 | ContentProviderClient client = contentResolver.acquireContentProviderClient( |
| 888 | LauncherSettings.Favorites.CONTENT_URI); |
| 889 | // Remove dead items |
| 890 | for (long id : itemsToRemove) { |
| 891 | if (DEBUG_LOADERS) { |
| 892 | Log.d(TAG, "Removed id = " + id); |
| 893 | } |
| 894 | // Don't notify content observers |
| 895 | try { |
| 896 | client.delete(LauncherSettings.Favorites.getContentUri(id, false), |
| 897 | null, null); |
| 898 | } catch (RemoteException e) { |
| 899 | Log.w(TAG, "Could not remove id = " + id); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 904 | if (DEBUG_LOADERS) { |
| 905 | Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms"); |
| 906 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /** |
| 910 | * Read everything out of our database. |
| 911 | */ |
| 912 | private void bindWorkspace() { |
| 913 | final long t = SystemClock.uptimeMillis(); |
| 914 | |
| 915 | // Don't use these two variables in any of the callback runnables. |
| 916 | // Otherwise we hold a reference to them. |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 917 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 918 | if (oldCallbacks == null) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 919 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 920 | Log.w(TAG, "LoaderThread running with no launcher"); |
| 921 | return; |
| 922 | } |
| 923 | |
| 924 | int N; |
| 925 | // Tell the workspace that we're about to start firing items at it |
| 926 | mHandler.post(new Runnable() { |
| 927 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 928 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 929 | if (callbacks != null) { |
| 930 | callbacks.startBinding(); |
| 931 | } |
| 932 | } |
| 933 | }); |
| 934 | // Add the items to the workspace. |
| 935 | N = mItems.size(); |
| 936 | for (int i=0; i<N; i+=ITEMS_CHUNK) { |
| 937 | final int start = i; |
| 938 | final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); |
| 939 | mHandler.post(new Runnable() { |
| 940 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 941 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 942 | if (callbacks != null) { |
| 943 | callbacks.bindItems(mItems, start, start+chunkSize); |
| 944 | } |
| 945 | } |
| 946 | }); |
| 947 | } |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 948 | mHandler.post(new Runnable() { |
| 949 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 950 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 951 | if (callbacks != null) { |
| 952 | callbacks.bindFolders(mFolders); |
| 953 | } |
| 954 | } |
| 955 | }); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 956 | // Wait until the queue goes empty. |
| 957 | mHandler.postIdle(new Runnable() { |
| 958 | public void run() { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 959 | if (DEBUG_LOADERS) { |
| 960 | Log.d(TAG, "Going to start binding widgets soon."); |
| 961 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 962 | } |
| 963 | }); |
| 964 | // Bind the widgets, one at a time. |
| 965 | // WARNING: this is calling into the workspace from the background thread, |
| 966 | // but since getCurrentScreen() just returns the int, we should be okay. This |
| 967 | // is just a hint for the order, and if it's wrong, we'll be okay. |
| 968 | // TODO: instead, we should have that push the current screen into here. |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 969 | final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 970 | N = mAppWidgets.size(); |
| 971 | // once for the current screen |
| 972 | for (int i=0; i<N; i++) { |
| 973 | final LauncherAppWidgetInfo widget = mAppWidgets.get(i); |
| 974 | if (widget.screen == currentScreen) { |
| 975 | mHandler.post(new Runnable() { |
| 976 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 977 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 978 | if (callbacks != null) { |
| 979 | callbacks.bindAppWidget(widget); |
| 980 | } |
| 981 | } |
| 982 | }); |
| 983 | } |
| 984 | } |
| 985 | // once for the other screens |
| 986 | for (int i=0; i<N; i++) { |
| 987 | final LauncherAppWidgetInfo widget = mAppWidgets.get(i); |
| 988 | if (widget.screen != currentScreen) { |
| 989 | mHandler.post(new Runnable() { |
| 990 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 991 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 992 | if (callbacks != null) { |
| 993 | callbacks.bindAppWidget(widget); |
| 994 | } |
| 995 | } |
| 996 | }); |
| 997 | } |
| 998 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 999 | // Tell the workspace that we're done. |
| 1000 | mHandler.post(new Runnable() { |
| 1001 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 1002 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1003 | if (callbacks != null) { |
| 1004 | callbacks.finishBindingItems(); |
| 1005 | } |
| 1006 | } |
| 1007 | }); |
| 1008 | // If we're profiling, this is the last thing in the queue. |
| 1009 | mHandler.post(new Runnable() { |
| 1010 | public void run() { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1011 | if (DEBUG_LOADERS) { |
| 1012 | Log.d(TAG, "bound workspace in " |
| 1013 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 1014 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1015 | if (Launcher.PROFILE_ROTATE) { |
| 1016 | android.os.Debug.stopMethodTracing(); |
| 1017 | } |
| 1018 | } |
| 1019 | }); |
| 1020 | } |
| 1021 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1022 | private void loadAndBindAllApps() { |
| 1023 | final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
| 1024 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1025 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
| 1026 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 1027 | |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 1028 | final Callbacks callbacks = mCallbacks.get(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1029 | if (callbacks == null) { |
| 1030 | return; |
| 1031 | } |
| 1032 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1033 | final PackageManager packageManager = mContext.getPackageManager(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1034 | final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0); |
| 1035 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1036 | int N; |
| 1037 | int batchSize = callbacks.getAppBatchSize(); |
| 1038 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1039 | synchronized (mLock) { |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 1040 | mBeforeFirstLoad = false; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1041 | mAllAppsList.clear(); |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1042 | if (apps == null) return; |
| 1043 | N = apps.size(); |
| 1044 | if (batchSize <= 0) |
| 1045 | batchSize = N; |
| 1046 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1047 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1048 | int i=0; |
| 1049 | while (i < N && !mStopped) { |
| 1050 | synchronized (mLock) { |
| 1051 | final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
| 1052 | |
| 1053 | for (int j=0; i<N && j<batchSize; j++) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1054 | // This builds the icon bitmaps. |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1055 | mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache)); |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1056 | i++; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1057 | } |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1058 | // re-sort before binding this batch to the grid |
Joe Onorato | b0c27f2 | 2009-12-01 16:19:38 -0800 | [diff] [blame] | 1059 | Collections.sort(mAllAppsList.data, APP_NAME_COMPARATOR); |
| 1060 | Collections.sort(mAllAppsList.added, APP_NAME_COMPARATOR); |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1061 | if (DEBUG_LOADERS) { |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1062 | Log.d(TAG, "batch of " + batchSize + " icons processed in " |
| 1063 | + (SystemClock.uptimeMillis()-t2) + "ms"); |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1064 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1065 | } |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1066 | |
| 1067 | mHandler.post(bindAllAppsTask); |
| 1068 | |
| 1069 | if (ALL_APPS_LOAD_DELAY > 0) { |
| 1070 | try { |
| 1071 | Thread.sleep(ALL_APPS_LOAD_DELAY); |
| 1072 | } catch (InterruptedException exc) { } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | if (DEBUG_LOADERS) { |
| 1077 | Log.d(TAG, "cached all " + N + " apps in " |
| 1078 | + (SystemClock.uptimeMillis()-t) + "ms"); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1082 | final Runnable bindAllAppsTask = new Runnable() { |
| 1083 | public void run() { |
| 1084 | final long t = SystemClock.uptimeMillis(); |
| 1085 | int count = 0; |
| 1086 | Callbacks callbacks = null; |
| 1087 | ArrayList<ApplicationInfo> results = null; |
| 1088 | synchronized (mLock) { |
| 1089 | mHandler.cancelRunnable(this); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1090 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1091 | results = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone(); |
| 1092 | // We're adding this now, so clear out this so we don't re-send them. |
| 1093 | mAllAppsList.added = new ArrayList<ApplicationInfo>(); |
| 1094 | count = results.size(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1095 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1096 | callbacks = tryGetCallbacks(mCallbacks.get()); |
| 1097 | } |
| 1098 | |
| 1099 | if (callbacks != null && count > 0) { |
| 1100 | callbacks.bindAllApplications(results); |
| 1101 | } |
| 1102 | |
| 1103 | if (DEBUG_LOADERS) { |
| 1104 | Log.d(TAG, "bound " + count + " apps in " |
| 1105 | + (SystemClock.uptimeMillis() - t) + "ms"); |
| 1106 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1107 | } |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 1108 | }; |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1109 | |
| 1110 | public void dumpState() { |
| 1111 | Log.d(TAG, "mLoader.mLoaderThread.mContext=" + mContext); |
| 1112 | Log.d(TAG, "mLoader.mLoaderThread.mWaitThread=" + mWaitThread); |
| 1113 | Log.d(TAG, "mLoader.mLoaderThread.mIsLaunching=" + mIsLaunching); |
| 1114 | Log.d(TAG, "mLoader.mLoaderThread.mStopped=" + mStopped); |
| 1115 | Log.d(TAG, "mLoader.mLoaderThread.mWorkspaceDoneBinding=" + mWorkspaceDoneBinding); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | public void dumpState() { |
| 1120 | Log.d(TAG, "mLoader.mLastWorkspaceSeq=" + mLoader.mLastWorkspaceSeq); |
| 1121 | Log.d(TAG, "mLoader.mWorkspaceSeq=" + mLoader.mWorkspaceSeq); |
| 1122 | Log.d(TAG, "mLoader.mLastAllAppsSeq=" + mLoader.mLastAllAppsSeq); |
| 1123 | Log.d(TAG, "mLoader.mAllAppsSeq=" + mLoader.mAllAppsSeq); |
| 1124 | Log.d(TAG, "mLoader.mItems size=" + mLoader.mItems.size()); |
| 1125 | if (mLoaderThread != null) { |
| 1126 | mLoaderThread.dumpState(); |
| 1127 | } else { |
| 1128 | Log.d(TAG, "mLoader.mLoaderThread=null"); |
| 1129 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | /** |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1134 | * This is called from the code that adds shortcuts from the intent receiver. This |
| 1135 | * doesn't have a Cursor, but |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1136 | */ |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1137 | public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) { |
Joe Onorato | e74daed | 2010-03-11 12:32:24 -0800 | [diff] [blame] | 1138 | return getShortcutInfo(manager, intent, context, null, -1, -1); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1139 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1140 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1141 | /** |
| 1142 | * Make an ShortcutInfo object for a shortcut that is an application. |
| 1143 | * |
| 1144 | * If c is not null, then it will be used to fill in missing data like the title and icon. |
| 1145 | */ |
| 1146 | public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context, |
| 1147 | Cursor c, int iconIndex, int titleIndex) { |
| 1148 | Bitmap icon = null; |
| 1149 | final ShortcutInfo info = new ShortcutInfo(); |
| 1150 | |
| 1151 | ComponentName componentName = intent.getComponent(); |
| 1152 | if (componentName == null) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1153 | return null; |
| 1154 | } |
| 1155 | |
Joe Onorato | 8ddc4fd | 2010-03-17 09:14:50 -0700 | [diff] [blame] | 1156 | // TODO: See if the PackageManager knows about this case. If it doesn't |
| 1157 | // then return null & delete this. |
| 1158 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1159 | // the resource -- This may implicitly give us back the fallback icon, |
| 1160 | // but don't worry about that. All we're doing with usingFallbackIcon is |
| 1161 | // to avoid saving lots of copies of that in the database, and most apps |
| 1162 | // have icons anyway. |
| 1163 | final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0); |
| 1164 | if (resolveInfo != null) { |
| 1165 | icon = mIconCache.getIcon(componentName, resolveInfo); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1166 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1167 | // the db |
| 1168 | if (icon == null) { |
| 1169 | if (c != null) { |
| 1170 | icon = getIconFromCursor(c, iconIndex); |
| 1171 | } |
| 1172 | } |
| 1173 | // the fallback icon |
| 1174 | if (icon == null) { |
| 1175 | icon = getFallbackIcon(); |
| 1176 | info.usingFallbackIcon = true; |
| 1177 | } |
| 1178 | info.setIcon(icon); |
| 1179 | |
| 1180 | // from the resource |
| 1181 | if (resolveInfo != null) { |
| 1182 | info.title = resolveInfo.activityInfo.loadLabel(manager); |
| 1183 | } |
| 1184 | // from the db |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1185 | if (info.title == null) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1186 | if (c != null) { |
| 1187 | info.title = c.getString(titleIndex); |
| 1188 | } |
| 1189 | } |
| 1190 | // fall back to the class name of the activity |
| 1191 | if (info.title == null) { |
| 1192 | info.title = componentName.getClassName(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1193 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1194 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; |
| 1195 | return info; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1196 | } |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 1197 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1198 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1199 | * Make an ShortcutInfo object for a shortcut that isn't an application. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1200 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1201 | private ShortcutInfo getShortcutInfo(Cursor c, Context context, |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1202 | int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex, |
| 1203 | int titleIndex) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1204 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1205 | Bitmap icon = null; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1206 | final ShortcutInfo info = new ShortcutInfo(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1207 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1208 | |
Joe Onorato | 8ddc4fd | 2010-03-17 09:14:50 -0700 | [diff] [blame] | 1209 | // TODO: If there's an explicit component and we can't install that, delete it. |
| 1210 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1211 | info.title = c.getString(titleIndex); |
| 1212 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1213 | int iconType = c.getInt(iconTypeIndex); |
| 1214 | switch (iconType) { |
| 1215 | case LauncherSettings.Favorites.ICON_TYPE_RESOURCE: |
| 1216 | String packageName = c.getString(iconPackageIndex); |
| 1217 | String resourceName = c.getString(iconResourceIndex); |
| 1218 | PackageManager packageManager = context.getPackageManager(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1219 | info.customIcon = false; |
| 1220 | // the resource |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1221 | try { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1222 | Resources resources = packageManager.getResourcesForApplication(packageName); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1223 | if (resources != null) { |
| 1224 | final int id = resources.getIdentifier(resourceName, null, null); |
| 1225 | icon = Utilities.createIconBitmap(resources.getDrawable(id), context); |
| 1226 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1227 | } catch (Exception e) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1228 | // drop this. we have other places to look for icons |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1229 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1230 | // the db |
| 1231 | if (icon == null) { |
| 1232 | icon = getIconFromCursor(c, iconIndex); |
| 1233 | } |
| 1234 | // the fallback icon |
| 1235 | if (icon == null) { |
| 1236 | icon = getFallbackIcon(); |
| 1237 | info.usingFallbackIcon = true; |
| 1238 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1239 | break; |
| 1240 | case LauncherSettings.Favorites.ICON_TYPE_BITMAP: |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1241 | icon = getIconFromCursor(c, iconIndex); |
| 1242 | if (icon == null) { |
| 1243 | icon = getFallbackIcon(); |
| 1244 | info.customIcon = false; |
| 1245 | info.usingFallbackIcon = true; |
| 1246 | } else { |
| 1247 | info.customIcon = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1248 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1249 | break; |
| 1250 | default: |
Joe Onorato | d8d22da | 2010-03-11 17:59:11 -0800 | [diff] [blame] | 1251 | icon = getFallbackIcon(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1252 | info.usingFallbackIcon = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1253 | info.customIcon = false; |
| 1254 | break; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1255 | } |
Joe Onorato | d8d22da | 2010-03-11 17:59:11 -0800 | [diff] [blame] | 1256 | info.setIcon(icon); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1257 | return info; |
| 1258 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1259 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1260 | Bitmap getIconFromCursor(Cursor c, int iconIndex) { |
| 1261 | if (false) { |
| 1262 | Log.d(TAG, "getIconFromCursor app=" |
| 1263 | + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE))); |
| 1264 | } |
| 1265 | byte[] data = c.getBlob(iconIndex); |
| 1266 | try { |
| 1267 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 1268 | } catch (Exception e) { |
| 1269 | return null; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1273 | ShortcutInfo addShortcut(Context context, Intent data, |
| 1274 | CellLayout.CellInfo cellInfo, boolean notify) { |
| 1275 | |
| 1276 | final ShortcutInfo info = infoFromShortcutIntent(context, data); |
| 1277 | addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 1278 | cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); |
| 1279 | |
| 1280 | return info; |
| 1281 | } |
| 1282 | |
| 1283 | private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) { |
| 1284 | Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 1285 | String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
| 1286 | Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); |
| 1287 | |
| 1288 | Bitmap icon = null; |
| 1289 | boolean filtered = false; |
| 1290 | boolean customIcon = false; |
| 1291 | ShortcutIconResource iconResource = null; |
| 1292 | |
| 1293 | if (bitmap != null && bitmap instanceof Bitmap) { |
| 1294 | icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context); |
| 1295 | filtered = true; |
| 1296 | customIcon = true; |
| 1297 | } else { |
| 1298 | Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); |
| 1299 | if (extra != null && extra instanceof ShortcutIconResource) { |
| 1300 | try { |
| 1301 | iconResource = (ShortcutIconResource) extra; |
| 1302 | final PackageManager packageManager = context.getPackageManager(); |
| 1303 | Resources resources = packageManager.getResourcesForApplication( |
| 1304 | iconResource.packageName); |
| 1305 | final int id = resources.getIdentifier(iconResource.resourceName, null, null); |
| 1306 | icon = Utilities.createIconBitmap(resources.getDrawable(id), context); |
| 1307 | } catch (Exception e) { |
| 1308 | Log.w(TAG, "Could not load shortcut icon: " + extra); |
| 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1313 | final ShortcutInfo info = new ShortcutInfo(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1314 | |
| 1315 | if (icon == null) { |
| 1316 | icon = getFallbackIcon(); |
| 1317 | info.usingFallbackIcon = true; |
| 1318 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1319 | info.setIcon(icon); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1320 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1321 | info.title = name; |
| 1322 | info.intent = intent; |
| 1323 | info.customIcon = customIcon; |
| 1324 | info.iconResource = iconResource; |
| 1325 | |
| 1326 | return info; |
| 1327 | } |
| 1328 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1329 | private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex, |
| 1330 | int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) { |
| 1331 | |
| 1332 | int iconType = c.getInt(iconTypeIndex); |
| 1333 | switch (iconType) { |
| 1334 | case LauncherSettings.Favorites.ICON_TYPE_RESOURCE: |
| 1335 | String packageName = c.getString(iconPackageIndex); |
| 1336 | String resourceName = c.getString(iconResourceIndex); |
| 1337 | PackageManager packageManager = context.getPackageManager(); |
| 1338 | try { |
| 1339 | Resources resources = packageManager.getResourcesForApplication(packageName); |
| 1340 | final int id = resources.getIdentifier(resourceName, null, null); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1341 | liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id), |
| 1342 | context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1343 | } catch (Exception e) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1344 | liveFolderInfo.icon = Utilities.createIconBitmap( |
| 1345 | context.getResources().getDrawable(R.drawable.ic_launcher_folder), |
| 1346 | context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1347 | } |
| 1348 | liveFolderInfo.iconResource = new Intent.ShortcutIconResource(); |
| 1349 | liveFolderInfo.iconResource.packageName = packageName; |
| 1350 | liveFolderInfo.iconResource.resourceName = resourceName; |
| 1351 | break; |
| 1352 | default: |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1353 | liveFolderInfo.icon = Utilities.createIconBitmap( |
| 1354 | context.getResources().getDrawable(R.drawable.ic_launcher_folder), |
| 1355 | context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 1359 | void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) { |
| 1360 | // If this icon doesn't have a custom icon, check to see |
| 1361 | // what's stored in the DB, and if it doesn't match what |
| 1362 | // we're going to show, store what we are going to show back |
| 1363 | // into the DB. We do this so when we're loading, if the |
| 1364 | // package manager can't find an icon (for example because |
| 1365 | // the app is on SD) then we can use that instead. |
| 1366 | if (info.onExternalStorage && !info.customIcon && !info.usingFallbackIcon) { |
| 1367 | boolean needSave; |
| 1368 | byte[] data = c.getBlob(iconIndex); |
| 1369 | try { |
| 1370 | if (data != null) { |
| 1371 | Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length); |
| 1372 | Bitmap loaded = info.getIcon(mIconCache); |
| 1373 | needSave = !saved.sameAs(loaded); |
| 1374 | } else { |
| 1375 | needSave = true; |
| 1376 | } |
| 1377 | } catch (Exception e) { |
| 1378 | needSave = true; |
| 1379 | } |
| 1380 | if (needSave) { |
| 1381 | Log.d(TAG, "going to save icon bitmap for info=" + info); |
| 1382 | // This is slower than is ideal, but this only happens either |
| 1383 | // after the froyo OTA or when the app is updated with a new |
| 1384 | // icon. |
| 1385 | updateItemInDatabase(context, info); |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1390 | /** |
| 1391 | * Return an existing UserFolderInfo object if we have encountered this ID previously, |
| 1392 | * or make a new one. |
| 1393 | */ |
| 1394 | private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) { |
| 1395 | // See if a placeholder was created for us already |
| 1396 | FolderInfo folderInfo = folders.get(id); |
| 1397 | if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) { |
| 1398 | // No placeholder -- create a new instance |
| 1399 | folderInfo = new UserFolderInfo(); |
| 1400 | folders.put(id, folderInfo); |
| 1401 | } |
| 1402 | return (UserFolderInfo) folderInfo; |
| 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a |
| 1407 | * new one. |
| 1408 | */ |
| 1409 | private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) { |
| 1410 | // See if a placeholder was created for us already |
| 1411 | FolderInfo folderInfo = folders.get(id); |
| 1412 | if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) { |
| 1413 | // No placeholder -- create a new instance |
| 1414 | folderInfo = new LiveFolderInfo(); |
| 1415 | folders.put(id, folderInfo); |
| 1416 | } |
| 1417 | return (LiveFolderInfo) folderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1420 | private static String getLabel(PackageManager manager, ActivityInfo activityInfo) { |
| 1421 | String label = activityInfo.loadLabel(manager).toString(); |
| 1422 | if (label == null) { |
| 1423 | label = manager.getApplicationLabel(activityInfo.applicationInfo).toString(); |
| 1424 | if (label == null) { |
| 1425 | label = activityInfo.name; |
| 1426 | } |
| 1427 | } |
| 1428 | return label; |
| 1429 | } |
| 1430 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1431 | private static final Collator sCollator = Collator.getInstance(); |
Joe Onorato | b0c27f2 | 2009-12-01 16:19:38 -0800 | [diff] [blame] | 1432 | public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1433 | = new Comparator<ApplicationInfo>() { |
| 1434 | public final int compare(ApplicationInfo a, ApplicationInfo b) { |
| 1435 | return sCollator.compare(a.title.toString(), b.title.toString()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1436 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1437 | }; |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1438 | |
| 1439 | public void dumpState() { |
| 1440 | Log.d(TAG, "mBeforeFirstLoad=" + mBeforeFirstLoad); |
| 1441 | Log.d(TAG, "mCallbacks=" + mCallbacks); |
| 1442 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data); |
| 1443 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added); |
| 1444 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed); |
| 1445 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified); |
| 1446 | mLoader.dumpState(); |
| 1447 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1448 | } |