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