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