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