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