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