blob: 702b73afba8af5bf8cae86c7fdb4611c7d6b9bf7 [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
Sunny Goyalb47172b2021-05-03 19:59:51 -070019import static com.android.launcher3.Utilities.getDevicePrefs;
20import static com.android.launcher3.config.FeatureFlags.ENABLE_THEMED_ICONS;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070021import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Jon Mirandaec1277e2021-03-25 10:41:54 -040022import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI;
Sunny Goyald0e360a2018-06-29 14:40:18 -070023
Tonyd48710c2017-07-27 23:23:58 -070024import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070025import android.content.Context;
26import android.content.Intent;
Sunny Goyal01c32a62021-05-17 13:23:08 -070027import android.content.SharedPreferences;
28import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Sunny Goyale7b00122019-10-02 16:13:34 -070029import android.content.pm.LauncherApps;
Sunny Goyalb47172b2021-05-03 19:59:51 -070030import android.os.UserHandle;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040031import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080032
Tracy Zhoube13d102020-01-12 01:07:59 -080033import androidx.annotation.Nullable;
34
Sunny Goyal29947f02017-12-18 13:49:44 -080035import com.android.launcher3.config.FeatureFlags;
Sunny Goyalb47172b2021-05-03 19:59:51 -070036import com.android.launcher3.graphics.IconShape;
Sunny Goyalf840f102018-09-21 14:41:05 -070037import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070038import com.android.launcher3.icons.IconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070039import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070040import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080041import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070042import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080043import com.android.launcher3.pm.UserCache;
Sunny Goyald0e360a2018-06-29 14:40:18 -070044import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080045import com.android.launcher3.util.Preconditions;
Sunny Goyalb47172b2021-05-03 19:59:51 -070046import com.android.launcher3.util.RunnableList;
Sunny Goyal14168432019-10-24 15:59:49 -070047import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040048import com.android.launcher3.util.SettingsCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070049import com.android.launcher3.util.SimpleBroadcastReceiver;
Sunny Goyal01c32a62021-05-17 13:23:08 -070050import com.android.launcher3.util.Themes;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070051import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080052
Alex Chau7a4a30d2021-08-24 13:46:47 +000053public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050054
Sunny Goyal29947f02017-12-18 13:49:44 -080055 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
Sunny Goyalb47172b2021-05-03 19:59:51 -070056 private static final String KEY_ICON_STATE = "pref_icon_shape_path";
Sunny Goyal29947f02017-12-18 13:49:44 -080057
Sunny Goyalfdbef272017-02-01 12:52:54 -080058 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070059 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070060 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061
Sunny Goyalfdbef272017-02-01 12:52:54 -080062 private final Context mContext;
63 private final LauncherModel mModel;
Sunny Goyalb47172b2021-05-03 19:59:51 -070064 private final IconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080065 private final IconCache mIconCache;
Sunny Goyalfdbef272017-02-01 12:52:54 -080066 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyalb47172b2021-05-03 19:59:51 -070067 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070068
Sunny Goyalfdbef272017-02-01 12:52:54 -080069 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070070 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071 }
72
Chris Wrend8fe6de2013-10-04 10:42:14 -040073 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070074 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040075 }
76
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080078 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 }
80
Tracy Zhoube13d102020-01-12 01:07:59 -080081 public LauncherAppState(Context context) {
82 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyaldf4241c2021-05-19 19:42:14 -070083 Log.v(Launcher.TAG, "LauncherAppState initiated");
84 Preconditions.assertUIThread();
Sunny Goyal27595792015-03-19 13:18:44 -070085
Sunny Goyalb47172b2021-05-03 19:59:51 -070086 mInvariantDeviceProfile.addOnChangeListener(idp -> refreshAndReloadLauncher());
Daniel Sandlercc8befa2013-06-11 14:45:48 -040087
Sunny Goyale7b00122019-10-02 16:13:34 -070088 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
Sunny Goyalb47172b2021-05-03 19:59:51 -070089
90 SimpleBroadcastReceiver modelChangeReceiver =
91 new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
92 modelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
Sunny Goyale7b00122019-10-02 16:13:34 -070093 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
94 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
95 Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Zak Cohen3eeb41d2020-02-14 14:15:13 -080096 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyalb47172b2021-05-03 19:59:51 -070097 modelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -080098 }
Sunny Goyalb47172b2021-05-03 19:59:51 -070099 mOnTerminateCallback.add(() -> mContext.unregisterReceiver(modelChangeReceiver));
Sunny Goyal14168432019-10-24 15:59:49 -0700100
Sunny Goyal01615a62019-09-20 12:00:07 -0700101 CustomWidgetManager.INSTANCE.get(mContext)
102 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Sunny Goyal29947f02017-12-18 13:49:44 -0800103
Sunny Goyalb47172b2021-05-03 19:59:51 -0700104 SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext)
Sunny Goyal337c81f2019-12-10 12:19:13 -0800105 .addUserChangeListener(mModel::forceReload);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700106 mOnTerminateCallback.add(userChangeListener::close);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800107
Sunny Goyalb47172b2021-05-03 19:59:51 -0700108 IconObserver observer = new IconObserver();
109 SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener(
110 observer, MODEL_EXECUTOR.getHandler());
111 mOnTerminateCallback.add(iconChangeTracker::close);
112 MODEL_EXECUTOR.execute(observer::verifyIconChanged);
Sunny Goyal01c32a62021-05-17 13:23:08 -0700113 if (ENABLE_THEMED_ICONS.get()) {
114 SharedPreferences prefs = Utilities.getPrefs(mContext);
115 prefs.registerOnSharedPreferenceChangeListener(observer);
116 mOnTerminateCallback.add(
117 () -> prefs.unregisterOnSharedPreferenceChangeListener(observer));
118 }
Tony Wickham827cef22016-03-17 15:39:39 -0700119
Sunny Goyalb47172b2021-05-03 19:59:51 -0700120 InstallSessionTracker installSessionTracker =
121 InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(mModel);
122 mOnTerminateCallback.add(installSessionTracker::unregister);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700123
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700124 // Register an observer to rebind the notification listener when dots are re-enabled.
Sunny Goyalb47172b2021-05-03 19:59:51 -0700125 SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext);
126 SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged;
127 settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister);
128 onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI));
129 mOnTerminateCallback.add(() ->
130 settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister));
Sunny Goyal420d5452018-10-17 10:25:14 -0700131 }
132
Tracy Zhoube13d102020-01-12 01:07:59 -0800133 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
Tracy Zhoube13d102020-01-12 01:07:59 -0800134 mContext = context;
135
136 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal01c32a62021-05-17 13:23:08 -0700137 mIconProvider = new IconProvider(context, Themes.isThemedIconEnabled(context));
Sunny Goyalb47172b2021-05-03 19:59:51 -0700138 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
139 iconCacheFileName, mIconProvider);
Sunny Goyal44840152020-08-25 23:22:18 -0700140 mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext));
Sunny Goyaldf4241c2021-05-19 19:42:14 -0700141 mOnTerminateCallback.add(mIconCache::close);
Tracy Zhoube13d102020-01-12 01:07:59 -0800142 }
143
Sunny Goyalb47172b2021-05-03 19:59:51 -0700144 private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800145 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700146 NotificationListener.requestRebind(new ComponentName(
147 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700148 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400149 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100150
Sunny Goyalb47172b2021-05-03 19:59:51 -0700151 private void refreshAndReloadLauncher() {
152 LauncherIcons.clearPool();
153 mIconCache.updateIconParams(
154 mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700155 mModel.forceReload();
156 }
157
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400158 /**
159 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
160 */
Alex Chau7a4a30d2021-08-24 13:46:47 +0000161 public void onTerminate() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700162 mModel.destroy();
Sunny Goyale7b00122019-10-02 16:13:34 -0700163 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Sunny Goyal01615a62019-09-20 12:00:07 -0700164 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700165 mOnTerminateCallback.executeAllAndDestroy();
166 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700167
Sunny Goyalb47172b2021-05-03 19:59:51 -0700168 public IconProvider getIconProvider() {
169 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400170 }
171
Sunny Goyal34942622014-08-29 17:20:55 -0700172 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400173 return mIconCache;
174 }
175
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700176 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400177 return mModel;
178 }
179
Adam Cohen2e6da152015-05-06 11:42:25 -0700180 public InvariantDeviceProfile getInvariantDeviceProfile() {
181 return mInvariantDeviceProfile;
182 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800183
184 /**
185 * Shorthand for {@link #getInvariantDeviceProfile()}
186 */
187 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700188 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800189 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700190
Sunny Goyal01c32a62021-05-17 13:23:08 -0700191 private class IconObserver
192 implements IconProvider.IconChangeListener, OnSharedPreferenceChangeListener {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700193
194 @Override
195 public void onAppIconChanged(String packageName, UserHandle user) {
196 mModel.onAppIconChanged(packageName, user);
197 }
198
199 @Override
200 public void onSystemIconStateChanged(String iconState) {
201 IconShape.init(mContext);
202 refreshAndReloadLauncher();
203 getDevicePrefs(mContext).edit().putString(KEY_ICON_STATE, iconState).apply();
204 }
205
206 void verifyIconChanged() {
207 String iconState = mIconProvider.getSystemIconState();
208 if (!iconState.equals(getDevicePrefs(mContext).getString(KEY_ICON_STATE, ""))) {
209 onSystemIconStateChanged(iconState);
210 }
211 }
Sunny Goyal01c32a62021-05-17 13:23:08 -0700212
213 @Override
214 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
215 if (Themes.KEY_THEMED_ICONS.equals(key)) {
216 mIconProvider.setIconThemeSupported(Themes.isThemedIconEnabled(mContext));
217 verifyIconChanged();
218 }
219 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700220 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400221}