blob: efb3d36bf11102dba4bd347848226be2cd8528a5 [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 Goyalfdbef272017-02-01 12:52:54 -080024import android.content.ContentProviderClient;
Sunny Goyale755d462014-07-22 13:48:29 -070025import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080028import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040029import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080030
Kenny Guyed131872014-04-30 03:02:21 +010031import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070032import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal29947f02017-12-18 13:49:44 -080033import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070034import com.android.launcher3.icons.IconCache;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070035import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070036import com.android.launcher3.notification.NotificationListener;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070037import com.android.launcher3.pm.InstallSessionTracker;
38import com.android.launcher3.pm.PackageInstallerCompat;
Sunny Goyald0e360a2018-06-29 14:40:18 -070039import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080040import com.android.launcher3.util.Preconditions;
Sunny Goyal420d5452018-10-17 10:25:14 -070041import com.android.launcher3.util.SecureSettingsObserver;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070042import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080043
Sunny Goyalc6205602015-05-21 20:46:33 -070044public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050045
Sunny Goyal29947f02017-12-18 13:49:44 -080046 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
47
Sunny Goyalfdbef272017-02-01 12:52:54 -080048 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070049 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070050 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051
Sunny Goyalfdbef272017-02-01 12:52:54 -080052 private final Context mContext;
53 private final LauncherModel mModel;
54 private final IconCache mIconCache;
55 private final WidgetPreviewLoader mWidgetCache;
56 private final InvariantDeviceProfile mInvariantDeviceProfile;
Tony Wickhamf34bee82018-12-03 18:11:39 -080057 private final SecureSettingsObserver mNotificationDotsObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080058
Sunny Goyal045b4fa2019-09-20 12:51:37 -070059 private final InstallSessionTracker mInstallSessionTracker;
60
Sunny Goyalfdbef272017-02-01 12:52:54 -080061 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070062 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 }
64
Chris Wrend8fe6de2013-10-04 10:42:14 -040065 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070066 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040067 }
68
Daniel Sandlercc8befa2013-06-11 14:45:48 -040069 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080070 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071 }
72
Sunny Goyalfdbef272017-02-01 12:52:54 -080073 private LauncherAppState(Context context) {
74 if (getLocalProvider(context) == null) {
75 throw new RuntimeException(
76 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040077 }
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 Goyalfdbef272017-02-01 12:52:54 -080087 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088
89 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010090 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040091 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070092 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -070093 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
94 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
95 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
96 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
97 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070098
Sunny Goyal29947f02017-12-18 13:49:44 -080099 if (FeatureFlags.IS_DOGFOOD_BUILD) {
100 filter.addAction(ACTION_FORCE_ROLOAD);
101 }
Hyunyoung Songe17a6992019-09-09 23:32:17 -0700102 FeatureFlags.APP_SEARCH_IMPROVEMENTS.addChangeListener(context, mModel::forceReload);
Sunny Goyal29947f02017-12-18 13:49:44 -0800103
Sunny Goyalfdbef272017-02-01 12:52:54 -0800104 mContext.registerReceiver(mModel, filter);
105 UserManagerCompat.getInstance(mContext).enableAndResetCache();
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 Goyal045b4fa2019-09-20 12:51:37 -0700109 mInstallSessionTracker = PackageInstallerCompat.getInstance(context)
110 .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
Tony Wickhamf34bee82018-12-03 18:11:39 -0800123 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
124 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700125 NotificationListener.requestRebind(new ComponentName(
126 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700127 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400128 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100129
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700130 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
131 if (changeFlags == 0) {
132 return;
133 }
134
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800135 if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700136 LauncherIcons.clearPool();
137 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700138 mWidgetCache.refresh();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700139 }
140
141 mModel.forceReload();
142 }
143
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 /**
145 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
146 */
147 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800148 mContext.unregisterReceiver(mModel);
149 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100150 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700151 mInstallSessionTracker.unregister();
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) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800158 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400159 mModel.initialize(launcher);
Pinyao Tingc7a6c292019-08-26 14:36:02 -0700160 CustomWidgetManager.INSTANCE.get(launcher)
161 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162 return mModel;
163 }
164
Sunny Goyal34942622014-08-29 17:20:55 -0700165 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 return mIconCache;
167 }
168
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700169 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400170 return mModel;
171 }
172
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700173 public WidgetPreviewLoader getWidgetCache() {
174 return mWidgetCache;
175 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100176
Adam Cohen2e6da152015-05-06 11:42:25 -0700177 public InvariantDeviceProfile getInvariantDeviceProfile() {
178 return mInvariantDeviceProfile;
179 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800180
181 /**
182 * Shorthand for {@link #getInvariantDeviceProfile()}
183 */
184 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700185 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800186 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800187
188 private static LauncherProvider getLocalProvider(Context context) {
189 try (ContentProviderClient cl = context.getContentResolver()
190 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
191 return (LauncherProvider) cl.getLocalContentProvider();
192 }
193 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400194}