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