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