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