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