blob: fdabf9f49c258cd7060c9d755a781c725c5696b4 [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
609 public void onReceive(Context context, Intent intent) {
610 final String action = intent.getAction();
611 if (DEBUG) Log.d(TAG, "received broadcast " + action);
612
613 if (Intent.ACTION_TIME_TICK.equals(action)
614 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200615 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700616 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700617 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
618 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
619 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
620 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
621 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -0800622
623 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
624 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
625 final int maxChargingMicroWatt;
626
627 if (maxChargingMicroVolt <= 0) {
628 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
629 }
630 if (maxChargingMicroAmp > 0) {
631 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
632 // to maintain precision equally on both factors.
633 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
634 * (maxChargingMicroVolt / 1000);
635 } else {
636 maxChargingMicroWatt = -1;
637 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700638 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700639 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -0800640 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -0700641 mHandler.sendMessage(msg);
642 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Jim Miller52a61332014-11-12 19:29:51 -0800643 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700644 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800645 Log.v(TAG, "action " + action
646 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
647 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700648 }
Jim Miller52a61332014-11-12 19:29:51 -0800649 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
650 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700651 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
652 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
653 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
654 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
655 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
656 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400657 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
658 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800659 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700660 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700661 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
662 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
663 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
664 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
665 if (DEBUG) {
666 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
667 + subId);
668 }
669 mHandler.sendMessage(
670 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Jim Millerbbf1a742012-07-17 18:30:30 -0700671 }
672 }
673 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700674
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700675 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
676
677 public void onReceive(Context context, Intent intent) {
678 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200679 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
680 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
681 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700682 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
683 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200684 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
685 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
686 getSendingUserId()));
687 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
688 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
689 getSendingUserId()));
690 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
691 .equals(action)) {
692 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700693 }
694 }
695 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700696
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700697 private final BroadcastReceiver mStrongAuthTimeoutReceiver = new BroadcastReceiver() {
698 @Override
699 public void onReceive(Context context, Intent intent) {
700 if (ACTION_STRONG_AUTH_TIMEOUT.equals(intent.getAction())) {
701 int userId = intent.getIntExtra(USER_ID, -1);
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700702 mStrongAuthNotTimedOut.remove(userId);
703 notifyStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700704 }
705 }
706 };
707
Jorim Jaggi3a464782015-08-28 16:59:13 -0700708 private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
709 = new FingerprintManager.LockoutResetCallback() {
710 @Override
711 public void onLockoutReset() {
712 handleFingerprintLockoutReset();
713 }
714 };
715
Jim Miller9f0753f2015-03-23 23:59:22 -0700716 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
717 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700718
719 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700720 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700721 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700722 };
723
724 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700725 public void onAuthenticationSucceeded(AuthenticationResult result) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700726 handleFingerprintAuthenticated();
Jim Millerf41fc962014-06-18 16:33:51 -0700727 }
728
729 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700730 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700731 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700732 }
733
734 @Override
735 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700736 handleFingerprintError(errMsgId, errString.toString());
737 }
738
739 @Override
740 public void onAuthenticationAcquired(int acquireInfo) {
741 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700742 }
743 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700744 private CancellationSignal mFingerprintCancelSignal;
745 private FingerprintManager mFpm;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700746
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800747 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800748 * When we receive a
749 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700750 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800751 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800752 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800753 */
Jim Miller52a61332014-11-12 19:29:51 -0800754 private static class SimData {
755 public State simState;
756 public int slotId;
757 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800758
Jim Miller52a61332014-11-12 19:29:51 -0800759 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800760 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800761 slotId = slot;
762 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800763 }
764
Jim Miller52a61332014-11-12 19:29:51 -0800765 static SimData fromIntent(Intent intent) {
766 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800767 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
768 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
769 }
Wink Savillea639b312012-07-10 12:37:54 -0700770 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800771 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
772 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800773 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700774 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700775 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700776 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700777
Wink Savillea639b312012-07-10 12:37:54 -0700778 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700779 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700780 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700781 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700782 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700783 }
Wink Savillea639b312012-07-10 12:37:54 -0700784 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
785 state = IccCardConstants.State.READY;
786 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800787 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700788 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
789 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
790 state = IccCardConstants.State.PIN_REQUIRED;
791 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
792 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800793 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700794 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800795 }
Wink Savillea639b312012-07-10 12:37:54 -0700796 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
797 state = IccCardConstants.State.NETWORK_LOCKED;
Jim Miller109f1fd2012-09-19 20:44:16 -0700798 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
799 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
800 // This is required because telephony doesn't return to "READY" after
801 // these state transitions. See bug 7197471.
802 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800803 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700804 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800805 }
Jim Miller52a61332014-11-12 19:29:51 -0800806 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800807 }
808
809 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800810 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800811 }
812 }
813
Adrian Roos12c1ef52014-06-04 13:54:08 +0200814 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700815 public static final int CHARGING_UNKNOWN = -1;
816 public static final int CHARGING_SLOWLY = 0;
817 public static final int CHARGING_REGULAR = 1;
818 public static final int CHARGING_FAST = 2;
819
Jim Miller16464b82011-10-20 21:10:13 -0700820 public final int status;
821 public final int level;
822 public final int plugged;
823 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800824 public final int maxChargingWattage;
825 public BatteryStatus(int status, int level, int plugged, int health,
826 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -0700827 this.status = status;
828 this.level = level;
829 this.plugged = plugged;
830 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800831 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -0700832 }
833
Jim Millerbbf1a742012-07-17 18:30:30 -0700834 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700835 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700836 * @return true if the device is plugged in.
837 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100838 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700839 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700840 || plugged == BatteryManager.BATTERY_PLUGGED_USB
841 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -0700842 }
843
844 /**
845 * Whether or not the device is charged. Note that some devices never return 100% for
846 * battery level, so this allows either battery level or status to determine if the
847 * battery is charged.
848 * @return true if the device is charged
849 */
850 public boolean isCharged() {
851 return status == BATTERY_STATUS_FULL || level >= 100;
852 }
853
854 /**
855 * Whether battery is low and needs to be charged.
856 * @return true if battery is low
857 */
858 public boolean isBatteryLow() {
859 return level < LOW_BATTERY_THRESHOLD;
860 }
861
Adrian Roos7b043112015-07-10 13:00:33 -0700862 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -0800863 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
864 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
865 maxChargingWattage > fastThreshold ? CHARGING_FAST :
Adrian Roos7b043112015-07-10 13:00:33 -0700866 CHARGING_REGULAR;
867 }
Jim Miller16464b82011-10-20 21:10:13 -0700868 }
869
Adrian Roosb5e47222015-08-14 15:53:06 -0700870 public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
871
872 public boolean isUnlockingWithFingerprintAllowed() {
873 int userId = getCurrentUser();
874 return isFingerprintAllowedForUser(userId);
875 }
876
877 public boolean hasUserAuthenticatedSinceBoot() {
878 int userId = getCurrentUser();
879 return (getStrongAuthForUser(userId)
880 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
881 }
882
883 @Override
884 public void onStrongAuthRequiredChanged(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700885 notifyStrongAuthStateChanged(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -0700886 }
887 }
888
Jim Millerdcb3d842012-08-23 19:18:12 -0700889 public static KeyguardUpdateMonitor getInstance(Context context) {
890 if (sInstance == null) {
891 sInstance = new KeyguardUpdateMonitor(context);
892 }
893 return sInstance;
894 }
895
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700896 protected void handleStartedWakingUp() {
Jorim Jaggi864e64b2015-05-20 14:13:23 -0700897 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700898 final int count = mCallbacks.size();
899 for (int i = 0; i < count; i++) {
900 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
901 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700902 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -0700903 }
904 }
905 }
906
Jorim Jaggi95e40382015-09-16 15:53:42 -0700907 protected void handleStartedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -0700908 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -0700909 final int count = mCallbacks.size();
910 for (int i = 0; i < count; i++) {
911 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
912 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -0700913 cb.onStartedGoingToSleep(arg1);
914 }
915 }
916 mGoingToSleep = true;
917 mFingerprintAlreadyAuthenticated = false;
918 updateFingerprintListeningState();
919 }
920
921 protected void handleFinishedGoingToSleep(int arg1) {
922 mGoingToSleep = false;
923 final int count = mCallbacks.size();
924 for (int i = 0; i < count; i++) {
925 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
926 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700927 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700928 }
929 }
Jorim Jaggiea657062015-04-28 13:45:11 -0700930 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700931 }
932
Jorim Jaggif1518da2015-07-30 11:56:36 -0700933 private void handleScreenTurnedOn() {
934 final int count = mCallbacks.size();
935 for (int i = 0; i < count; i++) {
936 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
937 if (cb != null) {
938 cb.onScreenTurnedOn();
939 }
940 }
941 }
942
943 private void handleScreenTurnedOff() {
944 final int count = mCallbacks.size();
945 for (int i = 0; i < count; i++) {
946 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
947 if (cb != null) {
948 cb.onScreenTurnedOff();
949 }
950 }
951 }
952
Adam Powell43a372f2013-09-27 17:43:53 -0700953 /**
954 * IMPORTANT: Must be called from UI thread.
955 */
956 public void dispatchSetBackground(Bitmap bmp) {
957 if (DEBUG) Log.d(TAG, "dispatchSetBackground");
958 final int count = mCallbacks.size();
959 for (int i = 0; i < count; i++) {
960 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
961 if (cb != null) {
962 cb.onSetBackground(bmp);
963 }
964 }
965 }
966
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700967 private void handleUserInfoChanged(int userId) {
968 for (int i = 0; i < mCallbacks.size(); i++) {
969 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
970 if (cb != null) {
971 cb.onUserInfoChanged(userId);
972 }
973 }
974 }
975
Jim Millerdcb3d842012-08-23 19:18:12 -0700976 private KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800977 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800978 mSubscriptionManager = SubscriptionManager.from(context);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700979 mAlarmManager = context.getSystemService(AlarmManager.class);
Michael Jurkafff56142012-11-28 16:51:00 -0800980 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700981
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800982 // Since device can't be un-provisioned, we only need to register a content observer
983 // to update mDeviceProvisioned when we are...
984 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700985 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800986 }
Jim Miller47088bb2009-11-24 00:40:16 -0800987
Jim Millerbbf1a742012-07-17 18:30:30 -0700988 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -0700989 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800990
Jim Millerbbf1a742012-07-17 18:30:30 -0700991 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800992 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800993 filter.addAction(Intent.ACTION_TIME_TICK);
994 filter.addAction(Intent.ACTION_TIME_CHANGED);
995 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
996 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -0400997 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800998 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -0700999 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001000 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001001 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Jim Millerbbf1a742012-07-17 18:30:30 -07001002 context.registerReceiver(mBroadcastReceiver, filter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001003
Adam Cohenc276e822012-11-08 13:01:08 -08001004 final IntentFilter bootCompleteFilter = new IntentFilter();
1005 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1006 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
1007 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter);
1008
Adrian Roos48c796c2014-09-01 14:59:23 +02001009 final IntentFilter allUserFilter = new IntentFilter();
1010 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1011 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1012 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1013 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1014 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
1015 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001016 null, null);
1017
Wink Saville071743f2015-01-12 17:11:04 -08001018 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001019 try {
1020 ActivityManagerNative.getDefault().registerUserSwitchObserver(
1021 new IUserSwitchObserver.Stub() {
1022 @Override
1023 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001024 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001025 newUserId, 0, reply));
1026 }
1027 @Override
1028 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001029 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001030 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001031 }
Kenny Guy42979622015-04-13 18:03:05 +00001032 @Override
1033 public void onForegroundProfileSwitch(int newProfileId) {
1034 // Ignore.
1035 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001036 });
1037 } catch (RemoteException e) {
1038 // TODO Auto-generated catch block
1039 e.printStackTrace();
1040 }
Adrian Roos46842d92014-03-27 14:58:03 +01001041
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001042 IntentFilter strongAuthTimeoutFilter = new IntentFilter();
1043 strongAuthTimeoutFilter.addAction(ACTION_STRONG_AUTH_TIMEOUT);
1044 context.registerReceiver(mStrongAuthTimeoutReceiver, strongAuthTimeoutFilter,
1045 PERMISSION_SELF, null /* handler */);
Jorim Jaggi237b0612015-05-01 14:28:49 -07001046 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1047 mTrustManager.registerTrustListener(this);
Adrian Roosb5e47222015-08-14 15:53:06 -07001048 new LockPatternUtils(context).registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001049
Jim Miller9f0753f2015-03-23 23:59:22 -07001050 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
Jorim Jaggiea657062015-04-28 13:45:11 -07001051 updateFingerprintListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001052 if (mFpm != null) {
1053 mFpm.addLockoutResetCallback(mLockoutResetCallback);
1054 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001055 }
1056
1057 private void updateFingerprintListeningState() {
1058 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001059 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001060 stopListeningForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001061 } else if (mFingerprintRunningState != FINGERPRINT_STATE_RUNNING
1062 && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001063 startListeningForFingerprint();
1064 }
1065 }
1066
1067 private boolean shouldListenForFingerprint() {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001068 return (mKeyguardIsVisible || !mDeviceInteractive || mBouncer || mGoingToSleep)
1069 && !mSwitchingUser && !mFingerprintAlreadyAuthenticated
1070 && !isFingerprintDisabled(getCurrentUser());
Jim Miller9f0753f2015-03-23 23:59:22 -07001071 }
1072
Jim Millerce7eb6d2015-04-03 19:29:13 -07001073 private void startListeningForFingerprint() {
Jorim Jaggi86bed402015-08-20 18:20:02 -07001074 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING) {
1075 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING_RESTARTING);
1076 return;
1077 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001078 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001079 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001080 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001081 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001082 mFingerprintCancelSignal.cancel();
1083 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001084 mFingerprintCancelSignal = new CancellationSignal();
Jim Millerf501b582015-06-03 16:36:31 -07001085 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null, userId);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001086 setFingerprintRunningState(FINGERPRINT_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001087 }
1088 }
1089
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001090 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001091 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1092 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1093 }
1094
Jorim Jaggiea657062015-04-28 13:45:11 -07001095 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001096 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Jorim Jaggi86bed402015-08-20 18:20:02 -07001097 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001098 mFingerprintCancelSignal.cancel();
Jim Millerce7eb6d2015-04-03 19:29:13 -07001099 mFingerprintCancelSignal = null;
Jorim Jaggi86bed402015-08-20 18:20:02 -07001100 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001101 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001102 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
1103 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
1104 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001105 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001106
Michael Jurkafff56142012-11-28 16:51:00 -08001107 private boolean isDeviceProvisionedInSettingsDb() {
1108 return Settings.Global.getInt(mContext.getContentResolver(),
1109 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1110 }
1111
Jim Millerbbf1a742012-07-17 18:30:30 -07001112 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001113 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001114 @Override
1115 public void onChange(boolean selfChange) {
1116 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001117 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001118 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001119 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001120 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001121 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001122 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001123 };
1124
1125 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001126 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001127 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001128
1129 // prevent a race condition between where we check the flag and where we register the
1130 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001131 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001132 if (provisioned != mDeviceProvisioned) {
1133 mDeviceProvisioned = provisioned;
1134 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001135 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001136 }
1137 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001138 }
1139
Jim Millerbbf1a742012-07-17 18:30:30 -07001140 /**
1141 * Handle {@link #MSG_DPM_STATE_CHANGED}
1142 */
Jim Millerb0304762012-03-13 20:01:25 -07001143 protected void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001144 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001145 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1146 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1147 if (cb != null) {
1148 cb.onDevicePolicyManagerStateChanged();
1149 }
Jim Millerb0304762012-03-13 20:01:25 -07001150 }
1151 }
1152
Jim Millerbbf1a742012-07-17 18:30:30 -07001153 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001154 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001155 */
Chris Wrenf41c61b2012-11-29 15:19:54 -05001156 protected void handleUserSwitching(int userId, IRemoteCallback reply) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001157 mSwitchingUser = true;
1158 updateFingerprintListeningState();
1159
Jim Millerbbf1a742012-07-17 18:30:30 -07001160 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001161 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1162 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001163 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001164 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001165 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001166 try {
1167 reply.sendResult(null);
1168 } catch (RemoteException e) {
1169 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001170 }
1171
Jim Millerbbf1a742012-07-17 18:30:30 -07001172 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001173 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1174 */
1175 protected void handleUserSwitchComplete(int userId) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001176 mSwitchingUser = false;
1177 updateFingerprintListeningState();
1178
Chris Wrenf41c61b2012-11-29 15:19:54 -05001179 for (int i = 0; i < mCallbacks.size(); i++) {
1180 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1181 if (cb != null) {
1182 cb.onUserSwitchComplete(userId);
1183 }
1184 }
1185 }
1186
1187 /**
Jim Miller90873d52013-09-26 18:11:38 -07001188 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1189 * keyguard crashes sometime after boot, then it will never receive this
1190 * broadcast and hence not handle the event. This method is ultimately called by
1191 * PhoneWindowManager in this case.
1192 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001193 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001194 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001195 }
1196
1197 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001198 * Handle {@link #MSG_BOOT_COMPLETED}
1199 */
1200 protected void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001201 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001202 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001203 for (int i = 0; i < mCallbacks.size(); i++) {
1204 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1205 if (cb != null) {
1206 cb.onBootCompleted();
1207 }
1208 }
1209 }
1210
1211 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001212 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001213 * destroyed.
1214 */
1215 public boolean hasBootCompleted() {
1216 return mBootCompleted;
1217 }
1218
1219 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001220 * Handle {@link #MSG_DEVICE_PROVISIONED}
1221 */
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001222 protected void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001223 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001224 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1225 if (cb != null) {
1226 cb.onDeviceProvisioned();
1227 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001228 }
Michael Jurkafff56142012-11-28 16:51:00 -08001229 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001230 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001231 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1232 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001233 }
1234 }
1235
Jim Millerbbf1a742012-07-17 18:30:30 -07001236 /**
1237 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1238 */
Jim Millerc23024d2010-02-24 15:37:00 -08001239 protected void handlePhoneStateChanged(String newState) {
1240 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001241 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1242 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1243 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1244 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1245 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1246 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1247 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001248 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001249 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1250 if (cb != null) {
1251 cb.onPhoneStateChanged(mPhoneState);
1252 }
Jim Millerc23024d2010-02-24 15:37:00 -08001253 }
1254 }
1255
Jim Millerbbf1a742012-07-17 18:30:30 -07001256 /**
1257 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1258 */
Jim Miller47088bb2009-11-24 00:40:16 -08001259 protected void handleRingerModeChange(int mode) {
1260 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001261 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001262 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001263 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1264 if (cb != null) {
1265 cb.onRingerModeChanged(mode);
1266 }
Jim Miller47088bb2009-11-24 00:40:16 -08001267 }
1268 }
1269
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001270 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001271 * Handle {@link #MSG_TIME_UPDATE}
1272 */
1273 private void handleTimeUpdate() {
1274 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001275 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001276 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1277 if (cb != null) {
1278 cb.onTimeChanged();
1279 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001280 }
1281 }
1282
1283 /**
1284 * Handle {@link #MSG_BATTERY_UPDATE}
1285 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001286 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001287 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001288 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1289 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001290 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001291 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001292 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1293 if (cb != null) {
1294 cb.onRefreshBatteryInfo(status);
1295 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001296 }
1297 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001298 }
1299
1300 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001301 * Handle {@link #MSG_SIM_STATE_CHANGE}
1302 */
Jim Miller52a61332014-11-12 19:29:51 -08001303 private void handleSimStateChange(int subId, int slotId, State state) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001304
Jim Miller52a61332014-11-12 19:29:51 -08001305 if (DEBUG_SIM_STATES) {
1306 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1307 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001308 }
1309
Wink Savillea54bf652014-12-11 13:37:50 -08001310 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001311 Log.w(TAG, "invalid subId in handleSimStateChange()");
1312 return;
1313 }
1314
1315 SimData data = mSimDatas.get(subId);
1316 final boolean changed;
1317 if (data == null) {
1318 data = new SimData(state, slotId, subId);
1319 mSimDatas.put(subId, data);
1320 changed = true; // no data yet; force update
1321 } else {
1322 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1323 data.simState = state;
1324 data.subId = subId;
1325 data.slotId = slotId;
1326 }
1327 if (changed && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001328 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001329 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1330 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08001331 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001332 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001333 }
1334 }
1335 }
1336
Jim Millerbbf1a742012-07-17 18:30:30 -07001337 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001338 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1339 */
1340 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1341 if (DEBUG) {
1342 Log.d(TAG,
1343 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1344 }
1345
1346 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1347 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1348 return;
1349 }
1350
1351 mServiceStates.put(subId, serviceState);
1352
1353 for (int j = 0; j < mCallbacks.size(); j++) {
1354 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1355 if (cb != null) {
1356 cb.onRefreshCarrierInfo();
1357 }
1358 }
1359 }
1360
1361 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001362 * Notifies that the visibility state of Keyguard has changed.
1363 *
1364 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001365 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001366 public void onKeyguardVisibilityChanged(boolean showing) {
1367 if (DEBUG) Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
1368 mKeyguardIsVisible = showing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001369 for (int i = 0; i < mCallbacks.size(); i++) {
1370 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1371 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001372 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001373 }
1374 }
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001375 if (!showing) {
Jorim Jaggi71448a72015-08-18 19:49:04 -07001376 mFingerprintAlreadyAuthenticated = false;
1377 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001378 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001379 }
1380
Brian Colonna7fce3802013-09-17 15:51:32 -04001381 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001382 * Handle {@link #MSG_KEYGUARD_RESET}
1383 */
1384 private void handleKeyguardReset() {
1385 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Adrian Roosf6d51ac2015-09-02 13:26:25 -07001386 updateFingerprintListeningState();
Selim Cinek1fcafc42015-07-20 14:39:25 -07001387 }
1388
1389 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001390 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1391 * @see #sendKeyguardBouncerChanged(boolean)
1392 */
1393 private void handleKeyguardBouncerChanged(int bouncer) {
1394 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1395 boolean isBouncer = (bouncer == 1);
1396 mBouncer = isBouncer;
1397 for (int i = 0; i < mCallbacks.size(); i++) {
1398 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1399 if (cb != null) {
1400 cb.onKeyguardBouncerChanged(isBouncer);
1401 }
1402 }
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001403 updateFingerprintListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02001404 }
1405
1406 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001407 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1408 */
1409 private void handleReportEmergencyCallAction() {
1410 for (int i = 0; i < mCallbacks.size(); i++) {
1411 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1412 if (cb != null) {
1413 cb.onEmergencyCallAction();
1414 }
1415 }
1416 }
1417
Jim Miller16464b82011-10-20 21:10:13 -07001418 private static boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001419 final boolean nowPluggedIn = current.isPluggedIn();
1420 final boolean wasPluggedIn = old.isPluggedIn();
Jim Miller79a444a2011-02-15 15:02:11 -08001421 final boolean stateChangedWhilePluggedIn =
Jim Miller16464b82011-10-20 21:10:13 -07001422 wasPluggedIn == true && nowPluggedIn == true
1423 && (old.status != current.status);
1424
1425 // change in plug state is always interesting
1426 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001427 return true;
1428 }
1429
1430 // change in battery level while plugged in
Jim Miller16464b82011-10-20 21:10:13 -07001431 if (nowPluggedIn && old.level != current.level) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001432 return true;
1433 }
1434
Jim Miller16464b82011-10-20 21:10:13 -07001435 // change where battery needs charging
Jim Millerbbf1a742012-07-17 18:30:30 -07001436 if (!nowPluggedIn && current.isBatteryLow() && current.level != old.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001437 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001438 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001439
1440 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08001441 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07001442 return true;
1443 }
1444
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001445 return false;
1446 }
1447
1448 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001449 * Remove the given observer's callback.
1450 *
Jim Miller6212cc02012-09-05 17:35:31 -07001451 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001452 */
Jim Miller6212cc02012-09-05 17:35:31 -07001453 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
1454 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1455 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1456 if (mCallbacks.get(i).get() == callback) {
1457 mCallbacks.remove(i);
1458 }
1459 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001460 }
1461
1462 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001463 * Register to receive notifications about general keyguard information
1464 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001465 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001466 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001467 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Jim Miller6212cc02012-09-05 17:35:31 -07001468 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1469 // Prevent adding duplicate callbacks
1470 for (int i = 0; i < mCallbacks.size(); i++) {
1471 if (mCallbacks.get(i).get() == callback) {
1472 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1473 new Exception("Called by"));
1474 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001475 }
1476 }
Jim Miller6212cc02012-09-05 17:35:31 -07001477 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1478 removeCallback(null); // remove unused references
1479 sendUpdates(callback);
1480 }
1481
1482 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1483 // Notify listener of the current state
1484 callback.onRefreshBatteryInfo(mBatteryStatus);
1485 callback.onTimeChanged();
1486 callback.onRingerModeChanged(mRingMode);
1487 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001488 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001489 callback.onClockVisibilityChanged();
Jim Miller52a61332014-11-12 19:29:51 -08001490 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1491 final SimData state = data.getValue();
1492 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1493 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001494 }
1495
Selim Cinek1fcafc42015-07-20 14:39:25 -07001496 public void sendKeyguardReset() {
1497 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1498 }
1499
Adrian Roosb6011622014-05-14 15:52:53 +02001500 /**
1501 * @see #handleKeyguardBouncerChanged(int)
1502 */
1503 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1504 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1505 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1506 message.arg1 = showingBouncer ? 1 : 0;
1507 message.sendToTarget();
1508 }
1509
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001510 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001511 * 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 -08001512 * have the information earlier than waiting for the intent
1513 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001514 *
1515 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1516 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001517 */
Jim Miller52a61332014-11-12 19:29:51 -08001518 public void reportSimUnlocked(int subId) {
1519 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
1520 int slotId = SubscriptionManager.getSlotId(subId);
1521 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001522 }
1523
Brian Colonna7fce3802013-09-17 15:51:32 -04001524 /**
1525 * Report that the emergency call button has been pressed and the emergency dialer is
1526 * about to be displayed.
1527 *
1528 * @param bypassHandler runs immediately.
1529 *
1530 * NOTE: Must be called from UI thread if bypassHandler == true.
1531 */
1532 public void reportEmergencyCallAction(boolean bypassHandler) {
1533 if (!bypassHandler) {
1534 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1535 } else {
1536 handleReportEmergencyCallAction();
1537 }
1538 }
1539
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001540 /**
1541 * @return Whether the device is provisioned (whether they have gone through
1542 * the setup wizard)
1543 */
1544 public boolean isDeviceProvisioned() {
1545 return mDeviceProvisioned;
1546 }
1547
Jorim Jaggi9f743032015-05-04 15:22:40 -07001548 public void clearFailedUnlockAttempts() {
1549 mFailedAttempts.delete(sCurrentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001550 }
1551
Jorim Jaggi9f743032015-05-04 15:22:40 -07001552 public int getFailedUnlockAttempts() {
1553 return mFailedAttempts.get(sCurrentUser, 0);
1554 }
1555
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001556 public void reportFailedStrongAuthUnlockAttempt() {
Jorim Jaggi9f743032015-05-04 15:22:40 -07001557 mFailedAttempts.put(sCurrentUser, getFailedUnlockAttempts() + 1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001558 }
1559
Jim Millerf41fc962014-06-18 16:33:51 -07001560 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001561 mUserFingerprintAuthenticated.clear();
Jim Millerf41fc962014-06-18 16:33:51 -07001562 }
1563
Jim Miller52a61332014-11-12 19:29:51 -08001564 public boolean isSimPinVoiceSecure() {
1565 // TODO: only count SIMs that handle voice
1566 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001567 }
1568
1569 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001570 // True if any SIM is pin secure
1571 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1572 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1573 }
1574 return false;
1575 }
1576
Jason Monk9ff69bd2014-12-02 16:43:17 -05001577 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001578 if (mSimDatas.containsKey(subId)) {
1579 return mSimDatas.get(subId).simState;
1580 } else {
1581 return State.UNKNOWN;
1582 }
1583 }
1584
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001585 /**
1586 * @return true if and only if the state has changed for the specified {@code slotId}
1587 */
1588 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08001589
1590 // This is awful. It exists because there are two APIs for getting the SIM status
1591 // that don't return the complete set of values and have different types. In Keyguard we
1592 // need IccCardConstants, but TelephonyManager would only give us
1593 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001594 final TelephonyManager tele = TelephonyManager.from(mContext);
1595 int simState = tele.getSimState(slotId);
Jim Miller52a61332014-11-12 19:29:51 -08001596 State state;
1597 try {
xinhe18b9c3c2014-12-02 15:03:20 -08001598 state = State.intToState(simState);
Jim Miller52a61332014-11-12 19:29:51 -08001599 } catch(IllegalArgumentException ex) {
xinhe18b9c3c2014-12-02 15:03:20 -08001600 Log.w(TAG, "Unknown sim state: " + simState);
Jim Miller52a61332014-11-12 19:29:51 -08001601 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05001602 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001603 SimData data = mSimDatas.get(subId);
1604 final boolean changed;
1605 if (data == null) {
1606 data = new SimData(state, slotId, subId);
1607 mSimDatas.put(subId, data);
1608 changed = true; // no data yet; force update
1609 } else {
1610 changed = data.simState != state;
1611 data.simState = state;
1612 }
1613 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07001614 }
1615
1616 public static boolean isSimPinSecure(IccCardConstants.State state) {
1617 final IccCardConstants.State simState = state;
1618 return (simState == IccCardConstants.State.PIN_REQUIRED
1619 || simState == IccCardConstants.State.PUK_REQUIRED
1620 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001621 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001622
1623 public DisplayClientState getCachedDisplayClientState() {
1624 return mDisplayClientState;
1625 }
Jim Miller20daffd2013-10-07 14:59:53 -07001626
1627 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1628 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001629 public void dispatchStartedWakingUp() {
1630 synchronized (this) {
1631 mDeviceInteractive = true;
1632 }
1633 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1634 }
1635
Jorim Jaggi95e40382015-09-16 15:53:42 -07001636 public void dispatchStartedGoingToSleep(int why) {
1637 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
1638 }
1639
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001640 public void dispatchFinishedGoingToSleep(int why) {
1641 synchronized(this) {
1642 mDeviceInteractive = false;
1643 }
1644 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1645 }
1646
Jim Miller20daffd2013-10-07 14:59:53 -07001647 public void dispatchScreenTurnedOn() {
1648 synchronized (this) {
1649 mScreenOn = true;
1650 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001651 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001652 }
1653
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001654 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001655 synchronized(this) {
1656 mScreenOn = false;
1657 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001658 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001659 }
1660
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001661 public boolean isDeviceInteractive() {
1662 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001663 }
Jim Miller52a61332014-11-12 19:29:51 -08001664
Jorim Jaggi95e40382015-09-16 15:53:42 -07001665 public boolean isGoingToSleep() {
1666 return mGoingToSleep;
1667 }
1668
Jim Miller52a61332014-11-12 19:29:51 -08001669 /**
1670 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1671 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001672 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001673 */
1674 public int getNextSubIdForState(State state) {
1675 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001676 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001677 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1678 for (int i = 0; i < list.size(); i++) {
1679 final SubscriptionInfo info = list.get(i);
1680 final int id = info.getSubscriptionId();
1681 int slotId = SubscriptionManager.getSlotId(id);
1682 if (state == getSimState(id) && bestSlotId > slotId ) {
1683 resultId = id;
1684 bestSlotId = slotId;
1685 }
1686 }
1687 return resultId;
1688 }
1689
1690 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1691 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1692 for (int i = 0; i < list.size(); i++) {
1693 SubscriptionInfo info = list.get(i);
1694 if (subId == info.getSubscriptionId()) return info;
1695 }
1696 return null; // not found
1697 }
Jason Monkab525272015-07-13 17:02:49 -04001698
1699 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1700 pw.println("KeyguardUpdateMonitor state:");
1701 pw.println(" SIM States:");
1702 for (SimData data : mSimDatas.values()) {
1703 pw.println(" " + data.toString());
1704 }
1705 pw.println(" Subs:");
1706 if (mSubscriptionInfo != null) {
1707 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
1708 pw.println(" " + mSubscriptionInfo.get(i));
1709 }
1710 }
1711 pw.println(" Service states:");
1712 for (int subId : mServiceStates.keySet()) {
1713 pw.println(" " + subId + "=" + mServiceStates.get(subId));
1714 }
1715 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001716}