blob: 4d15ac74521221a6b2a5f88af7f6bb550de0c49a [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
Stefan Andoniand1b33b32022-12-16 21:22:27 +000021import static com.android.launcher3.LauncherPrefs.ICON_STATE;
22import static com.android.launcher3.LauncherPrefs.THEMED_ICONS;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070023import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Jon Mirandaec1277e2021-03-25 10:41:54 -040024import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI;
Sunny Goyald0e360a2018-06-29 14:40:18 -070025
Tonyd48710c2017-07-27 23:23:58 -070026import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070027import android.content.Context;
28import android.content.Intent;
Sunny Goyal01c32a62021-05-17 13:23:08 -070029import android.content.SharedPreferences;
30import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Sunny Goyale7b00122019-10-02 16:13:34 -070031import android.content.pm.LauncherApps;
Sunny Goyalb47172b2021-05-03 19:59:51 -070032import android.os.UserHandle;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040033import android.util.Log;
Pinyao Tingbd638e72023-03-17 16:33:24 -070034import android.util.SparseArray;
35import android.widget.RemoteViews;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080036
Pinyao Tingbd638e72023-03-17 16:33:24 -070037import androidx.annotation.GuardedBy;
38import androidx.annotation.NonNull;
Tracy Zhoube13d102020-01-12 01:07:59 -080039import androidx.annotation.Nullable;
40
Sunny Goyal29947f02017-12-18 13:49:44 -080041import com.android.launcher3.config.FeatureFlags;
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";
Stefan Andoniand1b33b32022-12-16 21:22:27 +000064 public static final String KEY_ICON_STATE = "pref_icon_shape_path";
Sunny Goyal29947f02017-12-18 13:49:44 -080065
Sunny Goyalfdbef272017-02-01 12:52:54 -080066 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070067 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070068 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040069
Sunny Goyalfdbef272017-02-01 12:52:54 -080070 private final Context mContext;
71 private final LauncherModel mModel;
Sunny Goyal68af5492021-12-24 01:08:31 +053072 private final LauncherIconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080073 private final IconCache mIconCache;
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyalb47172b2021-05-03 19:59:51 -070075 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070076
Pinyao Tingbd638e72023-03-17 16:33:24 -070077 // WORKAROUND: b/269335387 remove this after widget background listener is enabled
78 /* Array of RemoteViews cached by Launcher process */
79 @GuardedBy("itself")
80 @NonNull
81 public final SparseArray<RemoteViews> mCachedRemoteViews = new SparseArray<>();
82
Sunny Goyalfdbef272017-02-01 12:52:54 -080083 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070084 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085 }
86
Chris Wrend8fe6de2013-10-04 10:42:14 -040087 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070088 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -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
Tracy Zhoube13d102020-01-12 01:07:59 -080095 public LauncherAppState(Context context) {
96 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyaldf4241c2021-05-19 19:42:14 -070097 Log.v(Launcher.TAG, "LauncherAppState initiated");
98 Preconditions.assertUIThread();
Sunny Goyal27595792015-03-19 13:18:44 -070099
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700100 mInvariantDeviceProfile.addOnChangeListener(modelPropertiesChanged -> {
101 if (modelPropertiesChanged) {
102 refreshAndReloadLauncher();
103 }
104 });
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400105
Sunny Goyale7b00122019-10-02 16:13:34 -0700106 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700107
108 SimpleBroadcastReceiver modelChangeReceiver =
109 new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
110 modelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
Sunny Goyale7b00122019-10-02 16:13:34 -0700111 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
112 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
kholoud mohamedd5a4d602022-04-28 15:55:40 +0100113 Intent.ACTION_MANAGED_PROFILE_UNLOCKED,
Pavel Grafova3d37c82023-05-11 15:35:56 +0100114 Intent.ACTION_PROFILE_INACCESSIBLE,
kholoud mohamedd5a4d602022-04-28 15:55:40 +0100115 ACTION_DEVICE_POLICY_RESOURCE_UPDATED);
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800116 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyala992ac92023-01-10 17:50:32 -0800117 modelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -0800118 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700119 mOnTerminateCallback.add(() -> mContext.unregisterReceiver(modelChangeReceiver));
Sunny Goyal14168432019-10-24 15:59:49 -0700120
Sunny Goyalb47172b2021-05-03 19:59:51 -0700121 SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext)
Sunny Goyal337c81f2019-12-10 12:19:13 -0800122 .addUserChangeListener(mModel::forceReload);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700123 mOnTerminateCallback.add(userChangeListener::close);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800124
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000125 LockedUserState.get(context).runOnUserUnlocked(() -> {
126 CustomWidgetManager.INSTANCE.get(mContext)
127 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Tony Wickham827cef22016-03-17 15:39:39 -0700128
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000129 IconObserver observer = new IconObserver();
130 SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener(
131 observer, MODEL_EXECUTOR.getHandler());
132 mOnTerminateCallback.add(iconChangeTracker::close);
133 MODEL_EXECUTOR.execute(observer::verifyIconChanged);
134 LauncherPrefs.get(context).addListener(observer, THEMED_ICONS);
135 mOnTerminateCallback.add(
136 () -> LauncherPrefs.get(mContext).removeListener(observer, THEMED_ICONS));
137
138 InstallSessionTracker installSessionTracker =
139 InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(mModel);
140 mOnTerminateCallback.add(installSessionTracker::unregister);
141 });
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700142
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700143 // Register an observer to rebind the notification listener when dots are re-enabled.
Sunny Goyalb47172b2021-05-03 19:59:51 -0700144 SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext);
145 SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged;
146 settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister);
147 onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI));
148 mOnTerminateCallback.add(() ->
149 settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister));
Sunny Goyal420d5452018-10-17 10:25:14 -0700150 }
151
Tracy Zhoube13d102020-01-12 01:07:59 -0800152 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
Tracy Zhoube13d102020-01-12 01:07:59 -0800153 mContext = context;
154
155 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal68af5492021-12-24 01:08:31 +0530156 mIconProvider = new LauncherIconProvider(context);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700157 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
158 iconCacheFileName, mIconProvider);
Thiru Ramasamyd495e8c2021-08-26 10:37:17 -0700159 mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext),
160 iconCacheFileName != null);
Sunny Goyaldf4241c2021-05-19 19:42:14 -0700161 mOnTerminateCallback.add(mIconCache::close);
Tracy Zhoube13d102020-01-12 01:07:59 -0800162 }
163
Sunny Goyalb47172b2021-05-03 19:59:51 -0700164 private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800165 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700166 NotificationListener.requestRebind(new ComponentName(
167 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700168 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400169 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100170
Sunny Goyalb47172b2021-05-03 19:59:51 -0700171 private void refreshAndReloadLauncher() {
172 LauncherIcons.clearPool();
173 mIconCache.updateIconParams(
174 mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700175 mModel.forceReload();
176 }
177
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400178 /**
179 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
180 */
Sunny Goyal777d4902021-08-27 21:22:17 +0000181 @Override
182 public void close() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700183 mModel.destroy();
Sunny Goyale7b00122019-10-02 16:13:34 -0700184 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Sunny Goyal01615a62019-09-20 12:00:07 -0700185 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700186 mOnTerminateCallback.executeAllAndDestroy();
187 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700188
Sunny Goyalb47172b2021-05-03 19:59:51 -0700189 public IconProvider getIconProvider() {
190 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400191 }
192
Sunny Goyal34942622014-08-29 17:20:55 -0700193 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400194 return mIconCache;
195 }
196
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700197 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400198 return mModel;
199 }
200
Adam Cohen2e6da152015-05-06 11:42:25 -0700201 public InvariantDeviceProfile getInvariantDeviceProfile() {
202 return mInvariantDeviceProfile;
203 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800204
205 /**
206 * Shorthand for {@link #getInvariantDeviceProfile()}
207 */
208 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700209 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800210 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700211
Sunny Goyal01c32a62021-05-17 13:23:08 -0700212 private class IconObserver
213 implements IconProvider.IconChangeListener, OnSharedPreferenceChangeListener {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700214
215 @Override
216 public void onAppIconChanged(String packageName, UserHandle user) {
217 mModel.onAppIconChanged(packageName, user);
218 }
219
220 @Override
221 public void onSystemIconStateChanged(String iconState) {
222 IconShape.init(mContext);
223 refreshAndReloadLauncher();
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000224 LauncherPrefs.get(mContext).put(ICON_STATE, iconState);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700225 }
226
227 void verifyIconChanged() {
228 String iconState = mIconProvider.getSystemIconState();
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000229 if (!iconState.equals(LauncherPrefs.get(mContext).get(ICON_STATE))) {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700230 onSystemIconStateChanged(iconState);
231 }
232 }
Sunny Goyal01c32a62021-05-17 13:23:08 -0700233
234 @Override
235 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
236 if (Themes.KEY_THEMED_ICONS.equals(key)) {
237 mIconProvider.setIconThemeSupported(Themes.isThemedIconEnabled(mContext));
238 verifyIconChanged();
239 }
240 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700241 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400242}