blob: 47d8e283257b045cdb0c9e41a100903df8f11061 [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;
78import static android.os.BatteryManager.EXTRA_PLUGGED;
79import static android.os.BatteryManager.EXTRA_STATUS;
80
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080081/**
82 * Watches for updates that may be interesting to the keyguard, and provides
83 * the up to date information as well as a registration for callbacks that care
84 * to be updated.
85 *
86 * Note: under time crunch, this has been extended to include some stuff that
87 * doesn't really belong here. see {@link #handleBatteryUpdate} where it shutdowns
Jim Miller258341c2012-08-30 16:50:10 -070088 * the device, and {@link #getFailedUnlockAttempts()}, {@link #reportFailedAttempt()}
89 * and {@link #clearFailedUnlockAttempts()}. Maybe we should rename this 'KeyguardContext'...
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080090 */
Adrian Roos46842d92014-03-27 14:58:03 +010091public class KeyguardUpdateMonitor implements TrustManager.TrustListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080092
Jim Millerbbf1a742012-07-17 18:30:30 -070093 private static final String TAG = "KeyguardUpdateMonitor";
Jorim Jaggi5cf17872014-03-26 18:31:48 +010094 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller52a61332014-11-12 19:29:51 -080095 private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
Jim Millerbbf1a742012-07-17 18:30:30 -070096 private static final int LOW_BATTERY_THRESHOLD = 20;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080097
Jorim Jaggie7b12522014-08-06 16:41:21 +020098 private static final String ACTION_FACE_UNLOCK_STARTED
99 = "com.android.facelock.FACE_UNLOCK_STARTED";
100 private static final String ACTION_FACE_UNLOCK_STOPPED
101 = "com.android.facelock.FACE_UNLOCK_STOPPED";
102
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700103 private static final String ACTION_STRONG_AUTH_TIMEOUT =
104 "com.android.systemui.ACTION_STRONG_AUTH_TIMEOUT";
105 private static final String USER_ID = "com.android.systemui.USER_ID";
106
107 private static final String PERMISSION_SELF = "com.android.systemui.permission.SELF";
108
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700109 /**
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700110 * Milliseconds after unlocking with fingerprint times out, i.e. the user has to use a
111 * strong auth method like password, PIN or pattern.
112 */
113 private static final long FINGERPRINT_UNLOCK_TIMEOUT_MS = 72 * 60 * 60 * 1000;
114
Jim Millerbbf1a742012-07-17 18:30:30 -0700115 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800116 private static final int MSG_TIME_UPDATE = 301;
117 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800118 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800119 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800120 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700121 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700122 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500123 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700124 private static final int MSG_KEYGUARD_VISIBILITY_CHANGED = 311;
125 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;
Adrian Roosb6011622014-05-14 15:52:53 +0200132 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700133 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
134 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400135 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700136 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700137 private static final int MSG_SCREEN_TURNED_ON = 331;
138 private static final int MSG_SCREEN_TURNED_OFF = 332;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800139
Jorim Jaggi86bed402015-08-20 18:20:02 -0700140 /** Fingerprint state: Not listening to fingerprint. */
141 private static final int FINGERPRINT_STATE_STOPPED = 0;
142
143 /** Fingerprint state: Listening. */
144 private static final int FINGERPRINT_STATE_RUNNING = 1;
145
146 /**
147 * Fingerprint state: Cancelling and waiting for the confirmation from FingerprintService to
148 * send us the confirmation that cancellation has happened.
149 */
150 private static final int FINGERPRINT_STATE_CANCELLING = 2;
151
152 /**
153 * Fingerprint state: During cancelling we got another request to start listening, so when we
154 * receive the cancellation done signal, we should start listening again.
155 */
156 private static final int FINGERPRINT_STATE_CANCELLING_RESTARTING = 3;
157
Jim Millerdcb3d842012-08-23 19:18:12 -0700158 private static KeyguardUpdateMonitor sInstance;
159
Jim Millerbbf1a742012-07-17 18:30:30 -0700160 private final Context mContext;
Jim Miller52a61332014-11-12 19:29:51 -0800161 HashMap<Integer, SimData> mSimDatas = new HashMap<Integer, SimData>();
Etan Cohen47051d82015-07-06 16:19:04 -0700162 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700163
Jim Millerbbf1a742012-07-17 18:30:30 -0700164 private int mRingMode;
165 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400166 private boolean mKeyguardIsVisible;
Jorim Jaggi71448a72015-08-18 19:49:04 -0700167
168 /**
169 * If true, fingerprint was already authenticated and we don't need to start listening again
170 * until the Keyguard has been dismissed.
171 */
172 private boolean mFingerprintAlreadyAuthenticated;
Adrian Roosb6011622014-05-14 15:52:53 +0200173 private boolean mBouncer;
Adam Cohen4eb36cf2012-11-07 11:45:30 -0800174 private boolean mBootCompleted;
Jim Millerbbf1a742012-07-17 18:30:30 -0700175
Jim Millerdcb3d842012-08-23 19:18:12 -0700176 // Device provisioning state
Jim Millerbbf1a742012-07-17 18:30:30 -0700177 private boolean mDeviceProvisioned;
178
Jim Millerdcb3d842012-08-23 19:18:12 -0700179 // Battery status
Jim Millerbbf1a742012-07-17 18:30:30 -0700180 private BatteryStatus mBatteryStatus;
181
Jim Millerdcb3d842012-08-23 19:18:12 -0700182 // Password attempts
Jorim Jaggi9f743032015-05-04 15:22:40 -0700183 private SparseIntArray mFailedAttempts = new SparseIntArray();
Brian Colonnacc4104f2012-10-09 17:50:46 -0400184
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700185 /** Tracks whether strong authentication hasn't been used since quite some time per user. */
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700186 private ArraySet<Integer> mStrongAuthNotTimedOut = new ArraySet<>();
Adrian Roosb5e47222015-08-14 15:53:06 -0700187 private final StrongAuthTracker mStrongAuthTracker = new StrongAuthTracker();
Jim Millerbbf1a742012-07-17 18:30:30 -0700188
Jim Miller6212cc02012-09-05 17:35:31 -0700189 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700190 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800191 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700192
Brian Colonnaa5239892013-04-15 11:45:40 -0400193 private boolean mSwitchingUser;
194
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700195 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700196 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800197 private SubscriptionManager mSubscriptionManager;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700198 private AlarmManager mAlarmManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800199 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700200 private TrustManager mTrustManager;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700201 private int mFingerprintRunningState = FINGERPRINT_STATE_STOPPED;
Jim Miller20daffd2013-10-07 14:59:53 -0700202
Jim Millerbbf1a742012-07-17 18:30:30 -0700203 private final Handler mHandler = new Handler() {
204 @Override
205 public void handleMessage(Message msg) {
206 switch (msg.what) {
207 case MSG_TIME_UPDATE:
208 handleTimeUpdate();
209 break;
210 case MSG_BATTERY_UPDATE:
211 handleBatteryUpdate((BatteryStatus) msg.obj);
212 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700213 case MSG_SIM_STATE_CHANGE:
Jim Miller52a61332014-11-12 19:29:51 -0800214 handleSimStateChange(msg.arg1, msg.arg2, (State) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700215 break;
216 case MSG_RINGER_MODE_CHANGED:
217 handleRingerModeChange(msg.arg1);
218 break;
219 case MSG_PHONE_STATE_CHANGED:
Adrian Roosb6011622014-05-14 15:52:53 +0200220 handlePhoneStateChanged((String) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700221 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700222 case MSG_DEVICE_PROVISIONED:
223 handleDeviceProvisioned();
224 break;
225 case MSG_DPM_STATE_CHANGED:
226 handleDevicePolicyManagerStateChanged();
227 break;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500228 case MSG_USER_SWITCHING:
Adrian Roosb6011622014-05-14 15:52:53 +0200229 handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500230 break;
231 case MSG_USER_SWITCH_COMPLETE:
232 handleUserSwitchComplete(msg.arg1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700233 break;
Danielle Millettf6d0fc12012-10-23 16:16:52 -0400234 case MSG_KEYGUARD_VISIBILITY_CHANGED:
235 handleKeyguardVisibilityChanged(msg.arg1);
236 break;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700237 case MSG_KEYGUARD_RESET:
238 handleKeyguardReset();
239 break;
Adrian Roosb6011622014-05-14 15:52:53 +0200240 case MSG_KEYGUARD_BOUNCER_CHANGED:
241 handleKeyguardBouncerChanged(msg.arg1);
242 break;
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800243 case MSG_BOOT_COMPLETED:
244 handleBootCompleted();
245 break;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700246 case MSG_USER_INFO_CHANGED:
247 handleUserInfoChanged(msg.arg1);
248 break;
Brian Colonna7fce3802013-09-17 15:51:32 -0400249 case MSG_REPORT_EMERGENCY_CALL_ACTION:
250 handleReportEmergencyCallAction();
251 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700252 case MSG_FINISHED_GOING_TO_SLEEP:
253 handleFinishedGoingToSleep(msg.arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700254 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700255 case MSG_STARTED_WAKING_UP:
256 handleStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -0700257 break;
Jorim Jaggie7b12522014-08-06 16:41:21 +0200258 case MSG_FACE_UNLOCK_STATE_CHANGED:
Adrian Roos4a410172014-08-20 17:41:44 +0200259 handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200260 break;
Jim Miller52a61332014-11-12 19:29:51 -0800261 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
262 handleSimSubscriptionInfoChanged();
263 break;
Jason Monk052082c2015-06-11 11:35:23 -0400264 case MSG_AIRPLANE_MODE_CHANGED:
265 handleAirplaneModeChanged();
266 break;
Etan Cohen47051d82015-07-06 16:19:04 -0700267 case MSG_SERVICE_STATE_CHANGE:
268 handleServiceStateChange(msg.arg1, (ServiceState) msg.obj);
269 break;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700270 case MSG_SCREEN_TURNED_ON:
271 handleScreenTurnedOn();
272 break;
273 case MSG_SCREEN_TURNED_OFF:
274 handleScreenTurnedOff();
275 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700276 }
277 }
278 };
279
Wink Savilled09c4ca2014-11-22 10:08:16 -0800280 private OnSubscriptionsChangedListener mSubscriptionListener =
281 new OnSubscriptionsChangedListener() {
Jim Miller52a61332014-11-12 19:29:51 -0800282 @Override
Wink Savilled09c4ca2014-11-22 10:08:16 -0800283 public void onSubscriptionsChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800284 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
285 }
286 };
287
Adrian Roos46842d92014-03-27 14:58:03 +0100288 private SparseBooleanArray mUserHasTrust = new SparseBooleanArray();
Adrian Roos7861c662014-07-25 15:37:28 +0200289 private SparseBooleanArray mUserTrustIsManaged = new SparseBooleanArray();
Jim Miller9f0753f2015-03-23 23:59:22 -0700290 private SparseBooleanArray mUserFingerprintAuthenticated = new SparseBooleanArray();
Adrian Roos4a410172014-08-20 17:41:44 +0200291 private SparseBooleanArray mUserFaceUnlockRunning = new SparseBooleanArray();
Adrian Roos46842d92014-03-27 14:58:03 +0100292
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700293 private static int sCurrentUser;
294
295 public synchronized static void setCurrentUser(int currentUser) {
296 sCurrentUser = currentUser;
297 }
298
299 public synchronized static int getCurrentUser() {
300 return sCurrentUser;
301 }
302
Adrian Roos46842d92014-03-27 14:58:03 +0100303 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700304 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos46842d92014-03-27 14:58:03 +0100305 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200306 for (int i = 0; i < mCallbacks.size(); i++) {
307 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
308 if (cb != null) {
309 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700310 if (enabled && flags != 0) {
311 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200312 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200313 }
314 }
Adrian Roos46842d92014-03-27 14:58:03 +0100315 }
316
Jim Miller52a61332014-11-12 19:29:51 -0800317 protected void handleSimSubscriptionInfoChanged() {
318 if (DEBUG_SIM_STATES) {
319 Log.v(TAG, "onSubscriptionInfoChanged()");
Wink Savilled09c4ca2014-11-22 10:08:16 -0800320 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
321 if (sil != null) {
322 for (SubscriptionInfo subInfo : sil) {
323 Log.v(TAG, "SubInfo:" + subInfo);
324 }
325 } else {
326 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800327 }
328 }
329 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
330
331 // Hack level over 9000: Because the subscription id is not yet valid when we see the
332 // first update in handleSimStateChange, we need to force refresh all all SIM states
333 // so the subscription id for them is consistent.
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100334 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
Jim Miller52a61332014-11-12 19:29:51 -0800335 for (int i = 0; i < subscriptionInfos.size(); i++) {
336 SubscriptionInfo info = subscriptionInfos.get(i);
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100337 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
338 if (changed) {
339 changedSubscriptions.add(info);
340 }
Jim Miller52a61332014-11-12 19:29:51 -0800341 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100342 for (int i = 0; i < changedSubscriptions.size(); i++) {
343 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800344 for (int j = 0; j < mCallbacks.size(); j++) {
345 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
346 if (cb != null) {
347 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
348 }
349 }
350 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500351 for (int j = 0; j < mCallbacks.size(); j++) {
352 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
353 if (cb != null) {
354 cb.onRefreshCarrierInfo();
355 }
356 }
Jim Miller52a61332014-11-12 19:29:51 -0800357 }
358
Jason Monk052082c2015-06-11 11:35:23 -0400359 private void handleAirplaneModeChanged() {
360 for (int j = 0; j < mCallbacks.size(); j++) {
361 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
362 if (cb != null) {
363 cb.onRefreshCarrierInfo();
364 }
365 }
366 }
367
Wink Savilled09c4ca2014-11-22 10:08:16 -0800368 /** @return List of SubscriptionInfo records, maybe empty but never null */
Jim Miller52a61332014-11-12 19:29:51 -0800369 List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800370 List<SubscriptionInfo> sil = mSubscriptionInfo;
371 if (sil == null || forceReload) {
372 sil = mSubscriptionManager.getActiveSubscriptionInfoList();
373 }
374 if (sil == null) {
375 // getActiveSubscriptionInfoList was null callers expect an empty list.
376 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
377 } else {
378 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800379 }
380 return mSubscriptionInfo;
381 }
382
Adrian Roos7861c662014-07-25 15:37:28 +0200383 @Override
384 public void onTrustManagedChanged(boolean managed, int userId) {
385 mUserTrustIsManaged.put(userId, managed);
386
387 for (int i = 0; i < mCallbacks.size(); i++) {
388 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
389 if (cb != null) {
390 cb.onTrustManagedChanged(userId);
391 }
392 }
393 }
394
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700395 private void onFingerprintAuthenticated(int userId) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700396 mUserFingerprintAuthenticated.put(userId, true);
Jorim Jaggi71448a72015-08-18 19:49:04 -0700397
398 // If fingerprint unlocking is allowed, this event will lead to a Keyguard dismiss or to a
399 // wake-up (if Keyguard is not showing), so we don't need to listen until Keyguard is
400 // fully gone.
401 mFingerprintAlreadyAuthenticated = isUnlockingWithFingerprintAllowed();
Jim Millerf41fc962014-06-18 16:33:51 -0700402 for (int i = 0; i < mCallbacks.size(); i++) {
403 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
404 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700405 cb.onFingerprintAuthenticated(userId);
Jim Millerf41fc962014-06-18 16:33:51 -0700406 }
407 }
408 }
409
Jim Millerce7eb6d2015-04-03 19:29:13 -0700410 private void handleFingerprintAuthFailed() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700411 for (int i = 0; i < mCallbacks.size(); i++) {
412 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
413 if (cb != null) {
414 cb.onFingerprintAuthFailed();
415 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700416 }
Jim Millerce7eb6d2015-04-03 19:29:13 -0700417 handleFingerprintHelp(-1, mContext.getString(R.string.fingerprint_not_recognized));
418 }
Jim Millerf41fc962014-06-18 16:33:51 -0700419
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700420 private void handleFingerprintAcquired(int acquireInfo) {
421 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
422 return;
423 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700424 for (int i = 0; i < mCallbacks.size(); i++) {
425 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
426 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700427 cb.onFingerprintAcquired();
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700428 }
429 }
430 }
431
Jim Miller4f364c92015-06-08 19:24:13 -0700432 private void handleFingerprintAuthenticated() {
Jim Millerf41fc962014-06-18 16:33:51 -0700433 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700434 final int userId;
435 try {
436 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
437 } catch (RemoteException e) {
438 Log.e(TAG, "Failed to get current user id: ", e);
439 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700440 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700441 if (isFingerprintDisabled(userId)) {
442 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
443 return;
444 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700445 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700446 } finally {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700447 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700448 }
449 }
450
Jim Miller9f0753f2015-03-23 23:59:22 -0700451 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700452 for (int i = 0; i < mCallbacks.size(); i++) {
453 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
454 if (cb != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700455 cb.onFingerprintHelp(msgId, helpString);
456 }
457 }
458 }
459
460 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700461 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
462 && mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
463 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
464 startListeningForFingerprint();
465 } else {
466 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
467 }
Jim Miller9f0753f2015-03-23 23:59:22 -0700468 for (int i = 0; i < mCallbacks.size(); i++) {
469 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
470 if (cb != null) {
471 cb.onFingerprintError(msgId, errString);
Jim Millerf41fc962014-06-18 16:33:51 -0700472 }
473 }
474 }
475
Jorim Jaggi3a464782015-08-28 16:59:13 -0700476 private void handleFingerprintLockoutReset() {
477 updateFingerprintListeningState();
478 }
479
Jorim Jaggi86bed402015-08-20 18:20:02 -0700480 private void setFingerprintRunningState(int fingerprintRunningState) {
481 boolean wasRunning = mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
482 boolean isRunning = fingerprintRunningState == FINGERPRINT_STATE_RUNNING;
483 mFingerprintRunningState = fingerprintRunningState;
484
485 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
486 // asynchronousness of the cancel cycle. So only notify them if the actualy running state
487 // has changed.
488 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700489 notifyFingerprintRunningStateChanged();
490 }
491 }
492
493 private void notifyFingerprintRunningStateChanged() {
494 for (int i = 0; i < mCallbacks.size(); i++) {
495 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
496 if (cb != null) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700497 cb.onFingerprintRunningStateChanged(isFingerprintDetectionRunning());
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700498 }
499 }
500 }
Adrian Roos4a410172014-08-20 17:41:44 +0200501 private void handleFaceUnlockStateChanged(boolean running, int userId) {
502 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200503 for (int i = 0; i < mCallbacks.size(); i++) {
504 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
505 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200506 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200507 }
508 }
509 }
510
Adrian Roos4a410172014-08-20 17:41:44 +0200511 public boolean isFaceUnlockRunning(int userId) {
512 return mUserFaceUnlockRunning.get(userId);
513 }
514
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700515 public boolean isFingerprintDetectionRunning() {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700516 return mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700517 }
518
Jim Miller50e62182014-04-23 17:25:00 -0700519 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100520 // Don't allow trust agent if device is secured with a SIM PIN. This is here
521 // mainly because there's no other way to prompt the user to enter their SIM PIN
522 // once they get past the keyguard screen.
523 final boolean disabledBySimPin = isSimPinSecure();
524 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700525 }
526
Jim Miller06e34502014-07-17 14:46:05 -0700527 private boolean isFingerprintDisabled(int userId) {
528 final DevicePolicyManager dpm =
529 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
530 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Adrian Roos733b6632015-08-21 14:32:35 -0700531 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
532 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700533 }
534
Selim Cineke8bae622015-07-15 13:24:06 -0700535 public boolean getUserCanSkipBouncer(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700536 return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
537 && isUnlockingWithFingerprintAllowed());
Selim Cineke8bae622015-07-15 13:24:06 -0700538 }
539
Adrian Roos46842d92014-03-27 14:58:03 +0100540 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700541 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100542 }
543
Adrian Roos7861c662014-07-25 15:37:28 +0200544 public boolean getUserTrustIsManaged(int userId) {
545 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
546 }
547
Selim Cinek1fcafc42015-07-20 14:39:25 -0700548 public boolean isUnlockingWithFingerprintAllowed() {
Adrian Roosb5e47222015-08-14 15:53:06 -0700549 return mStrongAuthTracker.isUnlockingWithFingerprintAllowed()
550 && !hasFingerprintUnlockTimedOut(sCurrentUser);
551 }
552
553 public StrongAuthTracker getStrongAuthTracker() {
554 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700555 }
556
557 /**
558 * @return true if the user hasn't use strong authentication (pattern, PIN, password) since a
559 * while and thus can't unlock with fingerprint, false otherwise
560 */
561 public boolean hasFingerprintUnlockTimedOut(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700562 return !mStrongAuthNotTimedOut.contains(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700563 }
564
565 public void reportSuccessfulStrongAuthUnlockAttempt() {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700566 mStrongAuthNotTimedOut.add(sCurrentUser);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700567 scheduleStrongAuthTimeout();
Jim Millere0507bb2015-08-12 20:30:34 -0700568 if (mFpm != null) {
569 byte[] token = null; /* TODO: pass real auth token once fp HAL supports it */
570 mFpm.resetTimeout(token);
571 }
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700572 }
573
574 private void scheduleStrongAuthTimeout() {
575 long when = SystemClock.elapsedRealtime() + FINGERPRINT_UNLOCK_TIMEOUT_MS;
576 Intent intent = new Intent(ACTION_STRONG_AUTH_TIMEOUT);
577 intent.putExtra(USER_ID, sCurrentUser);
578 PendingIntent sender = PendingIntent.getBroadcast(mContext,
579 sCurrentUser, intent, PendingIntent.FLAG_CANCEL_CURRENT);
580 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, when, sender);
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700581 notifyStrongAuthStateChanged(sCurrentUser);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700582 }
583
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700584 private void notifyStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700585 for (int i = 0; i < mCallbacks.size(); i++) {
586 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
587 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700588 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700589 }
590 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700591 }
592
Jim Miller8f09fd22013-03-14 19:04:28 -0700593 static class DisplayClientState {
594 public int clientGeneration;
595 public boolean clearing;
596 public PendingIntent intent;
597 public int playbackState;
598 public long playbackEventTime;
599 }
600
601 private DisplayClientState mDisplayClientState = new DisplayClientState();
602
Jim Millerbbf1a742012-07-17 18:30:30 -0700603 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
604
605 public void onReceive(Context context, Intent intent) {
606 final String action = intent.getAction();
607 if (DEBUG) Log.d(TAG, "received broadcast " + action);
608
609 if (Intent.ACTION_TIME_TICK.equals(action)
610 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200611 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700612 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700613 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
614 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
615 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
616 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
617 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos7b043112015-07-10 13:00:33 -0700618 final int maxChargingCurrent = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700619 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700620 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
621 maxChargingCurrent));
Jim Millerbbf1a742012-07-17 18:30:30 -0700622 mHandler.sendMessage(msg);
623 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Jim Miller52a61332014-11-12 19:29:51 -0800624 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700625 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800626 Log.v(TAG, "action " + action
627 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
628 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700629 }
Jim Miller52a61332014-11-12 19:29:51 -0800630 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
631 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700632 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
633 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
634 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
635 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
636 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
637 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400638 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
639 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800640 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700641 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700642 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
643 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
644 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
645 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
646 if (DEBUG) {
647 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
648 + subId);
649 }
650 mHandler.sendMessage(
651 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Jim Millerbbf1a742012-07-17 18:30:30 -0700652 }
653 }
654 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700655
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700656 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
657
658 public void onReceive(Context context, Intent intent) {
659 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200660 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
661 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
662 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700663 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
664 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200665 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
666 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
667 getSendingUserId()));
668 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
669 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
670 getSendingUserId()));
671 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
672 .equals(action)) {
673 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700674 }
675 }
676 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700677
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700678 private final BroadcastReceiver mStrongAuthTimeoutReceiver = new BroadcastReceiver() {
679 @Override
680 public void onReceive(Context context, Intent intent) {
681 if (ACTION_STRONG_AUTH_TIMEOUT.equals(intent.getAction())) {
682 int userId = intent.getIntExtra(USER_ID, -1);
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700683 mStrongAuthNotTimedOut.remove(userId);
684 notifyStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700685 }
686 }
687 };
688
Jorim Jaggi3a464782015-08-28 16:59:13 -0700689 private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
690 = new FingerprintManager.LockoutResetCallback() {
691 @Override
692 public void onLockoutReset() {
693 handleFingerprintLockoutReset();
694 }
695 };
696
Jim Miller9f0753f2015-03-23 23:59:22 -0700697 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
698 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700699
700 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700701 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700702 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700703 };
704
705 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700706 public void onAuthenticationSucceeded(AuthenticationResult result) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700707 handleFingerprintAuthenticated();
Jim Millerf41fc962014-06-18 16:33:51 -0700708 }
709
710 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700711 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700712 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700713 }
714
715 @Override
716 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700717 handleFingerprintError(errMsgId, errString.toString());
718 }
719
720 @Override
721 public void onAuthenticationAcquired(int acquireInfo) {
722 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700723 }
724 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700725 private CancellationSignal mFingerprintCancelSignal;
726 private FingerprintManager mFpm;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700727
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800728 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800729 * When we receive a
730 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700731 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800732 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800733 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800734 */
Jim Miller52a61332014-11-12 19:29:51 -0800735 private static class SimData {
736 public State simState;
737 public int slotId;
738 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800739
Jim Miller52a61332014-11-12 19:29:51 -0800740 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800741 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800742 slotId = slot;
743 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800744 }
745
Jim Miller52a61332014-11-12 19:29:51 -0800746 static SimData fromIntent(Intent intent) {
747 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800748 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
749 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
750 }
Wink Savillea639b312012-07-10 12:37:54 -0700751 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800752 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
753 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800754 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700755 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700756 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700757 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700758
Wink Savillea639b312012-07-10 12:37:54 -0700759 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700760 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700761 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700762 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700763 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700764 }
Wink Savillea639b312012-07-10 12:37:54 -0700765 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
766 state = IccCardConstants.State.READY;
767 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800768 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700769 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
770 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
771 state = IccCardConstants.State.PIN_REQUIRED;
772 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
773 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800774 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700775 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800776 }
Wink Savillea639b312012-07-10 12:37:54 -0700777 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
778 state = IccCardConstants.State.NETWORK_LOCKED;
Jim Miller109f1fd2012-09-19 20:44:16 -0700779 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
780 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
781 // This is required because telephony doesn't return to "READY" after
782 // these state transitions. See bug 7197471.
783 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800784 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700785 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800786 }
Jim Miller52a61332014-11-12 19:29:51 -0800787 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800788 }
789
790 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800791 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800792 }
793 }
794
Adrian Roos12c1ef52014-06-04 13:54:08 +0200795 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700796 public static final int CHARGING_UNKNOWN = -1;
797 public static final int CHARGING_SLOWLY = 0;
798 public static final int CHARGING_REGULAR = 1;
799 public static final int CHARGING_FAST = 2;
800
Jim Miller16464b82011-10-20 21:10:13 -0700801 public final int status;
802 public final int level;
803 public final int plugged;
804 public final int health;
Adrian Roos7b043112015-07-10 13:00:33 -0700805 public final int maxChargingCurrent;
806 public BatteryStatus(int status, int level, int plugged, int health, int maxChargingCurrent) {
Jim Miller16464b82011-10-20 21:10:13 -0700807 this.status = status;
808 this.level = level;
809 this.plugged = plugged;
810 this.health = health;
Adrian Roos7b043112015-07-10 13:00:33 -0700811 this.maxChargingCurrent = maxChargingCurrent;
Jim Miller16464b82011-10-20 21:10:13 -0700812 }
813
Jim Millerbbf1a742012-07-17 18:30:30 -0700814 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700815 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700816 * @return true if the device is plugged in.
817 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100818 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700819 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700820 || plugged == BatteryManager.BATTERY_PLUGGED_USB
821 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -0700822 }
823
824 /**
825 * Whether or not the device is charged. Note that some devices never return 100% for
826 * battery level, so this allows either battery level or status to determine if the
827 * battery is charged.
828 * @return true if the device is charged
829 */
830 public boolean isCharged() {
831 return status == BATTERY_STATUS_FULL || level >= 100;
832 }
833
834 /**
835 * Whether battery is low and needs to be charged.
836 * @return true if battery is low
837 */
838 public boolean isBatteryLow() {
839 return level < LOW_BATTERY_THRESHOLD;
840 }
841
Adrian Roos7b043112015-07-10 13:00:33 -0700842 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
843 return maxChargingCurrent <= 0 ? CHARGING_UNKNOWN :
844 maxChargingCurrent < slowThreshold ? CHARGING_SLOWLY :
845 maxChargingCurrent > fastThreshold ? CHARGING_FAST :
846 CHARGING_REGULAR;
847 }
Jim Miller16464b82011-10-20 21:10:13 -0700848 }
849
Adrian Roosb5e47222015-08-14 15:53:06 -0700850 public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
851
852 public boolean isUnlockingWithFingerprintAllowed() {
853 int userId = getCurrentUser();
854 return isFingerprintAllowedForUser(userId);
855 }
856
857 public boolean hasUserAuthenticatedSinceBoot() {
858 int userId = getCurrentUser();
859 return (getStrongAuthForUser(userId)
860 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
861 }
862
863 @Override
864 public void onStrongAuthRequiredChanged(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700865 notifyStrongAuthStateChanged(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -0700866 }
867 }
868
Jim Millerdcb3d842012-08-23 19:18:12 -0700869 public static KeyguardUpdateMonitor getInstance(Context context) {
870 if (sInstance == null) {
871 sInstance = new KeyguardUpdateMonitor(context);
872 }
873 return sInstance;
874 }
875
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700876 protected void handleStartedWakingUp() {
Jorim Jaggi864e64b2015-05-20 14:13:23 -0700877 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700878 final int count = mCallbacks.size();
879 for (int i = 0; i < count; i++) {
880 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
881 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700882 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -0700883 }
884 }
885 }
886
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700887 protected void handleFinishedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -0700888 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -0700889 final int count = mCallbacks.size();
890 for (int i = 0; i < count; i++) {
891 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
892 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700893 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700894 }
895 }
Jorim Jaggi71448a72015-08-18 19:49:04 -0700896 mFingerprintAlreadyAuthenticated = false;
Jorim Jaggiea657062015-04-28 13:45:11 -0700897 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700898 }
899
Jorim Jaggif1518da2015-07-30 11:56:36 -0700900 private void handleScreenTurnedOn() {
901 final int count = mCallbacks.size();
902 for (int i = 0; i < count; i++) {
903 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
904 if (cb != null) {
905 cb.onScreenTurnedOn();
906 }
907 }
908 }
909
910 private void handleScreenTurnedOff() {
911 final int count = mCallbacks.size();
912 for (int i = 0; i < count; i++) {
913 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
914 if (cb != null) {
915 cb.onScreenTurnedOff();
916 }
917 }
918 }
919
Adam Powell43a372f2013-09-27 17:43:53 -0700920 /**
921 * IMPORTANT: Must be called from UI thread.
922 */
923 public void dispatchSetBackground(Bitmap bmp) {
924 if (DEBUG) Log.d(TAG, "dispatchSetBackground");
925 final int count = mCallbacks.size();
926 for (int i = 0; i < count; i++) {
927 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
928 if (cb != null) {
929 cb.onSetBackground(bmp);
930 }
931 }
932 }
933
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700934 private void handleUserInfoChanged(int userId) {
935 for (int i = 0; i < mCallbacks.size(); i++) {
936 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
937 if (cb != null) {
938 cb.onUserInfoChanged(userId);
939 }
940 }
941 }
942
Jim Millerdcb3d842012-08-23 19:18:12 -0700943 private KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800944 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800945 mSubscriptionManager = SubscriptionManager.from(context);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700946 mAlarmManager = context.getSystemService(AlarmManager.class);
Michael Jurkafff56142012-11-28 16:51:00 -0800947 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700948
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800949 // Since device can't be un-provisioned, we only need to register a content observer
950 // to update mDeviceProvisioned when we are...
951 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700952 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800953 }
Jim Miller47088bb2009-11-24 00:40:16 -0800954
Jim Millerbbf1a742012-07-17 18:30:30 -0700955 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -0700956 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800957
Jim Millerbbf1a742012-07-17 18:30:30 -0700958 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800959 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800960 filter.addAction(Intent.ACTION_TIME_TICK);
961 filter.addAction(Intent.ACTION_TIME_CHANGED);
962 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
963 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -0400964 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800965 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -0700966 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -0800967 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -0800968 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Jim Millerbbf1a742012-07-17 18:30:30 -0700969 context.registerReceiver(mBroadcastReceiver, filter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700970
Adam Cohenc276e822012-11-08 13:01:08 -0800971 final IntentFilter bootCompleteFilter = new IntentFilter();
972 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
973 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
974 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter);
975
Adrian Roos48c796c2014-09-01 14:59:23 +0200976 final IntentFilter allUserFilter = new IntentFilter();
977 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
978 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
979 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
980 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
981 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
982 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700983 null, null);
984
Wink Saville071743f2015-01-12 17:11:04 -0800985 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700986 try {
987 ActivityManagerNative.getDefault().registerUserSwitchObserver(
988 new IUserSwitchObserver.Stub() {
989 @Override
990 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500991 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700992 newUserId, 0, reply));
993 }
994 @Override
995 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500996 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +0200997 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700998 }
Kenny Guy42979622015-04-13 18:03:05 +0000999 @Override
1000 public void onForegroundProfileSwitch(int newProfileId) {
1001 // Ignore.
1002 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001003 });
1004 } catch (RemoteException e) {
1005 // TODO Auto-generated catch block
1006 e.printStackTrace();
1007 }
Adrian Roos46842d92014-03-27 14:58:03 +01001008
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001009 IntentFilter strongAuthTimeoutFilter = new IntentFilter();
1010 strongAuthTimeoutFilter.addAction(ACTION_STRONG_AUTH_TIMEOUT);
1011 context.registerReceiver(mStrongAuthTimeoutReceiver, strongAuthTimeoutFilter,
1012 PERMISSION_SELF, null /* handler */);
Jorim Jaggi237b0612015-05-01 14:28:49 -07001013 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1014 mTrustManager.registerTrustListener(this);
Adrian Roosb5e47222015-08-14 15:53:06 -07001015 new LockPatternUtils(context).registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001016
Jim Miller9f0753f2015-03-23 23:59:22 -07001017 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
Jorim Jaggiea657062015-04-28 13:45:11 -07001018 updateFingerprintListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001019 if (mFpm != null) {
1020 mFpm.addLockoutResetCallback(mLockoutResetCallback);
1021 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001022 }
1023
1024 private void updateFingerprintListeningState() {
1025 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001026 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001027 stopListeningForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001028 } else if (mFingerprintRunningState != FINGERPRINT_STATE_RUNNING
1029 && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001030 startListeningForFingerprint();
1031 }
1032 }
1033
1034 private boolean shouldListenForFingerprint() {
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001035 return (mKeyguardIsVisible || !mDeviceInteractive || mBouncer) && !mSwitchingUser
Adrian Roos733b6632015-08-21 14:32:35 -07001036 && !mFingerprintAlreadyAuthenticated && !isFingerprintDisabled(getCurrentUser());
Jim Miller9f0753f2015-03-23 23:59:22 -07001037 }
1038
Jim Millerce7eb6d2015-04-03 19:29:13 -07001039 private void startListeningForFingerprint() {
Jorim Jaggi86bed402015-08-20 18:20:02 -07001040 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING) {
1041 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING_RESTARTING);
1042 return;
1043 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001044 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001045 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001046 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001047 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001048 mFingerprintCancelSignal.cancel();
1049 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001050 mFingerprintCancelSignal = new CancellationSignal();
Jim Millerf501b582015-06-03 16:36:31 -07001051 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null, userId);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001052 setFingerprintRunningState(FINGERPRINT_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001053 }
1054 }
1055
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001056 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001057 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1058 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1059 }
1060
Jorim Jaggiea657062015-04-28 13:45:11 -07001061 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001062 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Jorim Jaggi86bed402015-08-20 18:20:02 -07001063 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001064 mFingerprintCancelSignal.cancel();
Jim Millerce7eb6d2015-04-03 19:29:13 -07001065 mFingerprintCancelSignal = null;
Jorim Jaggi86bed402015-08-20 18:20:02 -07001066 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001067 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001068 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
1069 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
1070 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001071 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001072
Michael Jurkafff56142012-11-28 16:51:00 -08001073 private boolean isDeviceProvisionedInSettingsDb() {
1074 return Settings.Global.getInt(mContext.getContentResolver(),
1075 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1076 }
1077
Jim Millerbbf1a742012-07-17 18:30:30 -07001078 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001079 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001080 @Override
1081 public void onChange(boolean selfChange) {
1082 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001083 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001084 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001085 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001086 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001087 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001088 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001089 };
1090
1091 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001092 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001093 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001094
1095 // prevent a race condition between where we check the flag and where we register the
1096 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001097 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001098 if (provisioned != mDeviceProvisioned) {
1099 mDeviceProvisioned = provisioned;
1100 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001101 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001102 }
1103 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001104 }
1105
Jim Millerbbf1a742012-07-17 18:30:30 -07001106 /**
1107 * Handle {@link #MSG_DPM_STATE_CHANGED}
1108 */
Jim Millerb0304762012-03-13 20:01:25 -07001109 protected void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001110 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001111 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1112 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1113 if (cb != null) {
1114 cb.onDevicePolicyManagerStateChanged();
1115 }
Jim Millerb0304762012-03-13 20:01:25 -07001116 }
1117 }
1118
Jim Millerbbf1a742012-07-17 18:30:30 -07001119 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001120 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001121 */
Chris Wrenf41c61b2012-11-29 15:19:54 -05001122 protected void handleUserSwitching(int userId, IRemoteCallback reply) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001123 mSwitchingUser = true;
1124 updateFingerprintListeningState();
1125
Jim Millerbbf1a742012-07-17 18:30:30 -07001126 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001127 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1128 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001129 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001130 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001131 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001132 try {
1133 reply.sendResult(null);
1134 } catch (RemoteException e) {
1135 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001136 }
1137
Jim Millerbbf1a742012-07-17 18:30:30 -07001138 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001139 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1140 */
1141 protected void handleUserSwitchComplete(int userId) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001142 mSwitchingUser = false;
1143 updateFingerprintListeningState();
1144
Chris Wrenf41c61b2012-11-29 15:19:54 -05001145 for (int i = 0; i < mCallbacks.size(); i++) {
1146 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1147 if (cb != null) {
1148 cb.onUserSwitchComplete(userId);
1149 }
1150 }
1151 }
1152
1153 /**
Jim Miller90873d52013-09-26 18:11:38 -07001154 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1155 * keyguard crashes sometime after boot, then it will never receive this
1156 * broadcast and hence not handle the event. This method is ultimately called by
1157 * PhoneWindowManager in this case.
1158 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001159 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001160 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001161 }
1162
1163 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001164 * Handle {@link #MSG_BOOT_COMPLETED}
1165 */
1166 protected void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001167 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001168 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001169 for (int i = 0; i < mCallbacks.size(); i++) {
1170 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1171 if (cb != null) {
1172 cb.onBootCompleted();
1173 }
1174 }
1175 }
1176
1177 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001178 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001179 * destroyed.
1180 */
1181 public boolean hasBootCompleted() {
1182 return mBootCompleted;
1183 }
1184
1185 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001186 * Handle {@link #MSG_DEVICE_PROVISIONED}
1187 */
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001188 protected void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001189 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001190 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1191 if (cb != null) {
1192 cb.onDeviceProvisioned();
1193 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001194 }
Michael Jurkafff56142012-11-28 16:51:00 -08001195 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001196 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001197 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1198 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001199 }
1200 }
1201
Jim Millerbbf1a742012-07-17 18:30:30 -07001202 /**
1203 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1204 */
Jim Millerc23024d2010-02-24 15:37:00 -08001205 protected void handlePhoneStateChanged(String newState) {
1206 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001207 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1208 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1209 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1210 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1211 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1212 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1213 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001214 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001215 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1216 if (cb != null) {
1217 cb.onPhoneStateChanged(mPhoneState);
1218 }
Jim Millerc23024d2010-02-24 15:37:00 -08001219 }
1220 }
1221
Jim Millerbbf1a742012-07-17 18:30:30 -07001222 /**
1223 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1224 */
Jim Miller47088bb2009-11-24 00:40:16 -08001225 protected void handleRingerModeChange(int mode) {
1226 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001227 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001228 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001229 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1230 if (cb != null) {
1231 cb.onRingerModeChanged(mode);
1232 }
Jim Miller47088bb2009-11-24 00:40:16 -08001233 }
1234 }
1235
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001236 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001237 * Handle {@link #MSG_TIME_UPDATE}
1238 */
1239 private void handleTimeUpdate() {
1240 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001241 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001242 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1243 if (cb != null) {
1244 cb.onTimeChanged();
1245 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001246 }
1247 }
1248
1249 /**
1250 * Handle {@link #MSG_BATTERY_UPDATE}
1251 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001252 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001253 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001254 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1255 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001256 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001257 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001258 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1259 if (cb != null) {
1260 cb.onRefreshBatteryInfo(status);
1261 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001262 }
1263 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001264 }
1265
1266 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001267 * Handle {@link #MSG_SIM_STATE_CHANGE}
1268 */
Jim Miller52a61332014-11-12 19:29:51 -08001269 private void handleSimStateChange(int subId, int slotId, State state) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001270
Jim Miller52a61332014-11-12 19:29:51 -08001271 if (DEBUG_SIM_STATES) {
1272 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1273 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001274 }
1275
Wink Savillea54bf652014-12-11 13:37:50 -08001276 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001277 Log.w(TAG, "invalid subId in handleSimStateChange()");
1278 return;
1279 }
1280
1281 SimData data = mSimDatas.get(subId);
1282 final boolean changed;
1283 if (data == null) {
1284 data = new SimData(state, slotId, subId);
1285 mSimDatas.put(subId, data);
1286 changed = true; // no data yet; force update
1287 } else {
1288 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1289 data.simState = state;
1290 data.subId = subId;
1291 data.slotId = slotId;
1292 }
1293 if (changed && state != State.UNKNOWN) {
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) {
Jim Miller52a61332014-11-12 19:29:51 -08001297 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001298 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001299 }
1300 }
1301 }
1302
Jim Millerbbf1a742012-07-17 18:30:30 -07001303 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001304 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1305 */
1306 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1307 if (DEBUG) {
1308 Log.d(TAG,
1309 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1310 }
1311
1312 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1313 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1314 return;
1315 }
1316
1317 mServiceStates.put(subId, serviceState);
1318
1319 for (int j = 0; j < mCallbacks.size(); j++) {
1320 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1321 if (cb != null) {
1322 cb.onRefreshCarrierInfo();
1323 }
1324 }
1325 }
1326
1327 /**
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001328 * Handle {@link #MSG_KEYGUARD_VISIBILITY_CHANGED}
1329 */
1330 private void handleKeyguardVisibilityChanged(int showing) {
1331 if (DEBUG) Log.d(TAG, "handleKeyguardVisibilityChanged(" + showing + ")");
Danielle Millett5d2404d2012-11-01 00:05:27 -04001332 boolean isShowing = (showing == 1);
1333 mKeyguardIsVisible = isShowing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001334 for (int i = 0; i < mCallbacks.size(); i++) {
1335 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1336 if (cb != null) {
John Spurlock385a63d2013-10-30 19:40:48 -04001337 cb.onKeyguardVisibilityChangedRaw(isShowing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001338 }
1339 }
Jorim Jaggi71448a72015-08-18 19:49:04 -07001340 if (!isShowing) {
1341 mFingerprintAlreadyAuthenticated = false;
1342 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001343 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001344 }
1345
Brian Colonna7fce3802013-09-17 15:51:32 -04001346 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001347 * Handle {@link #MSG_KEYGUARD_RESET}
1348 */
1349 private void handleKeyguardReset() {
1350 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Adrian Roosf6d51ac2015-09-02 13:26:25 -07001351 updateFingerprintListeningState();
Selim Cinek1fcafc42015-07-20 14:39:25 -07001352 }
1353
1354 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001355 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1356 * @see #sendKeyguardBouncerChanged(boolean)
1357 */
1358 private void handleKeyguardBouncerChanged(int bouncer) {
1359 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1360 boolean isBouncer = (bouncer == 1);
1361 mBouncer = isBouncer;
1362 for (int i = 0; i < mCallbacks.size(); i++) {
1363 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1364 if (cb != null) {
1365 cb.onKeyguardBouncerChanged(isBouncer);
1366 }
1367 }
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001368 updateFingerprintListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02001369 }
1370
1371 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001372 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1373 */
1374 private void handleReportEmergencyCallAction() {
1375 for (int i = 0; i < mCallbacks.size(); i++) {
1376 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1377 if (cb != null) {
1378 cb.onEmergencyCallAction();
1379 }
1380 }
1381 }
1382
Jim Miller16464b82011-10-20 21:10:13 -07001383 private static boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001384 final boolean nowPluggedIn = current.isPluggedIn();
1385 final boolean wasPluggedIn = old.isPluggedIn();
Jim Miller79a444a2011-02-15 15:02:11 -08001386 final boolean stateChangedWhilePluggedIn =
Jim Miller16464b82011-10-20 21:10:13 -07001387 wasPluggedIn == true && nowPluggedIn == true
1388 && (old.status != current.status);
1389
1390 // change in plug state is always interesting
1391 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001392 return true;
1393 }
1394
1395 // change in battery level while plugged in
Jim Miller16464b82011-10-20 21:10:13 -07001396 if (nowPluggedIn && old.level != current.level) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001397 return true;
1398 }
1399
Jim Miller16464b82011-10-20 21:10:13 -07001400 // change where battery needs charging
Jim Millerbbf1a742012-07-17 18:30:30 -07001401 if (!nowPluggedIn && current.isBatteryLow() && current.level != old.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001402 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001403 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001404
1405 // change in charging current while plugged in
1406 if (nowPluggedIn && current.maxChargingCurrent != old.maxChargingCurrent) {
1407 return true;
1408 }
1409
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001410 return false;
1411 }
1412
1413 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001414 * Remove the given observer's callback.
1415 *
Jim Miller6212cc02012-09-05 17:35:31 -07001416 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001417 */
Jim Miller6212cc02012-09-05 17:35:31 -07001418 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
1419 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1420 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1421 if (mCallbacks.get(i).get() == callback) {
1422 mCallbacks.remove(i);
1423 }
1424 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001425 }
1426
1427 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001428 * Register to receive notifications about general keyguard information
1429 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001430 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001431 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001432 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Jim Miller6212cc02012-09-05 17:35:31 -07001433 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1434 // Prevent adding duplicate callbacks
1435 for (int i = 0; i < mCallbacks.size(); i++) {
1436 if (mCallbacks.get(i).get() == callback) {
1437 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1438 new Exception("Called by"));
1439 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001440 }
1441 }
Jim Miller6212cc02012-09-05 17:35:31 -07001442 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1443 removeCallback(null); // remove unused references
1444 sendUpdates(callback);
1445 }
1446
1447 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1448 // Notify listener of the current state
1449 callback.onRefreshBatteryInfo(mBatteryStatus);
1450 callback.onTimeChanged();
1451 callback.onRingerModeChanged(mRingMode);
1452 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001453 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001454 callback.onClockVisibilityChanged();
Jim Miller52a61332014-11-12 19:29:51 -08001455 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1456 final SimData state = data.getValue();
1457 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1458 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001459 }
1460
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001461 public void sendKeyguardVisibilityChanged(boolean showing) {
1462 if (DEBUG) Log.d(TAG, "sendKeyguardVisibilityChanged(" + showing + ")");
1463 Message message = mHandler.obtainMessage(MSG_KEYGUARD_VISIBILITY_CHANGED);
1464 message.arg1 = showing ? 1 : 0;
1465 message.sendToTarget();
1466 }
1467
Selim Cinek1fcafc42015-07-20 14:39:25 -07001468 public void sendKeyguardReset() {
1469 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1470 }
1471
Adrian Roosb6011622014-05-14 15:52:53 +02001472 /**
1473 * @see #handleKeyguardBouncerChanged(int)
1474 */
1475 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1476 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1477 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1478 message.arg1 = showingBouncer ? 1 : 0;
1479 message.sendToTarget();
1480 }
1481
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001482 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001483 * 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 -08001484 * have the information earlier than waiting for the intent
1485 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001486 *
1487 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1488 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001489 */
Jim Miller52a61332014-11-12 19:29:51 -08001490 public void reportSimUnlocked(int subId) {
1491 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
1492 int slotId = SubscriptionManager.getSlotId(subId);
1493 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001494 }
1495
Brian Colonna7fce3802013-09-17 15:51:32 -04001496 /**
1497 * Report that the emergency call button has been pressed and the emergency dialer is
1498 * about to be displayed.
1499 *
1500 * @param bypassHandler runs immediately.
1501 *
1502 * NOTE: Must be called from UI thread if bypassHandler == true.
1503 */
1504 public void reportEmergencyCallAction(boolean bypassHandler) {
1505 if (!bypassHandler) {
1506 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1507 } else {
1508 handleReportEmergencyCallAction();
1509 }
1510 }
1511
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001512 /**
1513 * @return Whether the device is provisioned (whether they have gone through
1514 * the setup wizard)
1515 */
1516 public boolean isDeviceProvisioned() {
1517 return mDeviceProvisioned;
1518 }
1519
Jorim Jaggi9f743032015-05-04 15:22:40 -07001520 public void clearFailedUnlockAttempts() {
1521 mFailedAttempts.delete(sCurrentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001522 }
1523
Jorim Jaggi9f743032015-05-04 15:22:40 -07001524 public int getFailedUnlockAttempts() {
1525 return mFailedAttempts.get(sCurrentUser, 0);
1526 }
1527
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001528 public void reportFailedStrongAuthUnlockAttempt() {
Jorim Jaggi9f743032015-05-04 15:22:40 -07001529 mFailedAttempts.put(sCurrentUser, getFailedUnlockAttempts() + 1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001530 }
1531
Jim Millerf41fc962014-06-18 16:33:51 -07001532 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001533 mUserFingerprintAuthenticated.clear();
Jim Millerf41fc962014-06-18 16:33:51 -07001534 }
1535
Jim Miller52a61332014-11-12 19:29:51 -08001536 public boolean isSimPinVoiceSecure() {
1537 // TODO: only count SIMs that handle voice
1538 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001539 }
1540
1541 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001542 // True if any SIM is pin secure
1543 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1544 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1545 }
1546 return false;
1547 }
1548
Jason Monk9ff69bd2014-12-02 16:43:17 -05001549 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001550 if (mSimDatas.containsKey(subId)) {
1551 return mSimDatas.get(subId).simState;
1552 } else {
1553 return State.UNKNOWN;
1554 }
1555 }
1556
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001557 /**
1558 * @return true if and only if the state has changed for the specified {@code slotId}
1559 */
1560 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08001561
1562 // This is awful. It exists because there are two APIs for getting the SIM status
1563 // that don't return the complete set of values and have different types. In Keyguard we
1564 // need IccCardConstants, but TelephonyManager would only give us
1565 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001566 final TelephonyManager tele = TelephonyManager.from(mContext);
1567 int simState = tele.getSimState(slotId);
Jim Miller52a61332014-11-12 19:29:51 -08001568 State state;
1569 try {
xinhe18b9c3c2014-12-02 15:03:20 -08001570 state = State.intToState(simState);
Jim Miller52a61332014-11-12 19:29:51 -08001571 } catch(IllegalArgumentException ex) {
xinhe18b9c3c2014-12-02 15:03:20 -08001572 Log.w(TAG, "Unknown sim state: " + simState);
Jim Miller52a61332014-11-12 19:29:51 -08001573 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05001574 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001575 SimData data = mSimDatas.get(subId);
1576 final boolean changed;
1577 if (data == null) {
1578 data = new SimData(state, slotId, subId);
1579 mSimDatas.put(subId, data);
1580 changed = true; // no data yet; force update
1581 } else {
1582 changed = data.simState != state;
1583 data.simState = state;
1584 }
1585 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07001586 }
1587
1588 public static boolean isSimPinSecure(IccCardConstants.State state) {
1589 final IccCardConstants.State simState = state;
1590 return (simState == IccCardConstants.State.PIN_REQUIRED
1591 || simState == IccCardConstants.State.PUK_REQUIRED
1592 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001593 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001594
1595 public DisplayClientState getCachedDisplayClientState() {
1596 return mDisplayClientState;
1597 }
Jim Miller20daffd2013-10-07 14:59:53 -07001598
1599 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1600 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001601 public void dispatchStartedWakingUp() {
1602 synchronized (this) {
1603 mDeviceInteractive = true;
1604 }
1605 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1606 }
1607
1608 public void dispatchFinishedGoingToSleep(int why) {
1609 synchronized(this) {
1610 mDeviceInteractive = false;
1611 }
1612 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1613 }
1614
Jim Miller20daffd2013-10-07 14:59:53 -07001615 public void dispatchScreenTurnedOn() {
1616 synchronized (this) {
1617 mScreenOn = true;
1618 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001619 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001620 }
1621
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001622 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001623 synchronized(this) {
1624 mScreenOn = false;
1625 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001626 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001627 }
1628
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001629 public boolean isDeviceInteractive() {
1630 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001631 }
Jim Miller52a61332014-11-12 19:29:51 -08001632
1633 /**
1634 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1635 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001636 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001637 */
1638 public int getNextSubIdForState(State state) {
1639 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001640 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001641 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1642 for (int i = 0; i < list.size(); i++) {
1643 final SubscriptionInfo info = list.get(i);
1644 final int id = info.getSubscriptionId();
1645 int slotId = SubscriptionManager.getSlotId(id);
1646 if (state == getSimState(id) && bestSlotId > slotId ) {
1647 resultId = id;
1648 bestSlotId = slotId;
1649 }
1650 }
1651 return resultId;
1652 }
1653
1654 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1655 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1656 for (int i = 0; i < list.size(); i++) {
1657 SubscriptionInfo info = list.get(i);
1658 if (subId == info.getSubscriptionId()) return info;
1659 }
1660 return null; // not found
1661 }
Jason Monkab525272015-07-13 17:02:49 -04001662
1663 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1664 pw.println("KeyguardUpdateMonitor state:");
1665 pw.println(" SIM States:");
1666 for (SimData data : mSimDatas.values()) {
1667 pw.println(" " + data.toString());
1668 }
1669 pw.println(" Subs:");
1670 if (mSubscriptionInfo != null) {
1671 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
1672 pw.println(" " + mSubscriptionInfo.get(i));
1673 }
1674 }
1675 pw.println(" Service states:");
1676 for (int subId : mServiceStates.keySet()) {
1677 pw.println(" " + subId + "=" + mServiceStates.get(subId));
1678 }
1679 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001680}