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