blob: 3232f65910b813d7d091220c3b88054e96c6c89f [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;
32
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080033import static android.os.BatteryManager.BATTERY_STATUS_FULL;
34import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
Jim Miller16464b82011-10-20 21:10:13 -070035import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
36import static android.os.BatteryManager.EXTRA_STATUS;
37import static android.os.BatteryManager.EXTRA_PLUGGED;
38import static android.os.BatteryManager.EXTRA_LEVEL;
39import static android.os.BatteryManager.EXTRA_HEALTH;
Adrian Roos7b043112015-07-10 13:00:33 -070040import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
Jim Miller06e34502014-07-17 14:46:05 -070041
Jim Miller47088bb2009-11-24 00:40:16 -080042import android.media.AudioManager;
Jim Miller79a444a2011-02-15 15:02:11 -080043import android.os.BatteryManager;
Jim Miller9f0753f2015-03-23 23:59:22 -070044import android.os.CancellationSignal;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080045import android.os.Handler;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070046import android.os.IRemoteCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080047import android.os.Message;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -070048import android.os.PowerManager;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070049import android.os.RemoteException;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -070050import android.os.SystemClock;
Amith Yamasanie8e93a12013-05-09 18:12:30 -070051import android.os.UserHandle;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080052import android.provider.Settings;
Dianne Hackbornb7e787f2009-05-07 15:55:42 -070053
Wink Savillea639b312012-07-10 12:37:54 -070054import com.android.internal.telephony.IccCardConstants;
Jim Miller52a61332014-11-12 19:29:51 -080055import com.android.internal.telephony.IccCardConstants.State;
56import com.android.internal.telephony.PhoneConstants;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080057import com.android.internal.telephony.TelephonyIntents;
Jim Miller9f0753f2015-03-23 23:59:22 -070058
Jim Millerebbf2052015-03-31 17:24:34 -070059import android.hardware.fingerprint.FingerprintManager;
60import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
Jim Millerebbf2052015-03-31 17:24:34 -070061import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
Etan Cohen47051d82015-07-06 16:19:04 -070062import android.telephony.ServiceState;
Jim Miller52a61332014-11-12 19:29:51 -080063import android.telephony.SubscriptionInfo;
Jim Miller52a61332014-11-12 19:29:51 -080064import android.telephony.SubscriptionManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -080065import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Jim Millerc23024d2010-02-24 15:37:00 -080066import android.telephony.TelephonyManager;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -070067import android.util.ArraySet;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080068import android.util.Log;
Adrian Roos46842d92014-03-27 14:58:03 +010069import android.util.SparseBooleanArray;
Jorim Jaggi9f743032015-05-04 15:22:40 -070070import android.util.SparseIntArray;
Adrian Roos46842d92014-03-27 14:58:03 +010071
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080072import com.google.android.collect.Lists;
73
Jason Monkab525272015-07-13 17:02:49 -040074import java.io.FileDescriptor;
75import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -070076import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080077import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -080078import java.util.HashMap;
79import java.util.List;
80import java.util.Map.Entry;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080081
82/**
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;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -070097 private static final boolean DEBUG_FP_WAKELOCK = KeyguardConstants.DEBUG_FP_WAKELOCK;
Jim Millerbbf1a742012-07-17 18:30:30 -070098 private static final int LOW_BATTERY_THRESHOLD = 20;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -070099 private static final long FINGERPRINT_WAKELOCK_TIMEOUT_MS = 15 * 1000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800100
Jorim Jaggie7b12522014-08-06 16:41:21 +0200101 private static final String ACTION_FACE_UNLOCK_STARTED
102 = "com.android.facelock.FACE_UNLOCK_STARTED";
103 private static final String ACTION_FACE_UNLOCK_STOPPED
104 = "com.android.facelock.FACE_UNLOCK_STOPPED";
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700105 private static final String FINGERPRINT_WAKE_LOCK_NAME = "wake-and-unlock wakelock";
Jorim Jaggie7b12522014-08-06 16:41:21 +0200106
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700107 private static final String ACTION_STRONG_AUTH_TIMEOUT =
108 "com.android.systemui.ACTION_STRONG_AUTH_TIMEOUT";
109 private static final String USER_ID = "com.android.systemui.USER_ID";
110
111 private static final String PERMISSION_SELF = "com.android.systemui.permission.SELF";
112
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700113 /**
114 * Mode in which we don't need to wake up the device when we get a fingerprint.
115 */
116 private static final int FP_WAKE_NONE = 0;
117
118 /**
119 * Mode in which we wake up the device, and directly dismiss Keyguard. Active when we acquire
120 * a fingerprint while the screen is off and the device was sleeping.
121 */
122 private static final int FP_WAKE_DIRECT_UNLOCK = 1;
123
124 /**
125 * Mode in which we wake up the device, but play the normal dismiss animation. Active when we
126 * acquire a fingerprint pulsing in doze mode.
127 * */
Jorim Jaggi959431b2015-07-28 13:19:38 -0700128 private static final int FP_WAKE_TO_BOUNCER = 2;
129
130 /**
131 * Mode in which we only wake up the device, and keyguard was not showing when we acquired a
132 * fingerprint.
133 * */
134 private static final int FP_ONLY_WAKE = 3;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700135
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700136 /**
137 * Milliseconds after unlocking with fingerprint times out, i.e. the user has to use a
138 * strong auth method like password, PIN or pattern.
139 */
140 private static final long FINGERPRINT_UNLOCK_TIMEOUT_MS = 72 * 60 * 60 * 1000;
141
Jim Millerbbf1a742012-07-17 18:30:30 -0700142 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800143 private static final int MSG_TIME_UPDATE = 301;
144 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800145 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800146 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800147 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700148 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700149 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500150 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700151 private static final int MSG_KEYGUARD_VISIBILITY_CHANGED = 311;
152 private static final int MSG_KEYGUARD_RESET = 312;
Jim Millerf41fc962014-06-18 16:33:51 -0700153 private static final int MSG_BOOT_COMPLETED = 313;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500154 private static final int MSG_USER_SWITCH_COMPLETE = 314;
Jim Millerf41fc962014-06-18 16:33:51 -0700155 private static final int MSG_USER_INFO_CHANGED = 317;
156 private static final int MSG_REPORT_EMERGENCY_CALL_ACTION = 318;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700157 private static final int MSG_STARTED_WAKING_UP = 319;
158 private static final int MSG_FINISHED_GOING_TO_SLEEP = 320;
Adrian Roosb6011622014-05-14 15:52:53 +0200159 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700160 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
161 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400162 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700163 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700164 private static final int MSG_SCREEN_TURNED_ON = 331;
165 private static final int MSG_SCREEN_TURNED_OFF = 332;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800166
Jim Millerdcb3d842012-08-23 19:18:12 -0700167 private static KeyguardUpdateMonitor sInstance;
168
Jim Millerbbf1a742012-07-17 18:30:30 -0700169 private final Context mContext;
Jim Miller52a61332014-11-12 19:29:51 -0800170 HashMap<Integer, SimData> mSimDatas = new HashMap<Integer, SimData>();
Etan Cohen47051d82015-07-06 16:19:04 -0700171 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700172
Jim Millerbbf1a742012-07-17 18:30:30 -0700173 private int mRingMode;
174 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400175 private boolean mKeyguardIsVisible;
Adrian Roosb6011622014-05-14 15:52:53 +0200176 private boolean mBouncer;
Adam Cohen4eb36cf2012-11-07 11:45:30 -0800177 private boolean mBootCompleted;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700178 private boolean mUserHasAuthenticatedSinceBoot;
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. */
190 private ArraySet<Integer> mStrongAuthTimedOut = new ArraySet<>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700191
Jim Miller6212cc02012-09-05 17:35:31 -0700192 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700193 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800194 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700195
Brian Colonnaa5239892013-04-15 11:45:40 -0400196 private boolean mSwitchingUser;
197
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700198 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700199 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800200 private SubscriptionManager mSubscriptionManager;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700201 private AlarmManager mAlarmManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800202 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700203 private boolean mFingerprintDetectionRunning;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700204 private TrustManager mTrustManager;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700205 private PowerManager mPowerManager;
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;
Danielle Millettf6d0fc12012-10-23 16:16:52 -0400238 case MSG_KEYGUARD_VISIBILITY_CHANGED:
239 handleKeyguardVisibilityChanged(msg.arg1);
240 break;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700241 case MSG_KEYGUARD_RESET:
242 handleKeyguardReset();
243 break;
Adrian Roosb6011622014-05-14 15:52:53 +0200244 case MSG_KEYGUARD_BOUNCER_CHANGED:
245 handleKeyguardBouncerChanged(msg.arg1);
246 break;
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800247 case MSG_BOOT_COMPLETED:
248 handleBootCompleted();
249 break;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700250 case MSG_USER_INFO_CHANGED:
251 handleUserInfoChanged(msg.arg1);
252 break;
Brian Colonna7fce3802013-09-17 15:51:32 -0400253 case MSG_REPORT_EMERGENCY_CALL_ACTION:
254 handleReportEmergencyCallAction();
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
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700299 private int mFpWakeMode;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700300
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700301 public synchronized static void setCurrentUser(int currentUser) {
302 sCurrentUser = currentUser;
303 }
304
305 public synchronized static int getCurrentUser() {
306 return sCurrentUser;
307 }
308
Adrian Roos46842d92014-03-27 14:58:03 +0100309 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700310 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos46842d92014-03-27 14:58:03 +0100311 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200312 for (int i = 0; i < mCallbacks.size(); i++) {
313 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
314 if (cb != null) {
315 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700316 if (enabled && flags != 0) {
317 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200318 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200319 }
320 }
Adrian Roos46842d92014-03-27 14:58:03 +0100321 }
322
Jim Miller52a61332014-11-12 19:29:51 -0800323 protected void handleSimSubscriptionInfoChanged() {
324 if (DEBUG_SIM_STATES) {
325 Log.v(TAG, "onSubscriptionInfoChanged()");
Wink Savilled09c4ca2014-11-22 10:08:16 -0800326 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
327 if (sil != null) {
328 for (SubscriptionInfo subInfo : sil) {
329 Log.v(TAG, "SubInfo:" + subInfo);
330 }
331 } else {
332 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800333 }
334 }
335 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
336
337 // Hack level over 9000: Because the subscription id is not yet valid when we see the
338 // first update in handleSimStateChange, we need to force refresh all all SIM states
339 // so the subscription id for them is consistent.
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100340 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
Jim Miller52a61332014-11-12 19:29:51 -0800341 for (int i = 0; i < subscriptionInfos.size(); i++) {
342 SubscriptionInfo info = subscriptionInfos.get(i);
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100343 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
344 if (changed) {
345 changedSubscriptions.add(info);
346 }
Jim Miller52a61332014-11-12 19:29:51 -0800347 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +0100348 for (int i = 0; i < changedSubscriptions.size(); i++) {
349 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800350 for (int j = 0; j < mCallbacks.size(); j++) {
351 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
352 if (cb != null) {
353 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
354 }
355 }
356 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500357 for (int j = 0; j < mCallbacks.size(); j++) {
358 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
359 if (cb != null) {
360 cb.onRefreshCarrierInfo();
361 }
362 }
Jim Miller52a61332014-11-12 19:29:51 -0800363 }
364
Jason Monk052082c2015-06-11 11:35:23 -0400365 private void handleAirplaneModeChanged() {
366 for (int j = 0; j < mCallbacks.size(); j++) {
367 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
368 if (cb != null) {
369 cb.onRefreshCarrierInfo();
370 }
371 }
372 }
373
Wink Savilled09c4ca2014-11-22 10:08:16 -0800374 /** @return List of SubscriptionInfo records, maybe empty but never null */
Jim Miller52a61332014-11-12 19:29:51 -0800375 List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800376 List<SubscriptionInfo> sil = mSubscriptionInfo;
377 if (sil == null || forceReload) {
378 sil = mSubscriptionManager.getActiveSubscriptionInfoList();
379 }
380 if (sil == null) {
381 // getActiveSubscriptionInfoList was null callers expect an empty list.
382 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
383 } else {
384 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800385 }
386 return mSubscriptionInfo;
387 }
388
Adrian Roos7861c662014-07-25 15:37:28 +0200389 @Override
390 public void onTrustManagedChanged(boolean managed, int userId) {
391 mUserTrustIsManaged.put(userId, managed);
392
393 for (int i = 0; i < mCallbacks.size(); i++) {
394 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
395 if (cb != null) {
396 cb.onTrustManagedChanged(userId);
397 }
398 }
399 }
400
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700401 private void onFingerprintAuthenticated(int userId, boolean wakeAndUnlocking) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700402 mUserFingerprintAuthenticated.put(userId, true);
Jim Millerf41fc962014-06-18 16:33:51 -0700403 for (int i = 0; i < mCallbacks.size(); i++) {
404 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
405 if (cb != null) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700406 cb.onFingerprintAuthenticated(userId, wakeAndUnlocking);
Jim Millerf41fc962014-06-18 16:33:51 -0700407 }
408 }
409 }
410
Jim Millerce7eb6d2015-04-03 19:29:13 -0700411 private void handleFingerprintAuthFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700412 releaseFingerprintWakeLock();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700413 handleFingerprintHelp(-1, mContext.getString(R.string.fingerprint_not_recognized));
414 }
Jim Millerf41fc962014-06-18 16:33:51 -0700415
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700416 private void handleFingerprintAcquired(int acquireInfo) {
417 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
418 return;
419 }
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700420 if (!mDeviceInteractive && !mScreenOn) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700421 releaseFingerprintWakeLock();
422 mWakeLock = mPowerManager.newWakeLock(
423 PowerManager.PARTIAL_WAKE_LOCK, FINGERPRINT_WAKE_LOCK_NAME);
424 mWakeLock.acquire();
Jorim Jaggi959431b2015-07-28 13:19:38 -0700425 mFpWakeMode = mKeyguardIsVisible ? FP_WAKE_DIRECT_UNLOCK : FP_ONLY_WAKE;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700426 if (DEBUG_FP_WAKELOCK) {
427 Log.i(TAG, "fingerprint acquired, grabbing fp wakelock");
428 }
429 mHandler.postDelayed(mReleaseFingerprintWakeLockRunnable,
430 FINGERPRINT_WAKELOCK_TIMEOUT_MS);
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700431 } else if (!mDeviceInteractive) {
Jorim Jaggi959431b2015-07-28 13:19:38 -0700432 mFpWakeMode = FP_WAKE_TO_BOUNCER;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700433 } else {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700434 mFpWakeMode = FP_WAKE_NONE;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700435 }
436 }
437
438 private final Runnable mReleaseFingerprintWakeLockRunnable = new Runnable() {
439 @Override
440 public void run() {
441 if (DEBUG_FP_WAKELOCK) {
442 Log.i(TAG, "fp wakelock: TIMEOUT!!");
443 }
444 releaseFingerprintWakeLock();
445 }
446 };
447
448 private void releaseFingerprintWakeLock() {
449 if (mWakeLock != null) {
450 mHandler.removeCallbacks(mReleaseFingerprintWakeLockRunnable);
451 if (DEBUG_FP_WAKELOCK) {
452 Log.i(TAG, "releasing fp wakelock");
453 }
454 mWakeLock.release();
455 mWakeLock = null;
456 }
457 }
458
Jim Miller4f364c92015-06-08 19:24:13 -0700459 private void handleFingerprintAuthenticated() {
Jorim Jaggi959431b2015-07-28 13:19:38 -0700460 if (mFpWakeMode == FP_WAKE_TO_BOUNCER || mFpWakeMode == FP_WAKE_DIRECT_UNLOCK
461 || mFpWakeMode == FP_ONLY_WAKE) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700462 if (DEBUG_FP_WAKELOCK) {
463 Log.i(TAG, "fp wakelock: Authenticated, waking up...");
464 }
465 mPowerManager.wakeUp(SystemClock.uptimeMillis());
466 }
467 releaseFingerprintWakeLock();
Jim Millerf41fc962014-06-18 16:33:51 -0700468 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700469 final int userId;
470 try {
471 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
472 } catch (RemoteException e) {
473 Log.e(TAG, "Failed to get current user id: ", e);
474 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700475 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700476 if (isFingerprintDisabled(userId)) {
477 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
478 return;
479 }
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700480 onFingerprintAuthenticated(userId, mFpWakeMode == FP_WAKE_DIRECT_UNLOCK);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700481 } finally {
482 setFingerprintRunningDetectionRunning(false);
Jim Millerf41fc962014-06-18 16:33:51 -0700483 }
484 }
485
Jim Miller9f0753f2015-03-23 23:59:22 -0700486 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700487 for (int i = 0; i < mCallbacks.size(); i++) {
488 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
489 if (cb != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700490 cb.onFingerprintHelp(msgId, helpString);
491 }
492 }
493 }
494
495 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700496 setFingerprintRunningDetectionRunning(false);
Jim Miller9f0753f2015-03-23 23:59:22 -0700497 for (int i = 0; i < mCallbacks.size(); i++) {
498 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
499 if (cb != null) {
500 cb.onFingerprintError(msgId, errString);
Jim Millerf41fc962014-06-18 16:33:51 -0700501 }
502 }
503 }
504
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700505 private void setFingerprintRunningDetectionRunning(boolean running) {
506 if (running != mFingerprintDetectionRunning) {
507 mFingerprintDetectionRunning = running;
508 notifyFingerprintRunningStateChanged();
509 }
510 }
511
512 private void notifyFingerprintRunningStateChanged() {
513 for (int i = 0; i < mCallbacks.size(); i++) {
514 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
515 if (cb != null) {
516 cb.onFingerprintRunningStateChanged(mFingerprintDetectionRunning);
517 }
518 }
519 }
Adrian Roos4a410172014-08-20 17:41:44 +0200520 private void handleFaceUnlockStateChanged(boolean running, int userId) {
521 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200522 for (int i = 0; i < mCallbacks.size(); i++) {
523 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
524 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200525 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200526 }
527 }
528 }
529
Adrian Roos4a410172014-08-20 17:41:44 +0200530 public boolean isFaceUnlockRunning(int userId) {
531 return mUserFaceUnlockRunning.get(userId);
532 }
533
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700534 public boolean isFingerprintDetectionRunning() {
535 return mFingerprintDetectionRunning;
536 }
537
Jim Miller50e62182014-04-23 17:25:00 -0700538 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100539 // Don't allow trust agent if device is secured with a SIM PIN. This is here
540 // mainly because there's no other way to prompt the user to enter their SIM PIN
541 // once they get past the keyguard screen.
542 final boolean disabledBySimPin = isSimPinSecure();
543 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700544 }
545
Jim Miller06e34502014-07-17 14:46:05 -0700546 private boolean isFingerprintDisabled(int userId) {
547 final DevicePolicyManager dpm =
548 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
549 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
550 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0;
551 }
552
Selim Cineke8bae622015-07-15 13:24:06 -0700553 public boolean getUserCanSkipBouncer(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700554 return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
555 && isUnlockingWithFingerprintAllowed());
Selim Cineke8bae622015-07-15 13:24:06 -0700556 }
557
Adrian Roos46842d92014-03-27 14:58:03 +0100558 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700559 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100560 }
561
Adrian Roos7861c662014-07-25 15:37:28 +0200562 public boolean getUserTrustIsManaged(int userId) {
563 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
564 }
565
Selim Cinek1fcafc42015-07-20 14:39:25 -0700566 public boolean isUnlockingWithFingerprintAllowed() {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700567 return mUserHasAuthenticatedSinceBoot && !hasFingerprintUnlockTimedOut(sCurrentUser);
568 }
569
570 /**
571 * @return true if the user hasn't use strong authentication (pattern, PIN, password) since a
572 * while and thus can't unlock with fingerprint, false otherwise
573 */
574 public boolean hasFingerprintUnlockTimedOut(int userId) {
575 return mStrongAuthTimedOut.contains(userId);
576 }
577
578 public void reportSuccessfulStrongAuthUnlockAttempt() {
579 mStrongAuthTimedOut.remove(sCurrentUser);
580 scheduleStrongAuthTimeout();
581 }
582
583 private void scheduleStrongAuthTimeout() {
584 long when = SystemClock.elapsedRealtime() + FINGERPRINT_UNLOCK_TIMEOUT_MS;
585 Intent intent = new Intent(ACTION_STRONG_AUTH_TIMEOUT);
586 intent.putExtra(USER_ID, sCurrentUser);
587 PendingIntent sender = PendingIntent.getBroadcast(mContext,
588 sCurrentUser, intent, PendingIntent.FLAG_CANCEL_CURRENT);
589 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, when, sender);
590 notifyStrongAuthTimedOutChanged(sCurrentUser);
591 }
592
593 private void notifyStrongAuthTimedOutChanged(int userId) {
594 for (int i = 0; i < mCallbacks.size(); i++) {
595 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
596 if (cb != null) {
597 cb.onStrongAuthTimeoutExpiredChanged(userId);
598 }
599 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700600 }
601
Jim Miller8f09fd22013-03-14 19:04:28 -0700602 static class DisplayClientState {
603 public int clientGeneration;
604 public boolean clearing;
605 public PendingIntent intent;
606 public int playbackState;
607 public long playbackEventTime;
608 }
609
610 private DisplayClientState mDisplayClientState = new DisplayClientState();
611
Jim Millerbbf1a742012-07-17 18:30:30 -0700612 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
613
Jim Miller8ae1be12015-08-05 13:03:21 -0700614 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700615 public void onReceive(Context context, Intent intent) {
616 final String action = intent.getAction();
617 if (DEBUG) Log.d(TAG, "received broadcast " + action);
618
619 if (Intent.ACTION_TIME_TICK.equals(action)
620 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200621 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700622 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700623 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
624 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
625 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
626 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
627 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos7b043112015-07-10 13:00:33 -0700628 final int maxChargingCurrent = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700629 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700630 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
631 maxChargingCurrent));
Jim Millerbbf1a742012-07-17 18:30:30 -0700632 mHandler.sendMessage(msg);
633 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Jim Miller52a61332014-11-12 19:29:51 -0800634 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700635 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800636 Log.v(TAG, "action " + action
637 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
638 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700639 }
Jim Miller52a61332014-11-12 19:29:51 -0800640 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
641 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700642 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
643 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
644 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
645 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
646 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
647 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400648 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
649 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800650 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700651 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700652 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
653 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
654 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
655 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
656 if (DEBUG) {
657 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
658 + subId);
659 }
660 mHandler.sendMessage(
661 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Jim Millerbbf1a742012-07-17 18:30:30 -0700662 }
663 }
664 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700665
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700666 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
667
Jim Miller8ae1be12015-08-05 13:03:21 -0700668 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700669 public void onReceive(Context context, Intent intent) {
670 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200671 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
672 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
673 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700674 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
675 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200676 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
677 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
678 getSendingUserId()));
679 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
680 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
681 getSendingUserId()));
682 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
683 .equals(action)) {
684 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700685 }
686 }
687 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700688
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700689 private final BroadcastReceiver mStrongAuthTimeoutReceiver = new BroadcastReceiver() {
690 @Override
691 public void onReceive(Context context, Intent intent) {
692 if (ACTION_STRONG_AUTH_TIMEOUT.equals(intent.getAction())) {
693 int userId = intent.getIntExtra(USER_ID, -1);
694 mStrongAuthTimedOut.add(userId);
695 notifyStrongAuthTimedOutChanged(userId);
696 }
697 }
698 };
699
Jim Miller9f0753f2015-03-23 23:59:22 -0700700 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
701 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700702
703 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700704 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700705 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700706 };
707
708 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700709 public void onAuthenticationSucceeded(AuthenticationResult result) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700710 handleFingerprintAuthenticated();
Jim Millerf41fc962014-06-18 16:33:51 -0700711 }
712
713 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700714 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700715 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700716 }
717
718 @Override
719 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700720 handleFingerprintError(errMsgId, errString.toString());
721 }
722
723 @Override
724 public void onAuthenticationAcquired(int acquireInfo) {
725 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700726 }
727 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700728 private CancellationSignal mFingerprintCancelSignal;
729 private FingerprintManager mFpm;
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700730 private PowerManager.WakeLock mWakeLock;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700731
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800732 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800733 * When we receive a
734 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700735 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800736 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800737 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800738 */
Jim Miller52a61332014-11-12 19:29:51 -0800739 private static class SimData {
740 public State simState;
741 public int slotId;
742 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800743
Jim Miller52a61332014-11-12 19:29:51 -0800744 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800745 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800746 slotId = slot;
747 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800748 }
749
Jim Miller52a61332014-11-12 19:29:51 -0800750 static SimData fromIntent(Intent intent) {
751 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800752 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
753 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
754 }
Wink Savillea639b312012-07-10 12:37:54 -0700755 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800756 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
757 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800758 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700759 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700760 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700761 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700762
Wink Savillea639b312012-07-10 12:37:54 -0700763 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700764 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700765 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700766 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700767 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700768 }
Wink Savillea639b312012-07-10 12:37:54 -0700769 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
770 state = IccCardConstants.State.READY;
771 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800772 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700773 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
774 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
775 state = IccCardConstants.State.PIN_REQUIRED;
776 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
777 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800778 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700779 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800780 }
Wink Savillea639b312012-07-10 12:37:54 -0700781 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
782 state = IccCardConstants.State.NETWORK_LOCKED;
Jim Miller109f1fd2012-09-19 20:44:16 -0700783 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
784 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
785 // This is required because telephony doesn't return to "READY" after
786 // these state transitions. See bug 7197471.
787 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800788 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700789 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800790 }
Jim Miller52a61332014-11-12 19:29:51 -0800791 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800792 }
793
Jim Miller8ae1be12015-08-05 13:03:21 -0700794 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800795 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800796 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800797 }
798 }
799
Adrian Roos12c1ef52014-06-04 13:54:08 +0200800 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700801 public static final int CHARGING_UNKNOWN = -1;
802 public static final int CHARGING_SLOWLY = 0;
803 public static final int CHARGING_REGULAR = 1;
804 public static final int CHARGING_FAST = 2;
805
Jim Miller16464b82011-10-20 21:10:13 -0700806 public final int status;
807 public final int level;
808 public final int plugged;
809 public final int health;
Adrian Roos7b043112015-07-10 13:00:33 -0700810 public final int maxChargingCurrent;
811 public BatteryStatus(int status, int level, int plugged, int health, int maxChargingCurrent) {
Jim Miller16464b82011-10-20 21:10:13 -0700812 this.status = status;
813 this.level = level;
814 this.plugged = plugged;
815 this.health = health;
Adrian Roos7b043112015-07-10 13:00:33 -0700816 this.maxChargingCurrent = maxChargingCurrent;
Jim Miller16464b82011-10-20 21:10:13 -0700817 }
818
Jim Millerbbf1a742012-07-17 18:30:30 -0700819 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700820 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700821 * @return true if the device is plugged in.
822 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100823 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700824 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700825 || plugged == BatteryManager.BATTERY_PLUGGED_USB
826 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -0700827 }
828
829 /**
830 * Whether or not the device is charged. Note that some devices never return 100% for
831 * battery level, so this allows either battery level or status to determine if the
832 * battery is charged.
833 * @return true if the device is charged
834 */
835 public boolean isCharged() {
836 return status == BATTERY_STATUS_FULL || level >= 100;
837 }
838
839 /**
840 * Whether battery is low and needs to be charged.
841 * @return true if battery is low
842 */
843 public boolean isBatteryLow() {
844 return level < LOW_BATTERY_THRESHOLD;
845 }
846
Adrian Roos7b043112015-07-10 13:00:33 -0700847 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
848 return maxChargingCurrent <= 0 ? CHARGING_UNKNOWN :
849 maxChargingCurrent < slowThreshold ? CHARGING_SLOWLY :
850 maxChargingCurrent > fastThreshold ? CHARGING_FAST :
851 CHARGING_REGULAR;
852 }
Jim Miller16464b82011-10-20 21:10:13 -0700853 }
854
Jim Millerdcb3d842012-08-23 19:18:12 -0700855 public static KeyguardUpdateMonitor getInstance(Context context) {
856 if (sInstance == null) {
857 sInstance = new KeyguardUpdateMonitor(context);
858 }
859 return sInstance;
860 }
861
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700862 protected void handleStartedWakingUp() {
Jorim Jaggi864e64b2015-05-20 14:13:23 -0700863 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700864 final int count = mCallbacks.size();
865 for (int i = 0; i < count; i++) {
866 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
867 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700868 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -0700869 }
870 }
871 }
872
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700873 protected void handleFinishedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -0700874 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -0700875 final int count = mCallbacks.size();
876 for (int i = 0; i < count; i++) {
877 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
878 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700879 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700880 }
881 }
Jorim Jaggiea657062015-04-28 13:45:11 -0700882 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -0700883 }
884
Jorim Jaggif1518da2015-07-30 11:56:36 -0700885 private void handleScreenTurnedOn() {
886 final int count = mCallbacks.size();
887 for (int i = 0; i < count; i++) {
888 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
889 if (cb != null) {
890 cb.onScreenTurnedOn();
891 }
892 }
893 }
894
895 private void handleScreenTurnedOff() {
896 final int count = mCallbacks.size();
897 for (int i = 0; i < count; i++) {
898 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
899 if (cb != null) {
900 cb.onScreenTurnedOff();
901 }
902 }
903 }
904
Adam Powell43a372f2013-09-27 17:43:53 -0700905 /**
906 * IMPORTANT: Must be called from UI thread.
907 */
908 public void dispatchSetBackground(Bitmap bmp) {
909 if (DEBUG) Log.d(TAG, "dispatchSetBackground");
910 final int count = mCallbacks.size();
911 for (int i = 0; i < count; i++) {
912 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
913 if (cb != null) {
914 cb.onSetBackground(bmp);
915 }
916 }
917 }
918
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700919 private void handleUserInfoChanged(int userId) {
920 for (int i = 0; i < mCallbacks.size(); i++) {
921 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
922 if (cb != null) {
923 cb.onUserInfoChanged(userId);
924 }
925 }
926 }
927
Jim Millerdcb3d842012-08-23 19:18:12 -0700928 private KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800929 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800930 mSubscriptionManager = SubscriptionManager.from(context);
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700931 mPowerManager = context.getSystemService(PowerManager.class);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700932 mAlarmManager = context.getSystemService(AlarmManager.class);
Michael Jurkafff56142012-11-28 16:51:00 -0800933 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700934
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800935 // Since device can't be un-provisioned, we only need to register a content observer
936 // to update mDeviceProvisioned when we are...
937 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700938 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800939 }
Jim Miller47088bb2009-11-24 00:40:16 -0800940
Jim Millerbbf1a742012-07-17 18:30:30 -0700941 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -0700942 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800943
Jim Millerbbf1a742012-07-17 18:30:30 -0700944 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800945 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800946 filter.addAction(Intent.ACTION_TIME_TICK);
947 filter.addAction(Intent.ACTION_TIME_CHANGED);
948 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
949 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -0400950 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800951 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -0700952 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -0800953 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -0800954 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Jim Millerbbf1a742012-07-17 18:30:30 -0700955 context.registerReceiver(mBroadcastReceiver, filter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700956
Adam Cohenc276e822012-11-08 13:01:08 -0800957 final IntentFilter bootCompleteFilter = new IntentFilter();
958 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
959 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
960 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter);
961
Adrian Roos48c796c2014-09-01 14:59:23 +0200962 final IntentFilter allUserFilter = new IntentFilter();
963 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
964 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
965 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
966 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
967 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
968 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700969 null, null);
970
Wink Saville071743f2015-01-12 17:11:04 -0800971 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700972 try {
973 ActivityManagerNative.getDefault().registerUserSwitchObserver(
974 new IUserSwitchObserver.Stub() {
975 @Override
976 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500977 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700978 newUserId, 0, reply));
979 }
980 @Override
981 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500982 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +0200983 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700984 }
Kenny Guy42979622015-04-13 18:03:05 +0000985 @Override
986 public void onForegroundProfileSwitch(int newProfileId) {
987 // Ignore.
988 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700989 });
990 } catch (RemoteException e) {
991 // TODO Auto-generated catch block
992 e.printStackTrace();
993 }
Adrian Roos46842d92014-03-27 14:58:03 +0100994
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700995 IntentFilter strongAuthTimeoutFilter = new IntentFilter();
996 strongAuthTimeoutFilter.addAction(ACTION_STRONG_AUTH_TIMEOUT);
997 context.registerReceiver(mStrongAuthTimeoutReceiver, strongAuthTimeoutFilter,
998 PERMISSION_SELF, null /* handler */);
Jorim Jaggi237b0612015-05-01 14:28:49 -0700999 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1000 mTrustManager.registerTrustListener(this);
Jim Millerf41fc962014-06-18 16:33:51 -07001001
Jim Miller9f0753f2015-03-23 23:59:22 -07001002 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
Jorim Jaggiea657062015-04-28 13:45:11 -07001003 updateFingerprintListeningState();
1004 }
1005
1006 private void updateFingerprintListeningState() {
1007 boolean shouldListenForFingerprint = shouldListenForFingerprint();
1008 if (mFingerprintDetectionRunning && !shouldListenForFingerprint) {
1009 stopListeningForFingerprint();
1010 } else if (!mFingerprintDetectionRunning && shouldListenForFingerprint) {
1011 startListeningForFingerprint();
1012 }
1013 }
1014
1015 private boolean shouldListenForFingerprint() {
Jorim Jaggi959431b2015-07-28 13:19:38 -07001016 return (mKeyguardIsVisible || !mDeviceInteractive) && !mSwitchingUser;
Jim Miller9f0753f2015-03-23 23:59:22 -07001017 }
1018
Jim Millerce7eb6d2015-04-03 19:29:13 -07001019 private void startListeningForFingerprint() {
1020 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001021 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001022 if (isUnlockWithFingerprintPossible(userId)) {
Selim Cinek1fcafc42015-07-20 14:39:25 -07001023 mUserHasAuthenticatedSinceBoot = mTrustManager.hasUserAuthenticatedSinceBoot(
1024 ActivityManager.getCurrentUser());
Jim Millerce7eb6d2015-04-03 19:29:13 -07001025 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001026 mFingerprintCancelSignal.cancel();
1027 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001028 mFingerprintCancelSignal = new CancellationSignal();
Jim Millerf501b582015-06-03 16:36:31 -07001029 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null, userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -07001030 setFingerprintRunningDetectionRunning(true);
Jim Miller9f0753f2015-03-23 23:59:22 -07001031 }
1032 }
1033
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001034 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001035 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1036 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1037 }
1038
Jorim Jaggiea657062015-04-28 13:45:11 -07001039 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001040 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
1041 if (isFingerprintDetectionRunning()) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001042 mFingerprintCancelSignal.cancel();
Jim Millerce7eb6d2015-04-03 19:29:13 -07001043 mFingerprintCancelSignal = null;
Jim Miller9f0753f2015-03-23 23:59:22 -07001044 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -07001045 setFingerprintRunningDetectionRunning(false);
Jim Millerbbf1a742012-07-17 18:30:30 -07001046 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001047
Michael Jurkafff56142012-11-28 16:51:00 -08001048 private boolean isDeviceProvisionedInSettingsDb() {
1049 return Settings.Global.getInt(mContext.getContentResolver(),
1050 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1051 }
1052
Jim Millerbbf1a742012-07-17 18:30:30 -07001053 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001054 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001055 @Override
1056 public void onChange(boolean selfChange) {
1057 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001058 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001059 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001060 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001061 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001062 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001063 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001064 };
1065
1066 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001067 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001068 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001069
1070 // prevent a race condition between where we check the flag and where we register the
1071 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001072 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001073 if (provisioned != mDeviceProvisioned) {
1074 mDeviceProvisioned = provisioned;
1075 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001076 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001077 }
1078 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001079 }
1080
Jim Millerbbf1a742012-07-17 18:30:30 -07001081 /**
1082 * Handle {@link #MSG_DPM_STATE_CHANGED}
1083 */
Jim Millerb0304762012-03-13 20:01:25 -07001084 protected void handleDevicePolicyManagerStateChanged() {
Jim Millerdcb3d842012-08-23 19:18:12 -07001085 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1086 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1087 if (cb != null) {
1088 cb.onDevicePolicyManagerStateChanged();
1089 }
Jim Millerb0304762012-03-13 20:01:25 -07001090 }
1091 }
1092
Jim Millerbbf1a742012-07-17 18:30:30 -07001093 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001094 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001095 */
Chris Wrenf41c61b2012-11-29 15:19:54 -05001096 protected void handleUserSwitching(int userId, IRemoteCallback reply) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001097 mSwitchingUser = true;
1098 updateFingerprintListeningState();
1099
Jim Millerbbf1a742012-07-17 18:30:30 -07001100 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001101 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1102 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001103 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001104 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001105 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001106 try {
1107 reply.sendResult(null);
1108 } catch (RemoteException e) {
1109 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001110 }
1111
Jim Millerbbf1a742012-07-17 18:30:30 -07001112 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001113 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1114 */
1115 protected void handleUserSwitchComplete(int userId) {
Jorim Jaggiaa4d32a2015-05-13 16:30:04 -07001116 mSwitchingUser = false;
1117 updateFingerprintListeningState();
1118
Chris Wrenf41c61b2012-11-29 15:19:54 -05001119 for (int i = 0; i < mCallbacks.size(); i++) {
1120 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1121 if (cb != null) {
1122 cb.onUserSwitchComplete(userId);
1123 }
1124 }
1125 }
1126
1127 /**
Jim Miller90873d52013-09-26 18:11:38 -07001128 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1129 * keyguard crashes sometime after boot, then it will never receive this
1130 * broadcast and hence not handle the event. This method is ultimately called by
1131 * PhoneWindowManager in this case.
1132 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001133 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001134 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001135 }
1136
1137 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001138 * Handle {@link #MSG_BOOT_COMPLETED}
1139 */
1140 protected void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001141 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001142 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001143 for (int i = 0; i < mCallbacks.size(); i++) {
1144 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1145 if (cb != null) {
1146 cb.onBootCompleted();
1147 }
1148 }
1149 }
1150
1151 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001152 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001153 * destroyed.
1154 */
1155 public boolean hasBootCompleted() {
1156 return mBootCompleted;
1157 }
1158
1159 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001160 * Handle {@link #MSG_DEVICE_PROVISIONED}
1161 */
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001162 protected void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001163 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001164 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1165 if (cb != null) {
1166 cb.onDeviceProvisioned();
1167 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001168 }
Michael Jurkafff56142012-11-28 16:51:00 -08001169 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001170 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001171 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1172 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001173 }
1174 }
1175
Jim Millerbbf1a742012-07-17 18:30:30 -07001176 /**
1177 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1178 */
Jim Millerc23024d2010-02-24 15:37:00 -08001179 protected void handlePhoneStateChanged(String newState) {
1180 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001181 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1182 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1183 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1184 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1185 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1186 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1187 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001188 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001189 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1190 if (cb != null) {
1191 cb.onPhoneStateChanged(mPhoneState);
1192 }
Jim Millerc23024d2010-02-24 15:37:00 -08001193 }
1194 }
1195
Jim Millerbbf1a742012-07-17 18:30:30 -07001196 /**
1197 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1198 */
Jim Miller47088bb2009-11-24 00:40:16 -08001199 protected void handleRingerModeChange(int mode) {
1200 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001201 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001202 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001203 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1204 if (cb != null) {
1205 cb.onRingerModeChanged(mode);
1206 }
Jim Miller47088bb2009-11-24 00:40:16 -08001207 }
1208 }
1209
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001210 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001211 * Handle {@link #MSG_TIME_UPDATE}
1212 */
1213 private void handleTimeUpdate() {
1214 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001215 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001216 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1217 if (cb != null) {
1218 cb.onTimeChanged();
1219 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001220 }
1221 }
1222
1223 /**
1224 * Handle {@link #MSG_BATTERY_UPDATE}
1225 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001226 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001227 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001228 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1229 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001230 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001231 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001232 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1233 if (cb != null) {
1234 cb.onRefreshBatteryInfo(status);
1235 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001236 }
1237 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001238 }
1239
1240 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001241 * Handle {@link #MSG_SIM_STATE_CHANGE}
1242 */
Jim Miller52a61332014-11-12 19:29:51 -08001243 private void handleSimStateChange(int subId, int slotId, State state) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001244
Jim Miller52a61332014-11-12 19:29:51 -08001245 if (DEBUG_SIM_STATES) {
1246 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1247 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001248 }
1249
Wink Savillea54bf652014-12-11 13:37:50 -08001250 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001251 Log.w(TAG, "invalid subId in handleSimStateChange()");
1252 return;
1253 }
1254
1255 SimData data = mSimDatas.get(subId);
1256 final boolean changed;
1257 if (data == null) {
1258 data = new SimData(state, slotId, subId);
1259 mSimDatas.put(subId, data);
1260 changed = true; // no data yet; force update
1261 } else {
1262 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1263 data.simState = state;
1264 data.subId = subId;
1265 data.slotId = slotId;
1266 }
1267 if (changed && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001268 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001269 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1270 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08001271 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001272 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001273 }
1274 }
1275 }
1276
Jim Millerbbf1a742012-07-17 18:30:30 -07001277 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001278 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1279 */
1280 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1281 if (DEBUG) {
1282 Log.d(TAG,
1283 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1284 }
1285
1286 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1287 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1288 return;
1289 }
1290
1291 mServiceStates.put(subId, serviceState);
1292
1293 for (int j = 0; j < mCallbacks.size(); j++) {
1294 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1295 if (cb != null) {
1296 cb.onRefreshCarrierInfo();
1297 }
1298 }
1299 }
1300
1301 /**
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001302 * Handle {@link #MSG_KEYGUARD_VISIBILITY_CHANGED}
1303 */
1304 private void handleKeyguardVisibilityChanged(int showing) {
1305 if (DEBUG) Log.d(TAG, "handleKeyguardVisibilityChanged(" + showing + ")");
Danielle Millett5d2404d2012-11-01 00:05:27 -04001306 boolean isShowing = (showing == 1);
1307 mKeyguardIsVisible = isShowing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001308 for (int i = 0; i < mCallbacks.size(); i++) {
1309 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1310 if (cb != null) {
John Spurlock385a63d2013-10-30 19:40:48 -04001311 cb.onKeyguardVisibilityChangedRaw(isShowing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001312 }
1313 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001314 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001315 }
1316
Brian Colonna7fce3802013-09-17 15:51:32 -04001317 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001318 * Handle {@link #MSG_KEYGUARD_RESET}
1319 */
1320 private void handleKeyguardReset() {
1321 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
1322 if (!isUnlockingWithFingerprintAllowed()) {
1323 updateFingerprintListeningState();
1324 }
1325 }
1326
1327 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001328 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1329 * @see #sendKeyguardBouncerChanged(boolean)
1330 */
1331 private void handleKeyguardBouncerChanged(int bouncer) {
1332 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1333 boolean isBouncer = (bouncer == 1);
1334 mBouncer = isBouncer;
1335 for (int i = 0; i < mCallbacks.size(); i++) {
1336 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1337 if (cb != null) {
1338 cb.onKeyguardBouncerChanged(isBouncer);
1339 }
1340 }
1341 }
1342
1343 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001344 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1345 */
1346 private void handleReportEmergencyCallAction() {
1347 for (int i = 0; i < mCallbacks.size(); i++) {
1348 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1349 if (cb != null) {
1350 cb.onEmergencyCallAction();
1351 }
1352 }
1353 }
1354
Jim Miller16464b82011-10-20 21:10:13 -07001355 private static boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001356 final boolean nowPluggedIn = current.isPluggedIn();
1357 final boolean wasPluggedIn = old.isPluggedIn();
Jim Miller79a444a2011-02-15 15:02:11 -08001358 final boolean stateChangedWhilePluggedIn =
Jim Miller16464b82011-10-20 21:10:13 -07001359 wasPluggedIn == true && nowPluggedIn == true
1360 && (old.status != current.status);
1361
1362 // change in plug state is always interesting
1363 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001364 return true;
1365 }
1366
1367 // change in battery level while plugged in
Jim Miller16464b82011-10-20 21:10:13 -07001368 if (nowPluggedIn && old.level != current.level) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001369 return true;
1370 }
1371
Jim Miller16464b82011-10-20 21:10:13 -07001372 // change where battery needs charging
Jim Millerbbf1a742012-07-17 18:30:30 -07001373 if (!nowPluggedIn && current.isBatteryLow() && current.level != old.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001374 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001375 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001376
1377 // change in charging current while plugged in
1378 if (nowPluggedIn && current.maxChargingCurrent != old.maxChargingCurrent) {
1379 return true;
1380 }
1381
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001382 return false;
1383 }
1384
1385 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001386 * Remove the given observer's callback.
1387 *
Jim Miller6212cc02012-09-05 17:35:31 -07001388 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001389 */
Jim Miller6212cc02012-09-05 17:35:31 -07001390 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
1391 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1392 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1393 if (mCallbacks.get(i).get() == callback) {
1394 mCallbacks.remove(i);
1395 }
1396 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001397 }
1398
1399 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001400 * Register to receive notifications about general keyguard information
1401 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001402 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001403 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001404 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Jim Miller6212cc02012-09-05 17:35:31 -07001405 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1406 // Prevent adding duplicate callbacks
1407 for (int i = 0; i < mCallbacks.size(); i++) {
1408 if (mCallbacks.get(i).get() == callback) {
1409 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1410 new Exception("Called by"));
1411 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001412 }
1413 }
Jim Miller6212cc02012-09-05 17:35:31 -07001414 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1415 removeCallback(null); // remove unused references
1416 sendUpdates(callback);
1417 }
1418
1419 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1420 // Notify listener of the current state
1421 callback.onRefreshBatteryInfo(mBatteryStatus);
1422 callback.onTimeChanged();
1423 callback.onRingerModeChanged(mRingMode);
1424 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001425 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001426 callback.onClockVisibilityChanged();
Jim Miller52a61332014-11-12 19:29:51 -08001427 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1428 final SimData state = data.getValue();
1429 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1430 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001431 }
1432
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001433 public void sendKeyguardVisibilityChanged(boolean showing) {
1434 if (DEBUG) Log.d(TAG, "sendKeyguardVisibilityChanged(" + showing + ")");
1435 Message message = mHandler.obtainMessage(MSG_KEYGUARD_VISIBILITY_CHANGED);
1436 message.arg1 = showing ? 1 : 0;
1437 message.sendToTarget();
1438 }
1439
Selim Cinek1fcafc42015-07-20 14:39:25 -07001440 public void sendKeyguardReset() {
1441 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1442 }
1443
Adrian Roosb6011622014-05-14 15:52:53 +02001444 /**
1445 * @see #handleKeyguardBouncerChanged(int)
1446 */
1447 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1448 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1449 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1450 message.arg1 = showingBouncer ? 1 : 0;
1451 message.sendToTarget();
1452 }
1453
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001454 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001455 * 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 -08001456 * have the information earlier than waiting for the intent
1457 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001458 *
1459 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1460 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001461 */
Jim Miller52a61332014-11-12 19:29:51 -08001462 public void reportSimUnlocked(int subId) {
1463 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
1464 int slotId = SubscriptionManager.getSlotId(subId);
1465 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001466 }
1467
Brian Colonna7fce3802013-09-17 15:51:32 -04001468 /**
1469 * Report that the emergency call button has been pressed and the emergency dialer is
1470 * about to be displayed.
1471 *
1472 * @param bypassHandler runs immediately.
1473 *
1474 * NOTE: Must be called from UI thread if bypassHandler == true.
1475 */
1476 public void reportEmergencyCallAction(boolean bypassHandler) {
1477 if (!bypassHandler) {
1478 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1479 } else {
1480 handleReportEmergencyCallAction();
1481 }
1482 }
1483
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001484 /**
1485 * @return Whether the device is provisioned (whether they have gone through
1486 * the setup wizard)
1487 */
1488 public boolean isDeviceProvisioned() {
1489 return mDeviceProvisioned;
1490 }
1491
Jorim Jaggi9f743032015-05-04 15:22:40 -07001492 public void clearFailedUnlockAttempts() {
1493 mFailedAttempts.delete(sCurrentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001494 }
1495
Jorim Jaggi9f743032015-05-04 15:22:40 -07001496 public int getFailedUnlockAttempts() {
1497 return mFailedAttempts.get(sCurrentUser, 0);
1498 }
1499
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001500 public void reportFailedStrongAuthUnlockAttempt() {
Jorim Jaggi9f743032015-05-04 15:22:40 -07001501 mFailedAttempts.put(sCurrentUser, getFailedUnlockAttempts() + 1);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001502 }
1503
Jim Millerf41fc962014-06-18 16:33:51 -07001504 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001505 mUserFingerprintAuthenticated.clear();
Jim Millerf41fc962014-06-18 16:33:51 -07001506 }
1507
Jim Miller52a61332014-11-12 19:29:51 -08001508 public boolean isSimPinVoiceSecure() {
1509 // TODO: only count SIMs that handle voice
1510 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001511 }
1512
1513 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001514 // True if any SIM is pin secure
1515 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1516 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1517 }
1518 return false;
1519 }
1520
Jason Monk9ff69bd2014-12-02 16:43:17 -05001521 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001522 if (mSimDatas.containsKey(subId)) {
1523 return mSimDatas.get(subId).simState;
1524 } else {
1525 return State.UNKNOWN;
1526 }
1527 }
1528
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001529 /**
1530 * @return true if and only if the state has changed for the specified {@code slotId}
1531 */
1532 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08001533
1534 // This is awful. It exists because there are two APIs for getting the SIM status
1535 // that don't return the complete set of values and have different types. In Keyguard we
1536 // need IccCardConstants, but TelephonyManager would only give us
1537 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001538 final TelephonyManager tele = TelephonyManager.from(mContext);
1539 int simState = tele.getSimState(slotId);
Jim Miller52a61332014-11-12 19:29:51 -08001540 State state;
1541 try {
xinhe18b9c3c2014-12-02 15:03:20 -08001542 state = State.intToState(simState);
Jim Miller52a61332014-11-12 19:29:51 -08001543 } catch(IllegalArgumentException ex) {
xinhe18b9c3c2014-12-02 15:03:20 -08001544 Log.w(TAG, "Unknown sim state: " + simState);
Jim Miller52a61332014-11-12 19:29:51 -08001545 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05001546 }
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001547 SimData data = mSimDatas.get(subId);
1548 final boolean changed;
1549 if (data == null) {
1550 data = new SimData(state, slotId, subId);
1551 mSimDatas.put(subId, data);
1552 changed = true; // no data yet; force update
1553 } else {
1554 changed = data.simState != state;
1555 data.simState = state;
1556 }
1557 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07001558 }
1559
1560 public static boolean isSimPinSecure(IccCardConstants.State state) {
1561 final IccCardConstants.State simState = state;
1562 return (simState == IccCardConstants.State.PIN_REQUIRED
1563 || simState == IccCardConstants.State.PUK_REQUIRED
1564 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001565 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001566
1567 public DisplayClientState getCachedDisplayClientState() {
1568 return mDisplayClientState;
1569 }
Jim Miller20daffd2013-10-07 14:59:53 -07001570
1571 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1572 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001573 public void dispatchStartedWakingUp() {
1574 synchronized (this) {
1575 mDeviceInteractive = true;
1576 }
1577 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1578 }
1579
1580 public void dispatchFinishedGoingToSleep(int why) {
1581 synchronized(this) {
1582 mDeviceInteractive = false;
1583 }
1584 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1585 }
1586
Jim Miller20daffd2013-10-07 14:59:53 -07001587 public void dispatchScreenTurnedOn() {
1588 synchronized (this) {
1589 mScreenOn = true;
1590 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001591 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001592 }
1593
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001594 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001595 synchronized(this) {
1596 mScreenOn = false;
1597 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001598 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001599 }
1600
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001601 public boolean isDeviceInteractive() {
1602 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001603 }
Jim Miller52a61332014-11-12 19:29:51 -08001604
1605 /**
1606 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1607 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001608 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001609 */
1610 public int getNextSubIdForState(State state) {
1611 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001612 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001613 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1614 for (int i = 0; i < list.size(); i++) {
1615 final SubscriptionInfo info = list.get(i);
1616 final int id = info.getSubscriptionId();
1617 int slotId = SubscriptionManager.getSlotId(id);
1618 if (state == getSimState(id) && bestSlotId > slotId ) {
1619 resultId = id;
1620 bestSlotId = slotId;
1621 }
1622 }
1623 return resultId;
1624 }
1625
1626 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1627 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1628 for (int i = 0; i < list.size(); i++) {
1629 SubscriptionInfo info = list.get(i);
1630 if (subId == info.getSubscriptionId()) return info;
1631 }
1632 return null; // not found
1633 }
Jason Monkab525272015-07-13 17:02:49 -04001634
1635 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1636 pw.println("KeyguardUpdateMonitor state:");
1637 pw.println(" SIM States:");
1638 for (SimData data : mSimDatas.values()) {
1639 pw.println(" " + data.toString());
1640 }
1641 pw.println(" Subs:");
1642 if (mSubscriptionInfo != null) {
1643 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
1644 pw.println(" " + mSubscriptionInfo.get(i));
1645 }
1646 }
1647 pw.println(" Service states:");
1648 for (int subId : mServiceStates.keySet()) {
1649 pw.println(" " + subId + "=" + mServiceStates.get(subId));
1650 }
1651 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001652}