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