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