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