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