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