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