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