blob: ea3f723e29a7981f12127471ce99c92ea3f02fb1 [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;
20
Sunny Goyalb47172b2021-05-03 19:59:51 -070021import static com.android.launcher3.Utilities.getDevicePrefs;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070022import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Jon Mirandaec1277e2021-03-25 10:41:54 -040023import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI;
Sunny Goyald0e360a2018-06-29 14:40:18 -070024
Tonyd48710c2017-07-27 23:23:58 -070025import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070026import android.content.Context;
27import android.content.Intent;
Sunny Goyal01c32a62021-05-17 13:23:08 -070028import android.content.SharedPreferences;
29import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Sunny Goyale7b00122019-10-02 16:13:34 -070030import android.content.pm.LauncherApps;
Sunny Goyalb47172b2021-05-03 19:59:51 -070031import android.os.UserHandle;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040032import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080033
Tracy Zhoube13d102020-01-12 01:07:59 -080034import androidx.annotation.Nullable;
35
Sunny Goyal29947f02017-12-18 13:49:44 -080036import com.android.launcher3.config.FeatureFlags;
Sunny Goyalb47172b2021-05-03 19:59:51 -070037import com.android.launcher3.graphics.IconShape;
Sunny Goyalf840f102018-09-21 14:41:05 -070038import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070039import com.android.launcher3.icons.IconProvider;
Sunny Goyal68af5492021-12-24 01:08:31 +053040import com.android.launcher3.icons.LauncherIconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070041import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070042import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080043import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070044import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080045import com.android.launcher3.pm.UserCache;
Sunny Goyald0e360a2018-06-29 14:40:18 -070046import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080047import com.android.launcher3.util.Preconditions;
Sunny Goyalb47172b2021-05-03 19:59:51 -070048import com.android.launcher3.util.RunnableList;
Sunny Goyal14168432019-10-24 15:59:49 -070049import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040050import com.android.launcher3.util.SettingsCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070051import com.android.launcher3.util.SimpleBroadcastReceiver;
Sunny Goyal01c32a62021-05-17 13:23:08 -070052import com.android.launcher3.util.Themes;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070053import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080054
Sunny Goyal777d4902021-08-27 21:22:17 +000055public class LauncherAppState implements SafeCloseable {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050056
Sunny Goyal29947f02017-12-18 13:49:44 -080057 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
Sunny Goyalb47172b2021-05-03 19:59:51 -070058 private static final String KEY_ICON_STATE = "pref_icon_shape_path";
Sunny Goyal29947f02017-12-18 13:49:44 -080059
Sunny Goyalfdbef272017-02-01 12:52:54 -080060 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070061 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070062 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063
Sunny Goyalfdbef272017-02-01 12:52:54 -080064 private final Context mContext;
65 private final LauncherModel mModel;
Sunny Goyal68af5492021-12-24 01:08:31 +053066 private final LauncherIconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080067 private final IconCache mIconCache;
Sunny Goyalfdbef272017-02-01 12:52:54 -080068 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyalb47172b2021-05-03 19:59:51 -070069 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070070
Sunny Goyalfdbef272017-02-01 12:52:54 -080071 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070072 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 }
74
Chris Wrend8fe6de2013-10-04 10:42:14 -040075 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070076 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040077 }
78
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080080 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081 }
82
Tracy Zhoube13d102020-01-12 01:07:59 -080083 public LauncherAppState(Context context) {
84 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyaldf4241c2021-05-19 19:42:14 -070085 Log.v(Launcher.TAG, "LauncherAppState initiated");
86 Preconditions.assertUIThread();
Sunny Goyal27595792015-03-19 13:18:44 -070087
Sunny Goyal6e6f7992021-08-24 16:23:29 -070088 mInvariantDeviceProfile.addOnChangeListener(modelPropertiesChanged -> {
89 if (modelPropertiesChanged) {
90 refreshAndReloadLauncher();
91 }
92 });
Daniel Sandlercc8befa2013-06-11 14:45:48 -040093
Sunny Goyale7b00122019-10-02 16:13:34 -070094 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
Sunny Goyalb47172b2021-05-03 19:59:51 -070095
96 SimpleBroadcastReceiver modelChangeReceiver =
97 new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
98 modelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
Sunny Goyale7b00122019-10-02 16:13:34 -070099 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
100 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
kholoud mohamedd5a4d602022-04-28 15:55:40 +0100101 Intent.ACTION_MANAGED_PROFILE_UNLOCKED,
102 ACTION_DEVICE_POLICY_RESOURCE_UPDATED);
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800103 if (FeatureFlags.IS_STUDIO_BUILD) {
Michael Groover871467b2021-12-07 17:43:31 -0800104 modelChangeReceiver.register(mContext, Context.RECEIVER_EXPORTED, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -0800105 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700106 mOnTerminateCallback.add(() -> mContext.unregisterReceiver(modelChangeReceiver));
Sunny Goyal14168432019-10-24 15:59:49 -0700107
Sunny Goyal01615a62019-09-20 12:00:07 -0700108 CustomWidgetManager.INSTANCE.get(mContext)
109 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Sunny Goyal29947f02017-12-18 13:49:44 -0800110
Sunny Goyalb47172b2021-05-03 19:59:51 -0700111 SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext)
Sunny Goyal337c81f2019-12-10 12:19:13 -0800112 .addUserChangeListener(mModel::forceReload);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700113 mOnTerminateCallback.add(userChangeListener::close);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800114
Sunny Goyalb47172b2021-05-03 19:59:51 -0700115 IconObserver observer = new IconObserver();
116 SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener(
117 observer, MODEL_EXECUTOR.getHandler());
118 mOnTerminateCallback.add(iconChangeTracker::close);
119 MODEL_EXECUTOR.execute(observer::verifyIconChanged);
Sunny Goyalc5c7da92022-08-02 13:36:54 -0700120 SharedPreferences prefs = Utilities.getPrefs(mContext);
121 prefs.registerOnSharedPreferenceChangeListener(observer);
122 mOnTerminateCallback.add(
123 () -> prefs.unregisterOnSharedPreferenceChangeListener(observer));
Tony Wickham827cef22016-03-17 15:39:39 -0700124
Sunny Goyalb47172b2021-05-03 19:59:51 -0700125 InstallSessionTracker installSessionTracker =
126 InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(mModel);
127 mOnTerminateCallback.add(installSessionTracker::unregister);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700128
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700129 // Register an observer to rebind the notification listener when dots are re-enabled.
Sunny Goyalb47172b2021-05-03 19:59:51 -0700130 SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext);
131 SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged;
132 settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister);
133 onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI));
134 mOnTerminateCallback.add(() ->
135 settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister));
Sunny Goyal420d5452018-10-17 10:25:14 -0700136 }
137
Tracy Zhoube13d102020-01-12 01:07:59 -0800138 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
Tracy Zhoube13d102020-01-12 01:07:59 -0800139 mContext = context;
140
141 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal68af5492021-12-24 01:08:31 +0530142 mIconProvider = new LauncherIconProvider(context);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700143 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
144 iconCacheFileName, mIconProvider);
Thiru Ramasamyd495e8c2021-08-26 10:37:17 -0700145 mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext),
146 iconCacheFileName != null);
Sunny Goyaldf4241c2021-05-19 19:42:14 -0700147 mOnTerminateCallback.add(mIconCache::close);
Tracy Zhoube13d102020-01-12 01:07:59 -0800148 }
149
Sunny Goyalb47172b2021-05-03 19:59:51 -0700150 private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800151 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700152 NotificationListener.requestRebind(new ComponentName(
153 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700154 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100156
Sunny Goyalb47172b2021-05-03 19:59:51 -0700157 private void refreshAndReloadLauncher() {
158 LauncherIcons.clearPool();
159 mIconCache.updateIconParams(
160 mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700161 mModel.forceReload();
162 }
163
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 /**
165 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
166 */
Sunny Goyal777d4902021-08-27 21:22:17 +0000167 @Override
168 public void close() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700169 mModel.destroy();
Sunny Goyale7b00122019-10-02 16:13:34 -0700170 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Sunny Goyal01615a62019-09-20 12:00:07 -0700171 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700172 mOnTerminateCallback.executeAllAndDestroy();
173 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700174
Sunny Goyalb47172b2021-05-03 19:59:51 -0700175 public IconProvider getIconProvider() {
176 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400177 }
178
Sunny Goyal34942622014-08-29 17:20:55 -0700179 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400180 return mIconCache;
181 }
182
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700183 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400184 return mModel;
185 }
186
Adam Cohen2e6da152015-05-06 11:42:25 -0700187 public InvariantDeviceProfile getInvariantDeviceProfile() {
188 return mInvariantDeviceProfile;
189 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800190
191 /**
192 * Shorthand for {@link #getInvariantDeviceProfile()}
193 */
194 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700195 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800196 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700197
Sunny Goyal01c32a62021-05-17 13:23:08 -0700198 private class IconObserver
199 implements IconProvider.IconChangeListener, OnSharedPreferenceChangeListener {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700200
201 @Override
202 public void onAppIconChanged(String packageName, UserHandle user) {
203 mModel.onAppIconChanged(packageName, user);
204 }
205
206 @Override
207 public void onSystemIconStateChanged(String iconState) {
208 IconShape.init(mContext);
209 refreshAndReloadLauncher();
210 getDevicePrefs(mContext).edit().putString(KEY_ICON_STATE, iconState).apply();
211 }
212
213 void verifyIconChanged() {
214 String iconState = mIconProvider.getSystemIconState();
215 if (!iconState.equals(getDevicePrefs(mContext).getString(KEY_ICON_STATE, ""))) {
216 onSystemIconStateChanged(iconState);
217 }
218 }
Sunny Goyal01c32a62021-05-17 13:23:08 -0700219
220 @Override
221 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
222 if (Themes.KEY_THEMED_ICONS.equals(key)) {
223 mIconProvider.setIconThemeSupported(Themes.isThemedIconEnabled(mContext));
224 verifyIconChanged();
225 }
226 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700227 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400228}