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