blob: c5847a8a7e3171a3fde1998f67841f73929cf4fe [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;
Jorim Jaggidadafd42016-09-30 07:20:25 -070021import static android.content.Intent.ACTION_USER_UNLOCKED;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070022import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
23import static android.os.BatteryManager.BATTERY_STATUS_FULL;
24import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
25import static android.os.BatteryManager.EXTRA_HEALTH;
26import static android.os.BatteryManager.EXTRA_LEVEL;
27import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
28import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE;
29import static android.os.BatteryManager.EXTRA_PLUGGED;
30import static android.os.BatteryManager.EXTRA_STATUS;
Fabian Kozynskiccde55d2019-06-26 10:06:09 -040031import static android.telephony.PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070032
Lucas Dupin8eec2682019-07-01 16:41:17 -070033import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT;
34import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW;
35import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT;
36import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT;
37import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
38
Adrian Roos30a2ae62018-04-25 19:09:50 +020039import android.annotation.AnyThread;
40import android.annotation.MainThread;
Jorim Jaggiccdfa932015-04-13 16:29:48 -070041import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070042import android.app.ActivityTaskManager;
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020043import android.app.AlarmManager;
Adrian Roos30a2ae62018-04-25 19:09:50 +020044import android.app.Instrumentation;
Jim Miller8f09fd22013-03-14 19:04:28 -070045import android.app.PendingIntent;
Sudheer Shanka2c4522c2016-08-27 20:53:28 -070046import android.app.UserSwitchObserver;
Jim Millerb0304762012-03-13 20:01:25 -070047import android.app.admin.DevicePolicyManager;
Adrian Roos46842d92014-03-27 14:58:03 +010048import android.app.trust.TrustManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080049import android.content.BroadcastReceiver;
Jorim Jaggi031f7952016-09-01 16:39:26 -070050import android.content.ComponentName;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080051import android.content.Context;
52import android.content.Intent;
53import android.content.IntentFilter;
Adrian Roosca8a2162017-08-17 19:00:58 +020054import android.content.pm.IPackageManager;
Jorim Jaggi031f7952016-09-01 16:39:26 -070055import android.content.pm.PackageManager;
56import android.content.pm.ResolveInfo;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080057import android.database.ContentObserver;
Kevin Chynb7b54a62018-09-28 18:48:12 -070058import android.hardware.biometrics.BiometricManager;
Kevin Chyn56233ab2018-09-20 17:10:57 -070059import android.hardware.biometrics.BiometricSourceType;
Kevin Chynb7b54a62018-09-28 18:48:12 -070060import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020061import android.hardware.face.FaceManager;
Jorim Jaggi86bed402015-08-20 18:20:02 -070062import android.hardware.fingerprint.FingerprintManager;
63import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
64import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
Jim Miller47088bb2009-11-24 00:40:16 -080065import android.media.AudioManager;
Jim Miller79a444a2011-02-15 15:02:11 -080066import android.os.BatteryManager;
Jim Miller9f0753f2015-03-23 23:59:22 -070067import android.os.CancellationSignal;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080068import android.os.Handler;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070069import android.os.IRemoteCallback;
Jason Monk7bb59302018-05-10 19:38:18 -070070import android.os.Looper;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080071import android.os.Message;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070072import android.os.RemoteException;
Adrian Roosca8a2162017-08-17 19:00:58 +020073import android.os.ServiceManager;
Nick Desaulniers1d396752016-07-25 15:05:33 -070074import android.os.Trace;
Amith Yamasanie8e93a12013-05-09 18:12:30 -070075import android.os.UserHandle;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070076import android.os.UserManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080077import android.provider.Settings;
Kevin Chyn36778ff2017-09-07 19:55:38 -070078import android.service.dreams.DreamService;
79import android.service.dreams.IDreamManager;
Fabian Kozynskiccde55d2019-06-26 10:06:09 -040080import android.telephony.PhoneStateListener;
Etan Cohen47051d82015-07-06 16:19:04 -070081import android.telephony.ServiceState;
Jim Miller52a61332014-11-12 19:29:51 -080082import android.telephony.SubscriptionInfo;
Jim Miller52a61332014-11-12 19:29:51 -080083import android.telephony.SubscriptionManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -080084import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Jim Millerc23024d2010-02-24 15:37:00 -080085import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080086import android.util.Log;
Adrian Roos46842d92014-03-27 14:58:03 +010087import android.util.SparseBooleanArray;
88
Lucas Dupin7517b5d2017-08-22 12:51:25 -070089import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi86bed402015-08-20 18:20:02 -070090import com.android.internal.telephony.IccCardConstants;
91import com.android.internal.telephony.IccCardConstants.State;
92import com.android.internal.telephony.PhoneConstants;
93import com.android.internal.telephony.TelephonyIntents;
Adrian Roos30a2ae62018-04-25 19:09:50 +020094import com.android.internal.util.Preconditions;
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;
Winson Chung2cf6ad82017-11-09 17:36:59 -080097import com.android.systemui.shared.system.ActivityManagerWrapper;
Winson Chung67f5c8b2018-09-24 12:09:19 -070098import com.android.systemui.shared.system.TaskStackChangeListener;
Lucas Dupin9e484aa2019-06-24 13:38:00 -070099import com.android.systemui.statusbar.phone.KeyguardBypassController;
Kevin Chyn1123ba72018-10-26 10:34:06 -0700100
Kevin Chyn36778ff2017-09-07 19:55:38 -0700101import com.google.android.collect.Lists;
102
Jason Monkab525272015-07-13 17:02:49 -0400103import java.io.FileDescriptor;
104import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -0700105import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800106import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -0800107import java.util.HashMap;
108import java.util.List;
109import java.util.Map.Entry;
Robert Snoeberger9c1074f2018-12-07 17:35:21 -0500110import java.util.TimeZone;
Lucas Dupin3d053532019-01-29 12:35:22 -0800111import java.util.function.Consumer;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800112
113/**
114 * Watches for updates that may be interesting to the keyguard, and provides
115 * the up to date information as well as a registration for callbacks that care
116 * to be updated.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800117 */
Adrian Roos46842d92014-03-27 14:58:03 +0100118public class KeyguardUpdateMonitor implements TrustManager.TrustListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800119
Jim Millerbbf1a742012-07-17 18:30:30 -0700120 private static final String TAG = "KeyguardUpdateMonitor";
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100121 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller52a61332014-11-12 19:29:51 -0800122 private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700123 private static final boolean DEBUG_FACE = true;
Jim Millerbbf1a742012-07-17 18:30:30 -0700124 private static final int LOW_BATTERY_THRESHOLD = 20;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800125
Jorim Jaggie7b12522014-08-06 16:41:21 +0200126 private static final String ACTION_FACE_UNLOCK_STARTED
127 = "com.android.facelock.FACE_UNLOCK_STARTED";
128 private static final String ACTION_FACE_UNLOCK_STOPPED
129 = "com.android.facelock.FACE_UNLOCK_STOPPED";
130
Jim Millerbbf1a742012-07-17 18:30:30 -0700131 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800132 private static final int MSG_TIME_UPDATE = 301;
133 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800134 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800135 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800136 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700137 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700138 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500139 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700140 private static final int MSG_KEYGUARD_RESET = 312;
Jim Millerf41fc962014-06-18 16:33:51 -0700141 private static final int MSG_BOOT_COMPLETED = 313;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500142 private static final int MSG_USER_SWITCH_COMPLETE = 314;
Jim Millerf41fc962014-06-18 16:33:51 -0700143 private static final int MSG_USER_INFO_CHANGED = 317;
144 private static final int MSG_REPORT_EMERGENCY_CALL_ACTION = 318;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700145 private static final int MSG_STARTED_WAKING_UP = 319;
146 private static final int MSG_FINISHED_GOING_TO_SLEEP = 320;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700147 private static final int MSG_STARTED_GOING_TO_SLEEP = 321;
Adrian Roosb6011622014-05-14 15:52:53 +0200148 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700149 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
150 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400151 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700152 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700153 private static final int MSG_SCREEN_TURNED_ON = 331;
154 private static final int MSG_SCREEN_TURNED_OFF = 332;
Selim Cinek99415392016-09-09 14:58:41 -0700155 private static final int MSG_DREAMING_STATE_CHANGED = 333;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700156 private static final int MSG_USER_UNLOCKED = 334;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700157 private static final int MSG_ASSISTANT_STACK_CHANGED = 335;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200158 private static final int MSG_BIOMETRIC_AUTHENTICATION_CONTINUE = 336;
Alex Chauff7653d2018-02-01 17:18:08 +0000159 private static final int MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED = 337;
Bill Linef81cbd2018-07-05 17:48:49 +0800160 private static final int MSG_TELEPHONY_CAPABLE = 338;
Robert Snoeberger9c1074f2018-12-07 17:35:21 -0500161 private static final int MSG_TIMEZONE_UPDATE = 339;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800162
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200163 /** Biometric authentication state: Not listening. */
164 private static final int BIOMETRIC_STATE_STOPPED = 0;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700165
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200166 /** Biometric authentication state: Listening. */
167 private static final int BIOMETRIC_STATE_RUNNING = 1;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700168
169 /**
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200170 * Biometric authentication: Cancelling and waiting for the relevant biometric service to
Jorim Jaggi86bed402015-08-20 18:20:02 -0700171 * send us the confirmation that cancellation has happened.
172 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200173 private static final int BIOMETRIC_STATE_CANCELLING = 2;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700174
175 /**
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200176 * Biometric state: During cancelling we got another request to start listening, so when we
Jorim Jaggi86bed402015-08-20 18:20:02 -0700177 * receive the cancellation done signal, we should start listening again.
178 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200179 private static final int BIOMETRIC_STATE_CANCELLING_RESTARTING = 3;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700180
Lucas Dupin51996bb2019-05-16 17:56:43 -0700181 private static final int BIOMETRIC_HELP_FINGERPRINT_NOT_RECOGNIZED = -1;
182 public static final int BIOMETRIC_HELP_FACE_NOT_RECOGNIZED = -2;
183
Adrian Roos0c859ae2015-11-23 16:47:50 -0800184 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
185
Jorim Jaggi031f7952016-09-01 16:39:26 -0700186 private static final ComponentName FALLBACK_HOME_COMPONENT = new ComponentName(
187 "com.android.settings", "com.android.settings.FallbackHome");
188
Adrian Roosca8a2162017-08-17 19:00:58 +0200189
190 /**
191 * If true, the system is in the half-boot-to-decryption-screen state.
192 * Prudently disable lockscreen.
193 */
194 public static final boolean CORE_APPS_ONLY;
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700195
Adrian Roosca8a2162017-08-17 19:00:58 +0200196 static {
197 try {
198 CORE_APPS_ONLY = IPackageManager.Stub.asInterface(
199 ServiceManager.getService("package")).isOnlyCoreApps();
200 } catch (RemoteException e) {
201 throw e.rethrowFromSystemServer();
202 }
203 }
204
Jim Millerdcb3d842012-08-23 19:18:12 -0700205 private static KeyguardUpdateMonitor sInstance;
206
Jim Millerbbf1a742012-07-17 18:30:30 -0700207 private final Context mContext;
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800208 private final boolean mIsPrimaryUser;
Jim Miller52a61332014-11-12 19:29:51 -0800209 HashMap<Integer, SimData> mSimDatas = new HashMap<Integer, SimData>();
Etan Cohen47051d82015-07-06 16:19:04 -0700210 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700211
Jim Millerbbf1a742012-07-17 18:30:30 -0700212 private int mRingMode;
213 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400214 private boolean mKeyguardIsVisible;
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700215 private boolean mKeyguardGoingAway;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700216 private boolean mGoingToSleep;
Adrian Roosb6011622014-05-14 15:52:53 +0200217 private boolean mBouncer;
Lucas Dupine0516d52019-02-05 17:54:06 -0500218 private boolean mAuthInterruptActive;
Adam Cohen4eb36cf2012-11-07 11:45:30 -0800219 private boolean mBootCompleted;
Jorim Jaggi031f7952016-09-01 16:39:26 -0700220 private boolean mNeedsSlowUnlockTransition;
Jorim Jaggid11d1a92016-08-16 16:02:32 -0700221 private boolean mHasLockscreenWallpaper;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700222 private boolean mAssistantVisible;
223 private boolean mKeyguardOccluded;
Kevin Chyn6951d3d2019-06-10 14:07:13 -0700224 private boolean mSecureCameraLaunched;
Bill Linef81cbd2018-07-05 17:48:49 +0800225 @VisibleForTesting
226 protected boolean mTelephonyCapable;
Jim Millerbbf1a742012-07-17 18:30:30 -0700227
Jim Millerdcb3d842012-08-23 19:18:12 -0700228 // Device provisioning state
Jim Millerbbf1a742012-07-17 18:30:30 -0700229 private boolean mDeviceProvisioned;
230
Jim Millerdcb3d842012-08-23 19:18:12 -0700231 // Battery status
Jim Millerbbf1a742012-07-17 18:30:30 -0700232 private BatteryStatus mBatteryStatus;
233
Lucas Dupin3d053532019-01-29 12:35:22 -0800234 @VisibleForTesting
235 protected StrongAuthTracker mStrongAuthTracker;
Jim Millerbbf1a742012-07-17 18:30:30 -0700236
Jim Miller6212cc02012-09-05 17:35:31 -0700237 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700238 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800239 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700240
Brian Colonnaa5239892013-04-15 11:45:40 -0400241 private boolean mSwitchingUser;
242
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700243 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700244 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800245 private SubscriptionManager mSubscriptionManager;
246 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700247 private TrustManager mTrustManager;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700248 private UserManager mUserManager;
Lucas Dupin9e484aa2019-06-24 13:38:00 -0700249 private KeyguardBypassController mKeyguardBypassController;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200250 private int mFingerprintRunningState = BIOMETRIC_STATE_STOPPED;
251 private int mFaceRunningState = BIOMETRIC_STATE_STOPPED;
Michal Karpinskic52f8672016-11-18 11:32:45 +0000252 private LockPatternUtils mLockPatternUtils;
Kevin Chyn36778ff2017-09-07 19:55:38 -0700253 private final IDreamManager mDreamManager;
254 private boolean mIsDreaming;
Alex Chauff7653d2018-02-01 17:18:08 +0000255 private final DevicePolicyManager mDevicePolicyManager;
256 private boolean mLogoutEnabled;
Lucas Dupinca88e5f2019-05-14 16:11:08 -0700257 // If the user long pressed the lock icon, disabling face auth for the current session.
258 private boolean mLockIconPressed;
Jim Miller20daffd2013-10-07 14:59:53 -0700259
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700260 /**
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200261 * Short delay before restarting biometric authentication after a successful try
262 * This should be slightly longer than the time between on<biometric>Authenticated
263 * (e.g. onFingerprintAuthenticated) and setKeyguardGoingAway(true).
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700264 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200265 private static final int BIOMETRIC_CONTINUE_DELAY_MS = 500;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700266
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700267 // If the HAL dies or is unable to authenticate, keyguard should retry after a short delay
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200268 private int mHardwareFingerprintUnavailableRetryCount = 0;
269 private int mHardwareFaceUnavailableRetryCount = 0;
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700270 private static final int HAL_ERROR_RETRY_TIMEOUT = 500; // ms
271 private static final int HAL_ERROR_RETRY_MAX = 10;
Kevin Chyn0c45b072017-04-24 16:27:11 -0700272
Jason Monk7bb59302018-05-10 19:38:18 -0700273 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700274 @Override
275 public void handleMessage(Message msg) {
276 switch (msg.what) {
277 case MSG_TIME_UPDATE:
278 handleTimeUpdate();
279 break;
Robert Snoeberger9c1074f2018-12-07 17:35:21 -0500280 case MSG_TIMEZONE_UPDATE:
281 handleTimeZoneUpdate((String) msg.obj);
282 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700283 case MSG_BATTERY_UPDATE:
284 handleBatteryUpdate((BatteryStatus) msg.obj);
285 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700286 case MSG_SIM_STATE_CHANGE:
Jim Miller52a61332014-11-12 19:29:51 -0800287 handleSimStateChange(msg.arg1, msg.arg2, (State) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700288 break;
289 case MSG_RINGER_MODE_CHANGED:
290 handleRingerModeChange(msg.arg1);
291 break;
292 case MSG_PHONE_STATE_CHANGED:
Adrian Roosb6011622014-05-14 15:52:53 +0200293 handlePhoneStateChanged((String) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700294 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700295 case MSG_DEVICE_PROVISIONED:
296 handleDeviceProvisioned();
297 break;
298 case MSG_DPM_STATE_CHANGED:
299 handleDevicePolicyManagerStateChanged();
300 break;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500301 case MSG_USER_SWITCHING:
Adrian Roosb6011622014-05-14 15:52:53 +0200302 handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500303 break;
304 case MSG_USER_SWITCH_COMPLETE:
305 handleUserSwitchComplete(msg.arg1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700306 break;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700307 case MSG_KEYGUARD_RESET:
308 handleKeyguardReset();
309 break;
Adrian Roosb6011622014-05-14 15:52:53 +0200310 case MSG_KEYGUARD_BOUNCER_CHANGED:
311 handleKeyguardBouncerChanged(msg.arg1);
312 break;
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800313 case MSG_BOOT_COMPLETED:
314 handleBootCompleted();
315 break;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700316 case MSG_USER_INFO_CHANGED:
317 handleUserInfoChanged(msg.arg1);
318 break;
Brian Colonna7fce3802013-09-17 15:51:32 -0400319 case MSG_REPORT_EMERGENCY_CALL_ACTION:
320 handleReportEmergencyCallAction();
321 break;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700322 case MSG_STARTED_GOING_TO_SLEEP:
323 handleStartedGoingToSleep(msg.arg1);
324 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700325 case MSG_FINISHED_GOING_TO_SLEEP:
326 handleFinishedGoingToSleep(msg.arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700327 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700328 case MSG_STARTED_WAKING_UP:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700329 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_STARTED_WAKING_UP");
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700330 handleStartedWakingUp();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700331 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -0700332 break;
Jorim Jaggie7b12522014-08-06 16:41:21 +0200333 case MSG_FACE_UNLOCK_STATE_CHANGED:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700334 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_FACE_UNLOCK_STATE_CHANGED");
Adrian Roos4a410172014-08-20 17:41:44 +0200335 handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700336 Trace.endSection();
Jorim Jaggie7b12522014-08-06 16:41:21 +0200337 break;
Jim Miller52a61332014-11-12 19:29:51 -0800338 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
339 handleSimSubscriptionInfoChanged();
340 break;
Jason Monk052082c2015-06-11 11:35:23 -0400341 case MSG_AIRPLANE_MODE_CHANGED:
342 handleAirplaneModeChanged();
343 break;
Etan Cohen47051d82015-07-06 16:19:04 -0700344 case MSG_SERVICE_STATE_CHANGE:
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000345 handleServiceStateChange(msg.arg1, (ServiceState) msg.obj);
Etan Cohen47051d82015-07-06 16:19:04 -0700346 break;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700347 case MSG_SCREEN_TURNED_ON:
348 handleScreenTurnedOn();
349 break;
350 case MSG_SCREEN_TURNED_OFF:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700351 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_SCREEN_TURNED_ON");
Jorim Jaggif1518da2015-07-30 11:56:36 -0700352 handleScreenTurnedOff();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700353 Trace.endSection();
Jorim Jaggif1518da2015-07-30 11:56:36 -0700354 break;
Selim Cinek99415392016-09-09 14:58:41 -0700355 case MSG_DREAMING_STATE_CHANGED:
356 handleDreamingStateChanged(msg.arg1);
357 break;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700358 case MSG_USER_UNLOCKED:
359 handleUserUnlocked();
360 break;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700361 case MSG_ASSISTANT_STACK_CHANGED:
Lucas Dupin3d053532019-01-29 12:35:22 -0800362 setAssistantVisible((boolean) msg.obj);
Kevin Chyn2fefd462017-04-28 12:18:19 -0700363 break;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200364 case MSG_BIOMETRIC_AUTHENTICATION_CONTINUE:
365 updateBiometricListeningState();
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700366 break;
Alex Chauff7653d2018-02-01 17:18:08 +0000367 case MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED:
368 updateLogoutEnabled();
369 break;
Bill Linef81cbd2018-07-05 17:48:49 +0800370 case MSG_TELEPHONY_CAPABLE:
Lucas Dupin3d053532019-01-29 12:35:22 -0800371 updateTelephonyCapable((boolean) msg.obj);
Bill Linef81cbd2018-07-05 17:48:49 +0800372 break;
Jason Monk7bb59302018-05-10 19:38:18 -0700373 default:
374 super.handleMessage(msg);
375 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700376 }
377 }
378 };
379
Lucas Dupin7d95f152019-07-17 16:25:54 -0700380 private SparseBooleanArray mFaceSettingEnabledForUser = new SparseBooleanArray();
Kevin Chynb7b54a62018-09-28 18:48:12 -0700381 private BiometricManager mBiometricManager;
382 private IBiometricEnabledOnKeyguardCallback mBiometricEnabledCallback =
383 new IBiometricEnabledOnKeyguardCallback.Stub() {
384 @Override
Lucas Dupin7d95f152019-07-17 16:25:54 -0700385 public void onChanged(BiometricSourceType type, boolean enabled, int userId)
386 throws RemoteException {
Kevin Chynb7b54a62018-09-28 18:48:12 -0700387 if (type == BiometricSourceType.FACE) {
Lucas Dupin7d95f152019-07-17 16:25:54 -0700388 mFaceSettingEnabledForUser.put(userId, enabled);
Kevin Chyn1e043d472019-03-11 14:48:17 -0700389 updateFaceListeningState();
Kevin Chynb7b54a62018-09-28 18:48:12 -0700390 }
391 }
392 };
393
Fabian Kozynskiccde55d2019-06-26 10:06:09 -0400394 private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
395 @Override
396 public void onActiveDataSubscriptionIdChanged(int subId) {
397 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
398 }
399 };
400
Wink Savilled09c4ca2014-11-22 10:08:16 -0800401 private OnSubscriptionsChangedListener mSubscriptionListener =
402 new OnSubscriptionsChangedListener() {
Jim Miller52a61332014-11-12 19:29:51 -0800403 @Override
Wink Savilled09c4ca2014-11-22 10:08:16 -0800404 public void onSubscriptionsChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800405 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
406 }
407 };
408
Adrian Roos46842d92014-03-27 14:58:03 +0100409 private SparseBooleanArray mUserHasTrust = new SparseBooleanArray();
Adrian Roos7861c662014-07-25 15:37:28 +0200410 private SparseBooleanArray mUserTrustIsManaged = new SparseBooleanArray();
Jim Miller9f0753f2015-03-23 23:59:22 -0700411 private SparseBooleanArray mUserFingerprintAuthenticated = new SparseBooleanArray();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200412 private SparseBooleanArray mUserFaceAuthenticated = new SparseBooleanArray();
Adrian Roos4a410172014-08-20 17:41:44 +0200413 private SparseBooleanArray mUserFaceUnlockRunning = new SparseBooleanArray();
Adrian Roos46842d92014-03-27 14:58:03 +0100414
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700415 private static int sCurrentUser;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200416 private Runnable mUpdateBiometricListeningState = this::updateBiometricListeningState;
Adrian Roos30a2ae62018-04-25 19:09:50 +0200417 private static boolean sDisableHandlerCheckForTesting;
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700418
419 public synchronized static void setCurrentUser(int currentUser) {
420 sCurrentUser = currentUser;
421 }
422
423 public synchronized static int getCurrentUser() {
424 return sCurrentUser;
425 }
426
Adrian Roos46842d92014-03-27 14:58:03 +0100427 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700428 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200429 checkIsHandlerThread();
Adrian Roos46842d92014-03-27 14:58:03 +0100430 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200431 for (int i = 0; i < mCallbacks.size(); i++) {
432 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
433 if (cb != null) {
434 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700435 if (enabled && flags != 0) {
436 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200437 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200438 }
439 }
Adrian Roos46842d92014-03-27 14:58:03 +0100440 }
441
Lucas Dupinef886542018-01-03 16:03:07 -0800442 @Override
443 public void onTrustError(CharSequence message) {
444 dispatchErrorMessage(message);
445 }
446
Adrian Roos30a2ae62018-04-25 19:09:50 +0200447 private void handleSimSubscriptionInfoChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800448 if (DEBUG_SIM_STATES) {
449 Log.v(TAG, "onSubscriptionInfoChanged()");
Fabian Kozynskiccde55d2019-06-26 10:06:09 -0400450 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList(false);
Wink Savilled09c4ca2014-11-22 10:08:16 -0800451 if (sil != null) {
452 for (SubscriptionInfo subInfo : sil) {
453 Log.v(TAG, "SubInfo:" + subInfo);
454 }
455 } else {
456 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800457 }
458 }
459 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
460
461 // Hack level over 9000: Because the subscription id is not yet valid when we see the
462 // first update in handleSimStateChange, we need to force refresh all all SIM states
463 // so the subscription id for them is consistent.
Richard Choue0381b82018-04-24 03:48:59 +0000464 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
465 for (int i = 0; i < subscriptionInfos.size(); i++) {
466 SubscriptionInfo info = subscriptionInfos.get(i);
467 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
468 if (changed) {
469 changedSubscriptions.add(info);
470 }
471 }
472 for (int i = 0; i < changedSubscriptions.size(); i++) {
473 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800474 for (int j = 0; j < mCallbacks.size(); j++) {
475 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
476 if (cb != null) {
477 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
478 }
479 }
480 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500481 for (int j = 0; j < mCallbacks.size(); j++) {
482 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
483 if (cb != null) {
484 cb.onRefreshCarrierInfo();
485 }
486 }
Jim Miller52a61332014-11-12 19:29:51 -0800487 }
488
Jason Monk052082c2015-06-11 11:35:23 -0400489 private void handleAirplaneModeChanged() {
490 for (int j = 0; j < mCallbacks.size(); j++) {
491 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
492 if (cb != null) {
493 cb.onRefreshCarrierInfo();
494 }
495 }
496 }
497
Wink Savilled09c4ca2014-11-22 10:08:16 -0800498 /** @return List of SubscriptionInfo records, maybe empty but never null */
Adrian Roos316bf542016-08-23 17:53:07 +0200499 public List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800500 List<SubscriptionInfo> sil = mSubscriptionInfo;
501 if (sil == null || forceReload) {
Fabian Kozynskiccde55d2019-06-26 10:06:09 -0400502 sil = mSubscriptionManager.getActiveSubscriptionInfoList(false);
Wink Savilled09c4ca2014-11-22 10:08:16 -0800503 }
504 if (sil == null) {
505 // getActiveSubscriptionInfoList was null callers expect an empty list.
506 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
507 } else {
508 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800509 }
510 return mSubscriptionInfo;
511 }
512
Adrian Roos7861c662014-07-25 15:37:28 +0200513 @Override
514 public void onTrustManagedChanged(boolean managed, int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200515 checkIsHandlerThread();
Adrian Roos7861c662014-07-25 15:37:28 +0200516 mUserTrustIsManaged.put(userId, managed);
517
518 for (int i = 0; i < mCallbacks.size(); i++) {
519 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
520 if (cb != null) {
521 cb.onTrustManagedChanged(userId);
522 }
523 }
524 }
525
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700526 /**
527 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is goingAway
528 * @param goingAway
529 */
530 public void setKeyguardGoingAway(boolean goingAway) {
531 mKeyguardGoingAway = goingAway;
Kevin Chyne22f1342017-09-26 10:03:38 -0700532 updateFingerprintListeningState();
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700533 }
534
Kevin Chyn2fefd462017-04-28 12:18:19 -0700535 /**
536 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is occluded
537 * @param occluded
538 */
539 public void setKeyguardOccluded(boolean occluded) {
540 mKeyguardOccluded = occluded;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200541 updateBiometricListeningState();
Kevin Chyn2fefd462017-04-28 12:18:19 -0700542 }
543
Kevin Chyn36778ff2017-09-07 19:55:38 -0700544 /**
Kevin Chyn6951d3d2019-06-10 14:07:13 -0700545 * Invoked when the secure camera is launched.
546 */
547 public void onCameraLaunched() {
548 mSecureCameraLaunched = true;
549 updateBiometricListeningState();
550 }
551
552 /**
Kevin Chyn36778ff2017-09-07 19:55:38 -0700553 * @return a cached version of DreamManager.isDreaming()
554 */
555 public boolean isDreaming() {
556 return mIsDreaming;
557 }
558
559 /**
560 * If the device is dreaming, awakens the device
561 */
562 public void awakenFromDream() {
563 if (mIsDreaming && mDreamManager != null) {
564 try {
565 mDreamManager.awaken();
566 } catch (RemoteException e) {
567 Log.e(TAG, "Unable to awaken from dream");
568 }
569 }
570 }
571
Lucas Dupin3d053532019-01-29 12:35:22 -0800572 @VisibleForTesting
573 protected void onFingerprintAuthenticated(int userId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700574 Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
Jim Miller9f0753f2015-03-23 23:59:22 -0700575 mUserFingerprintAuthenticated.put(userId, true);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700576 // Update/refresh trust state only if user can skip bouncer
577 if (getUserCanSkipBouncer(userId)) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200578 mTrustManager.unlockedByBiometricForUser(userId, BiometricSourceType.FINGERPRINT);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700579 }
Kevin Chyn625a0142017-04-10 14:53:59 -0700580 // Don't send cancel if authentication succeeds
581 mFingerprintCancelSignal = null;
Jim Millerf41fc962014-06-18 16:33:51 -0700582 for (int i = 0; i < mCallbacks.size(); i++) {
583 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
584 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200585 cb.onBiometricAuthenticated(userId, BiometricSourceType.FINGERPRINT);
Jim Millerf41fc962014-06-18 16:33:51 -0700586 }
587 }
Kevin Chyn2fefd462017-04-28 12:18:19 -0700588
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200589 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE),
590 BIOMETRIC_CONTINUE_DELAY_MS);
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700591
Kevin Chyn2fefd462017-04-28 12:18:19 -0700592 // Only authenticate fingerprint once when assistant is visible
593 mAssistantVisible = false;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700594
Nick Desaulniers1d396752016-07-25 15:05:33 -0700595 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700596 }
597
Jim Millerce7eb6d2015-04-03 19:29:13 -0700598 private void handleFingerprintAuthFailed() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700599 for (int i = 0; i < mCallbacks.size(); i++) {
600 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
601 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200602 cb.onBiometricAuthFailed(BiometricSourceType.FINGERPRINT);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700603 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700604 }
Lucas Dupin51996bb2019-05-16 17:56:43 -0700605 handleFingerprintHelp(BIOMETRIC_HELP_FINGERPRINT_NOT_RECOGNIZED,
606 mContext.getString(R.string.kg_fingerprint_not_recognized));
Jim Millerce7eb6d2015-04-03 19:29:13 -0700607 }
Jim Millerf41fc962014-06-18 16:33:51 -0700608
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700609 private void handleFingerprintAcquired(int acquireInfo) {
610 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
611 return;
612 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700613 for (int i = 0; i < mCallbacks.size(); i++) {
614 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
615 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200616 cb.onBiometricAcquired(BiometricSourceType.FINGERPRINT);
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700617 }
618 }
619 }
620
Jim Miller837fa7e2016-08-08 20:16:22 -0700621 private void handleFingerprintAuthenticated(int authUserId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700622 Trace.beginSection("KeyGuardUpdateMonitor#handlerFingerPrintAuthenticated");
Jim Millerf41fc962014-06-18 16:33:51 -0700623 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700624 final int userId;
625 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800626 userId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700627 } catch (RemoteException e) {
628 Log.e(TAG, "Failed to get current user id: ", e);
629 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700630 }
Jim Miller837fa7e2016-08-08 20:16:22 -0700631 if (userId != authUserId) {
632 Log.d(TAG, "Fingerprint authenticated for wrong user: " + authUserId);
633 return;
634 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700635 if (isFingerprintDisabled(userId)) {
636 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
637 return;
638 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700639 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700640 } finally {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200641 setFingerprintRunningState(BIOMETRIC_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700642 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700643 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700644 }
645
Jim Miller9f0753f2015-03-23 23:59:22 -0700646 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700647 for (int i = 0; i < mCallbacks.size(); i++) {
648 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
649 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200650 cb.onBiometricHelp(msgId, helpString, BiometricSourceType.FINGERPRINT);
Jim Miller9f0753f2015-03-23 23:59:22 -0700651 }
652 }
653 }
654
Kevin Chyn0c45b072017-04-24 16:27:11 -0700655 private Runnable mRetryFingerprintAuthentication = new Runnable() {
656 @Override
657 public void run() {
658 Log.w(TAG, "Retrying fingerprint after HW unavailable, attempt " +
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200659 mHardwareFingerprintUnavailableRetryCount);
Kevin Chyn0c45b072017-04-24 16:27:11 -0700660 updateFingerprintListeningState();
661 }
662 };
663
Jim Miller9f0753f2015-03-23 23:59:22 -0700664 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700665 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200666 && mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
667 setFingerprintRunningState(BIOMETRIC_STATE_STOPPED);
Kevin Chyn1123ba72018-10-26 10:34:06 -0700668 updateFingerprintListeningState();
Jorim Jaggi86bed402015-08-20 18:20:02 -0700669 } else {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200670 setFingerprintRunningState(BIOMETRIC_STATE_STOPPED);
Jorim Jaggi86bed402015-08-20 18:20:02 -0700671 }
Kevin Chyn0c45b072017-04-24 16:27:11 -0700672
673 if (msgId == FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE) {
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700674 if (mHardwareFingerprintUnavailableRetryCount < HAL_ERROR_RETRY_MAX) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200675 mHardwareFingerprintUnavailableRetryCount++;
Kevin Chyn0c45b072017-04-24 16:27:11 -0700676 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700677 mHandler.postDelayed(mRetryFingerprintAuthentication, HAL_ERROR_RETRY_TIMEOUT);
Kevin Chyn0c45b072017-04-24 16:27:11 -0700678 }
679 }
680
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700681 if (msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT) {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700682 mLockPatternUtils.requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700683 getCurrentUser());
684 }
685
Jim Miller9f0753f2015-03-23 23:59:22 -0700686 for (int i = 0; i < mCallbacks.size(); i++) {
687 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
688 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200689 cb.onBiometricError(msgId, errString, BiometricSourceType.FINGERPRINT);
Jim Millerf41fc962014-06-18 16:33:51 -0700690 }
691 }
692 }
693
Jorim Jaggi3a464782015-08-28 16:59:13 -0700694 private void handleFingerprintLockoutReset() {
695 updateFingerprintListeningState();
696 }
697
Jorim Jaggi86bed402015-08-20 18:20:02 -0700698 private void setFingerprintRunningState(int fingerprintRunningState) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200699 boolean wasRunning = mFingerprintRunningState == BIOMETRIC_STATE_RUNNING;
700 boolean isRunning = fingerprintRunningState == BIOMETRIC_STATE_RUNNING;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700701 mFingerprintRunningState = fingerprintRunningState;
Kevin Chynb4514d22019-04-15 13:47:25 -0700702 Log.d(TAG, "fingerprintRunningState: " + mFingerprintRunningState);
Jorim Jaggi86bed402015-08-20 18:20:02 -0700703 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
Kevin Chynb4514d22019-04-15 13:47:25 -0700704 // asynchronousness of the cancel cycle. So only notify them if the actually running state
Jorim Jaggi86bed402015-08-20 18:20:02 -0700705 // has changed.
706 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700707 notifyFingerprintRunningStateChanged();
708 }
709 }
710
711 private void notifyFingerprintRunningStateChanged() {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200712 checkIsHandlerThread();
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700713 for (int i = 0; i < mCallbacks.size(); i++) {
714 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
715 if (cb != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200716 cb.onBiometricRunningStateChanged(isFingerprintDetectionRunning(),
717 BiometricSourceType.FINGERPRINT);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700718 }
719 }
720 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200721
Lucas Dupin3d053532019-01-29 12:35:22 -0800722 @VisibleForTesting
723 protected void onFaceAuthenticated(int userId) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200724 Trace.beginSection("KeyGuardUpdateMonitor#onFaceAuthenticated");
725 mUserFaceAuthenticated.put(userId, true);
726 // Update/refresh trust state only if user can skip bouncer
727 if (getUserCanSkipBouncer(userId)) {
728 mTrustManager.unlockedByBiometricForUser(userId, BiometricSourceType.FACE);
729 }
730 // Don't send cancel if authentication succeeds
731 mFaceCancelSignal = null;
732 for (int i = 0; i < mCallbacks.size(); i++) {
733 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
734 if (cb != null) {
735 cb.onBiometricAuthenticated(userId,
736 BiometricSourceType.FACE);
737 }
738 }
739
740 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE),
741 BIOMETRIC_CONTINUE_DELAY_MS);
742
743 // Only authenticate face once when assistant is visible
744 mAssistantVisible = false;
745
746 Trace.endSection();
747 }
748
749 private void handleFaceAuthFailed() {
Lucas Dupin38314812019-02-15 14:10:55 -0800750 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200751 for (int i = 0; i < mCallbacks.size(); i++) {
752 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
753 if (cb != null) {
754 cb.onBiometricAuthFailed(BiometricSourceType.FACE);
755 }
756 }
Lucas Dupin51996bb2019-05-16 17:56:43 -0700757 handleFaceHelp(BIOMETRIC_HELP_FACE_NOT_RECOGNIZED,
758 mContext.getString(R.string.kg_face_not_recognized));
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200759 }
760
761 private void handleFaceAcquired(int acquireInfo) {
762 if (acquireInfo != FaceManager.FACE_ACQUIRED_GOOD) {
763 return;
764 }
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700765 if (DEBUG_FACE) Log.d(TAG, "Face acquired");
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200766 for (int i = 0; i < mCallbacks.size(); i++) {
767 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
768 if (cb != null) {
769 cb.onBiometricAcquired(BiometricSourceType.FACE);
770 }
771 }
772 }
773
774 private void handleFaceAuthenticated(int authUserId) {
775 Trace.beginSection("KeyGuardUpdateMonitor#handlerFaceAuthenticated");
776 try {
Lucas Dupin9bae9c22019-06-04 16:37:40 -0700777 if (mGoingToSleep) {
778 Log.d(TAG, "Aborted successful auth because device is going to sleep.");
779 return;
780 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200781 final int userId;
782 try {
783 userId = ActivityManager.getService().getCurrentUser().id;
784 } catch (RemoteException e) {
785 Log.e(TAG, "Failed to get current user id: ", e);
786 return;
787 }
788 if (userId != authUserId) {
789 Log.d(TAG, "Face authenticated for wrong user: " + authUserId);
790 return;
791 }
792 if (isFaceDisabled(userId)) {
793 Log.d(TAG, "Face authentication disabled by DPM for userId: " + userId);
794 return;
795 }
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700796 if (DEBUG_FACE) Log.d(TAG, "Face auth succeeded for user " + userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200797 onFaceAuthenticated(userId);
798 } finally {
799 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
800 }
801 Trace.endSection();
802 }
803
804 private void handleFaceHelp(int msgId, String helpString) {
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700805 if (DEBUG_FACE) Log.d(TAG, "Face help received: " + helpString);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200806 for (int i = 0; i < mCallbacks.size(); i++) {
807 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
808 if (cb != null) {
809 cb.onBiometricHelp(msgId, helpString, BiometricSourceType.FACE);
810 }
811 }
812 }
813
814 private Runnable mRetryFaceAuthentication = new Runnable() {
815 @Override
816 public void run() {
817 Log.w(TAG, "Retrying face after HW unavailable, attempt " +
818 mHardwareFaceUnavailableRetryCount);
819 updateFaceListeningState();
820 }
821 };
822
823 private void handleFaceError(int msgId, String errString) {
Lucas Dupin71d2fb22019-05-30 18:26:54 -0700824 if (DEBUG_FACE) Log.d(TAG, "Face error received: " + errString);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200825 if (msgId == FaceManager.FACE_ERROR_CANCELED
826 && mFaceRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
827 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
Kevin Chyn1123ba72018-10-26 10:34:06 -0700828 updateFaceListeningState();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200829 } else {
830 setFaceRunningState(BIOMETRIC_STATE_STOPPED);
831 }
832
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700833 if (msgId == FaceManager.FACE_ERROR_HW_UNAVAILABLE
834 || msgId == FaceManager.FACE_ERROR_UNABLE_TO_PROCESS) {
835 if (mHardwareFaceUnavailableRetryCount < HAL_ERROR_RETRY_MAX) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200836 mHardwareFaceUnavailableRetryCount++;
837 mHandler.removeCallbacks(mRetryFaceAuthentication);
Kevin Chyne1ac0c02019-07-26 17:54:46 -0700838 mHandler.postDelayed(mRetryFaceAuthentication, HAL_ERROR_RETRY_TIMEOUT);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200839 }
840 }
841
842 if (msgId == FaceManager.FACE_ERROR_LOCKOUT_PERMANENT) {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700843 mLockPatternUtils.requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200844 getCurrentUser());
845 }
846
847 for (int i = 0; i < mCallbacks.size(); i++) {
848 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
849 if (cb != null) {
850 cb.onBiometricError(msgId, errString,
851 BiometricSourceType.FACE);
852 }
853 }
854 }
855
856 private void handleFaceLockoutReset() {
857 updateFaceListeningState();
858 }
859
860 private void setFaceRunningState(int faceRunningState) {
861 boolean wasRunning = mFaceRunningState == BIOMETRIC_STATE_RUNNING;
862 boolean isRunning = faceRunningState == BIOMETRIC_STATE_RUNNING;
863 mFaceRunningState = faceRunningState;
Kevin Chynb4514d22019-04-15 13:47:25 -0700864 Log.d(TAG, "faceRunningState: " + mFaceRunningState);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200865 // Clients of KeyguardUpdateMonitor don't care about the internal state or about the
Kevin Chynb4514d22019-04-15 13:47:25 -0700866 // asynchronousness of the cancel cycle. So only notify them if the actually running state
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200867 // has changed.
868 if (wasRunning != isRunning) {
869 notifyFaceRunningStateChanged();
870 }
871 }
872
873 private void notifyFaceRunningStateChanged() {
874 for (int i = 0; i < mCallbacks.size(); i++) {
875 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
876 if (cb != null) {
877 cb.onBiometricRunningStateChanged(isFaceDetectionRunning(),
878 BiometricSourceType.FACE);
879 }
880 }
881 }
882
Adrian Roos4a410172014-08-20 17:41:44 +0200883 private void handleFaceUnlockStateChanged(boolean running, int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200884 checkIsHandlerThread();
Adrian Roos4a410172014-08-20 17:41:44 +0200885 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200886 for (int i = 0; i < mCallbacks.size(); i++) {
887 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
888 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200889 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200890 }
891 }
892 }
893
Adrian Roos4a410172014-08-20 17:41:44 +0200894 public boolean isFaceUnlockRunning(int userId) {
895 return mUserFaceUnlockRunning.get(userId);
896 }
897
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700898 public boolean isFingerprintDetectionRunning() {
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200899 return mFingerprintRunningState == BIOMETRIC_STATE_RUNNING;
900 }
901
902 public boolean isFaceDetectionRunning() {
903 return mFaceRunningState == BIOMETRIC_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700904 }
905
Jim Miller50e62182014-04-23 17:25:00 -0700906 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100907 // Don't allow trust agent if device is secured with a SIM PIN. This is here
908 // mainly because there's no other way to prompt the user to enter their SIM PIN
909 // once they get past the keyguard screen.
910 final boolean disabledBySimPin = isSimPinSecure();
911 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700912 }
913
Jim Miller06e34502014-07-17 14:46:05 -0700914 private boolean isFingerprintDisabled(int userId) {
915 final DevicePolicyManager dpm =
916 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
917 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Adrian Roos733b6632015-08-21 14:32:35 -0700918 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
919 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700920 }
921
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200922 private boolean isFaceDisabled(int userId) {
923 final DevicePolicyManager dpm =
924 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
925 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
926 & DevicePolicyManager.KEYGUARD_DISABLE_FACE) != 0
927 || isSimPinSecure();
928 }
929
930
Selim Cineke8bae622015-07-15 13:24:06 -0700931 public boolean getUserCanSkipBouncer(int userId) {
Steven Wucfe398d2019-03-21 11:32:15 -0400932 return getUserHasTrust(userId) || getUserUnlockedWithBiometric(userId);
Selim Cineke8bae622015-07-15 13:24:06 -0700933 }
934
Adrian Roos46842d92014-03-27 14:58:03 +0100935 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700936 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100937 }
938
Steven Wucfe398d2019-03-21 11:32:15 -0400939 /**
940 * Returns whether the user is unlocked with biometrics.
941 */
942 public boolean getUserUnlockedWithBiometric(int userId) {
943 boolean fingerprintOrFace = mUserFingerprintAuthenticated.get(userId)
944 || mUserFaceAuthenticated.get(userId);
945 return fingerprintOrFace && isUnlockingWithBiometricAllowed();
946 }
947
Adrian Roos7861c662014-07-25 15:37:28 +0200948 public boolean getUserTrustIsManaged(int userId) {
949 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
950 }
951
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200952 public boolean isUnlockingWithBiometricAllowed() {
953 return mStrongAuthTracker.isUnlockingWithBiometricAllowed();
Adrian Roosb5e47222015-08-14 15:53:06 -0700954 }
955
Lucas Dupin16013822018-05-17 18:00:16 -0700956 public boolean isUserInLockdown(int userId) {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700957 return containsFlag(mStrongAuthTracker.getStrongAuthForUser(userId),
958 STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
Lucas Dupin16013822018-05-17 18:00:16 -0700959 }
960
Lucas Dupin7825b942019-06-03 20:22:39 -0700961 public boolean userNeedsStrongAuth() {
962 return mStrongAuthTracker.getStrongAuthForUser(getCurrentUser())
963 != LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
964 }
965
Lucas Dupin8eec2682019-07-01 16:41:17 -0700966 private boolean containsFlag(int haystack, int needle) {
967 return (haystack & needle) != 0;
968 }
969
Jorim Jaggi031f7952016-09-01 16:39:26 -0700970 public boolean needsSlowUnlockTransition() {
971 return mNeedsSlowUnlockTransition;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700972 }
973
Adrian Roosb5e47222015-08-14 15:53:06 -0700974 public StrongAuthTracker getStrongAuthTracker() {
975 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700976 }
977
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700978 private void notifyStrongAuthStateChanged(int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200979 checkIsHandlerThread();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700980 for (int i = 0; i < mCallbacks.size(); i++) {
981 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
982 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700983 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700984 }
985 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700986 }
987
Adrian Roos91ba3072017-02-14 16:50:46 +0100988 public boolean isScreenOn() {
989 return mScreenOn;
990 }
991
Lucas Dupinef886542018-01-03 16:03:07 -0800992 private void dispatchErrorMessage(CharSequence message) {
993 for (int i = 0; i < mCallbacks.size(); i++) {
994 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
995 if (cb != null) {
996 cb.onTrustAgentErrorMessage(message);
997 }
998 }
999 }
1000
Lucas Dupin3d053532019-01-29 12:35:22 -08001001 @VisibleForTesting
1002 void setAssistantVisible(boolean assistantVisible) {
1003 mAssistantVisible = assistantVisible;
1004 updateBiometricListeningState();
1005 }
1006
Jim Miller8f09fd22013-03-14 19:04:28 -07001007 static class DisplayClientState {
1008 public int clientGeneration;
1009 public boolean clearing;
1010 public PendingIntent intent;
1011 public int playbackState;
1012 public long playbackEventTime;
1013 }
1014
1015 private DisplayClientState mDisplayClientState = new DisplayClientState();
1016
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001017 @VisibleForTesting
Lucas Dupin7ff82b02018-05-16 12:14:10 -07001018 protected final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001019
Jim Millerd72d5ac2015-09-29 18:55:32 -07001020 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -07001021 public void onReceive(Context context, Intent intent) {
1022 final String action = intent.getAction();
1023 if (DEBUG) Log.d(TAG, "received broadcast " + action);
1024
1025 if (Intent.ACTION_TIME_TICK.equals(action)
Robert Snoeberger9c1074f2018-12-07 17:35:21 -05001026 || Intent.ACTION_TIME_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -07001027 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Robert Snoeberger9c1074f2018-12-07 17:35:21 -05001028 } else if (Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
1029 final Message msg = mHandler.obtainMessage(
1030 MSG_TIMEZONE_UPDATE, intent.getStringExtra("time-zone"));
1031 mHandler.sendMessage(msg);
Jim Millerbbf1a742012-07-17 18:30:30 -07001032 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
1033 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
1034 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
1035 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
1036 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -08001037
1038 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
1039 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
1040 final int maxChargingMicroWatt;
1041
1042 if (maxChargingMicroVolt <= 0) {
1043 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
1044 }
1045 if (maxChargingMicroAmp > 0) {
1046 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
1047 // to maintain precision equally on both factors.
1048 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
1049 * (maxChargingMicroVolt / 1000);
1050 } else {
1051 maxChargingMicroWatt = -1;
1052 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001053 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -07001054 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -08001055 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -07001056 mHandler.sendMessage(msg);
1057 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Bill Linef81cbd2018-07-05 17:48:49 +08001058 SimData args = SimData.fromIntent(intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001059 // ACTION_SIM_STATE_CHANGED is rebroadcast after unlocking the device to
1060 // keep compatibility with apps that aren't direct boot aware.
1061 // SysUI should just ignore this broadcast because it was already received
1062 // and processed previously.
1063 if (intent.getBooleanExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, false)) {
Bill Linef81cbd2018-07-05 17:48:49 +08001064 // Guarantee mTelephonyCapable state after SysUI crash and restart
1065 if (args.simState == State.ABSENT) {
1066 mHandler.obtainMessage(MSG_TELEPHONY_CAPABLE, true).sendToTarget();
1067 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001068 return;
1069 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001070 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -08001071 Log.v(TAG, "action " + action
1072 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
1073 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -07001074 }
Jim Miller52a61332014-11-12 19:29:51 -08001075 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
1076 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -07001077 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
1078 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
1079 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
1080 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
1081 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
1082 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -04001083 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
1084 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001085 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -07001086 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -07001087 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
1088 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
1089 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
1090 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
1091 if (DEBUG) {
1092 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
1093 + subId);
1094 }
Bonian Chena7e9e8c2019-06-02 23:59:31 +00001095 mHandler.sendMessage(
1096 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Fabian Kozynskiccde55d2019-06-26 10:06:09 -04001097 } else if (TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED.equals(action)) {
1098 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
Alex Chauff7653d2018-02-01 17:18:08 +00001099 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(
1100 action)) {
1101 mHandler.sendEmptyMessage(MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001102 }
1103 }
1104 };
Jim Miller2de5ee82012-06-14 22:22:50 -07001105
Lucas Dupin3d053532019-01-29 12:35:22 -08001106 @VisibleForTesting
1107 protected final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001108
Jim Millerd72d5ac2015-09-29 18:55:32 -07001109 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001110 public void onReceive(Context context, Intent intent) {
1111 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +02001112 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
1113 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
1114 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001115 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
1116 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +02001117 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001118 Trace.beginSection("KeyguardUpdateMonitor.mBroadcastAllReceiver#onReceive ACTION_FACE_UNLOCK_STARTED");
Adrian Roos48c796c2014-09-01 14:59:23 +02001119 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
1120 getSendingUserId()));
Nick Desaulniers1d396752016-07-25 15:05:33 -07001121 Trace.endSection();
Adrian Roos48c796c2014-09-01 14:59:23 +02001122 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
1123 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
1124 getSendingUserId()));
1125 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
1126 .equals(action)) {
1127 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001128 } else if (ACTION_USER_UNLOCKED.equals(action)) {
1129 mHandler.sendEmptyMessage(MSG_USER_UNLOCKED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001130 }
1131 }
1132 };
Jim Miller9f0753f2015-03-23 23:59:22 -07001133
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001134 private final FingerprintManager.LockoutResetCallback mFingerprintLockoutResetCallback
Jorim Jaggi3a464782015-08-28 16:59:13 -07001135 = new FingerprintManager.LockoutResetCallback() {
1136 @Override
1137 public void onLockoutReset() {
1138 handleFingerprintLockoutReset();
1139 }
1140 };
1141
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001142 private final FaceManager.LockoutResetCallback mFaceLockoutResetCallback
1143 = new FaceManager.LockoutResetCallback() {
1144 @Override
1145 public void onLockoutReset() {
1146 handleFaceLockoutReset();
1147 }
1148 };
1149
1150 private FingerprintManager.AuthenticationCallback mFingerprintAuthenticationCallback
Jim Miller9f0753f2015-03-23 23:59:22 -07001151 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -07001152
1153 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -07001154 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -07001155 handleFingerprintAuthFailed();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001156 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001157
1158 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -07001159 public void onAuthenticationSucceeded(AuthenticationResult result) {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001160 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
Jim Miller837fa7e2016-08-08 20:16:22 -07001161 handleFingerprintAuthenticated(result.getUserId());
Nick Desaulniers1d396752016-07-25 15:05:33 -07001162 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -07001163 }
1164
1165 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -07001166 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -07001167 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -07001168 }
1169
1170 @Override
1171 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -07001172 handleFingerprintError(errMsgId, errString.toString());
1173 }
1174
1175 @Override
1176 public void onAuthenticationAcquired(int acquireInfo) {
1177 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -07001178 }
1179 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001180
Lucas Dupin3d053532019-01-29 12:35:22 -08001181 @VisibleForTesting
1182 FaceManager.AuthenticationCallback mFaceAuthenticationCallback
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001183 = new FaceManager.AuthenticationCallback() {
1184
1185 @Override
1186 public void onAuthenticationFailed() {
1187 handleFaceAuthFailed();
1188 }
1189
1190 @Override
1191 public void onAuthenticationSucceeded(FaceManager.AuthenticationResult result) {
1192 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
1193 handleFaceAuthenticated(result.getUserId());
1194 Trace.endSection();
1195 }
1196
1197 @Override
1198 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
1199 handleFaceHelp(helpMsgId, helpString.toString());
1200 }
1201
1202 @Override
1203 public void onAuthenticationError(int errMsgId, CharSequence errString) {
1204 handleFaceError(errMsgId, errString.toString());
1205 }
1206
1207 @Override
1208 public void onAuthenticationAcquired(int acquireInfo) {
1209 handleFaceAcquired(acquireInfo);
1210 }
1211 };
1212
Jim Miller9f0753f2015-03-23 23:59:22 -07001213 private CancellationSignal mFingerprintCancelSignal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001214 private CancellationSignal mFaceCancelSignal;
Jim Miller9f0753f2015-03-23 23:59:22 -07001215 private FingerprintManager mFpm;
Kevin Chynb7b54a62018-09-28 18:48:12 -07001216 private FaceManager mFaceManager;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001217
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001218 /**
Jim Miller47088bb2009-11-24 00:40:16 -08001219 * When we receive a
1220 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -07001221 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001222 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -08001223 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001224 */
Jim Miller52a61332014-11-12 19:29:51 -08001225 private static class SimData {
1226 public State simState;
1227 public int slotId;
1228 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001229
Jim Miller52a61332014-11-12 19:29:51 -08001230 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -08001231 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -08001232 slotId = slot;
1233 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -08001234 }
1235
Jim Miller52a61332014-11-12 19:29:51 -08001236 static SimData fromIntent(Intent intent) {
1237 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001238 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
1239 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
1240 }
Wink Savillea639b312012-07-10 12:37:54 -07001241 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jayachandran Cbd481352019-05-21 16:02:23 -07001242 int slotId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
Jim Miller52a61332014-11-12 19:29:51 -08001243 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -08001244 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -07001245 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -07001246 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -07001247 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -07001248
Wink Savillea639b312012-07-10 12:37:54 -07001249 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -07001250 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -07001251 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -07001252 } else {
Wink Savillea639b312012-07-10 12:37:54 -07001253 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -07001254 }
Wink Savillea639b312012-07-10 12:37:54 -07001255 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
1256 state = IccCardConstants.State.READY;
1257 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001258 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -07001259 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
1260 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
1261 state = IccCardConstants.State.PIN_REQUIRED;
1262 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
1263 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001264 } else {
Wink Savillea639b312012-07-10 12:37:54 -07001265 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001266 }
Wink Savillea639b312012-07-10 12:37:54 -07001267 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
1268 state = IccCardConstants.State.NETWORK_LOCKED;
Wileen Chiu6b8b22e2014-10-29 22:48:21 +05301269 } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
1270 state = IccCardConstants.State.CARD_IO_ERROR;
Jim Miller109f1fd2012-09-19 20:44:16 -07001271 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
1272 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
1273 // This is required because telephony doesn't return to "READY" after
1274 // these state transitions. See bug 7197471.
1275 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001276 } else {
Wink Savillea639b312012-07-10 12:37:54 -07001277 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001278 }
Jim Miller52a61332014-11-12 19:29:51 -08001279 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001280 }
1281
Jim Millerd72d5ac2015-09-29 18:55:32 -07001282 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001283 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -08001284 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001285 }
1286 }
1287
Adrian Roos12c1ef52014-06-04 13:54:08 +02001288 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -07001289 public static final int CHARGING_UNKNOWN = -1;
1290 public static final int CHARGING_SLOWLY = 0;
1291 public static final int CHARGING_REGULAR = 1;
1292 public static final int CHARGING_FAST = 2;
1293
Jim Miller16464b82011-10-20 21:10:13 -07001294 public final int status;
1295 public final int level;
1296 public final int plugged;
1297 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -08001298 public final int maxChargingWattage;
1299 public BatteryStatus(int status, int level, int plugged, int health,
1300 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -07001301 this.status = status;
1302 this.level = level;
1303 this.plugged = plugged;
1304 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -08001305 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -07001306 }
1307
Jim Millerbbf1a742012-07-17 18:30:30 -07001308 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -07001309 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -07001310 * @return true if the device is plugged in.
1311 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +01001312 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001313 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -07001314 || plugged == BatteryManager.BATTERY_PLUGGED_USB
1315 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -07001316 }
1317
1318 /**
Beverly2034c832018-03-19 11:18:51 -04001319 * Determine whether the device is plugged in (USB, power).
1320 * @return true if the device is plugged in wired (as opposed to wireless)
1321 */
1322 public boolean isPluggedInWired() {
1323 return plugged == BatteryManager.BATTERY_PLUGGED_AC
1324 || plugged == BatteryManager.BATTERY_PLUGGED_USB;
1325 }
1326
1327 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001328 * Whether or not the device is charged. Note that some devices never return 100% for
1329 * battery level, so this allows either battery level or status to determine if the
1330 * battery is charged.
1331 * @return true if the device is charged
1332 */
1333 public boolean isCharged() {
1334 return status == BATTERY_STATUS_FULL || level >= 100;
1335 }
1336
1337 /**
1338 * Whether battery is low and needs to be charged.
1339 * @return true if battery is low
1340 */
1341 public boolean isBatteryLow() {
1342 return level < LOW_BATTERY_THRESHOLD;
1343 }
1344
Adrian Roos7b043112015-07-10 13:00:33 -07001345 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -08001346 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
1347 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
1348 maxChargingWattage > fastThreshold ? CHARGING_FAST :
Adrian Roos7b043112015-07-10 13:00:33 -07001349 CHARGING_REGULAR;
1350 }
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001351
1352 @Override
1353 public String toString() {
1354 return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged
1355 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}";
1356 }
Jim Miller16464b82011-10-20 21:10:13 -07001357 }
1358
Lucas Dupin3d053532019-01-29 12:35:22 -08001359 public static class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
1360 private final Consumer<Integer> mStrongAuthRequiredChangedCallback;
1361
1362 public StrongAuthTracker(Context context,
1363 Consumer<Integer> strongAuthRequiredChangedCallback) {
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001364 super(context);
Lucas Dupin3d053532019-01-29 12:35:22 -08001365 mStrongAuthRequiredChangedCallback = strongAuthRequiredChangedCallback;
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001366 }
Adrian Roosb5e47222015-08-14 15:53:06 -07001367
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001368 public boolean isUnlockingWithBiometricAllowed() {
Adrian Roosb5e47222015-08-14 15:53:06 -07001369 int userId = getCurrentUser();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001370 return isBiometricAllowedForUser(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -07001371 }
1372
1373 public boolean hasUserAuthenticatedSinceBoot() {
1374 int userId = getCurrentUser();
1375 return (getStrongAuthForUser(userId)
1376 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
1377 }
1378
1379 @Override
1380 public void onStrongAuthRequiredChanged(int userId) {
Lucas Dupin3d053532019-01-29 12:35:22 -08001381 mStrongAuthRequiredChangedCallback.accept(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -07001382 }
1383 }
1384
Jim Millerdcb3d842012-08-23 19:18:12 -07001385 public static KeyguardUpdateMonitor getInstance(Context context) {
1386 if (sInstance == null) {
1387 sInstance = new KeyguardUpdateMonitor(context);
1388 }
1389 return sInstance;
1390 }
1391
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001392 protected void handleStartedWakingUp() {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001393 Trace.beginSection("KeyguardUpdateMonitor#handleStartedWakingUp");
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001394 updateBiometricListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001395 final int count = mCallbacks.size();
1396 for (int i = 0; i < count; i++) {
1397 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1398 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001399 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -07001400 }
1401 }
Nick Desaulniers1d396752016-07-25 15:05:33 -07001402 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -07001403 }
1404
Jorim Jaggi95e40382015-09-16 15:53:42 -07001405 protected void handleStartedGoingToSleep(int arg1) {
Curtis Belmonte15114ab2019-08-16 14:32:14 -07001406 mLockIconPressed = false;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001407 clearBiometricRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -07001408 final int count = mCallbacks.size();
1409 for (int i = 0; i < count; i++) {
1410 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1411 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001412 cb.onStartedGoingToSleep(arg1);
1413 }
1414 }
1415 mGoingToSleep = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001416 updateBiometricListeningState();
Jorim Jaggi95e40382015-09-16 15:53:42 -07001417 }
1418
1419 protected void handleFinishedGoingToSleep(int arg1) {
1420 mGoingToSleep = false;
1421 final int count = mCallbacks.size();
1422 for (int i = 0; i < count; i++) {
1423 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1424 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001425 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -07001426 }
1427 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001428 updateBiometricListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001429 }
1430
Jorim Jaggif1518da2015-07-30 11:56:36 -07001431 private void handleScreenTurnedOn() {
1432 final int count = mCallbacks.size();
1433 for (int i = 0; i < count; i++) {
1434 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1435 if (cb != null) {
1436 cb.onScreenTurnedOn();
1437 }
1438 }
1439 }
1440
1441 private void handleScreenTurnedOff() {
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001442 mHardwareFingerprintUnavailableRetryCount = 0;
1443 mHardwareFaceUnavailableRetryCount = 0;
Jorim Jaggif1518da2015-07-30 11:56:36 -07001444 final int count = mCallbacks.size();
1445 for (int i = 0; i < count; i++) {
1446 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1447 if (cb != null) {
1448 cb.onScreenTurnedOff();
1449 }
1450 }
1451 }
1452
Selim Cinek99415392016-09-09 14:58:41 -07001453 private void handleDreamingStateChanged(int dreamStart) {
1454 final int count = mCallbacks.size();
Kevin Chyn36778ff2017-09-07 19:55:38 -07001455 mIsDreaming = dreamStart == 1;
Selim Cinek99415392016-09-09 14:58:41 -07001456 for (int i = 0; i < count; i++) {
1457 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1458 if (cb != null) {
Kevin Chyn36778ff2017-09-07 19:55:38 -07001459 cb.onDreamingStateChanged(mIsDreaming);
Selim Cinek99415392016-09-09 14:58:41 -07001460 }
1461 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001462 updateBiometricListeningState();
Selim Cinek99415392016-09-09 14:58:41 -07001463 }
1464
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001465 private void handleUserInfoChanged(int userId) {
1466 for (int i = 0; i < mCallbacks.size(); i++) {
1467 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1468 if (cb != null) {
1469 cb.onUserInfoChanged(userId);
1470 }
1471 }
1472 }
1473
Jorim Jaggidadafd42016-09-30 07:20:25 -07001474 private void handleUserUnlocked() {
1475 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1476 for (int i = 0; i < mCallbacks.size(); i++) {
1477 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1478 if (cb != null) {
1479 cb.onUserUnlocked();
1480 }
1481 }
1482 }
1483
Lucas Dupin7517b5d2017-08-22 12:51:25 -07001484 @VisibleForTesting
1485 protected KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001486 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -08001487 mSubscriptionManager = SubscriptionManager.from(context);
Michael Jurkafff56142012-11-28 16:51:00 -08001488 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Lucas Dupin3d053532019-01-29 12:35:22 -08001489 mStrongAuthTracker = new StrongAuthTracker(context, this::notifyStrongAuthStateChanged);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001490
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001491 // Since device can't be un-provisioned, we only need to register a content observer
1492 // to update mDeviceProvisioned when we are...
1493 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001494 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001495 }
Jim Miller47088bb2009-11-24 00:40:16 -08001496
Jim Millerbbf1a742012-07-17 18:30:30 -07001497 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -07001498 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001499
Jim Millerbbf1a742012-07-17 18:30:30 -07001500 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001501 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001502 filter.addAction(Intent.ACTION_TIME_TICK);
1503 filter.addAction(Intent.ACTION_TIME_CHANGED);
1504 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1505 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -04001506 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001507 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -07001508 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Fabian Kozynskiccde55d2019-06-26 10:06:09 -04001509 filter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001510 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001511 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Alex Chauff7653d2018-02-01 17:18:08 +00001512 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jason Monk7bb59302018-05-10 19:38:18 -07001513 context.registerReceiver(mBroadcastReceiver, filter, null, mHandler);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001514
Adam Cohenc276e822012-11-08 13:01:08 -08001515 final IntentFilter bootCompleteFilter = new IntentFilter();
1516 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1517 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
Jason Monk7bb59302018-05-10 19:38:18 -07001518 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter, null, mHandler);
Adam Cohenc276e822012-11-08 13:01:08 -08001519
Adrian Roos48c796c2014-09-01 14:59:23 +02001520 final IntentFilter allUserFilter = new IntentFilter();
1521 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1522 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1523 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1524 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1525 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001526 allUserFilter.addAction(ACTION_USER_UNLOCKED);
Adrian Roos48c796c2014-09-01 14:59:23 +02001527 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Jason Monk7bb59302018-05-10 19:38:18 -07001528 null, mHandler);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001529
Wink Saville071743f2015-01-12 17:11:04 -08001530 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001531 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001532 ActivityManager.getService().registerUserSwitchObserver(
Sudheer Shanka2c4522c2016-08-27 20:53:28 -07001533 new UserSwitchObserver() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001534 @Override
1535 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001536 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001537 newUserId, 0, reply));
1538 }
1539 @Override
1540 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001541 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001542 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001543 }
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001544 }, TAG);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001545 } catch (RemoteException e) {
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001546 e.rethrowAsRuntimeException();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001547 }
Adrian Roos46842d92014-03-27 14:58:03 +01001548
Jorim Jaggi237b0612015-05-01 14:28:49 -07001549 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1550 mTrustManager.registerTrustListener(this);
Michal Karpinskic52f8672016-11-18 11:32:45 +00001551 mLockPatternUtils = new LockPatternUtils(context);
1552 mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001553
Kevin Chyn36778ff2017-09-07 19:55:38 -07001554 mDreamManager = IDreamManager.Stub.asInterface(
1555 ServiceManager.getService(DreamService.DREAM_SERVICE));
1556
Jorim Jaggi3f124262016-11-22 13:45:17 +01001557 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
1558 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
1559 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001560 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) {
Kevin Chynb7b54a62018-09-28 18:48:12 -07001561 mFaceManager = (FaceManager) context.getSystemService(Context.FACE_SERVICE);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001562 }
Kevin Chynb7b54a62018-09-28 18:48:12 -07001563
1564 if (mFpm != null || mFaceManager != null) {
1565 mBiometricManager = context.getSystemService(BiometricManager.class);
1566 mBiometricManager.registerEnabledOnKeyguardCallback(mBiometricEnabledCallback);
1567 }
1568
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001569 updateBiometricListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001570 if (mFpm != null) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001571 mFpm.addLockoutResetCallback(mFingerprintLockoutResetCallback);
1572 }
Kevin Chynb7b54a62018-09-28 18:48:12 -07001573 if (mFaceManager != null) {
1574 mFaceManager.addLockoutResetCallback(mFaceLockoutResetCallback);
Jorim Jaggi3a464782015-08-28 16:59:13 -07001575 }
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001576
Winson Chung2cf6ad82017-11-09 17:36:59 -08001577 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001578 mUserManager = context.getSystemService(UserManager.class);
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001579 mIsPrimaryUser = mUserManager.isPrimaryUser();
Alex Chauff7653d2018-02-01 17:18:08 +00001580 mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
1581 mLogoutEnabled = mDevicePolicyManager.isLogoutEnabled();
Bill Linef81cbd2018-07-05 17:48:49 +08001582 updateAirplaneModeState();
Fabian Kozynskiccde55d2019-06-26 10:06:09 -04001583
1584 TelephonyManager telephony =
1585 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
1586 if (telephony != null) {
1587 telephony.listen(mPhoneStateListener, LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
1588 }
Bill Linef81cbd2018-07-05 17:48:49 +08001589 }
1590
1591 private void updateAirplaneModeState() {
1592 // ACTION_AIRPLANE_MODE_CHANGED do not broadcast if device set AirplaneMode ON and boot
1593 if (!WirelessUtils.isAirplaneModeOn(mContext)
1594 || mHandler.hasMessages(MSG_AIRPLANE_MODE_CHANGED)) {
1595 return;
1596 }
1597 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Jorim Jaggiea657062015-04-28 13:45:11 -07001598 }
1599
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001600 private void updateBiometricListeningState() {
1601 updateFingerprintListeningState();
1602 updateFaceListeningState();
1603 }
1604
Jorim Jaggiea657062015-04-28 13:45:11 -07001605 private void updateFingerprintListeningState() {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001606 // If this message exists, we should not authenticate again until this message is
1607 // consumed by the handler
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001608 if (mHandler.hasMessages(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE)) {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001609 return;
1610 }
Kevin Chyn0c45b072017-04-24 16:27:11 -07001611 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Jorim Jaggiea657062015-04-28 13:45:11 -07001612 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Adrian Roos2aaf9442018-11-20 16:57:33 +01001613 boolean runningOrRestarting = mFingerprintRunningState == BIOMETRIC_STATE_RUNNING
1614 || mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING;
1615 if (runningOrRestarting && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001616 stopListeningForFingerprint();
Adrian Roos2aaf9442018-11-20 16:57:33 +01001617 } else if (!runningOrRestarting && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001618 startListeningForFingerprint();
1619 }
1620 }
1621
Lucas Dupin3d053532019-01-29 12:35:22 -08001622 /**
Lucas Dupine0516d52019-02-05 17:54:06 -05001623 * Called whenever passive authentication is requested or aborted by a sensor.
1624 * @param active If the interrupt started or ended.
Lucas Dupin3d053532019-01-29 12:35:22 -08001625 */
Lucas Dupine0516d52019-02-05 17:54:06 -05001626 public void onAuthInterruptDetected(boolean active) {
1627 if (DEBUG) Log.d(TAG, "onAuthInterruptDetected(" + active + ")");
1628 if (mAuthInterruptActive == active) {
1629 return;
1630 }
1631 mAuthInterruptActive = active;
Lucas Dupin3d053532019-01-29 12:35:22 -08001632 updateFaceListeningState();
1633 }
1634
Lucas Dupin8f3faac2019-03-12 15:28:49 -07001635 /**
1636 * Requests face authentication if we're on a state where it's allowed.
1637 * This will re-trigger auth in case it fails.
1638 */
1639 public void requestFaceAuth() {
1640 if (DEBUG) Log.d(TAG, "requestFaceAuth()");
1641 updateFaceListeningState();
1642 }
1643
Lucas Dupinccf67212019-08-07 12:53:02 -07001644 /**
1645 * In case face auth is running, cancel it.
1646 */
1647 public void cancelFaceAuth() {
1648 stopListeningForFace();
1649 }
1650
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001651 private void updateFaceListeningState() {
1652 // If this message exists, we should not authenticate again until this message is
1653 // consumed by the handler
1654 if (mHandler.hasMessages(MSG_BIOMETRIC_AUTHENTICATION_CONTINUE)) {
1655 return;
1656 }
1657 mHandler.removeCallbacks(mRetryFaceAuthentication);
1658 boolean shouldListenForFace = shouldListenForFace();
1659 if (mFaceRunningState == BIOMETRIC_STATE_RUNNING && !shouldListenForFace) {
1660 stopListeningForFace();
1661 } else if (mFaceRunningState != BIOMETRIC_STATE_RUNNING
1662 && shouldListenForFace) {
1663 startListeningForFace();
1664 }
1665 }
1666
Kevin Chyn129f60f2017-08-11 17:24:42 -07001667 private boolean shouldListenForFingerprintAssistant() {
1668 return mAssistantVisible && mKeyguardOccluded
1669 && !mUserFingerprintAuthenticated.get(getCurrentUser(), false)
1670 && !mUserHasTrust.get(getCurrentUser(), false);
1671 }
1672
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001673 private boolean shouldListenForFaceAssistant() {
1674 return mAssistantVisible && mKeyguardOccluded
1675 && !mUserFaceAuthenticated.get(getCurrentUser(), false)
1676 && !mUserHasTrust.get(getCurrentUser(), false);
1677 }
1678
Jorim Jaggiea657062015-04-28 13:45:11 -07001679 private boolean shouldListenForFingerprint() {
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001680 // Only listen if this KeyguardUpdateMonitor belongs to the primary user. There is an
1681 // instance of KeyguardUpdateMonitor for each user but KeyguardUpdateMonitor is user-aware.
1682 final boolean shouldListen = (mKeyguardIsVisible || !mDeviceInteractive ||
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001683 (mBouncer && !mKeyguardGoingAway) || mGoingToSleep ||
Kevin Chyn36778ff2017-09-07 19:55:38 -07001684 shouldListenForFingerprintAssistant() || (mKeyguardOccluded && mIsDreaming))
Lucas Dupinc12fad32019-05-14 20:59:17 +00001685 && !mSwitchingUser && !isFingerprintDisabled(getCurrentUser())
Kevin Chyn225eea72019-03-06 16:42:00 -08001686 && (!mKeyguardGoingAway || !mDeviceInteractive) && mIsPrimaryUser;
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001687 return shouldListen;
Jim Miller9f0753f2015-03-23 23:59:22 -07001688 }
1689
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001690 private boolean shouldListenForFace() {
Lucas Dupin3d053532019-01-29 12:35:22 -08001691 final boolean awakeKeyguard = mKeyguardIsVisible && mDeviceInteractive && !mGoingToSleep;
1692 final int user = getCurrentUser();
Robert Snoeberger5dcdf352019-06-24 11:28:28 -04001693 final int strongAuth = mStrongAuthTracker.getStrongAuthForUser(user);
1694 final boolean isLockOutOrLockDown =
Lucas Dupin8eec2682019-07-01 16:41:17 -07001695 containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_LOCKOUT)
1696 || containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW)
1697 || containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
1698 final boolean isEncryptedOrTimedOut =
1699 containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_BOOT)
1700 || containsFlag(strongAuth, STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001701
Lucas Dupin4befb742019-07-01 11:31:31 -07001702 boolean canBypass = mKeyguardBypassController != null
1703 && mKeyguardBypassController.canBypass();
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001704 // There's no reason to ask the HAL for authentication when the user can dismiss the
1705 // bouncer, unless we're bypassing and need to auto-dismiss the lock screen even when
1706 // TrustAgents or biometrics are keeping the device unlocked.
Lucas Dupin4befb742019-07-01 11:31:31 -07001707 boolean becauseCannotSkipBouncer = !getUserCanSkipBouncer(user) || canBypass;
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001708
Lucas Dupin8eec2682019-07-01 16:41:17 -07001709 // Scan even when encrypted or timeout to show a preemptive bouncer when bypassing.
1710 // Lockout/lockdown modes shouldn't scan, since they are more explicit.
1711 boolean strongAuthAllowsScanning = (!isEncryptedOrTimedOut || canBypass && !mBouncer)
1712 && !isLockOutOrLockDown;
1713
Kevin Chynfdfd2d32019-03-01 14:52:15 -08001714 // Only listen if this KeyguardUpdateMonitor belongs to the primary user. There is an
1715 // instance of KeyguardUpdateMonitor for each user but KeyguardUpdateMonitor is user-aware.
Lucas Dupine0516d52019-02-05 17:54:06 -05001716 return (mBouncer || mAuthInterruptActive || awakeKeyguard || shouldListenForFaceAssistant())
Lucas Dupin9e484aa2019-06-24 13:38:00 -07001717 && !mSwitchingUser && !isFaceDisabled(user) && becauseCannotSkipBouncer
Lucas Dupin7d95f152019-07-17 16:25:54 -07001718 && !mKeyguardGoingAway && mFaceSettingEnabledForUser.get(user) && !mLockIconPressed
Lucas Dupin8eec2682019-07-01 16:41:17 -07001719 && strongAuthAllowsScanning && mIsPrimaryUser
1720 && !mSecureCameraLaunched;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001721 }
1722
Lucas Dupinca88e5f2019-05-14 16:11:08 -07001723 /**
1724 * Whenever the lock icon is long pressed, disabling trust agents.
1725 * This means that we cannot auth passively (face) until the user presses power.
1726 */
1727 public void onLockIconPressed() {
1728 mLockIconPressed = true;
1729 mUserFaceAuthenticated.put(getCurrentUser(), false);
1730 updateFaceListeningState();
1731 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001732
Jim Millerce7eb6d2015-04-03 19:29:13 -07001733 private void startListeningForFingerprint() {
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001734 if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING) {
1735 setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING_RESTARTING);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001736 return;
1737 }
Adrian Roos2aaf9442018-11-20 16:57:33 +01001738 if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
1739 // Waiting for restart via handleFingerprintError().
1740 return;
1741 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001742 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Lucas Dupin8f3faac2019-03-12 15:28:49 -07001743 int userId = getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001744 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001745 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001746 mFingerprintCancelSignal.cancel();
1747 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001748 mFingerprintCancelSignal = new CancellationSignal();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001749 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mFingerprintAuthenticationCallback,
1750 null, userId);
1751 setFingerprintRunningState(BIOMETRIC_STATE_RUNNING);
1752 }
1753 }
1754
1755 private void startListeningForFace() {
1756 if (mFaceRunningState == BIOMETRIC_STATE_CANCELLING) {
1757 setFaceRunningState(BIOMETRIC_STATE_CANCELLING_RESTARTING);
1758 return;
1759 }
1760 if (DEBUG) Log.v(TAG, "startListeningForFace()");
Lucas Dupin8f3faac2019-03-12 15:28:49 -07001761 int userId = getCurrentUser();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001762 if (isUnlockWithFacePossible(userId)) {
1763 if (mFaceCancelSignal != null) {
1764 mFaceCancelSignal.cancel();
1765 }
1766 mFaceCancelSignal = new CancellationSignal();
Kevin Chynb7b54a62018-09-28 18:48:12 -07001767 mFaceManager.authenticate(null, mFaceCancelSignal, 0,
Kevin Chyn8d2694a2019-04-11 18:30:40 -07001768 mFaceAuthenticationCallback, null, userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001769 setFaceRunningState(BIOMETRIC_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001770 }
1771 }
1772
Lucas Dupin8d48fc42019-04-25 14:34:12 -07001773 /**
1774 * If biometrics hardware is available, not disabled, and user has enrolled templates.
1775 * This does NOT check if the device is encrypted or in lockdown.
1776 *
1777 * @param userId User that's trying to unlock.
1778 * @return {@code true} if possible.
1779 */
1780 public boolean isUnlockingWithBiometricsPossible(int userId) {
1781 return isUnlockWithFacePossible(userId) || isUnlockWithFingerprintPossible(userId);
1782 }
1783
1784 private boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001785 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1786 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1787 }
1788
Lucas Dupin4c507042019-07-22 16:47:34 -07001789 private boolean isUnlockWithFacePossible(int userId) {
1790 return isFaceAuthEnabledForUser(userId) && !isFaceDisabled(userId);
1791 }
1792
Lucas Dupin7156bc72019-05-03 19:37:39 -07001793 /**
Lucas Dupin7d95f152019-07-17 16:25:54 -07001794 * If face hardware is available, user has enrolled and enabled auth via setting.
Lucas Dupin7156bc72019-05-03 19:37:39 -07001795 */
Lucas Dupin4c507042019-07-22 16:47:34 -07001796 public boolean isFaceAuthEnabledForUser(int userId) {
Kevin Chynb7b54a62018-09-28 18:48:12 -07001797 return mFaceManager != null && mFaceManager.isHardwareDetected()
Lucas Dupin7d95f152019-07-17 16:25:54 -07001798 && mFaceManager.hasEnrolledTemplates(userId)
1799 && mFaceSettingEnabledForUser.get(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001800 }
1801
Jorim Jaggiea657062015-04-28 13:45:11 -07001802 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001803 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001804 if (mFingerprintRunningState == BIOMETRIC_STATE_RUNNING) {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001805 if (mFingerprintCancelSignal != null) {
1806 mFingerprintCancelSignal.cancel();
1807 mFingerprintCancelSignal = null;
1808 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001809 setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001810 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001811 if (mFingerprintRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
1812 setFingerprintRunningState(BIOMETRIC_STATE_CANCELLING);
1813 }
1814 }
1815
1816 private void stopListeningForFace() {
1817 if (DEBUG) Log.v(TAG, "stopListeningForFace()");
1818 if (mFaceRunningState == BIOMETRIC_STATE_RUNNING) {
1819 if (mFaceCancelSignal != null) {
1820 mFaceCancelSignal.cancel();
1821 mFaceCancelSignal = null;
1822 }
1823 setFaceRunningState(BIOMETRIC_STATE_CANCELLING);
1824 }
1825 if (mFaceRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
1826 setFaceRunningState(BIOMETRIC_STATE_CANCELLING);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001827 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001828 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001829
Michael Jurkafff56142012-11-28 16:51:00 -08001830 private boolean isDeviceProvisionedInSettingsDb() {
1831 return Settings.Global.getInt(mContext.getContentResolver(),
1832 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1833 }
1834
Jim Millerbbf1a742012-07-17 18:30:30 -07001835 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001836 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001837 @Override
1838 public void onChange(boolean selfChange) {
1839 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001840 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001841 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001842 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001843 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001844 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001845 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001846 };
1847
1848 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001849 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001850 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001851
1852 // prevent a race condition between where we check the flag and where we register the
1853 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001854 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001855 if (provisioned != mDeviceProvisioned) {
1856 mDeviceProvisioned = provisioned;
1857 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001858 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001859 }
1860 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001861 }
1862
Jim Millerbbf1a742012-07-17 18:30:30 -07001863 /**
Jorim Jaggid11d1a92016-08-16 16:02:32 -07001864 * Update the state whether Keyguard currently has a lockscreen wallpaper.
1865 *
1866 * @param hasLockscreenWallpaper Whether Keyguard has a lockscreen wallpaper.
1867 */
1868 public void setHasLockscreenWallpaper(boolean hasLockscreenWallpaper) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001869 checkIsHandlerThread();
Jorim Jaggid11d1a92016-08-16 16:02:32 -07001870 if (hasLockscreenWallpaper != mHasLockscreenWallpaper) {
1871 mHasLockscreenWallpaper = hasLockscreenWallpaper;
1872 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1873 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1874 if (cb != null) {
1875 cb.onHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
1876 }
1877 }
1878 }
1879 }
1880
1881 /**
1882 * @return Whether Keyguard has a lockscreen wallpaper.
1883 */
1884 public boolean hasLockscreenWallpaper() {
1885 return mHasLockscreenWallpaper;
1886 }
1887
1888 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001889 * Handle {@link #MSG_DPM_STATE_CHANGED}
1890 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001891 private void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001892 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001893 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1894 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1895 if (cb != null) {
1896 cb.onDevicePolicyManagerStateChanged();
1897 }
Jim Millerb0304762012-03-13 20:01:25 -07001898 }
1899 }
1900
Jim Millerbbf1a742012-07-17 18:30:30 -07001901 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001902 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001903 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001904 private void handleUserSwitching(int userId, IRemoteCallback reply) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001905 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001906 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1907 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001908 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001909 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001910 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001911 try {
1912 reply.sendResult(null);
1913 } catch (RemoteException e) {
1914 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001915 }
1916
Jim Millerbbf1a742012-07-17 18:30:30 -07001917 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001918 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1919 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001920 private void handleUserSwitchComplete(int userId) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001921 for (int i = 0; i < mCallbacks.size(); i++) {
1922 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1923 if (cb != null) {
1924 cb.onUserSwitchComplete(userId);
1925 }
1926 }
1927 }
1928
1929 /**
Jim Miller90873d52013-09-26 18:11:38 -07001930 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1931 * keyguard crashes sometime after boot, then it will never receive this
1932 * broadcast and hence not handle the event. This method is ultimately called by
1933 * PhoneWindowManager in this case.
1934 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001935 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001936 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001937 }
1938
1939 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001940 * Handle {@link #MSG_BOOT_COMPLETED}
1941 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001942 private void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001943 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001944 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001945 for (int i = 0; i < mCallbacks.size(); i++) {
1946 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1947 if (cb != null) {
1948 cb.onBootCompleted();
1949 }
1950 }
1951 }
1952
1953 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001954 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001955 * destroyed.
1956 */
1957 public boolean hasBootCompleted() {
1958 return mBootCompleted;
1959 }
1960
1961 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001962 * Handle {@link #MSG_DEVICE_PROVISIONED}
1963 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001964 private void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001965 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001966 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1967 if (cb != null) {
1968 cb.onDeviceProvisioned();
1969 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001970 }
Michael Jurkafff56142012-11-28 16:51:00 -08001971 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001972 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001973 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1974 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001975 }
1976 }
1977
Jim Millerbbf1a742012-07-17 18:30:30 -07001978 /**
1979 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1980 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001981 private void handlePhoneStateChanged(String newState) {
Jim Millerc23024d2010-02-24 15:37:00 -08001982 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001983 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1984 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1985 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1986 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1987 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1988 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1989 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001990 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001991 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1992 if (cb != null) {
1993 cb.onPhoneStateChanged(mPhoneState);
1994 }
Jim Millerc23024d2010-02-24 15:37:00 -08001995 }
1996 }
1997
Jim Millerbbf1a742012-07-17 18:30:30 -07001998 /**
1999 * Handle {@link #MSG_RINGER_MODE_CHANGED}
2000 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002001 private void handleRingerModeChange(int mode) {
Jim Miller47088bb2009-11-24 00:40:16 -08002002 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07002003 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07002004 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002005 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2006 if (cb != null) {
2007 cb.onRingerModeChanged(mode);
2008 }
Jim Miller47088bb2009-11-24 00:40:16 -08002009 }
2010 }
2011
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002012 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002013 * Handle {@link #MSG_TIME_UPDATE}
2014 */
2015 private void handleTimeUpdate() {
2016 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07002017 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002018 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2019 if (cb != null) {
2020 cb.onTimeChanged();
2021 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002022 }
2023 }
2024
2025 /**
Robert Snoeberger9c1074f2018-12-07 17:35:21 -05002026 * Handle (@line #MSG_TIMEZONE_UPDATE}
2027 */
2028 private void handleTimeZoneUpdate(String timeZone) {
2029 if (DEBUG) Log.d(TAG, "handleTimeZoneUpdate");
2030 for (int i = 0; i < mCallbacks.size(); i++) {
2031 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2032 if (cb != null) {
2033 cb.onTimeZoneChanged(TimeZone.getTimeZone(timeZone));
2034 // Also notify callbacks about time change to remain compatible.
2035 cb.onTimeChanged();
2036 }
2037 }
2038 }
2039
2040 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002041 * Handle {@link #MSG_BATTERY_UPDATE}
2042 */
Jim Millerbbf1a742012-07-17 18:30:30 -07002043 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002044 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07002045 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
2046 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07002047 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07002048 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002049 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2050 if (cb != null) {
2051 cb.onRefreshBatteryInfo(status);
2052 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002053 }
2054 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002055 }
2056
2057 /**
Bill Linef81cbd2018-07-05 17:48:49 +08002058 * Handle Telephony status during Boot for CarrierText display policy
2059 */
2060 @VisibleForTesting
2061 void updateTelephonyCapable(boolean capable){
2062 if (capable == mTelephonyCapable) {
2063 return;
2064 }
2065 mTelephonyCapable = capable;
2066 for (WeakReference<KeyguardUpdateMonitorCallback> ref : mCallbacks) {
2067 KeyguardUpdateMonitorCallback cb = ref.get();
2068 if (cb != null) {
2069 cb.onTelephonyCapable(mTelephonyCapable);
2070 }
2071 }
2072 }
2073
2074 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002075 * Handle {@link #MSG_SIM_STATE_CHANGE}
2076 */
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08002077 @VisibleForTesting
Adrian Roos30a2ae62018-04-25 19:09:50 +02002078 void handleSimStateChange(int subId, int slotId, State state) {
2079 checkIsHandlerThread();
Jim Miller52a61332014-11-12 19:29:51 -08002080 if (DEBUG_SIM_STATES) {
2081 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
2082 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002083 }
2084
Lucas Dupin2e0d4952018-12-05 20:03:53 -08002085 boolean becameAbsent = false;
Wink Savillea54bf652014-12-11 13:37:50 -08002086 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08002087 Log.w(TAG, "invalid subId in handleSimStateChange()");
andychou695a7602019-05-16 23:14:00 +08002088 /* Only handle No SIM(ABSENT) and Card Error(CARD_IO_ERROR) due to
2089 * handleServiceStateChange() handle other case */
Bill Linef81cbd2018-07-05 17:48:49 +08002090 if (state == State.ABSENT) {
2091 updateTelephonyCapable(true);
Lucas Dupin2e0d4952018-12-05 20:03:53 -08002092 // Even though the subscription is not valid anymore, we need to notify that the
2093 // SIM card was removed so we can update the UI.
2094 becameAbsent = true;
Brad Ebingere6c6f722018-12-20 21:11:44 -08002095 for (SimData data : mSimDatas.values()) {
2096 // Set the SIM state of all SimData associated with that slot to ABSENT se we
2097 // do not move back into PIN/PUK locked and not detect the change below.
2098 if (data.slotId == slotId) {
2099 data.simState = State.ABSENT;
2100 }
2101 }
andychou695a7602019-05-16 23:14:00 +08002102 } else if (state == State.CARD_IO_ERROR) {
2103 updateTelephonyCapable(true);
Lucas Dupin2e0d4952018-12-05 20:03:53 -08002104 } else {
2105 return;
Bill Linef81cbd2018-07-05 17:48:49 +08002106 }
Jim Miller52a61332014-11-12 19:29:51 -08002107 }
2108
2109 SimData data = mSimDatas.get(subId);
2110 final boolean changed;
2111 if (data == null) {
2112 data = new SimData(state, slotId, subId);
2113 mSimDatas.put(subId, data);
2114 changed = true; // no data yet; force update
2115 } else {
2116 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
2117 data.simState = state;
2118 data.subId = subId;
2119 data.slotId = slotId;
2120 }
Lucas Dupin2e0d4952018-12-05 20:03:53 -08002121 if ((changed || becameAbsent) && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07002122 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07002123 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2124 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08002125 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07002126 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002127 }
2128 }
2129 }
2130
Jim Millerbbf1a742012-07-17 18:30:30 -07002131 /**
Etan Cohen47051d82015-07-06 16:19:04 -07002132 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
2133 */
Bill Linef81cbd2018-07-05 17:48:49 +08002134 @VisibleForTesting
2135 void handleServiceStateChange(int subId, ServiceState serviceState) {
Etan Cohen47051d82015-07-06 16:19:04 -07002136 if (DEBUG) {
2137 Log.d(TAG,
2138 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
2139 }
2140
2141 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2142 Log.w(TAG, "invalid subId in handleServiceStateChange()");
2143 return;
Bill Linef81cbd2018-07-05 17:48:49 +08002144 } else {
2145 updateTelephonyCapable(true);
Etan Cohen47051d82015-07-06 16:19:04 -07002146 }
2147
2148 mServiceStates.put(subId, serviceState);
2149
2150 for (int j = 0; j < mCallbacks.size(); j++) {
2151 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
2152 if (cb != null) {
2153 cb.onRefreshCarrierInfo();
2154 }
2155 }
2156 }
2157
Lucas Dupinf8463ee2018-06-11 16:18:15 -07002158 public boolean isKeyguardVisible() {
2159 return mKeyguardIsVisible;
2160 }
2161
Etan Cohen47051d82015-07-06 16:19:04 -07002162 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002163 * Notifies that the visibility state of Keyguard has changed.
2164 *
2165 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002166 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002167 public void onKeyguardVisibilityChanged(boolean showing) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002168 checkIsHandlerThread();
Kevin Chynb4514d22019-04-15 13:47:25 -07002169 Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002170 mKeyguardIsVisible = showing;
Kevin Chyn6951d3d2019-06-10 14:07:13 -07002171
2172 if (showing) {
2173 mSecureCameraLaunched = false;
2174 }
2175
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002176 for (int i = 0; i < mCallbacks.size(); i++) {
2177 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2178 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07002179 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002180 }
2181 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002182 updateBiometricListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04002183 }
2184
Brian Colonna7fce3802013-09-17 15:51:32 -04002185 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07002186 * Handle {@link #MSG_KEYGUARD_RESET}
2187 */
2188 private void handleKeyguardReset() {
2189 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002190 updateBiometricListeningState();
Jorim Jaggi031f7952016-09-01 16:39:26 -07002191 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
2192 }
2193
2194 private boolean resolveNeedsSlowUnlockTransition() {
2195 if (mUserManager.isUserUnlocked(getCurrentUser())) {
2196 return false;
2197 }
2198 Intent homeIntent = new Intent(Intent.ACTION_MAIN)
2199 .addCategory(Intent.CATEGORY_HOME);
2200 ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
2201 0 /* flags */);
2202 return FALLBACK_HOME_COMPONENT.equals(resolveInfo.getComponentInfo().getComponentName());
Selim Cinek1fcafc42015-07-20 14:39:25 -07002203 }
2204
2205 /**
Adrian Roosb6011622014-05-14 15:52:53 +02002206 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
2207 * @see #sendKeyguardBouncerChanged(boolean)
2208 */
2209 private void handleKeyguardBouncerChanged(int bouncer) {
2210 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
2211 boolean isBouncer = (bouncer == 1);
2212 mBouncer = isBouncer;
Kevin Chynd0381892019-07-11 16:25:36 -07002213
2214 if (isBouncer) {
2215 // If the bouncer is shown, always clear this flag. This can happen in the following
2216 // situations: 1) Default camera with SHOW_WHEN_LOCKED is not chosen yet. 2) Secure
2217 // camera requests dismiss keyguard (tapping on photos for example). When these happen,
2218 // face auth should resume.
2219 mSecureCameraLaunched = false;
2220 }
2221
Adrian Roosb6011622014-05-14 15:52:53 +02002222 for (int i = 0; i < mCallbacks.size(); i++) {
2223 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2224 if (cb != null) {
2225 cb.onKeyguardBouncerChanged(isBouncer);
2226 }
2227 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002228 updateBiometricListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02002229 }
2230
2231 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04002232 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
2233 */
2234 private void handleReportEmergencyCallAction() {
2235 for (int i = 0; i < mCallbacks.size(); i++) {
2236 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2237 if (cb != null) {
2238 cb.onEmergencyCallAction();
2239 }
2240 }
2241 }
2242
Lucas Dupin4272f442018-01-13 22:00:35 -08002243 private boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07002244 final boolean nowPluggedIn = current.isPluggedIn();
2245 final boolean wasPluggedIn = old.isPluggedIn();
Lucas Dupin4272f442018-01-13 22:00:35 -08002246 final boolean stateChangedWhilePluggedIn = wasPluggedIn && nowPluggedIn
Jim Miller16464b82011-10-20 21:10:13 -07002247 && (old.status != current.status);
2248
2249 // change in plug state is always interesting
2250 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002251 return true;
2252 }
2253
Lucas Dupin3fcdd472018-01-19 19:06:45 -08002254 // change in battery level
2255 if (old.level != current.level) {
Jim Miller16464b82011-10-20 21:10:13 -07002256 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002257 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07002258
2259 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08002260 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07002261 return true;
2262 }
2263
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002264 return false;
2265 }
2266
2267 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07002268 * Remove the given observer's callback.
2269 *
Jim Miller6212cc02012-09-05 17:35:31 -07002270 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002271 */
Jim Miller6212cc02012-09-05 17:35:31 -07002272 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002273 checkIsHandlerThread();
Jim Miller6212cc02012-09-05 17:35:31 -07002274 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
2275 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
2276 if (mCallbacks.get(i).get() == callback) {
2277 mCallbacks.remove(i);
2278 }
2279 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002280 }
2281
2282 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002283 * Register to receive notifications about general keyguard information
2284 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07002285 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002286 */
Jim Millerbbf1a742012-07-17 18:30:30 -07002287 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002288 checkIsHandlerThread();
Jim Miller6212cc02012-09-05 17:35:31 -07002289 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
2290 // Prevent adding duplicate callbacks
2291 for (int i = 0; i < mCallbacks.size(); i++) {
2292 if (mCallbacks.get(i).get() == callback) {
2293 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
2294 new Exception("Called by"));
2295 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07002296 }
2297 }
Jim Miller6212cc02012-09-05 17:35:31 -07002298 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
2299 removeCallback(null); // remove unused references
2300 sendUpdates(callback);
2301 }
2302
Lucas Dupin9e484aa2019-06-24 13:38:00 -07002303 public void setKeyguardBypassController(KeyguardBypassController keyguardBypassController) {
2304 mKeyguardBypassController = keyguardBypassController;
2305 }
2306
Lucas Dupinc12fad32019-05-14 20:59:17 +00002307 public boolean isSwitchingUser() {
2308 return mSwitchingUser;
2309 }
2310
Adrian Roos30a2ae62018-04-25 19:09:50 +02002311 @AnyThread
Evan Rosky18396452016-07-27 15:19:37 -07002312 public void setSwitchingUser(boolean switching) {
Lucas Dupinc12fad32019-05-14 20:59:17 +00002313 mSwitchingUser = switching;
Selim Cinekbbe19242017-12-08 15:42:08 -08002314 // Since this comes in on a binder thread, we need to post if first
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002315 mHandler.post(mUpdateBiometricListeningState);
Evan Rosky18396452016-07-27 15:19:37 -07002316 }
2317
Jim Miller6212cc02012-09-05 17:35:31 -07002318 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
2319 // Notify listener of the current state
2320 callback.onRefreshBatteryInfo(mBatteryStatus);
2321 callback.onTimeChanged();
2322 callback.onRingerModeChanged(mRingMode);
2323 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05002324 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07002325 callback.onClockVisibilityChanged();
Lucas Dupin16cfe452018-02-08 13:14:50 -08002326 callback.onKeyguardVisibilityChangedRaw(mKeyguardIsVisible);
Bill Linef81cbd2018-07-05 17:48:49 +08002327 callback.onTelephonyCapable(mTelephonyCapable);
Jim Miller52a61332014-11-12 19:29:51 -08002328 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
2329 final SimData state = data.getValue();
2330 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
2331 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002332 }
2333
Selim Cinek1fcafc42015-07-20 14:39:25 -07002334 public void sendKeyguardReset() {
2335 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
2336 }
2337
Adrian Roosb6011622014-05-14 15:52:53 +02002338 /**
2339 * @see #handleKeyguardBouncerChanged(int)
2340 */
2341 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
2342 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
2343 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
2344 message.arg1 = showingBouncer ? 1 : 0;
2345 message.sendToTarget();
2346 }
2347
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002348 /**
Jim Miller90d5d462011-11-17 16:57:01 -08002349 * 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 -08002350 * have the information earlier than waiting for the intent
2351 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08002352 *
2353 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
2354 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002355 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02002356 @MainThread
Jim Miller52a61332014-11-12 19:29:51 -08002357 public void reportSimUnlocked(int subId) {
2358 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
Sanket Padawe7e460252017-03-10 16:18:20 -08002359 int slotId = SubscriptionManager.getSlotIndex(subId);
Jim Miller52a61332014-11-12 19:29:51 -08002360 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002361 }
2362
Brian Colonna7fce3802013-09-17 15:51:32 -04002363 /**
2364 * Report that the emergency call button has been pressed and the emergency dialer is
2365 * about to be displayed.
2366 *
2367 * @param bypassHandler runs immediately.
2368 *
2369 * NOTE: Must be called from UI thread if bypassHandler == true.
2370 */
2371 public void reportEmergencyCallAction(boolean bypassHandler) {
2372 if (!bypassHandler) {
2373 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
2374 } else {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002375 checkIsHandlerThread();
Brian Colonna7fce3802013-09-17 15:51:32 -04002376 handleReportEmergencyCallAction();
2377 }
2378 }
2379
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002380 /**
2381 * @return Whether the device is provisioned (whether they have gone through
2382 * the setup wizard)
2383 */
2384 public boolean isDeviceProvisioned() {
2385 return mDeviceProvisioned;
2386 }
2387
Kensuke Matsui21d1bf12017-03-14 13:27:20 +09002388 public ServiceState getServiceState(int subId) {
2389 return mServiceStates.get(subId);
2390 }
2391
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002392 public void clearBiometricRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07002393 mUserFingerprintAuthenticated.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002394 mUserFaceAuthenticated.clear();
2395 mTrustManager.clearAllBiometricRecognized(BiometricSourceType.FINGERPRINT);
2396 mTrustManager.clearAllBiometricRecognized(BiometricSourceType.FACE);
Aran Ink4c0d5602019-06-21 14:27:32 -04002397
2398 for (int i = 0; i < mCallbacks.size(); i++) {
2399 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2400 if (cb != null) {
2401 cb.onBiometricsCleared();
2402 }
2403 }
Jim Millerf41fc962014-06-18 16:33:51 -07002404 }
2405
Jim Miller52a61332014-11-12 19:29:51 -08002406 public boolean isSimPinVoiceSecure() {
2407 // TODO: only count SIMs that handle voice
2408 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07002409 }
2410
Lucas Dupin7156bc72019-05-03 19:37:39 -07002411 /**
2412 * If any SIM cards are currently secure.
2413 * @see #isSimPinSecure(State)
2414 */
Jim Millerdcb3d842012-08-23 19:18:12 -07002415 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08002416 // True if any SIM is pin secure
2417 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
2418 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
2419 }
2420 return false;
2421 }
2422
Jason Monk9ff69bd2014-12-02 16:43:17 -05002423 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08002424 if (mSimDatas.containsKey(subId)) {
2425 return mSimDatas.get(subId).simState;
2426 } else {
2427 return State.UNKNOWN;
2428 }
2429 }
2430
Winson Chung67f5c8b2018-09-24 12:09:19 -07002431 private final TaskStackChangeListener
2432 mTaskStackListener = new TaskStackChangeListener() {
Kevin Chyn2fefd462017-04-28 12:18:19 -07002433 @Override
2434 public void onTaskStackChangedBackground() {
2435 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002436 ActivityManager.StackInfo info = ActivityTaskManager.getService().getStackInfo(
Wale Ogunwale68278562017-09-23 17:13:55 -07002437 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_ASSISTANT);
Kevin Chyn2fefd462017-04-28 12:18:19 -07002438 if (info == null) {
2439 return;
2440 }
2441 mHandler.sendMessage(mHandler.obtainMessage(MSG_ASSISTANT_STACK_CHANGED,
2442 info.visible));
2443 } catch (RemoteException e) {
2444 Log.e(TAG, "unable to check task stack", e);
2445 }
2446 }
2447 };
2448
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01002449 /**
Richard Choue0381b82018-04-24 03:48:59 +00002450 * @return true if and only if the state has changed for the specified {@code slotId}
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01002451 */
Richard Choue0381b82018-04-24 03:48:59 +00002452 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08002453
2454 // This is awful. It exists because there are two APIs for getting the SIM status
2455 // that don't return the complete set of values and have different types. In Keyguard we
2456 // need IccCardConstants, but TelephonyManager would only give us
2457 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08002458 final TelephonyManager tele = TelephonyManager.from(mContext);
Richard Choue0381b82018-04-24 03:48:59 +00002459 int simState = tele.getSimState(slotId);
2460 State state;
2461 try {
2462 state = State.intToState(simState);
2463 } catch(IllegalArgumentException ex) {
2464 Log.w(TAG, "Unknown sim state: " + simState);
2465 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05002466 }
Richard Choue0381b82018-04-24 03:48:59 +00002467 SimData data = mSimDatas.get(subId);
2468 final boolean changed;
2469 if (data == null) {
2470 data = new SimData(state, slotId, subId);
2471 mSimDatas.put(subId, data);
2472 changed = true; // no data yet; force update
2473 } else {
2474 changed = data.simState != state;
2475 data.simState = state;
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01002476 }
Richard Choue0381b82018-04-24 03:48:59 +00002477 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07002478 }
2479
Lucas Dupin7156bc72019-05-03 19:37:39 -07002480 /**
2481 * If the {@code state} is currently requiring a SIM PIN, PUK, or is disabled.
2482 */
Jim Millerdcb3d842012-08-23 19:18:12 -07002483 public static boolean isSimPinSecure(IccCardConstants.State state) {
Lucas Dupin7156bc72019-05-03 19:37:39 -07002484 return (state == IccCardConstants.State.PIN_REQUIRED
2485 || state == IccCardConstants.State.PUK_REQUIRED
2486 || state == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07002487 }
Jim Miller8f09fd22013-03-14 19:04:28 -07002488
2489 public DisplayClientState getCachedDisplayClientState() {
2490 return mDisplayClientState;
2491 }
Jim Miller20daffd2013-10-07 14:59:53 -07002492
2493 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
2494 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002495 public void dispatchStartedWakingUp() {
2496 synchronized (this) {
2497 mDeviceInteractive = true;
2498 }
2499 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
2500 }
2501
Jorim Jaggi95e40382015-09-16 15:53:42 -07002502 public void dispatchStartedGoingToSleep(int why) {
2503 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
2504 }
2505
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002506 public void dispatchFinishedGoingToSleep(int why) {
2507 synchronized(this) {
2508 mDeviceInteractive = false;
2509 }
2510 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
2511 }
2512
Jim Miller20daffd2013-10-07 14:59:53 -07002513 public void dispatchScreenTurnedOn() {
2514 synchronized (this) {
2515 mScreenOn = true;
2516 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07002517 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07002518 }
2519
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002520 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07002521 synchronized(this) {
2522 mScreenOn = false;
2523 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07002524 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07002525 }
2526
Selim Cinek99415392016-09-09 14:58:41 -07002527 public void dispatchDreamingStarted() {
2528 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 1, 0));
2529 }
2530
2531 public void dispatchDreamingStopped() {
2532 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 0, 0));
2533 }
2534
Jorim Jaggi0d210f62015-07-10 14:24:44 -07002535 public boolean isDeviceInteractive() {
2536 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07002537 }
Jim Miller52a61332014-11-12 19:29:51 -08002538
Jorim Jaggi95e40382015-09-16 15:53:42 -07002539 public boolean isGoingToSleep() {
2540 return mGoingToSleep;
2541 }
2542
Jim Miller52a61332014-11-12 19:29:51 -08002543 /**
2544 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
2545 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08002546 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08002547 */
2548 public int getNextSubIdForState(State state) {
2549 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08002550 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08002551 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
2552 for (int i = 0; i < list.size(); i++) {
2553 final SubscriptionInfo info = list.get(i);
2554 final int id = info.getSubscriptionId();
Sanket Padawe7e460252017-03-10 16:18:20 -08002555 int slotId = SubscriptionManager.getSlotIndex(id);
Jim Miller52a61332014-11-12 19:29:51 -08002556 if (state == getSimState(id) && bestSlotId > slotId ) {
2557 resultId = id;
2558 bestSlotId = slotId;
2559 }
2560 }
2561 return resultId;
2562 }
2563
2564 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
2565 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
2566 for (int i = 0; i < list.size(); i++) {
2567 SubscriptionInfo info = list.get(i);
2568 if (subId == info.getSubscriptionId()) return info;
2569 }
2570 return null; // not found
2571 }
Jason Monkab525272015-07-13 17:02:49 -04002572
Alex Chauff7653d2018-02-01 17:18:08 +00002573 /**
2574 * @return a cached version of DevicePolicyManager.isLogoutEnabled()
2575 */
2576 public boolean isLogoutEnabled() {
2577 return mLogoutEnabled;
2578 }
2579
2580 private void updateLogoutEnabled() {
Adrian Roos30a2ae62018-04-25 19:09:50 +02002581 checkIsHandlerThread();
Alex Chauff7653d2018-02-01 17:18:08 +00002582 boolean logoutEnabled = mDevicePolicyManager.isLogoutEnabled();
2583 if (mLogoutEnabled != logoutEnabled) {
2584 mLogoutEnabled = logoutEnabled;
2585 for (int i = 0; i < mCallbacks.size(); i++) {
2586 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2587 if (cb != null) {
2588 cb.onLogoutEnabledChanged();
2589 }
2590 }
2591 }
2592 }
2593
Adrian Roos30a2ae62018-04-25 19:09:50 +02002594 private void checkIsHandlerThread() {
2595 if (sDisableHandlerCheckForTesting) {
2596 return;
2597 }
2598 if (!mHandler.getLooper().isCurrentThread()) {
2599 Log.wtf(TAG, "must call on mHandler's thread "
2600 + mHandler.getLooper().getThread() + ", not " + Thread.currentThread());
2601 }
2602 }
2603
2604 /**
2605 * Turn off the handler check for testing.
2606 *
2607 * This is necessary because currently tests are not too careful about which thread they call
2608 * into this class on.
2609 *
2610 * Note that this must be called before scheduling any work involving KeyguardUpdateMonitor
2611 * instances.
2612 *
2613 * TODO: fix the tests and remove this.
2614 */
2615 @VisibleForTesting
2616 public static void disableHandlerCheckForTesting(Instrumentation instrumentation) {
2617 Preconditions.checkNotNull(instrumentation, "Must only call this method in tests!");
2618 // Don't need synchronization here *if* the callers follow the contract and call this only
2619 // before scheduling work for KeyguardUpdateMonitor on other threads, because the scheduling
2620 // of that work forces a happens-before relationship.
2621 sDisableHandlerCheckForTesting = true;
2622 }
2623
Jason Monkab525272015-07-13 17:02:49 -04002624 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2625 pw.println("KeyguardUpdateMonitor state:");
2626 pw.println(" SIM States:");
2627 for (SimData data : mSimDatas.values()) {
2628 pw.println(" " + data.toString());
2629 }
2630 pw.println(" Subs:");
2631 if (mSubscriptionInfo != null) {
2632 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
2633 pw.println(" " + mSubscriptionInfo.get(i));
2634 }
2635 }
2636 pw.println(" Service states:");
2637 for (int subId : mServiceStates.keySet()) {
2638 pw.println(" " + subId + "=" + mServiceStates.get(subId));
2639 }
Jim Millerd72d5ac2015-09-29 18:55:32 -07002640 if (mFpm != null && mFpm.isHardwareDetected()) {
2641 final int userId = ActivityManager.getCurrentUser();
2642 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
2643 pw.println(" Fingerprint state (user=" + userId + ")");
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002644 pw.println(" allowed=" + isUnlockingWithBiometricAllowed());
Jim Millerd72d5ac2015-09-29 18:55:32 -07002645 pw.println(" auth'd=" + mUserFingerprintAuthenticated.get(userId));
2646 pw.println(" authSinceBoot="
2647 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
2648 pw.println(" disabled(DPM)=" + isFingerprintDisabled(userId));
2649 pw.println(" possible=" + isUnlockWithFingerprintPossible(userId));
Adrian Roos2aaf9442018-11-20 16:57:33 +01002650 pw.println(" listening: actual=" + mFingerprintRunningState
2651 + " expected=" + (shouldListenForFingerprint() ? 1 : 0));
Jim Millerd72d5ac2015-09-29 18:55:32 -07002652 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
Jim Millerd72d5ac2015-09-29 18:55:32 -07002653 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
2654 }
Kevin Chynb7b54a62018-09-28 18:48:12 -07002655 if (mFaceManager != null && mFaceManager.isHardwareDetected()) {
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002656 final int userId = ActivityManager.getCurrentUser();
2657 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
2658 pw.println(" Face authentication state (user=" + userId + ")");
2659 pw.println(" allowed=" + isUnlockingWithBiometricAllowed());
2660 pw.println(" auth'd=" + mUserFaceAuthenticated.get(userId));
2661 pw.println(" authSinceBoot="
2662 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
2663 pw.println(" disabled(DPM)=" + isFaceDisabled(userId));
2664 pw.println(" possible=" + isUnlockWithFacePossible(userId));
2665 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
2666 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
Lucas Dupin7d95f152019-07-17 16:25:54 -07002667 pw.println(" enabledByUser=" + mFaceSettingEnabledForUser.get(userId));
Kevin Chynd0381892019-07-11 16:25:36 -07002668 pw.println(" mSecureCameraLaunched=" + mSecureCameraLaunched);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02002669 }
Jason Monkab525272015-07-13 17:02:49 -04002670 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002671}