blob: c6946cacf100742d0e52235b587a744d8ca8ce67 [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
Hyunyoung Songc55a3502018-12-04 15:43:16 -080019import static com.android.launcher3.InvariantDeviceProfile.CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070020import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Sunny Goyal420d5452018-10-17 10:25:14 -070021import static com.android.launcher3.util.SecureSettingsObserver.newNotificationSettingsObserver;
Sunny Goyald0e360a2018-06-29 14:40:18 -070022
Tonyd48710c2017-07-27 23:23:58 -070023import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070024import android.content.Context;
25import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070026import android.content.pm.LauncherApps;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080027import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040028import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080029
Sunny Goyal29947f02017-12-18 13:49:44 -080030import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070031import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070032import com.android.launcher3.icons.IconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070033import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070034import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080035import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070036import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080037import com.android.launcher3.pm.UserCache;
Sunny Goyald0e360a2018-06-29 14:40:18 -070038import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080039import com.android.launcher3.util.Preconditions;
Sunny Goyal14168432019-10-24 15:59:49 -070040import com.android.launcher3.util.SafeCloseable;
Sunny Goyal420d5452018-10-17 10:25:14 -070041import com.android.launcher3.util.SecureSettingsObserver;
Sunny Goyale7b00122019-10-02 16:13:34 -070042import com.android.launcher3.util.SimpleBroadcastReceiver;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070043import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044
Sunny Goyalc6205602015-05-21 20:46:33 -070045public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050046
Sunny Goyal29947f02017-12-18 13:49:44 -080047 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
48
Sunny Goyalfdbef272017-02-01 12:52:54 -080049 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070050 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070051 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040052
Sunny Goyalfdbef272017-02-01 12:52:54 -080053 private final Context mContext;
54 private final LauncherModel mModel;
55 private final IconCache mIconCache;
56 private final WidgetPreviewLoader mWidgetCache;
57 private final InvariantDeviceProfile mInvariantDeviceProfile;
Tony Wickhamf34bee82018-12-03 18:11:39 -080058 private final SecureSettingsObserver mNotificationDotsObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080059
Sunny Goyal045b4fa2019-09-20 12:51:37 -070060 private final InstallSessionTracker mInstallSessionTracker;
Sunny Goyale7b00122019-10-02 16:13:34 -070061 private final SimpleBroadcastReceiver mModelChangeReceiver;
Sunny Goyal14168432019-10-24 15:59:49 -070062 private final SafeCloseable mCalendarChangeTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080063 private final SafeCloseable mUserChangeListener;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070064
Sunny Goyalfdbef272017-02-01 12:52:54 -080065 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070066 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 }
68
Chris Wrend8fe6de2013-10-04 10:42:14 -040069 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070070 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040071 }
72
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075 }
76
Sunny Goyalfdbef272017-02-01 12:52:54 -080077 private LauncherAppState(Context context) {
Hyunyoung Song0de01172016-10-05 16:27:48 -070078 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080079 Preconditions.assertUIThread();
80 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040081
Sunny Goyald0e360a2018-06-29 14:40:18 -070082 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext);
Sunny Goyalfdbef272017-02-01 12:52:54 -080083 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
84 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070085 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070086
Sunny Goyale7b00122019-10-02 16:13:34 -070087 mModelChangeReceiver = new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088
Sunny Goyale7b00122019-10-02 16:13:34 -070089 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
90 mModelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
Sunny Goyale7b00122019-10-02 16:13:34 -070091 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
92 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
93 Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal29947f02017-12-18 13:49:44 -080094 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyale7b00122019-10-02 16:13:34 -070095 mModelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -080096 }
Sunny Goyal14168432019-10-24 15:59:49 -070097
98 mCalendarChangeTracker = IconProvider.registerIconChangeListener(mContext,
99 mModel::onAppIconChanged, MODEL_EXECUTOR.getHandler());
100
Sunny Goyal01615a62019-09-20 12:00:07 -0700101 // TODO: remove listener on terminate
Hyunyoung Songe17a6992019-09-09 23:32:17 -0700102 FeatureFlags.APP_SEARCH_IMPROVEMENTS.addChangeListener(context, mModel::forceReload);
Sunny Goyal01615a62019-09-20 12:00:07 -0700103 CustomWidgetManager.INSTANCE.get(mContext)
104 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Sunny Goyal29947f02017-12-18 13:49:44 -0800105
Sunny Goyal337c81f2019-12-10 12:19:13 -0800106 mUserChangeListener = UserCache.INSTANCE.get(mContext)
107 .addUserChangeListener(mModel::forceReload);
108
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700109 mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800110 new Handler().post( () -> mInvariantDeviceProfile.verifyConfigChangedInBackground(context));
Tony Wickham827cef22016-03-17 15:39:39 -0700111
Sunny Goyal73b5a272019-12-09 14:55:56 -0800112 mInstallSessionTracker = InstallSessionHelper.INSTANCE.get(context)
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700113 .registerInstallTracker(mModel, MODEL_EXECUTOR);
114
Tony Wickhamf34bee82018-12-03 18:11:39 -0800115 if (!mContext.getResources().getBoolean(R.bool.notification_dots_enabled)) {
116 mNotificationDotsObserver = null;
Tonyd48710c2017-07-27 23:23:58 -0700117 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800118 // Register an observer to rebind the notification listener when dots are re-enabled.
119 mNotificationDotsObserver =
Sunny Goyal420d5452018-10-17 10:25:14 -0700120 newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800121 mNotificationDotsObserver.register();
122 mNotificationDotsObserver.dispatchOnChange();
Sunny Goyal420d5452018-10-17 10:25:14 -0700123 }
124 }
125
Tony Wickhamf34bee82018-12-03 18:11:39 -0800126 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
127 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700128 NotificationListener.requestRebind(new ComponentName(
129 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700130 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400131 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100132
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700133 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
134 if (changeFlags == 0) {
135 return;
136 }
137
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800138 if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700139 LauncherIcons.clearPool();
140 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700141 mWidgetCache.refresh();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700142 }
143
144 mModel.forceReload();
145 }
146
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400147 /**
148 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
149 */
150 public void onTerminate() {
Sunny Goyale7b00122019-10-02 16:13:34 -0700151 mContext.unregisterReceiver(mModelChangeReceiver);
152 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700153 mInstallSessionTracker.unregister();
Sunny Goyal14168432019-10-24 15:59:49 -0700154 mCalendarChangeTracker.close();
Sunny Goyal337c81f2019-12-10 12:19:13 -0800155 mUserChangeListener.close();
Sunny Goyal01615a62019-09-20 12:00:07 -0700156 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
157
Tony Wickhamf34bee82018-12-03 18:11:39 -0800158 if (mNotificationDotsObserver != null) {
159 mNotificationDotsObserver.unregister();
Tonyd48710c2017-07-27 23:23:58 -0700160 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400161 }
162
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400163 LauncherModel setLauncher(Launcher launcher) {
164 mModel.initialize(launcher);
165 return mModel;
166 }
167
Sunny Goyal34942622014-08-29 17:20:55 -0700168 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400169 return mIconCache;
170 }
171
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700172 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400173 return mModel;
174 }
175
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700176 public WidgetPreviewLoader getWidgetCache() {
177 return mWidgetCache;
178 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100179
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 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190}