blob: e3fe87d1e9a93f0dc3a745711b4a4269bf4e7a38 [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
Tracy Zhoube13d102020-01-12 01:07:59 -080030import androidx.annotation.Nullable;
31
Sunny Goyal29947f02017-12-18 13:49:44 -080032import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070033import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070034import com.android.launcher3.icons.IconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070035import com.android.launcher3.icons.LauncherIcons;
Samuel Fufa385e9322020-05-04 11:14:52 -070036import com.android.launcher3.model.PredictionModel;
Tonyd48710c2017-07-27 23:23:58 -070037import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080038import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070039import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080040import com.android.launcher3.pm.UserCache;
Sunny Goyald0e360a2018-06-29 14:40:18 -070041import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080042import com.android.launcher3.util.Preconditions;
Sunny Goyal14168432019-10-24 15:59:49 -070043import com.android.launcher3.util.SafeCloseable;
Sunny Goyal420d5452018-10-17 10:25:14 -070044import com.android.launcher3.util.SecureSettingsObserver;
Sunny Goyale7b00122019-10-02 16:13:34 -070045import com.android.launcher3.util.SimpleBroadcastReceiver;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070046import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080047
Sunny Goyalc6205602015-05-21 20:46:33 -070048public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050049
Sunny Goyal29947f02017-12-18 13:49:44 -080050 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
51
Sunny Goyalfdbef272017-02-01 12:52:54 -080052 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070053 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070054 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055
Sunny Goyalfdbef272017-02-01 12:52:54 -080056 private final Context mContext;
57 private final LauncherModel mModel;
58 private final IconCache mIconCache;
59 private final WidgetPreviewLoader mWidgetCache;
60 private final InvariantDeviceProfile mInvariantDeviceProfile;
Samuel Fufa385e9322020-05-04 11:14:52 -070061 private final PredictionModel mPredictionModel;
Sunny Goyalfdbef272017-02-01 12:52:54 -080062
Tracy Zhoube13d102020-01-12 01:07:59 -080063 private SecureSettingsObserver mNotificationDotsObserver;
64 private InstallSessionTracker mInstallSessionTracker;
65 private SimpleBroadcastReceiver mModelChangeReceiver;
66 private SafeCloseable mCalendarChangeTracker;
67 private SafeCloseable mUserChangeListener;
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 Goyal27595792015-03-19 13:18:44 -070083
Sunny Goyale7b00122019-10-02 16:13:34 -070084 mModelChangeReceiver = new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085
Sunny Goyale7b00122019-10-02 16:13:34 -070086 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
87 mModelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
Sunny Goyale7b00122019-10-02 16:13:34 -070088 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
89 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
90 Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Zak Cohen3eeb41d2020-02-14 14:15:13 -080091 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyale7b00122019-10-02 16:13:34 -070092 mModelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -080093 }
Sunny Goyal14168432019-10-24 15:59:49 -070094
95 mCalendarChangeTracker = IconProvider.registerIconChangeListener(mContext,
96 mModel::onAppIconChanged, MODEL_EXECUTOR.getHandler());
97
Sunny Goyal01615a62019-09-20 12:00:07 -070098 // TODO: remove listener on terminate
Hyunyoung Songe17a6992019-09-09 23:32:17 -070099 FeatureFlags.APP_SEARCH_IMPROVEMENTS.addChangeListener(context, mModel::forceReload);
Sunny Goyal01615a62019-09-20 12:00:07 -0700100 CustomWidgetManager.INSTANCE.get(mContext)
101 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Sunny Goyal29947f02017-12-18 13:49:44 -0800102
Sunny Goyal337c81f2019-12-10 12:19:13 -0800103 mUserChangeListener = UserCache.INSTANCE.get(mContext)
104 .addUserChangeListener(mModel::forceReload);
105
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700106 mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800107 new Handler().post( () -> mInvariantDeviceProfile.verifyConfigChangedInBackground(context));
Tony Wickham827cef22016-03-17 15:39:39 -0700108
Sunny Goyal73b5a272019-12-09 14:55:56 -0800109 mInstallSessionTracker = InstallSessionHelper.INSTANCE.get(context)
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700110 .registerInstallTracker(mModel, MODEL_EXECUTOR);
111
Tony Wickhamf34bee82018-12-03 18:11:39 -0800112 if (!mContext.getResources().getBoolean(R.bool.notification_dots_enabled)) {
113 mNotificationDotsObserver = null;
Tonyd48710c2017-07-27 23:23:58 -0700114 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800115 // Register an observer to rebind the notification listener when dots are re-enabled.
116 mNotificationDotsObserver =
Sunny Goyal420d5452018-10-17 10:25:14 -0700117 newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800118 mNotificationDotsObserver.register();
119 mNotificationDotsObserver.dispatchOnChange();
Sunny Goyal420d5452018-10-17 10:25:14 -0700120 }
121 }
122
Tracy Zhoube13d102020-01-12 01:07:59 -0800123 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
124 Log.v(Launcher.TAG, "LauncherAppState initiated");
125 Preconditions.assertUIThread();
126 mContext = context;
127
128 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
129 mIconCache = new IconCache(mContext, mInvariantDeviceProfile, iconCacheFileName);
130 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700131 mModel = new LauncherModel(context, this, mIconCache, AppFilter.newInstance(mContext));
Samuel Fufad12d6ab2020-05-11 19:48:59 -0700132 mPredictionModel = PredictionModel.newInstance(mContext);
Tracy Zhoube13d102020-01-12 01:07:59 -0800133 }
134
Tony Wickhamf34bee82018-12-03 18:11:39 -0800135 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
136 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700137 NotificationListener.requestRebind(new ComponentName(
138 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700139 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100141
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700142 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
143 if (changeFlags == 0) {
144 return;
145 }
146
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800147 if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700148 LauncherIcons.clearPool();
149 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700150 mWidgetCache.refresh();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700151 }
152
153 mModel.forceReload();
154 }
155
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400156 /**
157 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
158 */
159 public void onTerminate() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700160 mModel.destroy();
Tracy Zhoube13d102020-01-12 01:07:59 -0800161 if (mModelChangeReceiver != null) {
162 mContext.unregisterReceiver(mModelChangeReceiver);
163 }
Sunny Goyale7b00122019-10-02 16:13:34 -0700164 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Tracy Zhoube13d102020-01-12 01:07:59 -0800165 if (mInstallSessionTracker != null) {
166 mInstallSessionTracker.unregister();
167 }
168 if (mCalendarChangeTracker != null) {
169 mCalendarChangeTracker.close();
170 }
171 if (mUserChangeListener != null) {
172 mUserChangeListener.close();
173 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700174 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
175
Tony Wickhamf34bee82018-12-03 18:11:39 -0800176 if (mNotificationDotsObserver != null) {
177 mNotificationDotsObserver.unregister();
Tonyd48710c2017-07-27 23:23:58 -0700178 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400179 }
180
Sunny Goyal34942622014-08-29 17:20:55 -0700181 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400182 return mIconCache;
183 }
184
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700185 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400186 return mModel;
187 }
188
Samuel Fufa385e9322020-05-04 11:14:52 -0700189 public PredictionModel getPredictionModel() {
190 return mPredictionModel;
191 }
192
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700193 public WidgetPreviewLoader getWidgetCache() {
194 return mWidgetCache;
195 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100196
Adam Cohen2e6da152015-05-06 11:42:25 -0700197 public InvariantDeviceProfile getInvariantDeviceProfile() {
198 return mInvariantDeviceProfile;
199 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800200
201 /**
202 * Shorthand for {@link #getInvariantDeviceProfile()}
203 */
204 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700205 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800206 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400207}