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