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