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