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