blob: 71013c3fa69123c96ca8ea7502f367c37eae9bf6 [file] [log] [blame]
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001/*
2 * Copyright (C) 2013 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
17package com.android.launcher3;
18
kholoud mohamedd5a4d602022-04-28 15:55:40 +010019import static android.app.admin.DevicePolicyManager.ACTION_DEVICE_POLICY_RESOURCE_UPDATED;
Sunny Goyalb02dafc2023-06-22 11:17:29 -070020import static android.content.Context.RECEIVER_EXPORTED;
kholoud mohamedd5a4d602022-04-28 15:55:40 +010021
fbaron9d08e0f2024-04-24 11:26:01 -070022import static com.android.launcher3.Flags.enableSmartspaceRemovalToggle;
fbaronc0e687b2023-10-04 12:11:20 -070023import static com.android.launcher3.model.LoaderTask.SMARTSPACE_ON_HOME_SCREEN;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070024import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Fengjiang Lib87ad6f2024-07-09 10:15:43 -070025import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
Jon Mirandaec1277e2021-03-25 10:41:54 -040026import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI;
Himanshu Gupta739b3c92023-11-06 17:47:29 +000027import static com.android.launcher3.util.SettingsCache.PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI;
Sunny Goyald0e360a2018-06-29 14:40:18 -070028
Tonyd48710c2017-07-27 23:23:58 -070029import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070030import android.content.Context;
Sunny Goyal01c32a62021-05-17 13:23:08 -070031import android.content.SharedPreferences;
32import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Sunny Goyale7b00122019-10-02 16:13:34 -070033import android.content.pm.LauncherApps;
Jakob Schneider94b5f172024-01-31 19:17:10 +000034import android.content.pm.LauncherApps.ArchiveCompatibilityParams;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040035import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080036
Tracy Zhoube13d102020-01-12 01:07:59 -080037import androidx.annotation.Nullable;
Anton Vayvodee382242024-05-28 21:26:25 -040038import androidx.core.os.BuildCompat;
Tracy Zhoube13d102020-01-12 01:07:59 -080039
Sunny Goyalc369d1e2024-12-31 00:00:15 -080040import com.android.launcher3.graphics.ThemeManager;
41import com.android.launcher3.graphics.ThemeManager.ThemeChangeListener;
Sunny Goyalf840f102018-09-21 14:41:05 -070042import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070043import com.android.launcher3.icons.IconProvider;
Sunny Goyal68af5492021-12-24 01:08:31 +053044import com.android.launcher3.icons.LauncherIconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070045import com.android.launcher3.icons.LauncherIcons;
Sunny Goyal6449a212024-01-12 09:40:43 -080046import com.android.launcher3.model.ModelLauncherCallbacks;
Shamali Pea078cb2024-11-04 21:35:38 +000047import com.android.launcher3.model.WidgetsFilterDataProvider;
Tonyd48710c2017-07-27 23:23:58 -070048import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080049import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070050import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080051import com.android.launcher3.pm.UserCache;
Stefan Andonian5bd9a222023-02-23 00:58:33 +000052import com.android.launcher3.util.LockedUserState;
Sunny Goyald0e360a2018-06-29 14:40:18 -070053import com.android.launcher3.util.MainThreadInitializedObject;
Winson Chung94e8ad02024-05-08 21:14:57 +000054import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalfdbef272017-02-01 12:52:54 -080055import com.android.launcher3.util.Preconditions;
Sunny Goyalb47172b2021-05-03 19:59:51 -070056import com.android.launcher3.util.RunnableList;
Sunny Goyal14168432019-10-24 15:59:49 -070057import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040058import com.android.launcher3.util.SettingsCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070059import com.android.launcher3.util.SimpleBroadcastReceiver;
Sunny Goyal31e27ed2024-01-09 15:45:14 -080060import com.android.launcher3.util.TraceHelper;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070061import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080062
Sunny Goyal777d4902021-08-27 21:22:17 +000063public class LauncherAppState implements SafeCloseable {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050064
Charlie Andersonbad2be42024-12-06 16:32:03 -050065 public static final String TAG = "LauncherAppState";
Sunny Goyal29947f02017-12-18 13:49:44 -080066 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
67
Sunny Goyalfdbef272017-02-01 12:52:54 -080068 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070069 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070070 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071
Sunny Goyalfdbef272017-02-01 12:52:54 -080072 private final Context mContext;
73 private final LauncherModel mModel;
Sunny Goyal68af5492021-12-24 01:08:31 +053074 private final LauncherIconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080075 private final IconCache mIconCache;
Sunny Goyalfdbef272017-02-01 12:52:54 -080076 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal31e27ed2024-01-09 15:45:14 -080077 private boolean mIsSafeModeEnabled;
78
Sunny Goyalb47172b2021-05-03 19:59:51 -070079 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070080
Sunny Goyal10fa0162024-04-21 00:13:35 -070081 public static LauncherAppState getInstance(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070082 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083 }
84
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080086 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040087 }
88
Jakob Schneider2f609d02024-01-10 18:03:35 +000089 @SuppressWarnings("NewApi")
Tracy Zhoube13d102020-01-12 01:07:59 -080090 public LauncherAppState(Context context) {
91 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyaldf4241c2021-05-19 19:42:14 -070092 Log.v(Launcher.TAG, "LauncherAppState initiated");
93 Preconditions.assertUIThread();
Sunny Goyal27595792015-03-19 13:18:44 -070094
Sunny Goyal31e27ed2024-01-09 15:45:14 -080095 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
96 () -> context.getPackageManager().isSafeMode());
Sunny Goyal6e6f7992021-08-24 16:23:29 -070097 mInvariantDeviceProfile.addOnChangeListener(modelPropertiesChanged -> {
98 if (modelPropertiesChanged) {
99 refreshAndReloadLauncher();
100 }
101 });
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102
Sunny Goyalc369d1e2024-12-31 00:00:15 -0800103 ThemeChangeListener themeChangeListener = this::refreshAndReloadLauncher;
104 ThemeManager.INSTANCE.get(context).addChangeListener(themeChangeListener);
105 mOnTerminateCallback.add(() ->
106 ThemeManager.INSTANCE.get(context).removeChangeListener(themeChangeListener));
107
Sunny Goyal6449a212024-01-12 09:40:43 -0800108 ModelLauncherCallbacks callbacks = mModel.newModelCallbacks();
Jakob Schneider2f609d02024-01-10 18:03:35 +0000109 LauncherApps launcherApps = mContext.getSystemService(LauncherApps.class);
110 launcherApps.registerCallback(callbacks);
Sunny Goyale274d972023-05-01 16:55:59 -0700111 mOnTerminateCallback.add(() ->
Sunny Goyal6449a212024-01-12 09:40:43 -0800112 mContext.getSystemService(LauncherApps.class).unregisterCallback(callbacks));
Jakob Schneider328c3222024-02-01 15:39:11 +0000113
Anton Vayvodee382242024-05-28 21:26:25 -0400114 if (BuildCompat.isAtLeastV() && Flags.enableSupportForArchiving()) {
Jakob Schneider94b5f172024-01-31 19:17:10 +0000115 ArchiveCompatibilityParams params = new ArchiveCompatibilityParams();
116 params.setEnableUnarchivalConfirmation(false);
Charlie Anderson6b8e3622024-08-01 15:19:27 -0400117 params.setEnableIconOverlay(!Flags.useNewIconForArchivedApps());
Jakob Schneider94b5f172024-01-31 19:17:10 +0000118 launcherApps.setArchiveCompatibility(params);
Jakob Schneider2f609d02024-01-10 18:03:35 +0000119 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700120
121 SimpleBroadcastReceiver modelChangeReceiver =
Adnan Begovic1b26bb42025-02-11 16:03:45 -0800122 new SimpleBroadcastReceiver(context, UI_HELPER_EXECUTOR, mModel::onBroadcastIntent);
Fengjiang Lib87ad6f2024-07-09 10:15:43 -0700123 modelChangeReceiver.register(
kholoud mohamedd5a4d602022-04-28 15:55:40 +0100124 ACTION_DEVICE_POLICY_RESOURCE_UPDATED);
Sunny Goyalb02dafc2023-06-22 11:17:29 -0700125 if (BuildConfig.IS_STUDIO_BUILD) {
Adnan Begovic1b26bb42025-02-11 16:03:45 -0800126 modelChangeReceiver.register(RECEIVER_EXPORTED, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -0800127 }
Adnan Begovic1b26bb42025-02-11 16:03:45 -0800128 mOnTerminateCallback.add(() -> modelChangeReceiver.unregisterReceiverSafely());
Sunny Goyal14168432019-10-24 15:59:49 -0700129
Sunny Goyalb47172b2021-05-03 19:59:51 -0700130 SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext)
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700131 .addUserEventListener(mModel::onUserEvent);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700132 mOnTerminateCallback.add(userChangeListener::close);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800133
fbaron9d08e0f2024-04-24 11:26:01 -0700134 if (enableSmartspaceRemovalToggle()) {
fbaronc0e687b2023-10-04 12:11:20 -0700135 OnSharedPreferenceChangeListener firstPagePinnedItemListener =
136 new OnSharedPreferenceChangeListener() {
137 @Override
138 public void onSharedPreferenceChanged(
139 SharedPreferences sharedPreferences, String key) {
140 if (SMARTSPACE_ON_HOME_SCREEN.equals(key)) {
141 mModel.forceReload();
142 }
143 }
144 };
145 LauncherPrefs.getPrefs(mContext).registerOnSharedPreferenceChangeListener(
146 firstPagePinnedItemListener);
147 mOnTerminateCallback.add(() -> LauncherPrefs.getPrefs(mContext)
148 .unregisterOnSharedPreferenceChangeListener(firstPagePinnedItemListener));
149 }
150
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000151 LockedUserState.get(context).runOnUserUnlocked(() -> {
Sunny Goyale274d972023-05-01 16:55:59 -0700152 CustomWidgetManager cwm = CustomWidgetManager.INSTANCE.get(mContext);
Sunny Goyal6bbd8052024-10-24 12:38:33 -0700153 mOnTerminateCallback.add(cwm.addWidgetRefreshCallback(mModel::rebindCallbacks)::close);
Tony Wickham827cef22016-03-17 15:39:39 -0700154
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000155 SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener(
Sunny Goyalc369d1e2024-12-31 00:00:15 -0800156 mModel::onAppIconChanged, MODEL_EXECUTOR.getHandler());
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000157 mOnTerminateCallback.add(iconChangeTracker::close);
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000158
159 InstallSessionTracker installSessionTracker =
Sunny Goyalc3632952024-10-09 15:15:42 -0700160 InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(callbacks);
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000161 mOnTerminateCallback.add(installSessionTracker::unregister);
162 });
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700163
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700164 // Register an observer to rebind the notification listener when dots are re-enabled.
Sunny Goyalb47172b2021-05-03 19:59:51 -0700165 SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext);
166 SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged;
167 settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister);
168 onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI));
169 mOnTerminateCallback.add(() ->
170 settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister));
Himanshu Gupta739b3c92023-11-06 17:47:29 +0000171 // Register an observer to notify Launcher about Private Space settings toggle.
Pechetty Sravani (xWF)41743bd2024-11-14 06:11:08 +0000172 registerPrivateSpaceHideWhenLockListener(settingsCache);
Sunny Goyal420d5452018-10-17 10:25:14 -0700173 }
174
Tracy Zhoube13d102020-01-12 01:07:59 -0800175 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
Tracy Zhoube13d102020-01-12 01:07:59 -0800176 mContext = context;
177
178 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal68af5492021-12-24 01:08:31 +0530179 mIconProvider = new LauncherIconProvider(context);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700180 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
181 iconCacheFileName, mIconProvider);
Shamali Pea078cb2024-11-04 21:35:38 +0000182 mModel = new LauncherModel(context, this, mIconCache,
183 WidgetsFilterDataProvider.Companion.newInstance(context), new AppFilter(mContext),
Winson Chung94e8ad02024-05-08 21:14:57 +0000184 PackageManagerHelper.INSTANCE.get(context), iconCacheFileName != null);
Sunny Goyaldf4241c2021-05-19 19:42:14 -0700185 mOnTerminateCallback.add(mIconCache::close);
Sunny Goyale274d972023-05-01 16:55:59 -0700186 mOnTerminateCallback.add(mModel::destroy);
Tracy Zhoube13d102020-01-12 01:07:59 -0800187 }
188
Sunny Goyalb47172b2021-05-03 19:59:51 -0700189 private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800190 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700191 NotificationListener.requestRebind(new ComponentName(
192 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700193 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400194 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100195
Himanshu Gupta739b3c92023-11-06 17:47:29 +0000196 private void registerPrivateSpaceHideWhenLockListener(SettingsCache settingsCache) {
197 SettingsCache.OnChangeListener psHideWhenLockChangedListener =
198 this::onPrivateSpaceHideWhenLockChanged;
199 settingsCache.register(PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI, psHideWhenLockChangedListener);
200 mOnTerminateCallback.add(() -> settingsCache.unregister(PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI,
201 psHideWhenLockChangedListener));
202 }
203
204 private void onPrivateSpaceHideWhenLockChanged(boolean isPrivateSpaceHideOnLockEnabled) {
205 mModel.forceReload();
206 }
207
Sunny Goyalb47172b2021-05-03 19:59:51 -0700208 private void refreshAndReloadLauncher() {
Sunny Goyalbf3efe82024-05-07 10:19:57 -0700209 LauncherIcons.clearPool(mContext);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700210 mIconCache.updateIconParams(
211 mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700212 mModel.forceReload();
213 }
214
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400215 /**
216 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
217 */
Sunny Goyal777d4902021-08-27 21:22:17 +0000218 @Override
219 public void close() {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700220 mOnTerminateCallback.executeAllAndDestroy();
221 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700222
Sunny Goyalb47172b2021-05-03 19:59:51 -0700223 public IconProvider getIconProvider() {
224 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400225 }
226
Sunny Goyal34942622014-08-29 17:20:55 -0700227 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400228 return mIconCache;
229 }
230
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700231 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400232 return mModel;
233 }
234
Adam Cohen2e6da152015-05-06 11:42:25 -0700235 public InvariantDeviceProfile getInvariantDeviceProfile() {
236 return mInvariantDeviceProfile;
237 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800238
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800239 public boolean isSafeModeEnabled() {
240 return mIsSafeModeEnabled;
241 }
242
Sunny Goyal87f784c2017-01-11 10:48:34 -0800243 /**
244 * Shorthand for {@link #getInvariantDeviceProfile()}
245 */
246 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700247 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800248 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400249}