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