blob: 2bb992c449b63740de1250d4c75f119c54a8ffbe [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
Jorim Jaggi86bed402015-08-20 18:20:02 -070074import com.android.internal.telephony.IccCardConstants;
75import com.android.internal.telephony.IccCardConstants.State;
76import com.android.internal.telephony.PhoneConstants;
77import com.android.internal.telephony.TelephonyIntents;
Adrian Roosb5e47222015-08-14 15:53:06 -070078import com.android.internal.widget.LockPatternUtils;
Kevin Chyn2fefd462017-04-28 12:18:19 -070079import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung2db35572017-10-09 15:08:30 -070080import com.android.systemui.recents.misc.TaskStackChangeListener;
Jorim Jaggi86bed402015-08-20 18:20:02 -070081
Kevin Chyn36778ff2017-09-07 19:55:38 -070082import com.google.android.collect.Lists;
83
Jason Monkab525272015-07-13 17:02:49 -040084import java.io.FileDescriptor;
85import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -070086import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080087import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -080088import java.util.HashMap;
89import java.util.List;
90import java.util.Map.Entry;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080091
92/**
93 * Watches for updates that may be interesting to the keyguard, and provides
94 * the up to date information as well as a registration for callbacks that care
95 * to be updated.
96 *
97 * Note: under time crunch, this has been extended to include some stuff that
98 * doesn't really belong here. see {@link #handleBatteryUpdate} where it shutdowns
Jim Miller258341c2012-08-30 16:50:10 -070099 * the device, and {@link #getFailedUnlockAttempts()}, {@link #reportFailedAttempt()}
100 * and {@link #clearFailedUnlockAttempts()}. Maybe we should rename this 'KeyguardContext'...
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800101 */
Adrian Roos46842d92014-03-27 14:58:03 +0100102public class KeyguardUpdateMonitor implements TrustManager.TrustListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800103
Jim Millerbbf1a742012-07-17 18:30:30 -0700104 private static final String TAG = "KeyguardUpdateMonitor";
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100105 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller52a61332014-11-12 19:29:51 -0800106 private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
Jim Millerbbf1a742012-07-17 18:30:30 -0700107 private static final int LOW_BATTERY_THRESHOLD = 20;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800108
Jorim Jaggie7b12522014-08-06 16:41:21 +0200109 private static final String ACTION_FACE_UNLOCK_STARTED
110 = "com.android.facelock.FACE_UNLOCK_STARTED";
111 private static final String ACTION_FACE_UNLOCK_STOPPED
112 = "com.android.facelock.FACE_UNLOCK_STOPPED";
113
Jim Millerbbf1a742012-07-17 18:30:30 -0700114 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800115 private static final int MSG_TIME_UPDATE = 301;
116 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800117 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800118 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800119 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700120 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700121 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500122 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700123 private static final int MSG_KEYGUARD_RESET = 312;
Jim Millerf41fc962014-06-18 16:33:51 -0700124 private static final int MSG_BOOT_COMPLETED = 313;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500125 private static final int MSG_USER_SWITCH_COMPLETE = 314;
Jim Millerf41fc962014-06-18 16:33:51 -0700126 private static final int MSG_USER_INFO_CHANGED = 317;
127 private static final int MSG_REPORT_EMERGENCY_CALL_ACTION = 318;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700128 private static final int MSG_STARTED_WAKING_UP = 319;
129 private static final int MSG_FINISHED_GOING_TO_SLEEP = 320;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700130 private static final int MSG_STARTED_GOING_TO_SLEEP = 321;
Adrian Roosb6011622014-05-14 15:52:53 +0200131 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700132 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
133 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400134 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700135 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700136 private static final int MSG_SCREEN_TURNED_ON = 331;
137 private static final int MSG_SCREEN_TURNED_OFF = 332;
Selim Cinek99415392016-09-09 14:58:41 -0700138 private static final int MSG_DREAMING_STATE_CHANGED = 333;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700139 private static final int MSG_USER_UNLOCKED = 334;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700140 private static final int MSG_ASSISTANT_STACK_CHANGED = 335;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700141 private static final int MSG_FINGERPRINT_AUTHENTICATION_CONTINUE = 336;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800142
Jorim Jaggi86bed402015-08-20 18:20:02 -0700143 /** Fingerprint state: Not listening to fingerprint. */
144 private static final int FINGERPRINT_STATE_STOPPED = 0;
145
146 /** Fingerprint state: Listening. */
147 private static final int FINGERPRINT_STATE_RUNNING = 1;
148
149 /**
150 * Fingerprint state: Cancelling and waiting for the confirmation from FingerprintService to
151 * send us the confirmation that cancellation has happened.
152 */
153 private static final int FINGERPRINT_STATE_CANCELLING = 2;
154
155 /**
156 * Fingerprint state: During cancelling we got another request to start listening, so when we
157 * receive the cancellation done signal, we should start listening again.
158 */
159 private static final int FINGERPRINT_STATE_CANCELLING_RESTARTING = 3;
160
Adrian Roos0c859ae2015-11-23 16:47:50 -0800161 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
162
Jorim Jaggi031f7952016-09-01 16:39:26 -0700163 private static final ComponentName FALLBACK_HOME_COMPONENT = new ComponentName(
164 "com.android.settings", "com.android.settings.FallbackHome");
165
Adrian Roosca8a2162017-08-17 19:00:58 +0200166
167 /**
168 * If true, the system is in the half-boot-to-decryption-screen state.
169 * Prudently disable lockscreen.
170 */
171 public static final boolean CORE_APPS_ONLY;
172 static {
173 try {
174 CORE_APPS_ONLY = IPackageManager.Stub.asInterface(
175 ServiceManager.getService("package")).isOnlyCoreApps();
176 } catch (RemoteException e) {
177 throw e.rethrowFromSystemServer();
178 }
179 }
180
Jim Millerdcb3d842012-08-23 19:18:12 -0700181 private static KeyguardUpdateMonitor sInstance;
182
Jim Millerbbf1a742012-07-17 18:30:30 -0700183 private final Context mContext;
Jim Miller52a61332014-11-12 19:29:51 -0800184 HashMap<Integer, SimData> mSimDatas = new HashMap<Integer, SimData>();
Etan Cohen47051d82015-07-06 16:19:04 -0700185 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700186
Jim Millerbbf1a742012-07-17 18:30:30 -0700187 private int mRingMode;
188 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400189 private boolean mKeyguardIsVisible;
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700190 private boolean mKeyguardGoingAway;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700191 private boolean mGoingToSleep;
Adrian Roosb6011622014-05-14 15:52:53 +0200192 private boolean mBouncer;
Adam Cohen4eb36cf2012-11-07 11:45:30 -0800193 private boolean mBootCompleted;
Jorim Jaggi031f7952016-09-01 16:39:26 -0700194 private boolean mNeedsSlowUnlockTransition;
Jorim Jaggid11d1a92016-08-16 16:02:32 -0700195 private boolean mHasLockscreenWallpaper;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700196 private boolean mAssistantVisible;
197 private boolean mKeyguardOccluded;
Jim Millerbbf1a742012-07-17 18:30:30 -0700198
Jim Millerdcb3d842012-08-23 19:18:12 -0700199 // Device provisioning state
Jim Millerbbf1a742012-07-17 18:30:30 -0700200 private boolean mDeviceProvisioned;
201
Jim Millerdcb3d842012-08-23 19:18:12 -0700202 // Battery status
Jim Millerbbf1a742012-07-17 18:30:30 -0700203 private BatteryStatus mBatteryStatus;
204
Jim Millerdcb3d842012-08-23 19:18:12 -0700205 // Password attempts
Jorim Jaggi9f743032015-05-04 15:22:40 -0700206 private SparseIntArray mFailedAttempts = new SparseIntArray();
Brian Colonnacc4104f2012-10-09 17:50:46 -0400207
Rakesh Iyera7aa4d62016-01-19 17:27:23 -0800208 private final StrongAuthTracker mStrongAuthTracker;
Jim Millerbbf1a742012-07-17 18:30:30 -0700209
Jim Miller6212cc02012-09-05 17:35:31 -0700210 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700211 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800212 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700213
Brian Colonnaa5239892013-04-15 11:45:40 -0400214 private boolean mSwitchingUser;
215
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700216 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700217 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800218 private SubscriptionManager mSubscriptionManager;
219 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700220 private TrustManager mTrustManager;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700221 private UserManager mUserManager;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700222 private int mFingerprintRunningState = FINGERPRINT_STATE_STOPPED;
Michal Karpinskic52f8672016-11-18 11:32:45 +0000223 private LockPatternUtils mLockPatternUtils;
Kevin Chyn36778ff2017-09-07 19:55:38 -0700224 private final IDreamManager mDreamManager;
225 private boolean mIsDreaming;
Jim Miller20daffd2013-10-07 14:59:53 -0700226
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700227 /**
228 * Short delay before restarting fingerprint authentication after a successful try
229 * This should be slightly longer than the time between onFingerprintAuthenticated and
230 * setKeyguardGoingAway(true).
231 */
232 private static final int FINGERPRINT_CONTINUE_DELAY_MS = 500;
233
Kevin Chyn0c45b072017-04-24 16:27:11 -0700234 // If FP daemon dies, keyguard should retry after a short delay
235 private int mHardwareUnavailableRetryCount = 0;
236 private static final int HW_UNAVAILABLE_TIMEOUT = 3000; // ms
237 private static final int HW_UNAVAILABLE_RETRY_MAX = 3;
238
Jim Millerbbf1a742012-07-17 18:30:30 -0700239 private final Handler mHandler = new Handler() {
240 @Override
241 public void handleMessage(Message msg) {
242 switch (msg.what) {
243 case MSG_TIME_UPDATE:
244 handleTimeUpdate();
245 break;
246 case MSG_BATTERY_UPDATE:
247 handleBatteryUpdate((BatteryStatus) msg.obj);
248 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700249 case MSG_SIM_STATE_CHANGE:
Jim Miller52a61332014-11-12 19:29:51 -0800250 handleSimStateChange(msg.arg1, msg.arg2, (State) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700251 break;
252 case MSG_RINGER_MODE_CHANGED:
253 handleRingerModeChange(msg.arg1);
254 break;
255 case MSG_PHONE_STATE_CHANGED:
Adrian Roosb6011622014-05-14 15:52:53 +0200256 handlePhoneStateChanged((String) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700257 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700258 case MSG_DEVICE_PROVISIONED:
259 handleDeviceProvisioned();
260 break;
261 case MSG_DPM_STATE_CHANGED:
262 handleDevicePolicyManagerStateChanged();
263 break;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500264 case MSG_USER_SWITCHING:
Adrian Roosb6011622014-05-14 15:52:53 +0200265 handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500266 break;
267 case MSG_USER_SWITCH_COMPLETE:
268 handleUserSwitchComplete(msg.arg1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700269 break;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700270 case MSG_KEYGUARD_RESET:
271 handleKeyguardReset();
272 break;
Adrian Roosb6011622014-05-14 15:52:53 +0200273 case MSG_KEYGUARD_BOUNCER_CHANGED:
274 handleKeyguardBouncerChanged(msg.arg1);
275 break;
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800276 case MSG_BOOT_COMPLETED:
277 handleBootCompleted();
278 break;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700279 case MSG_USER_INFO_CHANGED:
280 handleUserInfoChanged(msg.arg1);
281 break;
Brian Colonna7fce3802013-09-17 15:51:32 -0400282 case MSG_REPORT_EMERGENCY_CALL_ACTION:
283 handleReportEmergencyCallAction();
284 break;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700285 case MSG_STARTED_GOING_TO_SLEEP:
286 handleStartedGoingToSleep(msg.arg1);
287 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700288 case MSG_FINISHED_GOING_TO_SLEEP:
289 handleFinishedGoingToSleep(msg.arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700290 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700291 case MSG_STARTED_WAKING_UP:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700292 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_STARTED_WAKING_UP");
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700293 handleStartedWakingUp();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700294 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -0700295 break;
Jorim Jaggie7b12522014-08-06 16:41:21 +0200296 case MSG_FACE_UNLOCK_STATE_CHANGED:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700297 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_FACE_UNLOCK_STATE_CHANGED");
Adrian Roos4a410172014-08-20 17:41:44 +0200298 handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700299 Trace.endSection();
Jorim Jaggie7b12522014-08-06 16:41:21 +0200300 break;
Jim Miller52a61332014-11-12 19:29:51 -0800301 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
302 handleSimSubscriptionInfoChanged();
303 break;
Jason Monk052082c2015-06-11 11:35:23 -0400304 case MSG_AIRPLANE_MODE_CHANGED:
305 handleAirplaneModeChanged();
306 break;
Etan Cohen47051d82015-07-06 16:19:04 -0700307 case MSG_SERVICE_STATE_CHANGE:
308 handleServiceStateChange(msg.arg1, (ServiceState) msg.obj);
309 break;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700310 case MSG_SCREEN_TURNED_ON:
311 handleScreenTurnedOn();
312 break;
313 case MSG_SCREEN_TURNED_OFF:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700314 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_SCREEN_TURNED_ON");
Jorim Jaggif1518da2015-07-30 11:56:36 -0700315 handleScreenTurnedOff();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700316 Trace.endSection();
Jorim Jaggif1518da2015-07-30 11:56:36 -0700317 break;
Selim Cinek99415392016-09-09 14:58:41 -0700318 case MSG_DREAMING_STATE_CHANGED:
319 handleDreamingStateChanged(msg.arg1);
320 break;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700321 case MSG_USER_UNLOCKED:
322 handleUserUnlocked();
323 break;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700324 case MSG_ASSISTANT_STACK_CHANGED:
325 mAssistantVisible = (boolean)msg.obj;
326 updateFingerprintListeningState();
327 break;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700328 case MSG_FINGERPRINT_AUTHENTICATION_CONTINUE:
329 updateFingerprintListeningState();
330 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700331 }
332 }
333 };
334
Wink Savilled09c4ca2014-11-22 10:08:16 -0800335 private OnSubscriptionsChangedListener mSubscriptionListener =
336 new OnSubscriptionsChangedListener() {
Jim Miller52a61332014-11-12 19:29:51 -0800337 @Override
Wink Savilled09c4ca2014-11-22 10:08:16 -0800338 public void onSubscriptionsChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800339 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
340 }
341 };
342
Adrian Roos46842d92014-03-27 14:58:03 +0100343 private SparseBooleanArray mUserHasTrust = new SparseBooleanArray();
Adrian Roos7861c662014-07-25 15:37:28 +0200344 private SparseBooleanArray mUserTrustIsManaged = new SparseBooleanArray();
Jim Miller9f0753f2015-03-23 23:59:22 -0700345 private SparseBooleanArray mUserFingerprintAuthenticated = new SparseBooleanArray();
Adrian Roos4a410172014-08-20 17:41:44 +0200346 private SparseBooleanArray mUserFaceUnlockRunning = new SparseBooleanArray();
Adrian Roos46842d92014-03-27 14:58:03 +0100347
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700348 private static int sCurrentUser;
349
350 public synchronized static void setCurrentUser(int currentUser) {
351 sCurrentUser = currentUser;
352 }
353
354 public synchronized static int getCurrentUser() {
355 return sCurrentUser;
356 }
357
Adrian Roos46842d92014-03-27 14:58:03 +0100358 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700359 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos46842d92014-03-27 14:58:03 +0100360 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200361 for (int i = 0; i < mCallbacks.size(); i++) {
362 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
363 if (cb != null) {
364 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700365 if (enabled && flags != 0) {
366 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200367 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200368 }
369 }
Adrian Roos46842d92014-03-27 14:58:03 +0100370 }
371
Jim Miller52a61332014-11-12 19:29:51 -0800372 protected void handleSimSubscriptionInfoChanged() {
373 if (DEBUG_SIM_STATES) {
374 Log.v(TAG, "onSubscriptionInfoChanged()");
Wink Savilled09c4ca2014-11-22 10:08:16 -0800375 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
376 if (sil != null) {
377 for (SubscriptionInfo subInfo : sil) {
378 Log.v(TAG, "SubInfo:" + subInfo);
379 }
380 } else {
381 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800382 }
383 }
384 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
385
386 // Hack level over 9000: Because the subscription id is not yet valid when we see the
387 // first update in handleSimStateChange, we need to force refresh all all SIM states
388 // so the subscription id for them is consistent.
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100389 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
Jim Miller52a61332014-11-12 19:29:51 -0800390 for (int i = 0; i < subscriptionInfos.size(); i++) {
391 SubscriptionInfo info = subscriptionInfos.get(i);
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100392 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
393 if (changed) {
394 changedSubscriptions.add(info);
395 }
Jim Miller52a61332014-11-12 19:29:51 -0800396 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100397 for (int i = 0; i < changedSubscriptions.size(); i++) {
398 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800399 for (int j = 0; j < mCallbacks.size(); j++) {
400 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
401 if (cb != null) {
402 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
403 }
404 }
405 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500406 for (int j = 0; j < mCallbacks.size(); j++) {
407 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
408 if (cb != null) {
409 cb.onRefreshCarrierInfo();
410 }
411 }
Jim Miller52a61332014-11-12 19:29:51 -0800412 }
413
Jason Monk052082c2015-06-11 11:35:23 -0400414 private void handleAirplaneModeChanged() {
415 for (int j = 0; j < mCallbacks.size(); j++) {
416 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
417 if (cb != null) {
418 cb.onRefreshCarrierInfo();
419 }
420 }
421 }
422
Wink Savilled09c4ca2014-11-22 10:08:16 -0800423 /** @return List of SubscriptionInfo records, maybe empty but never null */
Adrian Roos316bf542016-08-23 17:53:07 +0200424 public List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800425 List<SubscriptionInfo> sil = mSubscriptionInfo;
426 if (sil == null || forceReload) {
427 sil = mSubscriptionManager.getActiveSubscriptionInfoList();
428 }
429 if (sil == null) {
430 // getActiveSubscriptionInfoList was null callers expect an empty list.
431 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
432 } else {
433 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800434 }
435 return mSubscriptionInfo;
436 }
437
Adrian Roos7861c662014-07-25 15:37:28 +0200438 @Override
439 public void onTrustManagedChanged(boolean managed, int userId) {
440 mUserTrustIsManaged.put(userId, managed);
441
442 for (int i = 0; i < mCallbacks.size(); i++) {
443 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
444 if (cb != null) {
445 cb.onTrustManagedChanged(userId);
446 }
447 }
448 }
449
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700450 /**
451 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is goingAway
452 * @param goingAway
453 */
454 public void setKeyguardGoingAway(boolean goingAway) {
455 mKeyguardGoingAway = goingAway;
Kevin Chyne22f1342017-09-26 10:03:38 -0700456 updateFingerprintListeningState();
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700457 }
458
Kevin Chyn2fefd462017-04-28 12:18:19 -0700459 /**
460 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is occluded
461 * @param occluded
462 */
463 public void setKeyguardOccluded(boolean occluded) {
464 mKeyguardOccluded = occluded;
465 updateFingerprintListeningState();
466 }
467
Kevin Chyn36778ff2017-09-07 19:55:38 -0700468 /**
469 * @return a cached version of DreamManager.isDreaming()
470 */
471 public boolean isDreaming() {
472 return mIsDreaming;
473 }
474
475 /**
476 * If the device is dreaming, awakens the device
477 */
478 public void awakenFromDream() {
479 if (mIsDreaming && mDreamManager != null) {
480 try {
481 mDreamManager.awaken();
482 } catch (RemoteException e) {
483 Log.e(TAG, "Unable to awaken from dream");
484 }
485 }
486 }
487
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700488 private void onFingerprintAuthenticated(int userId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700489 Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
Jim Miller9f0753f2015-03-23 23:59:22 -0700490 mUserFingerprintAuthenticated.put(userId, true);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700491 // Update/refresh trust state only if user can skip bouncer
492 if (getUserCanSkipBouncer(userId)) {
493 mTrustManager.unlockedByFingerprintForUser(userId);
494 }
Kevin Chyn625a0142017-04-10 14:53:59 -0700495 // Don't send cancel if authentication succeeds
496 mFingerprintCancelSignal = null;
Jim Millerf41fc962014-06-18 16:33:51 -0700497 for (int i = 0; i < mCallbacks.size(); i++) {
498 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
499 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700500 cb.onFingerprintAuthenticated(userId);
Jim Millerf41fc962014-06-18 16:33:51 -0700501 }
502 }
Kevin Chyn2fefd462017-04-28 12:18:19 -0700503
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700504 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE),
505 FINGERPRINT_CONTINUE_DELAY_MS);
506
Kevin Chyn2fefd462017-04-28 12:18:19 -0700507 // Only authenticate fingerprint once when assistant is visible
508 mAssistantVisible = false;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700509
Nick Desaulniers1d396752016-07-25 15:05:33 -0700510 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700511 }
512
Jim Millerce7eb6d2015-04-03 19:29:13 -0700513 private void handleFingerprintAuthFailed() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700514 for (int i = 0; i < mCallbacks.size(); i++) {
515 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
516 if (cb != null) {
517 cb.onFingerprintAuthFailed();
518 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700519 }
Jim Millerce7eb6d2015-04-03 19:29:13 -0700520 handleFingerprintHelp(-1, mContext.getString(R.string.fingerprint_not_recognized));
521 }
Jim Millerf41fc962014-06-18 16:33:51 -0700522
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700523 private void handleFingerprintAcquired(int acquireInfo) {
524 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
525 return;
526 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700527 for (int i = 0; i < mCallbacks.size(); i++) {
528 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
529 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700530 cb.onFingerprintAcquired();
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700531 }
532 }
533 }
534
Jim Miller837fa7e2016-08-08 20:16:22 -0700535 private void handleFingerprintAuthenticated(int authUserId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700536 Trace.beginSection("KeyGuardUpdateMonitor#handlerFingerPrintAuthenticated");
Jim Millerf41fc962014-06-18 16:33:51 -0700537 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700538 final int userId;
539 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800540 userId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700541 } catch (RemoteException e) {
542 Log.e(TAG, "Failed to get current user id: ", e);
543 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700544 }
Jim Miller837fa7e2016-08-08 20:16:22 -0700545 if (userId != authUserId) {
546 Log.d(TAG, "Fingerprint authenticated for wrong user: " + authUserId);
547 return;
548 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700549 if (isFingerprintDisabled(userId)) {
550 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
551 return;
552 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700553 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700554 } finally {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700555 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700556 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700557 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700558 }
559
Jim Miller9f0753f2015-03-23 23:59:22 -0700560 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700561 for (int i = 0; i < mCallbacks.size(); i++) {
562 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
563 if (cb != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700564 cb.onFingerprintHelp(msgId, helpString);
565 }
566 }
567 }
568
Kevin Chyn0c45b072017-04-24 16:27:11 -0700569 private Runnable mRetryFingerprintAuthentication = new Runnable() {
570 @Override
571 public void run() {
572 Log.w(TAG, "Retrying fingerprint after HW unavailable, attempt " +
573 mHardwareUnavailableRetryCount);
574 updateFingerprintListeningState();
575 }
576 };
577
Jim Miller9f0753f2015-03-23 23:59:22 -0700578 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700579 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
580 && mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
581 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
582 startListeningForFingerprint();
583 } else {
584 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
585 }
Kevin Chyn0c45b072017-04-24 16:27:11 -0700586
587 if (msgId == FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE) {
588 if (mHardwareUnavailableRetryCount < HW_UNAVAILABLE_RETRY_MAX) {
589 mHardwareUnavailableRetryCount++;
590 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
591 mHandler.postDelayed(mRetryFingerprintAuthentication, HW_UNAVAILABLE_TIMEOUT);
592 }
593 }
594
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700595 if (msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT) {
596 mLockPatternUtils.requireStrongAuth(
597 LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
598 getCurrentUser());
599 }
600
Jim Miller9f0753f2015-03-23 23:59:22 -0700601 for (int i = 0; i < mCallbacks.size(); i++) {
602 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
603 if (cb != null) {
604 cb.onFingerprintError(msgId, errString);
Jim Millerf41fc962014-06-18 16:33:51 -0700605 }
606 }
607 }
608
Jorim Jaggi3a464782015-08-28 16:59:13 -0700609 private void handleFingerprintLockoutReset() {
610 updateFingerprintListeningState();
611 }
612
Jorim Jaggi86bed402015-08-20 18:20:02 -0700613 private void setFingerprintRunningState(int fingerprintRunningState) {
614 boolean wasRunning = mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
615 boolean isRunning = fingerprintRunningState == FINGERPRINT_STATE_RUNNING;
616 mFingerprintRunningState = fingerprintRunningState;
617
618 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
619 // asynchronousness of the cancel cycle. So only notify them if the actualy running state
620 // has changed.
621 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700622 notifyFingerprintRunningStateChanged();
623 }
624 }
625
626 private void notifyFingerprintRunningStateChanged() {
627 for (int i = 0; i < mCallbacks.size(); i++) {
628 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
629 if (cb != null) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700630 cb.onFingerprintRunningStateChanged(isFingerprintDetectionRunning());
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700631 }
632 }
633 }
Adrian Roos4a410172014-08-20 17:41:44 +0200634 private void handleFaceUnlockStateChanged(boolean running, int userId) {
635 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200636 for (int i = 0; i < mCallbacks.size(); i++) {
637 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
638 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200639 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200640 }
641 }
642 }
643
Adrian Roos4a410172014-08-20 17:41:44 +0200644 public boolean isFaceUnlockRunning(int userId) {
645 return mUserFaceUnlockRunning.get(userId);
646 }
647
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700648 public boolean isFingerprintDetectionRunning() {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700649 return mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700650 }
651
Jim Miller50e62182014-04-23 17:25:00 -0700652 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100653 // Don't allow trust agent if device is secured with a SIM PIN. This is here
654 // mainly because there's no other way to prompt the user to enter their SIM PIN
655 // once they get past the keyguard screen.
656 final boolean disabledBySimPin = isSimPinSecure();
657 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700658 }
659
Jim Miller06e34502014-07-17 14:46:05 -0700660 private boolean isFingerprintDisabled(int userId) {
661 final DevicePolicyManager dpm =
662 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
663 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Adrian Roos733b6632015-08-21 14:32:35 -0700664 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
665 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700666 }
667
Selim Cineke8bae622015-07-15 13:24:06 -0700668 public boolean getUserCanSkipBouncer(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700669 return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
670 && isUnlockingWithFingerprintAllowed());
Selim Cineke8bae622015-07-15 13:24:06 -0700671 }
672
Adrian Roos46842d92014-03-27 14:58:03 +0100673 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700674 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100675 }
676
Adrian Roos7861c662014-07-25 15:37:28 +0200677 public boolean getUserTrustIsManaged(int userId) {
678 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
679 }
680
Selim Cinek1fcafc42015-07-20 14:39:25 -0700681 public boolean isUnlockingWithFingerprintAllowed() {
Michal Karpinskic52f8672016-11-18 11:32:45 +0000682 return mStrongAuthTracker.isUnlockingWithFingerprintAllowed();
Adrian Roosb5e47222015-08-14 15:53:06 -0700683 }
684
Jorim Jaggi031f7952016-09-01 16:39:26 -0700685 public boolean needsSlowUnlockTransition() {
686 return mNeedsSlowUnlockTransition;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700687 }
688
Adrian Roosb5e47222015-08-14 15:53:06 -0700689 public StrongAuthTracker getStrongAuthTracker() {
690 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700691 }
692
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700693 private void notifyStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700694 for (int i = 0; i < mCallbacks.size(); i++) {
695 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
696 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700697 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700698 }
699 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700700 }
701
Adrian Roos91ba3072017-02-14 16:50:46 +0100702 public boolean isScreenOn() {
703 return mScreenOn;
704 }
705
Jim Miller8f09fd22013-03-14 19:04:28 -0700706 static class DisplayClientState {
707 public int clientGeneration;
708 public boolean clearing;
709 public PendingIntent intent;
710 public int playbackState;
711 public long playbackEventTime;
712 }
713
714 private DisplayClientState mDisplayClientState = new DisplayClientState();
715
Jim Millerbbf1a742012-07-17 18:30:30 -0700716 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
717
Jim Millerd72d5ac2015-09-29 18:55:32 -0700718 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700719 public void onReceive(Context context, Intent intent) {
720 final String action = intent.getAction();
721 if (DEBUG) Log.d(TAG, "received broadcast " + action);
722
723 if (Intent.ACTION_TIME_TICK.equals(action)
724 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200725 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700726 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700727 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
728 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
729 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
730 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
731 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -0800732
733 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
734 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
735 final int maxChargingMicroWatt;
736
737 if (maxChargingMicroVolt <= 0) {
738 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
739 }
740 if (maxChargingMicroAmp > 0) {
741 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
742 // to maintain precision equally on both factors.
743 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
744 * (maxChargingMicroVolt / 1000);
745 } else {
746 maxChargingMicroWatt = -1;
747 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700748 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700749 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -0800750 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -0700751 mHandler.sendMessage(msg);
752 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Jim Miller52a61332014-11-12 19:29:51 -0800753 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700754 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800755 Log.v(TAG, "action " + action
756 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
757 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700758 }
Jim Miller52a61332014-11-12 19:29:51 -0800759 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
760 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700761 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
762 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
763 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
764 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
765 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
766 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400767 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
768 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800769 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700770 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700771 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
772 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
773 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
774 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
775 if (DEBUG) {
776 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
777 + subId);
778 }
779 mHandler.sendMessage(
780 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Jim Millerbbf1a742012-07-17 18:30:30 -0700781 }
782 }
783 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700784
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700785 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
786
Jim Millerd72d5ac2015-09-29 18:55:32 -0700787 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700788 public void onReceive(Context context, Intent intent) {
789 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200790 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
791 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
792 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700793 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
794 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200795 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700796 Trace.beginSection("KeyguardUpdateMonitor.mBroadcastAllReceiver#onReceive ACTION_FACE_UNLOCK_STARTED");
Adrian Roos48c796c2014-09-01 14:59:23 +0200797 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
798 getSendingUserId()));
Nick Desaulniers1d396752016-07-25 15:05:33 -0700799 Trace.endSection();
Adrian Roos48c796c2014-09-01 14:59:23 +0200800 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
801 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
802 getSendingUserId()));
803 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
804 .equals(action)) {
805 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -0700806 } else if (ACTION_USER_UNLOCKED.equals(action)) {
807 mHandler.sendEmptyMessage(MSG_USER_UNLOCKED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700808 }
809 }
810 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700811
Jorim Jaggi3a464782015-08-28 16:59:13 -0700812 private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
813 = new FingerprintManager.LockoutResetCallback() {
814 @Override
815 public void onLockoutReset() {
816 handleFingerprintLockoutReset();
817 }
818 };
819
Jim Miller9f0753f2015-03-23 23:59:22 -0700820 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
821 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700822
823 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700824 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700825 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700826 };
827
828 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700829 public void onAuthenticationSucceeded(AuthenticationResult result) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700830 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
Jim Miller837fa7e2016-08-08 20:16:22 -0700831 handleFingerprintAuthenticated(result.getUserId());
Nick Desaulniers1d396752016-07-25 15:05:33 -0700832 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700833 }
834
835 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700836 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700837 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700838 }
839
840 @Override
841 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700842 handleFingerprintError(errMsgId, errString.toString());
843 }
844
845 @Override
846 public void onAuthenticationAcquired(int acquireInfo) {
847 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700848 }
849 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700850 private CancellationSignal mFingerprintCancelSignal;
851 private FingerprintManager mFpm;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700852
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800853 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800854 * When we receive a
855 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700856 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800857 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800858 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800859 */
Jim Miller52a61332014-11-12 19:29:51 -0800860 private static class SimData {
861 public State simState;
862 public int slotId;
863 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800864
Jim Miller52a61332014-11-12 19:29:51 -0800865 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800866 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800867 slotId = slot;
868 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800869 }
870
Jim Miller52a61332014-11-12 19:29:51 -0800871 static SimData fromIntent(Intent intent) {
872 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800873 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
874 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
875 }
Wink Savillea639b312012-07-10 12:37:54 -0700876 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800877 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
878 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800879 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700880 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700881 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700882 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700883
Wink Savillea639b312012-07-10 12:37:54 -0700884 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700885 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700886 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700887 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700888 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700889 }
Wink Savillea639b312012-07-10 12:37:54 -0700890 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
891 state = IccCardConstants.State.READY;
892 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800893 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700894 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
895 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
896 state = IccCardConstants.State.PIN_REQUIRED;
897 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
898 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800899 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700900 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800901 }
Wink Savillea639b312012-07-10 12:37:54 -0700902 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
903 state = IccCardConstants.State.NETWORK_LOCKED;
Wileen Chiu6b8b22e2014-10-29 22:48:21 +0530904 } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
905 state = IccCardConstants.State.CARD_IO_ERROR;
Jim Miller109f1fd2012-09-19 20:44:16 -0700906 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
907 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
908 // This is required because telephony doesn't return to "READY" after
909 // these state transitions. See bug 7197471.
910 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800911 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700912 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800913 }
Jim Miller52a61332014-11-12 19:29:51 -0800914 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800915 }
916
Jim Millerd72d5ac2015-09-29 18:55:32 -0700917 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800918 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800919 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800920 }
921 }
922
Adrian Roos12c1ef52014-06-04 13:54:08 +0200923 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700924 public static final int CHARGING_UNKNOWN = -1;
925 public static final int CHARGING_SLOWLY = 0;
926 public static final int CHARGING_REGULAR = 1;
927 public static final int CHARGING_FAST = 2;
928
Jim Miller16464b82011-10-20 21:10:13 -0700929 public final int status;
930 public final int level;
931 public final int plugged;
932 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800933 public final int maxChargingWattage;
934 public BatteryStatus(int status, int level, int plugged, int health,
935 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -0700936 this.status = status;
937 this.level = level;
938 this.plugged = plugged;
939 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800940 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -0700941 }
942
Jim Millerbbf1a742012-07-17 18:30:30 -0700943 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700944 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700945 * @return true if the device is plugged in.
946 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100947 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700948 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700949 || plugged == BatteryManager.BATTERY_PLUGGED_USB
950 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -0700951 }
952
953 /**
954 * Whether or not the device is charged. Note that some devices never return 100% for
955 * battery level, so this allows either battery level or status to determine if the
956 * battery is charged.
957 * @return true if the device is charged
958 */
959 public boolean isCharged() {
960 return status == BATTERY_STATUS_FULL || level >= 100;
961 }
962
963 /**
964 * Whether battery is low and needs to be charged.
965 * @return true if battery is low
966 */
967 public boolean isBatteryLow() {
968 return level < LOW_BATTERY_THRESHOLD;
969 }
970
Adrian Roos7b043112015-07-10 13:00:33 -0700971 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -0800972 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
973 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
974 maxChargingWattage > fastThreshold ? CHARGING_FAST :
Adrian Roos7b043112015-07-10 13:00:33 -0700975 CHARGING_REGULAR;
976 }
Jim Miller16464b82011-10-20 21:10:13 -0700977 }
978
Adrian Roosb5e47222015-08-14 15:53:06 -0700979 public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
Rakesh Iyera7aa4d62016-01-19 17:27:23 -0800980 public StrongAuthTracker(Context context) {
981 super(context);
982 }
Adrian Roosb5e47222015-08-14 15:53:06 -0700983
984 public boolean isUnlockingWithFingerprintAllowed() {
985 int userId = getCurrentUser();
986 return isFingerprintAllowedForUser(userId);
987 }
988
989 public boolean hasUserAuthenticatedSinceBoot() {
990 int userId = getCurrentUser();
991 return (getStrongAuthForUser(userId)
992 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
993 }
994
995 @Override
996 public void onStrongAuthRequiredChanged(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700997 notifyStrongAuthStateChanged(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -0700998 }
999 }
1000
Jim Millerdcb3d842012-08-23 19:18:12 -07001001 public static KeyguardUpdateMonitor getInstance(Context context) {
1002 if (sInstance == null) {
1003 sInstance = new KeyguardUpdateMonitor(context);
1004 }
1005 return sInstance;
1006 }
1007
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001008 protected void handleStartedWakingUp() {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001009 Trace.beginSection("KeyguardUpdateMonitor#handleStartedWakingUp");
Jorim Jaggi864e64b2015-05-20 14:13:23 -07001010 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001011 final int count = mCallbacks.size();
1012 for (int i = 0; i < count; i++) {
1013 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1014 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001015 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -07001016 }
1017 }
Nick Desaulniers1d396752016-07-25 15:05:33 -07001018 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -07001019 }
1020
Jorim Jaggi95e40382015-09-16 15:53:42 -07001021 protected void handleStartedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -07001022 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -07001023 final int count = mCallbacks.size();
1024 for (int i = 0; i < count; i++) {
1025 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1026 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001027 cb.onStartedGoingToSleep(arg1);
1028 }
1029 }
1030 mGoingToSleep = true;
Jorim Jaggi95e40382015-09-16 15:53:42 -07001031 updateFingerprintListeningState();
1032 }
1033
1034 protected void handleFinishedGoingToSleep(int arg1) {
1035 mGoingToSleep = false;
1036 final int count = mCallbacks.size();
1037 for (int i = 0; i < count; i++) {
1038 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1039 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001040 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -07001041 }
1042 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001043 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001044 }
1045
Jorim Jaggif1518da2015-07-30 11:56:36 -07001046 private void handleScreenTurnedOn() {
1047 final int count = mCallbacks.size();
1048 for (int i = 0; i < count; i++) {
1049 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1050 if (cb != null) {
1051 cb.onScreenTurnedOn();
1052 }
1053 }
1054 }
1055
1056 private void handleScreenTurnedOff() {
Kevin Chyn0c45b072017-04-24 16:27:11 -07001057 mHardwareUnavailableRetryCount = 0;
Jorim Jaggif1518da2015-07-30 11:56:36 -07001058 final int count = mCallbacks.size();
1059 for (int i = 0; i < count; i++) {
1060 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1061 if (cb != null) {
1062 cb.onScreenTurnedOff();
1063 }
1064 }
1065 }
1066
Selim Cinek99415392016-09-09 14:58:41 -07001067 private void handleDreamingStateChanged(int dreamStart) {
1068 final int count = mCallbacks.size();
Kevin Chyn36778ff2017-09-07 19:55:38 -07001069 mIsDreaming = dreamStart == 1;
Selim Cinek99415392016-09-09 14:58:41 -07001070 for (int i = 0; i < count; i++) {
1071 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1072 if (cb != null) {
Kevin Chyn36778ff2017-09-07 19:55:38 -07001073 cb.onDreamingStateChanged(mIsDreaming);
Selim Cinek99415392016-09-09 14:58:41 -07001074 }
1075 }
Kevin Chyn20a68dc2017-09-21 11:41:01 -07001076 updateFingerprintListeningState();
Selim Cinek99415392016-09-09 14:58:41 -07001077 }
1078
Adam Powell43a372f2013-09-27 17:43:53 -07001079 /**
1080 * IMPORTANT: Must be called from UI thread.
1081 */
1082 public void dispatchSetBackground(Bitmap bmp) {
1083 if (DEBUG) Log.d(TAG, "dispatchSetBackground");
1084 final int count = mCallbacks.size();
1085 for (int i = 0; i < count; i++) {
1086 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1087 if (cb != null) {
1088 cb.onSetBackground(bmp);
1089 }
1090 }
1091 }
1092
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001093 private void handleUserInfoChanged(int userId) {
1094 for (int i = 0; i < mCallbacks.size(); i++) {
1095 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1096 if (cb != null) {
1097 cb.onUserInfoChanged(userId);
1098 }
1099 }
1100 }
1101
Jorim Jaggidadafd42016-09-30 07:20:25 -07001102 private void handleUserUnlocked() {
1103 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1104 for (int i = 0; i < mCallbacks.size(); i++) {
1105 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1106 if (cb != null) {
1107 cb.onUserUnlocked();
1108 }
1109 }
1110 }
1111
Jim Millerdcb3d842012-08-23 19:18:12 -07001112 private KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001113 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -08001114 mSubscriptionManager = SubscriptionManager.from(context);
Michael Jurkafff56142012-11-28 16:51:00 -08001115 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001116 mStrongAuthTracker = new StrongAuthTracker(context);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001117
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001118 // Since device can't be un-provisioned, we only need to register a content observer
1119 // to update mDeviceProvisioned when we are...
1120 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001121 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001122 }
Jim Miller47088bb2009-11-24 00:40:16 -08001123
Jim Millerbbf1a742012-07-17 18:30:30 -07001124 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -07001125 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001126
Jim Millerbbf1a742012-07-17 18:30:30 -07001127 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001128 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001129 filter.addAction(Intent.ACTION_TIME_TICK);
1130 filter.addAction(Intent.ACTION_TIME_CHANGED);
1131 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1132 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -04001133 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001134 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -07001135 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001136 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001137 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Jim Millerbbf1a742012-07-17 18:30:30 -07001138 context.registerReceiver(mBroadcastReceiver, filter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001139
Adam Cohenc276e822012-11-08 13:01:08 -08001140 final IntentFilter bootCompleteFilter = new IntentFilter();
1141 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1142 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
1143 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter);
1144
Adrian Roos48c796c2014-09-01 14:59:23 +02001145 final IntentFilter allUserFilter = new IntentFilter();
1146 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1147 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1148 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1149 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1150 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001151 allUserFilter.addAction(ACTION_USER_UNLOCKED);
Adrian Roos48c796c2014-09-01 14:59:23 +02001152 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001153 null, null);
1154
Wink Saville071743f2015-01-12 17:11:04 -08001155 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001156 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001157 ActivityManager.getService().registerUserSwitchObserver(
Sudheer Shanka2c4522c2016-08-27 20:53:28 -07001158 new UserSwitchObserver() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001159 @Override
1160 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001161 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001162 newUserId, 0, reply));
1163 }
1164 @Override
1165 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001166 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001167 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001168 }
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001169 }, TAG);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001170 } catch (RemoteException e) {
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001171 e.rethrowAsRuntimeException();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001172 }
Adrian Roos46842d92014-03-27 14:58:03 +01001173
Jorim Jaggi237b0612015-05-01 14:28:49 -07001174 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1175 mTrustManager.registerTrustListener(this);
Michal Karpinskic52f8672016-11-18 11:32:45 +00001176 mLockPatternUtils = new LockPatternUtils(context);
1177 mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001178
Kevin Chyn36778ff2017-09-07 19:55:38 -07001179 mDreamManager = IDreamManager.Stub.asInterface(
1180 ServiceManager.getService(DreamService.DREAM_SERVICE));
1181
Jorim Jaggi3f124262016-11-22 13:45:17 +01001182 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
1183 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
1184 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001185 updateFingerprintListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001186 if (mFpm != null) {
1187 mFpm.addLockoutResetCallback(mLockoutResetCallback);
1188 }
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001189
Kevin Chyn2fefd462017-04-28 12:18:19 -07001190 SystemServicesProxy.getInstance(mContext).registerTaskStackListener(mTaskStackListener);
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001191 mUserManager = context.getSystemService(UserManager.class);
Jorim Jaggiea657062015-04-28 13:45:11 -07001192 }
1193
1194 private void updateFingerprintListeningState() {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001195 // If this message exists, we should not authenticate again until this message is
1196 // consumed by the handler
1197 if (mHandler.hasMessages(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE)) {
1198 return;
1199 }
Kevin Chyn0c45b072017-04-24 16:27:11 -07001200 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Jorim Jaggiea657062015-04-28 13:45:11 -07001201 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001202 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001203 stopListeningForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001204 } else if (mFingerprintRunningState != FINGERPRINT_STATE_RUNNING
1205 && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001206 startListeningForFingerprint();
1207 }
1208 }
1209
Kevin Chyn129f60f2017-08-11 17:24:42 -07001210 private boolean shouldListenForFingerprintAssistant() {
1211 return mAssistantVisible && mKeyguardOccluded
1212 && !mUserFingerprintAuthenticated.get(getCurrentUser(), false)
1213 && !mUserHasTrust.get(getCurrentUser(), false);
1214 }
1215
Jorim Jaggiea657062015-04-28 13:45:11 -07001216 private boolean shouldListenForFingerprint() {
Kevin Chynf3b8fbd2017-05-03 22:24:31 -07001217 return (mKeyguardIsVisible || !mDeviceInteractive ||
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001218 (mBouncer && !mKeyguardGoingAway) || mGoingToSleep ||
Kevin Chyn36778ff2017-09-07 19:55:38 -07001219 shouldListenForFingerprintAssistant() || (mKeyguardOccluded && mIsDreaming))
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001220 && !mSwitchingUser && !isFingerprintDisabled(getCurrentUser())
1221 && !mKeyguardGoingAway;
Jim Miller9f0753f2015-03-23 23:59:22 -07001222 }
1223
Jim Millerce7eb6d2015-04-03 19:29:13 -07001224 private void startListeningForFingerprint() {
Jorim Jaggi86bed402015-08-20 18:20:02 -07001225 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING) {
1226 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING_RESTARTING);
1227 return;
1228 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001229 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001230 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001231 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001232 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001233 mFingerprintCancelSignal.cancel();
1234 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001235 mFingerprintCancelSignal = new CancellationSignal();
Jim Millerf501b582015-06-03 16:36:31 -07001236 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null, userId);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001237 setFingerprintRunningState(FINGERPRINT_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001238 }
1239 }
1240
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001241 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001242 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1243 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1244 }
1245
Jorim Jaggiea657062015-04-28 13:45:11 -07001246 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001247 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Jorim Jaggi86bed402015-08-20 18:20:02 -07001248 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING) {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001249 if (mFingerprintCancelSignal != null) {
1250 mFingerprintCancelSignal.cancel();
1251 mFingerprintCancelSignal = null;
1252 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001253 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001254 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001255 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
1256 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
1257 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001258 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001259
Michael Jurkafff56142012-11-28 16:51:00 -08001260 private boolean isDeviceProvisionedInSettingsDb() {
1261 return Settings.Global.getInt(mContext.getContentResolver(),
1262 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1263 }
1264
Jim Millerbbf1a742012-07-17 18:30:30 -07001265 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001266 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001267 @Override
1268 public void onChange(boolean selfChange) {
1269 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001270 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001271 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001272 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001273 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001274 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001275 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001276 };
1277
1278 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001279 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001280 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001281
1282 // prevent a race condition between where we check the flag and where we register the
1283 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001284 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001285 if (provisioned != mDeviceProvisioned) {
1286 mDeviceProvisioned = provisioned;
1287 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001288 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001289 }
1290 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001291 }
1292
Jim Millerbbf1a742012-07-17 18:30:30 -07001293 /**
Jorim Jaggid11d1a92016-08-16 16:02:32 -07001294 * Update the state whether Keyguard currently has a lockscreen wallpaper.
1295 *
1296 * @param hasLockscreenWallpaper Whether Keyguard has a lockscreen wallpaper.
1297 */
1298 public void setHasLockscreenWallpaper(boolean hasLockscreenWallpaper) {
1299 if (hasLockscreenWallpaper != mHasLockscreenWallpaper) {
1300 mHasLockscreenWallpaper = hasLockscreenWallpaper;
1301 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1302 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1303 if (cb != null) {
1304 cb.onHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
1305 }
1306 }
1307 }
1308 }
1309
1310 /**
1311 * @return Whether Keyguard has a lockscreen wallpaper.
1312 */
1313 public boolean hasLockscreenWallpaper() {
1314 return mHasLockscreenWallpaper;
1315 }
1316
1317 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001318 * Handle {@link #MSG_DPM_STATE_CHANGED}
1319 */
Jim Millerb0304762012-03-13 20:01:25 -07001320 protected void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001321 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001322 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1323 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1324 if (cb != null) {
1325 cb.onDevicePolicyManagerStateChanged();
1326 }
Jim Millerb0304762012-03-13 20:01:25 -07001327 }
1328 }
1329
Jim Millerbbf1a742012-07-17 18:30:30 -07001330 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001331 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001332 */
Chris Wrenf41c61b2012-11-29 15:19:54 -05001333 protected void handleUserSwitching(int userId, IRemoteCallback reply) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001334 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001335 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1336 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001337 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001338 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001339 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001340 try {
1341 reply.sendResult(null);
1342 } catch (RemoteException e) {
1343 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001344 }
1345
Jim Millerbbf1a742012-07-17 18:30:30 -07001346 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001347 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1348 */
1349 protected void handleUserSwitchComplete(int userId) {
1350 for (int i = 0; i < mCallbacks.size(); i++) {
1351 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1352 if (cb != null) {
1353 cb.onUserSwitchComplete(userId);
1354 }
1355 }
1356 }
1357
1358 /**
Jim Miller90873d52013-09-26 18:11:38 -07001359 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1360 * keyguard crashes sometime after boot, then it will never receive this
1361 * broadcast and hence not handle the event. This method is ultimately called by
1362 * PhoneWindowManager in this case.
1363 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001364 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001365 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001366 }
1367
1368 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001369 * Handle {@link #MSG_BOOT_COMPLETED}
1370 */
1371 protected void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001372 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001373 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001374 for (int i = 0; i < mCallbacks.size(); i++) {
1375 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1376 if (cb != null) {
1377 cb.onBootCompleted();
1378 }
1379 }
1380 }
1381
1382 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001383 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001384 * destroyed.
1385 */
1386 public boolean hasBootCompleted() {
1387 return mBootCompleted;
1388 }
1389
1390 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001391 * Handle {@link #MSG_DEVICE_PROVISIONED}
1392 */
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001393 protected void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001394 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001395 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1396 if (cb != null) {
1397 cb.onDeviceProvisioned();
1398 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001399 }
Michael Jurkafff56142012-11-28 16:51:00 -08001400 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001401 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001402 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1403 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001404 }
1405 }
1406
Jim Millerbbf1a742012-07-17 18:30:30 -07001407 /**
1408 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1409 */
Jim Millerc23024d2010-02-24 15:37:00 -08001410 protected void handlePhoneStateChanged(String newState) {
1411 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001412 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1413 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1414 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1415 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1416 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1417 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1418 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001419 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001420 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1421 if (cb != null) {
1422 cb.onPhoneStateChanged(mPhoneState);
1423 }
Jim Millerc23024d2010-02-24 15:37:00 -08001424 }
1425 }
1426
Jim Millerbbf1a742012-07-17 18:30:30 -07001427 /**
1428 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1429 */
Jim Miller47088bb2009-11-24 00:40:16 -08001430 protected void handleRingerModeChange(int mode) {
1431 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001432 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001433 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001434 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1435 if (cb != null) {
1436 cb.onRingerModeChanged(mode);
1437 }
Jim Miller47088bb2009-11-24 00:40:16 -08001438 }
1439 }
1440
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001441 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001442 * Handle {@link #MSG_TIME_UPDATE}
1443 */
1444 private void handleTimeUpdate() {
1445 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001446 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001447 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1448 if (cb != null) {
1449 cb.onTimeChanged();
1450 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001451 }
1452 }
1453
1454 /**
1455 * Handle {@link #MSG_BATTERY_UPDATE}
1456 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001457 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001458 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001459 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1460 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001461 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001462 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001463 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1464 if (cb != null) {
1465 cb.onRefreshBatteryInfo(status);
1466 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001467 }
1468 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001469 }
1470
1471 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001472 * Handle {@link #MSG_SIM_STATE_CHANGE}
1473 */
Jim Miller52a61332014-11-12 19:29:51 -08001474 private void handleSimStateChange(int subId, int slotId, State state) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001475
Jim Miller52a61332014-11-12 19:29:51 -08001476 if (DEBUG_SIM_STATES) {
1477 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1478 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001479 }
1480
Wink Savillea54bf652014-12-11 13:37:50 -08001481 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001482 Log.w(TAG, "invalid subId in handleSimStateChange()");
1483 return;
1484 }
1485
1486 SimData data = mSimDatas.get(subId);
1487 final boolean changed;
1488 if (data == null) {
1489 data = new SimData(state, slotId, subId);
1490 mSimDatas.put(subId, data);
1491 changed = true; // no data yet; force update
1492 } else {
1493 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1494 data.simState = state;
1495 data.subId = subId;
1496 data.slotId = slotId;
1497 }
1498 if (changed && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001499 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001500 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1501 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08001502 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001503 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001504 }
1505 }
1506 }
1507
Jim Millerbbf1a742012-07-17 18:30:30 -07001508 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001509 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1510 */
1511 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1512 if (DEBUG) {
1513 Log.d(TAG,
1514 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1515 }
1516
1517 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1518 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1519 return;
1520 }
1521
1522 mServiceStates.put(subId, serviceState);
1523
1524 for (int j = 0; j < mCallbacks.size(); j++) {
1525 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1526 if (cb != null) {
1527 cb.onRefreshCarrierInfo();
1528 }
1529 }
1530 }
1531
1532 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001533 * Notifies that the visibility state of Keyguard has changed.
1534 *
1535 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001536 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001537 public void onKeyguardVisibilityChanged(boolean showing) {
1538 if (DEBUG) Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
1539 mKeyguardIsVisible = showing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001540 for (int i = 0; i < mCallbacks.size(); i++) {
1541 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1542 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001543 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001544 }
1545 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001546 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001547 }
1548
Brian Colonna7fce3802013-09-17 15:51:32 -04001549 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001550 * Handle {@link #MSG_KEYGUARD_RESET}
1551 */
1552 private void handleKeyguardReset() {
1553 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Adrian Roosf6d51ac2015-09-02 13:26:25 -07001554 updateFingerprintListeningState();
Jorim Jaggi031f7952016-09-01 16:39:26 -07001555 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1556 }
1557
1558 private boolean resolveNeedsSlowUnlockTransition() {
1559 if (mUserManager.isUserUnlocked(getCurrentUser())) {
1560 return false;
1561 }
1562 Intent homeIntent = new Intent(Intent.ACTION_MAIN)
1563 .addCategory(Intent.CATEGORY_HOME);
1564 ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
1565 0 /* flags */);
1566 return FALLBACK_HOME_COMPONENT.equals(resolveInfo.getComponentInfo().getComponentName());
Selim Cinek1fcafc42015-07-20 14:39:25 -07001567 }
1568
1569 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001570 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1571 * @see #sendKeyguardBouncerChanged(boolean)
1572 */
1573 private void handleKeyguardBouncerChanged(int bouncer) {
1574 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1575 boolean isBouncer = (bouncer == 1);
1576 mBouncer = isBouncer;
1577 for (int i = 0; i < mCallbacks.size(); i++) {
1578 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1579 if (cb != null) {
1580 cb.onKeyguardBouncerChanged(isBouncer);
1581 }
1582 }
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001583 updateFingerprintListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02001584 }
1585
1586 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001587 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1588 */
1589 private void handleReportEmergencyCallAction() {
1590 for (int i = 0; i < mCallbacks.size(); i++) {
1591 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1592 if (cb != null) {
1593 cb.onEmergencyCallAction();
1594 }
1595 }
1596 }
1597
Jim Miller16464b82011-10-20 21:10:13 -07001598 private static boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001599 final boolean nowPluggedIn = current.isPluggedIn();
1600 final boolean wasPluggedIn = old.isPluggedIn();
Jim Miller79a444a2011-02-15 15:02:11 -08001601 final boolean stateChangedWhilePluggedIn =
Jim Miller16464b82011-10-20 21:10:13 -07001602 wasPluggedIn == true && nowPluggedIn == true
1603 && (old.status != current.status);
1604
1605 // change in plug state is always interesting
1606 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001607 return true;
1608 }
1609
1610 // change in battery level while plugged in
Jim Miller16464b82011-10-20 21:10:13 -07001611 if (nowPluggedIn && old.level != current.level) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001612 return true;
1613 }
1614
Jim Miller16464b82011-10-20 21:10:13 -07001615 // change where battery needs charging
Jim Millerbbf1a742012-07-17 18:30:30 -07001616 if (!nowPluggedIn && current.isBatteryLow() && current.level != old.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001617 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001618 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001619
1620 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08001621 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07001622 return true;
1623 }
1624
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001625 return false;
1626 }
1627
1628 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001629 * Remove the given observer's callback.
1630 *
Jim Miller6212cc02012-09-05 17:35:31 -07001631 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001632 */
Jim Miller6212cc02012-09-05 17:35:31 -07001633 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
1634 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1635 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1636 if (mCallbacks.get(i).get() == callback) {
1637 mCallbacks.remove(i);
1638 }
1639 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001640 }
1641
1642 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001643 * Register to receive notifications about general keyguard information
1644 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001645 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001646 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001647 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Jim Miller6212cc02012-09-05 17:35:31 -07001648 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1649 // Prevent adding duplicate callbacks
1650 for (int i = 0; i < mCallbacks.size(); i++) {
1651 if (mCallbacks.get(i).get() == callback) {
1652 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1653 new Exception("Called by"));
1654 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001655 }
1656 }
Jim Miller6212cc02012-09-05 17:35:31 -07001657 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1658 removeCallback(null); // remove unused references
1659 sendUpdates(callback);
1660 }
1661
Evan Rosky18396452016-07-27 15:19:37 -07001662 public boolean isSwitchingUser() {
1663 return mSwitchingUser;
1664 }
1665
1666 public void setSwitchingUser(boolean switching) {
1667 mSwitchingUser = switching;
1668 updateFingerprintListeningState();
1669 }
1670
Jim Miller6212cc02012-09-05 17:35:31 -07001671 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1672 // Notify listener of the current state
1673 callback.onRefreshBatteryInfo(mBatteryStatus);
1674 callback.onTimeChanged();
1675 callback.onRingerModeChanged(mRingMode);
1676 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001677 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001678 callback.onClockVisibilityChanged();
Jim Miller52a61332014-11-12 19:29:51 -08001679 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1680 final SimData state = data.getValue();
1681 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1682 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001683 }
1684
Selim Cinek1fcafc42015-07-20 14:39:25 -07001685 public void sendKeyguardReset() {
1686 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1687 }
1688
Adrian Roosb6011622014-05-14 15:52:53 +02001689 /**
1690 * @see #handleKeyguardBouncerChanged(int)
1691 */
1692 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1693 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1694 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1695 message.arg1 = showingBouncer ? 1 : 0;
1696 message.sendToTarget();
1697 }
1698
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001699 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001700 * 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 -08001701 * have the information earlier than waiting for the intent
1702 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001703 *
1704 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1705 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001706 */
Jim Miller52a61332014-11-12 19:29:51 -08001707 public void reportSimUnlocked(int subId) {
1708 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
Sanket Padawe7e460252017-03-10 16:18:20 -08001709 int slotId = SubscriptionManager.getSlotIndex(subId);
Jim Miller52a61332014-11-12 19:29:51 -08001710 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001711 }
1712
Brian Colonna7fce3802013-09-17 15:51:32 -04001713 /**
1714 * Report that the emergency call button has been pressed and the emergency dialer is
1715 * about to be displayed.
1716 *
1717 * @param bypassHandler runs immediately.
1718 *
1719 * NOTE: Must be called from UI thread if bypassHandler == true.
1720 */
1721 public void reportEmergencyCallAction(boolean bypassHandler) {
1722 if (!bypassHandler) {
1723 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1724 } else {
1725 handleReportEmergencyCallAction();
1726 }
1727 }
1728
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001729 /**
1730 * @return Whether the device is provisioned (whether they have gone through
1731 * the setup wizard)
1732 */
1733 public boolean isDeviceProvisioned() {
1734 return mDeviceProvisioned;
1735 }
1736
Jorim Jaggi9f743032015-05-04 15:22:40 -07001737 public void clearFailedUnlockAttempts() {
1738 mFailedAttempts.delete(sCurrentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001739 }
1740
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001741 public int getFailedUnlockAttempts(int userId) {
1742 return mFailedAttempts.get(userId, 0);
Jorim Jaggi9f743032015-05-04 15:22:40 -07001743 }
1744
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001745 public void reportFailedStrongAuthUnlockAttempt(int userId) {
1746 mFailedAttempts.put(userId, getFailedUnlockAttempts(userId) + 1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001747 }
1748
Jim Millerf41fc962014-06-18 16:33:51 -07001749 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001750 mUserFingerprintAuthenticated.clear();
Kevin Chyn3fdbbf82017-05-06 15:11:53 -07001751 mTrustManager.clearAllFingerprints();
Jim Millerf41fc962014-06-18 16:33:51 -07001752 }
1753
Jim Miller52a61332014-11-12 19:29:51 -08001754 public boolean isSimPinVoiceSecure() {
1755 // TODO: only count SIMs that handle voice
1756 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001757 }
1758
1759 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001760 // True if any SIM is pin secure
1761 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1762 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1763 }
1764 return false;
1765 }
1766
Jason Monk9ff69bd2014-12-02 16:43:17 -05001767 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001768 if (mSimDatas.containsKey(subId)) {
1769 return mSimDatas.get(subId).simState;
1770 } else {
1771 return State.UNKNOWN;
1772 }
1773 }
1774
Winson Chung2db35572017-10-09 15:08:30 -07001775 private final TaskStackChangeListener mTaskStackListener = new TaskStackChangeListener() {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001776 @Override
1777 public void onTaskStackChangedBackground() {
1778 try {
1779 ActivityManager.StackInfo info = ActivityManager.getService().getStackInfo(
Wale Ogunwale68278562017-09-23 17:13:55 -07001780 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_ASSISTANT);
Kevin Chyn2fefd462017-04-28 12:18:19 -07001781 if (info == null) {
1782 return;
1783 }
1784 mHandler.sendMessage(mHandler.obtainMessage(MSG_ASSISTANT_STACK_CHANGED,
1785 info.visible));
1786 } catch (RemoteException e) {
1787 Log.e(TAG, "unable to check task stack", e);
1788 }
1789 }
1790 };
1791
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001792 /**
1793 * @return true if and only if the state has changed for the specified {@code slotId}
1794 */
1795 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08001796
1797 // This is awful. It exists because there are two APIs for getting the SIM status
1798 // that don't return the complete set of values and have different types. In Keyguard we
1799 // need IccCardConstants, but TelephonyManager would only give us
1800 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001801 final TelephonyManager tele = TelephonyManager.from(mContext);
1802 int simState = tele.getSimState(slotId);
Jim Miller52a61332014-11-12 19:29:51 -08001803 State state;
1804 try {
xinhe18b9c3c2014-12-02 15:03:20 -08001805 state = State.intToState(simState);
Jim Miller52a61332014-11-12 19:29:51 -08001806 } catch(IllegalArgumentException ex) {
xinhe18b9c3c2014-12-02 15:03:20 -08001807 Log.w(TAG, "Unknown sim state: " + simState);
Jim Miller52a61332014-11-12 19:29:51 -08001808 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05001809 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001810 SimData data = mSimDatas.get(subId);
1811 final boolean changed;
1812 if (data == null) {
1813 data = new SimData(state, slotId, subId);
1814 mSimDatas.put(subId, data);
1815 changed = true; // no data yet; force update
1816 } else {
1817 changed = data.simState != state;
1818 data.simState = state;
1819 }
1820 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07001821 }
1822
1823 public static boolean isSimPinSecure(IccCardConstants.State state) {
1824 final IccCardConstants.State simState = state;
1825 return (simState == IccCardConstants.State.PIN_REQUIRED
1826 || simState == IccCardConstants.State.PUK_REQUIRED
1827 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001828 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001829
1830 public DisplayClientState getCachedDisplayClientState() {
1831 return mDisplayClientState;
1832 }
Jim Miller20daffd2013-10-07 14:59:53 -07001833
1834 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1835 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001836 public void dispatchStartedWakingUp() {
1837 synchronized (this) {
1838 mDeviceInteractive = true;
1839 }
1840 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1841 }
1842
Jorim Jaggi95e40382015-09-16 15:53:42 -07001843 public void dispatchStartedGoingToSleep(int why) {
1844 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
1845 }
1846
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001847 public void dispatchFinishedGoingToSleep(int why) {
1848 synchronized(this) {
1849 mDeviceInteractive = false;
1850 }
1851 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1852 }
1853
Jim Miller20daffd2013-10-07 14:59:53 -07001854 public void dispatchScreenTurnedOn() {
1855 synchronized (this) {
1856 mScreenOn = true;
1857 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001858 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001859 }
1860
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001861 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001862 synchronized(this) {
1863 mScreenOn = false;
1864 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001865 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001866 }
1867
Selim Cinek99415392016-09-09 14:58:41 -07001868 public void dispatchDreamingStarted() {
1869 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 1, 0));
1870 }
1871
1872 public void dispatchDreamingStopped() {
1873 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 0, 0));
1874 }
1875
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001876 public boolean isDeviceInteractive() {
1877 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001878 }
Jim Miller52a61332014-11-12 19:29:51 -08001879
Jorim Jaggi95e40382015-09-16 15:53:42 -07001880 public boolean isGoingToSleep() {
1881 return mGoingToSleep;
1882 }
1883
Jim Miller52a61332014-11-12 19:29:51 -08001884 /**
1885 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1886 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001887 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001888 */
1889 public int getNextSubIdForState(State state) {
1890 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001891 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001892 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1893 for (int i = 0; i < list.size(); i++) {
1894 final SubscriptionInfo info = list.get(i);
1895 final int id = info.getSubscriptionId();
Sanket Padawe7e460252017-03-10 16:18:20 -08001896 int slotId = SubscriptionManager.getSlotIndex(id);
Jim Miller52a61332014-11-12 19:29:51 -08001897 if (state == getSimState(id) && bestSlotId > slotId ) {
1898 resultId = id;
1899 bestSlotId = slotId;
1900 }
1901 }
1902 return resultId;
1903 }
1904
1905 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1906 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1907 for (int i = 0; i < list.size(); i++) {
1908 SubscriptionInfo info = list.get(i);
1909 if (subId == info.getSubscriptionId()) return info;
1910 }
1911 return null; // not found
1912 }
Jason Monkab525272015-07-13 17:02:49 -04001913
1914 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1915 pw.println("KeyguardUpdateMonitor state:");
1916 pw.println(" SIM States:");
1917 for (SimData data : mSimDatas.values()) {
1918 pw.println(" " + data.toString());
1919 }
1920 pw.println(" Subs:");
1921 if (mSubscriptionInfo != null) {
1922 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
1923 pw.println(" " + mSubscriptionInfo.get(i));
1924 }
1925 }
1926 pw.println(" Service states:");
1927 for (int subId : mServiceStates.keySet()) {
1928 pw.println(" " + subId + "=" + mServiceStates.get(subId));
1929 }
Jim Millerd72d5ac2015-09-29 18:55:32 -07001930 if (mFpm != null && mFpm.isHardwareDetected()) {
1931 final int userId = ActivityManager.getCurrentUser();
1932 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
1933 pw.println(" Fingerprint state (user=" + userId + ")");
1934 pw.println(" allowed=" + isUnlockingWithFingerprintAllowed());
1935 pw.println(" auth'd=" + mUserFingerprintAuthenticated.get(userId));
1936 pw.println(" authSinceBoot="
1937 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
1938 pw.println(" disabled(DPM)=" + isFingerprintDisabled(userId));
1939 pw.println(" possible=" + isUnlockWithFingerprintPossible(userId));
1940 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
Jim Millerd72d5ac2015-09-29 18:55:32 -07001941 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
1942 }
Jason Monkab525272015-07-13 17:02:49 -04001943 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001944}