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