blob: 5ae2d71a39e25e6c38d6d19817289c647a64c831 [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
Stefan Andoniand1b33b32022-12-16 21:22:27 +000022import static com.android.launcher3.LauncherPrefs.ICON_STATE;
23import static com.android.launcher3.LauncherPrefs.THEMED_ICONS;
fbaronc0e687b2023-10-04 12:11:20 -070024import static com.android.launcher3.config.FeatureFlags.ENABLE_SMARTSPACE_REMOVAL;
25import 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;
Jon Mirandaec1277e2021-03-25 10:41:54 -040027import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI;
Himanshu Gupta739b3c92023-11-06 17:47:29 +000028import static com.android.launcher3.util.SettingsCache.PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI;
Sunny Goyald0e360a2018-06-29 14:40:18 -070029
Tonyd48710c2017-07-27 23:23:58 -070030import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070031import android.content.Context;
32import android.content.Intent;
Sunny Goyalb02dafc2023-06-22 11:17:29 -070033import android.content.IntentFilter;
Sunny Goyal01c32a62021-05-17 13:23:08 -070034import android.content.SharedPreferences;
35import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Sunny Goyale7b00122019-10-02 16:13:34 -070036import android.content.pm.LauncherApps;
Sunny Goyalb47172b2021-05-03 19:59:51 -070037import android.os.UserHandle;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040038import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080039
Tracy Zhoube13d102020-01-12 01:07:59 -080040import androidx.annotation.Nullable;
41
Sunny Goyalb47172b2021-05-03 19:59:51 -070042import com.android.launcher3.graphics.IconShape;
Sunny Goyalf840f102018-09-21 14:41:05 -070043import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070044import com.android.launcher3.icons.IconProvider;
Sunny Goyal68af5492021-12-24 01:08:31 +053045import com.android.launcher3.icons.LauncherIconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070046import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070047import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080048import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070049import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080050import com.android.launcher3.pm.UserCache;
Stefan Andonian5bd9a222023-02-23 00:58:33 +000051import com.android.launcher3.util.LockedUserState;
Sunny Goyald0e360a2018-06-29 14:40:18 -070052import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080053import com.android.launcher3.util.Preconditions;
Sunny Goyalb47172b2021-05-03 19:59:51 -070054import com.android.launcher3.util.RunnableList;
Sunny Goyal14168432019-10-24 15:59:49 -070055import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040056import com.android.launcher3.util.SettingsCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070057import com.android.launcher3.util.SimpleBroadcastReceiver;
Sunny Goyal01c32a62021-05-17 13:23:08 -070058import com.android.launcher3.util.Themes;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070059import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080060
Sunny Goyal777d4902021-08-27 21:22:17 +000061public class LauncherAppState implements SafeCloseable {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050062
Sunny Goyal29947f02017-12-18 13:49:44 -080063 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
64
Sunny Goyalfdbef272017-02-01 12:52:54 -080065 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070066 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070067 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040068
Sunny Goyalfdbef272017-02-01 12:52:54 -080069 private final Context mContext;
70 private final LauncherModel mModel;
Sunny Goyal68af5492021-12-24 01:08:31 +053071 private final LauncherIconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080072 private final IconCache mIconCache;
Sunny Goyalfdbef272017-02-01 12:52:54 -080073 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyalb47172b2021-05-03 19:59:51 -070074 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070075
Sunny Goyalfdbef272017-02-01 12:52:54 -080076 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070077 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078 }
79
Chris Wrend8fe6de2013-10-04 10:42:14 -040080 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070081 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040082 }
83
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080085 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086 }
87
Tracy Zhoube13d102020-01-12 01:07:59 -080088 public LauncherAppState(Context context) {
89 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyaldf4241c2021-05-19 19:42:14 -070090 Log.v(Launcher.TAG, "LauncherAppState initiated");
91 Preconditions.assertUIThread();
Sunny Goyal27595792015-03-19 13:18:44 -070092
Sunny Goyal6e6f7992021-08-24 16:23:29 -070093 mInvariantDeviceProfile.addOnChangeListener(modelPropertiesChanged -> {
94 if (modelPropertiesChanged) {
95 refreshAndReloadLauncher();
96 }
97 });
Daniel Sandlercc8befa2013-06-11 14:45:48 -040098
Sunny Goyale7b00122019-10-02 16:13:34 -070099 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
Sunny Goyale274d972023-05-01 16:55:59 -0700100 mOnTerminateCallback.add(() ->
101 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel));
Sunny Goyalb47172b2021-05-03 19:59:51 -0700102
103 SimpleBroadcastReceiver modelChangeReceiver =
104 new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
105 modelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
kholoud mohamedd5a4d602022-04-28 15:55:40 +0100106 ACTION_DEVICE_POLICY_RESOURCE_UPDATED);
Sunny Goyalb02dafc2023-06-22 11:17:29 -0700107 if (BuildConfig.IS_STUDIO_BUILD) {
108 mContext.registerReceiver(modelChangeReceiver, new IntentFilter(ACTION_FORCE_ROLOAD),
109 RECEIVER_EXPORTED);
Sunny Goyal29947f02017-12-18 13:49:44 -0800110 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700111 mOnTerminateCallback.add(() -> mContext.unregisterReceiver(modelChangeReceiver));
Sunny Goyal14168432019-10-24 15:59:49 -0700112
Sunny Goyalb47172b2021-05-03 19:59:51 -0700113 SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext)
Sunny Goyalc80e60d2023-06-07 12:52:30 -0700114 .addUserEventListener(mModel::onUserEvent);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700115 mOnTerminateCallback.add(userChangeListener::close);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800116
fbaronc0e687b2023-10-04 12:11:20 -0700117 if (ENABLE_SMARTSPACE_REMOVAL.get()) {
118 OnSharedPreferenceChangeListener firstPagePinnedItemListener =
119 new OnSharedPreferenceChangeListener() {
120 @Override
121 public void onSharedPreferenceChanged(
122 SharedPreferences sharedPreferences, String key) {
123 if (SMARTSPACE_ON_HOME_SCREEN.equals(key)) {
124 mModel.forceReload();
125 }
126 }
127 };
128 LauncherPrefs.getPrefs(mContext).registerOnSharedPreferenceChangeListener(
129 firstPagePinnedItemListener);
130 mOnTerminateCallback.add(() -> LauncherPrefs.getPrefs(mContext)
131 .unregisterOnSharedPreferenceChangeListener(firstPagePinnedItemListener));
132 }
133
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000134 LockedUserState.get(context).runOnUserUnlocked(() -> {
Sunny Goyale274d972023-05-01 16:55:59 -0700135 CustomWidgetManager cwm = CustomWidgetManager.INSTANCE.get(mContext);
136 cwm.setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
137 mOnTerminateCallback.add(() -> cwm.setWidgetRefreshCallback(null));
Tony Wickham827cef22016-03-17 15:39:39 -0700138
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000139 IconObserver observer = new IconObserver();
140 SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener(
141 observer, MODEL_EXECUTOR.getHandler());
142 mOnTerminateCallback.add(iconChangeTracker::close);
143 MODEL_EXECUTOR.execute(observer::verifyIconChanged);
144 LauncherPrefs.get(context).addListener(observer, THEMED_ICONS);
145 mOnTerminateCallback.add(
146 () -> LauncherPrefs.get(mContext).removeListener(observer, THEMED_ICONS));
147
148 InstallSessionTracker installSessionTracker =
149 InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(mModel);
150 mOnTerminateCallback.add(installSessionTracker::unregister);
151 });
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700152
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700153 // Register an observer to rebind the notification listener when dots are re-enabled.
Sunny Goyalb47172b2021-05-03 19:59:51 -0700154 SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext);
155 SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged;
156 settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister);
157 onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI));
158 mOnTerminateCallback.add(() ->
159 settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister));
Himanshu Gupta739b3c92023-11-06 17:47:29 +0000160 // Register an observer to notify Launcher about Private Space settings toggle.
161 registerPrivateSpaceHideWhenLockListener(settingsCache);
Sunny Goyal420d5452018-10-17 10:25:14 -0700162 }
163
Tracy Zhoube13d102020-01-12 01:07:59 -0800164 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
Tracy Zhoube13d102020-01-12 01:07:59 -0800165 mContext = context;
166
167 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal68af5492021-12-24 01:08:31 +0530168 mIconProvider = new LauncherIconProvider(context);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700169 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
170 iconCacheFileName, mIconProvider);
Thiru Ramasamyd495e8c2021-08-26 10:37:17 -0700171 mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext),
172 iconCacheFileName != null);
Sunny Goyaldf4241c2021-05-19 19:42:14 -0700173 mOnTerminateCallback.add(mIconCache::close);
Sunny Goyale274d972023-05-01 16:55:59 -0700174 mOnTerminateCallback.add(mModel::destroy);
Tracy Zhoube13d102020-01-12 01:07:59 -0800175 }
176
Sunny Goyalb47172b2021-05-03 19:59:51 -0700177 private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800178 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700179 NotificationListener.requestRebind(new ComponentName(
180 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700181 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400182 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100183
Himanshu Gupta739b3c92023-11-06 17:47:29 +0000184 private void registerPrivateSpaceHideWhenLockListener(SettingsCache settingsCache) {
185 SettingsCache.OnChangeListener psHideWhenLockChangedListener =
186 this::onPrivateSpaceHideWhenLockChanged;
187 settingsCache.register(PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI, psHideWhenLockChangedListener);
188 mOnTerminateCallback.add(() -> settingsCache.unregister(PRIVATE_SPACE_HIDE_WHEN_LOCKED_URI,
189 psHideWhenLockChangedListener));
190 }
191
192 private void onPrivateSpaceHideWhenLockChanged(boolean isPrivateSpaceHideOnLockEnabled) {
193 mModel.forceReload();
194 }
195
Sunny Goyalb47172b2021-05-03 19:59:51 -0700196 private void refreshAndReloadLauncher() {
197 LauncherIcons.clearPool();
198 mIconCache.updateIconParams(
199 mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700200 mModel.forceReload();
201 }
202
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400203 /**
204 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
205 */
Sunny Goyal777d4902021-08-27 21:22:17 +0000206 @Override
207 public void close() {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700208 mOnTerminateCallback.executeAllAndDestroy();
209 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700210
Sunny Goyalb47172b2021-05-03 19:59:51 -0700211 public IconProvider getIconProvider() {
212 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400213 }
214
Sunny Goyal34942622014-08-29 17:20:55 -0700215 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400216 return mIconCache;
217 }
218
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700219 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400220 return mModel;
221 }
222
Adam Cohen2e6da152015-05-06 11:42:25 -0700223 public InvariantDeviceProfile getInvariantDeviceProfile() {
224 return mInvariantDeviceProfile;
225 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800226
227 /**
228 * Shorthand for {@link #getInvariantDeviceProfile()}
229 */
230 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700231 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800232 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700233
Sunny Goyal01c32a62021-05-17 13:23:08 -0700234 private class IconObserver
235 implements IconProvider.IconChangeListener, OnSharedPreferenceChangeListener {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700236
237 @Override
238 public void onAppIconChanged(String packageName, UserHandle user) {
239 mModel.onAppIconChanged(packageName, user);
240 }
241
242 @Override
243 public void onSystemIconStateChanged(String iconState) {
244 IconShape.init(mContext);
245 refreshAndReloadLauncher();
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000246 LauncherPrefs.get(mContext).put(ICON_STATE, iconState);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700247 }
248
249 void verifyIconChanged() {
250 String iconState = mIconProvider.getSystemIconState();
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000251 if (!iconState.equals(LauncherPrefs.get(mContext).get(ICON_STATE))) {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700252 onSystemIconStateChanged(iconState);
253 }
254 }
Sunny Goyal01c32a62021-05-17 13:23:08 -0700255
256 @Override
257 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
258 if (Themes.KEY_THEMED_ICONS.equals(key)) {
259 mIconProvider.setIconThemeSupported(Themes.isThemedIconEnabled(mContext));
260 verifyIconChanged();
261 }
262 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700263 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400264}