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