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