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