blob: 018b48fd33375c24159930ef1c91b61bdceed496 [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;
26import android.content.IntentFilter;
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
Kenny Guyed131872014-04-30 03:02:21 +010030import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070031import com.android.launcher3.compat.UserManagerCompat;
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 Goyal87dc48b2018-10-12 11:42:33 -070034import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070035import com.android.launcher3.notification.NotificationListener;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070036import com.android.launcher3.pm.InstallSessionTracker;
37import com.android.launcher3.pm.PackageInstallerCompat;
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 Goyal420d5452018-10-17 10:25:14 -070040import com.android.launcher3.util.SecureSettingsObserver;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070041import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080042
Sunny Goyalc6205602015-05-21 20:46:33 -070043public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050044
Sunny Goyal29947f02017-12-18 13:49:44 -080045 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
46
Sunny Goyalfdbef272017-02-01 12:52:54 -080047 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070048 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070049 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040050
Sunny Goyalfdbef272017-02-01 12:52:54 -080051 private final Context mContext;
52 private final LauncherModel mModel;
53 private final IconCache mIconCache;
54 private final WidgetPreviewLoader mWidgetCache;
55 private final InvariantDeviceProfile mInvariantDeviceProfile;
Tony Wickhamf34bee82018-12-03 18:11:39 -080056 private final SecureSettingsObserver mNotificationDotsObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080057
Sunny Goyal045b4fa2019-09-20 12:51:37 -070058 private final InstallSessionTracker mInstallSessionTracker;
59
Sunny Goyalfdbef272017-02-01 12:52:54 -080060 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070061 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062 }
63
Chris Wrend8fe6de2013-10-04 10:42:14 -040064 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070065 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040066 }
67
Daniel Sandlercc8befa2013-06-11 14:45:48 -040068 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080069 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040070 }
71
Sunny Goyalfdbef272017-02-01 12:52:54 -080072 private LauncherAppState(Context context) {
Hyunyoung Song0de01172016-10-05 16:27:48 -070073 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 Preconditions.assertUIThread();
75 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040076
Sunny Goyald0e360a2018-06-29 14:40:18 -070077 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext);
Sunny Goyalfdbef272017-02-01 12:52:54 -080078 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
79 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070080 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070081
Sunny Goyalfdbef272017-02-01 12:52:54 -080082 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083
84 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010085 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070087 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -070088 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
89 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
90 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
91 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
92 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070093
Sunny Goyal29947f02017-12-18 13:49:44 -080094 if (FeatureFlags.IS_DOGFOOD_BUILD) {
95 filter.addAction(ACTION_FORCE_ROLOAD);
96 }
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 Goyalfdbef272017-02-01 12:52:54 -0800102 mContext.registerReceiver(mModel, filter);
103 UserManagerCompat.getInstance(mContext).enableAndResetCache();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700104 mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800105 new Handler().post( () -> mInvariantDeviceProfile.verifyConfigChangedInBackground(context));
Tony Wickham827cef22016-03-17 15:39:39 -0700106
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700107 mInstallSessionTracker = PackageInstallerCompat.getInstance(context)
108 .registerInstallTracker(mModel, MODEL_EXECUTOR);
109
Tony Wickhamf34bee82018-12-03 18:11:39 -0800110 if (!mContext.getResources().getBoolean(R.bool.notification_dots_enabled)) {
111 mNotificationDotsObserver = null;
Tonyd48710c2017-07-27 23:23:58 -0700112 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800113 // Register an observer to rebind the notification listener when dots are re-enabled.
114 mNotificationDotsObserver =
Sunny Goyal420d5452018-10-17 10:25:14 -0700115 newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800116 mNotificationDotsObserver.register();
117 mNotificationDotsObserver.dispatchOnChange();
Sunny Goyal420d5452018-10-17 10:25:14 -0700118 }
119 }
120
Tony Wickhamf34bee82018-12-03 18:11:39 -0800121 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
122 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700123 NotificationListener.requestRebind(new ComponentName(
124 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700125 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100127
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700128 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
129 if (changeFlags == 0) {
130 return;
131 }
132
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800133 if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700134 LauncherIcons.clearPool();
135 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700136 mWidgetCache.refresh();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700137 }
138
139 mModel.forceReload();
140 }
141
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400142 /**
143 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
144 */
145 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800146 mContext.unregisterReceiver(mModel);
147 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100148 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700149 mInstallSessionTracker.unregister();
Sunny Goyal01615a62019-09-20 12:00:07 -0700150 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
151
Tony Wickhamf34bee82018-12-03 18:11:39 -0800152 if (mNotificationDotsObserver != null) {
153 mNotificationDotsObserver.unregister();
Tonyd48710c2017-07-27 23:23:58 -0700154 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 }
156
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157 LauncherModel setLauncher(Launcher launcher) {
158 mModel.initialize(launcher);
159 return mModel;
160 }
161
Sunny Goyal34942622014-08-29 17:20:55 -0700162 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400163 return mIconCache;
164 }
165
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700166 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400167 return mModel;
168 }
169
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700170 public WidgetPreviewLoader getWidgetCache() {
171 return mWidgetCache;
172 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100173
Adam Cohen2e6da152015-05-06 11:42:25 -0700174 public InvariantDeviceProfile getInvariantDeviceProfile() {
175 return mInvariantDeviceProfile;
176 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800177
178 /**
179 * Shorthand for {@link #getInvariantDeviceProfile()}
180 */
181 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700182 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800183 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400184}