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