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