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