blob: 6966abf7e28c5dcf8995f4a25765607b4ce415ef [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
Sunny Goyala7a5bf32020-01-05 15:35:29 +053019import static com.android.launcher3.LauncherAppState.ACTION_FORCE_ROLOAD;
Zak Cohen3eeb41d2020-02-14 14:15:13 -080020import static com.android.launcher3.config.FeatureFlags.IS_STUDIO_BUILD;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053021import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
22import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Sunny Goyala7a5bf32020-01-05 15:35:29 +053023
Sunny Goyal14168432019-10-24 15:59:49 -070024import android.content.Context;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070025import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070026import android.content.pm.LauncherApps;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070027import android.content.pm.PackageInstaller;
Sunny Goyal6bbf6002019-04-17 18:38:52 -070028import android.content.pm.ShortcutInfo;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080029import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080030import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070031import android.util.Log;
Sunny Goyala474a9b2017-05-04 16:47:11 -070032import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010033
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070034import androidx.annotation.Nullable;
Sunny Goyal14168432019-10-24 15:59:49 -070035import androidx.annotation.WorkerThread;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070036
Sunny Goyal045b4fa2019-09-20 12:51:37 -070037import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070038import com.android.launcher3.icons.IconCache;
Samuel Fufacc1e1072019-09-06 16:19:11 -070039import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070040import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyal87dcde62019-07-17 20:35:56 -070041import com.android.launcher3.model.AllAppsList;
Sunny Goyale75d9f12018-01-31 11:59:00 -080042import com.android.launcher3.model.BaseModelUpdateTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070043import com.android.launcher3.model.BgDataModel;
Sunny Goyal87dcde62019-07-17 20:35:56 -070044import com.android.launcher3.model.BgDataModel.Callbacks;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070045import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal60e68c92020-08-12 13:59:27 -070046import com.android.launcher3.model.ItemInstallQueue;
Sunny Goyalf8d6ed22017-06-01 14:26:38 -070047import com.android.launcher3.model.LoaderResults;
Sunny Goyalb434fde2017-06-06 10:46:59 -070048import com.android.launcher3.model.LoaderTask;
Sunny Goyal8b74cc72020-07-27 17:50:33 -070049import com.android.launcher3.model.ModelDelegate;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080050import com.android.launcher3.model.ModelWriter;
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000051import com.android.launcher3.model.PackageIncrementalDownloadUpdatedTask;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070052import com.android.launcher3.model.PackageInstallStateChangedTask;
53import com.android.launcher3.model.PackageUpdatedTask;
54import com.android.launcher3.model.ShortcutsChangedTask;
55import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyale396abf2020-04-06 15:11:17 -070056import com.android.launcher3.model.data.AppInfo;
57import com.android.launcher3.model.data.ItemInfo;
58import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070059import com.android.launcher3.pm.InstallSessionTracker;
60import com.android.launcher3.pm.PackageInstallInfo;
Sunny Goyal337c81f2019-12-10 12:19:13 -080061import com.android.launcher3.pm.UserCache;
Sunny Goyalfa395362019-12-11 10:00:47 -080062import com.android.launcher3.shortcuts.ShortcutRequest;
Sunny Goyalea600c72020-07-09 19:31:40 -070063import com.android.launcher3.util.IntSet;
Jon Miranda088b9c22019-09-17 11:51:49 -070064import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickham86222d22017-03-29 15:30:43 -070065import com.android.launcher3.util.PackageUserKey;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070066import com.android.launcher3.util.Preconditions;
Romain Guyedcce092010-03-04 13:03:17 -080067
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080068import java.io.FileDescriptor;
69import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.util.ArrayList;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070071import java.util.HashSet;
Michael Jurkac2f801e2011-07-12 14:19:46 -070072import java.util.List;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080073import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070074import java.util.concurrent.Executor;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080075import java.util.function.Supplier;
Michael Jurkac2f801e2011-07-12 14:19:46 -070076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077/**
78 * Maintains in-memory state of the Launcher. It is expected that there should be only one
79 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070080 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 */
Sunny Goyale7b00122019-10-02 16:13:34 -070082public class LauncherModel extends LauncherApps.Callback implements InstallSessionTracker.Callback {
Chris Wrenee523362014-09-09 10:09:02 -040083 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -040084
Joe Onorato9c1289c2009-08-17 11:03:03 -040085 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070086
Sunny Goyala7a5bf32020-01-05 15:35:29 +053087 private final LauncherAppState mApp;
88 private final Object mLock = new Object();
Sunny Goyala7a5bf32020-01-05 15:35:29 +053089
90 private LoaderTask mLoaderTask;
91 private boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080093 // Indicates whether the current model data is valid or not.
94 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -070095 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080096 // need to do a requery. This is only ever touched from the loader thread.
97 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -080098 public boolean isModelLoaded() {
99 synchronized (mLock) {
100 return mModelLoaded && mLoaderTask == null;
101 }
102 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700103
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530104 private final ArrayList<Callbacks> mCallbacksList = new ArrayList<>(1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700106 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800107 private final AllAppsList mBgAllAppsList;
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700108
Sunny Goyale9956a72016-09-01 17:24:47 -0700109 /**
110 * All the static data should be accessed on the background thread, A lock should be acquired
111 * on this object when accessing any data from this model.
112 */
Sunny Goyal9ae9b602019-12-18 19:29:00 +0530113 private final BgDataModel mBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700114
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700115 private final ModelDelegate mModelDelegate;
116
Sunny Goyalc6e97692017-06-02 13:46:55 -0700117 // Runnable to check if the shortcuts permission has changed.
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700118 private final Runnable mDataValidationCheck = new Runnable() {
Sunny Goyalc6e97692017-06-02 13:46:55 -0700119 @Override
120 public void run() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700121 if (mModelLoaded) {
122 mModelDelegate.validateData();
Sunny Goyalc6e97692017-06-02 13:46:55 -0700123 }
124 }
125 };
Kenny Guyed131872014-04-30 03:02:21 +0100126
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700127 LauncherModel(Context context, LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400128 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100129 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700130 mModelDelegate = ModelDelegate.newInstance(context, app, mBgAllAppsList, mBgDataModel);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800131 }
132
Schneider Victor-tulias3ec8f552021-04-13 14:17:41 -0700133 public ModelDelegate getModelDelegate() {
134 return mModelDelegate;
135 }
136
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800137 /**
138 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800139 */
Sunny Goyal91498ab2017-10-05 15:57:40 -0700140 public void addAndBindAddedWorkspaceItems(List<Pair<ItemInfo, Object>> itemList) {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530141 for (Callbacks cb : getCallbacks()) {
142 cb.preAddApps();
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700143 }
Sunny Goyal91498ab2017-10-05 15:57:40 -0700144 enqueueModelUpdateTask(new AddWorkspaceItemsTask(itemList));
Winson Chung64359a52013-07-08 17:17:08 -0700145 }
146
Sunny Goyal711c5962021-06-23 12:36:18 -0700147 public ModelWriter getWriter(boolean hasVerticalHotseat, boolean verifyChanges,
148 @Nullable Callbacks owner) {
Sunny Goyal9ae9b602019-12-18 19:29:00 +0530149 return new ModelWriter(mApp.getContext(), this, mBgDataModel,
Sunny Goyal711c5962021-06-23 12:36:18 -0700150 hasVerticalHotseat, verifyChanges, owner);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
Kenny Guyed131872014-04-30 03:02:21 +0100153 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800154 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100155 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700156 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100157 }
158
159 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800160 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700161 onPackagesRemoved(user, packageName);
162 }
163
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800164 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100165 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalbb18f4c2020-02-25 15:27:06 -0800166 FileLog.d(TAG, "package removed received " + TextUtils.join(",", packages));
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700167 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100168 }
169
170 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800171 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100172 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700173 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100174 }
175
176 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800177 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100178 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700179 enqueueModelUpdateTask(
180 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100181 }
182
183 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800184 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100185 boolean replacing) {
186 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700187 enqueueModelUpdateTask(new PackageUpdatedTask(
188 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100189 }
Kenny Guyed131872014-04-30 03:02:21 +0100190 }
191
Kenny Guy44cba692016-01-21 19:50:02 +0000192 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800193 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700194 enqueueModelUpdateTask(new PackageUpdatedTask(
195 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000196 }
197
198 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800199 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700200 enqueueModelUpdateTask(new PackageUpdatedTask(
201 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000202 }
203
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700204 @Override
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000205 public void onPackageLoadingProgressChanged(
206 String packageName, UserHandle user, float progress) {
207 if (Utilities.ATLEAST_S) {
208 enqueueModelUpdateTask(new PackageIncrementalDownloadUpdatedTask(
209 packageName, user, progress));
210 }
211 }
212
213 @Override
Sunny Goyal6bbf6002019-04-17 18:38:52 -0700214 public void onShortcutsChanged(String packageName, List<ShortcutInfo> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800215 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700216 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700217 }
218
Sunny Goyal14168432019-10-24 15:59:49 -0700219 /**
220 * Called when the icon for an app changes, outside of package event
221 */
222 @WorkerThread
223 public void onAppIconChanged(String packageName, UserHandle user) {
224 // Update the icon for the calendar package
225 Context context = mApp.getContext();
226 onPackageChanged(packageName, user);
227
Sunny Goyalfa395362019-12-11 10:00:47 -0800228 List<ShortcutInfo> pinnedShortcuts = new ShortcutRequest(context, user)
229 .forPackage(packageName).query(ShortcutRequest.PINNED);
Sunny Goyal14168432019-10-24 15:59:49 -0700230 if (!pinnedShortcuts.isEmpty()) {
231 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, pinnedShortcuts, user,
232 false));
233 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700234 }
235
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700236 /**
Sunny Goyal0fc3d122020-08-11 18:49:28 -0700237 * Called when the workspace items have drastically changed
238 */
239 public void onWorkspaceUiChanged() {
240 MODEL_EXECUTOR.execute(mModelDelegate::workspaceLoadComplete);
241 }
242
243 /**
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700244 * Called when the model is destroyed
245 */
246 public void destroy() {
247 MODEL_EXECUTOR.execute(mModelDelegate::destroy);
248 }
249
Sunny Goyale7b00122019-10-02 16:13:34 -0700250 public void onBroadcastIntent(Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400251 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Joe Onorato36115782010-06-17 13:28:48 -0400252 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100253 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700254 // If we have changed locale we need to clear out the labels in all apps/workspace.
255 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700256 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
257 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
258 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800259 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700260 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700261 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
262 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700263 enqueueModelUpdateTask(new PackageUpdatedTask(
264 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700265 }
266
267 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
268 // we need to run the state change task again.
269 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
270 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Pinyao Ting8e5db812020-04-06 18:11:29 -0700271 enqueueModelUpdateTask(new UserLockStateChangedTask(
272 user, Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700273 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700274 }
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800275 } else if (IS_STUDIO_BUILD && ACTION_FORCE_ROLOAD.equals(action)) {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530276 for (Callbacks cb : getCallbacks()) {
277 if (cb instanceof Launcher) {
278 ((Launcher) cb).recreate();
279 }
280 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700281 }
282 }
283
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800284 /**
285 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
286 * not be called as DB updates are automatically followed by UI update
287 */
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530288 public void forceReload() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800289 synchronized (mLock) {
290 // Stop any existing loaders first, so they don't set mModelLoaded to true later
Sunny Goyale86f11f2017-06-06 14:33:18 -0700291 stopLoader();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800292 mModelLoaded = false;
293 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700294
Sunny Goyal29947f02017-12-18 13:49:44 -0800295 // Start the loader if launcher is already running, otherwise the loader will run,
296 // the next time launcher starts
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530297 if (hasCallbacks()) {
298 startLoader();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700299 }
Joe Onorato36115782010-06-17 13:28:48 -0400300 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400301
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530302 /**
303 * Rebinds all existing callbacks with already loaded model
304 */
305 public void rebindCallbacks() {
306 if (hasCallbacks()) {
307 startLoader();
308 }
309 }
310
311 /**
312 * Removes an existing callback
313 */
314 public void removeCallbacks(Callbacks callbacks) {
315 synchronized (mCallbacksList) {
316 Preconditions.assertUIThread();
317 if (mCallbacksList.remove(callbacks)) {
318 if (stopLoader()) {
319 // Rebind existing callbacks
320 startLoader();
321 }
322 }
323 }
324 }
325
326 /**
327 * Adds a callbacks to receive model updates
328 * @return true if workspace load was performed synchronously
329 */
330 public boolean addCallbacksAndLoad(Callbacks callbacks) {
331 synchronized (mLock) {
332 addCallbacks(callbacks);
Sunny Goyal711c5962021-06-23 12:36:18 -0700333 return startLoader(new Callbacks[] { callbacks });
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530334
335 }
336 }
337
338 /**
339 * Adds a callbacks to receive model updates
340 */
341 public void addCallbacks(Callbacks callbacks) {
342 Preconditions.assertUIThread();
343 synchronized (mCallbacksList) {
344 mCallbacksList.add(callbacks);
345 }
Adam Cohen1a85c582014-09-30 09:48:49 -0700346 }
347
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700348 /**
349 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
350 * @return true if the page could be bound synchronously.
351 */
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530352 public boolean startLoader() {
Sunny Goyal711c5962021-06-23 12:36:18 -0700353 return startLoader(new Callbacks[0]);
354 }
355
356 private boolean startLoader(Callbacks[] newCallbacks) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700357 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
Sunny Goyal60e68c92020-08-12 13:59:27 -0700358 ItemInstallQueue.INSTANCE.get(mApp.getContext())
359 .pauseModelPush(ItemInstallQueue.FLAG_LOADER_RUNNING);
Joe Onorato36115782010-06-17 13:28:48 -0400360 synchronized (mLock) {
Sunny Goyal711c5962021-06-23 12:36:18 -0700361 // If there is already one running, tell it to stop.
362 boolean wasRunning = stopLoader();
363 boolean bindDirectly = mModelLoaded && !mIsLoaderTaskRunning;
364 boolean bindAllCallbacks = wasRunning || !bindDirectly || newCallbacks.length == 0;
365 final Callbacks[] callbacksList = bindAllCallbacks ? getCallbacks() : newCallbacks;
366
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530367 if (callbacksList.length > 0) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700368 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530369 for (Callbacks cb : callbacksList) {
Sunny Goyal5fb83a42020-08-10 10:50:36 -0700370 MAIN_EXECUTOR.execute(cb::clearPendingBinds);
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530371 }
Sunny Goyal527c7d32015-08-28 15:19:36 -0700372
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530373 LoaderResults loaderResults = new LoaderResults(
Sunny Goyal5fb83a42020-08-10 10:50:36 -0700374 mApp, mBgDataModel, mBgAllAppsList, callbacksList);
Sunny Goyal711c5962021-06-23 12:36:18 -0700375 if (bindDirectly) {
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700376 // Divide the set of loaded items into those that we are binding synchronously,
377 // and everything else that is to be bound normally (asynchronously).
Sunny Goyal711c5962021-06-23 12:36:18 -0700378 loaderResults.bindWorkspace(bindAllCallbacks);
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700379 // For now, continue posting the binding of AllApps as there are other
380 // issues that arise from that.
381 loaderResults.bindAllApps();
382 loaderResults.bindDeepShortcuts();
Sunny Goyalc6e97692017-06-02 13:46:55 -0700383 loaderResults.bindWidgets();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700384 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 } else {
Sunny Goyale86f11f2017-06-06 14:33:18 -0700386 startLoaderForResults(loaderResults);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700387 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400388 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700390 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 }
392
Sunny Goyale86f11f2017-06-06 14:33:18 -0700393 /**
394 * If there is already a loader task running, tell it to stop.
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530395 * @return true if an existing loader was stopped.
Sunny Goyale86f11f2017-06-06 14:33:18 -0700396 */
Sunny Goyal711c5962021-06-23 12:36:18 -0700397 private boolean stopLoader() {
Joe Onorato36115782010-06-17 13:28:48 -0400398 synchronized (mLock) {
Sunny Goyale86f11f2017-06-06 14:33:18 -0700399 LoaderTask oldTask = mLoaderTask;
400 mLoaderTask = null;
401 if (oldTask != null) {
402 oldTask.stopLocked();
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530403 return true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404 }
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530405 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 }
Joe Onorato36115782010-06-17 13:28:48 -0400407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408
Sunny Goyale86f11f2017-06-06 14:33:18 -0700409 public void startLoaderForResults(LoaderResults results) {
410 synchronized (mLock) {
411 stopLoader();
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700412 mLoaderTask = new LoaderTask(
413 mApp, mBgAllAppsList, mBgDataModel, mModelDelegate, results);
Sunny Goyal51f220d2019-05-29 11:05:30 -0700414
415 // Always post the loader task, instead of running directly (even on same thread) so
416 // that we exit any nested synchronized blocks
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700417 MODEL_EXECUTOR.post(mLoaderTask);
Sunny Goyale86f11f2017-06-06 14:33:18 -0700418 }
419 }
420
Winson Chungd6519662018-02-16 03:23:51 +0000421 public void startLoaderForResultsIfNotLoaded(LoaderResults results) {
422 synchronized (mLock) {
423 if (!isModelLoaded()) {
424 Log.d(TAG, "Workspace not loaded, loading now");
425 startLoaderForResults(results);
426 }
427 }
428 }
429
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700430 @Override
Mario Bertschler817afa32017-03-15 11:56:47 -0700431 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700432 if (FeatureFlags.PROMISE_APPS_IN_ALL_APPS.get()) {
433 enqueueModelUpdateTask(new BaseModelUpdateTask() {
434 @Override
435 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
436 apps.addPromiseApp(app.getContext(), sessionInfo);
437 bindApplicationsIfNeeded();
438 }
439 });
440 }
441 }
442
443 @Override
444 public void onSessionFailure(String packageName, UserHandle user) {
445 if (!FeatureFlags.PROMISE_APPS_NEW_INSTALLS.get()) {
446 return;
447 }
Sunny Goyale86f11f2017-06-06 14:33:18 -0700448 enqueueModelUpdateTask(new BaseModelUpdateTask() {
Mario Bertschler817afa32017-03-15 11:56:47 -0700449 @Override
450 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyalea600c72020-07-09 19:31:40 -0700451 final IntSet removedIds = new IntSet();
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700452 synchronized (dataModel) {
453 for (ItemInfo info : dataModel.itemsIdMap) {
454 if (info instanceof WorkspaceItemInfo
455 && ((WorkspaceItemInfo) info).hasPromiseIconUi()
456 && user.equals(info.user)
457 && info.getIntent() != null
458 && TextUtils.equals(packageName, info.getIntent().getPackage())) {
Sunny Goyalea600c72020-07-09 19:31:40 -0700459 removedIds.add(info.id);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700460 }
461 }
462 }
463
464 if (!removedIds.isEmpty()) {
Sunny Goyalea600c72020-07-09 19:31:40 -0700465 deleteAndBindComponentsRemoved(ItemInfoMatcher.ofItemIds(removedIds));
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700466 }
Mario Bertschler817afa32017-03-15 11:56:47 -0700467 }
468 });
469 }
470
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700471 @Override
472 public void onPackageStateChanged(PackageInstallInfo installInfo) {
473 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
474 }
475
476 /**
477 * Updates the icons and label of all pending icons for the provided package name.
478 */
479 @Override
480 public void onUpdateSessionDisplay(PackageUserKey key, PackageInstaller.SessionInfo info) {
481 mApp.getIconCache().updateSessionCache(key, info);
482
483 HashSet<String> packages = new HashSet<>();
484 packages.add(key.mPackageName);
485 enqueueModelUpdateTask(new CacheDataUpdatedTask(
486 CacheDataUpdatedTask.OP_SESSION_UPDATE, key.mUser, packages));
487 }
488
Sunny Goyalc6e97692017-06-02 13:46:55 -0700489 public class LoaderTransaction implements AutoCloseable {
490
491 private final LoaderTask mTask;
492
493 private LoaderTransaction(LoaderTask task) throws CancellationException {
494 synchronized (mLock) {
495 if (mLoaderTask != task) {
496 throw new CancellationException("Loader already stopped");
497 }
498 mTask = task;
499 mIsLoaderTaskRunning = true;
500 mModelLoaded = false;
501 }
502 }
503
504 public void commit() {
505 synchronized (mLock) {
506 // Everything loaded bind the data.
507 mModelLoaded = true;
508 }
509 }
510
511 @Override
512 public void close() {
513 synchronized (mLock) {
514 // If we are still the last one to be scheduled, remove ourselves.
515 if (mLoaderTask == mTask) {
516 mLoaderTask = null;
517 }
518 mIsLoaderTaskRunning = false;
519 }
520 }
521 }
522
523 public LoaderTransaction beginLoader(LoaderTask task) throws CancellationException {
524 return new LoaderTransaction(task);
525 }
526
Joe Onorato36115782010-06-17 13:28:48 -0400527 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700528 * Refreshes the cached shortcuts if the shortcut permission has changed.
529 * Current implementation simply reloads the workspace, but it can be optimized to
Sunny Goyal337c81f2019-12-10 12:19:13 -0800530 * use partial updates similar to {@link UserCache}
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700531 */
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700532 public void validateModelDataOnResume() {
533 MODEL_EXECUTOR.getHandler().removeCallbacks(mDataValidationCheck);
534 MODEL_EXECUTOR.post(mDataValidationCheck);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700535 }
536
537 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700538 * Called when the icons for packages have been updated in the icon cache.
539 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800540 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700541 // If any package icon has changed (app was updated while launcher was dead),
542 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700543 enqueueModelUpdateTask(new CacheDataUpdatedTask(
544 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -0700545 }
546
Sunny Goyalac8154a2018-09-26 12:00:30 -0700547 /**
548 * Called when the labels for the widgets has updated in the icon cache.
549 */
550 public void onWidgetLabelsUpdated(HashSet<String> updatedPackages, UserHandle user) {
551 enqueueModelUpdateTask(new BaseModelUpdateTask() {
552 @Override
553 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
554 dataModel.widgetsModel.onPackageIconsUpdated(updatedPackages, user, app);
555 bindUpdatedWidgets(dataModel);
556 }
557 });
558 }
559
Sunny Goyale86f11f2017-06-06 14:33:18 -0700560 public void enqueueModelUpdateTask(ModelUpdateTask task) {
Sunny Goyal5fb83a42020-08-10 10:50:36 -0700561 task.init(mApp, this, mBgDataModel, mBgAllAppsList, MAIN_EXECUTOR);
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700562 MODEL_EXECUTOR.execute(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700563 }
564
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700565 /**
566 * A task to be executed on the current callbacks on the UI thread.
567 * If there is no current callbacks, the task is ignored.
568 */
569 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700570
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700571 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700572 }
573
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700574 /**
575 * A runnable which changes/updates the data model of the launcher based on certain events.
576 */
Sunny Goyale86f11f2017-06-06 14:33:18 -0700577 public interface ModelUpdateTask extends Runnable {
Joe Onorato36115782010-06-17 13:28:48 -0400578
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700579 /**
Sunny Goyale86f11f2017-06-06 14:33:18 -0700580 * Called before the task is posted to initialize the internal state.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700581 */
Sunny Goyale86f11f2017-06-06 14:33:18 -0700582 void init(LauncherAppState app, LauncherModel model,
583 BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor);
Sunny Goyale0f58d72014-11-10 18:05:31 -0800584
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800585 }
586
Sunny Goyal6bbf6002019-04-17 18:38:52 -0700587 public void updateAndBindWorkspaceItem(WorkspaceItemInfo si, ShortcutInfo info) {
Sunny Goyal95899162019-03-27 16:03:06 -0700588 updateAndBindWorkspaceItem(() -> {
Sunny Goyal83fd25e2018-07-09 16:47:01 -0700589 si.updateFromDeepShortcutInfo(info, mApp.getContext());
Sunny Goyal18204e42020-02-06 11:28:01 -0800590 mApp.getIconCache().getShortcutIcon(si, info);
Sunny Goyal83fd25e2018-07-09 16:47:01 -0700591 return si;
Sunny Goyal1b072632017-01-18 11:30:23 -0800592 });
593 }
594
Sunny Goyal10923b32016-07-20 15:42:44 -0700595 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -0800596 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -0700597 */
Sunny Goyal95899162019-03-27 16:03:06 -0700598 public void updateAndBindWorkspaceItem(final Supplier<WorkspaceItemInfo> itemProvider) {
Sunny Goyale86f11f2017-06-06 14:33:18 -0700599 enqueueModelUpdateTask(new BaseModelUpdateTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -0700600 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700601 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal95899162019-03-27 16:03:06 -0700602 WorkspaceItemInfo info = itemProvider.get();
Sunny Goyalf266deb2018-06-21 09:59:08 -0700603 getModelWriter().updateItemInDatabase(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700604 ArrayList<WorkspaceItemInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -0700605 update.add(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700606 bindUpdatedWorkspaceItems(update);
Sunny Goyal10923b32016-07-20 15:42:44 -0700607 }
608 });
609 }
610
Sunny Goyalc6e97692017-06-02 13:46:55 -0700611 public void refreshAndBindWidgetsAndShortcuts(@Nullable final PackageUserKey packageUser) {
Sunny Goyale86f11f2017-06-06 14:33:18 -0700612 enqueueModelUpdateTask(new BaseModelUpdateTask() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800613 @Override
Sunny Goyalc6e97692017-06-02 13:46:55 -0700614 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
615 dataModel.widgetsModel.update(app, packageUser);
616 bindUpdatedWidgets(dataModel);
Sunny Goyal31860582015-09-18 08:38:57 -0700617 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800618 });
Michael Jurkac402cd92013-05-20 15:49:32 +0200619 }
620
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -0800621 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
622 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
623 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
624 for (AppInfo info : mBgAllAppsList.data) {
Sunny Goyal3808a692019-10-25 13:41:28 -0700625 writer.println(prefix + " title=\"" + info.title
626 + "\" bitmapIcon=" + info.bitmap.icon
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -0800627 + " componentName=" + info.componentName.getPackageName());
628 }
Andy Wickhambb993c42021-02-26 12:56:25 -0800629 writer.println();
Joe Onorato36115782010-06-17 13:28:48 -0400630 }
Andy Wickhambb993c42021-02-26 12:56:25 -0800631 mModelDelegate.dump(prefix, fd, writer, args);
Sunny Goyal9ae9b602019-12-18 19:29:00 +0530632 mBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -0800633 }
Sunny Goyale0f58d72014-11-10 18:05:31 -0800634
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530635 /**
636 * Returns true if there are any callbacks attached to the model
637 */
638 public boolean hasCallbacks() {
639 synchronized (mCallbacksList) {
640 return !mCallbacksList.isEmpty();
641 }
642 }
643
644 /**
645 * Returns an array of currently attached callbacks
646 */
647 public Callbacks[] getCallbacks() {
648 synchronized (mCallbacksList) {
649 return mCallbacksList.toArray(new Callbacks[mCallbacksList.size()]);
650 }
Sunny Goyale0f58d72014-11-10 18:05:31 -0800651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652}