blob: 834b5a7e8c24605af33e0ae3f6c0d9973d959122 [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 Goyale7b00122019-10-02 16:13:34 -070027import android.content.pm.LauncherApps;
Sunny Goyalb47172b2021-05-03 19:59:51 -070028import android.os.UserHandle;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040029import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080030
Tracy Zhoube13d102020-01-12 01:07:59 -080031import androidx.annotation.Nullable;
32
Sunny Goyal29947f02017-12-18 13:49:44 -080033import com.android.launcher3.config.FeatureFlags;
Sunny Goyalb47172b2021-05-03 19:59:51 -070034import com.android.launcher3.graphics.IconShape;
Sunny Goyalf840f102018-09-21 14:41:05 -070035import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070036import com.android.launcher3.icons.IconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070037import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070038import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080039import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070040import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080041import com.android.launcher3.pm.UserCache;
Sunny Goyald0e360a2018-06-29 14:40:18 -070042import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080043import com.android.launcher3.util.Preconditions;
Sunny Goyalb47172b2021-05-03 19:59:51 -070044import com.android.launcher3.util.RunnableList;
Sunny Goyal14168432019-10-24 15:59:49 -070045import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040046import com.android.launcher3.util.SettingsCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070047import com.android.launcher3.util.SimpleBroadcastReceiver;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070048import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049
Sunny Goyalc6205602015-05-21 20:46:33 -070050public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050051
Sunny Goyal29947f02017-12-18 13:49:44 -080052 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
Sunny Goyalb47172b2021-05-03 19:59:51 -070053 private static final String KEY_ICON_STATE = "pref_icon_shape_path";
Sunny Goyal29947f02017-12-18 13:49:44 -080054
Sunny Goyalfdbef272017-02-01 12:52:54 -080055 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070056 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070057 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058
Sunny Goyalfdbef272017-02-01 12:52:54 -080059 private final Context mContext;
60 private final LauncherModel mModel;
Sunny Goyalb47172b2021-05-03 19:59:51 -070061 private final IconProvider mIconProvider;
Sunny Goyalfdbef272017-02-01 12:52:54 -080062 private final IconCache mIconCache;
63 private final WidgetPreviewLoader mWidgetCache;
64 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyalb47172b2021-05-03 19:59:51 -070065 private final RunnableList mOnTerminateCallback = new RunnableList();
Sunny Goyal045b4fa2019-09-20 12:51:37 -070066
Sunny Goyalfdbef272017-02-01 12:52:54 -080067 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070068 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040069 }
70
Chris Wrend8fe6de2013-10-04 10:42:14 -040071 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070072 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040073 }
74
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080076 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 }
78
Tracy Zhoube13d102020-01-12 01:07:59 -080079 public LauncherAppState(Context context) {
80 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyal27595792015-03-19 13:18:44 -070081
Sunny Goyalb47172b2021-05-03 19:59:51 -070082 mInvariantDeviceProfile.addOnChangeListener(idp -> refreshAndReloadLauncher());
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083
Sunny Goyale7b00122019-10-02 16:13:34 -070084 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
Sunny Goyalb47172b2021-05-03 19:59:51 -070085
86 SimpleBroadcastReceiver modelChangeReceiver =
87 new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
88 modelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
Sunny Goyale7b00122019-10-02 16:13:34 -070089 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
90 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
91 Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Zak Cohen3eeb41d2020-02-14 14:15:13 -080092 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyalb47172b2021-05-03 19:59:51 -070093 modelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -080094 }
Sunny Goyalb47172b2021-05-03 19:59:51 -070095 mOnTerminateCallback.add(() -> mContext.unregisterReceiver(modelChangeReceiver));
Sunny Goyal14168432019-10-24 15:59:49 -070096
Sunny Goyal01615a62019-09-20 12:00:07 -070097 // TODO: remove listener on terminate
Hyunyoung Songe17a6992019-09-09 23:32:17 -070098 FeatureFlags.APP_SEARCH_IMPROVEMENTS.addChangeListener(context, mModel::forceReload);
Sunny Goyal01615a62019-09-20 12:00:07 -070099 CustomWidgetManager.INSTANCE.get(mContext)
100 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Sunny Goyal29947f02017-12-18 13:49:44 -0800101
Sunny Goyalb47172b2021-05-03 19:59:51 -0700102 SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext)
Sunny Goyal337c81f2019-12-10 12:19:13 -0800103 .addUserChangeListener(mModel::forceReload);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700104 mOnTerminateCallback.add(userChangeListener::close);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800105
Sunny Goyalb47172b2021-05-03 19:59:51 -0700106 IconObserver observer = new IconObserver();
107 SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener(
108 observer, MODEL_EXECUTOR.getHandler());
109 mOnTerminateCallback.add(iconChangeTracker::close);
110 MODEL_EXECUTOR.execute(observer::verifyIconChanged);
Tony Wickham827cef22016-03-17 15:39:39 -0700111
Sunny Goyalb47172b2021-05-03 19:59:51 -0700112 InstallSessionTracker installSessionTracker =
113 InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(mModel);
114 mOnTerminateCallback.add(installSessionTracker::unregister);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700115
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700116 // Register an observer to rebind the notification listener when dots are re-enabled.
Sunny Goyalb47172b2021-05-03 19:59:51 -0700117 SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext);
118 SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged;
119 settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister);
120 onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI));
121 mOnTerminateCallback.add(() ->
122 settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister));
Sunny Goyal420d5452018-10-17 10:25:14 -0700123 }
124
Tracy Zhoube13d102020-01-12 01:07:59 -0800125 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
126 Log.v(Launcher.TAG, "LauncherAppState initiated");
127 Preconditions.assertUIThread();
128 mContext = context;
129
130 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700131 mIconProvider = new IconProvider(context, ENABLE_THEMED_ICONS.get());
132 mIconCache = new IconCache(mContext, mInvariantDeviceProfile,
133 iconCacheFileName, mIconProvider);
Tracy Zhoube13d102020-01-12 01:07:59 -0800134 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyal44840152020-08-25 23:22:18 -0700135 mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext));
Tracy Zhoube13d102020-01-12 01:07:59 -0800136 }
137
Sunny Goyalb47172b2021-05-03 19:59:51 -0700138 private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800139 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700140 NotificationListener.requestRebind(new ComponentName(
141 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700142 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100144
Sunny Goyalb47172b2021-05-03 19:59:51 -0700145 private void refreshAndReloadLauncher() {
146 LauncherIcons.clearPool();
147 mIconCache.updateIconParams(
148 mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize);
149 mWidgetCache.refresh();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700150 mModel.forceReload();
151 }
152
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400153 /**
154 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
155 */
156 public void onTerminate() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700157 mModel.destroy();
Sunny Goyale7b00122019-10-02 16:13:34 -0700158 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Sunny Goyal01615a62019-09-20 12:00:07 -0700159 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
Sunny Goyalb47172b2021-05-03 19:59:51 -0700160 mOnTerminateCallback.executeAllAndDestroy();
161 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700162
Sunny Goyalb47172b2021-05-03 19:59:51 -0700163 public IconProvider getIconProvider() {
164 return mIconProvider;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400165 }
166
Sunny Goyal34942622014-08-29 17:20:55 -0700167 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400168 return mIconCache;
169 }
170
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700171 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400172 return mModel;
173 }
174
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700175 public WidgetPreviewLoader getWidgetCache() {
176 return mWidgetCache;
177 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100178
Adam Cohen2e6da152015-05-06 11:42:25 -0700179 public InvariantDeviceProfile getInvariantDeviceProfile() {
180 return mInvariantDeviceProfile;
181 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800182
183 /**
184 * Shorthand for {@link #getInvariantDeviceProfile()}
185 */
186 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700187 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800188 }
Sunny Goyalb47172b2021-05-03 19:59:51 -0700189
190 private class IconObserver implements IconProvider.IconChangeListener {
191
192 @Override
193 public void onAppIconChanged(String packageName, UserHandle user) {
194 mModel.onAppIconChanged(packageName, user);
195 }
196
197 @Override
198 public void onSystemIconStateChanged(String iconState) {
199 IconShape.init(mContext);
200 refreshAndReloadLauncher();
201 getDevicePrefs(mContext).edit().putString(KEY_ICON_STATE, iconState).apply();
202 }
203
204 void verifyIconChanged() {
205 String iconState = mIconProvider.getSystemIconState();
206 if (!iconState.equals(getDevicePrefs(mContext).getString(KEY_ICON_STATE, ""))) {
207 onSystemIconStateChanged(iconState);
208 }
209 }
210 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400211}