blob: 05a3a524631a28de447e6aeea14478949b7a35db [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;
Stefan Andoniand1b33b32022-12-16 21:22:27 +000023import static com.android.launcher3.LauncherPrefs.ICON_STATE;
24import static com.android.launcher3.LauncherPrefs.THEMED_ICONS;
fbaronc0e687b2023-10-04 12:11:20 -070025import static com.android.launcher3.model.LoaderTask.SMARTSPACE_ON_HOME_SCREEN;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070026import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Fengjiang Lib87ad6f2024-07-09 10:15:43 -070027import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
Jon Mirandaec1277e2021-03-25 10:41:54 -040028import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI;
Himanshu Gupta739b3c92023-11-06 17:47:29 +000029import static com.android.launcher3.util.SettingsCache.PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI;
Sunny Goyald0e360a2018-06-29 14:40:18 -070030
Tonyd48710c2017-07-27 23:23:58 -070031import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070032import android.content.Context;
33import android.content.Intent;
Sunny Goyalb02dafc2023-06-22 11:17:29 -070034import android.content.IntentFilter;
Sunny Goyal01c32a62021-05-17 13:23:08 -070035import android.content.SharedPreferences;
36import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Sunny Goyale7b00122019-10-02 16:13:34 -070037import android.content.pm.LauncherApps;
Jakob Schneider94b5f172024-01-31 19:17:10 +000038import android.content.pm.LauncherApps.ArchiveCompatibilityParams;
Sunny Goyalb47172b2021-05-03 19:59:51 -070039import android.os.UserHandle;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040040import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080041
Tracy Zhoube13d102020-01-12 01:07:59 -080042import androidx.annotation.Nullable;
Anton Vayvodee382242024-05-28 21:26:25 -040043import androidx.core.os.BuildCompat;
Tracy Zhoube13d102020-01-12 01:07:59 -080044
Sunny Goyalb47172b2021-05-03 19:59:51 -070045import com.android.launcher3.graphics.IconShape;
Sunny Goyalf840f102018-09-21 14:41:05 -070046import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070047import com.android.launcher3.icons.IconProvider;
Sunny Goyal68af5492021-12-24 01:08:31 +053048import com.android.launcher3.icons.LauncherIconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070049import com.android.launcher3.icons.LauncherIcons;
Sunny Goyal6449a212024-01-12 09:40:43 -080050import com.android.launcher3.model.ModelLauncherCallbacks;
Tonyd48710c2017-07-27 23:23:58 -070051import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080052import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070053import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080054import com.android.launcher3.pm.UserCache;
Stefan Andonian5bd9a222023-02-23 00:58:33 +000055import com.android.launcher3.util.LockedUserState;
Sunny Goyald0e360a2018-06-29 14:40:18 -070056import com.android.launcher3.util.MainThreadInitializedObject;
Winson Chung94e8ad02024-05-08 21:14:57 +000057import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalfdbef272017-02-01 12:52:54 -080058import com.android.launcher3.util.Preconditions;
Sunny Goyalb47172b2021-05-03 19:59:51 -070059import com.android.launcher3.util.RunnableList;
Sunny Goyal14168432019-10-24 15:59:49 -070060import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040061import com.android.launcher3.util.SettingsCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070062import com.android.launcher3.util.SimpleBroadcastReceiver;
Sunny Goyal01c32a62021-05-17 13:23:08 -070063import com.android.launcher3.util.Themes;
Sunny Goyal31e27ed2024-01-09 15:45:14 -080064import com.android.launcher3.util.TraceHelper;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070065import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080066
Fengjiang Lib87ad6f2024-07-09 10:15:43 -070067import java.util.Locale;
68import java.util.Objects;
69
Sunny Goyal777d4902021-08-27 21:22:17 +000070public class LauncherAppState implements SafeCloseable {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050071
Sunny Goyal29947f02017-12-18 13:49:44 -080072 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
73
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070075 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070076 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077
Sunny Goyalfdbef272017-02-01 12:52:54 -080078 private final Context mContext;
79 private final LauncherModel mModel;
Sunny Goyal68af5492021-12-24 01:08:31 +053080 private final LauncherIconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080081 private final IconCache mIconCache;
Sunny Goyalfdbef272017-02-01 12:52:54 -080082 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal31e27ed2024-01-09 15:45:14 -080083 private boolean mIsSafeModeEnabled;
84
Sunny Goyalb47172b2021-05-03 19:59:51 -070085 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070086
Sunny Goyal10fa0162024-04-21 00:13:35 -070087 public static LauncherAppState getInstance(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070088 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040089 }
90
Daniel Sandlercc8befa2013-06-11 14:45:48 -040091 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080092 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040093 }
94
Jakob Schneider2f609d02024-01-10 18:03:35 +000095 @SuppressWarnings("NewApi")
Tracy Zhoube13d102020-01-12 01:07:59 -080096 public LauncherAppState(Context context) {
97 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyaldf4241c2021-05-19 19:42:14 -070098 Log.v(Launcher.TAG, "LauncherAppState initiated");
99 Preconditions.assertUIThread();
Sunny Goyal27595792015-03-19 13:18:44 -0700100
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800101 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
102 () -> context.getPackageManager().isSafeMode());
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700103 mInvariantDeviceProfile.addOnChangeListener(modelPropertiesChanged -> {
104 if (modelPropertiesChanged) {
105 refreshAndReloadLauncher();
106 }
107 });
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108
Sunny Goyal6449a212024-01-12 09:40:43 -0800109 ModelLauncherCallbacks callbacks = mModel.newModelCallbacks();
Jakob Schneider2f609d02024-01-10 18:03:35 +0000110 LauncherApps launcherApps = mContext.getSystemService(LauncherApps.class);
111 launcherApps.registerCallback(callbacks);
Sunny Goyale274d972023-05-01 16:55:59 -0700112 mOnTerminateCallback.add(() ->
Sunny Goyal6449a212024-01-12 09:40:43 -0800113 mContext.getSystemService(LauncherApps.class).unregisterCallback(callbacks));
Jakob Schneider328c3222024-02-01 15:39:11 +0000114
Anton Vayvodee382242024-05-28 21:26:25 -0400115 if (BuildCompat.isAtLeastV() && Flags.enableSupportForArchiving()) {
Jakob Schneider94b5f172024-01-31 19:17:10 +0000116 ArchiveCompatibilityParams params = new ArchiveCompatibilityParams();
117 params.setEnableUnarchivalConfirmation(false);
Charlie Anderson6b8e3622024-08-01 15:19:27 -0400118 params.setEnableIconOverlay(!Flags.useNewIconForArchivedApps());
Jakob Schneider94b5f172024-01-31 19:17:10 +0000119 launcherApps.setArchiveCompatibility(params);
Jakob Schneider2f609d02024-01-10 18:03:35 +0000120 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700121
122 SimpleBroadcastReceiver modelChangeReceiver =
Fengjiang Lib87ad6f2024-07-09 10:15:43 -0700123 new SimpleBroadcastReceiver(UI_HELPER_EXECUTOR, mModel::onBroadcastIntent);
124 final Locale oldLocale = mContext.getResources().getConfiguration().locale;
125 modelChangeReceiver.register(
126 mContext,
127 () -> {
128 // if local has changed before receiver is registered on bg thread,
129 // mModel needs to reload.
130 Locale newLocale = mContext.getResources().getConfiguration().locale;
131 if (!Objects.equals(oldLocale, newLocale)) {
132 mModel.forceReload();
133 }
134 },
135 Intent.ACTION_LOCALE_CHANGED,
kholoud mohamedd5a4d602022-04-28 15:55:40 +0100136 ACTION_DEVICE_POLICY_RESOURCE_UPDATED);
Sunny Goyalb02dafc2023-06-22 11:17:29 -0700137 if (BuildConfig.IS_STUDIO_BUILD) {
138 mContext.registerReceiver(modelChangeReceiver, new IntentFilter(ACTION_FORCE_ROLOAD),
139 RECEIVER_EXPORTED);
Sunny Goyal29947f02017-12-18 13:49:44 -0800140 }
Fengjiang Lib87ad6f2024-07-09 10:15:43 -0700141 mOnTerminateCallback.add(() -> modelChangeReceiver.unregisterReceiverSafely(mContext));
Sunny Goyal14168432019-10-24 15:59:49 -0700142
Sunny Goyalb47172b2021-05-03 19:59:51 -0700143 SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext)
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700144 .addUserEventListener(mModel::onUserEvent);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700145 mOnTerminateCallback.add(userChangeListener::close);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800146
fbaron9d08e0f2024-04-24 11:26:01 -0700147 if (enableSmartspaceRemovalToggle()) {
fbaronc0e687b2023-10-04 12:11:20 -0700148 OnSharedPreferenceChangeListener firstPagePinnedItemListener =
149 new OnSharedPreferenceChangeListener() {
150 @Override
151 public void onSharedPreferenceChanged(
152 SharedPreferences sharedPreferences, String key) {
153 if (SMARTSPACE_ON_HOME_SCREEN.equals(key)) {
154 mModel.forceReload();
155 }
156 }
157 };
158 LauncherPrefs.getPrefs(mContext).registerOnSharedPreferenceChangeListener(
159 firstPagePinnedItemListener);
160 mOnTerminateCallback.add(() -> LauncherPrefs.getPrefs(mContext)
161 .unregisterOnSharedPreferenceChangeListener(firstPagePinnedItemListener));
162 }
163
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000164 LockedUserState.get(context).runOnUserUnlocked(() -> {
Sunny Goyale274d972023-05-01 16:55:59 -0700165 CustomWidgetManager cwm = CustomWidgetManager.INSTANCE.get(mContext);
166 cwm.setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
167 mOnTerminateCallback.add(() -> cwm.setWidgetRefreshCallback(null));
Tony Wickham827cef22016-03-17 15:39:39 -0700168
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000169 IconObserver observer = new IconObserver();
170 SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener(
171 observer, MODEL_EXECUTOR.getHandler());
172 mOnTerminateCallback.add(iconChangeTracker::close);
173 MODEL_EXECUTOR.execute(observer::verifyIconChanged);
174 LauncherPrefs.get(context).addListener(observer, THEMED_ICONS);
175 mOnTerminateCallback.add(
176 () -> LauncherPrefs.get(mContext).removeListener(observer, THEMED_ICONS));
177
178 InstallSessionTracker installSessionTracker =
179 InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(mModel);
180 mOnTerminateCallback.add(installSessionTracker::unregister);
181 });
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700182
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700183 // Register an observer to rebind the notification listener when dots are re-enabled.
Sunny Goyalb47172b2021-05-03 19:59:51 -0700184 SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext);
185 SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged;
186 settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister);
187 onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI));
188 mOnTerminateCallback.add(() ->
189 settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister));
Himanshu Gupta739b3c92023-11-06 17:47:29 +0000190 // Register an observer to notify Launcher about Private Space settings toggle.
191 registerPrivateSpaceHideWhenLockListener(settingsCache);
Sunny Goyal420d5452018-10-17 10:25:14 -0700192 }
193
Tracy Zhoube13d102020-01-12 01:07:59 -0800194 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
Tracy Zhoube13d102020-01-12 01:07:59 -0800195 mContext = context;
196
197 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal68af5492021-12-24 01:08:31 +0530198 mIconProvider = new LauncherIconProvider(context);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700199 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
200 iconCacheFileName, mIconProvider);
Thiru Ramasamyd495e8c2021-08-26 10:37:17 -0700201 mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext),
Winson Chung94e8ad02024-05-08 21:14:57 +0000202 PackageManagerHelper.INSTANCE.get(context), iconCacheFileName != null);
Sunny Goyaldf4241c2021-05-19 19:42:14 -0700203 mOnTerminateCallback.add(mIconCache::close);
Sunny Goyale274d972023-05-01 16:55:59 -0700204 mOnTerminateCallback.add(mModel::destroy);
Tracy Zhoube13d102020-01-12 01:07:59 -0800205 }
206
Sunny Goyalb47172b2021-05-03 19:59:51 -0700207 private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800208 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700209 NotificationListener.requestRebind(new ComponentName(
210 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700211 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400212 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100213
Himanshu Gupta739b3c92023-11-06 17:47:29 +0000214 private void registerPrivateSpaceHideWhenLockListener(SettingsCache settingsCache) {
215 SettingsCache.OnChangeListener psHideWhenLockChangedListener =
216 this::onPrivateSpaceHideWhenLockChanged;
217 settingsCache.register(PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI, psHideWhenLockChangedListener);
218 mOnTerminateCallback.add(() -> settingsCache.unregister(PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI,
219 psHideWhenLockChangedListener));
220 }
221
222 private void onPrivateSpaceHideWhenLockChanged(boolean isPrivateSpaceHideOnLockEnabled) {
223 mModel.forceReload();
224 }
225
Sunny Goyalb47172b2021-05-03 19:59:51 -0700226 private void refreshAndReloadLauncher() {
Sunny Goyalbf3efe82024-05-07 10:19:57 -0700227 LauncherIcons.clearPool(mContext);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700228 mIconCache.updateIconParams(
229 mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700230 mModel.forceReload();
231 }
232
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400233 /**
234 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
235 */
Sunny Goyal777d4902021-08-27 21:22:17 +0000236 @Override
237 public void close() {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700238 mOnTerminateCallback.executeAllAndDestroy();
239 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700240
Sunny Goyalb47172b2021-05-03 19:59:51 -0700241 public IconProvider getIconProvider() {
242 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400243 }
244
Sunny Goyal34942622014-08-29 17:20:55 -0700245 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400246 return mIconCache;
247 }
248
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700249 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400250 return mModel;
251 }
252
Adam Cohen2e6da152015-05-06 11:42:25 -0700253 public InvariantDeviceProfile getInvariantDeviceProfile() {
254 return mInvariantDeviceProfile;
255 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800256
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800257 public boolean isSafeModeEnabled() {
258 return mIsSafeModeEnabled;
259 }
260
Sunny Goyal87f784c2017-01-11 10:48:34 -0800261 /**
262 * Shorthand for {@link #getInvariantDeviceProfile()}
263 */
264 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700265 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800266 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700267
Sunny Goyal01c32a62021-05-17 13:23:08 -0700268 private class IconObserver
Brian Isganitis9cbc4782024-10-08 14:47:17 -0400269 implements IconProvider.IconChangeListener, LauncherPrefChangeListener {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700270
271 @Override
272 public void onAppIconChanged(String packageName, UserHandle user) {
273 mModel.onAppIconChanged(packageName, user);
274 }
275
276 @Override
277 public void onSystemIconStateChanged(String iconState) {
Sunny Goyal638a6872024-04-25 15:55:11 -0700278 IconShape.INSTANCE.get(mContext).pickBestShape(mContext);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700279 refreshAndReloadLauncher();
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000280 LauncherPrefs.get(mContext).put(ICON_STATE, iconState);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700281 }
282
283 void verifyIconChanged() {
284 String iconState = mIconProvider.getSystemIconState();
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000285 if (!iconState.equals(LauncherPrefs.get(mContext).get(ICON_STATE))) {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700286 onSystemIconStateChanged(iconState);
287 }
288 }
Sunny Goyal01c32a62021-05-17 13:23:08 -0700289
290 @Override
Brian Isganitis9cbc4782024-10-08 14:47:17 -0400291 public void onPrefChanged(String key) {
Sunny Goyal01c32a62021-05-17 13:23:08 -0700292 if (Themes.KEY_THEMED_ICONS.equals(key)) {
293 mIconProvider.setIconThemeSupported(Themes.isThemedIconEnabled(mContext));
294 verifyIconChanged();
295 }
296 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700297 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400298}