blob: b4f9b9f4a6531afa9b724b69130decadbdea647c [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
Jorim Jaggiccdfa932015-04-13 16:29:48 -070019import android.app.ActivityManager;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070020import android.app.ActivityManagerNative;
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020021import android.app.AlarmManager;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070022import android.app.IUserSwitchObserver;
Jim Miller8f09fd22013-03-14 19:04:28 -070023import android.app.PendingIntent;
Jim Millerb0304762012-03-13 20:01:25 -070024import android.app.admin.DevicePolicyManager;
Adrian Roos46842d92014-03-27 14:58:03 +010025import android.app.trust.TrustManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080026import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080030import android.database.ContentObserver;
Jim Miller8f09fd22013-03-14 19:04:28 -070031import android.graphics.Bitmap;
Jorim Jaggi3a464782015-08-28 16:59:13 -070032import android.hardware.fingerprint.Fingerprint;
Jorim Jaggi86bed402015-08-20 18:20:02 -070033import android.hardware.fingerprint.FingerprintManager;
34import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
35import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
Jim Miller47088bb2009-11-24 00:40:16 -080036import android.media.AudioManager;
Jim Miller79a444a2011-02-15 15:02:11 -080037import android.os.BatteryManager;
Jim Miller9f0753f2015-03-23 23:59:22 -070038import android.os.CancellationSignal;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080039import android.os.Handler;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070040import android.os.IRemoteCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080041import android.os.Message;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070042import android.os.RemoteException;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -070043import android.os.SystemClock;
Amith Yamasanie8e93a12013-05-09 18:12:30 -070044import android.os.UserHandle;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080045import android.provider.Settings;
Etan Cohen47051d82015-07-06 16:19:04 -070046import android.telephony.ServiceState;
Jim Miller52a61332014-11-12 19:29:51 -080047import android.telephony.SubscriptionInfo;
Jim Miller52a61332014-11-12 19:29:51 -080048import android.telephony.SubscriptionManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -080049import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Jim Millerc23024d2010-02-24 15:37:00 -080050import android.telephony.TelephonyManager;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -070051import android.util.ArraySet;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080052import android.util.Log;
Adrian Roos46842d92014-03-27 14:58:03 +010053import android.util.SparseBooleanArray;
Jorim Jaggi9f743032015-05-04 15:22:40 -070054import android.util.SparseIntArray;
Adrian Roos46842d92014-03-27 14:58:03 +010055
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080056import com.google.android.collect.Lists;
57
Jorim Jaggi86bed402015-08-20 18:20:02 -070058import com.android.internal.telephony.IccCardConstants;
59import com.android.internal.telephony.IccCardConstants.State;
60import com.android.internal.telephony.PhoneConstants;
61import com.android.internal.telephony.TelephonyIntents;
Adrian Roosb5e47222015-08-14 15:53:06 -070062import com.android.internal.widget.LockPatternUtils;
Jorim Jaggi86bed402015-08-20 18:20:02 -070063
Jason Monkab525272015-07-13 17:02:49 -040064import java.io.FileDescriptor;
65import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -070066import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080067import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -080068import java.util.HashMap;
69import java.util.List;
70import java.util.Map.Entry;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080071
Jorim Jaggi86bed402015-08-20 18:20:02 -070072import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
73import static android.os.BatteryManager.BATTERY_STATUS_FULL;
74import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
75import static android.os.BatteryManager.EXTRA_HEALTH;
76import static android.os.BatteryManager.EXTRA_LEVEL;
77import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
Adrian Roos0c859ae2015-11-23 16:47:50 -080078import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE;
Jorim Jaggi86bed402015-08-20 18:20:02 -070079import static android.os.BatteryManager.EXTRA_PLUGGED;
80import static android.os.BatteryManager.EXTRA_STATUS;
81
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080082/**
83 * Watches for updates that may be interesting to the keyguard, and provides
84 * the up to date information as well as a registration for callbacks that care
85 * to be updated.
86 *
87 * Note: under time crunch, this has been extended to include some stuff that
88 * doesn't really belong here. see {@link #handleBatteryUpdate} where it shutdowns
Jim Miller258341c2012-08-30 16:50:10 -070089 * the device, and {@link #getFailedUnlockAttempts()}, {@link #reportFailedAttempt()}
90 * and {@link #clearFailedUnlockAttempts()}. Maybe we should rename this 'KeyguardContext'...
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080091 */
Adrian Roos46842d92014-03-27 14:58:03 +010092public class KeyguardUpdateMonitor implements TrustManager.TrustListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080093
Jim Millerbbf1a742012-07-17 18:30:30 -070094 private static final String TAG = "KeyguardUpdateMonitor";
Jorim Jaggi5cf17872014-03-26 18:31:48 +010095 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller52a61332014-11-12 19:29:51 -080096 private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
Jim Millerbbf1a742012-07-17 18:30:30 -070097 private static final int LOW_BATTERY_THRESHOLD = 20;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080098
Jorim Jaggie7b12522014-08-06 16:41:21 +020099 private static final String ACTION_FACE_UNLOCK_STARTED
100 = "com.android.facelock.FACE_UNLOCK_STARTED";
101 private static final String ACTION_FACE_UNLOCK_STOPPED
102 = "com.android.facelock.FACE_UNLOCK_STOPPED";
103
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700104 private static final String ACTION_STRONG_AUTH_TIMEOUT =
105 "com.android.systemui.ACTION_STRONG_AUTH_TIMEOUT";
106 private static final String USER_ID = "com.android.systemui.USER_ID";
107
108 private static final String PERMISSION_SELF = "com.android.systemui.permission.SELF";
109
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700110 /**
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700111 * Milliseconds after unlocking with fingerprint times out, i.e. the user has to use a
112 * strong auth method like password, PIN or pattern.
113 */
114 private static final long FINGERPRINT_UNLOCK_TIMEOUT_MS = 72 * 60 * 60 * 1000;
115
Jim Millerbbf1a742012-07-17 18:30:30 -0700116 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800117 private static final int MSG_TIME_UPDATE = 301;
118 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800119 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800120 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800121 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700122 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700123 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500124 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700125 private static final int MSG_KEYGUARD_RESET = 312;
Jim Millerf41fc962014-06-18 16:33:51 -0700126 private static final int MSG_BOOT_COMPLETED = 313;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500127 private static final int MSG_USER_SWITCH_COMPLETE = 314;
Jim Millerf41fc962014-06-18 16:33:51 -0700128 private static final int MSG_USER_INFO_CHANGED = 317;
129 private static final int MSG_REPORT_EMERGENCY_CALL_ACTION = 318;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700130 private static final int MSG_STARTED_WAKING_UP = 319;
131 private static final int MSG_FINISHED_GOING_TO_SLEEP = 320;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700132 private static final int MSG_STARTED_GOING_TO_SLEEP = 321;
Adrian Roosb6011622014-05-14 15:52:53 +0200133 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700134 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
135 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400136 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700137 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700138 private static final int MSG_SCREEN_TURNED_ON = 331;
139 private static final int MSG_SCREEN_TURNED_OFF = 332;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800140
Jorim Jaggi86bed402015-08-20 18:20:02 -0700141 /** Fingerprint state: Not listening to fingerprint. */
142 private static final int FINGERPRINT_STATE_STOPPED = 0;
143
144 /** Fingerprint state: Listening. */
145 private static final int FINGERPRINT_STATE_RUNNING = 1;
146
147 /**
148 * Fingerprint state: Cancelling and waiting for the confirmation from FingerprintService to
149 * send us the confirmation that cancellation has happened.
150 */
151 private static final int FINGERPRINT_STATE_CANCELLING = 2;
152
153 /**
154 * Fingerprint state: During cancelling we got another request to start listening, so when we
155 * receive the cancellation done signal, we should start listening again.
156 */
157 private static final int FINGERPRINT_STATE_CANCELLING_RESTARTING = 3;
158
Adrian Roos0c859ae2015-11-23 16:47:50 -0800159 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
160
Jim Millerdcb3d842012-08-23 19:18:12 -0700161 private static KeyguardUpdateMonitor sInstance;
162
Jim Millerbbf1a742012-07-17 18:30:30 -0700163 private final Context mContext;
Jim Miller52a61332014-11-12 19:29:51 -0800164 HashMap<Integer, SimData> mSimDatas = new HashMap<Integer, SimData>();
Etan Cohen47051d82015-07-06 16:19:04 -0700165 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700166
Jim Millerbbf1a742012-07-17 18:30:30 -0700167 private int mRingMode;
168 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400169 private boolean mKeyguardIsVisible;
Jorim Jaggi71448a72015-08-18 19:49:04 -0700170
171 /**
172 * If true, fingerprint was already authenticated and we don't need to start listening again
173 * until the Keyguard has been dismissed.
174 */
175 private boolean mFingerprintAlreadyAuthenticated;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700176 private boolean mGoingToSleep;
Adrian Roosb6011622014-05-14 15:52:53 +0200177 private boolean mBouncer;
Adam Cohen4eb36cf2012-11-07 11:45:30 -0800178 private boolean mBootCompleted;
Jim Millerbbf1a742012-07-17 18:30:30 -0700179
Jim Millerdcb3d842012-08-23 19:18:12 -0700180 // Device provisioning state
Jim Millerbbf1a742012-07-17 18:30:30 -0700181 private boolean mDeviceProvisioned;
182
Jim Millerdcb3d842012-08-23 19:18:12 -0700183 // Battery status
Jim Millerbbf1a742012-07-17 18:30:30 -0700184 private BatteryStatus mBatteryStatus;
185
Jim Millerdcb3d842012-08-23 19:18:12 -0700186 // Password attempts
Jorim Jaggi9f743032015-05-04 15:22:40 -0700187 private SparseIntArray mFailedAttempts = new SparseIntArray();
Brian Colonnacc4104f2012-10-09 17:50:46 -0400188
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700189 /** Tracks whether strong authentication hasn't been used since quite some time per user. */
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700190 private ArraySet<Integer> mStrongAuthNotTimedOut = new ArraySet<>();
Adrian Roosb5e47222015-08-14 15:53:06 -0700191 private final StrongAuthTracker mStrongAuthTracker = new StrongAuthTracker();
Jim Millerbbf1a742012-07-17 18:30:30 -0700192
Jim Miller6212cc02012-09-05 17:35:31 -0700193 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700194 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800195 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700196
Brian Colonnaa5239892013-04-15 11:45:40 -0400197 private boolean mSwitchingUser;
198
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700199 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700200 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800201 private SubscriptionManager mSubscriptionManager;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700202 private AlarmManager mAlarmManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800203 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700204 private TrustManager mTrustManager;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700205 private int mFingerprintRunningState = FINGERPRINT_STATE_STOPPED;
Jim Miller20daffd2013-10-07 14:59:53 -0700206
Jim Millerbbf1a742012-07-17 18:30:30 -0700207 private final Handler mHandler = new Handler() {
208 @Override
209 public void handleMessage(Message msg) {
210 switch (msg.what) {
211 case MSG_TIME_UPDATE:
212 handleTimeUpdate();
213 break;
214 case MSG_BATTERY_UPDATE:
215 handleBatteryUpdate((BatteryStatus) msg.obj);
216 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700217 case MSG_SIM_STATE_CHANGE:
Jim Miller52a61332014-11-12 19:29:51 -0800218 handleSimStateChange(msg.arg1, msg.arg2, (State) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700219 break;
220 case MSG_RINGER_MODE_CHANGED:
221 handleRingerModeChange(msg.arg1);
222 break;
223 case MSG_PHONE_STATE_CHANGED:
Adrian Roosb6011622014-05-14 15:52:53 +0200224 handlePhoneStateChanged((String) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700225 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700226 case MSG_DEVICE_PROVISIONED:
227 handleDeviceProvisioned();
228 break;
229 case MSG_DPM_STATE_CHANGED:
230 handleDevicePolicyManagerStateChanged();
231 break;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500232 case MSG_USER_SWITCHING:
Adrian Roosb6011622014-05-14 15:52:53 +0200233 handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500234 break;
235 case MSG_USER_SWITCH_COMPLETE:
236 handleUserSwitchComplete(msg.arg1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700237 break;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700238 case MSG_KEYGUARD_RESET:
239 handleKeyguardReset();
240 break;
Adrian Roosb6011622014-05-14 15:52:53 +0200241 case MSG_KEYGUARD_BOUNCER_CHANGED:
242 handleKeyguardBouncerChanged(msg.arg1);
243 break;
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800244 case MSG_BOOT_COMPLETED:
245 handleBootCompleted();
246 break;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700247 case MSG_USER_INFO_CHANGED:
248 handleUserInfoChanged(msg.arg1);
249 break;
Brian Colonna7fce3802013-09-17 15:51:32 -0400250 case MSG_REPORT_EMERGENCY_CALL_ACTION:
251 handleReportEmergencyCallAction();
252 break;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700253 case MSG_STARTED_GOING_TO_SLEEP:
254 handleStartedGoingToSleep(msg.arg1);
255 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700256 case MSG_FINISHED_GOING_TO_SLEEP:
257 handleFinishedGoingToSleep(msg.arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700258 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700259 case MSG_STARTED_WAKING_UP:
260 handleStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -0700261 break;
Jorim Jaggie7b12522014-08-06 16:41:21 +0200262 case MSG_FACE_UNLOCK_STATE_CHANGED:
Adrian Roos4a410172014-08-20 17:41:44 +0200263 handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200264 break;
Jim Miller52a61332014-11-12 19:29:51 -0800265 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
266 handleSimSubscriptionInfoChanged();
267 break;
Jason Monk052082c2015-06-11 11:35:23 -0400268 case MSG_AIRPLANE_MODE_CHANGED:
269 handleAirplaneModeChanged();
270 break;
Etan Cohen47051d82015-07-06 16:19:04 -0700271 case MSG_SERVICE_STATE_CHANGE:
272 handleServiceStateChange(msg.arg1, (ServiceState) msg.obj);
273 break;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700274 case MSG_SCREEN_TURNED_ON:
275 handleScreenTurnedOn();
276 break;
277 case MSG_SCREEN_TURNED_OFF:
278 handleScreenTurnedOff();
279 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700280 }
281 }
282 };
283
Wink Savilled09c4ca2014-11-22 10:08:16 -0800284 private OnSubscriptionsChangedListener mSubscriptionListener =
285 new OnSubscriptionsChangedListener() {
Jim Miller52a61332014-11-12 19:29:51 -0800286 @Override
Wink Savilled09c4ca2014-11-22 10:08:16 -0800287 public void onSubscriptionsChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800288 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
289 }
290 };
291
Adrian Roos46842d92014-03-27 14:58:03 +0100292 private SparseBooleanArray mUserHasTrust = new SparseBooleanArray();
Adrian Roos7861c662014-07-25 15:37:28 +0200293 private SparseBooleanArray mUserTrustIsManaged = new SparseBooleanArray();
Jim Miller9f0753f2015-03-23 23:59:22 -0700294 private SparseBooleanArray mUserFingerprintAuthenticated = new SparseBooleanArray();
Adrian Roos4a410172014-08-20 17:41:44 +0200295 private SparseBooleanArray mUserFaceUnlockRunning = new SparseBooleanArray();
Adrian Roos46842d92014-03-27 14:58:03 +0100296
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700297 private static int sCurrentUser;
298
299 public synchronized static void setCurrentUser(int currentUser) {
300 sCurrentUser = currentUser;
301 }
302
303 public synchronized static int getCurrentUser() {
304 return sCurrentUser;
305 }
306
Adrian Roos46842d92014-03-27 14:58:03 +0100307 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700308 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos46842d92014-03-27 14:58:03 +0100309 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200310 for (int i = 0; i < mCallbacks.size(); i++) {
311 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
312 if (cb != null) {
313 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700314 if (enabled && flags != 0) {
315 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200316 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200317 }
318 }
Adrian Roos46842d92014-03-27 14:58:03 +0100319 }
320
Jim Miller52a61332014-11-12 19:29:51 -0800321 protected void handleSimSubscriptionInfoChanged() {
322 if (DEBUG_SIM_STATES) {
323 Log.v(TAG, "onSubscriptionInfoChanged()");
Wink Savilled09c4ca2014-11-22 10:08:16 -0800324 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
325 if (sil != null) {
326 for (SubscriptionInfo subInfo : sil) {
327 Log.v(TAG, "SubInfo:" + subInfo);
328 }
329 } else {
330 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800331 }
332 }
333 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
334
335 // Hack level over 9000: Because the subscription id is not yet valid when we see the
336 // first update in handleSimStateChange, we need to force refresh all all SIM states
337 // so the subscription id for them is consistent.
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100338 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
Jim Miller52a61332014-11-12 19:29:51 -0800339 for (int i = 0; i < subscriptionInfos.size(); i++) {
340 SubscriptionInfo info = subscriptionInfos.get(i);
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100341 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
342 if (changed) {
343 changedSubscriptions.add(info);
344 }
Jim Miller52a61332014-11-12 19:29:51 -0800345 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100346 for (int i = 0; i < changedSubscriptions.size(); i++) {
347 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800348 for (int j = 0; j < mCallbacks.size(); j++) {
349 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
350 if (cb != null) {
351 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
352 }
353 }
354 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500355 for (int j = 0; j < mCallbacks.size(); j++) {
356 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
357 if (cb != null) {
358 cb.onRefreshCarrierInfo();
359 }
360 }
Jim Miller52a61332014-11-12 19:29:51 -0800361 }
362
Jason Monk052082c2015-06-11 11:35:23 -0400363 private void handleAirplaneModeChanged() {
364 for (int j = 0; j < mCallbacks.size(); j++) {
365 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
366 if (cb != null) {
367 cb.onRefreshCarrierInfo();
368 }
369 }
370 }
371
Wink Savilled09c4ca2014-11-22 10:08:16 -0800372 /** @return List of SubscriptionInfo records, maybe empty but never null */
Jim Miller52a61332014-11-12 19:29:51 -0800373 List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800374 List<SubscriptionInfo> sil = mSubscriptionInfo;
375 if (sil == null || forceReload) {
376 sil = mSubscriptionManager.getActiveSubscriptionInfoList();
377 }
378 if (sil == null) {
379 // getActiveSubscriptionInfoList was null callers expect an empty list.
380 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
381 } else {
382 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800383 }
384 return mSubscriptionInfo;
385 }
386
Adrian Roos7861c662014-07-25 15:37:28 +0200387 @Override
388 public void onTrustManagedChanged(boolean managed, int userId) {
389 mUserTrustIsManaged.put(userId, managed);
390
391 for (int i = 0; i < mCallbacks.size(); i++) {
392 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
393 if (cb != null) {
394 cb.onTrustManagedChanged(userId);
395 }
396 }
397 }
398
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700399 private void onFingerprintAuthenticated(int userId) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700400 mUserFingerprintAuthenticated.put(userId, true);
Jorim Jaggi71448a72015-08-18 19:49:04 -0700401
402 // If fingerprint unlocking is allowed, this event will lead to a Keyguard dismiss or to a
403 // wake-up (if Keyguard is not showing), so we don't need to listen until Keyguard is
404 // fully gone.
405 mFingerprintAlreadyAuthenticated = isUnlockingWithFingerprintAllowed();
Jim Millerf41fc962014-06-18 16:33:51 -0700406 for (int i = 0; i < mCallbacks.size(); i++) {
407 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
408 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700409 cb.onFingerprintAuthenticated(userId);
Jim Millerf41fc962014-06-18 16:33:51 -0700410 }
411 }
412 }
413
Jim Millerce7eb6d2015-04-03 19:29:13 -0700414 private void handleFingerprintAuthFailed() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700415 for (int i = 0; i < mCallbacks.size(); i++) {
416 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
417 if (cb != null) {
418 cb.onFingerprintAuthFailed();
419 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700420 }
Jim Millerce7eb6d2015-04-03 19:29:13 -0700421 handleFingerprintHelp(-1, mContext.getString(R.string.fingerprint_not_recognized));
422 }
Jim Millerf41fc962014-06-18 16:33:51 -0700423
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700424 private void handleFingerprintAcquired(int acquireInfo) {
425 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
426 return;
427 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700428 for (int i = 0; i < mCallbacks.size(); i++) {
429 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
430 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700431 cb.onFingerprintAcquired();
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700432 }
433 }
434 }
435
Jim Miller4f364c92015-06-08 19:24:13 -0700436 private void handleFingerprintAuthenticated() {
Jim Millerf41fc962014-06-18 16:33:51 -0700437 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700438 final int userId;
439 try {
440 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
441 } catch (RemoteException e) {
442 Log.e(TAG, "Failed to get current user id: ", e);
443 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700444 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700445 if (isFingerprintDisabled(userId)) {
446 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
447 return;
448 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700449 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700450 } finally {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700451 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700452 }
453 }
454
Jim Miller9f0753f2015-03-23 23:59:22 -0700455 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700456 for (int i = 0; i < mCallbacks.size(); i++) {
457 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
458 if (cb != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700459 cb.onFingerprintHelp(msgId, helpString);
460 }
461 }
462 }
463
464 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700465 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
466 && mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
467 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
468 startListeningForFingerprint();
469 } else {
470 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
471 }
Jim Miller9f0753f2015-03-23 23:59:22 -0700472 for (int i = 0; i < mCallbacks.size(); i++) {
473 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
474 if (cb != null) {
475 cb.onFingerprintError(msgId, errString);
Jim Millerf41fc962014-06-18 16:33:51 -0700476 }
477 }
478 }
479
Jorim Jaggi3a464782015-08-28 16:59:13 -0700480 private void handleFingerprintLockoutReset() {
481 updateFingerprintListeningState();
482 }
483
Jorim Jaggi86bed402015-08-20 18:20:02 -0700484 private void setFingerprintRunningState(int fingerprintRunningState) {
485 boolean wasRunning = mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
486 boolean isRunning = fingerprintRunningState == FINGERPRINT_STATE_RUNNING;
487 mFingerprintRunningState = fingerprintRunningState;
488
489 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
490 // asynchronousness of the cancel cycle. So only notify them if the actualy running state
491 // has changed.
492 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700493 notifyFingerprintRunningStateChanged();
494 }
495 }
496
497 private void notifyFingerprintRunningStateChanged() {
498 for (int i = 0; i < mCallbacks.size(); i++) {
499 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
500 if (cb != null) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700501 cb.onFingerprintRunningStateChanged(isFingerprintDetectionRunning());
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700502 }
503 }
504 }
Adrian Roos4a410172014-08-20 17:41:44 +0200505 private void handleFaceUnlockStateChanged(boolean running, int userId) {
506 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200507 for (int i = 0; i < mCallbacks.size(); i++) {
508 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
509 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200510 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200511 }
512 }
513 }
514
Adrian Roos4a410172014-08-20 17:41:44 +0200515 public boolean isFaceUnlockRunning(int userId) {
516 return mUserFaceUnlockRunning.get(userId);
517 }
518
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700519 public boolean isFingerprintDetectionRunning() {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700520 return mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700521 }
522
Jim Miller50e62182014-04-23 17:25:00 -0700523 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100524 // Don't allow trust agent if device is secured with a SIM PIN. This is here
525 // mainly because there's no other way to prompt the user to enter their SIM PIN
526 // once they get past the keyguard screen.
527 final boolean disabledBySimPin = isSimPinSecure();
528 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700529 }
530
Jim Miller06e34502014-07-17 14:46:05 -0700531 private boolean isFingerprintDisabled(int userId) {
532 final DevicePolicyManager dpm =
533 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
534 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Adrian Roos733b6632015-08-21 14:32:35 -0700535 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
536 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700537 }
538
Selim Cineke8bae622015-07-15 13:24:06 -0700539 public boolean getUserCanSkipBouncer(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700540 return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
541 && isUnlockingWithFingerprintAllowed());
Selim Cineke8bae622015-07-15 13:24:06 -0700542 }
543
Adrian Roos46842d92014-03-27 14:58:03 +0100544 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700545 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100546 }
547
Adrian Roos7861c662014-07-25 15:37:28 +0200548 public boolean getUserTrustIsManaged(int userId) {
549 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
550 }
551
Selim Cinek1fcafc42015-07-20 14:39:25 -0700552 public boolean isUnlockingWithFingerprintAllowed() {
Adrian Roosb5e47222015-08-14 15:53:06 -0700553 return mStrongAuthTracker.isUnlockingWithFingerprintAllowed()
554 && !hasFingerprintUnlockTimedOut(sCurrentUser);
555 }
556
557 public StrongAuthTracker getStrongAuthTracker() {
558 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700559 }
560
561 /**
562 * @return true if the user hasn't use strong authentication (pattern, PIN, password) since a
563 * while and thus can't unlock with fingerprint, false otherwise
564 */
565 public boolean hasFingerprintUnlockTimedOut(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700566 return !mStrongAuthNotTimedOut.contains(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700567 }
568
569 public void reportSuccessfulStrongAuthUnlockAttempt() {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700570 mStrongAuthNotTimedOut.add(sCurrentUser);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700571 scheduleStrongAuthTimeout();
Jim Millere0507bb2015-08-12 20:30:34 -0700572 if (mFpm != null) {
573 byte[] token = null; /* TODO: pass real auth token once fp HAL supports it */
574 mFpm.resetTimeout(token);
575 }
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700576 }
577
578 private void scheduleStrongAuthTimeout() {
579 long when = SystemClock.elapsedRealtime() + FINGERPRINT_UNLOCK_TIMEOUT_MS;
580 Intent intent = new Intent(ACTION_STRONG_AUTH_TIMEOUT);
581 intent.putExtra(USER_ID, sCurrentUser);
582 PendingIntent sender = PendingIntent.getBroadcast(mContext,
583 sCurrentUser, intent, PendingIntent.FLAG_CANCEL_CURRENT);
584 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, when, sender);
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700585 notifyStrongAuthStateChanged(sCurrentUser);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700586 }
587
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700588 private void notifyStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700589 for (int i = 0; i < mCallbacks.size(); i++) {
590 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
591 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700592 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700593 }
594 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700595 }
596
Jim Miller8f09fd22013-03-14 19:04:28 -0700597 static class DisplayClientState {
598 public int clientGeneration;
599 public boolean clearing;
600 public PendingIntent intent;
601 public int playbackState;
602 public long playbackEventTime;
603 }
604
605 private DisplayClientState mDisplayClientState = new DisplayClientState();
606
Jim Millerbbf1a742012-07-17 18:30:30 -0700607 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
608
Jim Millerd72d5ac2015-09-29 18:55:32 -0700609 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700610 public void onReceive(Context context, Intent intent) {
611 final String action = intent.getAction();
612 if (DEBUG) Log.d(TAG, "received broadcast " + action);
613
614 if (Intent.ACTION_TIME_TICK.equals(action)
615 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200616 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700617 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700618 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
619 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
620 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
621 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
622 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -0800623
624 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
625 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
626 final int maxChargingMicroWatt;
627
628 if (maxChargingMicroVolt <= 0) {
629 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
630 }
631 if (maxChargingMicroAmp > 0) {
632 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
633 // to maintain precision equally on both factors.
634 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
635 * (maxChargingMicroVolt / 1000);
636 } else {
637 maxChargingMicroWatt = -1;
638 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700639 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700640 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -0800641 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -0700642 mHandler.sendMessage(msg);
643 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Jim Miller52a61332014-11-12 19:29:51 -0800644 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700645 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800646 Log.v(TAG, "action " + action
647 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
648 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700649 }
Jim Miller52a61332014-11-12 19:29:51 -0800650 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
651 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700652 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
653 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
654 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
655 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
656 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
657 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400658 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
659 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800660 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700661 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700662 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
663 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
664 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
665 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
666 if (DEBUG) {
667 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
668 + subId);
669 }
670 mHandler.sendMessage(
671 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Jim Millerbbf1a742012-07-17 18:30:30 -0700672 }
673 }
674 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700675
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700676 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
677
Jim Millerd72d5ac2015-09-29 18:55:32 -0700678 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700679 public void onReceive(Context context, Intent intent) {
680 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200681 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
682 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
683 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700684 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
685 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200686 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
687 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
688 getSendingUserId()));
689 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
690 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
691 getSendingUserId()));
692 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
693 .equals(action)) {
694 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700695 }
696 }
697 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700698
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700699 private final BroadcastReceiver mStrongAuthTimeoutReceiver = new BroadcastReceiver() {
700 @Override
701 public void onReceive(Context context, Intent intent) {
702 if (ACTION_STRONG_AUTH_TIMEOUT.equals(intent.getAction())) {
703 int userId = intent.getIntExtra(USER_ID, -1);
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700704 mStrongAuthNotTimedOut.remove(userId);
705 notifyStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700706 }
707 }
708 };
709
Jorim Jaggi3a464782015-08-28 16:59:13 -0700710 private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
711 = new FingerprintManager.LockoutResetCallback() {
712 @Override
713 public void onLockoutReset() {
714 handleFingerprintLockoutReset();
715 }
716 };
717
Jim Miller9f0753f2015-03-23 23:59:22 -0700718 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
719 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700720
721 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700722 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700723 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700724 };
725
726 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700727 public void onAuthenticationSucceeded(AuthenticationResult result) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700728 handleFingerprintAuthenticated();
Jim Millerf41fc962014-06-18 16:33:51 -0700729 }
730
731 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700732 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700733 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700734 }
735
736 @Override
737 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700738 handleFingerprintError(errMsgId, errString.toString());
739 }
740
741 @Override
742 public void onAuthenticationAcquired(int acquireInfo) {
743 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700744 }
745 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700746 private CancellationSignal mFingerprintCancelSignal;
747 private FingerprintManager mFpm;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700748
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800749 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800750 * When we receive a
751 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700752 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800753 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800754 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800755 */
Jim Miller52a61332014-11-12 19:29:51 -0800756 private static class SimData {
757 public State simState;
758 public int slotId;
759 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800760
Jim Miller52a61332014-11-12 19:29:51 -0800761 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800762 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800763 slotId = slot;
764 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800765 }
766
Jim Miller52a61332014-11-12 19:29:51 -0800767 static SimData fromIntent(Intent intent) {
768 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800769 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
770 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
771 }
Wink Savillea639b312012-07-10 12:37:54 -0700772 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800773 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
774 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800775 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700776 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700777 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700778 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700779
Wink Savillea639b312012-07-10 12:37:54 -0700780 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700781 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700782 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700783 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700784 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700785 }
Wink Savillea639b312012-07-10 12:37:54 -0700786 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
787 state = IccCardConstants.State.READY;
788 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800789 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700790 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
791 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
792 state = IccCardConstants.State.PIN_REQUIRED;
793 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
794 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800795 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700796 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800797 }
Wink Savillea639b312012-07-10 12:37:54 -0700798 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
799 state = IccCardConstants.State.NETWORK_LOCKED;
Jim Miller109f1fd2012-09-19 20:44:16 -0700800 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
801 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
802 // This is required because telephony doesn't return to "READY" after
803 // these state transitions. See bug 7197471.
804 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800805 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700806 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800807 }
Jim Miller52a61332014-11-12 19:29:51 -0800808 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800809 }
810
Jim Millerd72d5ac2015-09-29 18:55:32 -0700811 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800812 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800813 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800814 }
815 }
816
Adrian Roos12c1ef52014-06-04 13:54:08 +0200817 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700818 public static final int CHARGING_UNKNOWN = -1;
819 public static final int CHARGING_SLOWLY = 0;
820 public static final int CHARGING_REGULAR = 1;
821 public static final int CHARGING_FAST = 2;
822
Jim Miller16464b82011-10-20 21:10:13 -0700823 public final int status;
824 public final int level;
825 public final int plugged;
826 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800827 public final int maxChargingWattage;
828 public BatteryStatus(int status, int level, int plugged, int health,
829 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -0700830 this.status = status;
831 this.level = level;
832 this.plugged = plugged;
833 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800834 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -0700835 }
836
Jim Millerbbf1a742012-07-17 18:30:30 -0700837 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700838 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700839 * @return true if the device is plugged in.
840 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100841 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700842 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700843 || plugged == BatteryManager.BATTERY_PLUGGED_USB
844 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -0700845 }
846
847 /**
848 * Whether or not the device is charged. Note that some devices never return 100% for
849 * battery level, so this allows either battery level or status to determine if the
850 * battery is charged.
851 * @return true if the device is charged
852 */
853 public boolean isCharged() {
854 return status == BATTERY_STATUS_FULL || level >= 100;
855 }
856
857 /**
858 * Whether battery is low and needs to be charged.
859 * @return true if battery is low
860 */
861 public boolean isBatteryLow() {
862 return level < LOW_BATTERY_THRESHOLD;
863 }
864
Adrian Roos7b043112015-07-10 13:00:33 -0700865 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -0800866 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
867 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
868 maxChargingWattage > fastThreshold ? CHARGING_FAST :
Adrian Roos7b043112015-07-10 13:00:33 -0700869 CHARGING_REGULAR;
870 }
Jim Miller16464b82011-10-20 21:10:13 -0700871 }
872
Adrian Roosb5e47222015-08-14 15:53:06 -0700873 public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
874
875 public boolean isUnlockingWithFingerprintAllowed() {
876 int userId = getCurrentUser();
877 return isFingerprintAllowedForUser(userId);
878 }
879
880 public boolean hasUserAuthenticatedSinceBoot() {
881 int userId = getCurrentUser();
882 return (getStrongAuthForUser(userId)
883 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
884 }
885
886 @Override
887 public void onStrongAuthRequiredChanged(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700888 notifyStrongAuthStateChanged(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -0700889 }
890 }
891
Jim Millerdcb3d842012-08-23 19:18:12 -0700892 public static KeyguardUpdateMonitor getInstance(Context context) {
893 if (sInstance == null) {
894 sInstance = new KeyguardUpdateMonitor(context);
895 }
896 return sInstance;
897 }
898
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700899 protected void handleStartedWakingUp() {
Jorim Jaggi864e64b2015-05-20 14:13:23 -0700900 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700901 final int count = mCallbacks.size();
902 for (int i = 0; i < count; i++) {
903 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
904 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700905 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -0700906 }
907 }
908 }
909
Jorim Jaggi95e40382015-09-16 15:53:42 -0700910 protected void handleStartedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -0700911 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -0700912 final int count = mCallbacks.size();
913 for (int i = 0; i < count; i++) {
914 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
915 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -0700916 cb.onStartedGoingToSleep(arg1);
917 }
918 }
919 mGoingToSleep = true;
920 mFingerprintAlreadyAuthenticated = false;
921 updateFingerprintListeningState();
922 }
923
924 protected void handleFinishedGoingToSleep(int arg1) {
925 mGoingToSleep = false;
926 final int count = mCallbacks.size();
927 for (int i = 0; i < count; i++) {
928 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
929 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700930 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700931 }
932 }
Jorim Jaggiea657062015-04-28 13:45:11 -0700933 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700934 }
935
Jorim Jaggif1518da2015-07-30 11:56:36 -0700936 private void handleScreenTurnedOn() {
937 final int count = mCallbacks.size();
938 for (int i = 0; i < count; i++) {
939 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
940 if (cb != null) {
941 cb.onScreenTurnedOn();
942 }
943 }
944 }
945
946 private void handleScreenTurnedOff() {
947 final int count = mCallbacks.size();
948 for (int i = 0; i < count; i++) {
949 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
950 if (cb != null) {
951 cb.onScreenTurnedOff();
952 }
953 }
954 }
955
Adam Powell43a372f2013-09-27 17:43:53 -0700956 /**
957 * IMPORTANT: Must be called from UI thread.
958 */
959 public void dispatchSetBackground(Bitmap bmp) {
960 if (DEBUG) Log.d(TAG, "dispatchSetBackground");
961 final int count = mCallbacks.size();
962 for (int i = 0; i < count; i++) {
963 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
964 if (cb != null) {
965 cb.onSetBackground(bmp);
966 }
967 }
968 }
969
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700970 private void handleUserInfoChanged(int userId) {
971 for (int i = 0; i < mCallbacks.size(); i++) {
972 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
973 if (cb != null) {
974 cb.onUserInfoChanged(userId);
975 }
976 }
977 }
978
Jim Millerdcb3d842012-08-23 19:18:12 -0700979 private KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800980 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800981 mSubscriptionManager = SubscriptionManager.from(context);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700982 mAlarmManager = context.getSystemService(AlarmManager.class);
Michael Jurkafff56142012-11-28 16:51:00 -0800983 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700984
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800985 // Since device can't be un-provisioned, we only need to register a content observer
986 // to update mDeviceProvisioned when we are...
987 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700988 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800989 }
Jim Miller47088bb2009-11-24 00:40:16 -0800990
Jim Millerbbf1a742012-07-17 18:30:30 -0700991 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -0700992 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800993
Jim Millerbbf1a742012-07-17 18:30:30 -0700994 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800995 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800996 filter.addAction(Intent.ACTION_TIME_TICK);
997 filter.addAction(Intent.ACTION_TIME_CHANGED);
998 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
999 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -04001000 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001001 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -07001002 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001003 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001004 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Jim Millerbbf1a742012-07-17 18:30:30 -07001005 context.registerReceiver(mBroadcastReceiver, filter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001006
Adam Cohenc276e822012-11-08 13:01:08 -08001007 final IntentFilter bootCompleteFilter = new IntentFilter();
1008 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1009 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
1010 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter);
1011
Adrian Roos48c796c2014-09-01 14:59:23 +02001012 final IntentFilter allUserFilter = new IntentFilter();
1013 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1014 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1015 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1016 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1017 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
1018 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001019 null, null);
1020
Wink Saville071743f2015-01-12 17:11:04 -08001021 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001022 try {
1023 ActivityManagerNative.getDefault().registerUserSwitchObserver(
1024 new IUserSwitchObserver.Stub() {
1025 @Override
1026 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001027 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001028 newUserId, 0, reply));
1029 }
1030 @Override
1031 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001032 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001033 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001034 }
Kenny Guy42979622015-04-13 18:03:05 +00001035 @Override
1036 public void onForegroundProfileSwitch(int newProfileId) {
1037 // Ignore.
1038 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001039 });
1040 } catch (RemoteException e) {
1041 // TODO Auto-generated catch block
1042 e.printStackTrace();
1043 }
Adrian Roos46842d92014-03-27 14:58:03 +01001044
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001045 IntentFilter strongAuthTimeoutFilter = new IntentFilter();
1046 strongAuthTimeoutFilter.addAction(ACTION_STRONG_AUTH_TIMEOUT);
1047 context.registerReceiver(mStrongAuthTimeoutReceiver, strongAuthTimeoutFilter,
1048 PERMISSION_SELF, null /* handler */);
Jorim Jaggi237b0612015-05-01 14:28:49 -07001049 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1050 mTrustManager.registerTrustListener(this);
Adrian Roosb5e47222015-08-14 15:53:06 -07001051 new LockPatternUtils(context).registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001052
Jim Miller9f0753f2015-03-23 23:59:22 -07001053 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
Jorim Jaggiea657062015-04-28 13:45:11 -07001054 updateFingerprintListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001055 if (mFpm != null) {
1056 mFpm.addLockoutResetCallback(mLockoutResetCallback);
1057 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001058 }
1059
1060 private void updateFingerprintListeningState() {
1061 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001062 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001063 stopListeningForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001064 } else if (mFingerprintRunningState != FINGERPRINT_STATE_RUNNING
1065 && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001066 startListeningForFingerprint();
1067 }
1068 }
1069
1070 private boolean shouldListenForFingerprint() {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001071 return (mKeyguardIsVisible || !mDeviceInteractive || mBouncer || mGoingToSleep)
1072 && !mSwitchingUser && !mFingerprintAlreadyAuthenticated
1073 && !isFingerprintDisabled(getCurrentUser());
Jim Miller9f0753f2015-03-23 23:59:22 -07001074 }
1075
Jim Millerce7eb6d2015-04-03 19:29:13 -07001076 private void startListeningForFingerprint() {
Jorim Jaggi86bed402015-08-20 18:20:02 -07001077 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING) {
1078 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING_RESTARTING);
1079 return;
1080 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001081 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001082 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001083 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001084 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001085 mFingerprintCancelSignal.cancel();
1086 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001087 mFingerprintCancelSignal = new CancellationSignal();
Jim Millerf501b582015-06-03 16:36:31 -07001088 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null, userId);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001089 setFingerprintRunningState(FINGERPRINT_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001090 }
1091 }
1092
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001093 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001094 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1095 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1096 }
1097
Jorim Jaggiea657062015-04-28 13:45:11 -07001098 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001099 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Jorim Jaggi86bed402015-08-20 18:20:02 -07001100 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001101 mFingerprintCancelSignal.cancel();
Jim Millerce7eb6d2015-04-03 19:29:13 -07001102 mFingerprintCancelSignal = null;
Jorim Jaggi86bed402015-08-20 18:20:02 -07001103 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001104 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001105 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
1106 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
1107 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001108 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001109
Michael Jurkafff56142012-11-28 16:51:00 -08001110 private boolean isDeviceProvisionedInSettingsDb() {
1111 return Settings.Global.getInt(mContext.getContentResolver(),
1112 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1113 }
1114
Jim Millerbbf1a742012-07-17 18:30:30 -07001115 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001116 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001117 @Override
1118 public void onChange(boolean selfChange) {
1119 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001120 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001121 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001122 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001123 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001124 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001125 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001126 };
1127
1128 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001129 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001130 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001131
1132 // prevent a race condition between where we check the flag and where we register the
1133 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001134 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001135 if (provisioned != mDeviceProvisioned) {
1136 mDeviceProvisioned = provisioned;
1137 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001138 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001139 }
1140 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001141 }
1142
Jim Millerbbf1a742012-07-17 18:30:30 -07001143 /**
1144 * Handle {@link #MSG_DPM_STATE_CHANGED}
1145 */
Jim Millerb0304762012-03-13 20:01:25 -07001146 protected void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001147 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001148 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1149 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1150 if (cb != null) {
1151 cb.onDevicePolicyManagerStateChanged();
1152 }
Jim Millerb0304762012-03-13 20:01:25 -07001153 }
1154 }
1155
Jim Millerbbf1a742012-07-17 18:30:30 -07001156 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001157 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001158 */
Chris Wrenf41c61b2012-11-29 15:19:54 -05001159 protected void handleUserSwitching(int userId, IRemoteCallback reply) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001160 mSwitchingUser = true;
1161 updateFingerprintListeningState();
1162
Jim Millerbbf1a742012-07-17 18:30:30 -07001163 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001164 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1165 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001166 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001167 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001168 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001169 try {
1170 reply.sendResult(null);
1171 } catch (RemoteException e) {
1172 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001173 }
1174
Jim Millerbbf1a742012-07-17 18:30:30 -07001175 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001176 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1177 */
1178 protected void handleUserSwitchComplete(int userId) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001179 mSwitchingUser = false;
1180 updateFingerprintListeningState();
1181
Chris Wrenf41c61b2012-11-29 15:19:54 -05001182 for (int i = 0; i < mCallbacks.size(); i++) {
1183 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1184 if (cb != null) {
1185 cb.onUserSwitchComplete(userId);
1186 }
1187 }
1188 }
1189
1190 /**
Jim Miller90873d52013-09-26 18:11:38 -07001191 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1192 * keyguard crashes sometime after boot, then it will never receive this
1193 * broadcast and hence not handle the event. This method is ultimately called by
1194 * PhoneWindowManager in this case.
1195 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001196 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001197 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001198 }
1199
1200 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001201 * Handle {@link #MSG_BOOT_COMPLETED}
1202 */
1203 protected void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001204 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001205 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001206 for (int i = 0; i < mCallbacks.size(); i++) {
1207 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1208 if (cb != null) {
1209 cb.onBootCompleted();
1210 }
1211 }
1212 }
1213
1214 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001215 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001216 * destroyed.
1217 */
1218 public boolean hasBootCompleted() {
1219 return mBootCompleted;
1220 }
1221
1222 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001223 * Handle {@link #MSG_DEVICE_PROVISIONED}
1224 */
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001225 protected void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001226 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001227 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1228 if (cb != null) {
1229 cb.onDeviceProvisioned();
1230 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001231 }
Michael Jurkafff56142012-11-28 16:51:00 -08001232 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001233 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001234 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1235 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001236 }
1237 }
1238
Jim Millerbbf1a742012-07-17 18:30:30 -07001239 /**
1240 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1241 */
Jim Millerc23024d2010-02-24 15:37:00 -08001242 protected void handlePhoneStateChanged(String newState) {
1243 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001244 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1245 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1246 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1247 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1248 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1249 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1250 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001251 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001252 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1253 if (cb != null) {
1254 cb.onPhoneStateChanged(mPhoneState);
1255 }
Jim Millerc23024d2010-02-24 15:37:00 -08001256 }
1257 }
1258
Jim Millerbbf1a742012-07-17 18:30:30 -07001259 /**
1260 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1261 */
Jim Miller47088bb2009-11-24 00:40:16 -08001262 protected void handleRingerModeChange(int mode) {
1263 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001264 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001265 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001266 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1267 if (cb != null) {
1268 cb.onRingerModeChanged(mode);
1269 }
Jim Miller47088bb2009-11-24 00:40:16 -08001270 }
1271 }
1272
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001273 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001274 * Handle {@link #MSG_TIME_UPDATE}
1275 */
1276 private void handleTimeUpdate() {
1277 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001278 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001279 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1280 if (cb != null) {
1281 cb.onTimeChanged();
1282 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001283 }
1284 }
1285
1286 /**
1287 * Handle {@link #MSG_BATTERY_UPDATE}
1288 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001289 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001290 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001291 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1292 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001293 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001294 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001295 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1296 if (cb != null) {
1297 cb.onRefreshBatteryInfo(status);
1298 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001299 }
1300 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001301 }
1302
1303 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001304 * Handle {@link #MSG_SIM_STATE_CHANGE}
1305 */
Jim Miller52a61332014-11-12 19:29:51 -08001306 private void handleSimStateChange(int subId, int slotId, State state) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001307
Jim Miller52a61332014-11-12 19:29:51 -08001308 if (DEBUG_SIM_STATES) {
1309 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1310 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001311 }
1312
Wink Savillea54bf652014-12-11 13:37:50 -08001313 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001314 Log.w(TAG, "invalid subId in handleSimStateChange()");
1315 return;
1316 }
1317
1318 SimData data = mSimDatas.get(subId);
1319 final boolean changed;
1320 if (data == null) {
1321 data = new SimData(state, slotId, subId);
1322 mSimDatas.put(subId, data);
1323 changed = true; // no data yet; force update
1324 } else {
1325 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1326 data.simState = state;
1327 data.subId = subId;
1328 data.slotId = slotId;
1329 }
1330 if (changed && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001331 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001332 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1333 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08001334 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001335 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001336 }
1337 }
1338 }
1339
Jim Millerbbf1a742012-07-17 18:30:30 -07001340 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001341 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1342 */
1343 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1344 if (DEBUG) {
1345 Log.d(TAG,
1346 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1347 }
1348
1349 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1350 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1351 return;
1352 }
1353
1354 mServiceStates.put(subId, serviceState);
1355
1356 for (int j = 0; j < mCallbacks.size(); j++) {
1357 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1358 if (cb != null) {
1359 cb.onRefreshCarrierInfo();
1360 }
1361 }
1362 }
1363
1364 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001365 * Notifies that the visibility state of Keyguard has changed.
1366 *
1367 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001368 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001369 public void onKeyguardVisibilityChanged(boolean showing) {
1370 if (DEBUG) Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
1371 mKeyguardIsVisible = showing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001372 for (int i = 0; i < mCallbacks.size(); i++) {
1373 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1374 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001375 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001376 }
1377 }
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001378 if (!showing) {
Jorim Jaggi71448a72015-08-18 19:49:04 -07001379 mFingerprintAlreadyAuthenticated = false;
1380 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001381 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001382 }
1383
Brian Colonna7fce3802013-09-17 15:51:32 -04001384 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001385 * Handle {@link #MSG_KEYGUARD_RESET}
1386 */
1387 private void handleKeyguardReset() {
1388 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Adrian Roosf6d51ac2015-09-02 13:26:25 -07001389 updateFingerprintListeningState();
Selim Cinek1fcafc42015-07-20 14:39:25 -07001390 }
1391
1392 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001393 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1394 * @see #sendKeyguardBouncerChanged(boolean)
1395 */
1396 private void handleKeyguardBouncerChanged(int bouncer) {
1397 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1398 boolean isBouncer = (bouncer == 1);
1399 mBouncer = isBouncer;
1400 for (int i = 0; i < mCallbacks.size(); i++) {
1401 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1402 if (cb != null) {
1403 cb.onKeyguardBouncerChanged(isBouncer);
1404 }
1405 }
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001406 updateFingerprintListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02001407 }
1408
1409 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001410 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1411 */
1412 private void handleReportEmergencyCallAction() {
1413 for (int i = 0; i < mCallbacks.size(); i++) {
1414 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1415 if (cb != null) {
1416 cb.onEmergencyCallAction();
1417 }
1418 }
1419 }
1420
Jim Miller16464b82011-10-20 21:10:13 -07001421 private static boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001422 final boolean nowPluggedIn = current.isPluggedIn();
1423 final boolean wasPluggedIn = old.isPluggedIn();
Jim Miller79a444a2011-02-15 15:02:11 -08001424 final boolean stateChangedWhilePluggedIn =
Jim Miller16464b82011-10-20 21:10:13 -07001425 wasPluggedIn == true && nowPluggedIn == true
1426 && (old.status != current.status);
1427
1428 // change in plug state is always interesting
1429 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001430 return true;
1431 }
1432
1433 // change in battery level while plugged in
Jim Miller16464b82011-10-20 21:10:13 -07001434 if (nowPluggedIn && old.level != current.level) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001435 return true;
1436 }
1437
Jim Miller16464b82011-10-20 21:10:13 -07001438 // change where battery needs charging
Jim Millerbbf1a742012-07-17 18:30:30 -07001439 if (!nowPluggedIn && current.isBatteryLow() && current.level != old.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001440 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001441 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001442
1443 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08001444 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07001445 return true;
1446 }
1447
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001448 return false;
1449 }
1450
1451 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001452 * Remove the given observer's callback.
1453 *
Jim Miller6212cc02012-09-05 17:35:31 -07001454 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001455 */
Jim Miller6212cc02012-09-05 17:35:31 -07001456 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
1457 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1458 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1459 if (mCallbacks.get(i).get() == callback) {
1460 mCallbacks.remove(i);
1461 }
1462 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001463 }
1464
1465 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001466 * Register to receive notifications about general keyguard information
1467 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001468 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001469 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001470 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Jim Miller6212cc02012-09-05 17:35:31 -07001471 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1472 // Prevent adding duplicate callbacks
1473 for (int i = 0; i < mCallbacks.size(); i++) {
1474 if (mCallbacks.get(i).get() == callback) {
1475 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1476 new Exception("Called by"));
1477 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001478 }
1479 }
Jim Miller6212cc02012-09-05 17:35:31 -07001480 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1481 removeCallback(null); // remove unused references
1482 sendUpdates(callback);
1483 }
1484
1485 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1486 // Notify listener of the current state
1487 callback.onRefreshBatteryInfo(mBatteryStatus);
1488 callback.onTimeChanged();
1489 callback.onRingerModeChanged(mRingMode);
1490 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001491 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001492 callback.onClockVisibilityChanged();
Jim Miller52a61332014-11-12 19:29:51 -08001493 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1494 final SimData state = data.getValue();
1495 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1496 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001497 }
1498
Selim Cinek1fcafc42015-07-20 14:39:25 -07001499 public void sendKeyguardReset() {
1500 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1501 }
1502
Adrian Roosb6011622014-05-14 15:52:53 +02001503 /**
1504 * @see #handleKeyguardBouncerChanged(int)
1505 */
1506 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1507 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1508 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1509 message.arg1 = showingBouncer ? 1 : 0;
1510 message.sendToTarget();
1511 }
1512
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001513 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001514 * 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 -08001515 * have the information earlier than waiting for the intent
1516 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001517 *
1518 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1519 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001520 */
Jim Miller52a61332014-11-12 19:29:51 -08001521 public void reportSimUnlocked(int subId) {
1522 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
1523 int slotId = SubscriptionManager.getSlotId(subId);
1524 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001525 }
1526
Brian Colonna7fce3802013-09-17 15:51:32 -04001527 /**
1528 * Report that the emergency call button has been pressed and the emergency dialer is
1529 * about to be displayed.
1530 *
1531 * @param bypassHandler runs immediately.
1532 *
1533 * NOTE: Must be called from UI thread if bypassHandler == true.
1534 */
1535 public void reportEmergencyCallAction(boolean bypassHandler) {
1536 if (!bypassHandler) {
1537 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1538 } else {
1539 handleReportEmergencyCallAction();
1540 }
1541 }
1542
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001543 /**
1544 * @return Whether the device is provisioned (whether they have gone through
1545 * the setup wizard)
1546 */
1547 public boolean isDeviceProvisioned() {
1548 return mDeviceProvisioned;
1549 }
1550
Jorim Jaggi9f743032015-05-04 15:22:40 -07001551 public void clearFailedUnlockAttempts() {
1552 mFailedAttempts.delete(sCurrentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001553 }
1554
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001555 public int getFailedUnlockAttempts(int userId) {
1556 return mFailedAttempts.get(userId, 0);
Jorim Jaggi9f743032015-05-04 15:22:40 -07001557 }
1558
Xiyuan Xiace64cea2016-01-06 08:51:16 -08001559 public void reportFailedStrongAuthUnlockAttempt(int userId) {
1560 mFailedAttempts.put(userId, getFailedUnlockAttempts(userId) + 1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001561 }
1562
Jim Millerf41fc962014-06-18 16:33:51 -07001563 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001564 mUserFingerprintAuthenticated.clear();
Jim Millerf41fc962014-06-18 16:33:51 -07001565 }
1566
Jim Miller52a61332014-11-12 19:29:51 -08001567 public boolean isSimPinVoiceSecure() {
1568 // TODO: only count SIMs that handle voice
1569 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001570 }
1571
1572 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001573 // True if any SIM is pin secure
1574 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1575 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1576 }
1577 return false;
1578 }
1579
Jason Monk9ff69bd2014-12-02 16:43:17 -05001580 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001581 if (mSimDatas.containsKey(subId)) {
1582 return mSimDatas.get(subId).simState;
1583 } else {
1584 return State.UNKNOWN;
1585 }
1586 }
1587
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001588 /**
1589 * @return true if and only if the state has changed for the specified {@code slotId}
1590 */
1591 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08001592
1593 // This is awful. It exists because there are two APIs for getting the SIM status
1594 // that don't return the complete set of values and have different types. In Keyguard we
1595 // need IccCardConstants, but TelephonyManager would only give us
1596 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001597 final TelephonyManager tele = TelephonyManager.from(mContext);
1598 int simState = tele.getSimState(slotId);
Jim Miller52a61332014-11-12 19:29:51 -08001599 State state;
1600 try {
xinhe18b9c3c2014-12-02 15:03:20 -08001601 state = State.intToState(simState);
Jim Miller52a61332014-11-12 19:29:51 -08001602 } catch(IllegalArgumentException ex) {
xinhe18b9c3c2014-12-02 15:03:20 -08001603 Log.w(TAG, "Unknown sim state: " + simState);
Jim Miller52a61332014-11-12 19:29:51 -08001604 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05001605 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001606 SimData data = mSimDatas.get(subId);
1607 final boolean changed;
1608 if (data == null) {
1609 data = new SimData(state, slotId, subId);
1610 mSimDatas.put(subId, data);
1611 changed = true; // no data yet; force update
1612 } else {
1613 changed = data.simState != state;
1614 data.simState = state;
1615 }
1616 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07001617 }
1618
1619 public static boolean isSimPinSecure(IccCardConstants.State state) {
1620 final IccCardConstants.State simState = state;
1621 return (simState == IccCardConstants.State.PIN_REQUIRED
1622 || simState == IccCardConstants.State.PUK_REQUIRED
1623 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001624 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001625
1626 public DisplayClientState getCachedDisplayClientState() {
1627 return mDisplayClientState;
1628 }
Jim Miller20daffd2013-10-07 14:59:53 -07001629
1630 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1631 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001632 public void dispatchStartedWakingUp() {
1633 synchronized (this) {
1634 mDeviceInteractive = true;
1635 }
1636 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1637 }
1638
Jorim Jaggi95e40382015-09-16 15:53:42 -07001639 public void dispatchStartedGoingToSleep(int why) {
1640 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
1641 }
1642
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001643 public void dispatchFinishedGoingToSleep(int why) {
1644 synchronized(this) {
1645 mDeviceInteractive = false;
1646 }
1647 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1648 }
1649
Jim Miller20daffd2013-10-07 14:59:53 -07001650 public void dispatchScreenTurnedOn() {
1651 synchronized (this) {
1652 mScreenOn = true;
1653 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001654 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001655 }
1656
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001657 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001658 synchronized(this) {
1659 mScreenOn = false;
1660 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001661 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001662 }
1663
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001664 public boolean isDeviceInteractive() {
1665 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001666 }
Jim Miller52a61332014-11-12 19:29:51 -08001667
Jorim Jaggi95e40382015-09-16 15:53:42 -07001668 public boolean isGoingToSleep() {
1669 return mGoingToSleep;
1670 }
1671
Jim Miller52a61332014-11-12 19:29:51 -08001672 /**
1673 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1674 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001675 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001676 */
1677 public int getNextSubIdForState(State state) {
1678 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001679 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001680 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1681 for (int i = 0; i < list.size(); i++) {
1682 final SubscriptionInfo info = list.get(i);
1683 final int id = info.getSubscriptionId();
1684 int slotId = SubscriptionManager.getSlotId(id);
1685 if (state == getSimState(id) && bestSlotId > slotId ) {
1686 resultId = id;
1687 bestSlotId = slotId;
1688 }
1689 }
1690 return resultId;
1691 }
1692
1693 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1694 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1695 for (int i = 0; i < list.size(); i++) {
1696 SubscriptionInfo info = list.get(i);
1697 if (subId == info.getSubscriptionId()) return info;
1698 }
1699 return null; // not found
1700 }
Jason Monkab525272015-07-13 17:02:49 -04001701
1702 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1703 pw.println("KeyguardUpdateMonitor state:");
1704 pw.println(" SIM States:");
1705 for (SimData data : mSimDatas.values()) {
1706 pw.println(" " + data.toString());
1707 }
1708 pw.println(" Subs:");
1709 if (mSubscriptionInfo != null) {
1710 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
1711 pw.println(" " + mSubscriptionInfo.get(i));
1712 }
1713 }
1714 pw.println(" Service states:");
1715 for (int subId : mServiceStates.keySet()) {
1716 pw.println(" " + subId + "=" + mServiceStates.get(subId));
1717 }
Jim Millerd72d5ac2015-09-29 18:55:32 -07001718 if (mFpm != null && mFpm.isHardwareDetected()) {
1719 final int userId = ActivityManager.getCurrentUser();
1720 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
1721 pw.println(" Fingerprint state (user=" + userId + ")");
1722 pw.println(" allowed=" + isUnlockingWithFingerprintAllowed());
1723 pw.println(" auth'd=" + mUserFingerprintAuthenticated.get(userId));
1724 pw.println(" authSinceBoot="
1725 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
1726 pw.println(" disabled(DPM)=" + isFingerprintDisabled(userId));
1727 pw.println(" possible=" + isUnlockWithFingerprintPossible(userId));
1728 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
1729 pw.println(" timedout=" + hasFingerprintUnlockTimedOut(userId));
1730 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
1731 }
Jason Monkab525272015-07-13 17:02:49 -04001732 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001733}