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