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