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