blob: c826aaaac7490755ded3151a61c5576c76f7cd03 [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;
Adrian Roos46842d92014-03-27 14:58:03 +010071import android.util.SparseBooleanArray;
Jorim Jaggi9f743032015-05-04 15:22:40 -070072import android.util.SparseIntArray;
Adrian Roos46842d92014-03-27 14:58:03 +010073
Lucas Dupin7517b5d2017-08-22 12:51:25 -070074import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi86bed402015-08-20 18:20:02 -070075import com.android.internal.telephony.IccCardConstants;
76import com.android.internal.telephony.IccCardConstants.State;
77import com.android.internal.telephony.PhoneConstants;
78import com.android.internal.telephony.TelephonyIntents;
Adrian Roosb5e47222015-08-14 15:53:06 -070079import com.android.internal.widget.LockPatternUtils;
Winson Chungaa357452017-10-31 11:35:30 -070080import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
Winson Chung2cf6ad82017-11-09 17:36:59 -080081import com.android.systemui.shared.system.ActivityManagerWrapper;
Jorim Jaggi86bed402015-08-20 18:20:02 -070082
Kevin Chyn36778ff2017-09-07 19:55:38 -070083import com.google.android.collect.Lists;
84
Jason Monkab525272015-07-13 17:02:49 -040085import java.io.FileDescriptor;
86import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -070087import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080088import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -080089import java.util.HashMap;
richard choub1b24e72018-03-22 14:39:43 +080090import java.util.HashSet;
Jim Miller52a61332014-11-12 19:29:51 -080091import 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 choub1b24e72018-03-22 14:39:43 +0800403 List<Integer> changedSubscriptionIds = refreshSimState(subscriptionInfos);
404 for (int i = 0; i < changedSubscriptionIds.size(); i++) {
405 SimData data = mSimDatas.get(changedSubscriptionIds.get(i));
Jim Miller52a61332014-11-12 19:29:51 -0800406 for (int j = 0; j < mCallbacks.size(); j++) {
407 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
408 if (cb != null) {
409 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
410 }
411 }
412 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500413 for (int j = 0; j < mCallbacks.size(); j++) {
414 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
415 if (cb != null) {
416 cb.onRefreshCarrierInfo();
417 }
418 }
Jim Miller52a61332014-11-12 19:29:51 -0800419 }
420
Jason Monk052082c2015-06-11 11:35:23 -0400421 private void handleAirplaneModeChanged() {
422 for (int j = 0; j < mCallbacks.size(); j++) {
423 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
424 if (cb != null) {
425 cb.onRefreshCarrierInfo();
426 }
427 }
428 }
429
Wink Savilled09c4ca2014-11-22 10:08:16 -0800430 /** @return List of SubscriptionInfo records, maybe empty but never null */
Adrian Roos316bf542016-08-23 17:53:07 +0200431 public List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800432 List<SubscriptionInfo> sil = mSubscriptionInfo;
433 if (sil == null || forceReload) {
434 sil = mSubscriptionManager.getActiveSubscriptionInfoList();
435 }
436 if (sil == null) {
437 // getActiveSubscriptionInfoList was null callers expect an empty list.
438 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
439 } else {
440 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800441 }
442 return mSubscriptionInfo;
443 }
444
Adrian Roos7861c662014-07-25 15:37:28 +0200445 @Override
446 public void onTrustManagedChanged(boolean managed, int userId) {
447 mUserTrustIsManaged.put(userId, managed);
448
449 for (int i = 0; i < mCallbacks.size(); i++) {
450 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
451 if (cb != null) {
452 cb.onTrustManagedChanged(userId);
453 }
454 }
455 }
456
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700457 /**
458 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is goingAway
459 * @param goingAway
460 */
461 public void setKeyguardGoingAway(boolean goingAway) {
462 mKeyguardGoingAway = goingAway;
Kevin Chyne22f1342017-09-26 10:03:38 -0700463 updateFingerprintListeningState();
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700464 }
465
Kevin Chyn2fefd462017-04-28 12:18:19 -0700466 /**
467 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is occluded
468 * @param occluded
469 */
470 public void setKeyguardOccluded(boolean occluded) {
471 mKeyguardOccluded = occluded;
472 updateFingerprintListeningState();
473 }
474
Kevin Chyn36778ff2017-09-07 19:55:38 -0700475 /**
476 * @return a cached version of DreamManager.isDreaming()
477 */
478 public boolean isDreaming() {
479 return mIsDreaming;
480 }
481
482 /**
483 * If the device is dreaming, awakens the device
484 */
485 public void awakenFromDream() {
486 if (mIsDreaming && mDreamManager != null) {
487 try {
488 mDreamManager.awaken();
489 } catch (RemoteException e) {
490 Log.e(TAG, "Unable to awaken from dream");
491 }
492 }
493 }
494
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700495 private void onFingerprintAuthenticated(int userId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700496 Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
Jim Miller9f0753f2015-03-23 23:59:22 -0700497 mUserFingerprintAuthenticated.put(userId, true);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700498 // Update/refresh trust state only if user can skip bouncer
499 if (getUserCanSkipBouncer(userId)) {
500 mTrustManager.unlockedByFingerprintForUser(userId);
501 }
Kevin Chyn625a0142017-04-10 14:53:59 -0700502 // Don't send cancel if authentication succeeds
503 mFingerprintCancelSignal = null;
Jim Millerf41fc962014-06-18 16:33:51 -0700504 for (int i = 0; i < mCallbacks.size(); i++) {
505 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
506 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700507 cb.onFingerprintAuthenticated(userId);
Jim Millerf41fc962014-06-18 16:33:51 -0700508 }
509 }
Kevin Chyn2fefd462017-04-28 12:18:19 -0700510
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700511 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE),
512 FINGERPRINT_CONTINUE_DELAY_MS);
513
Kevin Chyn2fefd462017-04-28 12:18:19 -0700514 // Only authenticate fingerprint once when assistant is visible
515 mAssistantVisible = false;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700516
Nick Desaulniers1d396752016-07-25 15:05:33 -0700517 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700518 }
519
Jim Millerce7eb6d2015-04-03 19:29:13 -0700520 private void handleFingerprintAuthFailed() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700521 for (int i = 0; i < mCallbacks.size(); i++) {
522 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
523 if (cb != null) {
524 cb.onFingerprintAuthFailed();
525 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700526 }
Jim Millerce7eb6d2015-04-03 19:29:13 -0700527 handleFingerprintHelp(-1, mContext.getString(R.string.fingerprint_not_recognized));
528 }
Jim Millerf41fc962014-06-18 16:33:51 -0700529
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700530 private void handleFingerprintAcquired(int acquireInfo) {
531 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
532 return;
533 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700534 for (int i = 0; i < mCallbacks.size(); i++) {
535 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
536 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700537 cb.onFingerprintAcquired();
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700538 }
539 }
540 }
541
Jim Miller837fa7e2016-08-08 20:16:22 -0700542 private void handleFingerprintAuthenticated(int authUserId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700543 Trace.beginSection("KeyGuardUpdateMonitor#handlerFingerPrintAuthenticated");
Jim Millerf41fc962014-06-18 16:33:51 -0700544 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700545 final int userId;
546 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800547 userId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700548 } catch (RemoteException e) {
549 Log.e(TAG, "Failed to get current user id: ", e);
550 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700551 }
Jim Miller837fa7e2016-08-08 20:16:22 -0700552 if (userId != authUserId) {
553 Log.d(TAG, "Fingerprint authenticated for wrong user: " + authUserId);
554 return;
555 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700556 if (isFingerprintDisabled(userId)) {
557 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
558 return;
559 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700560 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700561 } finally {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700562 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700563 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700564 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700565 }
566
Jim Miller9f0753f2015-03-23 23:59:22 -0700567 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700568 for (int i = 0; i < mCallbacks.size(); i++) {
569 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
570 if (cb != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700571 cb.onFingerprintHelp(msgId, helpString);
572 }
573 }
574 }
575
Kevin Chyn0c45b072017-04-24 16:27:11 -0700576 private Runnable mRetryFingerprintAuthentication = new Runnable() {
577 @Override
578 public void run() {
579 Log.w(TAG, "Retrying fingerprint after HW unavailable, attempt " +
580 mHardwareUnavailableRetryCount);
581 updateFingerprintListeningState();
582 }
583 };
584
Jim Miller9f0753f2015-03-23 23:59:22 -0700585 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700586 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
587 && mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
588 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
589 startListeningForFingerprint();
590 } else {
591 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
592 }
Kevin Chyn0c45b072017-04-24 16:27:11 -0700593
594 if (msgId == FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE) {
595 if (mHardwareUnavailableRetryCount < HW_UNAVAILABLE_RETRY_MAX) {
596 mHardwareUnavailableRetryCount++;
597 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
598 mHandler.postDelayed(mRetryFingerprintAuthentication, HW_UNAVAILABLE_TIMEOUT);
599 }
600 }
601
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700602 if (msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT) {
603 mLockPatternUtils.requireStrongAuth(
604 LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
605 getCurrentUser());
606 }
607
Jim Miller9f0753f2015-03-23 23:59:22 -0700608 for (int i = 0; i < mCallbacks.size(); i++) {
609 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
610 if (cb != null) {
611 cb.onFingerprintError(msgId, errString);
Jim Millerf41fc962014-06-18 16:33:51 -0700612 }
613 }
614 }
615
Jorim Jaggi3a464782015-08-28 16:59:13 -0700616 private void handleFingerprintLockoutReset() {
617 updateFingerprintListeningState();
618 }
619
Jorim Jaggi86bed402015-08-20 18:20:02 -0700620 private void setFingerprintRunningState(int fingerprintRunningState) {
621 boolean wasRunning = mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
622 boolean isRunning = fingerprintRunningState == FINGERPRINT_STATE_RUNNING;
623 mFingerprintRunningState = fingerprintRunningState;
624
625 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
626 // asynchronousness of the cancel cycle. So only notify them if the actualy running state
627 // has changed.
628 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700629 notifyFingerprintRunningStateChanged();
630 }
631 }
632
633 private void notifyFingerprintRunningStateChanged() {
634 for (int i = 0; i < mCallbacks.size(); i++) {
635 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
636 if (cb != null) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700637 cb.onFingerprintRunningStateChanged(isFingerprintDetectionRunning());
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700638 }
639 }
640 }
Adrian Roos4a410172014-08-20 17:41:44 +0200641 private void handleFaceUnlockStateChanged(boolean running, int userId) {
642 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200643 for (int i = 0; i < mCallbacks.size(); i++) {
644 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
645 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200646 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200647 }
648 }
649 }
650
Adrian Roos4a410172014-08-20 17:41:44 +0200651 public boolean isFaceUnlockRunning(int userId) {
652 return mUserFaceUnlockRunning.get(userId);
653 }
654
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700655 public boolean isFingerprintDetectionRunning() {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700656 return mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700657 }
658
Jim Miller50e62182014-04-23 17:25:00 -0700659 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100660 // Don't allow trust agent if device is secured with a SIM PIN. This is here
661 // mainly because there's no other way to prompt the user to enter their SIM PIN
662 // once they get past the keyguard screen.
663 final boolean disabledBySimPin = isSimPinSecure();
664 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700665 }
666
Jim Miller06e34502014-07-17 14:46:05 -0700667 private boolean isFingerprintDisabled(int userId) {
668 final DevicePolicyManager dpm =
669 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
670 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Adrian Roos733b6632015-08-21 14:32:35 -0700671 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
672 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700673 }
674
Selim Cineke8bae622015-07-15 13:24:06 -0700675 public boolean getUserCanSkipBouncer(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700676 return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
677 && isUnlockingWithFingerprintAllowed());
Selim Cineke8bae622015-07-15 13:24:06 -0700678 }
679
Adrian Roos46842d92014-03-27 14:58:03 +0100680 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700681 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100682 }
683
Adrian Roos7861c662014-07-25 15:37:28 +0200684 public boolean getUserTrustIsManaged(int userId) {
685 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
686 }
687
Selim Cinek1fcafc42015-07-20 14:39:25 -0700688 public boolean isUnlockingWithFingerprintAllowed() {
Michal Karpinskic52f8672016-11-18 11:32:45 +0000689 return mStrongAuthTracker.isUnlockingWithFingerprintAllowed();
Adrian Roosb5e47222015-08-14 15:53:06 -0700690 }
691
Jorim Jaggi031f7952016-09-01 16:39:26 -0700692 public boolean needsSlowUnlockTransition() {
693 return mNeedsSlowUnlockTransition;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700694 }
695
Adrian Roosb5e47222015-08-14 15:53:06 -0700696 public StrongAuthTracker getStrongAuthTracker() {
697 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700698 }
699
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700700 private void notifyStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700701 for (int i = 0; i < mCallbacks.size(); i++) {
702 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
703 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700704 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700705 }
706 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700707 }
708
Adrian Roos91ba3072017-02-14 16:50:46 +0100709 public boolean isScreenOn() {
710 return mScreenOn;
711 }
712
Lucas Dupinef886542018-01-03 16:03:07 -0800713 private void dispatchErrorMessage(CharSequence message) {
714 for (int i = 0; i < mCallbacks.size(); i++) {
715 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
716 if (cb != null) {
717 cb.onTrustAgentErrorMessage(message);
718 }
719 }
720 }
721
Jim Miller8f09fd22013-03-14 19:04:28 -0700722 static class DisplayClientState {
723 public int clientGeneration;
724 public boolean clearing;
725 public PendingIntent intent;
726 public int playbackState;
727 public long playbackEventTime;
728 }
729
730 private DisplayClientState mDisplayClientState = new DisplayClientState();
731
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800732 @VisibleForTesting
733 final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700734
Jim Millerd72d5ac2015-09-29 18:55:32 -0700735 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700736 public void onReceive(Context context, Intent intent) {
737 final String action = intent.getAction();
738 if (DEBUG) Log.d(TAG, "received broadcast " + action);
739
740 if (Intent.ACTION_TIME_TICK.equals(action)
741 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200742 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700743 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700744 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
745 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
746 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
747 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
748 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -0800749
750 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
751 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
752 final int maxChargingMicroWatt;
753
754 if (maxChargingMicroVolt <= 0) {
755 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
756 }
757 if (maxChargingMicroAmp > 0) {
758 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
759 // to maintain precision equally on both factors.
760 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
761 * (maxChargingMicroVolt / 1000);
762 } else {
763 maxChargingMicroWatt = -1;
764 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700765 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700766 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -0800767 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -0700768 mHandler.sendMessage(msg);
769 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800770 // ACTION_SIM_STATE_CHANGED is rebroadcast after unlocking the device to
771 // keep compatibility with apps that aren't direct boot aware.
772 // SysUI should just ignore this broadcast because it was already received
773 // and processed previously.
774 if (intent.getBooleanExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, false)) {
775 return;
776 }
Jim Miller52a61332014-11-12 19:29:51 -0800777 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700778 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800779 Log.v(TAG, "action " + action
780 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
781 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700782 }
Jim Miller52a61332014-11-12 19:29:51 -0800783 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
784 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700785 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
786 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
787 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
788 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
789 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
790 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400791 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
792 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800793 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700794 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700795 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
796 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
797 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
798 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
799 if (DEBUG) {
800 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
801 + subId);
802 }
803 mHandler.sendMessage(
804 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Alex Chauff7653d2018-02-01 17:18:08 +0000805 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(
806 action)) {
807 mHandler.sendEmptyMessage(MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jim Millerbbf1a742012-07-17 18:30:30 -0700808 }
809 }
810 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700811
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700812 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
813
Jim Millerd72d5ac2015-09-29 18:55:32 -0700814 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700815 public void onReceive(Context context, Intent intent) {
816 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200817 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
818 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
819 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700820 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
821 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200822 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700823 Trace.beginSection("KeyguardUpdateMonitor.mBroadcastAllReceiver#onReceive ACTION_FACE_UNLOCK_STARTED");
Adrian Roos48c796c2014-09-01 14:59:23 +0200824 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
825 getSendingUserId()));
Nick Desaulniers1d396752016-07-25 15:05:33 -0700826 Trace.endSection();
Adrian Roos48c796c2014-09-01 14:59:23 +0200827 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
828 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
829 getSendingUserId()));
830 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
831 .equals(action)) {
832 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -0700833 } else if (ACTION_USER_UNLOCKED.equals(action)) {
834 mHandler.sendEmptyMessage(MSG_USER_UNLOCKED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700835 }
836 }
837 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700838
Jorim Jaggi3a464782015-08-28 16:59:13 -0700839 private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
840 = new FingerprintManager.LockoutResetCallback() {
841 @Override
842 public void onLockoutReset() {
843 handleFingerprintLockoutReset();
844 }
845 };
846
Jim Miller9f0753f2015-03-23 23:59:22 -0700847 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
848 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700849
850 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700851 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700852 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700853 };
854
855 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700856 public void onAuthenticationSucceeded(AuthenticationResult result) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700857 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
Jim Miller837fa7e2016-08-08 20:16:22 -0700858 handleFingerprintAuthenticated(result.getUserId());
Nick Desaulniers1d396752016-07-25 15:05:33 -0700859 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700860 }
861
862 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700863 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700864 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700865 }
866
867 @Override
868 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700869 handleFingerprintError(errMsgId, errString.toString());
870 }
871
872 @Override
873 public void onAuthenticationAcquired(int acquireInfo) {
874 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700875 }
876 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700877 private CancellationSignal mFingerprintCancelSignal;
878 private FingerprintManager mFpm;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700879
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800880 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800881 * When we receive a
882 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700883 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800884 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800885 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800886 */
Jim Miller52a61332014-11-12 19:29:51 -0800887 private static class SimData {
888 public State simState;
889 public int slotId;
890 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800891
Jim Miller52a61332014-11-12 19:29:51 -0800892 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800893 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800894 slotId = slot;
895 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800896 }
897
Jim Miller52a61332014-11-12 19:29:51 -0800898 static SimData fromIntent(Intent intent) {
899 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800900 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
901 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
902 }
Wink Savillea639b312012-07-10 12:37:54 -0700903 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800904 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
905 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800906 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700907 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700908 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700909 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700910
Wink Savillea639b312012-07-10 12:37:54 -0700911 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700912 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700913 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700914 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700915 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700916 }
Wink Savillea639b312012-07-10 12:37:54 -0700917 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
918 state = IccCardConstants.State.READY;
919 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800920 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700921 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
922 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
923 state = IccCardConstants.State.PIN_REQUIRED;
924 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
925 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800926 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700927 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800928 }
Wink Savillea639b312012-07-10 12:37:54 -0700929 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
930 state = IccCardConstants.State.NETWORK_LOCKED;
Wileen Chiu6b8b22e2014-10-29 22:48:21 +0530931 } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
932 state = IccCardConstants.State.CARD_IO_ERROR;
Jim Miller109f1fd2012-09-19 20:44:16 -0700933 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
934 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
935 // This is required because telephony doesn't return to "READY" after
936 // these state transitions. See bug 7197471.
937 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800938 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700939 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800940 }
Jim Miller52a61332014-11-12 19:29:51 -0800941 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800942 }
943
Jim Millerd72d5ac2015-09-29 18:55:32 -0700944 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800945 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800946 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800947 }
948 }
949
Adrian Roos12c1ef52014-06-04 13:54:08 +0200950 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700951 public static final int CHARGING_UNKNOWN = -1;
952 public static final int CHARGING_SLOWLY = 0;
953 public static final int CHARGING_REGULAR = 1;
954 public static final int CHARGING_FAST = 2;
955
Jim Miller16464b82011-10-20 21:10:13 -0700956 public final int status;
957 public final int level;
958 public final int plugged;
959 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800960 public final int maxChargingWattage;
961 public BatteryStatus(int status, int level, int plugged, int health,
962 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -0700963 this.status = status;
964 this.level = level;
965 this.plugged = plugged;
966 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800967 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -0700968 }
969
Jim Millerbbf1a742012-07-17 18:30:30 -0700970 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700971 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700972 * @return true if the device is plugged in.
973 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100974 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700975 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700976 || plugged == BatteryManager.BATTERY_PLUGGED_USB
977 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -0700978 }
979
980 /**
Beverly2034c832018-03-19 11:18:51 -0400981 * Determine whether the device is plugged in (USB, power).
982 * @return true if the device is plugged in wired (as opposed to wireless)
983 */
984 public boolean isPluggedInWired() {
985 return plugged == BatteryManager.BATTERY_PLUGGED_AC
986 || plugged == BatteryManager.BATTERY_PLUGGED_USB;
987 }
988
989 /**
Jim Millerbbf1a742012-07-17 18:30:30 -0700990 * Whether or not the device is charged. Note that some devices never return 100% for
991 * battery level, so this allows either battery level or status to determine if the
992 * battery is charged.
993 * @return true if the device is charged
994 */
995 public boolean isCharged() {
996 return status == BATTERY_STATUS_FULL || level >= 100;
997 }
998
999 /**
1000 * Whether battery is low and needs to be charged.
1001 * @return true if battery is low
1002 */
1003 public boolean isBatteryLow() {
1004 return level < LOW_BATTERY_THRESHOLD;
1005 }
1006
Adrian Roos7b043112015-07-10 13:00:33 -07001007 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -08001008 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
1009 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
1010 maxChargingWattage > fastThreshold ? CHARGING_FAST :
Adrian Roos7b043112015-07-10 13:00:33 -07001011 CHARGING_REGULAR;
1012 }
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001013
1014 @Override
1015 public String toString() {
1016 return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged
1017 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}";
1018 }
Jim Miller16464b82011-10-20 21:10:13 -07001019 }
1020
Adrian Roosb5e47222015-08-14 15:53:06 -07001021 public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001022 public StrongAuthTracker(Context context) {
1023 super(context);
1024 }
Adrian Roosb5e47222015-08-14 15:53:06 -07001025
1026 public boolean isUnlockingWithFingerprintAllowed() {
1027 int userId = getCurrentUser();
1028 return isFingerprintAllowedForUser(userId);
1029 }
1030
1031 public boolean hasUserAuthenticatedSinceBoot() {
1032 int userId = getCurrentUser();
1033 return (getStrongAuthForUser(userId)
1034 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
1035 }
1036
1037 @Override
1038 public void onStrongAuthRequiredChanged(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -07001039 notifyStrongAuthStateChanged(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -07001040 }
1041 }
1042
Jim Millerdcb3d842012-08-23 19:18:12 -07001043 public static KeyguardUpdateMonitor getInstance(Context context) {
1044 if (sInstance == null) {
1045 sInstance = new KeyguardUpdateMonitor(context);
1046 }
1047 return sInstance;
1048 }
1049
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001050 protected void handleStartedWakingUp() {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001051 Trace.beginSection("KeyguardUpdateMonitor#handleStartedWakingUp");
Jorim Jaggi864e64b2015-05-20 14:13:23 -07001052 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001053 final int count = mCallbacks.size();
1054 for (int i = 0; i < count; i++) {
1055 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1056 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001057 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -07001058 }
1059 }
Nick Desaulniers1d396752016-07-25 15:05:33 -07001060 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -07001061 }
1062
Jorim Jaggi95e40382015-09-16 15:53:42 -07001063 protected void handleStartedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -07001064 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -07001065 final int count = mCallbacks.size();
1066 for (int i = 0; i < count; i++) {
1067 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1068 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001069 cb.onStartedGoingToSleep(arg1);
1070 }
1071 }
1072 mGoingToSleep = true;
Jorim Jaggi95e40382015-09-16 15:53:42 -07001073 updateFingerprintListeningState();
1074 }
1075
1076 protected void handleFinishedGoingToSleep(int arg1) {
1077 mGoingToSleep = false;
1078 final int count = mCallbacks.size();
1079 for (int i = 0; i < count; i++) {
1080 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1081 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001082 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -07001083 }
1084 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001085 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001086 }
1087
Jorim Jaggif1518da2015-07-30 11:56:36 -07001088 private void handleScreenTurnedOn() {
1089 final int count = mCallbacks.size();
1090 for (int i = 0; i < count; i++) {
1091 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1092 if (cb != null) {
1093 cb.onScreenTurnedOn();
1094 }
1095 }
1096 }
1097
1098 private void handleScreenTurnedOff() {
Kevin Chyn0c45b072017-04-24 16:27:11 -07001099 mHardwareUnavailableRetryCount = 0;
Jorim Jaggif1518da2015-07-30 11:56:36 -07001100 final int count = mCallbacks.size();
1101 for (int i = 0; i < count; i++) {
1102 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1103 if (cb != null) {
1104 cb.onScreenTurnedOff();
1105 }
1106 }
1107 }
1108
Selim Cinek99415392016-09-09 14:58:41 -07001109 private void handleDreamingStateChanged(int dreamStart) {
1110 final int count = mCallbacks.size();
Kevin Chyn36778ff2017-09-07 19:55:38 -07001111 mIsDreaming = dreamStart == 1;
Selim Cinek99415392016-09-09 14:58:41 -07001112 for (int i = 0; i < count; i++) {
1113 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1114 if (cb != null) {
Kevin Chyn36778ff2017-09-07 19:55:38 -07001115 cb.onDreamingStateChanged(mIsDreaming);
Selim Cinek99415392016-09-09 14:58:41 -07001116 }
1117 }
Kevin Chyn20a68dc2017-09-21 11:41:01 -07001118 updateFingerprintListeningState();
Selim Cinek99415392016-09-09 14:58:41 -07001119 }
1120
Adam Powell43a372f2013-09-27 17:43:53 -07001121 /**
1122 * IMPORTANT: Must be called from UI thread.
1123 */
1124 public void dispatchSetBackground(Bitmap bmp) {
1125 if (DEBUG) Log.d(TAG, "dispatchSetBackground");
1126 final int count = mCallbacks.size();
1127 for (int i = 0; i < count; i++) {
1128 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1129 if (cb != null) {
1130 cb.onSetBackground(bmp);
1131 }
1132 }
1133 }
1134
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001135 private void handleUserInfoChanged(int userId) {
1136 for (int i = 0; i < mCallbacks.size(); i++) {
1137 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1138 if (cb != null) {
1139 cb.onUserInfoChanged(userId);
1140 }
1141 }
1142 }
1143
Jorim Jaggidadafd42016-09-30 07:20:25 -07001144 private void handleUserUnlocked() {
1145 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1146 for (int i = 0; i < mCallbacks.size(); i++) {
1147 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1148 if (cb != null) {
1149 cb.onUserUnlocked();
1150 }
1151 }
1152 }
1153
Lucas Dupin7517b5d2017-08-22 12:51:25 -07001154 @VisibleForTesting
1155 protected KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001156 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -08001157 mSubscriptionManager = SubscriptionManager.from(context);
Michael Jurkafff56142012-11-28 16:51:00 -08001158 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001159 mStrongAuthTracker = new StrongAuthTracker(context);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001160
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001161 // Since device can't be un-provisioned, we only need to register a content observer
1162 // to update mDeviceProvisioned when we are...
1163 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001164 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001165 }
Jim Miller47088bb2009-11-24 00:40:16 -08001166
Jim Millerbbf1a742012-07-17 18:30:30 -07001167 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -07001168 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001169
Jim Millerbbf1a742012-07-17 18:30:30 -07001170 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001171 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001172 filter.addAction(Intent.ACTION_TIME_TICK);
1173 filter.addAction(Intent.ACTION_TIME_CHANGED);
1174 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1175 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -04001176 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001177 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -07001178 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001179 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001180 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Alex Chauff7653d2018-02-01 17:18:08 +00001181 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001182 context.registerReceiver(mBroadcastReceiver, filter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001183
Adam Cohenc276e822012-11-08 13:01:08 -08001184 final IntentFilter bootCompleteFilter = new IntentFilter();
1185 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1186 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
1187 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter);
1188
Adrian Roos48c796c2014-09-01 14:59:23 +02001189 final IntentFilter allUserFilter = new IntentFilter();
1190 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1191 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1192 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1193 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1194 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001195 allUserFilter.addAction(ACTION_USER_UNLOCKED);
Adrian Roos48c796c2014-09-01 14:59:23 +02001196 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001197 null, null);
1198
Wink Saville071743f2015-01-12 17:11:04 -08001199 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001200 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001201 ActivityManager.getService().registerUserSwitchObserver(
Sudheer Shanka2c4522c2016-08-27 20:53:28 -07001202 new UserSwitchObserver() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001203 @Override
1204 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001205 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001206 newUserId, 0, reply));
1207 }
1208 @Override
1209 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001210 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001211 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001212 }
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001213 }, TAG);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001214 } catch (RemoteException e) {
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001215 e.rethrowAsRuntimeException();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001216 }
Adrian Roos46842d92014-03-27 14:58:03 +01001217
Jorim Jaggi237b0612015-05-01 14:28:49 -07001218 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1219 mTrustManager.registerTrustListener(this);
Michal Karpinskic52f8672016-11-18 11:32:45 +00001220 mLockPatternUtils = new LockPatternUtils(context);
1221 mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001222
Kevin Chyn36778ff2017-09-07 19:55:38 -07001223 mDreamManager = IDreamManager.Stub.asInterface(
1224 ServiceManager.getService(DreamService.DREAM_SERVICE));
1225
Jorim Jaggi3f124262016-11-22 13:45:17 +01001226 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
1227 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
1228 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001229 updateFingerprintListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001230 if (mFpm != null) {
1231 mFpm.addLockoutResetCallback(mLockoutResetCallback);
1232 }
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001233
Winson Chung2cf6ad82017-11-09 17:36:59 -08001234 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001235 mUserManager = context.getSystemService(UserManager.class);
Alex Chauff7653d2018-02-01 17:18:08 +00001236 mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
1237 mLogoutEnabled = mDevicePolicyManager.isLogoutEnabled();
Jorim Jaggiea657062015-04-28 13:45:11 -07001238 }
1239
1240 private void updateFingerprintListeningState() {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001241 // If this message exists, we should not authenticate again until this message is
1242 // consumed by the handler
1243 if (mHandler.hasMessages(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE)) {
1244 return;
1245 }
Kevin Chyn0c45b072017-04-24 16:27:11 -07001246 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Jorim Jaggiea657062015-04-28 13:45:11 -07001247 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001248 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001249 stopListeningForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001250 } else if (mFingerprintRunningState != FINGERPRINT_STATE_RUNNING
1251 && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001252 startListeningForFingerprint();
1253 }
1254 }
1255
Kevin Chyn129f60f2017-08-11 17:24:42 -07001256 private boolean shouldListenForFingerprintAssistant() {
1257 return mAssistantVisible && mKeyguardOccluded
1258 && !mUserFingerprintAuthenticated.get(getCurrentUser(), false)
1259 && !mUserHasTrust.get(getCurrentUser(), false);
1260 }
1261
Jorim Jaggiea657062015-04-28 13:45:11 -07001262 private boolean shouldListenForFingerprint() {
Kevin Chynf3b8fbd2017-05-03 22:24:31 -07001263 return (mKeyguardIsVisible || !mDeviceInteractive ||
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001264 (mBouncer && !mKeyguardGoingAway) || mGoingToSleep ||
Kevin Chyn36778ff2017-09-07 19:55:38 -07001265 shouldListenForFingerprintAssistant() || (mKeyguardOccluded && mIsDreaming))
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001266 && !mSwitchingUser && !isFingerprintDisabled(getCurrentUser())
1267 && !mKeyguardGoingAway;
Jim Miller9f0753f2015-03-23 23:59:22 -07001268 }
1269
Jim Millerce7eb6d2015-04-03 19:29:13 -07001270 private void startListeningForFingerprint() {
Jorim Jaggi86bed402015-08-20 18:20:02 -07001271 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING) {
1272 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING_RESTARTING);
1273 return;
1274 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001275 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001276 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001277 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001278 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001279 mFingerprintCancelSignal.cancel();
1280 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001281 mFingerprintCancelSignal = new CancellationSignal();
Kevin Chynaae4a152018-01-18 11:48:09 -08001282 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null,
1283 userId);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001284 setFingerprintRunningState(FINGERPRINT_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001285 }
1286 }
1287
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001288 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001289 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1290 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1291 }
1292
Jorim Jaggiea657062015-04-28 13:45:11 -07001293 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001294 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Jorim Jaggi86bed402015-08-20 18:20:02 -07001295 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING) {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001296 if (mFingerprintCancelSignal != null) {
1297 mFingerprintCancelSignal.cancel();
1298 mFingerprintCancelSignal = null;
1299 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001300 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001301 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001302 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
1303 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
1304 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001305 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001306
Michael Jurkafff56142012-11-28 16:51:00 -08001307 private boolean isDeviceProvisionedInSettingsDb() {
1308 return Settings.Global.getInt(mContext.getContentResolver(),
1309 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1310 }
1311
Jim Millerbbf1a742012-07-17 18:30:30 -07001312 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001313 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001314 @Override
1315 public void onChange(boolean selfChange) {
1316 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001317 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001318 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001319 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001320 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001321 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001322 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001323 };
1324
1325 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001326 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001327 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001328
1329 // prevent a race condition between where we check the flag and where we register the
1330 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001331 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001332 if (provisioned != mDeviceProvisioned) {
1333 mDeviceProvisioned = provisioned;
1334 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001335 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001336 }
1337 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001338 }
1339
Jim Millerbbf1a742012-07-17 18:30:30 -07001340 /**
Jorim Jaggid11d1a92016-08-16 16:02:32 -07001341 * Update the state whether Keyguard currently has a lockscreen wallpaper.
1342 *
1343 * @param hasLockscreenWallpaper Whether Keyguard has a lockscreen wallpaper.
1344 */
1345 public void setHasLockscreenWallpaper(boolean hasLockscreenWallpaper) {
1346 if (hasLockscreenWallpaper != mHasLockscreenWallpaper) {
1347 mHasLockscreenWallpaper = hasLockscreenWallpaper;
1348 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1349 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1350 if (cb != null) {
1351 cb.onHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
1352 }
1353 }
1354 }
1355 }
1356
1357 /**
1358 * @return Whether Keyguard has a lockscreen wallpaper.
1359 */
1360 public boolean hasLockscreenWallpaper() {
1361 return mHasLockscreenWallpaper;
1362 }
1363
1364 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001365 * Handle {@link #MSG_DPM_STATE_CHANGED}
1366 */
Jim Millerb0304762012-03-13 20:01:25 -07001367 protected void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001368 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001369 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1370 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1371 if (cb != null) {
1372 cb.onDevicePolicyManagerStateChanged();
1373 }
Jim Millerb0304762012-03-13 20:01:25 -07001374 }
1375 }
1376
Jim Millerbbf1a742012-07-17 18:30:30 -07001377 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001378 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001379 */
Chris Wrenf41c61b2012-11-29 15:19:54 -05001380 protected void handleUserSwitching(int userId, IRemoteCallback reply) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001381 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001382 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1383 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001384 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001385 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001386 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001387 try {
1388 reply.sendResult(null);
1389 } catch (RemoteException e) {
1390 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001391 }
1392
Jim Millerbbf1a742012-07-17 18:30:30 -07001393 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001394 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1395 */
1396 protected void handleUserSwitchComplete(int userId) {
1397 for (int i = 0; i < mCallbacks.size(); i++) {
1398 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1399 if (cb != null) {
1400 cb.onUserSwitchComplete(userId);
1401 }
1402 }
1403 }
1404
1405 /**
Jim Miller90873d52013-09-26 18:11:38 -07001406 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1407 * keyguard crashes sometime after boot, then it will never receive this
1408 * broadcast and hence not handle the event. This method is ultimately called by
1409 * PhoneWindowManager in this case.
1410 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001411 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001412 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001413 }
1414
1415 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001416 * Handle {@link #MSG_BOOT_COMPLETED}
1417 */
1418 protected void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001419 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001420 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001421 for (int i = 0; i < mCallbacks.size(); i++) {
1422 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1423 if (cb != null) {
1424 cb.onBootCompleted();
1425 }
1426 }
1427 }
1428
1429 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001430 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001431 * destroyed.
1432 */
1433 public boolean hasBootCompleted() {
1434 return mBootCompleted;
1435 }
1436
1437 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001438 * Handle {@link #MSG_DEVICE_PROVISIONED}
1439 */
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001440 protected void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001441 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001442 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1443 if (cb != null) {
1444 cb.onDeviceProvisioned();
1445 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001446 }
Michael Jurkafff56142012-11-28 16:51:00 -08001447 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001448 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001449 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1450 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001451 }
1452 }
1453
Jim Millerbbf1a742012-07-17 18:30:30 -07001454 /**
1455 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1456 */
Jim Millerc23024d2010-02-24 15:37:00 -08001457 protected void handlePhoneStateChanged(String newState) {
1458 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001459 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1460 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1461 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1462 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1463 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1464 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1465 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001466 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001467 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1468 if (cb != null) {
1469 cb.onPhoneStateChanged(mPhoneState);
1470 }
Jim Millerc23024d2010-02-24 15:37:00 -08001471 }
1472 }
1473
Jim Millerbbf1a742012-07-17 18:30:30 -07001474 /**
1475 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1476 */
Jim Miller47088bb2009-11-24 00:40:16 -08001477 protected void handleRingerModeChange(int mode) {
1478 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001479 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001480 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001481 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1482 if (cb != null) {
1483 cb.onRingerModeChanged(mode);
1484 }
Jim Miller47088bb2009-11-24 00:40:16 -08001485 }
1486 }
1487
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001488 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001489 * Handle {@link #MSG_TIME_UPDATE}
1490 */
1491 private void handleTimeUpdate() {
1492 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001493 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001494 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1495 if (cb != null) {
1496 cb.onTimeChanged();
1497 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001498 }
1499 }
1500
1501 /**
1502 * Handle {@link #MSG_BATTERY_UPDATE}
1503 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001504 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001505 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001506 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1507 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001508 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001509 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001510 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1511 if (cb != null) {
1512 cb.onRefreshBatteryInfo(status);
1513 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001514 }
1515 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001516 }
1517
1518 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001519 * Handle {@link #MSG_SIM_STATE_CHANGE}
1520 */
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001521 @VisibleForTesting
1522 protected void handleSimStateChange(int subId, int slotId, State state) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001523
Jim Miller52a61332014-11-12 19:29:51 -08001524 if (DEBUG_SIM_STATES) {
1525 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1526 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001527 }
1528
Wink Savillea54bf652014-12-11 13:37:50 -08001529 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001530 Log.w(TAG, "invalid subId in handleSimStateChange()");
1531 return;
1532 }
1533
1534 SimData data = mSimDatas.get(subId);
1535 final boolean changed;
1536 if (data == null) {
1537 data = new SimData(state, slotId, subId);
1538 mSimDatas.put(subId, data);
1539 changed = true; // no data yet; force update
1540 } else {
1541 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1542 data.simState = state;
1543 data.subId = subId;
1544 data.slotId = slotId;
1545 }
1546 if (changed && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001547 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001548 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1549 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08001550 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001551 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001552 }
1553 }
1554 }
1555
Jim Millerbbf1a742012-07-17 18:30:30 -07001556 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001557 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1558 */
1559 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1560 if (DEBUG) {
1561 Log.d(TAG,
1562 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1563 }
1564
1565 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1566 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1567 return;
1568 }
1569
1570 mServiceStates.put(subId, serviceState);
1571
1572 for (int j = 0; j < mCallbacks.size(); j++) {
1573 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1574 if (cb != null) {
1575 cb.onRefreshCarrierInfo();
1576 }
1577 }
1578 }
1579
1580 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001581 * Notifies that the visibility state of Keyguard has changed.
1582 *
1583 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001584 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001585 public void onKeyguardVisibilityChanged(boolean showing) {
1586 if (DEBUG) Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
1587 mKeyguardIsVisible = showing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001588 for (int i = 0; i < mCallbacks.size(); i++) {
1589 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1590 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001591 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001592 }
1593 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001594 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001595 }
1596
Brian Colonna7fce3802013-09-17 15:51:32 -04001597 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001598 * Handle {@link #MSG_KEYGUARD_RESET}
1599 */
1600 private void handleKeyguardReset() {
1601 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Adrian Roosf6d51ac2015-09-02 13:26:25 -07001602 updateFingerprintListeningState();
Jorim Jaggi031f7952016-09-01 16:39:26 -07001603 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1604 }
1605
1606 private boolean resolveNeedsSlowUnlockTransition() {
1607 if (mUserManager.isUserUnlocked(getCurrentUser())) {
1608 return false;
1609 }
1610 Intent homeIntent = new Intent(Intent.ACTION_MAIN)
1611 .addCategory(Intent.CATEGORY_HOME);
1612 ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
1613 0 /* flags */);
1614 return FALLBACK_HOME_COMPONENT.equals(resolveInfo.getComponentInfo().getComponentName());
Selim Cinek1fcafc42015-07-20 14:39:25 -07001615 }
1616
1617 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001618 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1619 * @see #sendKeyguardBouncerChanged(boolean)
1620 */
1621 private void handleKeyguardBouncerChanged(int bouncer) {
1622 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1623 boolean isBouncer = (bouncer == 1);
1624 mBouncer = isBouncer;
1625 for (int i = 0; i < mCallbacks.size(); i++) {
1626 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1627 if (cb != null) {
1628 cb.onKeyguardBouncerChanged(isBouncer);
1629 }
1630 }
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001631 updateFingerprintListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02001632 }
1633
1634 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001635 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1636 */
1637 private void handleReportEmergencyCallAction() {
1638 for (int i = 0; i < mCallbacks.size(); i++) {
1639 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1640 if (cb != null) {
1641 cb.onEmergencyCallAction();
1642 }
1643 }
1644 }
1645
Lucas Dupin4272f442018-01-13 22:00:35 -08001646 private boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001647 final boolean nowPluggedIn = current.isPluggedIn();
1648 final boolean wasPluggedIn = old.isPluggedIn();
Lucas Dupin4272f442018-01-13 22:00:35 -08001649 final boolean stateChangedWhilePluggedIn = wasPluggedIn && nowPluggedIn
Jim Miller16464b82011-10-20 21:10:13 -07001650 && (old.status != current.status);
1651
1652 // change in plug state is always interesting
1653 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001654 return true;
1655 }
1656
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001657 // change in battery level
1658 if (old.level != current.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001659 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001660 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001661
1662 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08001663 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07001664 return true;
1665 }
1666
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001667 return false;
1668 }
1669
1670 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001671 * Remove the given observer's callback.
1672 *
Jim Miller6212cc02012-09-05 17:35:31 -07001673 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001674 */
Jim Miller6212cc02012-09-05 17:35:31 -07001675 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
1676 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1677 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1678 if (mCallbacks.get(i).get() == callback) {
1679 mCallbacks.remove(i);
1680 }
1681 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001682 }
1683
1684 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001685 * Register to receive notifications about general keyguard information
1686 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001687 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001688 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001689 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Jim Miller6212cc02012-09-05 17:35:31 -07001690 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1691 // Prevent adding duplicate callbacks
1692 for (int i = 0; i < mCallbacks.size(); i++) {
1693 if (mCallbacks.get(i).get() == callback) {
1694 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1695 new Exception("Called by"));
1696 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001697 }
1698 }
Jim Miller6212cc02012-09-05 17:35:31 -07001699 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1700 removeCallback(null); // remove unused references
1701 sendUpdates(callback);
1702 }
1703
Evan Rosky18396452016-07-27 15:19:37 -07001704 public boolean isSwitchingUser() {
1705 return mSwitchingUser;
1706 }
1707
1708 public void setSwitchingUser(boolean switching) {
1709 mSwitchingUser = switching;
Selim Cinekbbe19242017-12-08 15:42:08 -08001710 // Since this comes in on a binder thread, we need to post if first
1711 mHandler.post(mUpdateFingerprintListeningState);
Evan Rosky18396452016-07-27 15:19:37 -07001712 }
1713
Jim Miller6212cc02012-09-05 17:35:31 -07001714 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1715 // Notify listener of the current state
1716 callback.onRefreshBatteryInfo(mBatteryStatus);
1717 callback.onTimeChanged();
1718 callback.onRingerModeChanged(mRingMode);
1719 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001720 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001721 callback.onClockVisibilityChanged();
Lucas Dupin16cfe452018-02-08 13:14:50 -08001722 callback.onKeyguardVisibilityChangedRaw(mKeyguardIsVisible);
Jim Miller52a61332014-11-12 19:29:51 -08001723 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1724 final SimData state = data.getValue();
1725 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1726 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001727 }
1728
Selim Cinek1fcafc42015-07-20 14:39:25 -07001729 public void sendKeyguardReset() {
1730 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1731 }
1732
Adrian Roosb6011622014-05-14 15:52:53 +02001733 /**
1734 * @see #handleKeyguardBouncerChanged(int)
1735 */
1736 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1737 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1738 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1739 message.arg1 = showingBouncer ? 1 : 0;
1740 message.sendToTarget();
1741 }
1742
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001743 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001744 * 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 -08001745 * have the information earlier than waiting for the intent
1746 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001747 *
1748 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1749 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001750 */
Jim Miller52a61332014-11-12 19:29:51 -08001751 public void reportSimUnlocked(int subId) {
1752 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
Sanket Padawe7e460252017-03-10 16:18:20 -08001753 int slotId = SubscriptionManager.getSlotIndex(subId);
Jim Miller52a61332014-11-12 19:29:51 -08001754 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001755 }
1756
Brian Colonna7fce3802013-09-17 15:51:32 -04001757 /**
1758 * Report that the emergency call button has been pressed and the emergency dialer is
1759 * about to be displayed.
1760 *
1761 * @param bypassHandler runs immediately.
1762 *
1763 * NOTE: Must be called from UI thread if bypassHandler == true.
1764 */
1765 public void reportEmergencyCallAction(boolean bypassHandler) {
1766 if (!bypassHandler) {
1767 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1768 } else {
1769 handleReportEmergencyCallAction();
1770 }
1771 }
1772
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001773 /**
1774 * @return Whether the device is provisioned (whether they have gone through
1775 * the setup wizard)
1776 */
1777 public boolean isDeviceProvisioned() {
1778 return mDeviceProvisioned;
1779 }
1780
Jorim Jaggi9f743032015-05-04 15:22:40 -07001781 public void clearFailedUnlockAttempts() {
1782 mFailedAttempts.delete(sCurrentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001783 }
1784
Kensuke Matsui21d1bf12017-03-14 13:27:20 +09001785 public ServiceState getServiceState(int subId) {
1786 return mServiceStates.get(subId);
1787 }
1788
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001789 public int getFailedUnlockAttempts(int userId) {
1790 return mFailedAttempts.get(userId, 0);
Jorim Jaggi9f743032015-05-04 15:22:40 -07001791 }
1792
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001793 public void reportFailedStrongAuthUnlockAttempt(int userId) {
1794 mFailedAttempts.put(userId, getFailedUnlockAttempts(userId) + 1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001795 }
1796
Jim Millerf41fc962014-06-18 16:33:51 -07001797 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001798 mUserFingerprintAuthenticated.clear();
Kevin Chyn3fdbbf82017-05-06 15:11:53 -07001799 mTrustManager.clearAllFingerprints();
Jim Millerf41fc962014-06-18 16:33:51 -07001800 }
1801
Jim Miller52a61332014-11-12 19:29:51 -08001802 public boolean isSimPinVoiceSecure() {
1803 // TODO: only count SIMs that handle voice
1804 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001805 }
1806
1807 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001808 // True if any SIM is pin secure
1809 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1810 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1811 }
1812 return false;
1813 }
1814
Jason Monk9ff69bd2014-12-02 16:43:17 -05001815 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001816 if (mSimDatas.containsKey(subId)) {
1817 return mSimDatas.get(subId).simState;
1818 } else {
1819 return State.UNKNOWN;
1820 }
1821 }
1822
Winson Chungaa357452017-10-31 11:35:30 -07001823 private final SysUiTaskStackChangeListener
1824 mTaskStackListener = new SysUiTaskStackChangeListener() {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001825 @Override
1826 public void onTaskStackChangedBackground() {
1827 try {
1828 ActivityManager.StackInfo info = ActivityManager.getService().getStackInfo(
Wale Ogunwale68278562017-09-23 17:13:55 -07001829 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_ASSISTANT);
Kevin Chyn2fefd462017-04-28 12:18:19 -07001830 if (info == null) {
1831 return;
1832 }
1833 mHandler.sendMessage(mHandler.obtainMessage(MSG_ASSISTANT_STACK_CHANGED,
1834 info.visible));
1835 } catch (RemoteException e) {
1836 Log.e(TAG, "unable to check task stack", e);
1837 }
1838 }
1839 };
1840
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001841 /**
richard choub1b24e72018-03-22 14:39:43 +08001842 * @return A list of changed subscriptions, maybe empty but never null
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001843 */
richard choub1b24e72018-03-22 14:39:43 +08001844 private List<Integer> refreshSimState(final List<SubscriptionInfo> activeSubscriptionInfos) {
Jim Miller52a61332014-11-12 19:29:51 -08001845
1846 // This is awful. It exists because there are two APIs for getting the SIM status
1847 // that don't return the complete set of values and have different types. In Keyguard we
1848 // need IccCardConstants, but TelephonyManager would only give us
1849 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001850 final TelephonyManager tele = TelephonyManager.from(mContext);
richard choub1b24e72018-03-22 14:39:43 +08001851 ArrayList<Integer> changedSubscriptionIds = new ArrayList<>();
1852 HashSet<Integer> activeSubIds = new HashSet<>();
richard chou3e7c7ca2018-04-12 14:25:16 +08001853 HashSet<Integer> activeSlotIds = new HashSet<>();
richard choub1b24e72018-03-22 14:39:43 +08001854
1855 for (SubscriptionInfo info : activeSubscriptionInfos) {
1856 int subId = info.getSubscriptionId();
1857 int slotId = info.getSimSlotIndex();
1858 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;
1865 }
1866
1867 SimData data = mSimDatas.get(subId);
1868 final boolean changed;
1869 if (data == null) {
1870 data = new SimData(state, slotId, subId);
1871 mSimDatas.put(subId, data);
1872 changed = true; // no data yet; force update
1873 } else {
1874 changed = data.simState != state;
1875 data.simState = state;
1876 }
1877 if (changed) {
1878 changedSubscriptionIds.add(subId);
1879 }
1880
1881 activeSubIds.add(subId);
richard chou3e7c7ca2018-04-12 14:25:16 +08001882 activeSlotIds.add(slotId);
John Spurlock5b13e922015-01-07 11:04:58 -05001883 }
richard choub1b24e72018-03-22 14:39:43 +08001884
1885 for (SimData data : mSimDatas.values()) {
richard chou3e7c7ca2018-04-12 14:25:16 +08001886 if (!activeSubIds.contains(data.subId)
1887 && !activeSlotIds.contains(data.slotId)
1888 && data.simState != State.ABSENT) {
richard choub1b24e72018-03-22 14:39:43 +08001889 // for the inactive subscriptions, reset state to ABSENT
richard chou3e7c7ca2018-04-12 14:25:16 +08001890 if (DEBUG_SIM_STATES) Log.d(TAG, "reset state to ABSENT for subId:" + data.subId);
richard choub1b24e72018-03-22 14:39:43 +08001891 data.simState = State.ABSENT;
1892 changedSubscriptionIds.add(data.subId);
1893 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001894 }
richard choub1b24e72018-03-22 14:39:43 +08001895
1896 return changedSubscriptionIds;
Jim Millerdcb3d842012-08-23 19:18:12 -07001897 }
1898
1899 public static boolean isSimPinSecure(IccCardConstants.State state) {
1900 final IccCardConstants.State simState = state;
1901 return (simState == IccCardConstants.State.PIN_REQUIRED
1902 || simState == IccCardConstants.State.PUK_REQUIRED
1903 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001904 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001905
1906 public DisplayClientState getCachedDisplayClientState() {
1907 return mDisplayClientState;
1908 }
Jim Miller20daffd2013-10-07 14:59:53 -07001909
1910 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1911 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001912 public void dispatchStartedWakingUp() {
1913 synchronized (this) {
1914 mDeviceInteractive = true;
1915 }
1916 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1917 }
1918
Jorim Jaggi95e40382015-09-16 15:53:42 -07001919 public void dispatchStartedGoingToSleep(int why) {
1920 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
1921 }
1922
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001923 public void dispatchFinishedGoingToSleep(int why) {
1924 synchronized(this) {
1925 mDeviceInteractive = false;
1926 }
1927 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1928 }
1929
Jim Miller20daffd2013-10-07 14:59:53 -07001930 public void dispatchScreenTurnedOn() {
1931 synchronized (this) {
1932 mScreenOn = true;
1933 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001934 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001935 }
1936
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001937 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001938 synchronized(this) {
1939 mScreenOn = false;
1940 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001941 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001942 }
1943
Selim Cinek99415392016-09-09 14:58:41 -07001944 public void dispatchDreamingStarted() {
1945 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 1, 0));
1946 }
1947
1948 public void dispatchDreamingStopped() {
1949 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 0, 0));
1950 }
1951
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001952 public boolean isDeviceInteractive() {
1953 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001954 }
Jim Miller52a61332014-11-12 19:29:51 -08001955
Jorim Jaggi95e40382015-09-16 15:53:42 -07001956 public boolean isGoingToSleep() {
1957 return mGoingToSleep;
1958 }
1959
Jim Miller52a61332014-11-12 19:29:51 -08001960 /**
1961 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1962 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001963 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001964 */
1965 public int getNextSubIdForState(State state) {
1966 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001967 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001968 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1969 for (int i = 0; i < list.size(); i++) {
1970 final SubscriptionInfo info = list.get(i);
1971 final int id = info.getSubscriptionId();
Sanket Padawe7e460252017-03-10 16:18:20 -08001972 int slotId = SubscriptionManager.getSlotIndex(id);
Jim Miller52a61332014-11-12 19:29:51 -08001973 if (state == getSimState(id) && bestSlotId > slotId ) {
1974 resultId = id;
1975 bestSlotId = slotId;
1976 }
1977 }
1978 return resultId;
1979 }
1980
1981 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1982 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1983 for (int i = 0; i < list.size(); i++) {
1984 SubscriptionInfo info = list.get(i);
1985 if (subId == info.getSubscriptionId()) return info;
1986 }
1987 return null; // not found
1988 }
Jason Monkab525272015-07-13 17:02:49 -04001989
Alex Chauff7653d2018-02-01 17:18:08 +00001990 /**
1991 * @return a cached version of DevicePolicyManager.isLogoutEnabled()
1992 */
1993 public boolean isLogoutEnabled() {
1994 return mLogoutEnabled;
1995 }
1996
1997 private void updateLogoutEnabled() {
1998 boolean logoutEnabled = mDevicePolicyManager.isLogoutEnabled();
1999 if (mLogoutEnabled != logoutEnabled) {
2000 mLogoutEnabled = logoutEnabled;
2001 for (int i = 0; i < mCallbacks.size(); i++) {
2002 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
2003 if (cb != null) {
2004 cb.onLogoutEnabledChanged();
2005 }
2006 }
2007 }
2008 }
2009
Jason Monkab525272015-07-13 17:02:49 -04002010 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2011 pw.println("KeyguardUpdateMonitor state:");
2012 pw.println(" SIM States:");
2013 for (SimData data : mSimDatas.values()) {
2014 pw.println(" " + data.toString());
2015 }
2016 pw.println(" Subs:");
2017 if (mSubscriptionInfo != null) {
2018 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
2019 pw.println(" " + mSubscriptionInfo.get(i));
2020 }
2021 }
2022 pw.println(" Service states:");
2023 for (int subId : mServiceStates.keySet()) {
2024 pw.println(" " + subId + "=" + mServiceStates.get(subId));
2025 }
Jim Millerd72d5ac2015-09-29 18:55:32 -07002026 if (mFpm != null && mFpm.isHardwareDetected()) {
2027 final int userId = ActivityManager.getCurrentUser();
2028 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
2029 pw.println(" Fingerprint state (user=" + userId + ")");
2030 pw.println(" allowed=" + isUnlockingWithFingerprintAllowed());
2031 pw.println(" auth'd=" + mUserFingerprintAuthenticated.get(userId));
2032 pw.println(" authSinceBoot="
2033 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
2034 pw.println(" disabled(DPM)=" + isFingerprintDisabled(userId));
2035 pw.println(" possible=" + isUnlockWithFingerprintPossible(userId));
2036 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
Jim Millerd72d5ac2015-09-29 18:55:32 -07002037 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
2038 }
Jason Monkab525272015-07-13 17:02:49 -04002039 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002040}