blob: 7bb60714f9417e5ea49652bb988208dfa43689a1 [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 Goyal420d5452018-10-17 10:25:14 -070020import static com.android.launcher3.util.SecureSettingsObserver.newNotificationSettingsObserver;
Sunny Goyald0e360a2018-06-29 14:40:18 -070021
Tony573c3e12019-02-22 13:10:59 -080022import android.app.KeyguardManager;
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;
Tony573c3e12019-02-22 13:10:59 -080029import android.os.Process;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040030import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080031
Kenny Guyed131872014-04-30 03:02:21 +010032import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070033import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070034import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal29947f02017-12-18 13:49:44 -080035import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070036import com.android.launcher3.icons.IconCache;
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 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;
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 Goyald0e360a2018-06-29 14:40:18 -070048 private static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
49 new MainThreadInitializedObject<>((c) -> new LauncherAppState(c));
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
58 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070059 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040060 }
61
Chris Wrend8fe6de2013-10-04 10:42:14 -040062 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070063 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040064 }
65
Daniel Sandlercc8befa2013-06-11 14:45:48 -040066 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080067 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040068 }
69
Sunny Goyalfdbef272017-02-01 12:52:54 -080070 private LauncherAppState(Context context) {
Tony573c3e12019-02-22 13:10:59 -080071 if (!UserManagerCompat.getInstance(context).isUserUnlocked(Process.myUserHandle())) {
72 throw new RuntimeException("LauncherAppState should not start in direct boot mode");
73 }
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 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 }
102
Sunny Goyalfdbef272017-02-01 12:52:54 -0800103 mContext.registerReceiver(mModel, filter);
104 UserManagerCompat.getInstance(mContext).enableAndResetCache();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700105 mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800106 new Handler().post( () -> mInvariantDeviceProfile.verifyConfigChangedInBackground(context));
Tony Wickham827cef22016-03-17 15:39:39 -0700107
Tony Wickhamf34bee82018-12-03 18:11:39 -0800108 if (!mContext.getResources().getBoolean(R.bool.notification_dots_enabled)) {
109 mNotificationDotsObserver = null;
Tonyd48710c2017-07-27 23:23:58 -0700110 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800111 // Register an observer to rebind the notification listener when dots are re-enabled.
112 mNotificationDotsObserver =
Sunny Goyal420d5452018-10-17 10:25:14 -0700113 newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800114 mNotificationDotsObserver.register();
115 mNotificationDotsObserver.dispatchOnChange();
Sunny Goyal420d5452018-10-17 10:25:14 -0700116 }
117 }
118
Tony Wickhamf34bee82018-12-03 18:11:39 -0800119 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
120 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700121 NotificationListener.requestRebind(new ComponentName(
122 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700123 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100125
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700126 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
127 if (changeFlags == 0) {
128 return;
129 }
130
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800131 if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700132 LauncherIcons.clearPool();
133 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
134 }
135
136 mModel.forceReload();
137 }
138
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139 /**
140 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
141 */
142 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800143 mContext.unregisterReceiver(mModel);
144 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100145 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800146 PackageInstallerCompat.getInstance(mContext).onStop();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800147 if (mNotificationDotsObserver != null) {
148 mNotificationDotsObserver.unregister();
Tonyd48710c2017-07-27 23:23:58 -0700149 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400150 }
151
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800153 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400154 mModel.initialize(launcher);
155 return mModel;
156 }
157
Sunny Goyal34942622014-08-29 17:20:55 -0700158 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400159 return mIconCache;
160 }
161
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700162 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400163 return mModel;
164 }
165
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700166 public WidgetPreviewLoader getWidgetCache() {
167 return mWidgetCache;
168 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100169
Adam Cohen2e6da152015-05-06 11:42:25 -0700170 public InvariantDeviceProfile getInvariantDeviceProfile() {
171 return mInvariantDeviceProfile;
172 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800173
174 /**
175 * Shorthand for {@link #getInvariantDeviceProfile()}
176 */
177 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700178 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800179 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800180
181 private static LauncherProvider getLocalProvider(Context context) {
182 try (ContentProviderClient cl = context.getContentResolver()
183 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
184 return (LauncherProvider) cl.getLocalContentProvider();
185 }
186 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400187}