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