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