blob: d24675c671884c969352479362027552ec00ab27 [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;
31
Jorim Jaggiccdfa932015-04-13 16:29:48 -070032import android.app.ActivityManager;
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020033import android.app.AlarmManager;
Jim Miller8f09fd22013-03-14 19:04:28 -070034import android.app.PendingIntent;
Sudheer Shanka2c4522c2016-08-27 20:53:28 -070035import android.app.UserSwitchObserver;
Jim Millerb0304762012-03-13 20:01:25 -070036import android.app.admin.DevicePolicyManager;
Adrian Roos46842d92014-03-27 14:58:03 +010037import android.app.trust.TrustManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080038import android.content.BroadcastReceiver;
Jorim Jaggi031f7952016-09-01 16:39:26 -070039import android.content.ComponentName;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080040import android.content.Context;
41import android.content.Intent;
42import android.content.IntentFilter;
Adrian Roosca8a2162017-08-17 19:00:58 +020043import android.content.pm.IPackageManager;
Jorim Jaggi031f7952016-09-01 16:39:26 -070044import android.content.pm.PackageManager;
45import android.content.pm.ResolveInfo;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080046import android.database.ContentObserver;
Jim Miller8f09fd22013-03-14 19:04:28 -070047import android.graphics.Bitmap;
Jorim Jaggi86bed402015-08-20 18:20:02 -070048import android.hardware.fingerprint.FingerprintManager;
49import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
50import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
Jim Miller47088bb2009-11-24 00:40:16 -080051import android.media.AudioManager;
Jim Miller79a444a2011-02-15 15:02:11 -080052import android.os.BatteryManager;
Jim Miller9f0753f2015-03-23 23:59:22 -070053import android.os.CancellationSignal;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080054import android.os.Handler;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070055import android.os.IRemoteCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080056import android.os.Message;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070057import android.os.RemoteException;
Adrian Roosca8a2162017-08-17 19:00:58 +020058import android.os.ServiceManager;
Nick Desaulniers1d396752016-07-25 15:05:33 -070059import android.os.Trace;
Amith Yamasanie8e93a12013-05-09 18:12:30 -070060import android.os.UserHandle;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070061import android.os.UserManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080062import android.provider.Settings;
Kevin Chyn36778ff2017-09-07 19:55:38 -070063import android.service.dreams.DreamService;
64import android.service.dreams.IDreamManager;
Etan Cohen47051d82015-07-06 16:19:04 -070065import android.telephony.ServiceState;
Jim Miller52a61332014-11-12 19:29:51 -080066import android.telephony.SubscriptionInfo;
Jim Miller52a61332014-11-12 19:29:51 -080067import android.telephony.SubscriptionManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -080068import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Jim Millerc23024d2010-02-24 15:37:00 -080069import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080070import android.util.Log;
Richard Choue0381b82018-04-24 03:48:59 +000071import android.util.Slog;
Adrian Roos46842d92014-03-27 14:58:03 +010072import android.util.SparseBooleanArray;
Jorim Jaggi9f743032015-05-04 15:22:40 -070073import android.util.SparseIntArray;
Adrian Roos46842d92014-03-27 14:58:03 +010074
Lucas Dupin7517b5d2017-08-22 12:51:25 -070075import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi86bed402015-08-20 18:20:02 -070076import com.android.internal.telephony.IccCardConstants;
77import com.android.internal.telephony.IccCardConstants.State;
78import com.android.internal.telephony.PhoneConstants;
79import com.android.internal.telephony.TelephonyIntents;
Adrian Roosb5e47222015-08-14 15:53:06 -070080import com.android.internal.widget.LockPatternUtils;
Winson Chungaa357452017-10-31 11:35:30 -070081import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
Winson Chung2cf6ad82017-11-09 17:36:59 -080082import com.android.systemui.shared.system.ActivityManagerWrapper;
Jorim Jaggi86bed402015-08-20 18:20:02 -070083
Kevin Chyn36778ff2017-09-07 19:55:38 -070084import com.google.android.collect.Lists;
85
Jason Monkab525272015-07-13 17:02:49 -040086import java.io.FileDescriptor;
87import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -070088import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080089import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -080090import java.util.HashMap;
91import java.util.List;
92import java.util.Map.Entry;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080093
94/**
95 * Watches for updates that may be interesting to the keyguard, and provides
96 * the up to date information as well as a registration for callbacks that care
97 * to be updated.
98 *
99 * Note: under time crunch, this has been extended to include some stuff that
100 * doesn't really belong here. see {@link #handleBatteryUpdate} where it shutdowns
Jim Miller258341c2012-08-30 16:50:10 -0700101 * the device, and {@link #getFailedUnlockAttempts()}, {@link #reportFailedAttempt()}
102 * and {@link #clearFailedUnlockAttempts()}. Maybe we should rename this 'KeyguardContext'...
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800103 */
Adrian Roos46842d92014-03-27 14:58:03 +0100104public class KeyguardUpdateMonitor implements TrustManager.TrustListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800105
Jim Millerbbf1a742012-07-17 18:30:30 -0700106 private static final String TAG = "KeyguardUpdateMonitor";
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100107 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller52a61332014-11-12 19:29:51 -0800108 private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
Jim Millerbbf1a742012-07-17 18:30:30 -0700109 private static final int LOW_BATTERY_THRESHOLD = 20;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800110
Jorim Jaggie7b12522014-08-06 16:41:21 +0200111 private static final String ACTION_FACE_UNLOCK_STARTED
112 = "com.android.facelock.FACE_UNLOCK_STARTED";
113 private static final String ACTION_FACE_UNLOCK_STOPPED
114 = "com.android.facelock.FACE_UNLOCK_STOPPED";
115
Jim Millerbbf1a742012-07-17 18:30:30 -0700116 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800117 private static final int MSG_TIME_UPDATE = 301;
118 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800119 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800120 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800121 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700122 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700123 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500124 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700125 private static final int MSG_KEYGUARD_RESET = 312;
Jim Millerf41fc962014-06-18 16:33:51 -0700126 private static final int MSG_BOOT_COMPLETED = 313;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500127 private static final int MSG_USER_SWITCH_COMPLETE = 314;
Jim Millerf41fc962014-06-18 16:33:51 -0700128 private static final int MSG_USER_INFO_CHANGED = 317;
129 private static final int MSG_REPORT_EMERGENCY_CALL_ACTION = 318;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700130 private static final int MSG_STARTED_WAKING_UP = 319;
131 private static final int MSG_FINISHED_GOING_TO_SLEEP = 320;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700132 private static final int MSG_STARTED_GOING_TO_SLEEP = 321;
Adrian Roosb6011622014-05-14 15:52:53 +0200133 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700134 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
135 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400136 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700137 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700138 private static final int MSG_SCREEN_TURNED_ON = 331;
139 private static final int MSG_SCREEN_TURNED_OFF = 332;
Selim Cinek99415392016-09-09 14:58:41 -0700140 private static final int MSG_DREAMING_STATE_CHANGED = 333;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700141 private static final int MSG_USER_UNLOCKED = 334;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700142 private static final int MSG_ASSISTANT_STACK_CHANGED = 335;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700143 private static final int MSG_FINGERPRINT_AUTHENTICATION_CONTINUE = 336;
Alex Chauff7653d2018-02-01 17:18:08 +0000144 private static final int MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED = 337;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800145
Jorim Jaggi86bed402015-08-20 18:20:02 -0700146 /** Fingerprint state: Not listening to fingerprint. */
147 private static final int FINGERPRINT_STATE_STOPPED = 0;
148
149 /** Fingerprint state: Listening. */
150 private static final int FINGERPRINT_STATE_RUNNING = 1;
151
152 /**
153 * Fingerprint state: Cancelling and waiting for the confirmation from FingerprintService to
154 * send us the confirmation that cancellation has happened.
155 */
156 private static final int FINGERPRINT_STATE_CANCELLING = 2;
157
158 /**
159 * Fingerprint state: During cancelling we got another request to start listening, so when we
160 * receive the cancellation done signal, we should start listening again.
161 */
162 private static final int FINGERPRINT_STATE_CANCELLING_RESTARTING = 3;
163
Adrian Roos0c859ae2015-11-23 16:47:50 -0800164 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
165
Jorim Jaggi031f7952016-09-01 16:39:26 -0700166 private static final ComponentName FALLBACK_HOME_COMPONENT = new ComponentName(
167 "com.android.settings", "com.android.settings.FallbackHome");
168
Adrian Roosca8a2162017-08-17 19:00:58 +0200169
170 /**
171 * If true, the system is in the half-boot-to-decryption-screen state.
172 * Prudently disable lockscreen.
173 */
174 public static final boolean CORE_APPS_ONLY;
175 static {
176 try {
177 CORE_APPS_ONLY = IPackageManager.Stub.asInterface(
178 ServiceManager.getService("package")).isOnlyCoreApps();
179 } catch (RemoteException e) {
180 throw e.rethrowFromSystemServer();
181 }
182 }
183
Jim Millerdcb3d842012-08-23 19:18:12 -0700184 private static KeyguardUpdateMonitor sInstance;
185
Jim Millerbbf1a742012-07-17 18:30:30 -0700186 private final Context mContext;
Jim Miller52a61332014-11-12 19:29:51 -0800187 HashMap<Integer, SimData> mSimDatas = new HashMap<Integer, SimData>();
Etan Cohen47051d82015-07-06 16:19:04 -0700188 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700189
Jim Millerbbf1a742012-07-17 18:30:30 -0700190 private int mRingMode;
191 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400192 private boolean mKeyguardIsVisible;
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700193 private boolean mKeyguardGoingAway;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700194 private boolean mGoingToSleep;
Adrian Roosb6011622014-05-14 15:52:53 +0200195 private boolean mBouncer;
Adam Cohen4eb36cf2012-11-07 11:45:30 -0800196 private boolean mBootCompleted;
Jorim Jaggi031f7952016-09-01 16:39:26 -0700197 private boolean mNeedsSlowUnlockTransition;
Jorim Jaggid11d1a92016-08-16 16:02:32 -0700198 private boolean mHasLockscreenWallpaper;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700199 private boolean mAssistantVisible;
200 private boolean mKeyguardOccluded;
Jim Millerbbf1a742012-07-17 18:30:30 -0700201
Jim Millerdcb3d842012-08-23 19:18:12 -0700202 // Device provisioning state
Jim Millerbbf1a742012-07-17 18:30:30 -0700203 private boolean mDeviceProvisioned;
204
Jim Millerdcb3d842012-08-23 19:18:12 -0700205 // Battery status
Jim Millerbbf1a742012-07-17 18:30:30 -0700206 private BatteryStatus mBatteryStatus;
207
Jim Millerdcb3d842012-08-23 19:18:12 -0700208 // Password attempts
Jorim Jaggi9f743032015-05-04 15:22:40 -0700209 private SparseIntArray mFailedAttempts = new SparseIntArray();
Brian Colonnacc4104f2012-10-09 17:50:46 -0400210
Rakesh Iyera7aa4d62016-01-19 17:27:23 -0800211 private final StrongAuthTracker mStrongAuthTracker;
Jim Millerbbf1a742012-07-17 18:30:30 -0700212
Jim Miller6212cc02012-09-05 17:35:31 -0700213 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700214 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800215 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700216
Brian Colonnaa5239892013-04-15 11:45:40 -0400217 private boolean mSwitchingUser;
218
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700219 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700220 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800221 private SubscriptionManager mSubscriptionManager;
222 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700223 private TrustManager mTrustManager;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700224 private UserManager mUserManager;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700225 private int mFingerprintRunningState = FINGERPRINT_STATE_STOPPED;
Michal Karpinskic52f8672016-11-18 11:32:45 +0000226 private LockPatternUtils mLockPatternUtils;
Kevin Chyn36778ff2017-09-07 19:55:38 -0700227 private final IDreamManager mDreamManager;
228 private boolean mIsDreaming;
Alex Chauff7653d2018-02-01 17:18:08 +0000229 private final DevicePolicyManager mDevicePolicyManager;
230 private boolean mLogoutEnabled;
Jim Miller20daffd2013-10-07 14:59:53 -0700231
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700232 /**
233 * Short delay before restarting fingerprint authentication after a successful try
234 * This should be slightly longer than the time between onFingerprintAuthenticated and
235 * setKeyguardGoingAway(true).
236 */
237 private static final int FINGERPRINT_CONTINUE_DELAY_MS = 500;
238
Kevin Chyn0c45b072017-04-24 16:27:11 -0700239 // If FP daemon dies, keyguard should retry after a short delay
240 private int mHardwareUnavailableRetryCount = 0;
241 private static final int HW_UNAVAILABLE_TIMEOUT = 3000; // ms
242 private static final int HW_UNAVAILABLE_RETRY_MAX = 3;
243
Jim Millerbbf1a742012-07-17 18:30:30 -0700244 private final Handler mHandler = new Handler() {
245 @Override
246 public void handleMessage(Message msg) {
247 switch (msg.what) {
248 case MSG_TIME_UPDATE:
249 handleTimeUpdate();
250 break;
251 case MSG_BATTERY_UPDATE:
252 handleBatteryUpdate((BatteryStatus) msg.obj);
253 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700254 case MSG_SIM_STATE_CHANGE:
Jim Miller52a61332014-11-12 19:29:51 -0800255 handleSimStateChange(msg.arg1, msg.arg2, (State) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700256 break;
257 case MSG_RINGER_MODE_CHANGED:
258 handleRingerModeChange(msg.arg1);
259 break;
260 case MSG_PHONE_STATE_CHANGED:
Adrian Roosb6011622014-05-14 15:52:53 +0200261 handlePhoneStateChanged((String) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700262 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700263 case MSG_DEVICE_PROVISIONED:
264 handleDeviceProvisioned();
265 break;
266 case MSG_DPM_STATE_CHANGED:
267 handleDevicePolicyManagerStateChanged();
268 break;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500269 case MSG_USER_SWITCHING:
Adrian Roosb6011622014-05-14 15:52:53 +0200270 handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500271 break;
272 case MSG_USER_SWITCH_COMPLETE:
273 handleUserSwitchComplete(msg.arg1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700274 break;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700275 case MSG_KEYGUARD_RESET:
276 handleKeyguardReset();
277 break;
Adrian Roosb6011622014-05-14 15:52:53 +0200278 case MSG_KEYGUARD_BOUNCER_CHANGED:
279 handleKeyguardBouncerChanged(msg.arg1);
280 break;
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800281 case MSG_BOOT_COMPLETED:
282 handleBootCompleted();
283 break;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700284 case MSG_USER_INFO_CHANGED:
285 handleUserInfoChanged(msg.arg1);
286 break;
Brian Colonna7fce3802013-09-17 15:51:32 -0400287 case MSG_REPORT_EMERGENCY_CALL_ACTION:
288 handleReportEmergencyCallAction();
289 break;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700290 case MSG_STARTED_GOING_TO_SLEEP:
291 handleStartedGoingToSleep(msg.arg1);
292 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700293 case MSG_FINISHED_GOING_TO_SLEEP:
294 handleFinishedGoingToSleep(msg.arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700295 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700296 case MSG_STARTED_WAKING_UP:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700297 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_STARTED_WAKING_UP");
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700298 handleStartedWakingUp();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700299 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -0700300 break;
Jorim Jaggie7b12522014-08-06 16:41:21 +0200301 case MSG_FACE_UNLOCK_STATE_CHANGED:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700302 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_FACE_UNLOCK_STATE_CHANGED");
Adrian Roos4a410172014-08-20 17:41:44 +0200303 handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700304 Trace.endSection();
Jorim Jaggie7b12522014-08-06 16:41:21 +0200305 break;
Jim Miller52a61332014-11-12 19:29:51 -0800306 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
307 handleSimSubscriptionInfoChanged();
308 break;
Jason Monk052082c2015-06-11 11:35:23 -0400309 case MSG_AIRPLANE_MODE_CHANGED:
310 handleAirplaneModeChanged();
311 break;
Etan Cohen47051d82015-07-06 16:19:04 -0700312 case MSG_SERVICE_STATE_CHANGE:
313 handleServiceStateChange(msg.arg1, (ServiceState) msg.obj);
314 break;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700315 case MSG_SCREEN_TURNED_ON:
316 handleScreenTurnedOn();
317 break;
318 case MSG_SCREEN_TURNED_OFF:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700319 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_SCREEN_TURNED_ON");
Jorim Jaggif1518da2015-07-30 11:56:36 -0700320 handleScreenTurnedOff();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700321 Trace.endSection();
Jorim Jaggif1518da2015-07-30 11:56:36 -0700322 break;
Selim Cinek99415392016-09-09 14:58:41 -0700323 case MSG_DREAMING_STATE_CHANGED:
324 handleDreamingStateChanged(msg.arg1);
325 break;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700326 case MSG_USER_UNLOCKED:
327 handleUserUnlocked();
328 break;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700329 case MSG_ASSISTANT_STACK_CHANGED:
330 mAssistantVisible = (boolean)msg.obj;
331 updateFingerprintListeningState();
332 break;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700333 case MSG_FINGERPRINT_AUTHENTICATION_CONTINUE:
334 updateFingerprintListeningState();
335 break;
Alex Chauff7653d2018-02-01 17:18:08 +0000336 case MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED:
337 updateLogoutEnabled();
338 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700339 }
340 }
341 };
342
Wink Savilled09c4ca2014-11-22 10:08:16 -0800343 private OnSubscriptionsChangedListener mSubscriptionListener =
344 new OnSubscriptionsChangedListener() {
Jim Miller52a61332014-11-12 19:29:51 -0800345 @Override
Wink Savilled09c4ca2014-11-22 10:08:16 -0800346 public void onSubscriptionsChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800347 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
348 }
349 };
350
Adrian Roos46842d92014-03-27 14:58:03 +0100351 private SparseBooleanArray mUserHasTrust = new SparseBooleanArray();
Adrian Roos7861c662014-07-25 15:37:28 +0200352 private SparseBooleanArray mUserTrustIsManaged = new SparseBooleanArray();
Jim Miller9f0753f2015-03-23 23:59:22 -0700353 private SparseBooleanArray mUserFingerprintAuthenticated = new SparseBooleanArray();
Adrian Roos4a410172014-08-20 17:41:44 +0200354 private SparseBooleanArray mUserFaceUnlockRunning = new SparseBooleanArray();
Adrian Roos46842d92014-03-27 14:58:03 +0100355
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700356 private static int sCurrentUser;
Selim Cinekbbe19242017-12-08 15:42:08 -0800357 private Runnable mUpdateFingerprintListeningState = this::updateFingerprintListeningState;
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700358
359 public synchronized static void setCurrentUser(int currentUser) {
360 sCurrentUser = currentUser;
361 }
362
363 public synchronized static int getCurrentUser() {
364 return sCurrentUser;
365 }
366
Adrian Roos46842d92014-03-27 14:58:03 +0100367 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700368 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos46842d92014-03-27 14:58:03 +0100369 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200370 for (int i = 0; i < mCallbacks.size(); i++) {
371 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
372 if (cb != null) {
373 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700374 if (enabled && flags != 0) {
375 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200376 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200377 }
378 }
Adrian Roos46842d92014-03-27 14:58:03 +0100379 }
380
Lucas Dupinef886542018-01-03 16:03:07 -0800381 @Override
382 public void onTrustError(CharSequence message) {
383 dispatchErrorMessage(message);
384 }
385
Jim Miller52a61332014-11-12 19:29:51 -0800386 protected void handleSimSubscriptionInfoChanged() {
387 if (DEBUG_SIM_STATES) {
388 Log.v(TAG, "onSubscriptionInfoChanged()");
Wink Savilled09c4ca2014-11-22 10:08:16 -0800389 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
390 if (sil != null) {
391 for (SubscriptionInfo subInfo : sil) {
392 Log.v(TAG, "SubInfo:" + subInfo);
393 }
394 } else {
395 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800396 }
397 }
398 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
399
400 // Hack level over 9000: Because the subscription id is not yet valid when we see the
401 // first update in handleSimStateChange, we need to force refresh all all SIM states
402 // so the subscription id for them is consistent.
Richard Choue0381b82018-04-24 03:48:59 +0000403 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
404 for (int i = 0; i < subscriptionInfos.size(); i++) {
405 SubscriptionInfo info = subscriptionInfos.get(i);
406 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
407 if (changed) {
408 changedSubscriptions.add(info);
409 }
410 }
411 for (int i = 0; i < changedSubscriptions.size(); i++) {
412 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800413 for (int j = 0; j < mCallbacks.size(); j++) {
414 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
415 if (cb != null) {
416 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
417 }
418 }
419 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500420 for (int j = 0; j < mCallbacks.size(); j++) {
421 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
422 if (cb != null) {
423 cb.onRefreshCarrierInfo();
424 }
425 }
Jim Miller52a61332014-11-12 19:29:51 -0800426 }
427
Jason Monk052082c2015-06-11 11:35:23 -0400428 private void handleAirplaneModeChanged() {
429 for (int j = 0; j < mCallbacks.size(); j++) {
430 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
431 if (cb != null) {
432 cb.onRefreshCarrierInfo();
433 }
434 }
435 }
436
Wink Savilled09c4ca2014-11-22 10:08:16 -0800437 /** @return List of SubscriptionInfo records, maybe empty but never null */
Adrian Roos316bf542016-08-23 17:53:07 +0200438 public List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800439 List<SubscriptionInfo> sil = mSubscriptionInfo;
440 if (sil == null || forceReload) {
441 sil = mSubscriptionManager.getActiveSubscriptionInfoList();
442 }
443 if (sil == null) {
444 // getActiveSubscriptionInfoList was null callers expect an empty list.
445 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
446 } else {
447 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800448 }
449 return mSubscriptionInfo;
450 }
451
Adrian Roos7861c662014-07-25 15:37:28 +0200452 @Override
453 public void onTrustManagedChanged(boolean managed, int userId) {
454 mUserTrustIsManaged.put(userId, managed);
455
456 for (int i = 0; i < mCallbacks.size(); i++) {
457 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
458 if (cb != null) {
459 cb.onTrustManagedChanged(userId);
460 }
461 }
462 }
463
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700464 /**
465 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is goingAway
466 * @param goingAway
467 */
468 public void setKeyguardGoingAway(boolean goingAway) {
469 mKeyguardGoingAway = goingAway;
Kevin Chyne22f1342017-09-26 10:03:38 -0700470 updateFingerprintListeningState();
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700471 }
472
Kevin Chyn2fefd462017-04-28 12:18:19 -0700473 /**
474 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is occluded
475 * @param occluded
476 */
477 public void setKeyguardOccluded(boolean occluded) {
478 mKeyguardOccluded = occluded;
479 updateFingerprintListeningState();
480 }
481
Kevin Chyn36778ff2017-09-07 19:55:38 -0700482 /**
483 * @return a cached version of DreamManager.isDreaming()
484 */
485 public boolean isDreaming() {
486 return mIsDreaming;
487 }
488
489 /**
490 * If the device is dreaming, awakens the device
491 */
492 public void awakenFromDream() {
493 if (mIsDreaming && mDreamManager != null) {
494 try {
495 mDreamManager.awaken();
496 } catch (RemoteException e) {
497 Log.e(TAG, "Unable to awaken from dream");
498 }
499 }
500 }
501
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700502 private void onFingerprintAuthenticated(int userId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700503 Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
Jim Miller9f0753f2015-03-23 23:59:22 -0700504 mUserFingerprintAuthenticated.put(userId, true);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700505 // Update/refresh trust state only if user can skip bouncer
506 if (getUserCanSkipBouncer(userId)) {
507 mTrustManager.unlockedByFingerprintForUser(userId);
508 }
Kevin Chyn625a0142017-04-10 14:53:59 -0700509 // Don't send cancel if authentication succeeds
510 mFingerprintCancelSignal = null;
Jim Millerf41fc962014-06-18 16:33:51 -0700511 for (int i = 0; i < mCallbacks.size(); i++) {
512 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
513 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700514 cb.onFingerprintAuthenticated(userId);
Jim Millerf41fc962014-06-18 16:33:51 -0700515 }
516 }
Kevin Chyn2fefd462017-04-28 12:18:19 -0700517
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700518 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE),
519 FINGERPRINT_CONTINUE_DELAY_MS);
520
Kevin Chyn2fefd462017-04-28 12:18:19 -0700521 // Only authenticate fingerprint once when assistant is visible
522 mAssistantVisible = false;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700523
Nick Desaulniers1d396752016-07-25 15:05:33 -0700524 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700525 }
526
Jim Millerce7eb6d2015-04-03 19:29:13 -0700527 private void handleFingerprintAuthFailed() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700528 for (int i = 0; i < mCallbacks.size(); i++) {
529 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
530 if (cb != null) {
531 cb.onFingerprintAuthFailed();
532 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700533 }
Jim Millerce7eb6d2015-04-03 19:29:13 -0700534 handleFingerprintHelp(-1, mContext.getString(R.string.fingerprint_not_recognized));
535 }
Jim Millerf41fc962014-06-18 16:33:51 -0700536
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700537 private void handleFingerprintAcquired(int acquireInfo) {
538 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
539 return;
540 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700541 for (int i = 0; i < mCallbacks.size(); i++) {
542 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
543 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700544 cb.onFingerprintAcquired();
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700545 }
546 }
547 }
548
Jim Miller837fa7e2016-08-08 20:16:22 -0700549 private void handleFingerprintAuthenticated(int authUserId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700550 Trace.beginSection("KeyGuardUpdateMonitor#handlerFingerPrintAuthenticated");
Jim Millerf41fc962014-06-18 16:33:51 -0700551 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700552 final int userId;
553 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800554 userId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700555 } catch (RemoteException e) {
556 Log.e(TAG, "Failed to get current user id: ", e);
557 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700558 }
Jim Miller837fa7e2016-08-08 20:16:22 -0700559 if (userId != authUserId) {
560 Log.d(TAG, "Fingerprint authenticated for wrong user: " + authUserId);
561 return;
562 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700563 if (isFingerprintDisabled(userId)) {
564 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
565 return;
566 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700567 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700568 } finally {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700569 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700570 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700571 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700572 }
573
Jim Miller9f0753f2015-03-23 23:59:22 -0700574 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700575 for (int i = 0; i < mCallbacks.size(); i++) {
576 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
577 if (cb != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700578 cb.onFingerprintHelp(msgId, helpString);
579 }
580 }
581 }
582
Kevin Chyn0c45b072017-04-24 16:27:11 -0700583 private Runnable mRetryFingerprintAuthentication = new Runnable() {
584 @Override
585 public void run() {
586 Log.w(TAG, "Retrying fingerprint after HW unavailable, attempt " +
587 mHardwareUnavailableRetryCount);
588 updateFingerprintListeningState();
589 }
590 };
591
Jim Miller9f0753f2015-03-23 23:59:22 -0700592 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700593 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
594 && mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
595 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
596 startListeningForFingerprint();
597 } else {
598 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
599 }
Kevin Chyn0c45b072017-04-24 16:27:11 -0700600
601 if (msgId == FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE) {
602 if (mHardwareUnavailableRetryCount < HW_UNAVAILABLE_RETRY_MAX) {
603 mHardwareUnavailableRetryCount++;
604 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
605 mHandler.postDelayed(mRetryFingerprintAuthentication, HW_UNAVAILABLE_TIMEOUT);
606 }
607 }
608
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700609 if (msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT) {
610 mLockPatternUtils.requireStrongAuth(
611 LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
612 getCurrentUser());
613 }
614
Jim Miller9f0753f2015-03-23 23:59:22 -0700615 for (int i = 0; i < mCallbacks.size(); i++) {
616 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
617 if (cb != null) {
618 cb.onFingerprintError(msgId, errString);
Jim Millerf41fc962014-06-18 16:33:51 -0700619 }
620 }
621 }
622
Jorim Jaggi3a464782015-08-28 16:59:13 -0700623 private void handleFingerprintLockoutReset() {
624 updateFingerprintListeningState();
625 }
626
Jorim Jaggi86bed402015-08-20 18:20:02 -0700627 private void setFingerprintRunningState(int fingerprintRunningState) {
628 boolean wasRunning = mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
629 boolean isRunning = fingerprintRunningState == FINGERPRINT_STATE_RUNNING;
630 mFingerprintRunningState = fingerprintRunningState;
631
632 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
633 // asynchronousness of the cancel cycle. So only notify them if the actualy running state
634 // has changed.
635 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700636 notifyFingerprintRunningStateChanged();
637 }
638 }
639
640 private void notifyFingerprintRunningStateChanged() {
641 for (int i = 0; i < mCallbacks.size(); i++) {
642 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
643 if (cb != null) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700644 cb.onFingerprintRunningStateChanged(isFingerprintDetectionRunning());
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700645 }
646 }
647 }
Adrian Roos4a410172014-08-20 17:41:44 +0200648 private void handleFaceUnlockStateChanged(boolean running, int userId) {
649 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200650 for (int i = 0; i < mCallbacks.size(); i++) {
651 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
652 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200653 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200654 }
655 }
656 }
657
Adrian Roos4a410172014-08-20 17:41:44 +0200658 public boolean isFaceUnlockRunning(int userId) {
659 return mUserFaceUnlockRunning.get(userId);
660 }
661
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700662 public boolean isFingerprintDetectionRunning() {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700663 return mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700664 }
665
Jim Miller50e62182014-04-23 17:25:00 -0700666 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100667 // Don't allow trust agent if device is secured with a SIM PIN. This is here
668 // mainly because there's no other way to prompt the user to enter their SIM PIN
669 // once they get past the keyguard screen.
670 final boolean disabledBySimPin = isSimPinSecure();
671 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700672 }
673
Jim Miller06e34502014-07-17 14:46:05 -0700674 private boolean isFingerprintDisabled(int userId) {
675 final DevicePolicyManager dpm =
676 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
677 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Adrian Roos733b6632015-08-21 14:32:35 -0700678 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
679 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700680 }
681
Selim Cineke8bae622015-07-15 13:24:06 -0700682 public boolean getUserCanSkipBouncer(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700683 return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
684 && isUnlockingWithFingerprintAllowed());
Selim Cineke8bae622015-07-15 13:24:06 -0700685 }
686
Adrian Roos46842d92014-03-27 14:58:03 +0100687 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700688 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100689 }
690
Adrian Roos7861c662014-07-25 15:37:28 +0200691 public boolean getUserTrustIsManaged(int userId) {
692 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
693 }
694
Selim Cinek1fcafc42015-07-20 14:39:25 -0700695 public boolean isUnlockingWithFingerprintAllowed() {
Michal Karpinskic52f8672016-11-18 11:32:45 +0000696 return mStrongAuthTracker.isUnlockingWithFingerprintAllowed();
Adrian Roosb5e47222015-08-14 15:53:06 -0700697 }
698
Jorim Jaggi031f7952016-09-01 16:39:26 -0700699 public boolean needsSlowUnlockTransition() {
700 return mNeedsSlowUnlockTransition;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700701 }
702
Adrian Roosb5e47222015-08-14 15:53:06 -0700703 public StrongAuthTracker getStrongAuthTracker() {
704 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700705 }
706
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700707 private void notifyStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700708 for (int i = 0; i < mCallbacks.size(); i++) {
709 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
710 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700711 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700712 }
713 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700714 }
715
Adrian Roos91ba3072017-02-14 16:50:46 +0100716 public boolean isScreenOn() {
717 return mScreenOn;
718 }
719
Lucas Dupinef886542018-01-03 16:03:07 -0800720 private void dispatchErrorMessage(CharSequence message) {
721 for (int i = 0; i < mCallbacks.size(); i++) {
722 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
723 if (cb != null) {
724 cb.onTrustAgentErrorMessage(message);
725 }
726 }
727 }
728
Jim Miller8f09fd22013-03-14 19:04:28 -0700729 static class DisplayClientState {
730 public int clientGeneration;
731 public boolean clearing;
732 public PendingIntent intent;
733 public int playbackState;
734 public long playbackEventTime;
735 }
736
737 private DisplayClientState mDisplayClientState = new DisplayClientState();
738
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800739 @VisibleForTesting
740 final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700741
Jim Millerd72d5ac2015-09-29 18:55:32 -0700742 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700743 public void onReceive(Context context, Intent intent) {
744 final String action = intent.getAction();
745 if (DEBUG) Log.d(TAG, "received broadcast " + action);
746
747 if (Intent.ACTION_TIME_TICK.equals(action)
748 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200749 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700750 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700751 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
752 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
753 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
754 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
755 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -0800756
757 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
758 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
759 final int maxChargingMicroWatt;
760
761 if (maxChargingMicroVolt <= 0) {
762 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
763 }
764 if (maxChargingMicroAmp > 0) {
765 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
766 // to maintain precision equally on both factors.
767 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
768 * (maxChargingMicroVolt / 1000);
769 } else {
770 maxChargingMicroWatt = -1;
771 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700772 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700773 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -0800774 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -0700775 mHandler.sendMessage(msg);
776 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800777 // ACTION_SIM_STATE_CHANGED is rebroadcast after unlocking the device to
778 // keep compatibility with apps that aren't direct boot aware.
779 // SysUI should just ignore this broadcast because it was already received
780 // and processed previously.
781 if (intent.getBooleanExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, false)) {
782 return;
783 }
Jim Miller52a61332014-11-12 19:29:51 -0800784 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700785 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800786 Log.v(TAG, "action " + action
787 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
788 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700789 }
Jim Miller52a61332014-11-12 19:29:51 -0800790 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
791 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700792 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
793 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
794 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
795 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
796 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
797 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400798 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
799 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800800 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700801 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700802 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
803 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
804 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
805 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
806 if (DEBUG) {
807 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
808 + subId);
809 }
810 mHandler.sendMessage(
811 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Alex Chauff7653d2018-02-01 17:18:08 +0000812 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(
813 action)) {
814 mHandler.sendEmptyMessage(MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jim Millerbbf1a742012-07-17 18:30:30 -0700815 }
816 }
817 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700818
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700819 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
820
Jim Millerd72d5ac2015-09-29 18:55:32 -0700821 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700822 public void onReceive(Context context, Intent intent) {
823 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200824 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
825 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
826 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700827 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
828 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200829 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700830 Trace.beginSection("KeyguardUpdateMonitor.mBroadcastAllReceiver#onReceive ACTION_FACE_UNLOCK_STARTED");
Adrian Roos48c796c2014-09-01 14:59:23 +0200831 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
832 getSendingUserId()));
Nick Desaulniers1d396752016-07-25 15:05:33 -0700833 Trace.endSection();
Adrian Roos48c796c2014-09-01 14:59:23 +0200834 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
835 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
836 getSendingUserId()));
837 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
838 .equals(action)) {
839 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -0700840 } else if (ACTION_USER_UNLOCKED.equals(action)) {
841 mHandler.sendEmptyMessage(MSG_USER_UNLOCKED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700842 }
843 }
844 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700845
Jorim Jaggi3a464782015-08-28 16:59:13 -0700846 private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
847 = new FingerprintManager.LockoutResetCallback() {
848 @Override
849 public void onLockoutReset() {
850 handleFingerprintLockoutReset();
851 }
852 };
853
Jim Miller9f0753f2015-03-23 23:59:22 -0700854 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
855 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700856
857 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700858 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700859 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700860 };
861
862 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700863 public void onAuthenticationSucceeded(AuthenticationResult result) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700864 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
Jim Miller837fa7e2016-08-08 20:16:22 -0700865 handleFingerprintAuthenticated(result.getUserId());
Nick Desaulniers1d396752016-07-25 15:05:33 -0700866 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700867 }
868
869 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700870 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700871 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700872 }
873
874 @Override
875 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700876 handleFingerprintError(errMsgId, errString.toString());
877 }
878
879 @Override
880 public void onAuthenticationAcquired(int acquireInfo) {
881 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700882 }
883 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700884 private CancellationSignal mFingerprintCancelSignal;
885 private FingerprintManager mFpm;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700886
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800887 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800888 * When we receive a
889 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700890 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800891 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800892 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800893 */
Jim Miller52a61332014-11-12 19:29:51 -0800894 private static class SimData {
895 public State simState;
896 public int slotId;
897 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800898
Jim Miller52a61332014-11-12 19:29:51 -0800899 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800900 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800901 slotId = slot;
902 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800903 }
904
Jim Miller52a61332014-11-12 19:29:51 -0800905 static SimData fromIntent(Intent intent) {
906 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800907 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
908 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
909 }
Wink Savillea639b312012-07-10 12:37:54 -0700910 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800911 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
912 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800913 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700914 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700915 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700916 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700917
Wink Savillea639b312012-07-10 12:37:54 -0700918 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700919 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700920 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700921 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700922 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700923 }
Wink Savillea639b312012-07-10 12:37:54 -0700924 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
925 state = IccCardConstants.State.READY;
926 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800927 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700928 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
929 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
930 state = IccCardConstants.State.PIN_REQUIRED;
931 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
932 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800933 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700934 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800935 }
Wink Savillea639b312012-07-10 12:37:54 -0700936 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
937 state = IccCardConstants.State.NETWORK_LOCKED;
Wileen Chiu6b8b22e2014-10-29 22:48:21 +0530938 } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
939 state = IccCardConstants.State.CARD_IO_ERROR;
Jim Miller109f1fd2012-09-19 20:44:16 -0700940 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
941 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
942 // This is required because telephony doesn't return to "READY" after
943 // these state transitions. See bug 7197471.
944 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800945 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700946 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800947 }
Jim Miller52a61332014-11-12 19:29:51 -0800948 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800949 }
950
Jim Millerd72d5ac2015-09-29 18:55:32 -0700951 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800952 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800953 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800954 }
955 }
956
Adrian Roos12c1ef52014-06-04 13:54:08 +0200957 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700958 public static final int CHARGING_UNKNOWN = -1;
959 public static final int CHARGING_SLOWLY = 0;
960 public static final int CHARGING_REGULAR = 1;
961 public static final int CHARGING_FAST = 2;
962
Jim Miller16464b82011-10-20 21:10:13 -0700963 public final int status;
964 public final int level;
965 public final int plugged;
966 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800967 public final int maxChargingWattage;
968 public BatteryStatus(int status, int level, int plugged, int health,
969 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -0700970 this.status = status;
971 this.level = level;
972 this.plugged = plugged;
973 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800974 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -0700975 }
976
Jim Millerbbf1a742012-07-17 18:30:30 -0700977 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700978 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700979 * @return true if the device is plugged in.
980 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100981 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700982 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700983 || plugged == BatteryManager.BATTERY_PLUGGED_USB
984 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -0700985 }
986
987 /**
Beverly2034c832018-03-19 11:18:51 -0400988 * Determine whether the device is plugged in (USB, power).
989 * @return true if the device is plugged in wired (as opposed to wireless)
990 */
991 public boolean isPluggedInWired() {
992 return plugged == BatteryManager.BATTERY_PLUGGED_AC
993 || plugged == BatteryManager.BATTERY_PLUGGED_USB;
994 }
995
996 /**
Jim Millerbbf1a742012-07-17 18:30:30 -0700997 * Whether or not the device is charged. Note that some devices never return 100% for
998 * battery level, so this allows either battery level or status to determine if the
999 * battery is charged.
1000 * @return true if the device is charged
1001 */
1002 public boolean isCharged() {
1003 return status == BATTERY_STATUS_FULL || level >= 100;
1004 }
1005
1006 /**
1007 * Whether battery is low and needs to be charged.
1008 * @return true if battery is low
1009 */
1010 public boolean isBatteryLow() {
1011 return level < LOW_BATTERY_THRESHOLD;
1012 }
1013
Adrian Roos7b043112015-07-10 13:00:33 -07001014 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -08001015 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
1016 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
1017 maxChargingWattage > fastThreshold ? CHARGING_FAST :
Adrian Roos7b043112015-07-10 13:00:33 -07001018 CHARGING_REGULAR;
1019 }
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001020
1021 @Override
1022 public String toString() {
1023 return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged
1024 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}";
1025 }
Jim Miller16464b82011-10-20 21:10:13 -07001026 }
1027
Adrian Roosb5e47222015-08-14 15:53:06 -07001028 public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001029 public StrongAuthTracker(Context context) {
1030 super(context);
1031 }
Adrian Roosb5e47222015-08-14 15:53:06 -07001032
1033 public boolean isUnlockingWithFingerprintAllowed() {
1034 int userId = getCurrentUser();
1035 return isFingerprintAllowedForUser(userId);
1036 }
1037
1038 public boolean hasUserAuthenticatedSinceBoot() {
1039 int userId = getCurrentUser();
1040 return (getStrongAuthForUser(userId)
1041 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
1042 }
1043
1044 @Override
1045 public void onStrongAuthRequiredChanged(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -07001046 notifyStrongAuthStateChanged(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -07001047 }
1048 }
1049
Jim Millerdcb3d842012-08-23 19:18:12 -07001050 public static KeyguardUpdateMonitor getInstance(Context context) {
1051 if (sInstance == null) {
1052 sInstance = new KeyguardUpdateMonitor(context);
1053 }
1054 return sInstance;
1055 }
1056
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001057 protected void handleStartedWakingUp() {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001058 Trace.beginSection("KeyguardUpdateMonitor#handleStartedWakingUp");
Jorim Jaggi864e64b2015-05-20 14:13:23 -07001059 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001060 final int count = mCallbacks.size();
1061 for (int i = 0; i < count; i++) {
1062 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1063 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001064 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -07001065 }
1066 }
Nick Desaulniers1d396752016-07-25 15:05:33 -07001067 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -07001068 }
1069
Jorim Jaggi95e40382015-09-16 15:53:42 -07001070 protected void handleStartedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -07001071 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -07001072 final int count = mCallbacks.size();
1073 for (int i = 0; i < count; i++) {
1074 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1075 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001076 cb.onStartedGoingToSleep(arg1);
1077 }
1078 }
1079 mGoingToSleep = true;
Jorim Jaggi95e40382015-09-16 15:53:42 -07001080 updateFingerprintListeningState();
1081 }
1082
1083 protected void handleFinishedGoingToSleep(int arg1) {
1084 mGoingToSleep = false;
1085 final int count = mCallbacks.size();
1086 for (int i = 0; i < count; i++) {
1087 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1088 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001089 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -07001090 }
1091 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001092 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001093 }
1094
Jorim Jaggif1518da2015-07-30 11:56:36 -07001095 private void handleScreenTurnedOn() {
1096 final int count = mCallbacks.size();
1097 for (int i = 0; i < count; i++) {
1098 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1099 if (cb != null) {
1100 cb.onScreenTurnedOn();
1101 }
1102 }
1103 }
1104
1105 private void handleScreenTurnedOff() {
Kevin Chyn0c45b072017-04-24 16:27:11 -07001106 mHardwareUnavailableRetryCount = 0;
Jorim Jaggif1518da2015-07-30 11:56:36 -07001107 final int count = mCallbacks.size();
1108 for (int i = 0; i < count; i++) {
1109 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1110 if (cb != null) {
1111 cb.onScreenTurnedOff();
1112 }
1113 }
1114 }
1115
Selim Cinek99415392016-09-09 14:58:41 -07001116 private void handleDreamingStateChanged(int dreamStart) {
1117 final int count = mCallbacks.size();
Kevin Chyn36778ff2017-09-07 19:55:38 -07001118 mIsDreaming = dreamStart == 1;
Selim Cinek99415392016-09-09 14:58:41 -07001119 for (int i = 0; i < count; i++) {
1120 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1121 if (cb != null) {
Kevin Chyn36778ff2017-09-07 19:55:38 -07001122 cb.onDreamingStateChanged(mIsDreaming);
Selim Cinek99415392016-09-09 14:58:41 -07001123 }
1124 }
Kevin Chyn20a68dc2017-09-21 11:41:01 -07001125 updateFingerprintListeningState();
Selim Cinek99415392016-09-09 14:58:41 -07001126 }
1127
Adam Powell43a372f2013-09-27 17:43:53 -07001128 /**
1129 * IMPORTANT: Must be called from UI thread.
1130 */
1131 public void dispatchSetBackground(Bitmap bmp) {
1132 if (DEBUG) Log.d(TAG, "dispatchSetBackground");
1133 final int count = mCallbacks.size();
1134 for (int i = 0; i < count; i++) {
1135 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1136 if (cb != null) {
1137 cb.onSetBackground(bmp);
1138 }
1139 }
1140 }
1141
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001142 private void handleUserInfoChanged(int userId) {
1143 for (int i = 0; i < mCallbacks.size(); i++) {
1144 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1145 if (cb != null) {
1146 cb.onUserInfoChanged(userId);
1147 }
1148 }
1149 }
1150
Jorim Jaggidadafd42016-09-30 07:20:25 -07001151 private void handleUserUnlocked() {
1152 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1153 for (int i = 0; i < mCallbacks.size(); i++) {
1154 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1155 if (cb != null) {
1156 cb.onUserUnlocked();
1157 }
1158 }
1159 }
1160
Lucas Dupin7517b5d2017-08-22 12:51:25 -07001161 @VisibleForTesting
1162 protected KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001163 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -08001164 mSubscriptionManager = SubscriptionManager.from(context);
Michael Jurkafff56142012-11-28 16:51:00 -08001165 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001166 mStrongAuthTracker = new StrongAuthTracker(context);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001167
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001168 // Since device can't be un-provisioned, we only need to register a content observer
1169 // to update mDeviceProvisioned when we are...
1170 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001171 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001172 }
Jim Miller47088bb2009-11-24 00:40:16 -08001173
Jim Millerbbf1a742012-07-17 18:30:30 -07001174 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -07001175 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001176
Jim Millerbbf1a742012-07-17 18:30:30 -07001177 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001178 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001179 filter.addAction(Intent.ACTION_TIME_TICK);
1180 filter.addAction(Intent.ACTION_TIME_CHANGED);
1181 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1182 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -04001183 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001184 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -07001185 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001186 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001187 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Alex Chauff7653d2018-02-01 17:18:08 +00001188 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001189 context.registerReceiver(mBroadcastReceiver, filter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001190
Adam Cohenc276e822012-11-08 13:01:08 -08001191 final IntentFilter bootCompleteFilter = new IntentFilter();
1192 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1193 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
1194 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter);
1195
Adrian Roos48c796c2014-09-01 14:59:23 +02001196 final IntentFilter allUserFilter = new IntentFilter();
1197 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1198 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1199 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1200 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1201 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001202 allUserFilter.addAction(ACTION_USER_UNLOCKED);
Adrian Roos48c796c2014-09-01 14:59:23 +02001203 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001204 null, null);
1205
Wink Saville071743f2015-01-12 17:11:04 -08001206 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001207 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001208 ActivityManager.getService().registerUserSwitchObserver(
Sudheer Shanka2c4522c2016-08-27 20:53:28 -07001209 new UserSwitchObserver() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001210 @Override
1211 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001212 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001213 newUserId, 0, reply));
1214 }
1215 @Override
1216 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001217 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001218 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001219 }
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001220 }, TAG);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001221 } catch (RemoteException e) {
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001222 e.rethrowAsRuntimeException();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001223 }
Adrian Roos46842d92014-03-27 14:58:03 +01001224
Jorim Jaggi237b0612015-05-01 14:28:49 -07001225 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1226 mTrustManager.registerTrustListener(this);
Michal Karpinskic52f8672016-11-18 11:32:45 +00001227 mLockPatternUtils = new LockPatternUtils(context);
1228 mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001229
Kevin Chyn36778ff2017-09-07 19:55:38 -07001230 mDreamManager = IDreamManager.Stub.asInterface(
1231 ServiceManager.getService(DreamService.DREAM_SERVICE));
1232
Jorim Jaggi3f124262016-11-22 13:45:17 +01001233 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
1234 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
1235 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001236 updateFingerprintListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001237 if (mFpm != null) {
1238 mFpm.addLockoutResetCallback(mLockoutResetCallback);
1239 }
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001240
Winson Chung2cf6ad82017-11-09 17:36:59 -08001241 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001242 mUserManager = context.getSystemService(UserManager.class);
Alex Chauff7653d2018-02-01 17:18:08 +00001243 mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
1244 mLogoutEnabled = mDevicePolicyManager.isLogoutEnabled();
Jorim Jaggiea657062015-04-28 13:45:11 -07001245 }
1246
1247 private void updateFingerprintListeningState() {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001248 // If this message exists, we should not authenticate again until this message is
1249 // consumed by the handler
1250 if (mHandler.hasMessages(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE)) {
1251 return;
1252 }
Kevin Chyn0c45b072017-04-24 16:27:11 -07001253 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Jorim Jaggiea657062015-04-28 13:45:11 -07001254 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001255 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001256 stopListeningForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001257 } else if (mFingerprintRunningState != FINGERPRINT_STATE_RUNNING
1258 && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001259 startListeningForFingerprint();
1260 }
1261 }
1262
Kevin Chyn129f60f2017-08-11 17:24:42 -07001263 private boolean shouldListenForFingerprintAssistant() {
1264 return mAssistantVisible && mKeyguardOccluded
1265 && !mUserFingerprintAuthenticated.get(getCurrentUser(), false)
1266 && !mUserHasTrust.get(getCurrentUser(), false);
1267 }
1268
Jorim Jaggiea657062015-04-28 13:45:11 -07001269 private boolean shouldListenForFingerprint() {
Kevin Chynf3b8fbd2017-05-03 22:24:31 -07001270 return (mKeyguardIsVisible || !mDeviceInteractive ||
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001271 (mBouncer && !mKeyguardGoingAway) || mGoingToSleep ||
Kevin Chyn36778ff2017-09-07 19:55:38 -07001272 shouldListenForFingerprintAssistant() || (mKeyguardOccluded && mIsDreaming))
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001273 && !mSwitchingUser && !isFingerprintDisabled(getCurrentUser())
1274 && !mKeyguardGoingAway;
Jim Miller9f0753f2015-03-23 23:59:22 -07001275 }
1276
Jim Millerce7eb6d2015-04-03 19:29:13 -07001277 private void startListeningForFingerprint() {
Jorim Jaggi86bed402015-08-20 18:20:02 -07001278 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING) {
1279 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING_RESTARTING);
1280 return;
1281 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001282 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001283 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001284 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001285 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001286 mFingerprintCancelSignal.cancel();
1287 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001288 mFingerprintCancelSignal = new CancellationSignal();
Kevin Chynaae4a152018-01-18 11:48:09 -08001289 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null,
1290 userId);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001291 setFingerprintRunningState(FINGERPRINT_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001292 }
1293 }
1294
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001295 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001296 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1297 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1298 }
1299
Jorim Jaggiea657062015-04-28 13:45:11 -07001300 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001301 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Jorim Jaggi86bed402015-08-20 18:20:02 -07001302 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING) {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001303 if (mFingerprintCancelSignal != null) {
1304 mFingerprintCancelSignal.cancel();
1305 mFingerprintCancelSignal = null;
1306 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001307 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001308 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001309 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
1310 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
1311 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001312 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001313
Michael Jurkafff56142012-11-28 16:51:00 -08001314 private boolean isDeviceProvisionedInSettingsDb() {
1315 return Settings.Global.getInt(mContext.getContentResolver(),
1316 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1317 }
1318
Jim Millerbbf1a742012-07-17 18:30:30 -07001319 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001320 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001321 @Override
1322 public void onChange(boolean selfChange) {
1323 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001324 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001325 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001326 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001327 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001328 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001329 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001330 };
1331
1332 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001333 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001334 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001335
1336 // prevent a race condition between where we check the flag and where we register the
1337 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001338 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001339 if (provisioned != mDeviceProvisioned) {
1340 mDeviceProvisioned = provisioned;
1341 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001342 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001343 }
1344 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001345 }
1346
Jim Millerbbf1a742012-07-17 18:30:30 -07001347 /**
Jorim Jaggid11d1a92016-08-16 16:02:32 -07001348 * Update the state whether Keyguard currently has a lockscreen wallpaper.
1349 *
1350 * @param hasLockscreenWallpaper Whether Keyguard has a lockscreen wallpaper.
1351 */
1352 public void setHasLockscreenWallpaper(boolean hasLockscreenWallpaper) {
1353 if (hasLockscreenWallpaper != mHasLockscreenWallpaper) {
1354 mHasLockscreenWallpaper = hasLockscreenWallpaper;
1355 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1356 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1357 if (cb != null) {
1358 cb.onHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
1359 }
1360 }
1361 }
1362 }
1363
1364 /**
1365 * @return Whether Keyguard has a lockscreen wallpaper.
1366 */
1367 public boolean hasLockscreenWallpaper() {
1368 return mHasLockscreenWallpaper;
1369 }
1370
1371 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001372 * Handle {@link #MSG_DPM_STATE_CHANGED}
1373 */
Jim Millerb0304762012-03-13 20:01:25 -07001374 protected void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001375 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001376 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1377 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1378 if (cb != null) {
1379 cb.onDevicePolicyManagerStateChanged();
1380 }
Jim Millerb0304762012-03-13 20:01:25 -07001381 }
1382 }
1383
Jim Millerbbf1a742012-07-17 18:30:30 -07001384 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001385 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001386 */
Chris Wrenf41c61b2012-11-29 15:19:54 -05001387 protected void handleUserSwitching(int userId, IRemoteCallback reply) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001388 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001389 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1390 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001391 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001392 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001393 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001394 try {
1395 reply.sendResult(null);
1396 } catch (RemoteException e) {
1397 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001398 }
1399
Jim Millerbbf1a742012-07-17 18:30:30 -07001400 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001401 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1402 */
1403 protected void handleUserSwitchComplete(int userId) {
1404 for (int i = 0; i < mCallbacks.size(); i++) {
1405 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1406 if (cb != null) {
1407 cb.onUserSwitchComplete(userId);
1408 }
1409 }
1410 }
1411
1412 /**
Jim Miller90873d52013-09-26 18:11:38 -07001413 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1414 * keyguard crashes sometime after boot, then it will never receive this
1415 * broadcast and hence not handle the event. This method is ultimately called by
1416 * PhoneWindowManager in this case.
1417 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001418 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001419 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001420 }
1421
1422 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001423 * Handle {@link #MSG_BOOT_COMPLETED}
1424 */
1425 protected void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001426 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001427 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001428 for (int i = 0; i < mCallbacks.size(); i++) {
1429 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1430 if (cb != null) {
1431 cb.onBootCompleted();
1432 }
1433 }
1434 }
1435
1436 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001437 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001438 * destroyed.
1439 */
1440 public boolean hasBootCompleted() {
1441 return mBootCompleted;
1442 }
1443
1444 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001445 * Handle {@link #MSG_DEVICE_PROVISIONED}
1446 */
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001447 protected void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001448 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001449 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1450 if (cb != null) {
1451 cb.onDeviceProvisioned();
1452 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001453 }
Michael Jurkafff56142012-11-28 16:51:00 -08001454 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001455 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001456 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1457 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001458 }
1459 }
1460
Jim Millerbbf1a742012-07-17 18:30:30 -07001461 /**
1462 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1463 */
Jim Millerc23024d2010-02-24 15:37:00 -08001464 protected void handlePhoneStateChanged(String newState) {
1465 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001466 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1467 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1468 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1469 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1470 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1471 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1472 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001473 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001474 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1475 if (cb != null) {
1476 cb.onPhoneStateChanged(mPhoneState);
1477 }
Jim Millerc23024d2010-02-24 15:37:00 -08001478 }
1479 }
1480
Jim Millerbbf1a742012-07-17 18:30:30 -07001481 /**
1482 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1483 */
Jim Miller47088bb2009-11-24 00:40:16 -08001484 protected void handleRingerModeChange(int mode) {
1485 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001486 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001487 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001488 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1489 if (cb != null) {
1490 cb.onRingerModeChanged(mode);
1491 }
Jim Miller47088bb2009-11-24 00:40:16 -08001492 }
1493 }
1494
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001495 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001496 * Handle {@link #MSG_TIME_UPDATE}
1497 */
1498 private void handleTimeUpdate() {
1499 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001500 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001501 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1502 if (cb != null) {
1503 cb.onTimeChanged();
1504 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001505 }
1506 }
1507
1508 /**
1509 * Handle {@link #MSG_BATTERY_UPDATE}
1510 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001511 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001512 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001513 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1514 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001515 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001516 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001517 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1518 if (cb != null) {
1519 cb.onRefreshBatteryInfo(status);
1520 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001521 }
1522 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001523 }
1524
1525 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001526 * Handle {@link #MSG_SIM_STATE_CHANGE}
1527 */
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001528 @VisibleForTesting
1529 protected void handleSimStateChange(int subId, int slotId, State state) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001530
Jim Miller52a61332014-11-12 19:29:51 -08001531 if (DEBUG_SIM_STATES) {
1532 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1533 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001534 }
1535
Wink Savillea54bf652014-12-11 13:37:50 -08001536 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001537 Log.w(TAG, "invalid subId in handleSimStateChange()");
1538 return;
1539 }
1540
1541 SimData data = mSimDatas.get(subId);
1542 final boolean changed;
1543 if (data == null) {
1544 data = new SimData(state, slotId, subId);
1545 mSimDatas.put(subId, data);
1546 changed = true; // no data yet; force update
1547 } else {
1548 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1549 data.simState = state;
1550 data.subId = subId;
1551 data.slotId = slotId;
1552 }
1553 if (changed && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001554 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001555 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1556 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08001557 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001558 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001559 }
1560 }
1561 }
1562
Jim Millerbbf1a742012-07-17 18:30:30 -07001563 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001564 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1565 */
1566 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1567 if (DEBUG) {
1568 Log.d(TAG,
1569 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1570 }
1571
1572 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1573 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1574 return;
1575 }
1576
1577 mServiceStates.put(subId, serviceState);
1578
1579 for (int j = 0; j < mCallbacks.size(); j++) {
1580 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1581 if (cb != null) {
1582 cb.onRefreshCarrierInfo();
1583 }
1584 }
1585 }
1586
1587 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001588 * Notifies that the visibility state of Keyguard has changed.
1589 *
1590 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001591 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001592 public void onKeyguardVisibilityChanged(boolean showing) {
1593 if (DEBUG) Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
1594 mKeyguardIsVisible = showing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001595 for (int i = 0; i < mCallbacks.size(); i++) {
1596 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1597 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001598 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001599 }
1600 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001601 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001602 }
1603
Brian Colonna7fce3802013-09-17 15:51:32 -04001604 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001605 * Handle {@link #MSG_KEYGUARD_RESET}
1606 */
1607 private void handleKeyguardReset() {
1608 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Adrian Roosf6d51ac2015-09-02 13:26:25 -07001609 updateFingerprintListeningState();
Jorim Jaggi031f7952016-09-01 16:39:26 -07001610 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1611 }
1612
1613 private boolean resolveNeedsSlowUnlockTransition() {
1614 if (mUserManager.isUserUnlocked(getCurrentUser())) {
1615 return false;
1616 }
1617 Intent homeIntent = new Intent(Intent.ACTION_MAIN)
1618 .addCategory(Intent.CATEGORY_HOME);
1619 ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
1620 0 /* flags */);
1621 return FALLBACK_HOME_COMPONENT.equals(resolveInfo.getComponentInfo().getComponentName());
Selim Cinek1fcafc42015-07-20 14:39:25 -07001622 }
1623
1624 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001625 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1626 * @see #sendKeyguardBouncerChanged(boolean)
1627 */
1628 private void handleKeyguardBouncerChanged(int bouncer) {
1629 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1630 boolean isBouncer = (bouncer == 1);
1631 mBouncer = isBouncer;
1632 for (int i = 0; i < mCallbacks.size(); i++) {
1633 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1634 if (cb != null) {
1635 cb.onKeyguardBouncerChanged(isBouncer);
1636 }
1637 }
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001638 updateFingerprintListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02001639 }
1640
1641 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001642 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1643 */
1644 private void handleReportEmergencyCallAction() {
1645 for (int i = 0; i < mCallbacks.size(); i++) {
1646 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1647 if (cb != null) {
1648 cb.onEmergencyCallAction();
1649 }
1650 }
1651 }
1652
Lucas Dupin4272f442018-01-13 22:00:35 -08001653 private boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001654 final boolean nowPluggedIn = current.isPluggedIn();
1655 final boolean wasPluggedIn = old.isPluggedIn();
Lucas Dupin4272f442018-01-13 22:00:35 -08001656 final boolean stateChangedWhilePluggedIn = wasPluggedIn && nowPluggedIn
Jim Miller16464b82011-10-20 21:10:13 -07001657 && (old.status != current.status);
1658
1659 // change in plug state is always interesting
1660 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001661 return true;
1662 }
1663
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001664 // change in battery level
1665 if (old.level != current.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001666 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001667 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001668
1669 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08001670 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07001671 return true;
1672 }
1673
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001674 return false;
1675 }
1676
1677 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001678 * Remove the given observer's callback.
1679 *
Jim Miller6212cc02012-09-05 17:35:31 -07001680 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001681 */
Jim Miller6212cc02012-09-05 17:35:31 -07001682 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
1683 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1684 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1685 if (mCallbacks.get(i).get() == callback) {
1686 mCallbacks.remove(i);
1687 }
1688 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001689 }
1690
1691 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001692 * Register to receive notifications about general keyguard information
1693 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001694 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001695 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001696 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Jim Miller6212cc02012-09-05 17:35:31 -07001697 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1698 // Prevent adding duplicate callbacks
1699 for (int i = 0; i < mCallbacks.size(); i++) {
1700 if (mCallbacks.get(i).get() == callback) {
1701 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1702 new Exception("Called by"));
1703 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001704 }
1705 }
Jim Miller6212cc02012-09-05 17:35:31 -07001706 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1707 removeCallback(null); // remove unused references
1708 sendUpdates(callback);
1709 }
1710
Evan Rosky18396452016-07-27 15:19:37 -07001711 public boolean isSwitchingUser() {
1712 return mSwitchingUser;
1713 }
1714
1715 public void setSwitchingUser(boolean switching) {
1716 mSwitchingUser = switching;
Selim Cinekbbe19242017-12-08 15:42:08 -08001717 // Since this comes in on a binder thread, we need to post if first
1718 mHandler.post(mUpdateFingerprintListeningState);
Evan Rosky18396452016-07-27 15:19:37 -07001719 }
1720
Jim Miller6212cc02012-09-05 17:35:31 -07001721 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1722 // Notify listener of the current state
1723 callback.onRefreshBatteryInfo(mBatteryStatus);
1724 callback.onTimeChanged();
1725 callback.onRingerModeChanged(mRingMode);
1726 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001727 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001728 callback.onClockVisibilityChanged();
Lucas Dupin16cfe452018-02-08 13:14:50 -08001729 callback.onKeyguardVisibilityChangedRaw(mKeyguardIsVisible);
Jim Miller52a61332014-11-12 19:29:51 -08001730 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1731 final SimData state = data.getValue();
1732 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1733 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001734 }
1735
Selim Cinek1fcafc42015-07-20 14:39:25 -07001736 public void sendKeyguardReset() {
1737 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1738 }
1739
Adrian Roosb6011622014-05-14 15:52:53 +02001740 /**
1741 * @see #handleKeyguardBouncerChanged(int)
1742 */
1743 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1744 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1745 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1746 message.arg1 = showingBouncer ? 1 : 0;
1747 message.sendToTarget();
1748 }
1749
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001750 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001751 * 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 -08001752 * have the information earlier than waiting for the intent
1753 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001754 *
1755 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1756 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001757 */
Jim Miller52a61332014-11-12 19:29:51 -08001758 public void reportSimUnlocked(int subId) {
1759 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
Sanket Padawe7e460252017-03-10 16:18:20 -08001760 int slotId = SubscriptionManager.getSlotIndex(subId);
Jim Miller52a61332014-11-12 19:29:51 -08001761 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001762 }
1763
Brian Colonna7fce3802013-09-17 15:51:32 -04001764 /**
1765 * Report that the emergency call button has been pressed and the emergency dialer is
1766 * about to be displayed.
1767 *
1768 * @param bypassHandler runs immediately.
1769 *
1770 * NOTE: Must be called from UI thread if bypassHandler == true.
1771 */
1772 public void reportEmergencyCallAction(boolean bypassHandler) {
1773 if (!bypassHandler) {
1774 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1775 } else {
1776 handleReportEmergencyCallAction();
1777 }
1778 }
1779
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001780 /**
1781 * @return Whether the device is provisioned (whether they have gone through
1782 * the setup wizard)
1783 */
1784 public boolean isDeviceProvisioned() {
1785 return mDeviceProvisioned;
1786 }
1787
Jorim Jaggi9f743032015-05-04 15:22:40 -07001788 public void clearFailedUnlockAttempts() {
1789 mFailedAttempts.delete(sCurrentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001790 }
1791
Kensuke Matsui21d1bf12017-03-14 13:27:20 +09001792 public ServiceState getServiceState(int subId) {
1793 return mServiceStates.get(subId);
1794 }
1795
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001796 public int getFailedUnlockAttempts(int userId) {
1797 return mFailedAttempts.get(userId, 0);
Jorim Jaggi9f743032015-05-04 15:22:40 -07001798 }
1799
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001800 public void reportFailedStrongAuthUnlockAttempt(int userId) {
1801 mFailedAttempts.put(userId, getFailedUnlockAttempts(userId) + 1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001802 }
1803
Jim Millerf41fc962014-06-18 16:33:51 -07001804 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001805 mUserFingerprintAuthenticated.clear();
Kevin Chyn3fdbbf82017-05-06 15:11:53 -07001806 mTrustManager.clearAllFingerprints();
Jim Millerf41fc962014-06-18 16:33:51 -07001807 }
1808
Jim Miller52a61332014-11-12 19:29:51 -08001809 public boolean isSimPinVoiceSecure() {
1810 // TODO: only count SIMs that handle voice
1811 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001812 }
1813
1814 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001815 // True if any SIM is pin secure
1816 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1817 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1818 }
1819 return false;
1820 }
1821
Jason Monk9ff69bd2014-12-02 16:43:17 -05001822 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001823 if (mSimDatas.containsKey(subId)) {
1824 return mSimDatas.get(subId).simState;
1825 } else {
1826 return State.UNKNOWN;
1827 }
1828 }
1829
Winson Chungaa357452017-10-31 11:35:30 -07001830 private final SysUiTaskStackChangeListener
1831 mTaskStackListener = new SysUiTaskStackChangeListener() {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001832 @Override
1833 public void onTaskStackChangedBackground() {
1834 try {
1835 ActivityManager.StackInfo info = ActivityManager.getService().getStackInfo(
Wale Ogunwale68278562017-09-23 17:13:55 -07001836 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_ASSISTANT);
Kevin Chyn2fefd462017-04-28 12:18:19 -07001837 if (info == null) {
1838 return;
1839 }
1840 mHandler.sendMessage(mHandler.obtainMessage(MSG_ASSISTANT_STACK_CHANGED,
1841 info.visible));
1842 } catch (RemoteException e) {
1843 Log.e(TAG, "unable to check task stack", e);
1844 }
1845 }
1846 };
1847
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001848 /**
Richard Choue0381b82018-04-24 03:48:59 +00001849 * @return true if and only if the state has changed for the specified {@code slotId}
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001850 */
Richard Choue0381b82018-04-24 03:48:59 +00001851 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08001852
1853 // This is awful. It exists because there are two APIs for getting the SIM status
1854 // that don't return the complete set of values and have different types. In Keyguard we
1855 // need IccCardConstants, but TelephonyManager would only give us
1856 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001857 final TelephonyManager tele = TelephonyManager.from(mContext);
Richard Choue0381b82018-04-24 03:48:59 +00001858 int simState = tele.getSimState(slotId);
1859 State state;
1860 try {
1861 state = State.intToState(simState);
1862 } catch(IllegalArgumentException ex) {
1863 Log.w(TAG, "Unknown sim state: " + simState);
1864 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05001865 }
Richard Choue0381b82018-04-24 03:48:59 +00001866 SimData data = mSimDatas.get(subId);
1867 final boolean changed;
1868 if (data == null) {
1869 data = new SimData(state, slotId, subId);
1870 mSimDatas.put(subId, data);
1871 changed = true; // no data yet; force update
1872 } else {
1873 changed = data.simState != state;
1874 data.simState = state;
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001875 }
Richard Choue0381b82018-04-24 03:48:59 +00001876 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07001877 }
1878
1879 public static boolean isSimPinSecure(IccCardConstants.State state) {
1880 final IccCardConstants.State simState = state;
1881 return (simState == IccCardConstants.State.PIN_REQUIRED
1882 || simState == IccCardConstants.State.PUK_REQUIRED
1883 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001884 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001885
1886 public DisplayClientState getCachedDisplayClientState() {
1887 return mDisplayClientState;
1888 }
Jim Miller20daffd2013-10-07 14:59:53 -07001889
1890 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1891 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001892 public void dispatchStartedWakingUp() {
1893 synchronized (this) {
1894 mDeviceInteractive = true;
1895 }
1896 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1897 }
1898
Jorim Jaggi95e40382015-09-16 15:53:42 -07001899 public void dispatchStartedGoingToSleep(int why) {
1900 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
1901 }
1902
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001903 public void dispatchFinishedGoingToSleep(int why) {
1904 synchronized(this) {
1905 mDeviceInteractive = false;
1906 }
1907 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1908 }
1909
Jim Miller20daffd2013-10-07 14:59:53 -07001910 public void dispatchScreenTurnedOn() {
1911 synchronized (this) {
1912 mScreenOn = true;
1913 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001914 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001915 }
1916
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001917 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001918 synchronized(this) {
1919 mScreenOn = false;
1920 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001921 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001922 }
1923
Selim Cinek99415392016-09-09 14:58:41 -07001924 public void dispatchDreamingStarted() {
1925 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 1, 0));
1926 }
1927
1928 public void dispatchDreamingStopped() {
1929 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 0, 0));
1930 }
1931
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001932 public boolean isDeviceInteractive() {
1933 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001934 }
Jim Miller52a61332014-11-12 19:29:51 -08001935
Jorim Jaggi95e40382015-09-16 15:53:42 -07001936 public boolean isGoingToSleep() {
1937 return mGoingToSleep;
1938 }
1939
Jim Miller52a61332014-11-12 19:29:51 -08001940 /**
1941 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1942 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001943 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001944 */
1945 public int getNextSubIdForState(State state) {
1946 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001947 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001948 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1949 for (int i = 0; i < list.size(); i++) {
1950 final SubscriptionInfo info = list.get(i);
1951 final int id = info.getSubscriptionId();
Sanket Padawe7e460252017-03-10 16:18:20 -08001952 int slotId = SubscriptionManager.getSlotIndex(id);
Jim Miller52a61332014-11-12 19:29:51 -08001953 if (state == getSimState(id) && bestSlotId > slotId ) {
1954 resultId = id;
1955 bestSlotId = slotId;
1956 }
1957 }
1958 return resultId;
1959 }
1960
1961 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1962 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1963 for (int i = 0; i < list.size(); i++) {
1964 SubscriptionInfo info = list.get(i);
1965 if (subId == info.getSubscriptionId()) return info;
1966 }
1967 return null; // not found
1968 }
Jason Monkab525272015-07-13 17:02:49 -04001969
Alex Chauff7653d2018-02-01 17:18:08 +00001970 /**
1971 * @return a cached version of DevicePolicyManager.isLogoutEnabled()
1972 */
1973 public boolean isLogoutEnabled() {
1974 return mLogoutEnabled;
1975 }
1976
1977 private void updateLogoutEnabled() {
1978 boolean logoutEnabled = mDevicePolicyManager.isLogoutEnabled();
1979 if (mLogoutEnabled != logoutEnabled) {
1980 mLogoutEnabled = logoutEnabled;
1981 for (int i = 0; i < mCallbacks.size(); i++) {
1982 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1983 if (cb != null) {
1984 cb.onLogoutEnabledChanged();
1985 }
1986 }
1987 }
1988 }
1989
Jason Monkab525272015-07-13 17:02:49 -04001990 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1991 pw.println("KeyguardUpdateMonitor state:");
1992 pw.println(" SIM States:");
1993 for (SimData data : mSimDatas.values()) {
1994 pw.println(" " + data.toString());
1995 }
1996 pw.println(" Subs:");
1997 if (mSubscriptionInfo != null) {
1998 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
1999 pw.println(" " + mSubscriptionInfo.get(i));
2000 }
2001 }
2002 pw.println(" Service states:");
2003 for (int subId : mServiceStates.keySet()) {
2004 pw.println(" " + subId + "=" + mServiceStates.get(subId));
2005 }
Jim Millerd72d5ac2015-09-29 18:55:32 -07002006 if (mFpm != null && mFpm.isHardwareDetected()) {
2007 final int userId = ActivityManager.getCurrentUser();
2008 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
2009 pw.println(" Fingerprint state (user=" + userId + ")");
2010 pw.println(" allowed=" + isUnlockingWithFingerprintAllowed());
2011 pw.println(" auth'd=" + mUserFingerprintAuthenticated.get(userId));
2012 pw.println(" authSinceBoot="
2013 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
2014 pw.println(" disabled(DPM)=" + isFingerprintDisabled(userId));
2015 pw.println(" possible=" + isUnlockWithFingerprintPossible(userId));
2016 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
Jim Millerd72d5ac2015-09-29 18:55:32 -07002017 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
2018 }
Jason Monkab525272015-07-13 17:02:49 -04002019 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002020}