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