blob: e3da38937d429443f7e6ccc99ee6a7aabd63b2ee [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
kholoud mohamedd5a4d602022-04-28 15:55:40 +010019import static android.app.admin.DevicePolicyManager.ACTION_DEVICE_POLICY_RESOURCE_UPDATED;
20
Sunny Goyala7a5bf32020-01-05 15:35:29 +053021import static com.android.launcher3.LauncherAppState.ACTION_FORCE_ROLOAD;
Zak Cohen3eeb41d2020-02-14 14:15:13 -080022import static com.android.launcher3.config.FeatureFlags.IS_STUDIO_BUILD;
Rohit Goyal86a1b1a2024-03-11 12:27:19 +000023import static com.android.launcher3.icons.cache.BaseIconCache.EMPTY_CLASS_NAME;
Sunny Goyal6449a212024-01-12 09:40:43 -080024import static com.android.launcher3.model.PackageUpdatedTask.OP_UPDATE;
Himanshu Gupta9aab4d42023-10-12 16:53:00 +010025import static com.android.launcher3.pm.UserCache.ACTION_PROFILE_AVAILABLE;
26import static com.android.launcher3.pm.UserCache.ACTION_PROFILE_UNAVAILABLE;
Vinit Nayak134e4292023-06-01 17:09:43 -070027import static com.android.launcher3.testing.shared.TestProtocol.sDebugTracing;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053028import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
29import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053030
Rohit Goyal86a1b1a2024-03-11 12:27:19 +000031import android.content.ComponentName;
Sunny Goyal14168432019-10-24 15:59:49 -070032import android.content.Context;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070033import android.content.Intent;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070034import android.content.pm.PackageInstaller;
Sunny Goyal6bbf6002019-04-17 18:38:52 -070035import android.content.pm.ShortcutInfo;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080036import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080037import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070038import android.util.Log;
Sunny Goyala474a9b2017-05-04 16:47:11 -070039import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010040
Pinyao Ting777c13e2022-09-16 09:44:26 -070041import androidx.annotation.NonNull;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070042import androidx.annotation.Nullable;
Sunny Goyal14168432019-10-24 15:59:49 -070043import androidx.annotation.WorkerThread;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070044
Sunny Goyal669b71f2023-01-27 14:37:07 -080045import com.android.launcher3.celllayout.CellPosMapper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070046import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070047import com.android.launcher3.icons.IconCache;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070048import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyal87dcde62019-07-17 20:35:56 -070049import com.android.launcher3.model.AllAppsList;
Sunny Goyal77954ba2024-03-25 11:53:17 -070050import com.android.launcher3.model.BaseLauncherBinder;
Sunny Goyale9956a72016-09-01 17:24:47 -070051import com.android.launcher3.model.BgDataModel;
Sunny Goyal87dcde62019-07-17 20:35:56 -070052import com.android.launcher3.model.BgDataModel.Callbacks;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070053import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal60e68c92020-08-12 13:59:27 -070054import com.android.launcher3.model.ItemInstallQueue;
Sunny Goyalb434fde2017-06-06 10:46:59 -070055import com.android.launcher3.model.LoaderTask;
Sunny Goyalce953a32023-04-14 14:08:37 -070056import com.android.launcher3.model.ModelDbController;
Sunny Goyal8b74cc72020-07-27 17:50:33 -070057import com.android.launcher3.model.ModelDelegate;
Sunny Goyal6449a212024-01-12 09:40:43 -080058import com.android.launcher3.model.ModelLauncherCallbacks;
Sunny Goyal99389382024-05-01 14:47:43 -070059import com.android.launcher3.model.ModelTaskController;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080060import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070061import com.android.launcher3.model.PackageInstallStateChangedTask;
62import com.android.launcher3.model.PackageUpdatedTask;
kholoud mohamedd5a4d602022-04-28 15:55:40 +010063import com.android.launcher3.model.ReloadStringCacheTask;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070064import com.android.launcher3.model.ShortcutsChangedTask;
65import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyale396abf2020-04-06 15:11:17 -070066import com.android.launcher3.model.data.AppInfo;
67import com.android.launcher3.model.data.ItemInfo;
68import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070069import com.android.launcher3.pm.InstallSessionTracker;
70import com.android.launcher3.pm.PackageInstallInfo;
Sunny Goyal337c81f2019-12-10 12:19:13 -080071import com.android.launcher3.pm.UserCache;
Sunny Goyalfa395362019-12-11 10:00:47 -080072import com.android.launcher3.shortcuts.ShortcutRequest;
Sunny Goyalea600c72020-07-09 19:31:40 -070073import com.android.launcher3.util.IntSet;
Jon Miranda088b9c22019-09-17 11:51:49 -070074import com.android.launcher3.util.ItemInfoMatcher;
Rohit Goyal86a1b1a2024-03-11 12:27:19 +000075import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham86222d22017-03-29 15:30:43 -070076import com.android.launcher3.util.PackageUserKey;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070077import com.android.launcher3.util.Preconditions;
Romain Guyedcce092010-03-04 13:03:17 -080078
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080079import java.io.FileDescriptor;
80import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.util.ArrayList;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082import java.util.HashSet;
Michael Jurkac2f801e2011-07-12 14:19:46 -070083import java.util.List;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080084import java.util.concurrent.CancellationException;
Sunny Goyal57e08662021-08-06 11:52:10 -070085import java.util.function.Consumer;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080086import java.util.function.Supplier;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088/**
89 * Maintains in-memory state of the Launcher. It is expected that there should be only one
90 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070091 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 */
Sunny Goyal6449a212024-01-12 09:40:43 -080093public class LauncherModel implements InstallSessionTracker.Callback {
Chris Wrenee523362014-09-09 10:09:02 -040094 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -040095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070097
Pinyao Ting777c13e2022-09-16 09:44:26 -070098 @NonNull
Sunny Goyala7a5bf32020-01-05 15:35:29 +053099 private final LauncherAppState mApp;
Pinyao Ting777c13e2022-09-16 09:44:26 -0700100 @NonNull
Winson Chung94e8ad02024-05-08 21:14:57 +0000101 private final PackageManagerHelper mPmHelper;
102 @NonNull
Sunny Goyalce953a32023-04-14 14:08:37 -0700103 private final ModelDbController mModelDbController;
104 @NonNull
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530105 private final Object mLock = new Object();
Pinyao Ting777c13e2022-09-16 09:44:26 -0700106 @Nullable
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530107 private LoaderTask mLoaderTask;
108 private boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Charlie Anderson673a81b2023-07-21 15:05:33 -0400110 // only allow this once per reboot to reload work apps
111 private boolean mShouldReloadWorkProfile = true;
112
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800113 // Indicates whether the current model data is valid or not.
114 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800116 // need to do a requery. This is only ever touched from the loader thread.
117 private boolean mModelLoaded;
Sunny Goyal777d4902021-08-27 21:22:17 +0000118 private boolean mModelDestroyed = false;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800119 public boolean isModelLoaded() {
120 synchronized (mLock) {
Sunny Goyal777d4902021-08-27 21:22:17 +0000121 return mModelLoaded && mLoaderTask == null && !mModelDestroyed;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800122 }
123 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700124
Pinyao Ting777c13e2022-09-16 09:44:26 -0700125 @NonNull
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530126 private final ArrayList<Callbacks> mCallbacksList = new ArrayList<>(1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700128 // < only access in worker thread >
Pinyao Ting777c13e2022-09-16 09:44:26 -0700129 @NonNull
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800130 private final AllAppsList mBgAllAppsList;
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700131
Sunny Goyale9956a72016-09-01 17:24:47 -0700132 /**
133 * All the static data should be accessed on the background thread, A lock should be acquired
134 * on this object when accessing any data from this model.
135 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700136 @NonNull
Sunny Goyal9ae9b602019-12-18 19:29:00 +0530137 private final BgDataModel mBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700138
Pinyao Ting777c13e2022-09-16 09:44:26 -0700139 @NonNull
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700140 private final ModelDelegate mModelDelegate;
141
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700142 private int mLastLoadId = -1;
143
Sunny Goyalc6e97692017-06-02 13:46:55 -0700144 // Runnable to check if the shortcuts permission has changed.
Pinyao Ting777c13e2022-09-16 09:44:26 -0700145 @NonNull
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700146 private final Runnable mDataValidationCheck = new Runnable() {
Sunny Goyalc6e97692017-06-02 13:46:55 -0700147 @Override
148 public void run() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700149 if (mModelLoaded) {
150 mModelDelegate.validateData();
Sunny Goyalc6e97692017-06-02 13:46:55 -0700151 }
152 }
153 };
Kenny Guyed131872014-04-30 03:02:21 +0100154
Pinyao Ting777c13e2022-09-16 09:44:26 -0700155 LauncherModel(@NonNull final Context context, @NonNull final LauncherAppState app,
156 @NonNull final IconCache iconCache, @NonNull final AppFilter appFilter,
Winson Chung94e8ad02024-05-08 21:14:57 +0000157 @NonNull final PackageManagerHelper pmHelper, final boolean isPrimaryInstance) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400158 mApp = app;
Winson Chung94e8ad02024-05-08 21:14:57 +0000159 mPmHelper = pmHelper;
Sunny Goyalce953a32023-04-14 14:08:37 -0700160 mModelDbController = new ModelDbController(context);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100161 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Winson Chung94e8ad02024-05-08 21:14:57 +0000162 mModelDelegate = ModelDelegate.newInstance(context, app, mPmHelper, mBgAllAppsList,
163 mBgDataModel, isPrimaryInstance);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800164 }
165
Pinyao Ting777c13e2022-09-16 09:44:26 -0700166 @NonNull
Schneider Victor-tulias3ec8f552021-04-13 14:17:41 -0700167 public ModelDelegate getModelDelegate() {
168 return mModelDelegate;
169 }
170
Sunny Goyalce953a32023-04-14 14:08:37 -0700171 public ModelDbController getModelDbController() {
172 return mModelDbController;
173 }
174
Sunny Goyal6449a212024-01-12 09:40:43 -0800175 public ModelLauncherCallbacks newModelCallbacks() {
176 return new ModelLauncherCallbacks(this::enqueueModelUpdateTask);
177 }
178
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800179 /**
180 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800181 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700182 public void addAndBindAddedWorkspaceItems(
183 @NonNull final List<Pair<ItemInfo, Object>> itemList) {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530184 for (Callbacks cb : getCallbacks()) {
185 cb.preAddApps();
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700186 }
Sunny Goyal91498ab2017-10-05 15:57:40 -0700187 enqueueModelUpdateTask(new AddWorkspaceItemsTask(itemList));
Winson Chung64359a52013-07-08 17:17:08 -0700188 }
189
Pinyao Ting777c13e2022-09-16 09:44:26 -0700190 @NonNull
Sebastian Franco2d023e22023-11-15 16:48:34 -0600191 public ModelWriter getWriter(final boolean verifyChanges, CellPosMapper cellPosMapper,
192 @Nullable final Callbacks owner) {
193 return new ModelWriter(mApp.getContext(), this, mBgDataModel, verifyChanges, cellPosMapper,
194 owner);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 }
196
Sunny Goyal14168432019-10-24 15:59:49 -0700197 /**
198 * Called when the icon for an app changes, outside of package event
199 */
200 @WorkerThread
Pinyao Ting777c13e2022-09-16 09:44:26 -0700201 public void onAppIconChanged(@NonNull final String packageName,
202 @NonNull final UserHandle user) {
Sunny Goyal14168432019-10-24 15:59:49 -0700203 // Update the icon for the calendar package
204 Context context = mApp.getContext();
Sunny Goyal6449a212024-01-12 09:40:43 -0800205 enqueueModelUpdateTask(new PackageUpdatedTask(OP_UPDATE, user, packageName));
Sunny Goyal14168432019-10-24 15:59:49 -0700206
Sunny Goyalfa395362019-12-11 10:00:47 -0800207 List<ShortcutInfo> pinnedShortcuts = new ShortcutRequest(context, user)
208 .forPackage(packageName).query(ShortcutRequest.PINNED);
Sunny Goyal14168432019-10-24 15:59:49 -0700209 if (!pinnedShortcuts.isEmpty()) {
210 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, pinnedShortcuts, user,
211 false));
212 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700213 }
214
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700215 /**
Sunny Goyal0fc3d122020-08-11 18:49:28 -0700216 * Called when the workspace items have drastically changed
217 */
218 public void onWorkspaceUiChanged() {
219 MODEL_EXECUTOR.execute(mModelDelegate::workspaceLoadComplete);
220 }
221
222 /**
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700223 * Called when the model is destroyed
224 */
225 public void destroy() {
Sunny Goyal777d4902021-08-27 21:22:17 +0000226 mModelDestroyed = true;
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700227 MODEL_EXECUTOR.execute(mModelDelegate::destroy);
228 }
229
Pinyao Ting777c13e2022-09-16 09:44:26 -0700230 public void onBroadcastIntent(@NonNull final Intent intent) {
Vinit Nayak134e4292023-06-01 17:09:43 -0700231 if (DEBUG_RECEIVER || sDebugTracing) Log.d(TAG, "onReceive intent=" + intent);
Joe Onorato36115782010-06-17 13:28:48 -0400232 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100233 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700234 // If we have changed locale we need to clear out the labels in all apps/workspace.
235 forceReload();
kholoud mohamedd5a4d602022-04-28 15:55:40 +0100236 } else if (ACTION_DEVICE_POLICY_RESOURCE_UPDATED.equals(action)) {
237 enqueueModelUpdateTask(new ReloadStringCacheTask(mModelDelegate));
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800238 } else if (IS_STUDIO_BUILD && ACTION_FORCE_ROLOAD.equals(action)) {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530239 for (Callbacks cb : getCallbacks()) {
240 if (cb instanceof Launcher) {
241 ((Launcher) cb).recreate();
242 }
243 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700244 }
245 }
246
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800247 /**
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700248 * Called then there use a user event
249 * @see UserCache#addUserEventListener
250 */
251 public void onUserEvent(UserHandle user, String action) {
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700252 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action)
Charlie Anderson673a81b2023-07-21 15:05:33 -0400253 && mShouldReloadWorkProfile) {
254 mShouldReloadWorkProfile = false;
255 forceReload();
256 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action)
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700257 || Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Charlie Anderson673a81b2023-07-21 15:05:33 -0400258 mShouldReloadWorkProfile = false;
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700259 enqueueModelUpdateTask(new PackageUpdatedTask(
260 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Charlie Anderson673a81b2023-07-21 15:05:33 -0400261 } else if (UserCache.ACTION_PROFILE_LOCKED.equals(action)
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700262 || UserCache.ACTION_PROFILE_UNLOCKED.equals(action)) {
263 enqueueModelUpdateTask(new UserLockStateChangedTask(
264 user, UserCache.ACTION_PROFILE_UNLOCKED.equals(action)));
Charlie Anderson673a81b2023-07-21 15:05:33 -0400265 } else if (UserCache.ACTION_PROFILE_ADDED.equals(action)
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700266 || UserCache.ACTION_PROFILE_REMOVED.equals(action)) {
267 forceReload();
Himanshu Gupta9aab4d42023-10-12 16:53:00 +0100268 } else if (ACTION_PROFILE_AVAILABLE.equals(action)
269 || ACTION_PROFILE_UNAVAILABLE.equals(action)) {
270 /*
271 * This broadcast is only available when android.os.Flags.allowPrivateProfile() is set.
272 * For Work-profile this broadcast will be sent in addition to
273 * ACTION_MANAGED_PROFILE_AVAILABLE/UNAVAILABLE.
274 * So effectively, this if block only handles the non-work profile case.
275 */
276 enqueueModelUpdateTask(new PackageUpdatedTask(
277 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700278 }
279 }
280
281 /**
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800282 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
283 * not be called as DB updates are automatically followed by UI update
284 */
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530285 public void forceReload() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800286 synchronized (mLock) {
287 // Stop any existing loaders first, so they don't set mModelLoaded to true later
Sunny Goyale86f11f2017-06-06 14:33:18 -0700288 stopLoader();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800289 mModelLoaded = false;
290 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700291
Sunny Goyal29947f02017-12-18 13:49:44 -0800292 // Start the loader if launcher is already running, otherwise the loader will run,
293 // the next time launcher starts
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530294 if (hasCallbacks()) {
295 startLoader();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700296 }
Joe Onorato36115782010-06-17 13:28:48 -0400297 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400298
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530299 /**
300 * Rebinds all existing callbacks with already loaded model
301 */
302 public void rebindCallbacks() {
303 if (hasCallbacks()) {
304 startLoader();
305 }
306 }
307
308 /**
309 * Removes an existing callback
310 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700311 public void removeCallbacks(@NonNull final Callbacks callbacks) {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530312 synchronized (mCallbacksList) {
313 Preconditions.assertUIThread();
314 if (mCallbacksList.remove(callbacks)) {
315 if (stopLoader()) {
316 // Rebind existing callbacks
317 startLoader();
318 }
319 }
320 }
321 }
322
323 /**
324 * Adds a callbacks to receive model updates
325 * @return true if workspace load was performed synchronously
326 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700327 public boolean addCallbacksAndLoad(@NonNull final Callbacks callbacks) {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530328 synchronized (mLock) {
329 addCallbacks(callbacks);
Sunny Goyal711c5962021-06-23 12:36:18 -0700330 return startLoader(new Callbacks[] { callbacks });
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530331
332 }
333 }
334
335 /**
336 * Adds a callbacks to receive model updates
337 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700338 public void addCallbacks(@NonNull final Callbacks callbacks) {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530339 Preconditions.assertUIThread();
340 synchronized (mCallbacksList) {
341 mCallbacksList.add(callbacks);
342 }
Adam Cohen1a85c582014-09-30 09:48:49 -0700343 }
344
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700345 /**
346 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
347 * @return true if the page could be bound synchronously.
348 */
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530349 public boolean startLoader() {
Sunny Goyal711c5962021-06-23 12:36:18 -0700350 return startLoader(new Callbacks[0]);
351 }
352
Pinyao Ting777c13e2022-09-16 09:44:26 -0700353 private boolean startLoader(@NonNull final Callbacks[] newCallbacks) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700354 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
Sunny Goyal60e68c92020-08-12 13:59:27 -0700355 ItemInstallQueue.INSTANCE.get(mApp.getContext())
356 .pauseModelPush(ItemInstallQueue.FLAG_LOADER_RUNNING);
Joe Onorato36115782010-06-17 13:28:48 -0400357 synchronized (mLock) {
Sunny Goyal711c5962021-06-23 12:36:18 -0700358 // If there is already one running, tell it to stop.
359 boolean wasRunning = stopLoader();
360 boolean bindDirectly = mModelLoaded && !mIsLoaderTaskRunning;
361 boolean bindAllCallbacks = wasRunning || !bindDirectly || newCallbacks.length == 0;
362 final Callbacks[] callbacksList = bindAllCallbacks ? getCallbacks() : newCallbacks;
363
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530364 if (callbacksList.length > 0) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700365 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530366 for (Callbacks cb : callbacksList) {
Sunny Goyal5fb83a42020-08-10 10:50:36 -0700367 MAIN_EXECUTOR.execute(cb::clearPendingBinds);
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530368 }
Sunny Goyal527c7d32015-08-28 15:19:36 -0700369
Sunny Goyal77954ba2024-03-25 11:53:17 -0700370 BaseLauncherBinder launcherBinder = new BaseLauncherBinder(
Sunny Goyal5fb83a42020-08-10 10:50:36 -0700371 mApp, mBgDataModel, mBgAllAppsList, callbacksList);
Sunny Goyal711c5962021-06-23 12:36:18 -0700372 if (bindDirectly) {
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700373 // Divide the set of loaded items into those that we are binding synchronously,
374 // and everything else that is to be bound normally (asynchronously).
Fengjiang Li5a36c172023-04-13 18:18:27 +0000375 launcherBinder.bindWorkspace(bindAllCallbacks, /* isBindSync= */ true);
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700376 // For now, continue posting the binding of AllApps as there are other
377 // issues that arise from that.
Stefan Andoniane82476a2023-01-10 22:46:31 +0000378 launcherBinder.bindAllApps();
379 launcherBinder.bindDeepShortcuts();
380 launcherBinder.bindWidgets();
Stefan Andoniane4609a22023-04-07 19:28:05 +0000381 if (FeatureFlags.CHANGE_MODEL_DELEGATE_LOADING_ORDER.get()) {
382 mModelDelegate.bindAllModelExtras(callbacksList);
383 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700384 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 } else {
Sunny Goyal57e08662021-08-06 11:52:10 -0700386 stopLoader();
387 mLoaderTask = new LoaderTask(
Stefan Andoniane82476a2023-01-10 22:46:31 +0000388 mApp, mBgAllAppsList, mBgDataModel, mModelDelegate, launcherBinder);
Sunny Goyal57e08662021-08-06 11:52:10 -0700389
390 // Always post the loader task, instead of running directly
391 // (even on same thread) so that we exit any nested synchronized blocks
392 MODEL_EXECUTOR.post(mLoaderTask);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400394 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700396 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397 }
398
Sunny Goyale86f11f2017-06-06 14:33:18 -0700399 /**
400 * If there is already a loader task running, tell it to stop.
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530401 * @return true if an existing loader was stopped.
Sunny Goyale86f11f2017-06-06 14:33:18 -0700402 */
Sunny Goyal711c5962021-06-23 12:36:18 -0700403 private boolean stopLoader() {
Joe Onorato36115782010-06-17 13:28:48 -0400404 synchronized (mLock) {
Sunny Goyale86f11f2017-06-06 14:33:18 -0700405 LoaderTask oldTask = mLoaderTask;
406 mLoaderTask = null;
407 if (oldTask != null) {
408 oldTask.stopLocked();
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530409 return true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400410 }
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530411 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400412 }
Joe Onorato36115782010-06-17 13:28:48 -0400413 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400414
Sunny Goyal57e08662021-08-06 11:52:10 -0700415 /**
416 * Loads the model if not loaded
417 * @param callback called with the data model upon successful load or null on model thread.
418 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700419 public void loadAsync(@NonNull final Consumer<BgDataModel> callback) {
Sunny Goyale86f11f2017-06-06 14:33:18 -0700420 synchronized (mLock) {
Sunny Goyal57e08662021-08-06 11:52:10 -0700421 if (!mModelLoaded && !mIsLoaderTaskRunning) {
422 startLoader();
Winson Chungd6519662018-02-16 03:23:51 +0000423 }
424 }
Sunny Goyal57e08662021-08-06 11:52:10 -0700425 MODEL_EXECUTOR.post(() -> callback.accept(isModelLoaded() ? mBgDataModel : null));
Winson Chungd6519662018-02-16 03:23:51 +0000426 }
427
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700428 @Override
Pinyao Ting777c13e2022-09-16 09:44:26 -0700429 public void onInstallSessionCreated(@NonNull final PackageInstallInfo sessionInfo) {
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700430 if (FeatureFlags.PROMISE_APPS_IN_ALL_APPS.get()) {
Sunny Goyal99389382024-05-01 14:47:43 -0700431 enqueueModelUpdateTask((taskController, dataModel, apps) -> {
432 apps.addPromiseApp(mApp.getContext(), sessionInfo);
433 taskController.bindApplicationsIfNeeded();
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700434 });
435 }
436 }
437
438 @Override
Pinyao Ting777c13e2022-09-16 09:44:26 -0700439 public void onSessionFailure(@NonNull final String packageName,
440 @NonNull final UserHandle user) {
Sunny Goyal99389382024-05-01 14:47:43 -0700441 enqueueModelUpdateTask((taskController, dataModel, apps) -> {
442 IconCache iconCache = mApp.getIconCache();
443 final IntSet removedIds = new IntSet();
444 HashSet<WorkspaceItemInfo> archivedWorkspaceItemsToCacheRefresh = new HashSet<>();
445 boolean isAppArchived = PackageManagerHelper.INSTANCE.get(mApp.getContext())
446 .isAppArchivedForUser(packageName, user);
447 synchronized (dataModel) {
448 if (isAppArchived) {
449 // Remove package icon cache entry for archived app in case of a session
450 // failure.
451 mApp.getIconCache().remove(
452 new ComponentName(packageName, packageName + EMPTY_CLASS_NAME),
453 user);
454 }
Rohit Goyal86a1b1a2024-03-11 12:27:19 +0000455
Sunny Goyal99389382024-05-01 14:47:43 -0700456 for (ItemInfo info : dataModel.itemsIdMap) {
457 if (info instanceof WorkspaceItemInfo
458 && ((WorkspaceItemInfo) info).hasPromiseIconUi()
459 && user.equals(info.user)
460 && info.getIntent() != null) {
461 if (TextUtils.equals(packageName, info.getIntent().getPackage())) {
462 removedIds.add(info.id);
463 }
464 if (((WorkspaceItemInfo) info).isArchived()) {
465 WorkspaceItemInfo workspaceItem = (WorkspaceItemInfo) info;
466 // Refresh icons on the workspace for archived apps.
467 iconCache.getTitleAndIcon(workspaceItem,
468 workspaceItem.usingLowResIcon());
469 archivedWorkspaceItemsToCacheRefresh.add(workspaceItem);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700470 }
471 }
472 }
473
Rohit Goyal86a1b1a2024-03-11 12:27:19 +0000474 if (isAppArchived) {
Sunny Goyal99389382024-05-01 14:47:43 -0700475 apps.updateIconsAndLabels(new HashSet<>(List.of(packageName)), user);
Jakob Schneider4f5b6012024-02-07 17:29:25 +0000476 }
Mario Bertschler817afa32017-03-15 11:56:47 -0700477 }
Sunny Goyal99389382024-05-01 14:47:43 -0700478
479 if (!removedIds.isEmpty()) {
480 taskController.deleteAndBindComponentsRemoved(
481 ItemInfoMatcher.ofItemIds(removedIds),
482 "removed because install session failed");
483 }
484 if (!archivedWorkspaceItemsToCacheRefresh.isEmpty()) {
485 taskController.bindUpdatedWorkspaceItems(
486 archivedWorkspaceItemsToCacheRefresh.stream().toList());
487 }
488 if (isAppArchived) {
489 taskController.bindApplicationsIfNeeded();
490 }
Mario Bertschler817afa32017-03-15 11:56:47 -0700491 });
492 }
493
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700494 @Override
Pinyao Ting777c13e2022-09-16 09:44:26 -0700495 public void onPackageStateChanged(@NonNull final PackageInstallInfo installInfo) {
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700496 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
497 }
498
499 /**
500 * Updates the icons and label of all pending icons for the provided package name.
501 */
502 @Override
Pinyao Ting777c13e2022-09-16 09:44:26 -0700503 public void onUpdateSessionDisplay(@NonNull final PackageUserKey key,
504 @NonNull final PackageInstaller.SessionInfo info) {
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700505 mApp.getIconCache().updateSessionCache(key, info);
506
507 HashSet<String> packages = new HashSet<>();
508 packages.add(key.mPackageName);
509 enqueueModelUpdateTask(new CacheDataUpdatedTask(
510 CacheDataUpdatedTask.OP_SESSION_UPDATE, key.mUser, packages));
511 }
512
Sunny Goyalc6e97692017-06-02 13:46:55 -0700513 public class LoaderTransaction implements AutoCloseable {
514
Pinyao Ting777c13e2022-09-16 09:44:26 -0700515 @NonNull
Sunny Goyalc6e97692017-06-02 13:46:55 -0700516 private final LoaderTask mTask;
517
Pinyao Ting777c13e2022-09-16 09:44:26 -0700518 private LoaderTransaction(@NonNull final LoaderTask task) throws CancellationException {
Sunny Goyalc6e97692017-06-02 13:46:55 -0700519 synchronized (mLock) {
520 if (mLoaderTask != task) {
521 throw new CancellationException("Loader already stopped");
522 }
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700523 mLastLoadId++;
Sunny Goyalc6e97692017-06-02 13:46:55 -0700524 mTask = task;
525 mIsLoaderTaskRunning = true;
526 mModelLoaded = false;
527 }
528 }
529
530 public void commit() {
531 synchronized (mLock) {
532 // Everything loaded bind the data.
533 mModelLoaded = true;
534 }
535 }
536
537 @Override
538 public void close() {
539 synchronized (mLock) {
540 // If we are still the last one to be scheduled, remove ourselves.
541 if (mLoaderTask == mTask) {
542 mLoaderTask = null;
543 }
544 mIsLoaderTaskRunning = false;
545 }
546 }
547 }
548
Pinyao Ting777c13e2022-09-16 09:44:26 -0700549 public LoaderTransaction beginLoader(@NonNull final LoaderTask task)
550 throws CancellationException {
Sunny Goyalc6e97692017-06-02 13:46:55 -0700551 return new LoaderTransaction(task);
552 }
553
Joe Onorato36115782010-06-17 13:28:48 -0400554 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700555 * Refreshes the cached shortcuts if the shortcut permission has changed.
556 * Current implementation simply reloads the workspace, but it can be optimized to
Sunny Goyal337c81f2019-12-10 12:19:13 -0800557 * use partial updates similar to {@link UserCache}
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700558 */
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700559 public void validateModelDataOnResume() {
560 MODEL_EXECUTOR.getHandler().removeCallbacks(mDataValidationCheck);
561 MODEL_EXECUTOR.post(mDataValidationCheck);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700562 }
563
564 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700565 * Called when the icons for packages have been updated in the icon cache.
566 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700567 public void onPackageIconsUpdated(@NonNull final HashSet<String> updatedPackages,
568 @NonNull final UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700569 // If any package icon has changed (app was updated while launcher was dead),
570 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700571 enqueueModelUpdateTask(new CacheDataUpdatedTask(
572 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -0700573 }
574
Sunny Goyalac8154a2018-09-26 12:00:30 -0700575 /**
576 * Called when the labels for the widgets has updated in the icon cache.
577 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700578 public void onWidgetLabelsUpdated(@NonNull final HashSet<String> updatedPackages,
579 @NonNull final UserHandle user) {
Sunny Goyal99389382024-05-01 14:47:43 -0700580 enqueueModelUpdateTask((taskController, dataModel, apps) -> {
581 dataModel.widgetsModel.onPackageIconsUpdated(updatedPackages, user, mApp);
582 taskController.bindUpdatedWidgets(dataModel);
Sunny Goyalac8154a2018-09-26 12:00:30 -0700583 });
584 }
585
Pinyao Ting777c13e2022-09-16 09:44:26 -0700586 public void enqueueModelUpdateTask(@NonNull final ModelUpdateTask task) {
Sunny Goyal777d4902021-08-27 21:22:17 +0000587 if (mModelDestroyed) {
588 return;
589 }
Sunny Goyal99389382024-05-01 14:47:43 -0700590 MODEL_EXECUTOR.execute(() -> {
591 if (!isModelLoaded()) {
592 // Loader has not yet run.
593 return;
594 }
595 ModelTaskController controller = new ModelTaskController(
596 mApp, mBgDataModel, mBgAllAppsList, this, MAIN_EXECUTOR);
597 task.execute(controller, mBgDataModel, mBgAllAppsList);
598 });
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700599 }
600
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700601 /**
602 * A task to be executed on the current callbacks on the UI thread.
603 * If there is no current callbacks, the task is ignored.
604 */
605 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700606
Pinyao Ting777c13e2022-09-16 09:44:26 -0700607 void execute(@NonNull Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700608 }
609
Sunny Goyal99389382024-05-01 14:47:43 -0700610 public interface ModelUpdateTask {
Joe Onorato36115782010-06-17 13:28:48 -0400611
Sunny Goyal99389382024-05-01 14:47:43 -0700612 void execute(@NonNull ModelTaskController taskController,
613 @NonNull BgDataModel dataModel, @NonNull AllAppsList apps);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800614 }
615
Pinyao Ting777c13e2022-09-16 09:44:26 -0700616 public void updateAndBindWorkspaceItem(@NonNull final WorkspaceItemInfo si,
617 @NonNull final ShortcutInfo info) {
Sunny Goyal95899162019-03-27 16:03:06 -0700618 updateAndBindWorkspaceItem(() -> {
Sunny Goyal83fd25e2018-07-09 16:47:01 -0700619 si.updateFromDeepShortcutInfo(info, mApp.getContext());
Sunny Goyal18204e42020-02-06 11:28:01 -0800620 mApp.getIconCache().getShortcutIcon(si, info);
Sunny Goyal83fd25e2018-07-09 16:47:01 -0700621 return si;
Sunny Goyal1b072632017-01-18 11:30:23 -0800622 });
623 }
624
Sunny Goyal10923b32016-07-20 15:42:44 -0700625 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -0800626 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -0700627 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700628 public void updateAndBindWorkspaceItem(
629 @NonNull final Supplier<WorkspaceItemInfo> itemProvider) {
Sunny Goyal99389382024-05-01 14:47:43 -0700630 enqueueModelUpdateTask((taskController, dataModel, apps) -> {
631 WorkspaceItemInfo info = itemProvider.get();
632 taskController.getModelWriter().updateItemInDatabase(info);
633 ArrayList<WorkspaceItemInfo> update = new ArrayList<>();
634 update.add(info);
635 taskController.bindUpdatedWorkspaceItems(update);
Sunny Goyal10923b32016-07-20 15:42:44 -0700636 });
637 }
638
Sunny Goyalc6e97692017-06-02 13:46:55 -0700639 public void refreshAndBindWidgetsAndShortcuts(@Nullable final PackageUserKey packageUser) {
Sunny Goyal99389382024-05-01 14:47:43 -0700640 enqueueModelUpdateTask((taskController, dataModel, apps) -> {
641 dataModel.widgetsModel.update(taskController.getApp(), packageUser);
642 taskController.bindUpdatedWidgets(dataModel);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800643 });
Michael Jurkac402cd92013-05-20 15:49:32 +0200644 }
645
Pinyao Ting777c13e2022-09-16 09:44:26 -0700646 public void dumpState(@Nullable final String prefix, @Nullable final FileDescriptor fd,
647 @NonNull final PrintWriter writer, @NonNull final String[] args) {
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -0800648 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
649 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
650 for (AppInfo info : mBgAllAppsList.data) {
Sunny Goyal3808a692019-10-25 13:41:28 -0700651 writer.println(prefix + " title=\"" + info.title
652 + "\" bitmapIcon=" + info.bitmap.icon
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -0800653 + " componentName=" + info.componentName.getPackageName());
654 }
Andy Wickhambb993c42021-02-26 12:56:25 -0800655 writer.println();
Joe Onorato36115782010-06-17 13:28:48 -0400656 }
Andy Wickhambb993c42021-02-26 12:56:25 -0800657 mModelDelegate.dump(prefix, fd, writer, args);
Sunny Goyal9ae9b602019-12-18 19:29:00 +0530658 mBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -0800659 }
Sunny Goyale0f58d72014-11-10 18:05:31 -0800660
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530661 /**
662 * Returns true if there are any callbacks attached to the model
663 */
664 public boolean hasCallbacks() {
665 synchronized (mCallbacksList) {
666 return !mCallbacksList.isEmpty();
667 }
668 }
669
670 /**
671 * Returns an array of currently attached callbacks
672 */
Pinyao Ting777c13e2022-09-16 09:44:26 -0700673 @NonNull
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530674 public Callbacks[] getCallbacks() {
675 synchronized (mCallbacksList) {
676 return mCallbacksList.toArray(new Callbacks[mCallbacksList.size()]);
677 }
Sunny Goyale0f58d72014-11-10 18:05:31 -0800678 }
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700679
680 /**
681 * Returns the ID for the last model load. If the load ID doesn't match for a transaction, the
682 * transaction should be ignored.
683 */
684 public int getLastLoadId() {
685 return mLastLoadId;
686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687}