blob: 4e2f7d4ee8620be15b3381839286b025d5284e85 [file] [log] [blame]
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001/*
2 * Copyright (C) 2008 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
Jim Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080018
Wale Ogunwale68278562017-09-23 17:13:55 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
20import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Lucas Dupin6c6d5732019-08-27 17:36:05 -070021import static android.content.Intent.ACTION_USER_REMOVED;
22import static android.content.Intent.ACTION_USER_STOPPED;
Jorim Jaggidadafd42016-09-30 07:20:25 -070023import static android.content.Intent.ACTION_USER_UNLOCKED;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070024import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
25import static android.os.BatteryManager.BATTERY_STATUS_FULL;
26import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
27import static android.os.BatteryManager.EXTRA_HEALTH;
28import static android.os.BatteryManager.EXTRA_LEVEL;
29import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
30import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE;
31import static android.os.BatteryManager.EXTRA_PLUGGED;
32import static android.os.BatteryManager.EXTRA_STATUS;
Fabian Kozynskiccde55d2019-06-26 10:06:09 -040033import static android.telephony.PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE;
Daniel Bright63514be2020-01-15 12:10:53 -080034import static android.telephony.TelephonyManager.MODEM_COUNT_DUAL_MODEM;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070035
Lucas Dupin8eec2682019-07-01 16:41:17 -070036import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT;
37import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW;
38import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT;
39import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT;
40import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
Lucas Dupin8968f6a2019-08-09 17:41:15 -070041import static com.android.systemui.DejankUtils.whitelistIpcs;
Lucas Dupin8eec2682019-07-01 16:41:17 -070042
Adrian Roos30a2ae62018-04-25 19:09:50 +020043import android.annotation.AnyThread;
44import android.annotation.MainThread;
Jorim Jaggiccdfa932015-04-13 16:29:48 -070045import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070046import android.app.ActivityTaskManager;
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020047import android.app.AlarmManager;
Jim Miller8f09fd22013-03-14 19:04:28 -070048import android.app.PendingIntent;
Sudheer Shanka2c4522c2016-08-27 20:53:28 -070049import android.app.UserSwitchObserver;
Jim Millerb0304762012-03-13 20:01:25 -070050import android.app.admin.DevicePolicyManager;
Adrian Roos46842d92014-03-27 14:58:03 +010051import android.app.trust.TrustManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080052import android.content.BroadcastReceiver;
Jorim Jaggi031f7952016-09-01 16:39:26 -070053import android.content.ComponentName;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080054import android.content.Context;
55import android.content.Intent;
56import android.content.IntentFilter;
Adrian Roosca8a2162017-08-17 19:00:58 +020057import android.content.pm.IPackageManager;
Jorim Jaggi031f7952016-09-01 16:39:26 -070058import android.content.pm.PackageManager;
59import android.content.pm.ResolveInfo;
Lucas Dupinf2c53502019-10-03 13:56:18 -070060import android.content.pm.UserInfo;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080061import android.database.ContentObserver;
Kevin Chynb7b54a62018-09-28 18:48:12 -070062import android.hardware.biometrics.BiometricManager;
Kevin Chyn56233ab2018-09-20 17:10:57 -070063import android.hardware.biometrics.BiometricSourceType;
Kevin Chynb7b54a62018-09-28 18:48:12 -070064import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020065import android.hardware.face.FaceManager;
Jorim Jaggi86bed402015-08-20 18:20:02 -070066import android.hardware.fingerprint.FingerprintManager;
67import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
68import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
Jim Miller47088bb2009-11-24 00:40:16 -080069import android.media.AudioManager;
Jim Miller79a444a2011-02-15 15:02:11 -080070import android.os.BatteryManager;
Jim Miller9f0753f2015-03-23 23:59:22 -070071import android.os.CancellationSignal;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080072import android.os.Handler;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070073import android.os.IRemoteCallback;
Jason Monk7bb59302018-05-10 19:38:18 -070074import android.os.Looper;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080075import android.os.Message;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070076import android.os.RemoteException;
Adrian Roosca8a2162017-08-17 19:00:58 +020077import android.os.ServiceManager;
Nick Desaulniers1d396752016-07-25 15:05:33 -070078import android.os.Trace;
Amith Yamasanie8e93a12013-05-09 18:12:30 -070079import android.os.UserHandle;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070080import android.os.UserManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080081import android.provider.Settings;
Kevin Chyn36778ff2017-09-07 19:55:38 -070082import android.service.dreams.DreamService;
83import android.service.dreams.IDreamManager;
Malcolm Chen5c63b512019-08-13 13:24:07 -070084import android.telephony.CarrierConfigManager;
Fabian Kozynskiccde55d2019-06-26 10:06:09 -040085import android.telephony.PhoneStateListener;
Etan Cohen47051d82015-07-06 16:19:04 -070086import android.telephony.ServiceState;
Jim Miller52a61332014-11-12 19:29:51 -080087import android.telephony.SubscriptionInfo;
Jim Miller52a61332014-11-12 19:29:51 -080088import android.telephony.SubscriptionManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -080089import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Jim Millerc23024d2010-02-24 15:37:00 -080090import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080091import android.util.Log;
Adrian Roos46842d92014-03-27 14:58:03 +010092import android.util.SparseBooleanArray;
93
Lucas Dupin7517b5d2017-08-22 12:51:25 -070094import com.android.internal.annotations.VisibleForTesting;
Adrian Roosb5e47222015-08-14 15:53:06 -070095import com.android.internal.widget.LockPatternUtils;
Bill Linef81cbd2018-07-05 17:48:49 +080096import com.android.settingslib.WirelessUtils;
Lucas Dupind236ee32019-10-08 15:33:59 -070097import com.android.systemui.DejankUtils;
Lucas Dupin64171fe2019-10-30 14:28:29 -070098import com.android.systemui.DumpController;
99import com.android.systemui.Dumpable;
Hyunyoung Song8f9d34c2019-08-30 14:47:43 -0700100import com.android.systemui.R;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000101import com.android.systemui.broadcast.BroadcastDispatcher;
Dave Mankoff00e8a2f2019-12-18 16:59:49 -0500102import com.android.systemui.dagger.qualifiers.Main;
Winson Chung2cf6ad82017-11-09 17:36:59 -0800103import com.android.systemui.shared.system.ActivityManagerWrapper;
Winson Chung67f5c8b2018-09-24 12:09:19 -0700104import com.android.systemui.shared.system.TaskStackChangeListener;
Lucas Dupin9e484aa2019-06-24 13:38:00 -0700105import com.android.systemui.statusbar.phone.KeyguardBypassController;
Kevin Chyn1123ba72018-10-26 10:34:06 -0700106
Kevin Chyn36778ff2017-09-07 19:55:38 -0700107import com.google.android.collect.Lists;
108
Jason Monkab525272015-07-13 17:02:49 -0400109import java.io.FileDescriptor;
110import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -0700111import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800112import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -0800113import java.util.HashMap;
114import java.util.List;
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800115import java.util.Map;
Jim Miller52a61332014-11-12 19:29:51 -0800116import java.util.Map.Entry;
Robert Snoeberger9c1074f2018-12-07 17:35:21 -0500117import java.util.TimeZone;
Lucas Dupin3d053532019-01-29 12:35:22 -0800118import java.util.function.Consumer;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800119
Dave Mankoffe2294692019-08-14 11:53:13 -0400120import javax.inject.Inject;
Lucas Dupinab39e802019-10-08 14:42:00 -0700121import javax.inject.Singleton;
Dave Mankoffe2294692019-08-14 11:53:13 -0400122
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800123/**
124 * Watches for updates that may be interesting to the keyguard, and provides
125 * the up to date information as well as a registration for callbacks that care
126 * to be updated.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800127 */
Lucas Dupinab39e802019-10-08 14:42:00 -0700128@Singleton
Lucas Dupin64171fe2019-10-30 14:28:29 -0700129public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpable {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800130
Jim Millerbbf1a742012-07-17 18:30:30 -0700131 private static final String TAG = "KeyguardUpdateMonitor";
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100132 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller52a61332014-11-12 19:29:51 -0800133 private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700134 private static final boolean DEBUG_FACE = true;
Jim Millerbbf1a742012-07-17 18:30:30 -0700135 private static final int LOW_BATTERY_THRESHOLD = 20;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800136
Jorim Jaggie7b12522014-08-06 16:41:21 +0200137 private static final String ACTION_FACE_UNLOCK_STARTED
138 = "com.android.facelock.FACE_UNLOCK_STARTED";
139 private static final String ACTION_FACE_UNLOCK_STOPPED
140 = "com.android.facelock.FACE_UNLOCK_STOPPED";
141
Jim Millerbbf1a742012-07-17 18:30:30 -0700142 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800143 private static final int MSG_TIME_UPDATE = 301;
144 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800145 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800146 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800147 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700148 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700149 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500150 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700151 private static final int MSG_KEYGUARD_RESET = 312;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500152 private static final int MSG_USER_SWITCH_COMPLETE = 314;
Jim Millerf41fc962014-06-18 16:33:51 -0700153 private static final int MSG_USER_INFO_CHANGED = 317;
154 private static final int MSG_REPORT_EMERGENCY_CALL_ACTION = 318;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700155 private static final int MSG_STARTED_WAKING_UP = 319;
156 private static final int MSG_FINISHED_GOING_TO_SLEEP = 320;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700157 private static final int MSG_STARTED_GOING_TO_SLEEP = 321;
Adrian Roosb6011622014-05-14 15:52:53 +0200158 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700159 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
160 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400161 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700162 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700163 private static final int MSG_SCREEN_TURNED_ON = 331;
164 private static final int MSG_SCREEN_TURNED_OFF = 332;
Selim Cinek99415392016-09-09 14:58:41 -0700165 private static final int MSG_DREAMING_STATE_CHANGED = 333;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700166 private static final int MSG_USER_UNLOCKED = 334;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700167 private static final int MSG_ASSISTANT_STACK_CHANGED = 335;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200168 private static final int MSG_BIOMETRIC_AUTHENTICATION_CONTINUE = 336;
Alex Chauff7653d2018-02-01 17:18:08 +0000169 private static final int MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED = 337;
Bill Linef81cbd2018-07-05 17:48:49 +0800170 private static final int MSG_TELEPHONY_CAPABLE = 338;
Robert Snoeberger9c1074f2018-12-07 17:35:21 -0500171 private static final int MSG_TIMEZONE_UPDATE = 339;
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700172 private static final int MSG_USER_STOPPED = 340;
173 private static final int MSG_USER_REMOVED = 341;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800174
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200175 /** Biometric authentication state: Not listening. */
176 private static final int BIOMETRIC_STATE_STOPPED = 0;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700177
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200178 /** Biometric authentication state: Listening. */
179 private static final int BIOMETRIC_STATE_RUNNING = 1;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700180
181 /**
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200182 * Biometric authentication: Cancelling and waiting for the relevant biometric service to
Jorim Jaggi86bed402015-08-20 18:20:02 -0700183 * send us the confirmation that cancellation has happened.
184 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200185 private static final int BIOMETRIC_STATE_CANCELLING = 2;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700186
187 /**
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200188 * Biometric state: During cancelling we got another request to start listening, so when we
Jorim Jaggi86bed402015-08-20 18:20:02 -0700189 * receive the cancellation done signal, we should start listening again.
190 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200191 private static final int BIOMETRIC_STATE_CANCELLING_RESTARTING = 3;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700192
Lucas Dupin51996bb2019-05-16 17:56:43 -0700193 private static final int BIOMETRIC_HELP_FINGERPRINT_NOT_RECOGNIZED = -1;
194 public static final int BIOMETRIC_HELP_FACE_NOT_RECOGNIZED = -2;
195
Adrian Roos0c859ae2015-11-23 16:47:50 -0800196 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
joshmccloskey7dee4542019-07-31 18:35:33 -0700197 /**
198 * If no cancel signal has been received after this amount of time, set the biometric running
199 * state to stopped to allow Keyguard to retry authentication.
200 */
201 private static final int DEFAULT_CANCEL_SIGNAL_TIMEOUT = 3000;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800202
Jorim Jaggi031f7952016-09-01 16:39:26 -0700203 private static final ComponentName FALLBACK_HOME_COMPONENT = new ComponentName(
204 "com.android.settings", "com.android.settings.FallbackHome");
205
Adrian Roosca8a2162017-08-17 19:00:58 +0200206
207 /**
208 * If true, the system is in the half-boot-to-decryption-screen state.
209 * Prudently disable lockscreen.
210 */
211 public static final boolean CORE_APPS_ONLY;
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700212
Adrian Roosca8a2162017-08-17 19:00:58 +0200213 static {
214 try {
215 CORE_APPS_ONLY = IPackageManager.Stub.asInterface(
216 ServiceManager.getService("package")).isOnlyCoreApps();
217 } catch (RemoteException e) {
218 throw e.rethrowFromSystemServer();
219 }
220 }
221
Jim Millerbbf1a742012-07-17 18:30:30 -0700222 private final Context mContext;
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800223 private final boolean mIsPrimaryUser;
Lucas Dupin10960bd2019-09-27 16:08:32 -0700224 HashMap<Integer, SimData> mSimDatas = new HashMap<>();
Etan Cohen47051d82015-07-06 16:19:04 -0700225 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700226
Jim Millerbbf1a742012-07-17 18:30:30 -0700227 private int mRingMode;
228 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400229 private boolean mKeyguardIsVisible;
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700230 private boolean mKeyguardGoingAway;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700231 private boolean mGoingToSleep;
Adrian Roosb6011622014-05-14 15:52:53 +0200232 private boolean mBouncer;
Lucas Dupine0516d52019-02-05 17:54:06 -0500233 private boolean mAuthInterruptActive;
Jorim Jaggi031f7952016-09-01 16:39:26 -0700234 private boolean mNeedsSlowUnlockTransition;
Jorim Jaggid11d1a92016-08-16 16:02:32 -0700235 private boolean mHasLockscreenWallpaper;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700236 private boolean mAssistantVisible;
237 private boolean mKeyguardOccluded;
Kevin Chyn6951d3d2019-06-10 14:07:13 -0700238 private boolean mSecureCameraLaunched;
Bill Linef81cbd2018-07-05 17:48:49 +0800239 @VisibleForTesting
240 protected boolean mTelephonyCapable;
Jim Millerbbf1a742012-07-17 18:30:30 -0700241
Jim Millerdcb3d842012-08-23 19:18:12 -0700242 // Device provisioning state
Jim Millerbbf1a742012-07-17 18:30:30 -0700243 private boolean mDeviceProvisioned;
244
Jim Millerdcb3d842012-08-23 19:18:12 -0700245 // Battery status
Jim Millerbbf1a742012-07-17 18:30:30 -0700246 private BatteryStatus mBatteryStatus;
247
Lucas Dupin3d053532019-01-29 12:35:22 -0800248 @VisibleForTesting
249 protected StrongAuthTracker mStrongAuthTracker;
Jim Millerbbf1a742012-07-17 18:30:30 -0700250
Jim Miller6212cc02012-09-05 17:35:31 -0700251 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700252 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800253 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700254
Brian Colonnaa5239892013-04-15 11:45:40 -0400255 private boolean mSwitchingUser;
256
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700257 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700258 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800259 private SubscriptionManager mSubscriptionManager;
260 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700261 private TrustManager mTrustManager;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700262 private UserManager mUserManager;
Lucas Dupin9e484aa2019-06-24 13:38:00 -0700263 private KeyguardBypassController mKeyguardBypassController;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200264 private int mFingerprintRunningState = BIOMETRIC_STATE_STOPPED;
265 private int mFaceRunningState = BIOMETRIC_STATE_STOPPED;
Michal Karpinskic52f8672016-11-18 11:32:45 +0000266 private LockPatternUtils mLockPatternUtils;
Kevin Chyn36778ff2017-09-07 19:55:38 -0700267 private final IDreamManager mDreamManager;
268 private boolean mIsDreaming;
Alex Chauff7653d2018-02-01 17:18:08 +0000269 private final DevicePolicyManager mDevicePolicyManager;
270 private boolean mLogoutEnabled;
Lucas Dupinca88e5f2019-05-14 16:11:08 -0700271 // If the user long pressed the lock icon, disabling face auth for the current session.
272 private boolean mLockIconPressed;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700273 private int mActiveMobileDataSubscription = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller20daffd2013-10-07 14:59:53 -0700274
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700275 /**
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200276 * Short delay before restarting biometric authentication after a successful try
277 * This should be slightly longer than the time between on<biometric>Authenticated
278 * (e.g. onFingerprintAuthenticated) and setKeyguardGoingAway(true).
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700279 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200280 private static final int BIOMETRIC_CONTINUE_DELAY_MS = 500;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700281
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700282 // If the HAL dies or is unable to authenticate, keyguard should retry after a short delay
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200283 private int mHardwareFingerprintUnavailableRetryCount = 0;
284 private int mHardwareFaceUnavailableRetryCount = 0;
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700285 private static final int HAL_ERROR_RETRY_TIMEOUT = 500; // ms
286 private static final int HAL_ERROR_RETRY_MAX = 10;
Kevin Chyn0c45b072017-04-24 16:27:11 -0700287
joshmccloskey7dee4542019-07-31 18:35:33 -0700288 private final Runnable mCancelNotReceived = new Runnable() {
289 @Override
290 public void run() {
291 Log.w(TAG, "Cancel not received, transitioning to STOPPED");
292 mFingerprintRunningState = mFaceRunningState = BIOMETRIC_STATE_STOPPED;
293 updateBiometricListeningState();
294 }
295 };
296
Dave Mankoffe2294692019-08-14 11:53:13 -0400297 private final Handler mHandler;
Jim Millerbbf1a742012-07-17 18:30:30 -0700298
Lucas Dupin7d95f152019-07-17 16:25:54 -0700299 private SparseBooleanArray mFaceSettingEnabledForUser = new SparseBooleanArray();
Kevin Chynb7b54a62018-09-28 18:48:12 -0700300 private BiometricManager mBiometricManager;
301 private IBiometricEnabledOnKeyguardCallback mBiometricEnabledCallback =
302 new IBiometricEnabledOnKeyguardCallback.Stub() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400303 @Override
304 public void onChanged(BiometricSourceType type, boolean enabled, int userId)
305 throws RemoteException {
306 if (type == BiometricSourceType.FACE) {
307 mFaceSettingEnabledForUser.put(userId, enabled);
308 updateFaceListeningState();
309 }
310 }
311 };
Kevin Chynb7b54a62018-09-28 18:48:12 -0700312
Malcolm Chen5c63b512019-08-13 13:24:07 -0700313 @VisibleForTesting
314 public PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
Fabian Kozynskiccde55d2019-06-26 10:06:09 -0400315 @Override
316 public void onActiveDataSubscriptionIdChanged(int subId) {
Malcolm Chen5c63b512019-08-13 13:24:07 -0700317 mActiveMobileDataSubscription = subId;
Fabian Kozynskiccde55d2019-06-26 10:06:09 -0400318 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
319 }
320 };
321
Wink Savilled09c4ca2014-11-22 10:08:16 -0800322 private OnSubscriptionsChangedListener mSubscriptionListener =
323 new OnSubscriptionsChangedListener() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400324 @Override
325 public void onSubscriptionsChanged() {
326 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
327 }
328 };
Jim Miller52a61332014-11-12 19:29:51 -0800329
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700330 private SparseBooleanArray mUserIsUnlocked = new SparseBooleanArray();
Adrian Roos46842d92014-03-27 14:58:03 +0100331 private SparseBooleanArray mUserHasTrust = new SparseBooleanArray();
Adrian Roos7861c662014-07-25 15:37:28 +0200332 private SparseBooleanArray mUserTrustIsManaged = new SparseBooleanArray();
Lucas Dupinf2c53502019-10-03 13:56:18 -0700333 private SparseBooleanArray mUserTrustIsUsuallyManaged = new SparseBooleanArray();
Jim Miller9f0753f2015-03-23 23:59:22 -0700334 private SparseBooleanArray mUserFingerprintAuthenticated = new SparseBooleanArray();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200335 private SparseBooleanArray mUserFaceAuthenticated = new SparseBooleanArray();
Adrian Roos4a410172014-08-20 17:41:44 +0200336 private SparseBooleanArray mUserFaceUnlockRunning = new SparseBooleanArray();
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800337 private Map<Integer, Intent> mSecondaryLockscreenRequirement = new HashMap<Integer, Intent>();
Adrian Roos46842d92014-03-27 14:58:03 +0100338
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700339 private static int sCurrentUser;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200340 private Runnable mUpdateBiometricListeningState = this::updateBiometricListeningState;
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700341
342 public synchronized static void setCurrentUser(int currentUser) {
343 sCurrentUser = currentUser;
344 }
345
346 public synchronized static int getCurrentUser() {
347 return sCurrentUser;
348 }
349
Adrian Roos46842d92014-03-27 14:58:03 +0100350 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700351 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200352 checkIsHandlerThread();
Adrian Roos46842d92014-03-27 14:58:03 +0100353 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200354 for (int i = 0; i < mCallbacks.size(); i++) {
355 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
356 if (cb != null) {
357 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700358 if (enabled && flags != 0) {
359 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200360 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200361 }
362 }
Adrian Roos46842d92014-03-27 14:58:03 +0100363 }
364
Lucas Dupinef886542018-01-03 16:03:07 -0800365 @Override
366 public void onTrustError(CharSequence message) {
367 dispatchErrorMessage(message);
368 }
369
Adrian Roos30a2ae62018-04-25 19:09:50 +0200370 private void handleSimSubscriptionInfoChanged() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400371 checkIsHandlerThread();
Jim Miller52a61332014-11-12 19:29:51 -0800372 if (DEBUG_SIM_STATES) {
373 Log.v(TAG, "onSubscriptionInfoChanged()");
Amit Mahajan75eab472020-01-29 10:47:48 -0800374 List<SubscriptionInfo> sil = mSubscriptionManager
375 .getActiveAndHiddenSubscriptionInfoList();
Wink Savilled09c4ca2014-11-22 10:08:16 -0800376 if (sil != null) {
377 for (SubscriptionInfo subInfo : sil) {
378 Log.v(TAG, "SubInfo:" + subInfo);
379 }
380 } else {
381 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800382 }
383 }
384 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
385
386 // Hack level over 9000: Because the subscription id is not yet valid when we see the
387 // first update in handleSimStateChange, we need to force refresh all all SIM states
388 // so the subscription id for them is consistent.
Richard Choue0381b82018-04-24 03:48:59 +0000389 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
390 for (int i = 0; i < subscriptionInfos.size(); i++) {
391 SubscriptionInfo info = subscriptionInfos.get(i);
392 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
393 if (changed) {
394 changedSubscriptions.add(info);
395 }
396 }
397 for (int i = 0; i < changedSubscriptions.size(); i++) {
398 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800399 for (int j = 0; j < mCallbacks.size(); j++) {
400 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
401 if (cb != null) {
402 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
403 }
404 }
405 }
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400406 callbacksRefreshCarrierInfo();
Jim Miller52a61332014-11-12 19:29:51 -0800407 }
408
Jason Monk052082c2015-06-11 11:35:23 -0400409 private void handleAirplaneModeChanged() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400410 callbacksRefreshCarrierInfo();
411 }
412
413 private void callbacksRefreshCarrierInfo() {
414 checkIsHandlerThread();
415 for (int i = 0; i < mCallbacks.size(); i++) {
416 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
Jason Monk052082c2015-06-11 11:35:23 -0400417 if (cb != null) {
418 cb.onRefreshCarrierInfo();
419 }
420 }
421 }
422
Malcolm Chen5c63b512019-08-13 13:24:07 -0700423 /**
424 * @return List of SubscriptionInfo records, maybe empty but never null.
425 */
Adrian Roos316bf542016-08-23 17:53:07 +0200426 public List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800427 List<SubscriptionInfo> sil = mSubscriptionInfo;
428 if (sil == null || forceReload) {
Amit Mahajan75eab472020-01-29 10:47:48 -0800429 sil = mSubscriptionManager.getActiveAndHiddenSubscriptionInfoList();
Wink Savilled09c4ca2014-11-22 10:08:16 -0800430 }
431 if (sil == null) {
Amit Mahajan75eab472020-01-29 10:47:48 -0800432 // getActiveAndHiddenSubscriptionInfoList was null callers expect an empty list.
Wink Savilled09c4ca2014-11-22 10:08:16 -0800433 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
434 } else {
435 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800436 }
Malcolm Chen5c63b512019-08-13 13:24:07 -0700437 return new ArrayList<>(mSubscriptionInfo);
438 }
439
440 /**
441 * This method returns filtered list of SubscriptionInfo from {@link #getSubscriptionInfo}.
442 * above. Maybe empty but never null.
443 *
444 * In DSDS mode if both subscriptions are grouped and one is opportunistic, we filter out one
445 * of them based on carrier config. e.g. In this case we should only show one carrier name
446 * on the status bar and quick settings.
447 */
448 public List<SubscriptionInfo> getFilteredSubscriptionInfo(boolean forceReload) {
449 List<SubscriptionInfo> subscriptions = getSubscriptionInfo(false);
Daniel Bright63514be2020-01-15 12:10:53 -0800450 if (subscriptions.size() == MODEM_COUNT_DUAL_MODEM) {
Malcolm Chen5c63b512019-08-13 13:24:07 -0700451 SubscriptionInfo info1 = subscriptions.get(0);
452 SubscriptionInfo info2 = subscriptions.get(1);
453 if (info1.getGroupUuid() != null && info1.getGroupUuid().equals(info2.getGroupUuid())) {
454 // If both subscriptions are primary, show both.
455 if (!info1.isOpportunistic() && !info2.isOpportunistic()) return subscriptions;
456
457 // If carrier required, always show signal bar of primary subscription.
458 // Otherwise, show whichever subscription is currently active for Internet.
459 boolean alwaysShowPrimary = CarrierConfigManager.getDefaultConfig()
460 .getBoolean(CarrierConfigManager
461 .KEY_ALWAYS_SHOW_PRIMARY_SIGNAL_BAR_IN_OPPORTUNISTIC_NETWORK_BOOLEAN);
462 if (alwaysShowPrimary) {
463 subscriptions.remove(info1.isOpportunistic() ? info1 : info2);
464 } else {
465 subscriptions.remove(info1.getSubscriptionId() == mActiveMobileDataSubscription
466 ? info2 : info1);
467 }
468
469 }
470 }
471
472 return subscriptions;
Jim Miller52a61332014-11-12 19:29:51 -0800473 }
474
Adrian Roos7861c662014-07-25 15:37:28 +0200475 @Override
476 public void onTrustManagedChanged(boolean managed, int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200477 checkIsHandlerThread();
Adrian Roos7861c662014-07-25 15:37:28 +0200478 mUserTrustIsManaged.put(userId, managed);
Lucas Dupinf2c53502019-10-03 13:56:18 -0700479 mUserTrustIsUsuallyManaged.put(userId, mTrustManager.isTrustUsuallyManaged(userId));
Adrian Roos7861c662014-07-25 15:37:28 +0200480 for (int i = 0; i < mCallbacks.size(); i++) {
481 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
482 if (cb != null) {
483 cb.onTrustManagedChanged(userId);
484 }
485 }
486 }
487
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700488 /**
489 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is goingAway
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700490 */
491 public void setKeyguardGoingAway(boolean goingAway) {
492 mKeyguardGoingAway = goingAway;
Kevin Chyne22f1342017-09-26 10:03:38 -0700493 updateFingerprintListeningState();
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700494 }
495
Kevin Chyn2fefd462017-04-28 12:18:19 -0700496 /**
497 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is occluded
Kevin Chyn2fefd462017-04-28 12:18:19 -0700498 */
499 public void setKeyguardOccluded(boolean occluded) {
500 mKeyguardOccluded = occluded;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200501 updateBiometricListeningState();
Kevin Chyn2fefd462017-04-28 12:18:19 -0700502 }
503
Kevin Chyn36778ff2017-09-07 19:55:38 -0700504 /**
Kevin Chyn6951d3d2019-06-10 14:07:13 -0700505 * Invoked when the secure camera is launched.
506 */
507 public void onCameraLaunched() {
508 mSecureCameraLaunched = true;
509 updateBiometricListeningState();
510 }
511
512 /**
Kevin Chyn36778ff2017-09-07 19:55:38 -0700513 * @return a cached version of DreamManager.isDreaming()
514 */
515 public boolean isDreaming() {
516 return mIsDreaming;
517 }
518
519 /**
520 * If the device is dreaming, awakens the device
521 */
522 public void awakenFromDream() {
523 if (mIsDreaming && mDreamManager != null) {
524 try {
525 mDreamManager.awaken();
526 } catch (RemoteException e) {
527 Log.e(TAG, "Unable to awaken from dream");
528 }
529 }
530 }
531
Lucas Dupin3d053532019-01-29 12:35:22 -0800532 @VisibleForTesting
533 protected void onFingerprintAuthenticated(int userId) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400534 checkIsHandlerThread();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700535 Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
Jim Miller9f0753f2015-03-23 23:59:22 -0700536 mUserFingerprintAuthenticated.put(userId, true);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700537 // Update/refresh trust state only if user can skip bouncer
538 if (getUserCanSkipBouncer(userId)) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200539 mTrustManager.unlockedByBiometricForUser(userId, BiometricSourceType.FINGERPRINT);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700540 }
Kevin Chyn625a0142017-04-10 14:53:59 -0700541 // Don't send cancel if authentication succeeds
542 mFingerprintCancelSignal = null;
Jim Millerf41fc962014-06-18 16:33:51 -0700543 for (int i = 0; i < mCallbacks.size(); i++) {
544 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
545 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200546 cb.onBiometricAuthenticated(userId, BiometricSourceType.FINGERPRINT);
Jim Millerf41fc962014-06-18 16:33:51 -0700547 }
548 }
Kevin Chyn2fefd462017-04-28 12:18:19 -0700549
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200550 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE),
551 BIOMETRIC_CONTINUE_DELAY_MS);
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700552
Kevin Chyn2fefd462017-04-28 12:18:19 -0700553 // Only authenticate fingerprint once when assistant is visible
554 mAssistantVisible = false;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700555
Nick Desaulniers1d396752016-07-25 15:05:33 -0700556 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700557 }
558
Jim Millerce7eb6d2015-04-03 19:29:13 -0700559 private void handleFingerprintAuthFailed() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400560 checkIsHandlerThread();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700561 for (int i = 0; i < mCallbacks.size(); i++) {
562 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
563 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200564 cb.onBiometricAuthFailed(BiometricSourceType.FINGERPRINT);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700565 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700566 }
Lucas Dupin51996bb2019-05-16 17:56:43 -0700567 handleFingerprintHelp(BIOMETRIC_HELP_FINGERPRINT_NOT_RECOGNIZED,
568 mContext.getString(R.string.kg_fingerprint_not_recognized));
Jim Millerce7eb6d2015-04-03 19:29:13 -0700569 }
Jim Millerf41fc962014-06-18 16:33:51 -0700570
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700571 private void handleFingerprintAcquired(int acquireInfo) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400572 checkIsHandlerThread();
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700573 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
574 return;
575 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700576 for (int i = 0; i < mCallbacks.size(); i++) {
577 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
578 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200579 cb.onBiometricAcquired(BiometricSourceType.FINGERPRINT);
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700580 }
581 }
582 }
583
Jim Miller837fa7e2016-08-08 20:16:22 -0700584 private void handleFingerprintAuthenticated(int authUserId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700585 Trace.beginSection("KeyGuardUpdateMonitor#handlerFingerPrintAuthenticated");
Jim Millerf41fc962014-06-18 16:33:51 -0700586 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700587 final int userId;
588 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800589 userId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700590 } catch (RemoteException e) {
591 Log.e(TAG, "Failed to get current user id: ", e);
592 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700593 }
Jim Miller837fa7e2016-08-08 20:16:22 -0700594 if (userId != authUserId) {
595 Log.d(TAG, "Fingerprint authenticated for wrong user: " + authUserId);
596 return;
597 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700598 if (isFingerprintDisabled(userId)) {
599 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
600 return;
601 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700602 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700603 } finally {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200604 setFingerprintRunningState(BIOMETRIC_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700605 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700606 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700607 }
608
Jim Miller9f0753f2015-03-23 23:59:22 -0700609 private void handleFingerprintHelp(int msgId, String helpString) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400610 checkIsHandlerThread();
Jim Millerf41fc962014-06-18 16:33:51 -0700611 for (int i = 0; i < mCallbacks.size(); i++) {
612 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
613 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200614 cb.onBiometricHelp(msgId, helpString, BiometricSourceType.FINGERPRINT);
Jim Miller9f0753f2015-03-23 23:59:22 -0700615 }
616 }
617 }
618
Kevin Chyn0c45b072017-04-24 16:27:11 -0700619 private Runnable mRetryFingerprintAuthentication = new Runnable() {
620 @Override
621 public void run() {
622 Log.w(TAG, "Retrying fingerprint after HW unavailable, attempt " +
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200623 mHardwareFingerprintUnavailableRetryCount);
Kevin Chyn0c45b072017-04-24 16:27:11 -0700624 updateFingerprintListeningState();
625 }
626 };
627
Jim Miller9f0753f2015-03-23 23:59:22 -0700628 private void handleFingerprintError(int msgId, String errString) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400629 checkIsHandlerThread();
joshmccloskey7dee4542019-07-31 18:35:33 -0700630 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED && mHandler.hasCallbacks(
631 mCancelNotReceived)) {
632 mHandler.removeCallbacks(mCancelNotReceived);
633 }
634
Jorim Jaggi86bed402015-08-20 18:20:02 -0700635 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200636 && mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
637 setFingerprintRunningState(BIOMETRIC_STATE_STOPPED);
Kevin Chyn1123ba72018-10-26 10:34:06 -0700638 updateFingerprintListeningState();
Jorim Jaggi86bed402015-08-20 18:20:02 -0700639 } else {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200640 setFingerprintRunningState(BIOMETRIC_STATE_STOPPED);
Jorim Jaggi86bed402015-08-20 18:20:02 -0700641 }
Kevin Chyn0c45b072017-04-24 16:27:11 -0700642
643 if (msgId == FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE) {
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700644 if (mHardwareFingerprintUnavailableRetryCount < HAL_ERROR_RETRY_MAX) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200645 mHardwareFingerprintUnavailableRetryCount++;
Kevin Chyn0c45b072017-04-24 16:27:11 -0700646 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700647 mHandler.postDelayed(mRetryFingerprintAuthentication, HAL_ERROR_RETRY_TIMEOUT);
Kevin Chyn0c45b072017-04-24 16:27:11 -0700648 }
649 }
650
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700651 if (msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT) {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700652 mLockPatternUtils.requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700653 getCurrentUser());
654 }
655
Jim Miller9f0753f2015-03-23 23:59:22 -0700656 for (int i = 0; i < mCallbacks.size(); i++) {
657 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
658 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200659 cb.onBiometricError(msgId, errString, BiometricSourceType.FINGERPRINT);
Jim Millerf41fc962014-06-18 16:33:51 -0700660 }
661 }
662 }
663
Jorim Jaggi3a464782015-08-28 16:59:13 -0700664 private void handleFingerprintLockoutReset() {
665 updateFingerprintListeningState();
666 }
667
Jorim Jaggi86bed402015-08-20 18:20:02 -0700668 private void setFingerprintRunningState(int fingerprintRunningState) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200669 boolean wasRunning = mFingerprintRunningState == BIOMETRIC_STATE_RUNNING;
670 boolean isRunning = fingerprintRunningState == BIOMETRIC_STATE_RUNNING;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700671 mFingerprintRunningState = fingerprintRunningState;
Kevin Chynb4514d22019-04-15 13:47:25 -0700672 Log.d(TAG, "fingerprintRunningState: " + mFingerprintRunningState);
Jorim Jaggi86bed402015-08-20 18:20:02 -0700673 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
Kevin Chynb4514d22019-04-15 13:47:25 -0700674 // asynchronousness of the cancel cycle. So only notify them if the actually running state
Jorim Jaggi86bed402015-08-20 18:20:02 -0700675 // has changed.
676 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700677 notifyFingerprintRunningStateChanged();
678 }
679 }
680
681 private void notifyFingerprintRunningStateChanged() {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200682 checkIsHandlerThread();
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700683 for (int i = 0; i < mCallbacks.size(); i++) {
684 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
685 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200686 cb.onBiometricRunningStateChanged(isFingerprintDetectionRunning(),
687 BiometricSourceType.FINGERPRINT);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700688 }
689 }
690 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200691
Lucas Dupin3d053532019-01-29 12:35:22 -0800692 @VisibleForTesting
693 protected void onFaceAuthenticated(int userId) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200694 Trace.beginSection("KeyGuardUpdateMonitor#onFaceAuthenticated");
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400695 checkIsHandlerThread();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200696 mUserFaceAuthenticated.put(userId, true);
697 // Update/refresh trust state only if user can skip bouncer
698 if (getUserCanSkipBouncer(userId)) {
699 mTrustManager.unlockedByBiometricForUser(userId, BiometricSourceType.FACE);
700 }
701 // Don't send cancel if authentication succeeds
702 mFaceCancelSignal = null;
703 for (int i = 0; i < mCallbacks.size(); i++) {
704 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
705 if (cb != null) {
706 cb.onBiometricAuthenticated(userId,
707 BiometricSourceType.FACE);
708 }
709 }
710
711 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE),
712 BIOMETRIC_CONTINUE_DELAY_MS);
713
714 // Only authenticate face once when assistant is visible
715 mAssistantVisible = false;
716
717 Trace.endSection();
718 }
719
720 private void handleFaceAuthFailed() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400721 checkIsHandlerThread();
Lucas Dupin38314812019-02-15 14:10:55 -0800722 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200723 for (int i = 0; i < mCallbacks.size(); i++) {
724 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
725 if (cb != null) {
726 cb.onBiometricAuthFailed(BiometricSourceType.FACE);
727 }
728 }
Lucas Dupin51996bb2019-05-16 17:56:43 -0700729 handleFaceHelp(BIOMETRIC_HELP_FACE_NOT_RECOGNIZED,
730 mContext.getString(R.string.kg_face_not_recognized));
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200731 }
732
733 private void handleFaceAcquired(int acquireInfo) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400734 checkIsHandlerThread();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200735 if (acquireInfo != FaceManager.FACE_ACQUIRED_GOOD) {
736 return;
737 }
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700738 if (DEBUG_FACE) Log.d(TAG, "Face acquired");
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200739 for (int i = 0; i < mCallbacks.size(); i++) {
740 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
741 if (cb != null) {
742 cb.onBiometricAcquired(BiometricSourceType.FACE);
743 }
744 }
745 }
746
747 private void handleFaceAuthenticated(int authUserId) {
748 Trace.beginSection("KeyGuardUpdateMonitor#handlerFaceAuthenticated");
749 try {
Lucas Dupin9bae9c22019-06-04 16:37:40 -0700750 if (mGoingToSleep) {
751 Log.d(TAG, "Aborted successful auth because device is going to sleep.");
752 return;
753 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200754 final int userId;
755 try {
756 userId = ActivityManager.getService().getCurrentUser().id;
757 } catch (RemoteException e) {
758 Log.e(TAG, "Failed to get current user id: ", e);
759 return;
760 }
761 if (userId != authUserId) {
762 Log.d(TAG, "Face authenticated for wrong user: " + authUserId);
763 return;
764 }
765 if (isFaceDisabled(userId)) {
766 Log.d(TAG, "Face authentication disabled by DPM for userId: " + userId);
767 return;
768 }
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700769 if (DEBUG_FACE) Log.d(TAG, "Face auth succeeded for user " + userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200770 onFaceAuthenticated(userId);
771 } finally {
772 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
773 }
774 Trace.endSection();
775 }
776
777 private void handleFaceHelp(int msgId, String helpString) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400778 checkIsHandlerThread();
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700779 if (DEBUG_FACE) Log.d(TAG, "Face help received: " + helpString);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200780 for (int i = 0; i < mCallbacks.size(); i++) {
781 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
782 if (cb != null) {
783 cb.onBiometricHelp(msgId, helpString, BiometricSourceType.FACE);
784 }
785 }
786 }
787
788 private Runnable mRetryFaceAuthentication = new Runnable() {
789 @Override
790 public void run() {
791 Log.w(TAG, "Retrying face after HW unavailable, attempt " +
792 mHardwareFaceUnavailableRetryCount);
793 updateFaceListeningState();
794 }
795 };
796
797 private void handleFaceError(int msgId, String errString) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400798 checkIsHandlerThread();
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700799 if (DEBUG_FACE) Log.d(TAG, "Face error received: " + errString);
joshmccloskey7dee4542019-07-31 18:35:33 -0700800 if (msgId == FaceManager.FACE_ERROR_CANCELED && mHandler.hasCallbacks(mCancelNotReceived)) {
801 mHandler.removeCallbacks(mCancelNotReceived);
802 }
803
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200804 if (msgId == FaceManager.FACE_ERROR_CANCELED
805 && mFaceRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
806 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
Kevin Chyn1123ba72018-10-26 10:34:06 -0700807 updateFaceListeningState();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200808 } else {
809 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
810 }
811
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700812 if (msgId == FaceManager.FACE_ERROR_HW_UNAVAILABLE
813 || msgId == FaceManager.FACE_ERROR_UNABLE_TO_PROCESS) {
814 if (mHardwareFaceUnavailableRetryCount < HAL_ERROR_RETRY_MAX) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200815 mHardwareFaceUnavailableRetryCount++;
816 mHandler.removeCallbacks(mRetryFaceAuthentication);
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700817 mHandler.postDelayed(mRetryFaceAuthentication, HAL_ERROR_RETRY_TIMEOUT);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200818 }
819 }
820
821 if (msgId == FaceManager.FACE_ERROR_LOCKOUT_PERMANENT) {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700822 mLockPatternUtils.requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200823 getCurrentUser());
824 }
825
826 for (int i = 0; i < mCallbacks.size(); i++) {
827 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
828 if (cb != null) {
829 cb.onBiometricError(msgId, errString,
830 BiometricSourceType.FACE);
831 }
832 }
833 }
834
835 private void handleFaceLockoutReset() {
836 updateFaceListeningState();
837 }
838
839 private void setFaceRunningState(int faceRunningState) {
840 boolean wasRunning = mFaceRunningState == BIOMETRIC_STATE_RUNNING;
841 boolean isRunning = faceRunningState == BIOMETRIC_STATE_RUNNING;
842 mFaceRunningState = faceRunningState;
Kevin Chynb4514d22019-04-15 13:47:25 -0700843 Log.d(TAG, "faceRunningState: " + mFaceRunningState);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200844 // Clients of KeyguardUpdateMonitor don't care about the internal state or about the
Kevin Chynb4514d22019-04-15 13:47:25 -0700845 // asynchronousness of the cancel cycle. So only notify them if the actually running state
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200846 // has changed.
847 if (wasRunning != isRunning) {
848 notifyFaceRunningStateChanged();
849 }
850 }
851
852 private void notifyFaceRunningStateChanged() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400853 checkIsHandlerThread();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200854 for (int i = 0; i < mCallbacks.size(); i++) {
855 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
856 if (cb != null) {
857 cb.onBiometricRunningStateChanged(isFaceDetectionRunning(),
858 BiometricSourceType.FACE);
859 }
860 }
861 }
862
Adrian Roos4a410172014-08-20 17:41:44 +0200863 private void handleFaceUnlockStateChanged(boolean running, int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200864 checkIsHandlerThread();
Adrian Roos4a410172014-08-20 17:41:44 +0200865 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200866 for (int i = 0; i < mCallbacks.size(); i++) {
867 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
868 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200869 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200870 }
871 }
872 }
873
Adrian Roos4a410172014-08-20 17:41:44 +0200874 public boolean isFaceUnlockRunning(int userId) {
875 return mUserFaceUnlockRunning.get(userId);
876 }
877
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700878 public boolean isFingerprintDetectionRunning() {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200879 return mFingerprintRunningState == BIOMETRIC_STATE_RUNNING;
880 }
881
882 public boolean isFaceDetectionRunning() {
883 return mFaceRunningState == BIOMETRIC_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700884 }
885
Jim Miller50e62182014-04-23 17:25:00 -0700886 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100887 // Don't allow trust agent if device is secured with a SIM PIN. This is here
888 // mainly because there's no other way to prompt the user to enter their SIM PIN
889 // once they get past the keyguard screen.
890 final boolean disabledBySimPin = isSimPinSecure();
891 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700892 }
893
Jim Miller06e34502014-07-17 14:46:05 -0700894 private boolean isFingerprintDisabled(int userId) {
895 final DevicePolicyManager dpm =
896 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
897 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Dave Mankoff4b0ab652019-08-07 09:49:20 -0400898 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
Adrian Roos733b6632015-08-21 14:32:35 -0700899 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700900 }
901
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200902 private boolean isFaceDisabled(int userId) {
903 final DevicePolicyManager dpm =
904 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
Lucas Dupin8968f6a2019-08-09 17:41:15 -0700905 // TODO(b/140035044)
906 return whitelistIpcs(() -> dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200907 & DevicePolicyManager.KEYGUARD_DISABLE_FACE) != 0
Lucas Dupin8968f6a2019-08-09 17:41:15 -0700908 || isSimPinSecure());
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200909 }
910
911
Selim Cineke8bae622015-07-15 13:24:06 -0700912 public boolean getUserCanSkipBouncer(int userId) {
Steven Wucfe398d2019-03-21 11:32:15 -0400913 return getUserHasTrust(userId) || getUserUnlockedWithBiometric(userId);
Selim Cineke8bae622015-07-15 13:24:06 -0700914 }
915
Adrian Roos46842d92014-03-27 14:58:03 +0100916 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700917 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100918 }
919
Steven Wucfe398d2019-03-21 11:32:15 -0400920 /**
921 * Returns whether the user is unlocked with biometrics.
922 */
923 public boolean getUserUnlockedWithBiometric(int userId) {
924 boolean fingerprintOrFace = mUserFingerprintAuthenticated.get(userId)
925 || mUserFaceAuthenticated.get(userId);
926 return fingerprintOrFace && isUnlockingWithBiometricAllowed();
927 }
928
Adrian Roos7861c662014-07-25 15:37:28 +0200929 public boolean getUserTrustIsManaged(int userId) {
930 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
931 }
932
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800933 private void updateSecondaryLockscreenRequirement(int userId) {
934 Intent oldIntent = mSecondaryLockscreenRequirement.get(userId);
935 boolean enabled = mDevicePolicyManager.isSecondaryLockscreenEnabled(userId);
936 boolean changed = false;
937
938 if (enabled && (oldIntent == null)) {
939 ResolveInfo resolveInfo =
940 mContext.getPackageManager().resolveService(
941 new Intent(
942 DevicePolicyManager.ACTION_BIND_SECONDARY_LOCKSCREEN_SERVICE),
943 0);
944 if (resolveInfo != null) {
945 Intent newIntent = new Intent();
946 newIntent.setComponent(resolveInfo.serviceInfo.getComponentName());
947 mSecondaryLockscreenRequirement.put(userId, newIntent);
948 changed = true;
949 }
950 } else if (!enabled && (oldIntent != null)) {
951 mSecondaryLockscreenRequirement.put(userId, null);
952 changed = true;
953 }
954 if (changed) {
955 for (int i = 0; i < mCallbacks.size(); i++) {
956 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
957 if (cb != null) {
958 cb.onSecondaryLockscreenRequirementChanged(userId);
959 }
960 }
961 }
962 }
963
964 /**
965 * Returns an Intent by which to bind to a service that will provide additional security screen
966 * content that must be shown prior to dismissing the keyguard for this user.
967 */
968 public Intent getSecondaryLockscreenRequirement(int userId) {
969 return mSecondaryLockscreenRequirement.get(userId);
970 }
971
Lucas Dupinf2c53502019-10-03 13:56:18 -0700972 /**
973 * Cached version of {@link TrustManager#isTrustUsuallyManaged(int)}.
974 */
975 public boolean isTrustUsuallyManaged(int userId) {
976 checkIsHandlerThread();
977 return mUserTrustIsUsuallyManaged.get(userId);
978 }
979
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200980 public boolean isUnlockingWithBiometricAllowed() {
981 return mStrongAuthTracker.isUnlockingWithBiometricAllowed();
Adrian Roosb5e47222015-08-14 15:53:06 -0700982 }
983
Lucas Dupin16013822018-05-17 18:00:16 -0700984 public boolean isUserInLockdown(int userId) {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700985 return containsFlag(mStrongAuthTracker.getStrongAuthForUser(userId),
986 STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
Lucas Dupin16013822018-05-17 18:00:16 -0700987 }
988
Lucas Dupin7825b942019-06-03 20:22:39 -0700989 public boolean userNeedsStrongAuth() {
990 return mStrongAuthTracker.getStrongAuthForUser(getCurrentUser())
991 != LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
992 }
993
Lucas Dupin8eec2682019-07-01 16:41:17 -0700994 private boolean containsFlag(int haystack, int needle) {
995 return (haystack & needle) != 0;
996 }
997
Jorim Jaggi031f7952016-09-01 16:39:26 -0700998 public boolean needsSlowUnlockTransition() {
999 return mNeedsSlowUnlockTransition;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001000 }
1001
Adrian Roosb5e47222015-08-14 15:53:06 -07001002 public StrongAuthTracker getStrongAuthTracker() {
1003 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001004 }
1005
Adrian Roos1de8bcb2015-08-19 16:52:52 -07001006 private void notifyStrongAuthStateChanged(int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001007 checkIsHandlerThread();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001008 for (int i = 0; i < mCallbacks.size(); i++) {
1009 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1010 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -07001011 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001012 }
1013 }
Selim Cinek1fcafc42015-07-20 14:39:25 -07001014 }
1015
Adrian Roos91ba3072017-02-14 16:50:46 +01001016 public boolean isScreenOn() {
1017 return mScreenOn;
1018 }
1019
Lucas Dupinef886542018-01-03 16:03:07 -08001020 private void dispatchErrorMessage(CharSequence message) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001021 checkIsHandlerThread();
Lucas Dupinef886542018-01-03 16:03:07 -08001022 for (int i = 0; i < mCallbacks.size(); i++) {
1023 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1024 if (cb != null) {
1025 cb.onTrustAgentErrorMessage(message);
1026 }
1027 }
1028 }
1029
Lucas Dupin3d053532019-01-29 12:35:22 -08001030 @VisibleForTesting
1031 void setAssistantVisible(boolean assistantVisible) {
1032 mAssistantVisible = assistantVisible;
1033 updateBiometricListeningState();
1034 }
1035
Jim Miller8f09fd22013-03-14 19:04:28 -07001036 static class DisplayClientState {
1037 public int clientGeneration;
1038 public boolean clearing;
1039 public PendingIntent intent;
1040 public int playbackState;
1041 public long playbackEventTime;
1042 }
1043
1044 private DisplayClientState mDisplayClientState = new DisplayClientState();
1045
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001046 @VisibleForTesting
Lucas Dupin7ff82b02018-05-16 12:14:10 -07001047 protected final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001048
Jim Millerd72d5ac2015-09-29 18:55:32 -07001049 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -07001050 public void onReceive(Context context, Intent intent) {
1051 final String action = intent.getAction();
1052 if (DEBUG) Log.d(TAG, "received broadcast " + action);
1053
1054 if (Intent.ACTION_TIME_TICK.equals(action)
Robert Snoeberger9c1074f2018-12-07 17:35:21 -05001055 || Intent.ACTION_TIME_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -07001056 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Robert Snoeberger9c1074f2018-12-07 17:35:21 -05001057 } else if (Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
1058 final Message msg = mHandler.obtainMessage(
1059 MSG_TIMEZONE_UPDATE, intent.getStringExtra("time-zone"));
1060 mHandler.sendMessage(msg);
Jim Millerbbf1a742012-07-17 18:30:30 -07001061 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
1062 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
1063 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
1064 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
1065 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -08001066
1067 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
1068 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
1069 final int maxChargingMicroWatt;
1070
1071 if (maxChargingMicroVolt <= 0) {
1072 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
1073 }
1074 if (maxChargingMicroAmp > 0) {
1075 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
1076 // to maintain precision equally on both factors.
1077 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
1078 * (maxChargingMicroVolt / 1000);
1079 } else {
1080 maxChargingMicroWatt = -1;
1081 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001082 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -07001083 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -08001084 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -07001085 mHandler.sendMessage(msg);
Daniel Bright350f7f92020-01-22 14:24:11 -08001086 } else if (Intent.ACTION_SIM_STATE_CHANGED.equals(action)) {
Bill Linef81cbd2018-07-05 17:48:49 +08001087 SimData args = SimData.fromIntent(intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001088 // ACTION_SIM_STATE_CHANGED is rebroadcast after unlocking the device to
1089 // keep compatibility with apps that aren't direct boot aware.
1090 // SysUI should just ignore this broadcast because it was already received
1091 // and processed previously.
Meng Wang0e8d91a2020-01-17 14:58:01 -08001092 if (intent.getBooleanExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, false)) {
Bill Linef81cbd2018-07-05 17:48:49 +08001093 // Guarantee mTelephonyCapable state after SysUI crash and restart
Jayachandran Cf5436a62019-11-08 18:22:45 -08001094 if (args.simState == TelephonyManager.SIM_STATE_ABSENT) {
Bill Linef81cbd2018-07-05 17:48:49 +08001095 mHandler.obtainMessage(MSG_TELEPHONY_CAPABLE, true).sendToTarget();
1096 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001097 return;
1098 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001099 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -08001100 Log.v(TAG, "action " + action
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001101 + " state: " + intent.getStringExtra(
Jayachandran C0173df82019-12-19 12:11:03 -08001102 Intent.EXTRA_SIM_STATE)
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001103 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -07001104 }
Jim Miller52a61332014-11-12 19:29:51 -08001105 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
1106 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -07001107 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
1108 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
1109 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
1110 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
1111 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
1112 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -04001113 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
1114 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Jayachandran C041e7692019-12-20 16:20:02 -08001115 } else if (Intent.ACTION_SERVICE_STATE.equals(action)) {
Etan Cohen47051d82015-07-06 16:19:04 -07001116 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
Daniel Bright63514be2020-01-15 12:10:53 -08001117 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
Etan Cohen47051d82015-07-06 16:19:04 -07001118 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
1119 if (DEBUG) {
1120 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
1121 + subId);
1122 }
Bonian Chena7e9e8c2019-06-02 23:59:31 +00001123 mHandler.sendMessage(
1124 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Daniel Bright350f7f92020-01-22 14:24:11 -08001125 } else if (TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED.equals(action)) {
Fabian Kozynskiccde55d2019-06-26 10:06:09 -04001126 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
Alex Chauff7653d2018-02-01 17:18:08 +00001127 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(
1128 action)) {
1129 mHandler.sendEmptyMessage(MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001130 }
1131 }
1132 };
Jim Miller2de5ee82012-06-14 22:22:50 -07001133
Lucas Dupin3d053532019-01-29 12:35:22 -08001134 @VisibleForTesting
1135 protected final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001136
Jim Millerd72d5ac2015-09-29 18:55:32 -07001137 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001138 public void onReceive(Context context, Intent intent) {
1139 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +02001140 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
1141 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
1142 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001143 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
1144 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +02001145 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001146 Trace.beginSection(
1147 "KeyguardUpdateMonitor.mBroadcastAllReceiver#onReceive "
1148 + "ACTION_FACE_UNLOCK_STARTED");
Adrian Roos48c796c2014-09-01 14:59:23 +02001149 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
1150 getSendingUserId()));
Nick Desaulniers1d396752016-07-25 15:05:33 -07001151 Trace.endSection();
Adrian Roos48c796c2014-09-01 14:59:23 +02001152 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
1153 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
1154 getSendingUserId()));
1155 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
1156 .equals(action)) {
Yvonne Jiangb7024a22019-12-05 16:57:08 -08001157 mHandler.sendMessage(mHandler.obtainMessage(MSG_DPM_STATE_CHANGED,
1158 getSendingUserId()));
Jorim Jaggidadafd42016-09-30 07:20:25 -07001159 } else if (ACTION_USER_UNLOCKED.equals(action)) {
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001160 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_UNLOCKED,
1161 getSendingUserId(), 0));
1162 } else if (ACTION_USER_STOPPED.equals(action)) {
1163 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_STOPPED,
1164 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1), 0));
1165 } else if (ACTION_USER_REMOVED.equals(action)) {
1166 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_REMOVED,
1167 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1), 0));
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001168 }
1169 }
1170 };
Jim Miller9f0753f2015-03-23 23:59:22 -07001171
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001172 private final FingerprintManager.LockoutResetCallback mFingerprintLockoutResetCallback
Jorim Jaggi3a464782015-08-28 16:59:13 -07001173 = new FingerprintManager.LockoutResetCallback() {
1174 @Override
1175 public void onLockoutReset() {
1176 handleFingerprintLockoutReset();
1177 }
1178 };
1179
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001180 private final FaceManager.LockoutResetCallback mFaceLockoutResetCallback
1181 = new FaceManager.LockoutResetCallback() {
1182 @Override
1183 public void onLockoutReset() {
1184 handleFaceLockoutReset();
1185 }
1186 };
1187
1188 private FingerprintManager.AuthenticationCallback mFingerprintAuthenticationCallback
Jim Miller9f0753f2015-03-23 23:59:22 -07001189 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -07001190
1191 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -07001192 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -07001193 handleFingerprintAuthFailed();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001194 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001195
1196 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -07001197 public void onAuthenticationSucceeded(AuthenticationResult result) {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001198 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
Jim Miller837fa7e2016-08-08 20:16:22 -07001199 handleFingerprintAuthenticated(result.getUserId());
Nick Desaulniers1d396752016-07-25 15:05:33 -07001200 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -07001201 }
1202
1203 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -07001204 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -07001205 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -07001206 }
1207
1208 @Override
1209 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -07001210 handleFingerprintError(errMsgId, errString.toString());
1211 }
1212
1213 @Override
1214 public void onAuthenticationAcquired(int acquireInfo) {
1215 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -07001216 }
1217 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001218
Lucas Dupin3d053532019-01-29 12:35:22 -08001219 @VisibleForTesting
1220 FaceManager.AuthenticationCallback mFaceAuthenticationCallback
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001221 = new FaceManager.AuthenticationCallback() {
1222
1223 @Override
1224 public void onAuthenticationFailed() {
1225 handleFaceAuthFailed();
1226 }
1227
1228 @Override
1229 public void onAuthenticationSucceeded(FaceManager.AuthenticationResult result) {
1230 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
1231 handleFaceAuthenticated(result.getUserId());
1232 Trace.endSection();
1233 }
1234
1235 @Override
1236 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
1237 handleFaceHelp(helpMsgId, helpString.toString());
1238 }
1239
1240 @Override
1241 public void onAuthenticationError(int errMsgId, CharSequence errString) {
1242 handleFaceError(errMsgId, errString.toString());
1243 }
1244
1245 @Override
1246 public void onAuthenticationAcquired(int acquireInfo) {
1247 handleFaceAcquired(acquireInfo);
1248 }
1249 };
1250
Jim Miller9f0753f2015-03-23 23:59:22 -07001251 private CancellationSignal mFingerprintCancelSignal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001252 private CancellationSignal mFaceCancelSignal;
Jim Miller9f0753f2015-03-23 23:59:22 -07001253 private FingerprintManager mFpm;
Kevin Chynb7b54a62018-09-28 18:48:12 -07001254 private FaceManager mFaceManager;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001255
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001256 /**
Jim Miller47088bb2009-11-24 00:40:16 -08001257 * When we receive a
1258 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -07001259 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001260 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -08001261 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001262 */
Jim Miller52a61332014-11-12 19:29:51 -08001263 private static class SimData {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001264 public int simState;
Jim Miller52a61332014-11-12 19:29:51 -08001265 public int slotId;
1266 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001267
Jayachandran Cf5436a62019-11-08 18:22:45 -08001268 SimData(int state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -08001269 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -08001270 slotId = slot;
1271 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -08001272 }
1273
Jim Miller52a61332014-11-12 19:29:51 -08001274 static SimData fromIntent(Intent intent) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001275 int state;
Daniel Bright350f7f92020-01-22 14:24:11 -08001276 if (!Intent.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001277 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
1278 }
Jayachandran C0173df82019-12-19 12:11:03 -08001279 String stateExtra = intent.getStringExtra(Intent.EXTRA_SIM_STATE);
Daniel Bright63514be2020-01-15 12:10:53 -08001280 int slotId = intent.getIntExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 0);
1281 int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
Wink Savilled09c4ca2014-11-22 10:08:16 -08001282 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Jayachandran C0173df82019-12-19 12:11:03 -08001283 if (Intent.SIM_STATE_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -07001284 final String absentReason = intent
Jayachandran C0173df82019-12-19 12:11:03 -08001285 .getStringExtra(Intent.EXTRA_SIM_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -07001286
Jayachandran C0173df82019-12-19 12:11:03 -08001287 if (Intent.SIM_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -07001288 absentReason)) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001289 state = TelephonyManager.SIM_STATE_PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -07001290 } else {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001291 state = TelephonyManager.SIM_STATE_ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -07001292 }
Jayachandran C0173df82019-12-19 12:11:03 -08001293 } else if (Intent.SIM_STATE_READY.equals(stateExtra)) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001294 state = TelephonyManager.SIM_STATE_READY;
Jayachandran C0173df82019-12-19 12:11:03 -08001295 } else if (Intent.SIM_STATE_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001296 final String lockedReason = intent
Jayachandran C0173df82019-12-19 12:11:03 -08001297 .getStringExtra(Intent.EXTRA_SIM_LOCKED_REASON);
1298 if (Intent.SIM_LOCKED_ON_PIN.equals(lockedReason)) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001299 state = TelephonyManager.SIM_STATE_PIN_REQUIRED;
Jayachandran C0173df82019-12-19 12:11:03 -08001300 } else if (Intent.SIM_LOCKED_ON_PUK.equals(lockedReason)) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001301 state = TelephonyManager.SIM_STATE_PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001302 } else {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001303 state = TelephonyManager.SIM_STATE_UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001304 }
Jayachandran C0173df82019-12-19 12:11:03 -08001305 } else if (Intent.SIM_LOCKED_NETWORK.equals(stateExtra)) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001306 state = TelephonyManager.SIM_STATE_NETWORK_LOCKED;
Jayachandran C0173df82019-12-19 12:11:03 -08001307 } else if (Intent.SIM_STATE_CARD_IO_ERROR.equals(stateExtra)) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001308 state = TelephonyManager.SIM_STATE_CARD_IO_ERROR;
Jayachandran C0173df82019-12-19 12:11:03 -08001309 } else if (Intent.SIM_STATE_LOADED.equals(stateExtra)
1310 || Intent.SIM_STATE_IMSI.equals(stateExtra)) {
Jim Miller109f1fd2012-09-19 20:44:16 -07001311 // This is required because telephony doesn't return to "READY" after
1312 // these state transitions. See bug 7197471.
Jayachandran Cf5436a62019-11-08 18:22:45 -08001313 state = TelephonyManager.SIM_STATE_READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001314 } else {
Jayachandran Cf5436a62019-11-08 18:22:45 -08001315 state = TelephonyManager.SIM_STATE_UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001316 }
Jim Miller52a61332014-11-12 19:29:51 -08001317 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001318 }
1319
Jim Millerd72d5ac2015-09-29 18:55:32 -07001320 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001321 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -08001322 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001323 }
1324 }
1325
Adrian Roos12c1ef52014-06-04 13:54:08 +02001326 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -07001327 public static final int CHARGING_UNKNOWN = -1;
1328 public static final int CHARGING_SLOWLY = 0;
1329 public static final int CHARGING_REGULAR = 1;
1330 public static final int CHARGING_FAST = 2;
1331
Jim Miller16464b82011-10-20 21:10:13 -07001332 public final int status;
1333 public final int level;
1334 public final int plugged;
1335 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -08001336 public final int maxChargingWattage;
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001337
Adrian Roos0c859ae2015-11-23 16:47:50 -08001338 public BatteryStatus(int status, int level, int plugged, int health,
1339 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -07001340 this.status = status;
1341 this.level = level;
1342 this.plugged = plugged;
1343 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -08001344 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -07001345 }
1346
Jim Millerbbf1a742012-07-17 18:30:30 -07001347 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -07001348 * Determine whether the device is plugged in (USB, power, or wireless).
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001349 *
Jim Millerbbf1a742012-07-17 18:30:30 -07001350 * @return true if the device is plugged in.
1351 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +01001352 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001353 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -07001354 || plugged == BatteryManager.BATTERY_PLUGGED_USB
1355 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -07001356 }
1357
1358 /**
Beverly2034c832018-03-19 11:18:51 -04001359 * Determine whether the device is plugged in (USB, power).
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001360 *
Beverly2034c832018-03-19 11:18:51 -04001361 * @return true if the device is plugged in wired (as opposed to wireless)
1362 */
1363 public boolean isPluggedInWired() {
1364 return plugged == BatteryManager.BATTERY_PLUGGED_AC
1365 || plugged == BatteryManager.BATTERY_PLUGGED_USB;
1366 }
1367
1368 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001369 * Whether or not the device is charged. Note that some devices never return 100% for
1370 * battery level, so this allows either battery level or status to determine if the
1371 * battery is charged.
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001372 *
Jim Millerbbf1a742012-07-17 18:30:30 -07001373 * @return true if the device is charged
1374 */
1375 public boolean isCharged() {
1376 return status == BATTERY_STATUS_FULL || level >= 100;
1377 }
1378
1379 /**
1380 * Whether battery is low and needs to be charged.
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001381 *
Jim Millerbbf1a742012-07-17 18:30:30 -07001382 * @return true if battery is low
1383 */
1384 public boolean isBatteryLow() {
1385 return level < LOW_BATTERY_THRESHOLD;
1386 }
1387
Adrian Roos7b043112015-07-10 13:00:33 -07001388 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -08001389 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
1390 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001391 maxChargingWattage > fastThreshold ? CHARGING_FAST :
1392 CHARGING_REGULAR;
Adrian Roos7b043112015-07-10 13:00:33 -07001393 }
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001394
1395 @Override
1396 public String toString() {
1397 return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged
1398 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}";
1399 }
Jim Miller16464b82011-10-20 21:10:13 -07001400 }
1401
Lucas Dupin3d053532019-01-29 12:35:22 -08001402 public static class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
1403 private final Consumer<Integer> mStrongAuthRequiredChangedCallback;
1404
1405 public StrongAuthTracker(Context context,
1406 Consumer<Integer> strongAuthRequiredChangedCallback) {
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001407 super(context);
Lucas Dupin3d053532019-01-29 12:35:22 -08001408 mStrongAuthRequiredChangedCallback = strongAuthRequiredChangedCallback;
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001409 }
Adrian Roosb5e47222015-08-14 15:53:06 -07001410
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001411 public boolean isUnlockingWithBiometricAllowed() {
Adrian Roosb5e47222015-08-14 15:53:06 -07001412 int userId = getCurrentUser();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001413 return isBiometricAllowedForUser(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -07001414 }
1415
1416 public boolean hasUserAuthenticatedSinceBoot() {
1417 int userId = getCurrentUser();
1418 return (getStrongAuthForUser(userId)
1419 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
1420 }
1421
1422 @Override
1423 public void onStrongAuthRequiredChanged(int userId) {
Lucas Dupin3d053532019-01-29 12:35:22 -08001424 mStrongAuthRequiredChangedCallback.accept(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -07001425 }
1426 }
1427
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001428 protected void handleStartedWakingUp() {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001429 Trace.beginSection("KeyguardUpdateMonitor#handleStartedWakingUp");
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001430 checkIsHandlerThread();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001431 updateBiometricListeningState();
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001432 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Miller20daffd2013-10-07 14:59:53 -07001433 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1434 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001435 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -07001436 }
1437 }
Nick Desaulniers1d396752016-07-25 15:05:33 -07001438 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -07001439 }
1440
Jorim Jaggi95e40382015-09-16 15:53:42 -07001441 protected void handleStartedGoingToSleep(int arg1) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001442 checkIsHandlerThread();
Curtis Belmonte6a0634d2019-08-19 13:13:01 -07001443 mLockIconPressed = false;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001444 clearBiometricRecognized();
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001445 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Miller20daffd2013-10-07 14:59:53 -07001446 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1447 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001448 cb.onStartedGoingToSleep(arg1);
1449 }
1450 }
1451 mGoingToSleep = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001452 updateBiometricListeningState();
Jorim Jaggi95e40382015-09-16 15:53:42 -07001453 }
1454
1455 protected void handleFinishedGoingToSleep(int arg1) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001456 checkIsHandlerThread();
Jorim Jaggi95e40382015-09-16 15:53:42 -07001457 mGoingToSleep = false;
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001458 for (int i = 0; i < mCallbacks.size(); i++) {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001459 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1460 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001461 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -07001462 }
1463 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001464 updateBiometricListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001465 }
1466
Jorim Jaggif1518da2015-07-30 11:56:36 -07001467 private void handleScreenTurnedOn() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001468 checkIsHandlerThread();
1469 for (int i = 0; i < mCallbacks.size(); i++) {
Jorim Jaggif1518da2015-07-30 11:56:36 -07001470 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1471 if (cb != null) {
1472 cb.onScreenTurnedOn();
1473 }
1474 }
1475 }
1476
1477 private void handleScreenTurnedOff() {
Lucas Dupind236ee32019-10-08 15:33:59 -07001478 final String tag = "KeyguardUpdateMonitor#handleScreenTurnedOff";
1479 DejankUtils.startDetectingBlockingIpcs(tag);
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001480 checkIsHandlerThread();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001481 mHardwareFingerprintUnavailableRetryCount = 0;
1482 mHardwareFaceUnavailableRetryCount = 0;
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001483 for (int i = 0; i < mCallbacks.size(); i++) {
Jorim Jaggif1518da2015-07-30 11:56:36 -07001484 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1485 if (cb != null) {
1486 cb.onScreenTurnedOff();
1487 }
1488 }
Lucas Dupind236ee32019-10-08 15:33:59 -07001489 DejankUtils.stopDetectingBlockingIpcs(tag);
Jorim Jaggif1518da2015-07-30 11:56:36 -07001490 }
1491
Selim Cinek99415392016-09-09 14:58:41 -07001492 private void handleDreamingStateChanged(int dreamStart) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001493 checkIsHandlerThread();
Kevin Chyn36778ff2017-09-07 19:55:38 -07001494 mIsDreaming = dreamStart == 1;
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001495 for (int i = 0; i < mCallbacks.size(); i++) {
Selim Cinek99415392016-09-09 14:58:41 -07001496 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1497 if (cb != null) {
Kevin Chyn36778ff2017-09-07 19:55:38 -07001498 cb.onDreamingStateChanged(mIsDreaming);
Selim Cinek99415392016-09-09 14:58:41 -07001499 }
1500 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001501 updateBiometricListeningState();
Selim Cinek99415392016-09-09 14:58:41 -07001502 }
1503
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001504 private void handleUserInfoChanged(int userId) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001505 checkIsHandlerThread();
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001506 for (int i = 0; i < mCallbacks.size(); i++) {
1507 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1508 if (cb != null) {
1509 cb.onUserInfoChanged(userId);
1510 }
1511 }
1512 }
1513
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001514 private void handleUserUnlocked(int userId) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001515 checkIsHandlerThread();
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001516 mUserIsUnlocked.put(userId, true);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001517 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1518 for (int i = 0; i < mCallbacks.size(); i++) {
1519 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1520 if (cb != null) {
1521 cb.onUserUnlocked();
1522 }
1523 }
1524 }
1525
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001526 private void handleUserStopped(int userId) {
1527 checkIsHandlerThread();
1528 mUserIsUnlocked.put(userId, mUserManager.isUserUnlocked(userId));
1529 }
1530
Lucas Dupinf2c53502019-10-03 13:56:18 -07001531 @VisibleForTesting
1532 void handleUserRemoved(int userId) {
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001533 checkIsHandlerThread();
1534 mUserIsUnlocked.delete(userId);
Lucas Dupinf2c53502019-10-03 13:56:18 -07001535 mUserTrustIsUsuallyManaged.delete(userId);
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001536 }
1537
Lucas Dupin7517b5d2017-08-22 12:51:25 -07001538 @VisibleForTesting
Dave Mankoffe2294692019-08-14 11:53:13 -04001539 @Inject
Dave Mankoff00e8a2f2019-12-18 16:59:49 -05001540 protected KeyguardUpdateMonitor(Context context, @Main Looper mainLooper,
Fabian Kozynski5ca7a512019-10-16 19:56:11 +00001541 BroadcastDispatcher broadcastDispatcher,
Lucas Dupin64171fe2019-10-30 14:28:29 -07001542 DumpController dumpController) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001543 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -08001544 mSubscriptionManager = SubscriptionManager.from(context);
Michael Jurkafff56142012-11-28 16:51:00 -08001545 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Lucas Dupin3d053532019-01-29 12:35:22 -08001546 mStrongAuthTracker = new StrongAuthTracker(context, this::notifyStrongAuthStateChanged);
Lucas Dupin64171fe2019-10-30 14:28:29 -07001547 dumpController.registerDumpable(this);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001548
Dave Mankoffe2294692019-08-14 11:53:13 -04001549 mHandler = new Handler(mainLooper) {
1550 @Override
1551 public void handleMessage(Message msg) {
1552 switch (msg.what) {
1553 case MSG_TIME_UPDATE:
1554 handleTimeUpdate();
1555 break;
1556 case MSG_TIMEZONE_UPDATE:
1557 handleTimeZoneUpdate((String) msg.obj);
1558 break;
1559 case MSG_BATTERY_UPDATE:
1560 handleBatteryUpdate((BatteryStatus) msg.obj);
1561 break;
1562 case MSG_SIM_STATE_CHANGE:
Jayachandran Cf5436a62019-11-08 18:22:45 -08001563 handleSimStateChange(msg.arg1, msg.arg2, (int) msg.obj);
Dave Mankoffe2294692019-08-14 11:53:13 -04001564 break;
1565 case MSG_RINGER_MODE_CHANGED:
1566 handleRingerModeChange(msg.arg1);
1567 break;
1568 case MSG_PHONE_STATE_CHANGED:
1569 handlePhoneStateChanged((String) msg.obj);
1570 break;
1571 case MSG_DEVICE_PROVISIONED:
1572 handleDeviceProvisioned();
1573 break;
1574 case MSG_DPM_STATE_CHANGED:
Yvonne Jiangb7024a22019-12-05 16:57:08 -08001575 handleDevicePolicyManagerStateChanged(msg.arg1);
Dave Mankoffe2294692019-08-14 11:53:13 -04001576 break;
1577 case MSG_USER_SWITCHING:
1578 handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
1579 break;
1580 case MSG_USER_SWITCH_COMPLETE:
1581 handleUserSwitchComplete(msg.arg1);
1582 break;
1583 case MSG_KEYGUARD_RESET:
1584 handleKeyguardReset();
1585 break;
1586 case MSG_KEYGUARD_BOUNCER_CHANGED:
1587 handleKeyguardBouncerChanged(msg.arg1);
1588 break;
Dave Mankoffe2294692019-08-14 11:53:13 -04001589 case MSG_USER_INFO_CHANGED:
1590 handleUserInfoChanged(msg.arg1);
1591 break;
1592 case MSG_REPORT_EMERGENCY_CALL_ACTION:
1593 handleReportEmergencyCallAction();
1594 break;
1595 case MSG_STARTED_GOING_TO_SLEEP:
1596 handleStartedGoingToSleep(msg.arg1);
1597 break;
1598 case MSG_FINISHED_GOING_TO_SLEEP:
1599 handleFinishedGoingToSleep(msg.arg1);
1600 break;
1601 case MSG_STARTED_WAKING_UP:
1602 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_STARTED_WAKING_UP");
1603 handleStartedWakingUp();
1604 Trace.endSection();
1605 break;
1606 case MSG_FACE_UNLOCK_STATE_CHANGED:
1607 Trace.beginSection(
1608 "KeyguardUpdateMonitor#handler MSG_FACE_UNLOCK_STATE_CHANGED");
1609 handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
1610 Trace.endSection();
1611 break;
1612 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
1613 handleSimSubscriptionInfoChanged();
1614 break;
1615 case MSG_AIRPLANE_MODE_CHANGED:
1616 handleAirplaneModeChanged();
1617 break;
1618 case MSG_SERVICE_STATE_CHANGE:
1619 handleServiceStateChange(msg.arg1, (ServiceState) msg.obj);
1620 break;
1621 case MSG_SCREEN_TURNED_ON:
1622 handleScreenTurnedOn();
1623 break;
1624 case MSG_SCREEN_TURNED_OFF:
1625 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_SCREEN_TURNED_ON");
1626 handleScreenTurnedOff();
1627 Trace.endSection();
1628 break;
1629 case MSG_DREAMING_STATE_CHANGED:
1630 handleDreamingStateChanged(msg.arg1);
1631 break;
1632 case MSG_USER_UNLOCKED:
1633 handleUserUnlocked(msg.arg1);
1634 break;
1635 case MSG_USER_STOPPED:
1636 handleUserStopped(msg.arg1);
1637 break;
1638 case MSG_USER_REMOVED:
1639 handleUserRemoved(msg.arg1);
1640 break;
1641 case MSG_ASSISTANT_STACK_CHANGED:
1642 setAssistantVisible((boolean) msg.obj);
1643 break;
1644 case MSG_BIOMETRIC_AUTHENTICATION_CONTINUE:
1645 updateBiometricListeningState();
1646 break;
1647 case MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED:
1648 updateLogoutEnabled();
1649 break;
1650 case MSG_TELEPHONY_CAPABLE:
1651 updateTelephonyCapable((boolean) msg.obj);
1652 break;
1653 default:
1654 super.handleMessage(msg);
1655 break;
1656 }
1657 }
1658 };
1659
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001660 // Since device can't be un-provisioned, we only need to register a content observer
1661 // to update mDeviceProvisioned when we are...
1662 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001663 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001664 }
Jim Miller47088bb2009-11-24 00:40:16 -08001665
Jim Millerbbf1a742012-07-17 18:30:30 -07001666 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -07001667 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001668
Jim Millerbbf1a742012-07-17 18:30:30 -07001669 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001670 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001671 filter.addAction(Intent.ACTION_TIME_TICK);
1672 filter.addAction(Intent.ACTION_TIME_CHANGED);
1673 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1674 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -04001675 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
Daniel Bright350f7f92020-01-22 14:24:11 -08001676 filter.addAction(Intent.ACTION_SIM_STATE_CHANGED);
Jayachandran C041e7692019-12-20 16:20:02 -08001677 filter.addAction(Intent.ACTION_SERVICE_STATE);
1678 filter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001679 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001680 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Alex Chauff7653d2018-02-01 17:18:08 +00001681 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -05001682 broadcastDispatcher.registerReceiverWithHandler(mBroadcastReceiver, filter, mHandler);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001683
Adrian Roos48c796c2014-09-01 14:59:23 +02001684 final IntentFilter allUserFilter = new IntentFilter();
1685 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1686 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1687 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1688 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1689 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001690 allUserFilter.addAction(ACTION_USER_UNLOCKED);
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001691 allUserFilter.addAction(ACTION_USER_STOPPED);
1692 allUserFilter.addAction(ACTION_USER_REMOVED);
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -05001693 broadcastDispatcher.registerReceiverWithHandler(mBroadcastAllReceiver, allUserFilter,
1694 mHandler, UserHandle.ALL);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001695
Wink Saville071743f2015-01-12 17:11:04 -08001696 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001697 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001698 ActivityManager.getService().registerUserSwitchObserver(
Sudheer Shanka2c4522c2016-08-27 20:53:28 -07001699 new UserSwitchObserver() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001700 @Override
1701 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001702 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001703 newUserId, 0, reply));
1704 }
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001705
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001706 @Override
1707 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001708 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001709 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001710 }
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001711 }, TAG);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001712 } catch (RemoteException e) {
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001713 e.rethrowAsRuntimeException();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001714 }
Adrian Roos46842d92014-03-27 14:58:03 +01001715
Lucas Dupinf2c53502019-10-03 13:56:18 -07001716 mTrustManager = context.getSystemService(TrustManager.class);
Jorim Jaggi237b0612015-05-01 14:28:49 -07001717 mTrustManager.registerTrustListener(this);
Michal Karpinskic52f8672016-11-18 11:32:45 +00001718 mLockPatternUtils = new LockPatternUtils(context);
1719 mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001720
Kevin Chyn36778ff2017-09-07 19:55:38 -07001721 mDreamManager = IDreamManager.Stub.asInterface(
1722 ServiceManager.getService(DreamService.DREAM_SERVICE));
1723
Jorim Jaggi3f124262016-11-22 13:45:17 +01001724 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
1725 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
1726 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001727 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) {
Kevin Chynb7b54a62018-09-28 18:48:12 -07001728 mFaceManager = (FaceManager) context.getSystemService(Context.FACE_SERVICE);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001729 }
Kevin Chynb7b54a62018-09-28 18:48:12 -07001730
1731 if (mFpm != null || mFaceManager != null) {
1732 mBiometricManager = context.getSystemService(BiometricManager.class);
1733 mBiometricManager.registerEnabledOnKeyguardCallback(mBiometricEnabledCallback);
1734 }
1735
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001736 updateBiometricListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001737 if (mFpm != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001738 mFpm.addLockoutResetCallback(mFingerprintLockoutResetCallback);
1739 }
Kevin Chynb7b54a62018-09-28 18:48:12 -07001740 if (mFaceManager != null) {
1741 mFaceManager.addLockoutResetCallback(mFaceLockoutResetCallback);
Jorim Jaggi3a464782015-08-28 16:59:13 -07001742 }
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001743
Winson Chung2cf6ad82017-11-09 17:36:59 -08001744 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001745 mUserManager = context.getSystemService(UserManager.class);
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001746 mIsPrimaryUser = mUserManager.isPrimaryUser();
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001747 int user = ActivityManager.getCurrentUser();
1748 mUserIsUnlocked.put(user, mUserManager.isUserUnlocked(user));
Alex Chauff7653d2018-02-01 17:18:08 +00001749 mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
1750 mLogoutEnabled = mDevicePolicyManager.isLogoutEnabled();
Yvonne Jiangb7024a22019-12-05 16:57:08 -08001751 updateSecondaryLockscreenRequirement(user);
Lucas Dupinf2c53502019-10-03 13:56:18 -07001752 List<UserInfo> allUsers = mUserManager.getUsers();
1753 for (UserInfo userInfo : allUsers) {
1754 mUserTrustIsUsuallyManaged.put(userInfo.id,
1755 mTrustManager.isTrustUsuallyManaged(userInfo.id));
1756 }
Bill Linef81cbd2018-07-05 17:48:49 +08001757 updateAirplaneModeState();
Fabian Kozynskiccde55d2019-06-26 10:06:09 -04001758
1759 TelephonyManager telephony =
1760 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
1761 if (telephony != null) {
1762 telephony.listen(mPhoneStateListener, LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
1763 }
Bill Linef81cbd2018-07-05 17:48:49 +08001764 }
1765
1766 private void updateAirplaneModeState() {
1767 // ACTION_AIRPLANE_MODE_CHANGED do not broadcast if device set AirplaneMode ON and boot
1768 if (!WirelessUtils.isAirplaneModeOn(mContext)
1769 || mHandler.hasMessages(MSG_AIRPLANE_MODE_CHANGED)) {
1770 return;
1771 }
1772 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Jorim Jaggiea657062015-04-28 13:45:11 -07001773 }
1774
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001775 private void updateBiometricListeningState() {
1776 updateFingerprintListeningState();
1777 updateFaceListeningState();
1778 }
1779
Jorim Jaggiea657062015-04-28 13:45:11 -07001780 private void updateFingerprintListeningState() {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001781 // If this message exists, we should not authenticate again until this message is
1782 // consumed by the handler
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001783 if (mHandler.hasMessages(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE)) {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001784 return;
1785 }
Kevin Chyn0c45b072017-04-24 16:27:11 -07001786 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Jorim Jaggiea657062015-04-28 13:45:11 -07001787 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Adrian Roos2aaf9442018-11-20 16:57:33 +01001788 boolean runningOrRestarting = mFingerprintRunningState == BIOMETRIC_STATE_RUNNING
1789 || mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING;
1790 if (runningOrRestarting && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001791 stopListeningForFingerprint();
Adrian Roos2aaf9442018-11-20 16:57:33 +01001792 } else if (!runningOrRestarting && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001793 startListeningForFingerprint();
1794 }
1795 }
1796
Lucas Dupin3d053532019-01-29 12:35:22 -08001797 /**
Lucas Dupin6c6d5732019-08-27 17:36:05 -07001798 * If a user is encrypted or not.
1799 * This is NOT related to the lock screen being visible or not.
1800 *
1801 * @param userId The user.
1802 * @return {@code true} when encrypted.
1803 * @see UserManager#isUserUnlocked()
1804 * @see Intent#ACTION_USER_UNLOCKED
1805 */
1806 public boolean isUserUnlocked(int userId) {
1807 return mUserIsUnlocked.get(userId);
1808 }
1809
1810 /**
Lucas Dupine0516d52019-02-05 17:54:06 -05001811 * Called whenever passive authentication is requested or aborted by a sensor.
Dave Mankoff4b0ab652019-08-07 09:49:20 -04001812 *
Lucas Dupine0516d52019-02-05 17:54:06 -05001813 * @param active If the interrupt started or ended.
Lucas Dupin3d053532019-01-29 12:35:22 -08001814 */
Lucas Dupine0516d52019-02-05 17:54:06 -05001815 public void onAuthInterruptDetected(boolean active) {
1816 if (DEBUG) Log.d(TAG, "onAuthInterruptDetected(" + active + ")");
1817 if (mAuthInterruptActive == active) {
1818 return;
1819 }
1820 mAuthInterruptActive = active;
Lucas Dupin3d053532019-01-29 12:35:22 -08001821 updateFaceListeningState();
1822 }
1823
Lucas Dupin8f3faac2019-03-12 15:28:49 -07001824 /**
1825 * Requests face authentication if we're on a state where it's allowed.
1826 * This will re-trigger auth in case it fails.
1827 */
1828 public void requestFaceAuth() {
1829 if (DEBUG) Log.d(TAG, "requestFaceAuth()");
1830 updateFaceListeningState();
1831 }
1832
Lucas Dupinccf67212019-08-07 12:53:02 -07001833 /**
1834 * In case face auth is running, cancel it.
1835 */
1836 public void cancelFaceAuth() {
1837 stopListeningForFace();
1838 }
1839
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001840 private void updateFaceListeningState() {
1841 // If this message exists, we should not authenticate again until this message is
1842 // consumed by the handler
1843 if (mHandler.hasMessages(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE)) {
1844 return;
1845 }
1846 mHandler.removeCallbacks(mRetryFaceAuthentication);
1847 boolean shouldListenForFace = shouldListenForFace();
1848 if (mFaceRunningState == BIOMETRIC_STATE_RUNNING && !shouldListenForFace) {
1849 stopListeningForFace();
1850 } else if (mFaceRunningState != BIOMETRIC_STATE_RUNNING
1851 && shouldListenForFace) {
1852 startListeningForFace();
1853 }
1854 }
1855
Kevin Chyn129f60f2017-08-11 17:24:42 -07001856 private boolean shouldListenForFingerprintAssistant() {
1857 return mAssistantVisible && mKeyguardOccluded
1858 && !mUserFingerprintAuthenticated.get(getCurrentUser(), false)
1859 && !mUserHasTrust.get(getCurrentUser(), false);
1860 }
1861
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001862 private boolean shouldListenForFaceAssistant() {
1863 return mAssistantVisible && mKeyguardOccluded
1864 && !mUserFaceAuthenticated.get(getCurrentUser(), false)
1865 && !mUserHasTrust.get(getCurrentUser(), false);
1866 }
1867
Jorim Jaggiea657062015-04-28 13:45:11 -07001868 private boolean shouldListenForFingerprint() {
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001869 // Only listen if this KeyguardUpdateMonitor belongs to the primary user. There is an
1870 // instance of KeyguardUpdateMonitor for each user but KeyguardUpdateMonitor is user-aware.
1871 final boolean shouldListen = (mKeyguardIsVisible || !mDeviceInteractive ||
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001872 (mBouncer && !mKeyguardGoingAway) || mGoingToSleep ||
Kevin Chyn36778ff2017-09-07 19:55:38 -07001873 shouldListenForFingerprintAssistant() || (mKeyguardOccluded && mIsDreaming))
Lucas Dupinc12fad32019-05-14 20:59:17 +00001874 && !mSwitchingUser && !isFingerprintDisabled(getCurrentUser())
Kevin Chyn225eea72019-03-06 16:42:00 -08001875 && (!mKeyguardGoingAway || !mDeviceInteractive) && mIsPrimaryUser;
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001876 return shouldListen;
Jim Miller9f0753f2015-03-23 23:59:22 -07001877 }
1878
Lucas Dupin4b75b192019-08-29 14:46:29 -07001879 /**
1880 * If face auth is allows to scan on this exact moment.
1881 */
1882 public boolean shouldListenForFace() {
Lucas Dupin3d053532019-01-29 12:35:22 -08001883 final boolean awakeKeyguard = mKeyguardIsVisible && mDeviceInteractive && !mGoingToSleep;
1884 final int user = getCurrentUser();
Robert Snoeberger5dcdf352019-06-24 11:28:28 -04001885 final int strongAuth = mStrongAuthTracker.getStrongAuthForUser(user);
1886 final boolean isLockOutOrLockDown =
Lucas Dupin8eec2682019-07-01 16:41:17 -07001887 containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_LOCKOUT)
1888 || containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW)
1889 || containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
1890 final boolean isEncryptedOrTimedOut =
1891 containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_BOOT)
1892 || containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001893
Lucas Dupin4befb742019-07-01 11:31:31 -07001894 boolean canBypass = mKeyguardBypassController != null
1895 && mKeyguardBypassController.canBypass();
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001896 // There's no reason to ask the HAL for authentication when the user can dismiss the
1897 // bouncer, unless we're bypassing and need to auto-dismiss the lock screen even when
1898 // TrustAgents or biometrics are keeping the device unlocked.
Lucas Dupin4befb742019-07-01 11:31:31 -07001899 boolean becauseCannotSkipBouncer = !getUserCanSkipBouncer(user) || canBypass;
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001900
Lucas Dupin8eec2682019-07-01 16:41:17 -07001901 // Scan even when encrypted or timeout to show a preemptive bouncer when bypassing.
1902 // Lockout/lockdown modes shouldn't scan, since they are more explicit.
1903 boolean strongAuthAllowsScanning = (!isEncryptedOrTimedOut || canBypass && !mBouncer)
1904 && !isLockOutOrLockDown;
1905
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001906 // Only listen if this KeyguardUpdateMonitor belongs to the primary user. There is an
1907 // instance of KeyguardUpdateMonitor for each user but KeyguardUpdateMonitor is user-aware.
Lucas Dupine0516d52019-02-05 17:54:06 -05001908 return (mBouncer || mAuthInterruptActive || awakeKeyguard || shouldListenForFaceAssistant())
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001909 && !mSwitchingUser && !isFaceDisabled(user) && becauseCannotSkipBouncer
Lucas Dupin7d95f152019-07-17 16:25:54 -07001910 && !mKeyguardGoingAway && mFaceSettingEnabledForUser.get(user) && !mLockIconPressed
Lucas Dupin8eec2682019-07-01 16:41:17 -07001911 && strongAuthAllowsScanning && mIsPrimaryUser
1912 && !mSecureCameraLaunched;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001913 }
1914
Lucas Dupinca88e5f2019-05-14 16:11:08 -07001915 /**
1916 * Whenever the lock icon is long pressed, disabling trust agents.
1917 * This means that we cannot auth passively (face) until the user presses power.
1918 */
1919 public void onLockIconPressed() {
1920 mLockIconPressed = true;
Curtis Belmontec48eff52019-08-21 15:51:34 -07001921 final int userId = getCurrentUser();
1922 mUserFaceAuthenticated.put(userId, false);
Lucas Dupinca88e5f2019-05-14 16:11:08 -07001923 updateFaceListeningState();
Curtis Belmontec48eff52019-08-21 15:51:34 -07001924 mStrongAuthTracker.onStrongAuthRequiredChanged(userId);
Lucas Dupinca88e5f2019-05-14 16:11:08 -07001925 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001926
Jim Millerce7eb6d2015-04-03 19:29:13 -07001927 private void startListeningForFingerprint() {
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001928 if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING) {
1929 setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING_RESTARTING);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001930 return;
1931 }
Adrian Roos2aaf9442018-11-20 16:57:33 +01001932 if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
1933 // Waiting for restart via handleFingerprintError().
1934 return;
1935 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001936 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Lucas Dupin8f3faac2019-03-12 15:28:49 -07001937 int userId = getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001938 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001939 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001940 mFingerprintCancelSignal.cancel();
1941 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001942 mFingerprintCancelSignal = new CancellationSignal();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001943 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mFingerprintAuthenticationCallback,
1944 null, userId);
1945 setFingerprintRunningState(BIOMETRIC_STATE_RUNNING);
1946 }
1947 }
1948
1949 private void startListeningForFace() {
1950 if (mFaceRunningState == BIOMETRIC_STATE_CANCELLING) {
1951 setFaceRunningState(BIOMETRIC_STATE_CANCELLING_RESTARTING);
1952 return;
1953 }
1954 if (DEBUG) Log.v(TAG, "startListeningForFace()");
Lucas Dupin8f3faac2019-03-12 15:28:49 -07001955 int userId = getCurrentUser();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001956 if (isUnlockWithFacePossible(userId)) {
1957 if (mFaceCancelSignal != null) {
1958 mFaceCancelSignal.cancel();
1959 }
1960 mFaceCancelSignal = new CancellationSignal();
Kevin Chynb7b54a62018-09-28 18:48:12 -07001961 mFaceManager.authenticate(null, mFaceCancelSignal, 0,
Kevin Chyn8d2694a2019-04-11 18:30:40 -07001962 mFaceAuthenticationCallback, null, userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001963 setFaceRunningState(BIOMETRIC_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001964 }
1965 }
1966
Lucas Dupin8d48fc42019-04-25 14:34:12 -07001967 /**
1968 * If biometrics hardware is available, not disabled, and user has enrolled templates.
1969 * This does NOT check if the device is encrypted or in lockdown.
1970 *
1971 * @param userId User that's trying to unlock.
1972 * @return {@code true} if possible.
1973 */
1974 public boolean isUnlockingWithBiometricsPossible(int userId) {
1975 return isUnlockWithFacePossible(userId) || isUnlockWithFingerprintPossible(userId);
1976 }
1977
1978 private boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001979 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1980 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1981 }
1982
Lucas Dupin4c507042019-07-22 16:47:34 -07001983 private boolean isUnlockWithFacePossible(int userId) {
1984 return isFaceAuthEnabledForUser(userId) && !isFaceDisabled(userId);
1985 }
1986
Lucas Dupin7156bc72019-05-03 19:37:39 -07001987 /**
Lucas Dupin7d95f152019-07-17 16:25:54 -07001988 * If face hardware is available, user has enrolled and enabled auth via setting.
Lucas Dupin7156bc72019-05-03 19:37:39 -07001989 */
Lucas Dupin4c507042019-07-22 16:47:34 -07001990 public boolean isFaceAuthEnabledForUser(int userId) {
Lucas Dupin8968f6a2019-08-09 17:41:15 -07001991 // TODO(b/140034352)
1992 return whitelistIpcs(() -> mFaceManager != null && mFaceManager.isHardwareDetected()
Lucas Dupin7d95f152019-07-17 16:25:54 -07001993 && mFaceManager.hasEnrolledTemplates(userId)
Lucas Dupin8968f6a2019-08-09 17:41:15 -07001994 && mFaceSettingEnabledForUser.get(userId));
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001995 }
1996
Jorim Jaggiea657062015-04-28 13:45:11 -07001997 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001998 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001999 if (mFingerprintRunningState == BIOMETRIC_STATE_RUNNING) {
Kevin Chyn2fefd462017-04-28 12:18:19 -07002000 if (mFingerprintCancelSignal != null) {
2001 mFingerprintCancelSignal.cancel();
2002 mFingerprintCancelSignal = null;
joshmccloskey7dee4542019-07-31 18:35:33 -07002003 if (!mHandler.hasCallbacks(mCancelNotReceived)) {
2004 mHandler.postDelayed(mCancelNotReceived, DEFAULT_CANCEL_SIGNAL_TIMEOUT);
2005 }
Kevin Chyn2fefd462017-04-28 12:18:19 -07002006 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002007 setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07002008 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002009 if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
2010 setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING);
2011 }
2012 }
2013
2014 private void stopListeningForFace() {
2015 if (DEBUG) Log.v(TAG, "stopListeningForFace()");
2016 if (mFaceRunningState == BIOMETRIC_STATE_RUNNING) {
2017 if (mFaceCancelSignal != null) {
2018 mFaceCancelSignal.cancel();
2019 mFaceCancelSignal = null;
joshmccloskey7dee4542019-07-31 18:35:33 -07002020 if (!mHandler.hasCallbacks(mCancelNotReceived)) {
2021 mHandler.postDelayed(mCancelNotReceived, DEFAULT_CANCEL_SIGNAL_TIMEOUT);
2022 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002023 }
2024 setFaceRunningState(BIOMETRIC_STATE_CANCELLING);
2025 }
2026 if (mFaceRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
2027 setFaceRunningState(BIOMETRIC_STATE_CANCELLING);
Jorim Jaggi86bed402015-08-20 18:20:02 -07002028 }
Jim Millerbbf1a742012-07-17 18:30:30 -07002029 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002030
Michael Jurkafff56142012-11-28 16:51:00 -08002031 private boolean isDeviceProvisionedInSettingsDb() {
2032 return Settings.Global.getInt(mContext.getContentResolver(),
2033 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
2034 }
2035
Jim Millerbbf1a742012-07-17 18:30:30 -07002036 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08002037 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07002038 @Override
2039 public void onChange(boolean selfChange) {
2040 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08002041 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07002042 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07002043 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002044 }
Jim Millerbbf1a742012-07-17 18:30:30 -07002045 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002046 }
Jim Millerbbf1a742012-07-17 18:30:30 -07002047 };
2048
2049 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07002050 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08002051 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07002052
2053 // prevent a race condition between where we check the flag and where we register the
2054 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08002055 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07002056 if (provisioned != mDeviceProvisioned) {
2057 mDeviceProvisioned = provisioned;
2058 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07002059 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07002060 }
2061 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002062 }
2063
Jim Millerbbf1a742012-07-17 18:30:30 -07002064 /**
Jorim Jaggid11d1a92016-08-16 16:02:32 -07002065 * Update the state whether Keyguard currently has a lockscreen wallpaper.
2066 *
2067 * @param hasLockscreenWallpaper Whether Keyguard has a lockscreen wallpaper.
2068 */
2069 public void setHasLockscreenWallpaper(boolean hasLockscreenWallpaper) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002070 checkIsHandlerThread();
Jorim Jaggid11d1a92016-08-16 16:02:32 -07002071 if (hasLockscreenWallpaper != mHasLockscreenWallpaper) {
2072 mHasLockscreenWallpaper = hasLockscreenWallpaper;
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002073 for (int i = 0; i < mCallbacks.size(); i++) {
Jorim Jaggid11d1a92016-08-16 16:02:32 -07002074 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2075 if (cb != null) {
2076 cb.onHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
2077 }
2078 }
2079 }
2080 }
2081
2082 /**
2083 * @return Whether Keyguard has a lockscreen wallpaper.
2084 */
2085 public boolean hasLockscreenWallpaper() {
2086 return mHasLockscreenWallpaper;
2087 }
2088
2089 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07002090 * Handle {@link #MSG_DPM_STATE_CHANGED}
2091 */
Yvonne Jiangb7024a22019-12-05 16:57:08 -08002092 private void handleDevicePolicyManagerStateChanged(int userId) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002093 checkIsHandlerThread();
Adrian Roos733b6632015-08-21 14:32:35 -07002094 updateFingerprintListeningState();
Yvonne Jiangb7024a22019-12-05 16:57:08 -08002095 updateSecondaryLockscreenRequirement(userId);
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002096 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002097 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2098 if (cb != null) {
2099 cb.onDevicePolicyManagerStateChanged();
2100 }
Jim Millerb0304762012-03-13 20:01:25 -07002101 }
2102 }
2103
Jim Millerbbf1a742012-07-17 18:30:30 -07002104 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05002105 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07002106 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002107 private void handleUserSwitching(int userId, IRemoteCallback reply) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002108 checkIsHandlerThread();
Lucas Dupinf2c53502019-10-03 13:56:18 -07002109 mUserTrustIsUsuallyManaged.put(userId, mTrustManager.isTrustUsuallyManaged(userId));
Jim Millerbbf1a742012-07-17 18:30:30 -07002110 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002111 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2112 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05002113 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07002114 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07002115 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002116 try {
2117 reply.sendResult(null);
2118 } catch (RemoteException e) {
2119 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07002120 }
2121
Jim Millerbbf1a742012-07-17 18:30:30 -07002122 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05002123 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
2124 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002125 private void handleUserSwitchComplete(int userId) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002126 checkIsHandlerThread();
Chris Wrenf41c61b2012-11-29 15:19:54 -05002127 for (int i = 0; i < mCallbacks.size(); i++) {
2128 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2129 if (cb != null) {
2130 cb.onUserSwitchComplete(userId);
2131 }
2132 }
2133 }
2134
2135 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07002136 * Handle {@link #MSG_DEVICE_PROVISIONED}
2137 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002138 private void handleDeviceProvisioned() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002139 checkIsHandlerThread();
Jim Millerbbf1a742012-07-17 18:30:30 -07002140 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002141 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2142 if (cb != null) {
2143 cb.onDeviceProvisioned();
2144 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07002145 }
Michael Jurkafff56142012-11-28 16:51:00 -08002146 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07002147 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08002148 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
2149 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07002150 }
2151 }
2152
Jim Millerbbf1a742012-07-17 18:30:30 -07002153 /**
2154 * Handle {@link #MSG_PHONE_STATE_CHANGED}
2155 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002156 private void handlePhoneStateChanged(String newState) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002157 checkIsHandlerThread();
Jim Millerc23024d2010-02-24 15:37:00 -08002158 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07002159 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
2160 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
2161 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
2162 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
2163 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
2164 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
2165 }
Jim Millerbbf1a742012-07-17 18:30:30 -07002166 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002167 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2168 if (cb != null) {
2169 cb.onPhoneStateChanged(mPhoneState);
2170 }
Jim Millerc23024d2010-02-24 15:37:00 -08002171 }
2172 }
2173
Jim Millerbbf1a742012-07-17 18:30:30 -07002174 /**
2175 * Handle {@link #MSG_RINGER_MODE_CHANGED}
2176 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002177 private void handleRingerModeChange(int mode) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002178 checkIsHandlerThread();
Jim Miller47088bb2009-11-24 00:40:16 -08002179 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07002180 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07002181 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002182 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2183 if (cb != null) {
2184 cb.onRingerModeChanged(mode);
2185 }
Jim Miller47088bb2009-11-24 00:40:16 -08002186 }
2187 }
2188
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002189 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002190 * Handle {@link #MSG_TIME_UPDATE}
2191 */
2192 private void handleTimeUpdate() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002193 checkIsHandlerThread();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002194 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07002195 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002196 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2197 if (cb != null) {
2198 cb.onTimeChanged();
2199 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002200 }
2201 }
2202
2203 /**
Robert Snoeberger9c1074f2018-12-07 17:35:21 -05002204 * Handle (@line #MSG_TIMEZONE_UPDATE}
2205 */
2206 private void handleTimeZoneUpdate(String timeZone) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002207 checkIsHandlerThread();
Robert Snoeberger9c1074f2018-12-07 17:35:21 -05002208 if (DEBUG) Log.d(TAG, "handleTimeZoneUpdate");
2209 for (int i = 0; i < mCallbacks.size(); i++) {
2210 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2211 if (cb != null) {
2212 cb.onTimeZoneChanged(TimeZone.getTimeZone(timeZone));
2213 // Also notify callbacks about time change to remain compatible.
2214 cb.onTimeChanged();
2215 }
2216 }
2217 }
2218
2219 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002220 * Handle {@link #MSG_BATTERY_UPDATE}
2221 */
Jim Millerbbf1a742012-07-17 18:30:30 -07002222 private void handleBatteryUpdate(BatteryStatus status) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002223 checkIsHandlerThread();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002224 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07002225 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
2226 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07002227 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07002228 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002229 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2230 if (cb != null) {
2231 cb.onRefreshBatteryInfo(status);
2232 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002233 }
2234 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002235 }
2236
2237 /**
Bill Linef81cbd2018-07-05 17:48:49 +08002238 * Handle Telephony status during Boot for CarrierText display policy
2239 */
2240 @VisibleForTesting
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002241 void updateTelephonyCapable(boolean capable) {
2242 checkIsHandlerThread();
Bill Linef81cbd2018-07-05 17:48:49 +08002243 if (capable == mTelephonyCapable) {
2244 return;
2245 }
2246 mTelephonyCapable = capable;
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002247 for (int i = 0; i < mCallbacks.size(); i++) {
2248 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
Bill Linef81cbd2018-07-05 17:48:49 +08002249 if (cb != null) {
2250 cb.onTelephonyCapable(mTelephonyCapable);
2251 }
2252 }
2253 }
2254
2255 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002256 * Handle {@link #MSG_SIM_STATE_CHANGE}
2257 */
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08002258 @VisibleForTesting
Jayachandran Cf5436a62019-11-08 18:22:45 -08002259 void handleSimStateChange(int subId, int slotId, int state) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002260 checkIsHandlerThread();
Jim Miller52a61332014-11-12 19:29:51 -08002261 if (DEBUG_SIM_STATES) {
2262 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002263 + slotId + ", state=" + state + ")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002264 }
2265
Lucas Dupin2e0d4952018-12-05 20:03:53 -08002266 boolean becameAbsent = false;
Wink Savillea54bf652014-12-11 13:37:50 -08002267 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08002268 Log.w(TAG, "invalid subId in handleSimStateChange()");
andychou695a7602019-05-16 23:14:00 +08002269 /* Only handle No SIM(ABSENT) and Card Error(CARD_IO_ERROR) due to
2270 * handleServiceStateChange() handle other case */
Jayachandran Cf5436a62019-11-08 18:22:45 -08002271 if (state == TelephonyManager.SIM_STATE_ABSENT) {
Bill Linef81cbd2018-07-05 17:48:49 +08002272 updateTelephonyCapable(true);
Lucas Dupin2e0d4952018-12-05 20:03:53 -08002273 // Even though the subscription is not valid anymore, we need to notify that the
2274 // SIM card was removed so we can update the UI.
2275 becameAbsent = true;
Brad Ebingere6c6f722018-12-20 21:11:44 -08002276 for (SimData data : mSimDatas.values()) {
2277 // Set the SIM state of all SimData associated with that slot to ABSENT se we
2278 // do not move back into PIN/PUK locked and not detect the change below.
2279 if (data.slotId == slotId) {
Jayachandran Cf5436a62019-11-08 18:22:45 -08002280 data.simState = TelephonyManager.SIM_STATE_ABSENT;
Brad Ebingere6c6f722018-12-20 21:11:44 -08002281 }
2282 }
Jayachandran Cf5436a62019-11-08 18:22:45 -08002283 } else if (state == TelephonyManager.SIM_STATE_CARD_IO_ERROR) {
andychou695a7602019-05-16 23:14:00 +08002284 updateTelephonyCapable(true);
Lucas Dupin2e0d4952018-12-05 20:03:53 -08002285 } else {
2286 return;
Bill Linef81cbd2018-07-05 17:48:49 +08002287 }
Jim Miller52a61332014-11-12 19:29:51 -08002288 }
2289
2290 SimData data = mSimDatas.get(subId);
2291 final boolean changed;
2292 if (data == null) {
2293 data = new SimData(state, slotId, subId);
2294 mSimDatas.put(subId, data);
2295 changed = true; // no data yet; force update
2296 } else {
2297 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
2298 data.simState = state;
2299 data.subId = subId;
2300 data.slotId = slotId;
2301 }
Jayachandran Cf5436a62019-11-08 18:22:45 -08002302 if ((changed || becameAbsent) && state != TelephonyManager.SIM_STATE_UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07002303 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002304 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2305 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08002306 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07002307 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002308 }
2309 }
2310 }
2311
Jim Millerbbf1a742012-07-17 18:30:30 -07002312 /**
Etan Cohen47051d82015-07-06 16:19:04 -07002313 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
2314 */
Bill Linef81cbd2018-07-05 17:48:49 +08002315 @VisibleForTesting
2316 void handleServiceStateChange(int subId, ServiceState serviceState) {
Etan Cohen47051d82015-07-06 16:19:04 -07002317 if (DEBUG) {
2318 Log.d(TAG,
2319 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
2320 }
2321
2322 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2323 Log.w(TAG, "invalid subId in handleServiceStateChange()");
2324 return;
Bill Linef81cbd2018-07-05 17:48:49 +08002325 } else {
2326 updateTelephonyCapable(true);
Etan Cohen47051d82015-07-06 16:19:04 -07002327 }
2328
2329 mServiceStates.put(subId, serviceState);
2330
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002331 callbacksRefreshCarrierInfo();
Etan Cohen47051d82015-07-06 16:19:04 -07002332 }
2333
Lucas Dupinf8463ee2018-06-11 16:18:15 -07002334 public boolean isKeyguardVisible() {
2335 return mKeyguardIsVisible;
2336 }
2337
Etan Cohen47051d82015-07-06 16:19:04 -07002338 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002339 * Notifies that the visibility state of Keyguard has changed.
2340 *
2341 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002342 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002343 public void onKeyguardVisibilityChanged(boolean showing) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002344 checkIsHandlerThread();
Kevin Chynb4514d22019-04-15 13:47:25 -07002345 Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002346 mKeyguardIsVisible = showing;
Kevin Chyn6951d3d2019-06-10 14:07:13 -07002347
2348 if (showing) {
2349 mSecureCameraLaunched = false;
2350 }
2351
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002352 for (int i = 0; i < mCallbacks.size(); i++) {
2353 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2354 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002355 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002356 }
2357 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002358 updateBiometricListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002359 }
2360
Brian Colonna7fce3802013-09-17 15:51:32 -04002361 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07002362 * Handle {@link #MSG_KEYGUARD_RESET}
2363 */
2364 private void handleKeyguardReset() {
2365 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002366 updateBiometricListeningState();
Jorim Jaggi031f7952016-09-01 16:39:26 -07002367 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
2368 }
2369
2370 private boolean resolveNeedsSlowUnlockTransition() {
Lucas Dupin6c6d5732019-08-27 17:36:05 -07002371 if (isUserUnlocked(getCurrentUser())) {
Jorim Jaggi031f7952016-09-01 16:39:26 -07002372 return false;
2373 }
2374 Intent homeIntent = new Intent(Intent.ACTION_MAIN)
2375 .addCategory(Intent.CATEGORY_HOME);
2376 ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
2377 0 /* flags */);
2378 return FALLBACK_HOME_COMPONENT.equals(resolveInfo.getComponentInfo().getComponentName());
Selim Cinek1fcafc42015-07-20 14:39:25 -07002379 }
2380
2381 /**
Adrian Roosb6011622014-05-14 15:52:53 +02002382 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002383 *
Adrian Roosb6011622014-05-14 15:52:53 +02002384 * @see #sendKeyguardBouncerChanged(boolean)
2385 */
2386 private void handleKeyguardBouncerChanged(int bouncer) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002387 checkIsHandlerThread();
Adrian Roosb6011622014-05-14 15:52:53 +02002388 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
2389 boolean isBouncer = (bouncer == 1);
2390 mBouncer = isBouncer;
Kevin Chynd0381892019-07-11 16:25:36 -07002391
2392 if (isBouncer) {
2393 // If the bouncer is shown, always clear this flag. This can happen in the following
2394 // situations: 1) Default camera with SHOW_WHEN_LOCKED is not chosen yet. 2) Secure
2395 // camera requests dismiss keyguard (tapping on photos for example). When these happen,
2396 // face auth should resume.
2397 mSecureCameraLaunched = false;
2398 }
2399
Adrian Roosb6011622014-05-14 15:52:53 +02002400 for (int i = 0; i < mCallbacks.size(); i++) {
2401 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2402 if (cb != null) {
2403 cb.onKeyguardBouncerChanged(isBouncer);
2404 }
2405 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002406 updateBiometricListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02002407 }
2408
2409 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04002410 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
2411 */
2412 private void handleReportEmergencyCallAction() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002413 checkIsHandlerThread();
Brian Colonna7fce3802013-09-17 15:51:32 -04002414 for (int i = 0; i < mCallbacks.size(); i++) {
2415 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2416 if (cb != null) {
2417 cb.onEmergencyCallAction();
2418 }
2419 }
2420 }
2421
Lucas Dupin4272f442018-01-13 22:00:35 -08002422 private boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07002423 final boolean nowPluggedIn = current.isPluggedIn();
2424 final boolean wasPluggedIn = old.isPluggedIn();
Lucas Dupin4272f442018-01-13 22:00:35 -08002425 final boolean stateChangedWhilePluggedIn = wasPluggedIn && nowPluggedIn
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002426 && (old.status != current.status);
Jim Miller16464b82011-10-20 21:10:13 -07002427
2428 // change in plug state is always interesting
2429 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002430 return true;
2431 }
2432
Lucas Dupin3fcdd472018-01-19 19:06:45 -08002433 // change in battery level
2434 if (old.level != current.level) {
Jim Miller16464b82011-10-20 21:10:13 -07002435 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002436 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07002437
2438 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08002439 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07002440 return true;
2441 }
2442
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002443 return false;
2444 }
2445
2446 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07002447 * Remove the given observer's callback.
2448 *
Jim Miller6212cc02012-09-05 17:35:31 -07002449 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002450 */
Jim Miller6212cc02012-09-05 17:35:31 -07002451 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002452 checkIsHandlerThread();
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002453 if (DEBUG) {
2454 Log.v(TAG, "*** unregister callback for " + callback);
Jim Miller6212cc02012-09-05 17:35:31 -07002455 }
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002456
2457 mCallbacks.removeIf(el -> el.get() == callback);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002458 }
2459
2460 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002461 * Register to receive notifications about general keyguard information
2462 * (see {@link InfoCallback}.
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002463 *
Jim Miller6212cc02012-09-05 17:35:31 -07002464 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002465 */
Jim Millerbbf1a742012-07-17 18:30:30 -07002466 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002467 checkIsHandlerThread();
Jim Miller6212cc02012-09-05 17:35:31 -07002468 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
2469 // Prevent adding duplicate callbacks
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002470
Jim Miller6212cc02012-09-05 17:35:31 -07002471 for (int i = 0; i < mCallbacks.size(); i++) {
2472 if (mCallbacks.get(i).get() == callback) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002473 if (DEBUG) {
2474 Log.e(TAG, "Object tried to add another callback",
2475 new Exception("Called by"));
2476 }
Jim Miller6212cc02012-09-05 17:35:31 -07002477 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07002478 }
2479 }
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002480 mCallbacks.add(new WeakReference<>(callback));
Jim Miller6212cc02012-09-05 17:35:31 -07002481 removeCallback(null); // remove unused references
2482 sendUpdates(callback);
2483 }
2484
Lucas Dupin9e484aa2019-06-24 13:38:00 -07002485 public void setKeyguardBypassController(KeyguardBypassController keyguardBypassController) {
2486 mKeyguardBypassController = keyguardBypassController;
2487 }
2488
Lucas Dupinc12fad32019-05-14 20:59:17 +00002489 public boolean isSwitchingUser() {
2490 return mSwitchingUser;
2491 }
2492
Adrian Roos30a2ae62018-04-25 19:09:50 +02002493 @AnyThread
Evan Rosky18396452016-07-27 15:19:37 -07002494 public void setSwitchingUser(boolean switching) {
Lucas Dupinc12fad32019-05-14 20:59:17 +00002495 mSwitchingUser = switching;
Selim Cinekbbe19242017-12-08 15:42:08 -08002496 // Since this comes in on a binder thread, we need to post if first
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002497 mHandler.post(mUpdateBiometricListeningState);
Evan Rosky18396452016-07-27 15:19:37 -07002498 }
2499
Jim Miller6212cc02012-09-05 17:35:31 -07002500 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
2501 // Notify listener of the current state
2502 callback.onRefreshBatteryInfo(mBatteryStatus);
2503 callback.onTimeChanged();
2504 callback.onRingerModeChanged(mRingMode);
2505 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05002506 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07002507 callback.onClockVisibilityChanged();
Lucas Dupin16cfe452018-02-08 13:14:50 -08002508 callback.onKeyguardVisibilityChangedRaw(mKeyguardIsVisible);
Bill Linef81cbd2018-07-05 17:48:49 +08002509 callback.onTelephonyCapable(mTelephonyCapable);
Jim Miller52a61332014-11-12 19:29:51 -08002510 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
2511 final SimData state = data.getValue();
2512 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
2513 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002514 }
2515
Selim Cinek1fcafc42015-07-20 14:39:25 -07002516 public void sendKeyguardReset() {
2517 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
2518 }
2519
Adrian Roosb6011622014-05-14 15:52:53 +02002520 /**
2521 * @see #handleKeyguardBouncerChanged(int)
2522 */
2523 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
2524 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
2525 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
2526 message.arg1 = showingBouncer ? 1 : 0;
2527 message.sendToTarget();
2528 }
2529
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002530 /**
Jim Miller90d5d462011-11-17 16:57:01 -08002531 * Report that the user successfully entered the SIM PIN or PUK/SIM PIN so we
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002532 * have the information earlier than waiting for the intent
2533 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08002534 *
2535 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
2536 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002537 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002538 @MainThread
Jim Miller52a61332014-11-12 19:29:51 -08002539 public void reportSimUnlocked(int subId) {
2540 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
Jayachandran Cf5436a62019-11-08 18:22:45 -08002541 handleSimStateChange(subId, getSlotId(subId), TelephonyManager.SIM_STATE_READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002542 }
2543
Brian Colonna7fce3802013-09-17 15:51:32 -04002544 /**
2545 * Report that the emergency call button has been pressed and the emergency dialer is
2546 * about to be displayed.
2547 *
2548 * @param bypassHandler runs immediately.
2549 *
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002550 * NOTE: Must be called from UI thread if bypassHandler == true.
Brian Colonna7fce3802013-09-17 15:51:32 -04002551 */
2552 public void reportEmergencyCallAction(boolean bypassHandler) {
2553 if (!bypassHandler) {
2554 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
2555 } else {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002556 checkIsHandlerThread();
Brian Colonna7fce3802013-09-17 15:51:32 -04002557 handleReportEmergencyCallAction();
2558 }
2559 }
2560
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002561 /**
2562 * @return Whether the device is provisioned (whether they have gone through
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002563 * the setup wizard)
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002564 */
2565 public boolean isDeviceProvisioned() {
2566 return mDeviceProvisioned;
2567 }
2568
Kensuke Matsui21d1bf12017-03-14 13:27:20 +09002569 public ServiceState getServiceState(int subId) {
2570 return mServiceStates.get(subId);
2571 }
2572
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002573 public void clearBiometricRecognized() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002574 checkIsHandlerThread();
Jim Miller9f0753f2015-03-23 23:59:22 -07002575 mUserFingerprintAuthenticated.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002576 mUserFaceAuthenticated.clear();
2577 mTrustManager.clearAllBiometricRecognized(BiometricSourceType.FINGERPRINT);
2578 mTrustManager.clearAllBiometricRecognized(BiometricSourceType.FACE);
Aran Ink4c0d5602019-06-21 14:27:32 -04002579
2580 for (int i = 0; i < mCallbacks.size(); i++) {
2581 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2582 if (cb != null) {
2583 cb.onBiometricsCleared();
2584 }
2585 }
Jim Millerf41fc962014-06-18 16:33:51 -07002586 }
2587
Jim Miller52a61332014-11-12 19:29:51 -08002588 public boolean isSimPinVoiceSecure() {
2589 // TODO: only count SIMs that handle voice
2590 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07002591 }
2592
Lucas Dupin7156bc72019-05-03 19:37:39 -07002593 /**
2594 * If any SIM cards are currently secure.
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002595 *
Lucas Dupin7156bc72019-05-03 19:37:39 -07002596 * @see #isSimPinSecure(State)
2597 */
Jim Millerdcb3d842012-08-23 19:18:12 -07002598 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08002599 // True if any SIM is pin secure
2600 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
2601 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
2602 }
2603 return false;
2604 }
2605
Jayachandran Cf5436a62019-11-08 18:22:45 -08002606 public int getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08002607 if (mSimDatas.containsKey(subId)) {
2608 return mSimDatas.get(subId).simState;
2609 } else {
Jayachandran Cf5436a62019-11-08 18:22:45 -08002610 return TelephonyManager.SIM_STATE_UNKNOWN;
Jim Miller52a61332014-11-12 19:29:51 -08002611 }
2612 }
2613
Lucas Dupin10960bd2019-09-27 16:08:32 -07002614 private int getSlotId(int subId) {
2615 if (!mSimDatas.containsKey(subId)) {
2616 refreshSimState(subId, SubscriptionManager.getSlotIndex(subId));
2617 }
2618 return mSimDatas.get(subId).slotId;
2619 }
2620
Winson Chung67f5c8b2018-09-24 12:09:19 -07002621 private final TaskStackChangeListener
2622 mTaskStackListener = new TaskStackChangeListener() {
Kevin Chyn2fefd462017-04-28 12:18:19 -07002623 @Override
2624 public void onTaskStackChangedBackground() {
2625 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002626 ActivityManager.StackInfo info = ActivityTaskManager.getService().getStackInfo(
Wale Ogunwale68278562017-09-23 17:13:55 -07002627 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_ASSISTANT);
Kevin Chyn2fefd462017-04-28 12:18:19 -07002628 if (info == null) {
2629 return;
2630 }
2631 mHandler.sendMessage(mHandler.obtainMessage(MSG_ASSISTANT_STACK_CHANGED,
2632 info.visible));
2633 } catch (RemoteException e) {
2634 Log.e(TAG, "unable to check task stack", e);
2635 }
2636 }
2637 };
2638
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01002639 /**
Richard Choue0381b82018-04-24 03:48:59 +00002640 * @return true if and only if the state has changed for the specified {@code slotId}
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01002641 */
Richard Choue0381b82018-04-24 03:48:59 +00002642 private boolean refreshSimState(int subId, int slotId) {
Jayachandran Ca43d0a72019-11-01 17:39:38 -07002643 final TelephonyManager tele =
2644 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
Jayachandran Cf5436a62019-11-08 18:22:45 -08002645 int state = (tele != null) ?
Jayachandran Ca43d0a72019-11-01 17:39:38 -07002646 tele.getSimState(slotId) : TelephonyManager.SIM_STATE_UNKNOWN;
Richard Choue0381b82018-04-24 03:48:59 +00002647 SimData data = mSimDatas.get(subId);
2648 final boolean changed;
2649 if (data == null) {
2650 data = new SimData(state, slotId, subId);
2651 mSimDatas.put(subId, data);
2652 changed = true; // no data yet; force update
2653 } else {
2654 changed = data.simState != state;
2655 data.simState = state;
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01002656 }
Richard Choue0381b82018-04-24 03:48:59 +00002657 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07002658 }
2659
Lucas Dupin7156bc72019-05-03 19:37:39 -07002660 /**
2661 * If the {@code state} is currently requiring a SIM PIN, PUK, or is disabled.
2662 */
Jayachandran Cf5436a62019-11-08 18:22:45 -08002663 public static boolean isSimPinSecure(int state) {
2664 return (state == TelephonyManager.SIM_STATE_PIN_REQUIRED
2665 || state == TelephonyManager.SIM_STATE_PUK_REQUIRED
2666 || state == TelephonyManager.SIM_STATE_PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07002667 }
Jim Miller8f09fd22013-03-14 19:04:28 -07002668
2669 public DisplayClientState getCachedDisplayClientState() {
2670 return mDisplayClientState;
2671 }
Jim Miller20daffd2013-10-07 14:59:53 -07002672
2673 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
2674 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002675 public void dispatchStartedWakingUp() {
2676 synchronized (this) {
2677 mDeviceInteractive = true;
2678 }
2679 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
2680 }
2681
Jorim Jaggi95e40382015-09-16 15:53:42 -07002682 public void dispatchStartedGoingToSleep(int why) {
2683 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
2684 }
2685
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002686 public void dispatchFinishedGoingToSleep(int why) {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002687 synchronized (this) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002688 mDeviceInteractive = false;
2689 }
2690 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
2691 }
2692
Jim Miller20daffd2013-10-07 14:59:53 -07002693 public void dispatchScreenTurnedOn() {
2694 synchronized (this) {
2695 mScreenOn = true;
2696 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07002697 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07002698 }
2699
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002700 public void dispatchScreenTurnedOff() {
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002701 synchronized (this) {
Jim Miller20daffd2013-10-07 14:59:53 -07002702 mScreenOn = false;
2703 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07002704 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07002705 }
2706
Selim Cinek99415392016-09-09 14:58:41 -07002707 public void dispatchDreamingStarted() {
2708 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 1, 0));
2709 }
2710
2711 public void dispatchDreamingStopped() {
2712 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 0, 0));
2713 }
2714
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002715 public boolean isDeviceInteractive() {
2716 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07002717 }
Jim Miller52a61332014-11-12 19:29:51 -08002718
Jorim Jaggi95e40382015-09-16 15:53:42 -07002719 public boolean isGoingToSleep() {
2720 return mGoingToSleep;
2721 }
2722
Jim Miller52a61332014-11-12 19:29:51 -08002723 /**
2724 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002725 *
Wink Savilled09c4ca2014-11-22 10:08:16 -08002726 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08002727 */
Jayachandran Cf5436a62019-11-08 18:22:45 -08002728 public int getNextSubIdForState(int state) {
Jim Miller52a61332014-11-12 19:29:51 -08002729 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08002730 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08002731 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
2732 for (int i = 0; i < list.size(); i++) {
2733 final SubscriptionInfo info = list.get(i);
2734 final int id = info.getSubscriptionId();
Lucas Dupin10960bd2019-09-27 16:08:32 -07002735 int slotId = getSlotId(id);
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002736 if (state == getSimState(id) && bestSlotId > slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08002737 resultId = id;
2738 bestSlotId = slotId;
2739 }
2740 }
2741 return resultId;
2742 }
2743
2744 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
2745 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
2746 for (int i = 0; i < list.size(); i++) {
2747 SubscriptionInfo info = list.get(i);
2748 if (subId == info.getSubscriptionId()) return info;
2749 }
2750 return null; // not found
2751 }
Jason Monkab525272015-07-13 17:02:49 -04002752
Alex Chauff7653d2018-02-01 17:18:08 +00002753 /**
2754 * @return a cached version of DevicePolicyManager.isLogoutEnabled()
2755 */
2756 public boolean isLogoutEnabled() {
2757 return mLogoutEnabled;
2758 }
2759
2760 private void updateLogoutEnabled() {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002761 checkIsHandlerThread();
Alex Chauff7653d2018-02-01 17:18:08 +00002762 boolean logoutEnabled = mDevicePolicyManager.isLogoutEnabled();
2763 if (mLogoutEnabled != logoutEnabled) {
2764 mLogoutEnabled = logoutEnabled;
Dave Mankoff4b0ab652019-08-07 09:49:20 -04002765
Alex Chauff7653d2018-02-01 17:18:08 +00002766 for (int i = 0; i < mCallbacks.size(); i++) {
2767 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2768 if (cb != null) {
2769 cb.onLogoutEnabledChanged();
2770 }
2771 }
2772 }
2773 }
2774
Adrian Roos30a2ae62018-04-25 19:09:50 +02002775 private void checkIsHandlerThread() {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002776 if (!mHandler.getLooper().isCurrentThread()) {
Dave Mankoff898e1bb2019-09-25 17:54:19 -04002777 Log.wtfStack(TAG, "must call on mHandler's thread "
Adrian Roos30a2ae62018-04-25 19:09:50 +02002778 + mHandler.getLooper().getThread() + ", not " + Thread.currentThread());
2779 }
2780 }
2781
Lucas Dupin64171fe2019-10-30 14:28:29 -07002782 @Override
Jason Monkab525272015-07-13 17:02:49 -04002783 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2784 pw.println("KeyguardUpdateMonitor state:");
2785 pw.println(" SIM States:");
2786 for (SimData data : mSimDatas.values()) {
2787 pw.println(" " + data.toString());
2788 }
2789 pw.println(" Subs:");
2790 if (mSubscriptionInfo != null) {
2791 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
2792 pw.println(" " + mSubscriptionInfo.get(i));
2793 }
2794 }
Malcolm Chen5c63b512019-08-13 13:24:07 -07002795 pw.println(" Current active data subId=" + mActiveMobileDataSubscription);
Jason Monkab525272015-07-13 17:02:49 -04002796 pw.println(" Service states:");
2797 for (int subId : mServiceStates.keySet()) {
2798 pw.println(" " + subId + "=" + mServiceStates.get(subId));
2799 }
Jim Millerd72d5ac2015-09-29 18:55:32 -07002800 if (mFpm != null && mFpm.isHardwareDetected()) {
2801 final int userId = ActivityManager.getCurrentUser();
2802 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
2803 pw.println(" Fingerprint state (user=" + userId + ")");
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002804 pw.println(" allowed=" + isUnlockingWithBiometricAllowed());
Jim Millerd72d5ac2015-09-29 18:55:32 -07002805 pw.println(" auth'd=" + mUserFingerprintAuthenticated.get(userId));
2806 pw.println(" authSinceBoot="
2807 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
2808 pw.println(" disabled(DPM)=" + isFingerprintDisabled(userId));
2809 pw.println(" possible=" + isUnlockWithFingerprintPossible(userId));
Adrian Roos2aaf9442018-11-20 16:57:33 +01002810 pw.println(" listening: actual=" + mFingerprintRunningState
2811 + " expected=" + (shouldListenForFingerprint() ? 1 : 0));
Jim Millerd72d5ac2015-09-29 18:55:32 -07002812 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
Jim Millerd72d5ac2015-09-29 18:55:32 -07002813 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
2814 }
Kevin Chynb7b54a62018-09-28 18:48:12 -07002815 if (mFaceManager != null && mFaceManager.isHardwareDetected()) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002816 final int userId = ActivityManager.getCurrentUser();
2817 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
2818 pw.println(" Face authentication state (user=" + userId + ")");
2819 pw.println(" allowed=" + isUnlockingWithBiometricAllowed());
2820 pw.println(" auth'd=" + mUserFaceAuthenticated.get(userId));
2821 pw.println(" authSinceBoot="
2822 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
2823 pw.println(" disabled(DPM)=" + isFaceDisabled(userId));
2824 pw.println(" possible=" + isUnlockWithFacePossible(userId));
2825 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
2826 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
Lucas Dupin7d95f152019-07-17 16:25:54 -07002827 pw.println(" enabledByUser=" + mFaceSettingEnabledForUser.get(userId));
Kevin Chynd0381892019-07-11 16:25:36 -07002828 pw.println(" mSecureCameraLaunched=" + mSecureCameraLaunched);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002829 }
Jason Monkab525272015-07-13 17:02:49 -04002830 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002831}