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 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 19 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.ComponentName; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 22 | import android.content.ContentProviderOperation; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.ContentValues; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 27 | import android.content.IntentFilter; |
Mario Bertschler | 817afa3 | 2017-03-15 11:56:47 -0700 | [diff] [blame^] | 28 | import android.content.pm.ApplicationInfo; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 29 | import android.content.pm.LauncherActivityInfo; |
Mario Bertschler | 817afa3 | 2017-03-15 11:56:47 -0700 | [diff] [blame^] | 30 | import android.content.pm.PackageInstaller; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.net.Uri; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.HandlerThread; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 34 | import android.os.Looper; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.os.Process; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 36 | import android.os.SystemClock; |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 37 | import android.os.Trace; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 38 | import android.os.UserHandle; |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 39 | import android.text.TextUtils; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 40 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 41 | import android.util.LongSparseArray; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 42 | import android.util.MutableInt; |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 43 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 44 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 45 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 46 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 47 | import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 48 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 49 | import com.android.launcher3.config.FeatureFlags; |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 50 | import com.android.launcher3.dynamicui.ExtractionUtils; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 51 | import com.android.launcher3.folder.Folder; |
| 52 | import com.android.launcher3.folder.FolderIcon; |
Jon Miranda | 655ec42 | 2017-02-07 11:40:22 -0800 | [diff] [blame] | 53 | import com.android.launcher3.folder.FolderIconPreviewVerifier; |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 54 | import com.android.launcher3.graphics.LauncherIcons; |
Sunny Goyal | 1acb9e9 | 2016-05-16 12:41:09 -0700 | [diff] [blame] | 55 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 56 | import com.android.launcher3.model.AddWorkspaceItemsTask; |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 57 | import com.android.launcher3.model.BgDataModel; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 58 | import com.android.launcher3.model.CacheDataUpdatedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 59 | import com.android.launcher3.model.ExtendedModelTask; |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 60 | import com.android.launcher3.model.GridSizeMigrationTask; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 61 | import com.android.launcher3.model.LoaderCursor; |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 62 | import com.android.launcher3.model.ModelWriter; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 63 | import com.android.launcher3.model.PackageInstallStateChangedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 64 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 65 | import com.android.launcher3.model.PackageUpdatedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 66 | import com.android.launcher3.model.SdCardAvailableReceiver; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 67 | import com.android.launcher3.model.ShortcutsChangedTask; |
| 68 | import com.android.launcher3.model.UserLockStateChangedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 69 | import com.android.launcher3.model.WidgetItem; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 70 | import com.android.launcher3.model.WidgetsModel; |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 71 | import com.android.launcher3.provider.ImportDataTask; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 72 | import com.android.launcher3.provider.LauncherDbUtils; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 73 | import com.android.launcher3.shortcuts.DeepShortcutManager; |
| 74 | import com.android.launcher3.shortcuts.ShortcutInfoCompat; |
| 75 | import com.android.launcher3.shortcuts.ShortcutKey; |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 76 | import com.android.launcher3.util.ComponentKey; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 77 | import com.android.launcher3.util.LooperIdleLock; |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 78 | import com.android.launcher3.util.ManagedProfileHeuristic; |
Tony Wickham | d82a39d | 2016-07-01 15:44:13 -0700 | [diff] [blame] | 79 | import com.android.launcher3.util.MultiHashMap; |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 80 | import com.android.launcher3.util.PackageManagerHelper; |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 81 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | 2bcbe13 | 2016-11-16 09:23:42 -0800 | [diff] [blame] | 82 | import com.android.launcher3.util.Provider; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 83 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 84 | import com.android.launcher3.util.ViewOnDrawExecutor; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 85 | |
Hyunyoung Song | 3c7d9cb | 2017-01-30 15:11:27 -0800 | [diff] [blame] | 86 | import java.io.FileDescriptor; |
| 87 | import java.io.PrintWriter; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 88 | import java.lang.ref.WeakReference; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 89 | import java.util.ArrayList; |
| 90 | import java.util.Collections; |
| 91 | import java.util.Comparator; |
| 92 | import java.util.HashMap; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 93 | import java.util.HashSet; |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 94 | import java.util.Iterator; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 95 | import java.util.List; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 96 | import java.util.Map; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 97 | import java.util.Set; |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 98 | import java.util.concurrent.CancellationException; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 99 | import java.util.concurrent.Executor; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 100 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Maintains in-memory state of the Launcher. It is expected that there should be only one |
| 103 | * 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] | 104 | * for the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 106 | public class LauncherModel extends BroadcastReceiver |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 107 | implements LauncherAppsCompat.OnAppsChangedCallbackCompat { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 108 | static final boolean DEBUG_LOADERS = false; |
Chris Wren | ee52336 | 2014-09-09 10:09:02 -0400 | [diff] [blame] | 109 | private static final boolean DEBUG_RECEIVER = false; |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 110 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 111 | static final String TAG = "Launcher.Model"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 112 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 113 | 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] | 114 | private static final long INVALID_SCREEN_ID = -1L; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 115 | |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 116 | private final MainThreadExecutor mUiExecutor = new MainThreadExecutor(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 117 | @Thunk final LauncherAppState mApp; |
| 118 | @Thunk final Object mLock = new Object(); |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 119 | @Thunk LoaderTask mLoaderTask; |
| 120 | @Thunk boolean mIsLoaderTaskRunning; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 121 | @Thunk boolean mHasLoaderCompletedOnce; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 123 | @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader"); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 124 | static { |
| 125 | sWorkerThread.start(); |
| 126 | } |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 127 | @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper()); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 128 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 129 | // Indicates whether the current model data is valid or not. |
| 130 | // We start off with everything not loaded. After that, we assume that |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 131 | // our monitoring of the package manager provides all updates and we never |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 132 | // need to do a requery. This is only ever touched from the loader thread. |
| 133 | private boolean mModelLoaded; |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 134 | public boolean isModelLoaded() { |
| 135 | synchronized (mLock) { |
| 136 | return mModelLoaded && mLoaderTask == null; |
| 137 | } |
| 138 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 139 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Set of runnables to be called on the background thread after the workspace binding |
| 142 | * is complete. |
| 143 | */ |
| 144 | static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>(); |
| 145 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 146 | @Thunk WeakReference<Callbacks> mCallbacks; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 147 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 148 | // < only access in worker thread > |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 149 | private final AllAppsList mBgAllAppsList; |
Hyunyoung Song | 9110d48 | 2015-05-22 14:49:23 -0700 | [diff] [blame] | 150 | // Entire list of widgets. |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 151 | private final WidgetsModel mBgWidgetsModel; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 152 | |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 153 | private boolean mHasShortcutHostPermission; |
| 154 | // Runnable to check if the shortcuts permission has changed. |
| 155 | private final Runnable mShortcutPermissionCheckRunnable = new Runnable() { |
| 156 | @Override |
| 157 | public void run() { |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 158 | if (mModelLoaded) { |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 159 | boolean hasShortcutHostPermission = |
| 160 | DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 161 | if (hasShortcutHostPermission != mHasShortcutHostPermission) { |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 162 | forceReload(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | }; |
| 167 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 168 | /** |
| 169 | * All the static data should be accessed on the background thread, A lock should be acquired |
| 170 | * on this object when accessing any data from this model. |
| 171 | */ |
| 172 | static final BgDataModel sBgDataModel = new BgDataModel(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 173 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 174 | // </ only access in worker thread > |
| 175 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 176 | private final IconCache mIconCache; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 177 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 178 | private final LauncherAppsCompat mLauncherApps; |
| 179 | private final UserManagerCompat mUserManager; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 180 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 181 | public interface Callbacks { |
Joe Onorato | ef2efcf | 2010-10-27 13:21:00 -0700 | [diff] [blame] | 182 | public boolean setLoadOnResume(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 183 | public int getCurrentWorkspaceScreen(); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 184 | public void clearPendingBinds(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 185 | public void startBinding(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 186 | public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end, |
| 187 | boolean forceAnimateIcons); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 188 | public void bindScreens(ArrayList<Long> orderedScreenIds); |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 189 | public void finishFirstPageBind(ViewOnDrawExecutor executor); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 190 | public void finishBindingItems(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 191 | public void bindAppWidget(LauncherAppWidgetInfo info); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 192 | public void bindAllApplications(ArrayList<AppInfo> apps); |
Winson Chung | d64d176 | 2013-08-20 14:37:16 -0700 | [diff] [blame] | 193 | public void bindAppsAdded(ArrayList<Long> newScreens, |
| 194 | ArrayList<ItemInfo> addNotAnimated, |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 195 | ArrayList<ItemInfo> addAnimated, |
| 196 | ArrayList<AppInfo> addedApps); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 197 | public void bindAppsUpdated(ArrayList<AppInfo> apps); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 198 | public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 199 | ArrayList<ShortcutInfo> removed, UserHandle user); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 200 | public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets); |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 201 | public void bindRestoreItemsChange(HashSet<ItemInfo> updates); |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 202 | public void bindWorkspaceComponentsRemoved( |
| 203 | HashSet<String> packageNames, HashSet<ComponentName> components, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 204 | UserHandle user); |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 205 | public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 206 | public void notifyWidgetProvidersChanged(); |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 207 | public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 208 | public void onPageBoundSynchronously(int page); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 209 | public void executeOnNextDraw(ViewOnDrawExecutor executor); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 210 | public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 211 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 212 | |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 213 | LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) { |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 214 | Context context = app.getContext(); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 215 | mApp = app; |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 216 | mBgAllAppsList = new AllAppsList(iconCache, appFilter); |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 217 | mBgWidgetsModel = new WidgetsModel(iconCache, appFilter); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 218 | mIconCache = iconCache; |
| 219 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 220 | mLauncherApps = LauncherAppsCompat.getInstance(context); |
| 221 | mUserManager = UserManagerCompat.getInstance(context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 224 | /** Runs the specified runnable immediately if called from the worker thread, otherwise it is |
| 225 | * posted on the worker thread handler. */ |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 226 | private static void runOnWorkerThread(Runnable r) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 227 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 228 | r.run(); |
| 229 | } else { |
| 230 | // If we are not on the worker thread, then post to the worker handler |
| 231 | sWorker.post(r); |
| 232 | } |
| 233 | } |
| 234 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 235 | public void setPackageState(PackageInstallInfo installInfo) { |
| 236 | enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo)); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Updates the icons and label of all pending icons for the provided package name. |
| 241 | */ |
| 242 | public void updateSessionDisplayInfo(final String packageName) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 243 | HashSet<String> packages = new HashSet<>(); |
| 244 | packages.add(packageName); |
| 245 | enqueueModelUpdateTask(new CacheDataUpdatedTask( |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 246 | CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages)); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Adds the provided items to the workspace. |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 251 | */ |
Sunny Goyal | 2bcbe13 | 2016-11-16 09:23:42 -0800 | [diff] [blame] | 252 | public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) { |
| 253 | addAndBindAddedWorkspaceItems(Provider.of(workspaceApps)); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Adds the provided items to the workspace. |
| 258 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 259 | public void addAndBindAddedWorkspaceItems( |
Sunny Goyal | 2bcbe13 | 2016-11-16 09:23:42 -0800 | [diff] [blame] | 260 | Provider<List<ItemInfo>> appsProvider) { |
| 261 | enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider)); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 264 | public ModelWriter getWriter(boolean hasVerticalHotseat) { |
| 265 | return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 268 | static void checkItemInfoLocked( |
| 269 | final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 270 | ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId); |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 271 | if (modelItem != null && item != modelItem) { |
| 272 | // check all the data is consistent |
| 273 | if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) { |
| 274 | ShortcutInfo modelShortcut = (ShortcutInfo) modelItem; |
| 275 | ShortcutInfo shortcut = (ShortcutInfo) item; |
| 276 | if (modelShortcut.title.toString().equals(shortcut.title.toString()) && |
| 277 | modelShortcut.intent.filterEquals(shortcut.intent) && |
| 278 | modelShortcut.id == shortcut.id && |
| 279 | modelShortcut.itemType == shortcut.itemType && |
| 280 | modelShortcut.container == shortcut.container && |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 281 | modelShortcut.screenId == shortcut.screenId && |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 282 | modelShortcut.cellX == shortcut.cellX && |
| 283 | modelShortcut.cellY == shortcut.cellY && |
| 284 | modelShortcut.spanX == shortcut.spanX && |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 285 | modelShortcut.spanY == shortcut.spanY) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 286 | // For all intents and purposes, this is the same object |
| 287 | return; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // the modelItem needs to match up perfectly with item if our model is |
| 292 | // to be consistent with the database-- for now, just require |
| 293 | // modelItem == item or the equality check above |
| 294 | String msg = "item: " + ((item != null) ? item.toString() : "null") + |
| 295 | "modelItem: " + |
| 296 | ((modelItem != null) ? modelItem.toString() : "null") + |
| 297 | "Error: ItemInfo passed to checkItemInfo doesn't match original"; |
| 298 | RuntimeException e = new RuntimeException(msg); |
| 299 | if (stackTrace != null) { |
| 300 | e.setStackTrace(stackTrace); |
| 301 | } |
Adam Cohen | b9ada65 | 2013-11-08 08:25:08 -0800 | [diff] [blame] | 302 | throw e; |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 306 | static void checkItemInfo(final ItemInfo item) { |
| 307 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 308 | final long itemId = item.id; |
| 309 | Runnable r = new Runnable() { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 310 | public void run() { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 311 | synchronized (sBgDataModel) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 312 | checkItemInfoLocked(itemId, item, stackTrace); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 313 | } |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 314 | } |
| 315 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 316 | runOnWorkerThread(r); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 319 | /** |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 320 | * Update the order of the workspace screens in the database. The array list contains |
| 321 | * a list of screen ids in the order that they should appear. |
| 322 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 323 | public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 324 | final ArrayList<Long> screensCopy = new ArrayList<Long>(screens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 325 | final ContentResolver cr = context.getContentResolver(); |
| 326 | final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
| 327 | |
| 328 | // Remove any negative screen ids -- these aren't persisted |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 329 | Iterator<Long> iter = screensCopy.iterator(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 330 | while (iter.hasNext()) { |
| 331 | long id = iter.next(); |
| 332 | if (id < 0) { |
| 333 | iter.remove(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | Runnable r = new Runnable() { |
| 338 | @Override |
| 339 | public void run() { |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 340 | ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 341 | // Clear the table |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 342 | ops.add(ContentProviderOperation.newDelete(uri).build()); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 343 | int count = screensCopy.size(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 344 | for (int i = 0; i < count; i++) { |
| 345 | ContentValues v = new ContentValues(); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 346 | long screenId = screensCopy.get(i); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 347 | v.put(LauncherSettings.WorkspaceScreens._ID, screenId); |
| 348 | v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i); |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 349 | ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build()); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 350 | } |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 351 | |
| 352 | try { |
| 353 | cr.applyBatch(LauncherProvider.AUTHORITY, ops); |
| 354 | } catch (Exception ex) { |
| 355 | throw new RuntimeException(ex); |
| 356 | } |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 357 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 358 | synchronized (sBgDataModel) { |
| 359 | sBgDataModel.workspaceScreens.clear(); |
| 360 | sBgDataModel.workspaceScreens.addAll(screensCopy); |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 361 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 362 | } |
| 363 | }; |
| 364 | runOnWorkerThread(r); |
| 365 | } |
| 366 | |
| 367 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 368 | * Set this as the current Launcher activity object for the loader. |
| 369 | */ |
| 370 | public void initialize(Callbacks callbacks) { |
| 371 | synchronized (mLock) { |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 372 | Preconditions.assertUIThread(); |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 373 | mCallbacks = new WeakReference<>(callbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 377 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 378 | public void onPackageChanged(String packageName, UserHandle user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 379 | int op = PackageUpdatedTask.OP_UPDATE; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 380 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 384 | public void onPackageRemoved(String packageName, UserHandle user) { |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 385 | onPackagesRemoved(user, packageName); |
| 386 | } |
| 387 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 388 | public void onPackagesRemoved(UserHandle user, String... packages) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 389 | int op = PackageUpdatedTask.OP_REMOVE; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 390 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 394 | public void onPackageAdded(String packageName, UserHandle user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 395 | int op = PackageUpdatedTask.OP_ADD; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 396 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 400 | public void onPackagesAvailable(String[] packageNames, UserHandle user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 401 | boolean replacing) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 402 | enqueueModelUpdateTask( |
| 403 | new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 407 | public void onPackagesUnavailable(String[] packageNames, UserHandle user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 408 | boolean replacing) { |
| 409 | if (!replacing) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 410 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 411 | PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 412 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 415 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 416 | public void onPackagesSuspended(String[] packageNames, UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 417 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 418 | PackageUpdatedTask.OP_SUSPEND, user, packageNames)); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 422 | public void onPackagesUnsuspended(String[] packageNames, UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 423 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 424 | PackageUpdatedTask.OP_UNSUSPEND, user, packageNames)); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 427 | @Override |
| 428 | public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 429 | UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 430 | enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true)); |
Sunny Goyal | 50941fb | 2016-08-04 12:03:52 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 434 | UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 435 | enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false)); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Joe Onorato | 1d8e7bb | 2009-10-15 19:49:43 -0700 | [diff] [blame] | 438 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 439 | * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and |
| 440 | * ACTION_PACKAGE_CHANGED. |
| 441 | */ |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 442 | @Override |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 443 | public void onReceive(Context context, Intent intent) { |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 444 | if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 445 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 446 | final String action = intent.getAction(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 447 | if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 448 | // If we have changed locale we need to clear out the labels in all apps/workspace. |
| 449 | forceReload(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 450 | } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action) |
| 451 | || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) { |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 452 | UserManagerCompat.getInstance(context).enableAndResetCache(); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 453 | forceReload(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 454 | } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) || |
| 455 | Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) || |
| 456 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) { |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 457 | UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 458 | if (user != null) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 459 | if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) || |
| 460 | Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 461 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 462 | PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user)); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so |
| 466 | // we need to run the state change task again. |
| 467 | if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) || |
| 468 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 469 | enqueueModelUpdateTask(new UserLockStateChangedTask(user)); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 470 | } |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 471 | } |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 472 | } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) { |
| 473 | ExtractionUtils.startColorExtractionServiceIfNecessary(context); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 477 | /** |
| 478 | * Reloads the workspace items from the DB and re-binds the workspace. This should generally |
| 479 | * not be called as DB updates are automatically followed by UI update |
| 480 | */ |
| 481 | public void forceReload() { |
| 482 | synchronized (mLock) { |
| 483 | // Stop any existing loaders first, so they don't set mModelLoaded to true later |
| 484 | stopLoaderLocked(); |
| 485 | mModelLoaded = false; |
| 486 | } |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 487 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 488 | // Do this here because if the launcher activity is running it will be restarted. |
| 489 | // If it's not running startLoaderFromBackground will merely tell it that it needs |
| 490 | // to reload. |
| 491 | startLoaderFromBackground(); |
| 492 | } |
| 493 | |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 494 | /** |
| 495 | * When the launcher is in the background, it's possible for it to miss paired |
| 496 | * configuration changes. So whenever we trigger the loader from the background |
| 497 | * tell the launcher that it needs to re-run the loader when it comes back instead |
| 498 | * of doing it now. |
| 499 | */ |
| 500 | public void startLoaderFromBackground() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 501 | Callbacks callbacks = getCallback(); |
| 502 | if (callbacks != null) { |
| 503 | // Only actually run the loader if they're not paused. |
| 504 | if (!callbacks.setLoadOnResume()) { |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 505 | startLoader(callbacks.getCurrentWorkspaceScreen()); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 508 | } |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 509 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 510 | /** |
| 511 | * If there is already a loader task running, tell it to stop. |
| 512 | */ |
| 513 | private void stopLoaderLocked() { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 514 | LoaderTask oldTask = mLoaderTask; |
| 515 | if (oldTask != null) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 516 | oldTask.stopLocked(); |
| 517 | } |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Adam Cohen | 1a85c58 | 2014-09-30 09:48:49 -0700 | [diff] [blame] | 520 | public boolean isCurrentCallbacks(Callbacks callbacks) { |
| 521 | return (mCallbacks != null && mCallbacks.get() == callbacks); |
| 522 | } |
| 523 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 524 | /** |
| 525 | * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible. |
| 526 | * @return true if the page could be bound synchronously. |
| 527 | */ |
| 528 | public boolean startLoader(int synchronousBindPage) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 529 | // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems |
| 530 | InstallShortcutReceiver.enableInstallQueue(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 531 | synchronized (mLock) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 532 | // Don't bother to start the thread if we know it's not going to do anything |
| 533 | if (mCallbacks != null && mCallbacks.get() != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 534 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 535 | // Clear any pending bind-runnables from the synchronized load process. |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 536 | mUiExecutor.execute(new Runnable() { |
| 537 | public void run() { |
| 538 | oldCallbacks.clearPendingBinds(); |
| 539 | } |
| 540 | }); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 541 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 542 | // If there is already one running, tell it to stop. |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 543 | stopLoaderLocked(); |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 544 | mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage); |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 545 | if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE |
| 546 | && mModelLoaded && !mIsLoaderTaskRunning) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 547 | mLoaderTask.runBindSynchronousPage(synchronousBindPage); |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 548 | return true; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 549 | } else { |
| 550 | sWorkerThread.setPriority(Thread.NORM_PRIORITY); |
| 551 | sWorker.post(mLoaderTask); |
| 552 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 553 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 554 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 555 | return false; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 558 | public void stopLoader() { |
| 559 | synchronized (mLock) { |
| 560 | if (mLoaderTask != null) { |
| 561 | mLoaderTask.stopLocked(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 562 | } |
| 563 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 564 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 565 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 566 | /** |
| 567 | * Loads the workspace screen ids in an ordered list. |
| 568 | */ |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 569 | public static ArrayList<Long> loadWorkspaceScreensDb(Context context) { |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 570 | final ContentResolver contentResolver = context.getContentResolver(); |
| 571 | final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 572 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 573 | // Get screens ordered by rank. |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 574 | return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query( |
| 575 | screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK)); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Mario Bertschler | 817afa3 | 2017-03-15 11:56:47 -0700 | [diff] [blame^] | 578 | public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) { |
| 579 | enqueueModelUpdateTask(new ExtendedModelTask() { |
| 580 | @Override |
| 581 | public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) { |
| 582 | apps.addPromiseApp(app.getContext(), sessionInfo); |
| 583 | if (!apps.added.isEmpty()) { |
| 584 | final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added); |
| 585 | apps.added.clear(); |
| 586 | scheduleCallbackTask(new CallbackTask() { |
| 587 | @Override |
| 588 | public void execute(Callbacks callbacks) { |
| 589 | callbacks.bindAppsAdded(null, null, null, arrayList); |
| 590 | } |
| 591 | }); |
| 592 | } |
| 593 | } |
| 594 | }); |
| 595 | } |
| 596 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 597 | /** |
| 598 | * Runnable for the thread that loads the contents of the launcher: |
| 599 | * - workspace icons |
| 600 | * - widgets |
| 601 | * - all apps icons |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 602 | * - deep shortcuts within apps |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 603 | */ |
| 604 | private class LoaderTask implements Runnable { |
| 605 | private Context mContext; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 606 | private int mPageToBindFirst; |
| 607 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 608 | @Thunk boolean mIsLoadingAndBindingWorkspace; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 609 | private boolean mStopped; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 610 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 611 | LoaderTask(Context context, int pageToBindFirst) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 612 | mContext = context; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 613 | mPageToBindFirst = pageToBindFirst; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 614 | } |
| 615 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 616 | private void waitForIdle() { |
| 617 | // Wait until the either we're stopped or the other threads are done. |
| 618 | // This way we don't start loading all apps until the workspace has settled |
| 619 | // down. |
| 620 | synchronized (LoaderTask.this) { |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 621 | LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper()); |
| 622 | // Just in case mFlushingWorkerThread changes but we aren't woken up, |
| 623 | // wait no longer than 1sec at a time |
| 624 | while (!mStopped && idleLock.awaitLocked(1000)); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 625 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 626 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 627 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 628 | void runBindSynchronousPage(int synchronousBindPage) { |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 629 | if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 630 | // Ensure that we have a valid page index to load synchronously |
| 631 | throw new RuntimeException("Should not call runBindSynchronousPage() without " + |
| 632 | "valid page index"); |
| 633 | } |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 634 | if (!mModelLoaded) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 635 | // Ensure that we don't try and bind a specified page when the pages have not been |
| 636 | // loaded already (we should load everything asynchronously in that case) |
| 637 | throw new RuntimeException("Expecting AllApps and Workspace to be loaded"); |
| 638 | } |
| 639 | synchronized (mLock) { |
| 640 | if (mIsLoaderTaskRunning) { |
| 641 | // Ensure that we are never running the background loading at this point since |
| 642 | // we also touch the background collections |
| 643 | throw new RuntimeException("Error! Background loading is already running"); |
| 644 | } |
| 645 | } |
| 646 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 647 | // Divide the set of loaded items into those that we are binding synchronously, and |
| 648 | // everything else that is to be bound normally (asynchronously). |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 649 | bindWorkspace(synchronousBindPage); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 650 | // XXX: For now, continue posting the binding of AllApps as there are other issues that |
| 651 | // arise from that. |
| 652 | onlyBindAllApps(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 653 | |
| 654 | bindDeepShortcuts(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 657 | private void verifyNotStopped() throws CancellationException { |
| 658 | synchronized (LoaderTask.this) { |
| 659 | if (mStopped) { |
| 660 | throw new CancellationException("Loader stopped"); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 665 | public void run() { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 666 | synchronized (mLock) { |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 667 | if (mStopped) { |
| 668 | return; |
| 669 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 670 | mIsLoaderTaskRunning = true; |
| 671 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 672 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 673 | try { |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 674 | long now = 0; |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 675 | if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace"); |
| 676 | // Set to false in bindWorkspace() |
| 677 | mIsLoadingAndBindingWorkspace = true; |
| 678 | loadWorkspace(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 679 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 680 | verifyNotStopped(); |
| 681 | if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace"); |
| 682 | bindWorkspace(mPageToBindFirst); |
| 683 | |
| 684 | // Take a break |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 685 | if (DEBUG_LOADERS) { |
| 686 | Log.d(TAG, "step 1 completed, wait for idle"); |
| 687 | now = SystemClock.uptimeMillis(); |
| 688 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 689 | waitForIdle(); |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 690 | if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms"); |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 691 | verifyNotStopped(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 692 | |
| 693 | // second step |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 694 | if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps"); |
| 695 | loadAllApps(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 696 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 697 | verifyNotStopped(); |
| 698 | if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache"); |
| 699 | updateIconCache(); |
| 700 | |
| 701 | // Take a break |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 702 | if (DEBUG_LOADERS) { |
| 703 | Log.d(TAG, "step 2 completed, wait for idle"); |
| 704 | now = SystemClock.uptimeMillis(); |
| 705 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 706 | waitForIdle(); |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 707 | if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms"); |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 708 | verifyNotStopped(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 709 | |
| 710 | // third step |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 711 | if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts"); |
| 712 | loadDeepShortcuts(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 713 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 714 | verifyNotStopped(); |
| 715 | if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts"); |
| 716 | bindDeepShortcuts(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 717 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 718 | synchronized (mLock) { |
| 719 | // Everything loaded bind the data. |
| 720 | mModelLoaded = true; |
| 721 | mHasLoaderCompletedOnce = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 722 | } |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 723 | } catch (CancellationException e) { |
| 724 | // Loader stopped, ignore |
| 725 | } finally { |
| 726 | // Clear out this reference, otherwise we end up holding it until all of the |
| 727 | // callback runnables are done. |
| 728 | mContext = null; |
| 729 | |
| 730 | synchronized (mLock) { |
| 731 | // If we are still the last one to be scheduled, remove ourselves. |
| 732 | if (mLoaderTask == this) { |
| 733 | mLoaderTask = null; |
| 734 | } |
| 735 | mIsLoaderTaskRunning = false; |
| 736 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 737 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | public void stopLocked() { |
| 741 | synchronized (LoaderTask.this) { |
| 742 | mStopped = true; |
| 743 | this.notify(); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Gets the callbacks object. If we've been stopped, or if the launcher object |
| 749 | * has somehow been garbage collected, return null instead. Pass in the Callbacks |
| 750 | * object that was around when the deferred message was scheduled, and if there's |
| 751 | * a new Callbacks object around then also return null. This will save us from |
| 752 | * calling onto it with data that will be ignored. |
| 753 | */ |
| 754 | Callbacks tryGetCallbacks(Callbacks oldCallbacks) { |
| 755 | synchronized (mLock) { |
| 756 | if (mStopped) { |
| 757 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 758 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 759 | |
| 760 | if (mCallbacks == null) { |
| 761 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 762 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 763 | |
| 764 | final Callbacks callbacks = mCallbacks.get(); |
| 765 | if (callbacks != oldCallbacks) { |
| 766 | return null; |
| 767 | } |
| 768 | if (callbacks == null) { |
| 769 | Log.w(TAG, "no mCallbacks"); |
| 770 | return null; |
| 771 | } |
| 772 | |
| 773 | return callbacks; |
| 774 | } |
| 775 | } |
| 776 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 777 | private void loadWorkspace() { |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 778 | if (LauncherAppState.PROFILE_STARTUP) { |
| 779 | Trace.beginSection("Loading Workspace"); |
| 780 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 781 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 782 | final Context context = mContext; |
| 783 | final ContentResolver contentResolver = context.getContentResolver(); |
Sunny Goyal | 342e466 | 2017-02-02 15:21:08 -0800 | [diff] [blame] | 784 | final PackageManagerHelper pmHelper = new PackageManagerHelper(context); |
| 785 | final boolean isSafeMode = pmHelper.isSafeMode(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 786 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 787 | final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context); |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 788 | final boolean isSdCardReady = Utilities.isBootCompleted(); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 789 | final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>(); |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 790 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 791 | boolean clearDb = false; |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 792 | try { |
| 793 | ImportDataTask.performImportIfPossible(context); |
| 794 | } catch (Exception e) { |
| 795 | // Migration failed. Clear workspace. |
| 796 | clearDb = true; |
| 797 | } |
| 798 | |
| 799 | if (!clearDb && GridSizeMigrationTask.ENABLED && |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 800 | !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) { |
| 801 | // Migration failed. Clear workspace. |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 802 | clearDb = true; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 805 | if (clearDb) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 806 | Log.d(TAG, "loadWorkspace: resetting launcher database"); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 807 | LauncherSettings.Settings.call(contentResolver, |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 808 | LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 809 | } |
| 810 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 811 | Log.d(TAG, "loadWorkspace: loading default favorites"); |
| 812 | LauncherSettings.Settings.call(contentResolver, |
| 813 | LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 814 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 815 | synchronized (sBgDataModel) { |
| 816 | sBgDataModel.clear(); |
| 817 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 818 | final HashMap<String, Integer> installingPkgs = PackageInstallerCompat |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 819 | .getInstance(mContext).updateAndGetActiveSessionCache(); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 820 | sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext)); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 821 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 822 | Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>(); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 823 | final LoaderCursor c = new LoaderCursor(contentResolver.query( |
| 824 | LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp); |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 825 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 826 | HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 827 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 828 | try { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 829 | final int appWidgetIdIndex = c.getColumnIndexOrThrow( |
| 830 | LauncherSettings.Favorites.APPWIDGET_ID); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 831 | final int appWidgetProviderIndex = c.getColumnIndexOrThrow( |
| 832 | LauncherSettings.Favorites.APPWIDGET_PROVIDER); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 833 | final int spanXIndex = c.getColumnIndexOrThrow |
| 834 | (LauncherSettings.Favorites.SPANX); |
| 835 | final int spanYIndex = c.getColumnIndexOrThrow( |
| 836 | LauncherSettings.Favorites.SPANY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 837 | final int rankIndex = c.getColumnIndexOrThrow( |
| 838 | LauncherSettings.Favorites.RANK); |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 839 | final int optionsIndex = c.getColumnIndexOrThrow( |
| 840 | LauncherSettings.Favorites.OPTIONS); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 841 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 842 | final LongSparseArray<UserHandle> allUsers = c.allUsers; |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 843 | final LongSparseArray<Boolean> quietMode = new LongSparseArray<>(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 844 | final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>(); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 845 | for (UserHandle user : mUserManager.getUserProfiles()) { |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 846 | long serialNo = mUserManager.getSerialNumberForUser(user); |
| 847 | allUsers.put(serialNo, user); |
| 848 | quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user)); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 849 | |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 850 | boolean userUnlocked = mUserManager.isUserUnlocked(user); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 851 | |
| 852 | // We can only query for shortcuts when the user is unlocked. |
| 853 | if (userUnlocked) { |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 854 | List<ShortcutInfoCompat> pinnedShortcuts = |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 855 | shortcutManager.queryForPinnedShortcuts(null, user); |
| 856 | if (shortcutManager.wasLastCallSuccess()) { |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 857 | for (ShortcutInfoCompat shortcut : pinnedShortcuts) { |
| 858 | shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), |
| 859 | shortcut); |
| 860 | } |
| 861 | } else { |
| 862 | // Shortcut manager can fail due to some race condition when the |
| 863 | // lock state changes too frequently. For the purpose of the loading |
| 864 | // shortcuts, consider the user is still locked. |
| 865 | userUnlocked = false; |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 866 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 867 | } |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 868 | unlockedUsers.put(serialNo, userUnlocked); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 871 | ShortcutInfo info; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 872 | LauncherAppWidgetInfo appWidgetInfo; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 873 | Intent intent; |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 874 | String targetPkg; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 875 | |
Jon Miranda | 529af30 | 2017-02-28 14:18:54 -0800 | [diff] [blame] | 876 | FolderIconPreviewVerifier verifier = |
| 877 | new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile()); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 878 | while (!mStopped && c.moveToNext()) { |
| 879 | try { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 880 | if (c.user == null) { |
| 881 | // User has been deleted, remove the item. |
| 882 | c.markDeleted("User has been deleted"); |
| 883 | continue; |
| 884 | } |
| 885 | |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 886 | boolean allowMissingTarget = false; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 887 | switch (c.itemType) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 888 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 889 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 890 | case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 891 | intent = c.parseIntent(); |
| 892 | if (intent == null) { |
| 893 | c.markDeleted("Invalid or null intent"); |
| 894 | continue; |
| 895 | } |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 896 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 897 | int disabledState = quietMode.get(c.serialNumber) ? |
| 898 | ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0; |
| 899 | ComponentName cn = intent.getComponent(); |
| 900 | targetPkg = cn == null ? intent.getPackage() : cn.getPackageName(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 901 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 902 | if (!Process.myUserHandle().equals(c.user)) { |
| 903 | if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 904 | c.markDeleted("Legacy shortcuts are only allowed for default user"); |
| 905 | continue; |
| 906 | } else if (c.restoreFlag != 0) { |
| 907 | // Don't restore items for other profiles. |
| 908 | c.markDeleted("Restore from managed profile not supported"); |
| 909 | continue; |
| 910 | } |
| 911 | } |
| 912 | if (TextUtils.isEmpty(targetPkg) && |
| 913 | c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 914 | c.markDeleted("Only legacy shortcuts can have null package"); |
| 915 | continue; |
| 916 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 917 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 918 | // If there is no target package, its an implicit intent |
| 919 | // (legacy shortcut) which is always valid |
| 920 | boolean validTarget = TextUtils.isEmpty(targetPkg) || |
| 921 | launcherApps.isPackageEnabledForProfile(targetPkg, c.user); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 922 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 923 | if (cn != null && validTarget) { |
| 924 | // If the apk is present and the shortcut points to a specific |
| 925 | // component. |
| 926 | |
| 927 | // If the component is already present |
| 928 | if (launcherApps.isActivityEnabledForProfile(cn, c.user)) { |
| 929 | // no special handling necessary for this item |
| 930 | c.markRestored(); |
| 931 | } else { |
| 932 | if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) { |
| 933 | // We allow auto install apps to have their intent |
| 934 | // updated after an install. |
Sunny Goyal | 342e466 | 2017-02-02 15:21:08 -0800 | [diff] [blame] | 935 | intent = pmHelper.getAppLaunchIntent(targetPkg, c.user); |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 936 | if (intent != null) { |
| 937 | c.restoreFlag = 0; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 938 | c.updater().put( |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 939 | LauncherSettings.Favorites.INTENT, |
| 940 | intent.toUri(0)).commit(); |
| 941 | cn = intent.getComponent(); |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 942 | } else { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 943 | c.markDeleted("Unable to find a launch target"); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 944 | continue; |
| 945 | } |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 946 | } else { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 947 | // The app is installed but the component is no |
| 948 | // longer available. |
| 949 | c.markDeleted("Invalid component removed: " + cn); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 950 | continue; |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 951 | } |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 952 | } |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 953 | } |
| 954 | // else if cn == null => can't infer much, leave it |
| 955 | // else if !validPkg => could be restored icon or missing sd-card |
| 956 | |
| 957 | if (!TextUtils.isEmpty(targetPkg) && !validTarget) { |
| 958 | // Points to a valid app (superset of cn != null) but the apk |
| 959 | // is not available. |
| 960 | |
| 961 | if (c.restoreFlag != 0) { |
| 962 | // Package is not yet available but might be |
| 963 | // installed later. |
| 964 | FileLog.d(TAG, "package not yet restored: " + targetPkg); |
| 965 | |
| 966 | if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) { |
| 967 | // Restore has started once. |
| 968 | } else if (installingPkgs.containsKey(targetPkg)) { |
| 969 | // App restore has started. Update the flag |
| 970 | c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED; |
| 971 | c.updater().commit(); |
| 972 | } else { |
| 973 | c.markDeleted("Unrestored app removed: " + targetPkg); |
| 974 | continue; |
| 975 | } |
Sunny Goyal | ad2e91a | 2017-02-28 15:14:58 -0800 | [diff] [blame] | 976 | } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 977 | // Package is present but not available. |
| 978 | disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE; |
| 979 | // Add the icon on the workspace anyway. |
| 980 | allowMissingTarget = true; |
| 981 | } else if (!isSdCardReady) { |
| 982 | // SdCard is not ready yet. Package might get available, |
| 983 | // once it is ready. |
| 984 | Log.d(TAG, "Missing pkg, will check later: " + targetPkg); |
| 985 | pendingPackages.addToList(c.user, targetPkg); |
| 986 | // Add the icon on the workspace anyway. |
| 987 | allowMissingTarget = true; |
| 988 | } else { |
| 989 | // Do not wait for external media load anymore. |
| 990 | c.markDeleted("Invalid package removed: " + targetPkg); |
| 991 | continue; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | if (validTarget) { |
| 996 | // The shortcut points to a valid target (either no target |
| 997 | // or something which is ready to be used) |
| 998 | c.markRestored(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 999 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1000 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1001 | boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() && |
Jon Miranda | 529af30 | 2017-02-28 14:18:54 -0800 | [diff] [blame] | 1002 | !verifier.isItemInPreview(c.getInt(rankIndex)); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1003 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1004 | if (c.restoreFlag != 0) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1005 | // Already verified above that user is same as default user |
| 1006 | info = c.getRestoredItemInfo(intent); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1007 | } else if (c.itemType == |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1008 | LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1009 | info = c.getAppShortcutInfo( |
| 1010 | intent, allowMissingTarget, useLowResIcon); |
| 1011 | } else if (c.itemType == |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1012 | LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1013 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1014 | ShortcutKey key = ShortcutKey.fromIntent(intent, c.user); |
| 1015 | if (unlockedUsers.get(c.serialNumber)) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1016 | ShortcutInfoCompat pinnedShortcut = |
| 1017 | shortcutKeyToPinnedShortcuts.get(key); |
| 1018 | if (pinnedShortcut == null) { |
| 1019 | // The shortcut is no longer valid. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1020 | c.markDeleted("Pinned shortcut not found"); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1021 | continue; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1022 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1023 | info = new ShortcutInfo(pinnedShortcut, context); |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 1024 | info.iconBitmap = LauncherIcons |
| 1025 | .createShortcutIcon(pinnedShortcut, context); |
Charles He | 3ff9047 | 2017-02-15 12:22:56 +0000 | [diff] [blame] | 1026 | if (pmHelper.isAppSuspended( |
Sunny Goyal | ad2e91a | 2017-02-28 15:14:58 -0800 | [diff] [blame] | 1027 | pinnedShortcut.getPackage(), info.user)) { |
Charles He | 3ff9047 | 2017-02-15 12:22:56 +0000 | [diff] [blame] | 1028 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 1029 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1030 | intent = info.intent; |
| 1031 | } else { |
| 1032 | // Create a shortcut info in disabled mode for now. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1033 | info = c.loadSimpleShortcut(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1034 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1035 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1036 | } else { // item type == ITEM_TYPE_SHORTCUT |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1037 | info = c.loadSimpleShortcut(); |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1038 | |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1039 | // Shortcuts are only available on the primary profile |
Sunny Goyal | ad2e91a | 2017-02-28 15:14:58 -0800 | [diff] [blame] | 1040 | if (pmHelper.isAppSuspended(targetPkg, c.user)) { |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1041 | disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 1042 | } |
| 1043 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1044 | // App shortcuts that used to be automatically added to Launcher |
| 1045 | // didn't always have the correct intent flags set, so do that |
| 1046 | // here |
| 1047 | if (intent.getAction() != null && |
Michael Jurka | 9ad0056 | 2012-05-14 12:24:22 -0700 | [diff] [blame] | 1048 | intent.getCategories() != null && |
| 1049 | intent.getAction().equals(Intent.ACTION_MAIN) && |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1050 | intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1051 | intent.addFlags( |
| 1052 | Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1053 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1054 | } |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1055 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1056 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1057 | if (info != null) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1058 | c.applyCommonProperties(info); |
| 1059 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1060 | info.intent = intent; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1061 | info.rank = c.getInt(rankIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1062 | info.spanX = 1; |
| 1063 | info.spanY = 1; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1064 | // TODO: Remove this extra. Instead we should be using |
| 1065 | // itemInfo#user. |
| 1066 | info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber); |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1067 | info.isDisabled |= disabledState; |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1068 | if (isSafeMode && !Utilities.isSystemApp(context, intent)) { |
| 1069 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE; |
| 1070 | } |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1071 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1072 | if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) { |
| 1073 | Integer progress = installingPkgs.get(targetPkg); |
| 1074 | if (progress != null) { |
| 1075 | info.setInstallProgress(progress); |
| 1076 | } else { |
| 1077 | info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1081 | c.checkAndAddItem(info, sBgDataModel); |
Winson Chung | 1323b48 | 2013-08-05 12:41:55 -0700 | [diff] [blame] | 1082 | } else { |
| 1083 | throw new RuntimeException("Unexpected null ShortcutInfo"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1084 | } |
| 1085 | break; |
| 1086 | |
| 1087 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1088 | FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id); |
| 1089 | c.applyCommonProperties(folderInfo); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1090 | |
Sunny Goyal | a508e4f | 2015-05-21 09:33:57 -0700 | [diff] [blame] | 1091 | // Do not trim the folder label, as is was set by the user. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1092 | folderInfo.title = c.getString(c.titleIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1093 | folderInfo.spanX = 1; |
| 1094 | folderInfo.spanY = 1; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1095 | folderInfo.options = c.getInt(optionsIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1096 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1097 | // no special handling required for restored folders |
| 1098 | c.markRestored(); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1099 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1100 | c.checkAndAddItem(folderInfo, sBgDataModel); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1101 | break; |
| 1102 | |
| 1103 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1104 | case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1105 | // Read all Launcher-specific widget details |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1106 | boolean customWidget = c.itemType == |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1107 | LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 1108 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1109 | int appWidgetId = c.getInt(appWidgetIdIndex); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1110 | String savedProvider = c.getString(appWidgetProviderIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1111 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1112 | final ComponentName component = |
| 1113 | ComponentName.unflattenFromString(savedProvider); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1114 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1115 | final boolean isIdValid = !c.hasRestoreFlag( |
| 1116 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID); |
| 1117 | final boolean wasProviderReady = !c.hasRestoreFlag( |
| 1118 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1119 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1120 | if (widgetProvidersMap == null) { |
| 1121 | widgetProvidersMap = AppWidgetManagerCompat |
| 1122 | .getInstance(mContext).getAllProvidersMap(); |
| 1123 | } |
| 1124 | final AppWidgetProviderInfo provider = widgetProvidersMap.get( |
| 1125 | new ComponentKey( |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1126 | ComponentName.unflattenFromString(savedProvider), |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1127 | c.user)); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1128 | |
| 1129 | final boolean isProviderReady = isValidProvider(provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1130 | if (!isSafeMode && !customWidget && |
| 1131 | wasProviderReady && !isProviderReady) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1132 | c.markDeleted( |
| 1133 | "Deleting widget that isn't installed anymore: " |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1134 | + provider); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1135 | } else { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1136 | if (isProviderReady) { |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1137 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
| 1138 | provider.provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1139 | |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 1140 | // The provider is available. So the widget is either |
| 1141 | // available or not available. We do not need to track |
| 1142 | // any future restore updates. |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1143 | int status = c.restoreFlag & |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 1144 | ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1145 | if (!wasProviderReady) { |
| 1146 | // If provider was not previously ready, update the |
| 1147 | // status and UI flag. |
| 1148 | |
| 1149 | // Id would be valid only if the widget restore broadcast was received. |
| 1150 | if (isIdValid) { |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1151 | status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1152 | } else { |
| 1153 | status &= ~LauncherAppWidgetInfo |
| 1154 | .FLAG_PROVIDER_NOT_READY; |
| 1155 | } |
| 1156 | } |
| 1157 | appWidgetInfo.restoreStatus = status; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1158 | } else { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1159 | Log.v(TAG, "Widget restore pending id=" + c.id |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1160 | + " appWidgetId=" + appWidgetId |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1161 | + " status =" + c.restoreFlag); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1162 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1163 | component); |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1164 | appWidgetInfo.restoreStatus = c.restoreFlag; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1165 | Integer installProgress = installingPkgs.get(component.getPackageName()); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1166 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1167 | if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1168 | // Restore has started once. |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1169 | } else if (installProgress != null) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1170 | // App restore has started. Update the flag |
| 1171 | appWidgetInfo.restoreStatus |= |
| 1172 | LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 1173 | } else if (!isSafeMode) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1174 | c.markDeleted("Unrestored widget removed: " + component); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1175 | continue; |
| 1176 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1177 | |
| 1178 | appWidgetInfo.installProgress = |
| 1179 | installProgress == null ? 0 : installProgress; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1180 | } |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1181 | if (appWidgetInfo.hasRestoreFlag( |
| 1182 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1183 | appWidgetInfo.bindOptions = c.parseIntent(); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1184 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1185 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1186 | c.applyCommonProperties(appWidgetInfo); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1187 | appWidgetInfo.spanX = c.getInt(spanXIndex); |
| 1188 | appWidgetInfo.spanY = c.getInt(spanYIndex); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1189 | appWidgetInfo.user = c.user; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1190 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1191 | if (!c.isOnWorkspaceOrHotseat()) { |
| 1192 | c.markDeleted("Widget found where container != " + |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1193 | "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1194 | continue; |
| 1195 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1196 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1197 | if (!customWidget) { |
| 1198 | String providerName = |
| 1199 | appWidgetInfo.providerName.flattenToString(); |
| 1200 | if (!providerName.equals(savedProvider) || |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1201 | (appWidgetInfo.restoreStatus != c.restoreFlag)) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1202 | c.updater() |
| 1203 | .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, |
| 1204 | providerName) |
| 1205 | .put(LauncherSettings.Favorites.RESTORED, |
| 1206 | appWidgetInfo.restoreStatus) |
| 1207 | .commit(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1208 | } |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1209 | } |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1210 | c.checkAndAddItem(appWidgetInfo, sBgDataModel); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1211 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1212 | break; |
| 1213 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1214 | } catch (Exception e) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1215 | Log.e(TAG, "Desktop items loading interrupted", e); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1216 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1217 | } |
| 1218 | } finally { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1219 | Utilities.closeSilently(c); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1222 | // Break early if we've stopped loading |
| 1223 | if (mStopped) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1224 | sBgDataModel.clear(); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1225 | return; |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1228 | // Remove dead items |
| 1229 | if (c.commitDeleted()) { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1230 | // Remove any empty folder |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1231 | ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings |
| 1232 | .call(contentResolver, |
| 1233 | LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS) |
| 1234 | .getSerializable(LauncherSettings.Settings.EXTRA_VALUE); |
| 1235 | for (long folderId : deletedFolderIds) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1236 | sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId)); |
| 1237 | sBgDataModel.folders.remove(folderId); |
| 1238 | sBgDataModel.itemsIdMap.remove(folderId); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1242 | // Unpin shortcuts that don't exist on the workspace. |
Sunny Goyal | f75baa9 | 2016-11-22 03:23:51 +0530 | [diff] [blame] | 1243 | HashSet<ShortcutKey> pendingShortcuts = |
| 1244 | InstallShortcutReceiver.getPendingShortcuts(context); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1245 | for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1246 | MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key); |
Sunny Goyal | f75baa9 | 2016-11-22 03:23:51 +0530 | [diff] [blame] | 1247 | if ((numTimesPinned == null || numTimesPinned.value == 0) |
| 1248 | && !pendingShortcuts.contains(key)) { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1249 | // Shortcut is pinned but doesn't exist on the workspace; unpin it. |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1250 | shortcutManager.unpinShortcut(key); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
Jon Miranda | 655ec42 | 2017-02-07 11:40:22 -0800 | [diff] [blame] | 1254 | FolderIconPreviewVerifier verifier = |
| 1255 | new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile()); |
| 1256 | // Sort the folder items and make sure all items in the preview are high resolution. |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1257 | for (FolderInfo folder : sBgDataModel.folders) { |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1258 | Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR); |
Jon Miranda | 655ec42 | 2017-02-07 11:40:22 -0800 | [diff] [blame] | 1259 | verifier.setFolderInfo(folder); |
| 1260 | |
| 1261 | int numItemsInPreview = 0; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1262 | for (ShortcutInfo info : folder.contents) { |
Jon Miranda | 655ec42 | 2017-02-07 11:40:22 -0800 | [diff] [blame] | 1263 | if (info.usingLowResIcon |
| 1264 | && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION |
| 1265 | && verifier.isItemInPreview(info.rank)) { |
Sunny Goyal | 3fe4a14 | 2016-12-15 17:40:07 -0800 | [diff] [blame] | 1266 | mIconCache.getTitleAndIcon(info, false); |
Jon Miranda | 655ec42 | 2017-02-07 11:40:22 -0800 | [diff] [blame] | 1267 | numItemsInPreview++; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1268 | } |
Jon Miranda | 655ec42 | 2017-02-07 11:40:22 -0800 | [diff] [blame] | 1269 | |
| 1270 | if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) { |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1276 | c.commitRestoredItems(); |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1277 | if (!isSdCardReady && !pendingPackages.isEmpty()) { |
| 1278 | context.registerReceiver( |
| 1279 | new SdCardAvailableReceiver( |
| 1280 | LauncherModel.this, mContext, pendingPackages), |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 1281 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1282 | null, |
| 1283 | sWorker); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1286 | // Remove any empty screens |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1287 | ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1288 | for (ItemInfo item: sBgDataModel.itemsIdMap) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1289 | long screenId = item.screenId; |
| 1290 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1291 | unusedScreens.contains(screenId)) { |
| 1292 | unusedScreens.remove(screenId); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | // If there are any empty screens remove them, and update. |
| 1297 | if (unusedScreens.size() != 0) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1298 | sBgDataModel.workspaceScreens.removeAll(unusedScreens); |
| 1299 | updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1300 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1301 | } |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 1302 | if (LauncherAppState.PROFILE_STARTUP) { |
| 1303 | Trace.endSection(); |
| 1304 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1307 | /** Filters the set of items who are directly or indirectly (via another container) on the |
| 1308 | * specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1309 | private void filterCurrentWorkspaceItems(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1310 | ArrayList<ItemInfo> allWorkspaceItems, |
| 1311 | ArrayList<ItemInfo> currentScreenItems, |
| 1312 | ArrayList<ItemInfo> otherScreenItems) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1313 | // Purge any null ItemInfos |
| 1314 | Iterator<ItemInfo> iter = allWorkspaceItems.iterator(); |
| 1315 | while (iter.hasNext()) { |
| 1316 | ItemInfo i = iter.next(); |
| 1317 | if (i == null) { |
| 1318 | iter.remove(); |
| 1319 | } |
| 1320 | } |
| 1321 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1322 | // Order the set of items by their containers first, this allows use to walk through the |
| 1323 | // list sequentially, build up a list of containers that are in the specified screen, |
| 1324 | // as well as all items in those containers. |
| 1325 | Set<Long> itemsOnScreen = new HashSet<Long>(); |
| 1326 | Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() { |
| 1327 | @Override |
| 1328 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson | 12fb9fc | 2015-10-01 15:34:08 -0700 | [diff] [blame] | 1329 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1330 | } |
| 1331 | }); |
| 1332 | for (ItemInfo info : allWorkspaceItems) { |
| 1333 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1334 | if (info.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1335 | currentScreenItems.add(info); |
| 1336 | itemsOnScreen.add(info.id); |
| 1337 | } else { |
| 1338 | otherScreenItems.add(info); |
| 1339 | } |
| 1340 | } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1341 | currentScreenItems.add(info); |
| 1342 | itemsOnScreen.add(info.id); |
| 1343 | } else { |
| 1344 | if (itemsOnScreen.contains(info.container)) { |
| 1345 | currentScreenItems.add(info); |
| 1346 | itemsOnScreen.add(info.id); |
| 1347 | } else { |
| 1348 | otherScreenItems.add(info); |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | /** Filters the set of widgets which are on the specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1355 | private void filterCurrentAppWidgets(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1356 | ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 1357 | ArrayList<LauncherAppWidgetInfo> currentScreenWidgets, |
| 1358 | ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1359 | |
| 1360 | for (LauncherAppWidgetInfo widget : appWidgets) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1361 | if (widget == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1362 | if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1363 | widget.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1364 | currentScreenWidgets.add(widget); |
| 1365 | } else { |
| 1366 | otherScreenWidgets.add(widget); |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1371 | /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to |
| 1372 | * right) */ |
| 1373 | private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 1374 | final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile(); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1375 | final int screenCols = profile.numColumns; |
| 1376 | final int screenCellCount = profile.numColumns * profile.numRows; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1377 | Collections.sort(workspaceItems, new Comparator<ItemInfo>() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1378 | @Override |
| 1379 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1380 | if (lhs.container == rhs.container) { |
| 1381 | // Within containers, order by their spatial position in that container |
| 1382 | switch ((int) lhs.container) { |
| 1383 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: { |
| 1384 | long lr = (lhs.screenId * screenCellCount + |
| 1385 | lhs.cellY * screenCols + lhs.cellX); |
| 1386 | long rr = (rhs.screenId * screenCellCount + |
| 1387 | rhs.cellY * screenCols + rhs.cellX); |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1388 | return Utilities.longCompare(lr, rr); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1389 | } |
| 1390 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: { |
| 1391 | // We currently use the screen id as the rank |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1392 | return Utilities.longCompare(lhs.screenId, rhs.screenId); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1393 | } |
| 1394 | default: |
Sunny Goyal | 3d706ad | 2017-03-06 16:56:39 -0800 | [diff] [blame] | 1395 | if (FeatureFlags.IS_DOGFOOD_BUILD) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1396 | throw new RuntimeException("Unexpected container type when " + |
| 1397 | "sorting workspace items."); |
| 1398 | } |
| 1399 | return 0; |
| 1400 | } |
| 1401 | } else { |
| 1402 | // Between containers, order by hotseat, desktop |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1403 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1404 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1405 | } |
| 1406 | }); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1407 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1408 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1409 | private void bindWorkspaceScreens(final Callbacks oldCallbacks, |
| 1410 | final ArrayList<Long> orderedScreens) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1411 | final Runnable r = new Runnable() { |
| 1412 | @Override |
| 1413 | public void run() { |
| 1414 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1415 | if (callbacks != null) { |
| 1416 | callbacks.bindScreens(orderedScreens); |
| 1417 | } |
| 1418 | } |
| 1419 | }; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1420 | mUiExecutor.execute(r); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1423 | private void bindWorkspaceItems(final Callbacks oldCallbacks, |
| 1424 | final ArrayList<ItemInfo> workspaceItems, |
| 1425 | final ArrayList<LauncherAppWidgetInfo> appWidgets, |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1426 | final Executor executor) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1427 | |
| 1428 | // Bind the workspace items |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1429 | int N = workspaceItems.size(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1430 | for (int i = 0; i < N; i += ITEMS_CHUNK) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1431 | final int start = i; |
| 1432 | final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1433 | final Runnable r = new Runnable() { |
| 1434 | @Override |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1435 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 1436 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1437 | if (callbacks != null) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1438 | callbacks.bindItems(workspaceItems, start, start+chunkSize, |
| 1439 | false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1440 | } |
| 1441 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1442 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1443 | executor.execute(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1444 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1445 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1446 | // Bind the widgets, one at a time |
| 1447 | N = appWidgets.size(); |
| 1448 | for (int i = 0; i < N; i++) { |
| 1449 | final LauncherAppWidgetInfo widget = appWidgets.get(i); |
| 1450 | final Runnable r = new Runnable() { |
| 1451 | public void run() { |
| 1452 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1453 | if (callbacks != null) { |
| 1454 | callbacks.bindAppWidget(widget); |
| 1455 | } |
| 1456 | } |
| 1457 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1458 | executor.execute(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * Binds all loaded data to actual views on the main thread. |
| 1464 | */ |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1465 | private void bindWorkspace(int synchronizeBindPage) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1466 | final long t = SystemClock.uptimeMillis(); |
| 1467 | Runnable r; |
| 1468 | |
| 1469 | // Don't use these two variables in any of the callback runnables. |
| 1470 | // Otherwise we hold a reference to them. |
| 1471 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1472 | if (oldCallbacks == null) { |
| 1473 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 1474 | Log.w(TAG, "LoaderTask running with no launcher"); |
| 1475 | return; |
| 1476 | } |
| 1477 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1478 | // Save a copy of all the bg-thread collections |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1479 | ArrayList<ItemInfo> workspaceItems = new ArrayList<>(); |
| 1480 | ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>(); |
| 1481 | ArrayList<Long> orderedScreenIds = new ArrayList<>(); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 1482 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1483 | synchronized (sBgDataModel) { |
| 1484 | workspaceItems.addAll(sBgDataModel.workspaceItems); |
| 1485 | appWidgets.addAll(sBgDataModel.appWidgets); |
| 1486 | orderedScreenIds.addAll(sBgDataModel.workspaceScreens); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1489 | final int currentScreen; |
| 1490 | { |
| 1491 | int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE |
| 1492 | ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen(); |
| 1493 | if (currScreen >= orderedScreenIds.size()) { |
| 1494 | // There may be no workspace screens (just hotseat items and an empty page). |
| 1495 | currScreen = PagedView.INVALID_RESTORE_PAGE; |
| 1496 | } |
| 1497 | currentScreen = currScreen; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1498 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1499 | final boolean validFirstPage = currentScreen >= 0; |
| 1500 | final long currentScreenId = |
| 1501 | validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1502 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1503 | // Separate the items that are on the current screen, and all the other remaining items |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1504 | ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>(); |
| 1505 | ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>(); |
| 1506 | ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>(); |
| 1507 | ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1508 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1509 | filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1510 | otherWorkspaceItems); |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1511 | filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1512 | otherAppWidgets); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1513 | sortWorkspaceItemsSpatially(currentWorkspaceItems); |
| 1514 | sortWorkspaceItemsSpatially(otherWorkspaceItems); |
| 1515 | |
| 1516 | // Tell the workspace that we're about to start binding items |
| 1517 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1518 | public void run() { |
| 1519 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1520 | if (callbacks != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1521 | callbacks.clearPendingBinds(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1522 | callbacks.startBinding(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1523 | } |
| 1524 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1525 | }; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1526 | mUiExecutor.execute(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1527 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1528 | bindWorkspaceScreens(oldCallbacks, orderedScreenIds); |
| 1529 | |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1530 | Executor mainExecutor = mUiExecutor; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1531 | // Load items on the current page. |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1532 | bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1533 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1534 | // In case of validFirstPage, only bind the first screen, and defer binding the |
| 1535 | // remaining screens after first onDraw (and an optional the fade animation whichever |
| 1536 | // happens later). |
| 1537 | // This ensures that the first screen is immediately visible (eg. during rotation) |
| 1538 | // In case of !validFirstPage, bind all pages one after other. |
| 1539 | final Executor deferredExecutor = |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1540 | validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor; |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1541 | |
| 1542 | mainExecutor.execute(new Runnable() { |
| 1543 | @Override |
| 1544 | public void run() { |
| 1545 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1546 | if (callbacks != null) { |
| 1547 | callbacks.finishFirstPageBind( |
| 1548 | validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null); |
| 1549 | } |
| 1550 | } |
| 1551 | }); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1552 | |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1553 | bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1554 | |
| 1555 | // Tell the workspace that we're done binding items |
| 1556 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1557 | public void run() { |
| 1558 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1559 | if (callbacks != null) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1560 | callbacks.finishBindingItems(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1561 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1562 | |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1563 | mIsLoadingAndBindingWorkspace = false; |
| 1564 | |
| 1565 | // Run all the bind complete runnables after workspace is bound. |
| 1566 | if (!mBindCompleteRunnables.isEmpty()) { |
| 1567 | synchronized (mBindCompleteRunnables) { |
| 1568 | for (final Runnable r : mBindCompleteRunnables) { |
| 1569 | runOnWorkerThread(r); |
| 1570 | } |
| 1571 | mBindCompleteRunnables.clear(); |
| 1572 | } |
| 1573 | } |
| 1574 | |
Winson Chung | 98e030b | 2012-05-07 16:01:11 -0700 | [diff] [blame] | 1575 | // 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] | 1576 | if (DEBUG_LOADERS) { |
| 1577 | Log.d(TAG, "bound workspace in " |
| 1578 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 1579 | } |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1580 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1581 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1582 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1583 | deferredExecutor.execute(r); |
| 1584 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1585 | if (validFirstPage) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1586 | r = new Runnable() { |
| 1587 | public void run() { |
| 1588 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1589 | if (callbacks != null) { |
| 1590 | // We are loading synchronously, which means, some of the pages will be |
| 1591 | // bound after first draw. Inform the callbacks that page binding is |
| 1592 | // not complete, and schedule the remaining pages. |
| 1593 | if (currentScreen != PagedView.INVALID_RESTORE_PAGE) { |
| 1594 | callbacks.onPageBoundSynchronously(currentScreen); |
| 1595 | } |
| 1596 | callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor); |
| 1597 | } |
| 1598 | } |
| 1599 | }; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1600 | mUiExecutor.execute(r); |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 1601 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1602 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1603 | |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1604 | private void updateIconCache() { |
| 1605 | // Ignore packages which have a promise icon. |
| 1606 | HashSet<String> packagesToIgnore = new HashSet<>(); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1607 | synchronized (sBgDataModel) { |
| 1608 | for (ItemInfo info : sBgDataModel.itemsIdMap) { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1609 | if (info instanceof ShortcutInfo) { |
| 1610 | ShortcutInfo si = (ShortcutInfo) info; |
| 1611 | if (si.isPromise() && si.getTargetComponent() != null) { |
| 1612 | packagesToIgnore.add(si.getTargetComponent().getPackageName()); |
| 1613 | } |
| 1614 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 1615 | LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info; |
| 1616 | if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) { |
| 1617 | packagesToIgnore.add(lawi.providerName.getPackageName()); |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | mIconCache.updateDbIcons(packagesToIgnore); |
| 1623 | } |
| 1624 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1625 | private void onlyBindAllApps() { |
| 1626 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1627 | if (oldCallbacks == null) { |
| 1628 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 1629 | Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)"); |
| 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | // shallow copy |
Winson Chung | c208ff9 | 2012-03-29 17:37:41 -0700 | [diff] [blame] | 1634 | @SuppressWarnings("unchecked") |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1635 | final ArrayList<AppInfo> list |
| 1636 | = (ArrayList<AppInfo>) mBgAllAppsList.data.clone(); |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 1637 | Runnable r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1638 | public void run() { |
| 1639 | final long t = SystemClock.uptimeMillis(); |
| 1640 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1641 | if (callbacks != null) { |
| 1642 | callbacks.bindAllApplications(list); |
| 1643 | } |
| 1644 | if (DEBUG_LOADERS) { |
| 1645 | Log.d(TAG, "bound all " + list.size() + " apps from cache in " |
Hyunyoung Song | 747a5bc | 2016-02-08 11:31:33 -0800 | [diff] [blame] | 1646 | + (SystemClock.uptimeMillis() - t) + "ms"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1647 | } |
| 1648 | } |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 1649 | }; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1650 | mUiExecutor.execute(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1651 | } |
| 1652 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1653 | private void loadAllApps() { |
| 1654 | final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1655 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1656 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1657 | if (oldCallbacks == null) { |
| 1658 | // This launcher has exited and nobody bothered to tell us. Just bail. |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1659 | Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1660 | return; |
| 1661 | } |
| 1662 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1663 | final List<UserHandle> profiles = mUserManager.getUserProfiles(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1664 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1665 | // Clear the list of apps |
| 1666 | mBgAllAppsList.clear(); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1667 | for (UserHandle user : profiles) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1668 | // Query for the set of apps |
| 1669 | final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 1670 | final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1671 | if (DEBUG_LOADERS) { |
| 1672 | Log.d(TAG, "getActivityList took " |
| 1673 | + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user); |
| 1674 | Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user); |
| 1675 | } |
| 1676 | // Fail if we don't have any apps |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1677 | // TODO: Fix this. Only fail for the current user. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1678 | if (apps == null || apps.isEmpty()) { |
| 1679 | return; |
| 1680 | } |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1681 | boolean quietMode = mUserManager.isQuietModeEnabled(user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1682 | // Create the ApplicationInfos |
| 1683 | for (int i = 0; i < apps.size(); i++) { |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 1684 | LauncherActivityInfo app = apps.get(i); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1685 | // This builds the icon bitmaps. |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1686 | mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1687 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1688 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1689 | final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user); |
| 1690 | if (heuristic != null) { |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1691 | final Runnable r = new Runnable() { |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1692 | |
| 1693 | @Override |
| 1694 | public void run() { |
| 1695 | heuristic.processUserApps(apps); |
| 1696 | } |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1697 | }; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1698 | mUiExecutor.execute(new Runnable() { |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1699 | |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1700 | @Override |
| 1701 | public void run() { |
| 1702 | // Check isLoadingWorkspace on the UI thread, as it is updated on |
| 1703 | // the UI thread. |
| 1704 | if (mIsLoadingAndBindingWorkspace) { |
| 1705 | synchronized (mBindCompleteRunnables) { |
| 1706 | mBindCompleteRunnables.add(r); |
| 1707 | } |
| 1708 | } else { |
| 1709 | runOnWorkerThread(r); |
| 1710 | } |
| 1711 | } |
| 1712 | }); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1713 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1714 | } |
Mario Bertschler | 817afa3 | 2017-03-15 11:56:47 -0700 | [diff] [blame^] | 1715 | |
| 1716 | if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) { |
| 1717 | // get all active sessions and add them to the all apps list |
| 1718 | PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext); |
| 1719 | for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) { |
| 1720 | mBgAllAppsList.addPromiseApp(mContext, |
| 1721 | PackageInstallInfo.fromInstallingState(info)); |
| 1722 | } |
| 1723 | } |
| 1724 | |
Bjorn Bringert | 85f418d | 2013-09-06 12:50:05 +0100 | [diff] [blame] | 1725 | // Huh? Shouldn't this be inside the Runnable below? |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1726 | final ArrayList<AppInfo> added = mBgAllAppsList.added; |
| 1727 | mBgAllAppsList.added = new ArrayList<AppInfo>(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1728 | |
Mario Bertschler | 817afa3 | 2017-03-15 11:56:47 -0700 | [diff] [blame^] | 1729 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1730 | // Post callback on main thread |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1731 | mUiExecutor.execute(new Runnable() { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1732 | public void run() { |
Hyunyoung Song | 9892e58 | 2015-05-05 10:07:23 -0700 | [diff] [blame] | 1733 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1734 | final long bindTime = SystemClock.uptimeMillis(); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 1735 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1736 | if (callbacks != null) { |
| 1737 | callbacks.bindAllApplications(added); |
| 1738 | if (DEBUG_LOADERS) { |
| 1739 | Log.d(TAG, "bound " + added.size() + " apps in " |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1740 | + (SystemClock.uptimeMillis() - bindTime) + "ms"); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1741 | } |
| 1742 | } else { |
| 1743 | Log.i(TAG, "not binding apps: no Launcher activity"); |
| 1744 | } |
| 1745 | } |
| 1746 | }); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1747 | // Cleanup any data stored for a deleted user. |
| 1748 | ManagedProfileHeuristic.processAllUsers(profiles, mContext); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1749 | if (DEBUG_LOADERS) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1750 | Log.d(TAG, "Icons processed in " |
| 1751 | + (SystemClock.uptimeMillis() - loadTime) + "ms"); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 1755 | private void loadDeepShortcuts() { |
| 1756 | if (!mModelLoaded) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1757 | sBgDataModel.deepShortcutMap.clear(); |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1758 | DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext); |
| 1759 | mHasShortcutHostPermission = shortcutManager.hasHostPermission(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1760 | if (mHasShortcutHostPermission) { |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1761 | for (UserHandle user : mUserManager.getUserProfiles()) { |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1762 | if (mUserManager.isUserUnlocked(user)) { |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1763 | List<ShortcutInfoCompat> shortcuts = |
| 1764 | shortcutManager.queryForAllShortcuts(user); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1765 | sBgDataModel.updateDeepShortcutMap(null, user, shortcuts); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1766 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1767 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1768 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1769 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1770 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1771 | } |
| 1772 | |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1773 | public void bindDeepShortcuts() { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1774 | final MultiHashMap<ComponentKey, String> shortcutMapCopy = |
| 1775 | sBgDataModel.deepShortcutMap.clone(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1776 | Runnable r = new Runnable() { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1777 | @Override |
| 1778 | public void run() { |
| 1779 | Callbacks callbacks = getCallback(); |
| 1780 | if (callbacks != null) { |
| 1781 | callbacks.bindDeepShortcutMap(shortcutMapCopy); |
| 1782 | } |
| 1783 | } |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1784 | }; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1785 | mUiExecutor.execute(r); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1788 | /** |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1789 | * Refreshes the cached shortcuts if the shortcut permission has changed. |
| 1790 | * Current implementation simply reloads the workspace, but it can be optimized to |
| 1791 | * use partial updates similar to {@link UserManagerCompat} |
| 1792 | */ |
| 1793 | public void refreshShortcutsIfRequired() { |
Sunny Goyal | f5e3744 | 2016-11-02 10:31:24 -0700 | [diff] [blame] | 1794 | if (Utilities.ATLEAST_NOUGAT_MR1) { |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1795 | sWorker.removeCallbacks(mShortcutPermissionCheckRunnable); |
| 1796 | sWorker.post(mShortcutPermissionCheckRunnable); |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /** |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1801 | * Called when the icons for packages have been updated in the icon cache. |
| 1802 | */ |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1803 | public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) { |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1804 | // If any package icon has changed (app was updated while launcher was dead), |
| 1805 | // update the corresponding shortcuts. |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1806 | enqueueModelUpdateTask(new CacheDataUpdatedTask( |
| 1807 | CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages)); |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1810 | void enqueueModelUpdateTask(BaseModelUpdateTask task) { |
Jon Miranda | f28629a | 2017-02-23 15:12:42 -0800 | [diff] [blame] | 1811 | if (!mModelLoaded && mLoaderTask == null) { |
| 1812 | if (DEBUG_LOADERS) { |
| 1813 | Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task); |
| 1814 | } |
| 1815 | return; |
| 1816 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1817 | task.init(this); |
| 1818 | runOnWorkerThread(task); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1821 | /** |
| 1822 | * A task to be executed on the current callbacks on the UI thread. |
| 1823 | * If there is no current callbacks, the task is ignored. |
| 1824 | */ |
| 1825 | public interface CallbackTask { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1826 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1827 | void execute(Callbacks callbacks); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1830 | /** |
| 1831 | * A runnable which changes/updates the data model of the launcher based on certain events. |
| 1832 | */ |
| 1833 | public static abstract class BaseModelUpdateTask implements Runnable { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1834 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1835 | private LauncherModel mModel; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1836 | private Executor mUiExecutor; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1837 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1838 | /* package private */ |
| 1839 | void init(LauncherModel model) { |
| 1840 | mModel = model; |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1841 | mUiExecutor = mModel.mUiExecutor; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1842 | } |
| 1843 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1844 | @Override |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1845 | public void run() { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1846 | if (!mModel.mHasLoaderCompletedOnce) { |
Sunny Goyal | c905efc | 2015-05-06 09:54:53 -0700 | [diff] [blame] | 1847 | // Loader has not yet run. |
| 1848 | return; |
| 1849 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1850 | execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList); |
| 1851 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1852 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1853 | /** |
| 1854 | * Execute the actual task. Called on the worker thread. |
| 1855 | */ |
| 1856 | public abstract void execute( |
| 1857 | LauncherAppState app, BgDataModel dataModel, AllAppsList apps); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1858 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1859 | /** |
| 1860 | * Schedules a {@param task} to be executed on the current callbacks. |
| 1861 | */ |
| 1862 | public final void scheduleCallbackTask(final CallbackTask task) { |
| 1863 | final Callbacks callbacks = mModel.getCallback(); |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1864 | mUiExecutor.execute(new Runnable() { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1865 | public void run() { |
| 1866 | Callbacks cb = mModel.getCallback(); |
| 1867 | if (callbacks == cb && cb != null) { |
| 1868 | task.execute(callbacks); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 1869 | } |
| 1870 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1871 | }); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1872 | } |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 1873 | |
| 1874 | public ModelWriter getModelWriter() { |
| 1875 | // Updates from model task, do not deal with icon position in hotseat. |
| 1876 | return mModel.getWriter(false /* hasVerticalHotseat */); |
| 1877 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 1880 | public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) { |
| 1881 | updateAndBindShortcutInfo(new Provider<ShortcutInfo>() { |
| 1882 | @Override |
| 1883 | public ShortcutInfo get() { |
| 1884 | si.updateFromDeepShortcutInfo(info, mApp.getContext()); |
| 1885 | si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext()); |
| 1886 | return si; |
| 1887 | } |
| 1888 | }); |
| 1889 | } |
| 1890 | |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1891 | /** |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1892 | * Utility method to update a shortcut on the background thread. |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1893 | */ |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1894 | public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1895 | enqueueModelUpdateTask(new ExtendedModelTask() { |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1896 | @Override |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1897 | public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) { |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1898 | ShortcutInfo info = shortcutProvider.get(); |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1899 | ArrayList<ShortcutInfo> update = new ArrayList<>(); |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1900 | update.add(info); |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1901 | bindUpdatedShortcuts(update, info.user); |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1902 | } |
| 1903 | }); |
| 1904 | } |
| 1905 | |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1906 | private void bindWidgetsModel(final Callbacks callbacks) { |
| 1907 | final MultiHashMap<PackageItemInfo, WidgetItem> widgets |
| 1908 | = mBgWidgetsModel.getWidgetsMap().clone(); |
Sunny Goyal | b265ba7 | 2017-02-14 15:03:45 -0800 | [diff] [blame] | 1909 | mUiExecutor.execute(new Runnable() { |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1910 | @Override |
| 1911 | public void run() { |
| 1912 | Callbacks cb = getCallback(); |
| 1913 | if (callbacks == cb && cb != null) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1914 | callbacks.bindAllWidgets(widgets); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1915 | } |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 1916 | } |
| 1917 | }); |
| 1918 | } |
| 1919 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1920 | public void refreshAndBindWidgetsAndShortcuts( |
| 1921 | final Callbacks callbacks, final boolean bindFirst) { |
| 1922 | runOnWorkerThread(new Runnable() { |
| 1923 | @Override |
| 1924 | public void run() { |
| 1925 | if (bindFirst && !mBgWidgetsModel.isEmpty()) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1926 | bindWidgetsModel(callbacks); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 1927 | } |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1928 | ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext()); |
| 1929 | bindWidgetsModel(callbacks); |
| 1930 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1931 | // update the Widget entries inside DB on the worker thread. |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 1932 | mApp.getWidgetCache().removeObsoletePreviews(allWidgets); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 1933 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1934 | }); |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 1935 | } |
| 1936 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1937 | static boolean isValidProvider(AppWidgetProviderInfo provider) { |
| 1938 | return (provider != null) && (provider.provider != null) |
| 1939 | && (provider.provider.getPackageName() != null); |
| 1940 | } |
| 1941 | |
Hyunyoung Song | 3c7d9cb | 2017-01-30 15:11:27 -0800 | [diff] [blame] | 1942 | public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1943 | if (args.length > 0 && TextUtils.equals(args[0], "--all")) { |
| 1944 | writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size()); |
| 1945 | for (AppInfo info : mBgAllAppsList.data) { |
| 1946 | writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap |
| 1947 | + " componentName=" + info.componentName.getPackageName()); |
| 1948 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1949 | } |
Hyunyoung Song | 3c7d9cb | 2017-01-30 15:11:27 -0800 | [diff] [blame] | 1950 | sBgDataModel.dump(prefix, fd, writer, args); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1951 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1952 | |
| 1953 | public Callbacks getCallback() { |
| 1954 | return mCallbacks != null ? mCallbacks.get() : null; |
| 1955 | } |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1956 | |
| 1957 | /** |
| 1958 | * @return {@link FolderInfo} if its already loaded. |
| 1959 | */ |
| 1960 | public FolderInfo findFolderById(Long folderId) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1961 | synchronized (sBgDataModel) { |
| 1962 | return sBgDataModel.folders.get(folderId); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1963 | } |
| 1964 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1965 | |
| 1966 | /** |
| 1967 | * @return the looper for the worker thread which can be used to start background tasks. |
| 1968 | */ |
| 1969 | public static Looper getWorkerLooper() { |
| 1970 | return sWorkerThread.getLooper(); |
| 1971 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1972 | } |