blob: 0e2181532630539eaab6849fcaa053769145b25a [file] [log] [blame]
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jim Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080018
Wale Ogunwale68278562017-09-23 17:13:55 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
20import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Jorim Jaggidadafd42016-09-30 07:20:25 -070021import static android.content.Intent.ACTION_USER_UNLOCKED;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070022import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
23import static android.os.BatteryManager.BATTERY_STATUS_FULL;
24import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
25import static android.os.BatteryManager.EXTRA_HEALTH;
26import static android.os.BatteryManager.EXTRA_LEVEL;
27import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
28import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE;
29import static android.os.BatteryManager.EXTRA_PLUGGED;
30import static android.os.BatteryManager.EXTRA_STATUS;
31
Adrian Roos30a2ae62018-04-25 19:09:50 +020032import android.annotation.AnyThread;
33import android.annotation.MainThread;
Jorim Jaggiccdfa932015-04-13 16:29:48 -070034import android.app.ActivityManager;
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020035import android.app.AlarmManager;
Adrian Roos30a2ae62018-04-25 19:09:50 +020036import android.app.Instrumentation;
Jim Miller8f09fd22013-03-14 19:04:28 -070037import android.app.PendingIntent;
Sudheer Shanka2c4522c2016-08-27 20:53:28 -070038import android.app.UserSwitchObserver;
Jim Millerb0304762012-03-13 20:01:25 -070039import android.app.admin.DevicePolicyManager;
Adrian Roos46842d92014-03-27 14:58:03 +010040import android.app.trust.TrustManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080041import android.content.BroadcastReceiver;
Jorim Jaggi031f7952016-09-01 16:39:26 -070042import android.content.ComponentName;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080043import android.content.Context;
44import android.content.Intent;
45import android.content.IntentFilter;
Adrian Roosca8a2162017-08-17 19:00:58 +020046import android.content.pm.IPackageManager;
Jorim Jaggi031f7952016-09-01 16:39:26 -070047import android.content.pm.PackageManager;
48import android.content.pm.ResolveInfo;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080049import android.database.ContentObserver;
Jorim Jaggi86bed402015-08-20 18:20:02 -070050import android.hardware.fingerprint.FingerprintManager;
51import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
52import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
Jim Miller47088bb2009-11-24 00:40:16 -080053import android.media.AudioManager;
Jim Miller79a444a2011-02-15 15:02:11 -080054import android.os.BatteryManager;
Jim Miller9f0753f2015-03-23 23:59:22 -070055import android.os.CancellationSignal;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080056import android.os.Handler;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070057import android.os.IRemoteCallback;
Jason Monk7bb59302018-05-10 19:38:18 -070058import android.os.Looper;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080059import android.os.Message;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070060import android.os.RemoteException;
Adrian Roosca8a2162017-08-17 19:00:58 +020061import android.os.ServiceManager;
Nick Desaulniers1d396752016-07-25 15:05:33 -070062import android.os.Trace;
Amith Yamasanie8e93a12013-05-09 18:12:30 -070063import android.os.UserHandle;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070064import android.os.UserManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080065import android.provider.Settings;
Kevin Chyn36778ff2017-09-07 19:55:38 -070066import android.service.dreams.DreamService;
67import android.service.dreams.IDreamManager;
Etan Cohen47051d82015-07-06 16:19:04 -070068import android.telephony.ServiceState;
Jim Miller52a61332014-11-12 19:29:51 -080069import android.telephony.SubscriptionInfo;
Jim Miller52a61332014-11-12 19:29:51 -080070import android.telephony.SubscriptionManager;
Wink Savilled09c4ca2014-11-22 10:08:16 -080071import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Jim Millerc23024d2010-02-24 15:37:00 -080072import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080073import android.util.Log;
Richard Choue0381b82018-04-24 03:48:59 +000074import android.util.Slog;
Adrian Roos46842d92014-03-27 14:58:03 +010075import android.util.SparseBooleanArray;
Jorim Jaggi9f743032015-05-04 15:22:40 -070076import android.util.SparseIntArray;
Adrian Roos46842d92014-03-27 14:58:03 +010077
Lucas Dupin7517b5d2017-08-22 12:51:25 -070078import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi86bed402015-08-20 18:20:02 -070079import com.android.internal.telephony.IccCardConstants;
80import com.android.internal.telephony.IccCardConstants.State;
81import com.android.internal.telephony.PhoneConstants;
82import com.android.internal.telephony.TelephonyIntents;
Adrian Roos30a2ae62018-04-25 19:09:50 +020083import com.android.internal.util.Preconditions;
Adrian Roosb5e47222015-08-14 15:53:06 -070084import com.android.internal.widget.LockPatternUtils;
Winson Chungaa357452017-10-31 11:35:30 -070085import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
Winson Chung2cf6ad82017-11-09 17:36:59 -080086import com.android.systemui.shared.system.ActivityManagerWrapper;
Jorim Jaggi86bed402015-08-20 18:20:02 -070087
Kevin Chyn36778ff2017-09-07 19:55:38 -070088import com.google.android.collect.Lists;
89
Jason Monkab525272015-07-13 17:02:49 -040090import java.io.FileDescriptor;
91import java.io.PrintWriter;
Jim Millerdcb3d842012-08-23 19:18:12 -070092import java.lang.ref.WeakReference;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080093import java.util.ArrayList;
Jim Miller52a61332014-11-12 19:29:51 -080094import java.util.HashMap;
95import java.util.List;
96import java.util.Map.Entry;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080097
98/**
99 * Watches for updates that may be interesting to the keyguard, and provides
100 * the up to date information as well as a registration for callbacks that care
101 * to be updated.
102 *
103 * Note: under time crunch, this has been extended to include some stuff that
104 * doesn't really belong here. see {@link #handleBatteryUpdate} where it shutdowns
Jim Miller258341c2012-08-30 16:50:10 -0700105 * the device, and {@link #getFailedUnlockAttempts()}, {@link #reportFailedAttempt()}
106 * and {@link #clearFailedUnlockAttempts()}. Maybe we should rename this 'KeyguardContext'...
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800107 */
Adrian Roos46842d92014-03-27 14:58:03 +0100108public class KeyguardUpdateMonitor implements TrustManager.TrustListener {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800109
Jim Millerbbf1a742012-07-17 18:30:30 -0700110 private static final String TAG = "KeyguardUpdateMonitor";
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100111 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller52a61332014-11-12 19:29:51 -0800112 private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
Jim Millerbbf1a742012-07-17 18:30:30 -0700113 private static final int LOW_BATTERY_THRESHOLD = 20;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800114
Jorim Jaggie7b12522014-08-06 16:41:21 +0200115 private static final String ACTION_FACE_UNLOCK_STARTED
116 = "com.android.facelock.FACE_UNLOCK_STARTED";
117 private static final String ACTION_FACE_UNLOCK_STOPPED
118 = "com.android.facelock.FACE_UNLOCK_STOPPED";
119
Jim Millerbbf1a742012-07-17 18:30:30 -0700120 // Callback messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800121 private static final int MSG_TIME_UPDATE = 301;
122 private static final int MSG_BATTERY_UPDATE = 302;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800123 private static final int MSG_SIM_STATE_CHANGE = 304;
Jim Miller47088bb2009-11-24 00:40:16 -0800124 private static final int MSG_RINGER_MODE_CHANGED = 305;
Jim Millerc23024d2010-02-24 15:37:00 -0800125 private static final int MSG_PHONE_STATE_CHANGED = 306;
Nick Pelly24d7b5f2011-10-11 12:51:09 -0700126 private static final int MSG_DEVICE_PROVISIONED = 308;
Jim Miller57375342012-09-09 15:20:31 -0700127 private static final int MSG_DPM_STATE_CHANGED = 309;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500128 private static final int MSG_USER_SWITCHING = 310;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700129 private static final int MSG_KEYGUARD_RESET = 312;
Jim Millerf41fc962014-06-18 16:33:51 -0700130 private static final int MSG_BOOT_COMPLETED = 313;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500131 private static final int MSG_USER_SWITCH_COMPLETE = 314;
Jim Millerf41fc962014-06-18 16:33:51 -0700132 private static final int MSG_USER_INFO_CHANGED = 317;
133 private static final int MSG_REPORT_EMERGENCY_CALL_ACTION = 318;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700134 private static final int MSG_STARTED_WAKING_UP = 319;
135 private static final int MSG_FINISHED_GOING_TO_SLEEP = 320;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700136 private static final int MSG_STARTED_GOING_TO_SLEEP = 321;
Adrian Roosb6011622014-05-14 15:52:53 +0200137 private static final int MSG_KEYGUARD_BOUNCER_CHANGED = 322;
Jim Millerce7eb6d2015-04-03 19:29:13 -0700138 private static final int MSG_FACE_UNLOCK_STATE_CHANGED = 327;
139 private static final int MSG_SIM_SUBSCRIPTION_INFO_CHANGED = 328;
Jason Monk052082c2015-06-11 11:35:23 -0400140 private static final int MSG_AIRPLANE_MODE_CHANGED = 329;
Etan Cohen47051d82015-07-06 16:19:04 -0700141 private static final int MSG_SERVICE_STATE_CHANGE = 330;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700142 private static final int MSG_SCREEN_TURNED_ON = 331;
143 private static final int MSG_SCREEN_TURNED_OFF = 332;
Selim Cinek99415392016-09-09 14:58:41 -0700144 private static final int MSG_DREAMING_STATE_CHANGED = 333;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700145 private static final int MSG_USER_UNLOCKED = 334;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700146 private static final int MSG_ASSISTANT_STACK_CHANGED = 335;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700147 private static final int MSG_FINGERPRINT_AUTHENTICATION_CONTINUE = 336;
Alex Chauff7653d2018-02-01 17:18:08 +0000148 private static final int MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED = 337;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800149
Jorim Jaggi86bed402015-08-20 18:20:02 -0700150 /** Fingerprint state: Not listening to fingerprint. */
151 private static final int FINGERPRINT_STATE_STOPPED = 0;
152
153 /** Fingerprint state: Listening. */
154 private static final int FINGERPRINT_STATE_RUNNING = 1;
155
156 /**
157 * Fingerprint state: Cancelling and waiting for the confirmation from FingerprintService to
158 * send us the confirmation that cancellation has happened.
159 */
160 private static final int FINGERPRINT_STATE_CANCELLING = 2;
161
162 /**
163 * Fingerprint state: During cancelling we got another request to start listening, so when we
164 * receive the cancellation done signal, we should start listening again.
165 */
166 private static final int FINGERPRINT_STATE_CANCELLING_RESTARTING = 3;
167
Adrian Roos0c859ae2015-11-23 16:47:50 -0800168 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
169
Jorim Jaggi031f7952016-09-01 16:39:26 -0700170 private static final ComponentName FALLBACK_HOME_COMPONENT = new ComponentName(
171 "com.android.settings", "com.android.settings.FallbackHome");
172
Adrian Roosca8a2162017-08-17 19:00:58 +0200173
174 /**
175 * If true, the system is in the half-boot-to-decryption-screen state.
176 * Prudently disable lockscreen.
177 */
178 public static final boolean CORE_APPS_ONLY;
179 static {
180 try {
181 CORE_APPS_ONLY = IPackageManager.Stub.asInterface(
182 ServiceManager.getService("package")).isOnlyCoreApps();
183 } catch (RemoteException e) {
184 throw e.rethrowFromSystemServer();
185 }
186 }
187
Jim Millerdcb3d842012-08-23 19:18:12 -0700188 private static KeyguardUpdateMonitor sInstance;
189
Jim Millerbbf1a742012-07-17 18:30:30 -0700190 private final Context mContext;
Jim Miller52a61332014-11-12 19:29:51 -0800191 HashMap<Integer, SimData> mSimDatas = new HashMap<Integer, SimData>();
Etan Cohen47051d82015-07-06 16:19:04 -0700192 HashMap<Integer, ServiceState> mServiceStates = new HashMap<Integer, ServiceState>();
Jim Millerbbf1a742012-07-17 18:30:30 -0700193
Jim Millerbbf1a742012-07-17 18:30:30 -0700194 private int mRingMode;
195 private int mPhoneState;
Danielle Millett5d2404d2012-11-01 00:05:27 -0400196 private boolean mKeyguardIsVisible;
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700197 private boolean mKeyguardGoingAway;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700198 private boolean mGoingToSleep;
Adrian Roosb6011622014-05-14 15:52:53 +0200199 private boolean mBouncer;
Adam Cohen4eb36cf2012-11-07 11:45:30 -0800200 private boolean mBootCompleted;
Jorim Jaggi031f7952016-09-01 16:39:26 -0700201 private boolean mNeedsSlowUnlockTransition;
Jorim Jaggid11d1a92016-08-16 16:02:32 -0700202 private boolean mHasLockscreenWallpaper;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700203 private boolean mAssistantVisible;
204 private boolean mKeyguardOccluded;
Jim Millerbbf1a742012-07-17 18:30:30 -0700205
Jim Millerdcb3d842012-08-23 19:18:12 -0700206 // Device provisioning state
Jim Millerbbf1a742012-07-17 18:30:30 -0700207 private boolean mDeviceProvisioned;
208
Jim Millerdcb3d842012-08-23 19:18:12 -0700209 // Battery status
Jim Millerbbf1a742012-07-17 18:30:30 -0700210 private BatteryStatus mBatteryStatus;
211
Rakesh Iyera7aa4d62016-01-19 17:27:23 -0800212 private final StrongAuthTracker mStrongAuthTracker;
Jim Millerbbf1a742012-07-17 18:30:30 -0700213
Jim Miller6212cc02012-09-05 17:35:31 -0700214 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>>
Jim Millerdcb3d842012-08-23 19:18:12 -0700215 mCallbacks = Lists.newArrayList();
Michael Jurkafff56142012-11-28 16:51:00 -0800216 private ContentObserver mDeviceProvisionedObserver;
Jim Millerbbf1a742012-07-17 18:30:30 -0700217
Brian Colonnaa5239892013-04-15 11:45:40 -0400218 private boolean mSwitchingUser;
219
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700220 private boolean mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -0700221 private boolean mScreenOn;
Wink Savilled09c4ca2014-11-22 10:08:16 -0800222 private SubscriptionManager mSubscriptionManager;
223 private List<SubscriptionInfo> mSubscriptionInfo;
Jorim Jaggi237b0612015-05-01 14:28:49 -0700224 private TrustManager mTrustManager;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700225 private UserManager mUserManager;
Jorim Jaggi86bed402015-08-20 18:20:02 -0700226 private int mFingerprintRunningState = FINGERPRINT_STATE_STOPPED;
Michal Karpinskic52f8672016-11-18 11:32:45 +0000227 private LockPatternUtils mLockPatternUtils;
Kevin Chyn36778ff2017-09-07 19:55:38 -0700228 private final IDreamManager mDreamManager;
229 private boolean mIsDreaming;
Alex Chauff7653d2018-02-01 17:18:08 +0000230 private final DevicePolicyManager mDevicePolicyManager;
231 private boolean mLogoutEnabled;
Jim Miller20daffd2013-10-07 14:59:53 -0700232
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700233 /**
234 * Short delay before restarting fingerprint authentication after a successful try
235 * This should be slightly longer than the time between onFingerprintAuthenticated and
236 * setKeyguardGoingAway(true).
237 */
238 private static final int FINGERPRINT_CONTINUE_DELAY_MS = 500;
239
Kevin Chyn0c45b072017-04-24 16:27:11 -0700240 // If FP daemon dies, keyguard should retry after a short delay
241 private int mHardwareUnavailableRetryCount = 0;
242 private static final int HW_UNAVAILABLE_TIMEOUT = 3000; // ms
243 private static final int HW_UNAVAILABLE_RETRY_MAX = 3;
244
Jason Monk7bb59302018-05-10 19:38:18 -0700245 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700246 @Override
247 public void handleMessage(Message msg) {
248 switch (msg.what) {
249 case MSG_TIME_UPDATE:
250 handleTimeUpdate();
251 break;
252 case MSG_BATTERY_UPDATE:
253 handleBatteryUpdate((BatteryStatus) msg.obj);
254 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700255 case MSG_SIM_STATE_CHANGE:
Jim Miller52a61332014-11-12 19:29:51 -0800256 handleSimStateChange(msg.arg1, msg.arg2, (State) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700257 break;
258 case MSG_RINGER_MODE_CHANGED:
259 handleRingerModeChange(msg.arg1);
260 break;
261 case MSG_PHONE_STATE_CHANGED:
Adrian Roosb6011622014-05-14 15:52:53 +0200262 handlePhoneStateChanged((String) msg.obj);
Jim Millerbbf1a742012-07-17 18:30:30 -0700263 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700264 case MSG_DEVICE_PROVISIONED:
265 handleDeviceProvisioned();
266 break;
267 case MSG_DPM_STATE_CHANGED:
268 handleDevicePolicyManagerStateChanged();
269 break;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500270 case MSG_USER_SWITCHING:
Adrian Roosb6011622014-05-14 15:52:53 +0200271 handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500272 break;
273 case MSG_USER_SWITCH_COMPLETE:
274 handleUserSwitchComplete(msg.arg1);
Jim Millerbbf1a742012-07-17 18:30:30 -0700275 break;
Selim Cinek1fcafc42015-07-20 14:39:25 -0700276 case MSG_KEYGUARD_RESET:
277 handleKeyguardReset();
278 break;
Adrian Roosb6011622014-05-14 15:52:53 +0200279 case MSG_KEYGUARD_BOUNCER_CHANGED:
280 handleKeyguardBouncerChanged(msg.arg1);
281 break;
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800282 case MSG_BOOT_COMPLETED:
283 handleBootCompleted();
284 break;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700285 case MSG_USER_INFO_CHANGED:
286 handleUserInfoChanged(msg.arg1);
287 break;
Brian Colonna7fce3802013-09-17 15:51:32 -0400288 case MSG_REPORT_EMERGENCY_CALL_ACTION:
289 handleReportEmergencyCallAction();
290 break;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700291 case MSG_STARTED_GOING_TO_SLEEP:
292 handleStartedGoingToSleep(msg.arg1);
293 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700294 case MSG_FINISHED_GOING_TO_SLEEP:
295 handleFinishedGoingToSleep(msg.arg1);
Jim Miller20daffd2013-10-07 14:59:53 -0700296 break;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700297 case MSG_STARTED_WAKING_UP:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700298 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_STARTED_WAKING_UP");
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700299 handleStartedWakingUp();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700300 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -0700301 break;
Jorim Jaggie7b12522014-08-06 16:41:21 +0200302 case MSG_FACE_UNLOCK_STATE_CHANGED:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700303 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_FACE_UNLOCK_STATE_CHANGED");
Adrian Roos4a410172014-08-20 17:41:44 +0200304 handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700305 Trace.endSection();
Jorim Jaggie7b12522014-08-06 16:41:21 +0200306 break;
Jim Miller52a61332014-11-12 19:29:51 -0800307 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
308 handleSimSubscriptionInfoChanged();
309 break;
Jason Monk052082c2015-06-11 11:35:23 -0400310 case MSG_AIRPLANE_MODE_CHANGED:
311 handleAirplaneModeChanged();
312 break;
Etan Cohen47051d82015-07-06 16:19:04 -0700313 case MSG_SERVICE_STATE_CHANGE:
314 handleServiceStateChange(msg.arg1, (ServiceState) msg.obj);
315 break;
Jorim Jaggif1518da2015-07-30 11:56:36 -0700316 case MSG_SCREEN_TURNED_ON:
317 handleScreenTurnedOn();
318 break;
319 case MSG_SCREEN_TURNED_OFF:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700320 Trace.beginSection("KeyguardUpdateMonitor#handler MSG_SCREEN_TURNED_ON");
Jorim Jaggif1518da2015-07-30 11:56:36 -0700321 handleScreenTurnedOff();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700322 Trace.endSection();
Jorim Jaggif1518da2015-07-30 11:56:36 -0700323 break;
Selim Cinek99415392016-09-09 14:58:41 -0700324 case MSG_DREAMING_STATE_CHANGED:
325 handleDreamingStateChanged(msg.arg1);
326 break;
Jorim Jaggidadafd42016-09-30 07:20:25 -0700327 case MSG_USER_UNLOCKED:
328 handleUserUnlocked();
329 break;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700330 case MSG_ASSISTANT_STACK_CHANGED:
331 mAssistantVisible = (boolean)msg.obj;
332 updateFingerprintListeningState();
333 break;
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700334 case MSG_FINGERPRINT_AUTHENTICATION_CONTINUE:
335 updateFingerprintListeningState();
336 break;
Alex Chauff7653d2018-02-01 17:18:08 +0000337 case MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED:
338 updateLogoutEnabled();
339 break;
Jason Monk7bb59302018-05-10 19:38:18 -0700340 default:
341 super.handleMessage(msg);
342 break;
Jim Millerbbf1a742012-07-17 18:30:30 -0700343 }
344 }
345 };
346
Wink Savilled09c4ca2014-11-22 10:08:16 -0800347 private OnSubscriptionsChangedListener mSubscriptionListener =
348 new OnSubscriptionsChangedListener() {
Jim Miller52a61332014-11-12 19:29:51 -0800349 @Override
Wink Savilled09c4ca2014-11-22 10:08:16 -0800350 public void onSubscriptionsChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800351 mHandler.sendEmptyMessage(MSG_SIM_SUBSCRIPTION_INFO_CHANGED);
352 }
353 };
354
Adrian Roos46842d92014-03-27 14:58:03 +0100355 private SparseBooleanArray mUserHasTrust = new SparseBooleanArray();
Adrian Roos7861c662014-07-25 15:37:28 +0200356 private SparseBooleanArray mUserTrustIsManaged = new SparseBooleanArray();
Jim Miller9f0753f2015-03-23 23:59:22 -0700357 private SparseBooleanArray mUserFingerprintAuthenticated = new SparseBooleanArray();
Adrian Roos4a410172014-08-20 17:41:44 +0200358 private SparseBooleanArray mUserFaceUnlockRunning = new SparseBooleanArray();
Adrian Roos46842d92014-03-27 14:58:03 +0100359
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700360 private static int sCurrentUser;
Selim Cinekbbe19242017-12-08 15:42:08 -0800361 private Runnable mUpdateFingerprintListeningState = this::updateFingerprintListeningState;
Adrian Roos30a2ae62018-04-25 19:09:50 +0200362 private static boolean sDisableHandlerCheckForTesting;
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700363
364 public synchronized static void setCurrentUser(int currentUser) {
365 sCurrentUser = currentUser;
366 }
367
368 public synchronized static int getCurrentUser() {
369 return sCurrentUser;
370 }
371
Adrian Roos46842d92014-03-27 14:58:03 +0100372 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700373 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200374 checkIsHandlerThread();
Adrian Roos46842d92014-03-27 14:58:03 +0100375 mUserHasTrust.put(userId, enabled);
Adrian Roos2fe592d2014-05-17 03:11:59 +0200376 for (int i = 0; i < mCallbacks.size(); i++) {
377 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
378 if (cb != null) {
379 cb.onTrustChanged(userId);
Adrian Roos94e15a52015-04-16 12:23:18 -0700380 if (enabled && flags != 0) {
381 cb.onTrustGrantedWithFlags(flags, userId);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200382 }
Adrian Roos2fe592d2014-05-17 03:11:59 +0200383 }
384 }
Adrian Roos46842d92014-03-27 14:58:03 +0100385 }
386
Lucas Dupinef886542018-01-03 16:03:07 -0800387 @Override
388 public void onTrustError(CharSequence message) {
389 dispatchErrorMessage(message);
390 }
391
Adrian Roos30a2ae62018-04-25 19:09:50 +0200392 private void handleSimSubscriptionInfoChanged() {
Jim Miller52a61332014-11-12 19:29:51 -0800393 if (DEBUG_SIM_STATES) {
394 Log.v(TAG, "onSubscriptionInfoChanged()");
Wink Savilled09c4ca2014-11-22 10:08:16 -0800395 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
396 if (sil != null) {
397 for (SubscriptionInfo subInfo : sil) {
398 Log.v(TAG, "SubInfo:" + subInfo);
399 }
400 } else {
401 Log.v(TAG, "onSubscriptionInfoChanged: list is null");
Jim Miller52a61332014-11-12 19:29:51 -0800402 }
403 }
404 List<SubscriptionInfo> subscriptionInfos = getSubscriptionInfo(true /* forceReload */);
405
406 // Hack level over 9000: Because the subscription id is not yet valid when we see the
407 // first update in handleSimStateChange, we need to force refresh all all SIM states
408 // so the subscription id for them is consistent.
Richard Choue0381b82018-04-24 03:48:59 +0000409 ArrayList<SubscriptionInfo> changedSubscriptions = new ArrayList<>();
410 for (int i = 0; i < subscriptionInfos.size(); i++) {
411 SubscriptionInfo info = subscriptionInfos.get(i);
412 boolean changed = refreshSimState(info.getSubscriptionId(), info.getSimSlotIndex());
413 if (changed) {
414 changedSubscriptions.add(info);
415 }
416 }
417 for (int i = 0; i < changedSubscriptions.size(); i++) {
418 SimData data = mSimDatas.get(changedSubscriptions.get(i).getSubscriptionId());
Jim Miller52a61332014-11-12 19:29:51 -0800419 for (int j = 0; j < mCallbacks.size(); j++) {
420 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
421 if (cb != null) {
422 cb.onSimStateChanged(data.subId, data.slotId, data.simState);
423 }
424 }
425 }
Jason Monk6c985dc2015-01-09 16:07:14 -0500426 for (int j = 0; j < mCallbacks.size(); j++) {
427 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
428 if (cb != null) {
429 cb.onRefreshCarrierInfo();
430 }
431 }
Jim Miller52a61332014-11-12 19:29:51 -0800432 }
433
Jason Monk052082c2015-06-11 11:35:23 -0400434 private void handleAirplaneModeChanged() {
435 for (int j = 0; j < mCallbacks.size(); j++) {
436 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
437 if (cb != null) {
438 cb.onRefreshCarrierInfo();
439 }
440 }
441 }
442
Wink Savilled09c4ca2014-11-22 10:08:16 -0800443 /** @return List of SubscriptionInfo records, maybe empty but never null */
Adrian Roos316bf542016-08-23 17:53:07 +0200444 public List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
Wink Savilled09c4ca2014-11-22 10:08:16 -0800445 List<SubscriptionInfo> sil = mSubscriptionInfo;
446 if (sil == null || forceReload) {
447 sil = mSubscriptionManager.getActiveSubscriptionInfoList();
448 }
449 if (sil == null) {
450 // getActiveSubscriptionInfoList was null callers expect an empty list.
451 mSubscriptionInfo = new ArrayList<SubscriptionInfo>();
452 } else {
453 mSubscriptionInfo = sil;
Jim Miller52a61332014-11-12 19:29:51 -0800454 }
455 return mSubscriptionInfo;
456 }
457
Adrian Roos7861c662014-07-25 15:37:28 +0200458 @Override
459 public void onTrustManagedChanged(boolean managed, int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200460 checkIsHandlerThread();
Adrian Roos7861c662014-07-25 15:37:28 +0200461 mUserTrustIsManaged.put(userId, managed);
462
463 for (int i = 0; i < mCallbacks.size(); i++) {
464 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
465 if (cb != null) {
466 cb.onTrustManagedChanged(userId);
467 }
468 }
469 }
470
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700471 /**
472 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is goingAway
473 * @param goingAway
474 */
475 public void setKeyguardGoingAway(boolean goingAway) {
476 mKeyguardGoingAway = goingAway;
Kevin Chyne22f1342017-09-26 10:03:38 -0700477 updateFingerprintListeningState();
Kevin Chynf3b8fbd2017-05-03 22:24:31 -0700478 }
479
Kevin Chyn2fefd462017-04-28 12:18:19 -0700480 /**
481 * Updates KeyguardUpdateMonitor's internal state to know if keyguard is occluded
482 * @param occluded
483 */
484 public void setKeyguardOccluded(boolean occluded) {
485 mKeyguardOccluded = occluded;
486 updateFingerprintListeningState();
487 }
488
Kevin Chyn36778ff2017-09-07 19:55:38 -0700489 /**
490 * @return a cached version of DreamManager.isDreaming()
491 */
492 public boolean isDreaming() {
493 return mIsDreaming;
494 }
495
496 /**
497 * If the device is dreaming, awakens the device
498 */
499 public void awakenFromDream() {
500 if (mIsDreaming && mDreamManager != null) {
501 try {
502 mDreamManager.awaken();
503 } catch (RemoteException e) {
504 Log.e(TAG, "Unable to awaken from dream");
505 }
506 }
507 }
508
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700509 private void onFingerprintAuthenticated(int userId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700510 Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
Jim Miller9f0753f2015-03-23 23:59:22 -0700511 mUserFingerprintAuthenticated.put(userId, true);
Kevin Chyn3fdbbf82017-05-06 15:11:53 -0700512 // Update/refresh trust state only if user can skip bouncer
513 if (getUserCanSkipBouncer(userId)) {
514 mTrustManager.unlockedByFingerprintForUser(userId);
515 }
Kevin Chyn625a0142017-04-10 14:53:59 -0700516 // Don't send cancel if authentication succeeds
517 mFingerprintCancelSignal = null;
Jim Millerf41fc962014-06-18 16:33:51 -0700518 for (int i = 0; i < mCallbacks.size(); i++) {
519 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
520 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700521 cb.onFingerprintAuthenticated(userId);
Jim Millerf41fc962014-06-18 16:33:51 -0700522 }
523 }
Kevin Chyn2fefd462017-04-28 12:18:19 -0700524
Kevin Chyn0f3e0b12017-07-20 16:56:11 -0700525 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE),
526 FINGERPRINT_CONTINUE_DELAY_MS);
527
Kevin Chyn2fefd462017-04-28 12:18:19 -0700528 // Only authenticate fingerprint once when assistant is visible
529 mAssistantVisible = false;
Kevin Chyn2fefd462017-04-28 12:18:19 -0700530
Nick Desaulniers1d396752016-07-25 15:05:33 -0700531 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700532 }
533
Jim Millerce7eb6d2015-04-03 19:29:13 -0700534 private void handleFingerprintAuthFailed() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700535 for (int i = 0; i < mCallbacks.size(); i++) {
536 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
537 if (cb != null) {
538 cb.onFingerprintAuthFailed();
539 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700540 }
Jim Millerce7eb6d2015-04-03 19:29:13 -0700541 handleFingerprintHelp(-1, mContext.getString(R.string.fingerprint_not_recognized));
542 }
Jim Millerf41fc962014-06-18 16:33:51 -0700543
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700544 private void handleFingerprintAcquired(int acquireInfo) {
545 if (acquireInfo != FingerprintManager.FINGERPRINT_ACQUIRED_GOOD) {
546 return;
547 }
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700548 for (int i = 0; i < mCallbacks.size(); i++) {
549 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
550 if (cb != null) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700551 cb.onFingerprintAcquired();
Jorim Jaggi007f0e82015-08-14 13:56:01 -0700552 }
553 }
554 }
555
Jim Miller837fa7e2016-08-08 20:16:22 -0700556 private void handleFingerprintAuthenticated(int authUserId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700557 Trace.beginSection("KeyGuardUpdateMonitor#handlerFingerPrintAuthenticated");
Jim Millerf41fc962014-06-18 16:33:51 -0700558 try {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700559 final int userId;
560 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800561 userId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700562 } catch (RemoteException e) {
563 Log.e(TAG, "Failed to get current user id: ", e);
564 return;
Jim Millerf41fc962014-06-18 16:33:51 -0700565 }
Jim Miller837fa7e2016-08-08 20:16:22 -0700566 if (userId != authUserId) {
567 Log.d(TAG, "Fingerprint authenticated for wrong user: " + authUserId);
568 return;
569 }
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700570 if (isFingerprintDisabled(userId)) {
571 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
572 return;
573 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700574 onFingerprintAuthenticated(userId);
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700575 } finally {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700576 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
Jim Millerf41fc962014-06-18 16:33:51 -0700577 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700578 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700579 }
580
Jim Miller9f0753f2015-03-23 23:59:22 -0700581 private void handleFingerprintHelp(int msgId, String helpString) {
Jim Millerf41fc962014-06-18 16:33:51 -0700582 for (int i = 0; i < mCallbacks.size(); i++) {
583 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
584 if (cb != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -0700585 cb.onFingerprintHelp(msgId, helpString);
586 }
587 }
588 }
589
Kevin Chyn0c45b072017-04-24 16:27:11 -0700590 private Runnable mRetryFingerprintAuthentication = new Runnable() {
591 @Override
592 public void run() {
593 Log.w(TAG, "Retrying fingerprint after HW unavailable, attempt " +
594 mHardwareUnavailableRetryCount);
595 updateFingerprintListeningState();
596 }
597 };
598
Jim Miller9f0753f2015-03-23 23:59:22 -0700599 private void handleFingerprintError(int msgId, String errString) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700600 if (msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED
601 && mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
602 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
603 startListeningForFingerprint();
604 } else {
605 setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
606 }
Kevin Chyn0c45b072017-04-24 16:27:11 -0700607
608 if (msgId == FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE) {
609 if (mHardwareUnavailableRetryCount < HW_UNAVAILABLE_RETRY_MAX) {
610 mHardwareUnavailableRetryCount++;
611 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
612 mHandler.postDelayed(mRetryFingerprintAuthentication, HW_UNAVAILABLE_TIMEOUT);
613 }
614 }
615
Kevin Chyndf9d33e2017-05-03 21:40:12 -0700616 if (msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT) {
617 mLockPatternUtils.requireStrongAuth(
618 LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT,
619 getCurrentUser());
620 }
621
Jim Miller9f0753f2015-03-23 23:59:22 -0700622 for (int i = 0; i < mCallbacks.size(); i++) {
623 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
624 if (cb != null) {
625 cb.onFingerprintError(msgId, errString);
Jim Millerf41fc962014-06-18 16:33:51 -0700626 }
627 }
628 }
629
Jorim Jaggi3a464782015-08-28 16:59:13 -0700630 private void handleFingerprintLockoutReset() {
631 updateFingerprintListeningState();
632 }
633
Jorim Jaggi86bed402015-08-20 18:20:02 -0700634 private void setFingerprintRunningState(int fingerprintRunningState) {
635 boolean wasRunning = mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
636 boolean isRunning = fingerprintRunningState == FINGERPRINT_STATE_RUNNING;
637 mFingerprintRunningState = fingerprintRunningState;
638
639 // Clients of KeyguardUpdateMonitor don't care about the internal state about the
640 // asynchronousness of the cancel cycle. So only notify them if the actualy running state
641 // has changed.
642 if (wasRunning != isRunning) {
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700643 notifyFingerprintRunningStateChanged();
644 }
645 }
646
647 private void notifyFingerprintRunningStateChanged() {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200648 checkIsHandlerThread();
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700649 for (int i = 0; i < mCallbacks.size(); i++) {
650 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
651 if (cb != null) {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700652 cb.onFingerprintRunningStateChanged(isFingerprintDetectionRunning());
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700653 }
654 }
655 }
Adrian Roos4a410172014-08-20 17:41:44 +0200656 private void handleFaceUnlockStateChanged(boolean running, int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200657 checkIsHandlerThread();
Adrian Roos4a410172014-08-20 17:41:44 +0200658 mUserFaceUnlockRunning.put(userId, running);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200659 for (int i = 0; i < mCallbacks.size(); i++) {
660 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
661 if (cb != null) {
Adrian Roos4a410172014-08-20 17:41:44 +0200662 cb.onFaceUnlockStateChanged(running, userId);
Jorim Jaggie7b12522014-08-06 16:41:21 +0200663 }
664 }
665 }
666
Adrian Roos4a410172014-08-20 17:41:44 +0200667 public boolean isFaceUnlockRunning(int userId) {
668 return mUserFaceUnlockRunning.get(userId);
669 }
670
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700671 public boolean isFingerprintDetectionRunning() {
Jorim Jaggi86bed402015-08-20 18:20:02 -0700672 return mFingerprintRunningState == FINGERPRINT_STATE_RUNNING;
Jorim Jaggi27c9b742015-04-09 10:34:49 -0700673 }
674
Jim Miller50e62182014-04-23 17:25:00 -0700675 private boolean isTrustDisabled(int userId) {
Adrian Roosa4da9f62015-02-21 01:15:21 +0100676 // Don't allow trust agent if device is secured with a SIM PIN. This is here
677 // mainly because there's no other way to prompt the user to enter their SIM PIN
678 // once they get past the keyguard screen.
679 final boolean disabledBySimPin = isSimPinSecure();
680 return disabledBySimPin;
Jim Miller50e62182014-04-23 17:25:00 -0700681 }
682
Jim Miller06e34502014-07-17 14:46:05 -0700683 private boolean isFingerprintDisabled(int userId) {
684 final DevicePolicyManager dpm =
685 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
686 return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
Adrian Roos733b6632015-08-21 14:32:35 -0700687 & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
688 || isSimPinSecure();
Jim Miller06e34502014-07-17 14:46:05 -0700689 }
690
Selim Cineke8bae622015-07-15 13:24:06 -0700691 public boolean getUserCanSkipBouncer(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700692 return getUserHasTrust(userId) || (mUserFingerprintAuthenticated.get(userId)
693 && isUnlockingWithFingerprintAllowed());
Selim Cineke8bae622015-07-15 13:24:06 -0700694 }
695
Adrian Roos46842d92014-03-27 14:58:03 +0100696 public boolean getUserHasTrust(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700697 return !isTrustDisabled(userId) && mUserHasTrust.get(userId);
Adrian Roos46842d92014-03-27 14:58:03 +0100698 }
699
Adrian Roos7861c662014-07-25 15:37:28 +0200700 public boolean getUserTrustIsManaged(int userId) {
701 return mUserTrustIsManaged.get(userId) && !isTrustDisabled(userId);
702 }
703
Selim Cinek1fcafc42015-07-20 14:39:25 -0700704 public boolean isUnlockingWithFingerprintAllowed() {
Michal Karpinskic52f8672016-11-18 11:32:45 +0000705 return mStrongAuthTracker.isUnlockingWithFingerprintAllowed();
Adrian Roosb5e47222015-08-14 15:53:06 -0700706 }
707
Lucas Dupin16013822018-05-17 18:00:16 -0700708 public boolean isUserInLockdown(int userId) {
709 return mStrongAuthTracker.getStrongAuthForUser(userId)
710 == LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
711 }
712
Jorim Jaggi031f7952016-09-01 16:39:26 -0700713 public boolean needsSlowUnlockTransition() {
714 return mNeedsSlowUnlockTransition;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700715 }
716
Adrian Roosb5e47222015-08-14 15:53:06 -0700717 public StrongAuthTracker getStrongAuthTracker() {
718 return mStrongAuthTracker;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700719 }
720
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700721 private void notifyStrongAuthStateChanged(int userId) {
Adrian Roos30a2ae62018-04-25 19:09:50 +0200722 checkIsHandlerThread();
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700723 for (int i = 0; i < mCallbacks.size(); i++) {
724 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
725 if (cb != null) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700726 cb.onStrongAuthStateChanged(userId);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700727 }
728 }
Selim Cinek1fcafc42015-07-20 14:39:25 -0700729 }
730
Adrian Roos91ba3072017-02-14 16:50:46 +0100731 public boolean isScreenOn() {
732 return mScreenOn;
733 }
734
Lucas Dupinef886542018-01-03 16:03:07 -0800735 private void dispatchErrorMessage(CharSequence message) {
736 for (int i = 0; i < mCallbacks.size(); i++) {
737 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
738 if (cb != null) {
739 cb.onTrustAgentErrorMessage(message);
740 }
741 }
742 }
743
Jim Miller8f09fd22013-03-14 19:04:28 -0700744 static class DisplayClientState {
745 public int clientGeneration;
746 public boolean clearing;
747 public PendingIntent intent;
748 public int playbackState;
749 public long playbackEventTime;
750 }
751
752 private DisplayClientState mDisplayClientState = new DisplayClientState();
753
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800754 @VisibleForTesting
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700755 protected final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700756
Jim Millerd72d5ac2015-09-29 18:55:32 -0700757 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700758 public void onReceive(Context context, Intent intent) {
759 final String action = intent.getAction();
760 if (DEBUG) Log.d(TAG, "received broadcast " + action);
761
762 if (Intent.ACTION_TIME_TICK.equals(action)
763 || Intent.ACTION_TIME_CHANGED.equals(action)
Adrian Roos48c796c2014-09-01 14:59:23 +0200764 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700765 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
Jim Millerbbf1a742012-07-17 18:30:30 -0700766 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
767 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
768 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
769 final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
770 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
Adrian Roos0c859ae2015-11-23 16:47:50 -0800771
772 final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
773 int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
774 final int maxChargingMicroWatt;
775
776 if (maxChargingMicroVolt <= 0) {
777 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
778 }
779 if (maxChargingMicroAmp > 0) {
780 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
781 // to maintain precision equally on both factors.
782 maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
783 * (maxChargingMicroVolt / 1000);
784 } else {
785 maxChargingMicroWatt = -1;
786 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700787 final Message msg = mHandler.obtainMessage(
Adrian Roos7b043112015-07-10 13:00:33 -0700788 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
Adrian Roos0c859ae2015-11-23 16:47:50 -0800789 maxChargingMicroWatt));
Jim Millerbbf1a742012-07-17 18:30:30 -0700790 mHandler.sendMessage(msg);
791 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) {
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800792 // ACTION_SIM_STATE_CHANGED is rebroadcast after unlocking the device to
793 // keep compatibility with apps that aren't direct boot aware.
794 // SysUI should just ignore this broadcast because it was already received
795 // and processed previously.
796 if (intent.getBooleanExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, false)) {
797 return;
798 }
Jim Miller52a61332014-11-12 19:29:51 -0800799 SimData args = SimData.fromIntent(intent);
Jim Millerbbf1a742012-07-17 18:30:30 -0700800 if (DEBUG_SIM_STATES) {
Jim Miller52a61332014-11-12 19:29:51 -0800801 Log.v(TAG, "action " + action
802 + " state: " + intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)
803 + " slotId: " + args.slotId + " subid: " + args.subId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700804 }
Jim Miller52a61332014-11-12 19:29:51 -0800805 mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, args.subId, args.slotId, args.simState)
806 .sendToTarget();
Jim Millerbbf1a742012-07-17 18:30:30 -0700807 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
808 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED,
809 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0));
810 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
811 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
812 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state));
Jason Monk052082c2015-06-11 11:35:23 -0400813 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
814 mHandler.sendEmptyMessage(MSG_AIRPLANE_MODE_CHANGED);
Adam Cohenefb3ffb2012-11-06 16:55:32 -0800815 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Jim Miller90873d52013-09-26 18:11:38 -0700816 dispatchBootCompleted();
Etan Cohen47051d82015-07-06 16:19:04 -0700817 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
818 ServiceState serviceState = ServiceState.newFromBundle(intent.getExtras());
819 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
820 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
821 if (DEBUG) {
822 Log.v(TAG, "action " + action + " serviceState=" + serviceState + " subId="
823 + subId);
824 }
825 mHandler.sendMessage(
826 mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
Alex Chauff7653d2018-02-01 17:18:08 +0000827 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(
828 action)) {
829 mHandler.sendEmptyMessage(MSG_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jim Millerbbf1a742012-07-17 18:30:30 -0700830 }
831 }
832 };
Jim Miller2de5ee82012-06-14 22:22:50 -0700833
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700834 private final BroadcastReceiver mBroadcastAllReceiver = new BroadcastReceiver() {
835
Jim Millerd72d5ac2015-09-29 18:55:32 -0700836 @Override
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700837 public void onReceive(Context context, Intent intent) {
838 final String action = intent.getAction();
Adrian Roos48c796c2014-09-01 14:59:23 +0200839 if (AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED.equals(action)) {
840 mHandler.sendEmptyMessage(MSG_TIME_UPDATE);
841 } else if (Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700842 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
843 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
Adrian Roos48c796c2014-09-01 14:59:23 +0200844 } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700845 Trace.beginSection("KeyguardUpdateMonitor.mBroadcastAllReceiver#onReceive ACTION_FACE_UNLOCK_STARTED");
Adrian Roos48c796c2014-09-01 14:59:23 +0200846 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
847 getSendingUserId()));
Nick Desaulniers1d396752016-07-25 15:05:33 -0700848 Trace.endSection();
Adrian Roos48c796c2014-09-01 14:59:23 +0200849 } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
850 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
851 getSendingUserId()));
852 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
853 .equals(action)) {
854 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -0700855 } else if (ACTION_USER_UNLOCKED.equals(action)) {
856 mHandler.sendEmptyMessage(MSG_USER_UNLOCKED);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700857 }
858 }
859 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700860
Jorim Jaggi3a464782015-08-28 16:59:13 -0700861 private final FingerprintManager.LockoutResetCallback mLockoutResetCallback
862 = new FingerprintManager.LockoutResetCallback() {
863 @Override
864 public void onLockoutReset() {
865 handleFingerprintLockoutReset();
866 }
867 };
868
Jim Miller9f0753f2015-03-23 23:59:22 -0700869 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
870 = new AuthenticationCallback() {
Jim Millerf41fc962014-06-18 16:33:51 -0700871
872 @Override
Jim Millerce7eb6d2015-04-03 19:29:13 -0700873 public void onAuthenticationFailed() {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700874 handleFingerprintAuthFailed();
Jim Millerce7eb6d2015-04-03 19:29:13 -0700875 };
876
877 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700878 public void onAuthenticationSucceeded(AuthenticationResult result) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700879 Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
Jim Miller837fa7e2016-08-08 20:16:22 -0700880 handleFingerprintAuthenticated(result.getUserId());
Nick Desaulniers1d396752016-07-25 15:05:33 -0700881 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700882 }
883
884 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700885 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700886 handleFingerprintHelp(helpMsgId, helpString.toString());
Jim Miller9f0753f2015-03-23 23:59:22 -0700887 }
888
889 @Override
890 public void onAuthenticationError(int errMsgId, CharSequence errString) {
Jorim Jaggi4cfdcf52015-07-09 12:13:59 -0700891 handleFingerprintError(errMsgId, errString.toString());
892 }
893
894 @Override
895 public void onAuthenticationAcquired(int acquireInfo) {
896 handleFingerprintAcquired(acquireInfo);
Jim Millerf41fc962014-06-18 16:33:51 -0700897 }
898 };
Jim Miller9f0753f2015-03-23 23:59:22 -0700899 private CancellationSignal mFingerprintCancelSignal;
900 private FingerprintManager mFpm;
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700901
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800902 /**
Jim Miller47088bb2009-11-24 00:40:16 -0800903 * When we receive a
904 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast,
Wink Saville37c124c2009-04-02 01:37:02 -0700905 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800906 * we need a single object to pass to the handler. This class helps decode
Jim Miller47088bb2009-11-24 00:40:16 -0800907 * the intent and provide a {@link SimCard.State} result.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800908 */
Jim Miller52a61332014-11-12 19:29:51 -0800909 private static class SimData {
910 public State simState;
911 public int slotId;
912 public int subId;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800913
Jim Miller52a61332014-11-12 19:29:51 -0800914 SimData(State state, int slot, int id) {
Jim Miller90d5d462011-11-17 16:57:01 -0800915 simState = state;
Jim Miller52a61332014-11-12 19:29:51 -0800916 slotId = slot;
917 subId = id;
Jim Miller90d5d462011-11-17 16:57:01 -0800918 }
919
Jim Miller52a61332014-11-12 19:29:51 -0800920 static SimData fromIntent(Intent intent) {
921 State state;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800922 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
923 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED");
924 }
Wink Savillea639b312012-07-10 12:37:54 -0700925 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
Jim Miller52a61332014-11-12 19:29:51 -0800926 int slotId = intent.getIntExtra(PhoneConstants.SLOT_KEY, 0);
927 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Savilled09c4ca2014-11-22 10:08:16 -0800928 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savillea639b312012-07-10 12:37:54 -0700929 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
John Wangb0b24b32011-06-10 17:23:51 -0700930 final String absentReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700931 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
John Wangb0b24b32011-06-10 17:23:51 -0700932
Wink Savillea639b312012-07-10 12:37:54 -0700933 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals(
John Wangb0b24b32011-06-10 17:23:51 -0700934 absentReason)) {
Wink Savillea639b312012-07-10 12:37:54 -0700935 state = IccCardConstants.State.PERM_DISABLED;
John Wangb0b24b32011-06-10 17:23:51 -0700936 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700937 state = IccCardConstants.State.ABSENT;
John Wangb0b24b32011-06-10 17:23:51 -0700938 }
Wink Savillea639b312012-07-10 12:37:54 -0700939 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
940 state = IccCardConstants.State.READY;
941 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800942 final String lockedReason = intent
Wink Savillea639b312012-07-10 12:37:54 -0700943 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
944 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
945 state = IccCardConstants.State.PIN_REQUIRED;
946 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
947 state = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800948 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700949 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800950 }
Wink Savillea639b312012-07-10 12:37:54 -0700951 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) {
952 state = IccCardConstants.State.NETWORK_LOCKED;
Wileen Chiu6b8b22e2014-10-29 22:48:21 +0530953 } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
954 state = IccCardConstants.State.CARD_IO_ERROR;
Jim Miller109f1fd2012-09-19 20:44:16 -0700955 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra)
956 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) {
957 // This is required because telephony doesn't return to "READY" after
958 // these state transitions. See bug 7197471.
959 state = IccCardConstants.State.READY;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800960 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700961 state = IccCardConstants.State.UNKNOWN;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800962 }
Jim Miller52a61332014-11-12 19:29:51 -0800963 return new SimData(state, slotId, subId);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800964 }
965
Jim Millerd72d5ac2015-09-29 18:55:32 -0700966 @Override
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800967 public String toString() {
Jim Miller52a61332014-11-12 19:29:51 -0800968 return "SimData{state=" + simState + ",slotId=" + slotId + ",subId=" + subId + "}";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800969 }
970 }
971
Adrian Roos12c1ef52014-06-04 13:54:08 +0200972 public static class BatteryStatus {
Adrian Roos7b043112015-07-10 13:00:33 -0700973 public static final int CHARGING_UNKNOWN = -1;
974 public static final int CHARGING_SLOWLY = 0;
975 public static final int CHARGING_REGULAR = 1;
976 public static final int CHARGING_FAST = 2;
977
Jim Miller16464b82011-10-20 21:10:13 -0700978 public final int status;
979 public final int level;
980 public final int plugged;
981 public final int health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800982 public final int maxChargingWattage;
983 public BatteryStatus(int status, int level, int plugged, int health,
984 int maxChargingWattage) {
Jim Miller16464b82011-10-20 21:10:13 -0700985 this.status = status;
986 this.level = level;
987 this.plugged = plugged;
988 this.health = health;
Adrian Roos0c859ae2015-11-23 16:47:50 -0800989 this.maxChargingWattage = maxChargingWattage;
Jim Miller16464b82011-10-20 21:10:13 -0700990 }
991
Jim Millerbbf1a742012-07-17 18:30:30 -0700992 /**
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700993 * Determine whether the device is plugged in (USB, power, or wireless).
Jim Millerbbf1a742012-07-17 18:30:30 -0700994 * @return true if the device is plugged in.
995 */
Adrian Roosad3bc7f2014-10-30 18:29:38 +0100996 public boolean isPluggedIn() {
Jim Millerbbf1a742012-07-17 18:30:30 -0700997 return plugged == BatteryManager.BATTERY_PLUGGED_AC
Brian Muramatsua92a01b2012-09-05 21:54:39 -0700998 || plugged == BatteryManager.BATTERY_PLUGGED_USB
999 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
Jim Millerbbf1a742012-07-17 18:30:30 -07001000 }
1001
1002 /**
Beverly2034c832018-03-19 11:18:51 -04001003 * Determine whether the device is plugged in (USB, power).
1004 * @return true if the device is plugged in wired (as opposed to wireless)
1005 */
1006 public boolean isPluggedInWired() {
1007 return plugged == BatteryManager.BATTERY_PLUGGED_AC
1008 || plugged == BatteryManager.BATTERY_PLUGGED_USB;
1009 }
1010
1011 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001012 * Whether or not the device is charged. Note that some devices never return 100% for
1013 * battery level, so this allows either battery level or status to determine if the
1014 * battery is charged.
1015 * @return true if the device is charged
1016 */
1017 public boolean isCharged() {
1018 return status == BATTERY_STATUS_FULL || level >= 100;
1019 }
1020
1021 /**
1022 * Whether battery is low and needs to be charged.
1023 * @return true if battery is low
1024 */
1025 public boolean isBatteryLow() {
1026 return level < LOW_BATTERY_THRESHOLD;
1027 }
1028
Adrian Roos7b043112015-07-10 13:00:33 -07001029 public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
Adrian Roos0c859ae2015-11-23 16:47:50 -08001030 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
1031 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
1032 maxChargingWattage > fastThreshold ? CHARGING_FAST :
Adrian Roos7b043112015-07-10 13:00:33 -07001033 CHARGING_REGULAR;
1034 }
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001035
1036 @Override
1037 public String toString() {
1038 return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged
1039 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}";
1040 }
Jim Miller16464b82011-10-20 21:10:13 -07001041 }
1042
Adrian Roosb5e47222015-08-14 15:53:06 -07001043 public class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker {
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001044 public StrongAuthTracker(Context context) {
1045 super(context);
1046 }
Adrian Roosb5e47222015-08-14 15:53:06 -07001047
1048 public boolean isUnlockingWithFingerprintAllowed() {
1049 int userId = getCurrentUser();
1050 return isFingerprintAllowedForUser(userId);
1051 }
1052
1053 public boolean hasUserAuthenticatedSinceBoot() {
1054 int userId = getCurrentUser();
1055 return (getStrongAuthForUser(userId)
1056 & STRONG_AUTH_REQUIRED_AFTER_BOOT) == 0;
1057 }
1058
1059 @Override
1060 public void onStrongAuthRequiredChanged(int userId) {
Adrian Roos1de8bcb2015-08-19 16:52:52 -07001061 notifyStrongAuthStateChanged(userId);
Adrian Roosb5e47222015-08-14 15:53:06 -07001062 }
1063 }
1064
Jim Millerdcb3d842012-08-23 19:18:12 -07001065 public static KeyguardUpdateMonitor getInstance(Context context) {
1066 if (sInstance == null) {
1067 sInstance = new KeyguardUpdateMonitor(context);
1068 }
1069 return sInstance;
1070 }
1071
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001072 protected void handleStartedWakingUp() {
Nick Desaulniers1d396752016-07-25 15:05:33 -07001073 Trace.beginSection("KeyguardUpdateMonitor#handleStartedWakingUp");
Jorim Jaggi864e64b2015-05-20 14:13:23 -07001074 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001075 final int count = mCallbacks.size();
1076 for (int i = 0; i < count; i++) {
1077 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1078 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001079 cb.onStartedWakingUp();
Jim Miller20daffd2013-10-07 14:59:53 -07001080 }
1081 }
Nick Desaulniers1d396752016-07-25 15:05:33 -07001082 Trace.endSection();
Jim Miller20daffd2013-10-07 14:59:53 -07001083 }
1084
Jorim Jaggi95e40382015-09-16 15:53:42 -07001085 protected void handleStartedGoingToSleep(int arg1) {
Jim Millerf41fc962014-06-18 16:33:51 -07001086 clearFingerprintRecognized();
Jim Miller20daffd2013-10-07 14:59:53 -07001087 final int count = mCallbacks.size();
1088 for (int i = 0; i < count; i++) {
1089 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1090 if (cb != null) {
Jorim Jaggi95e40382015-09-16 15:53:42 -07001091 cb.onStartedGoingToSleep(arg1);
1092 }
1093 }
1094 mGoingToSleep = true;
Jorim Jaggi95e40382015-09-16 15:53:42 -07001095 updateFingerprintListeningState();
1096 }
1097
1098 protected void handleFinishedGoingToSleep(int arg1) {
1099 mGoingToSleep = false;
1100 final int count = mCallbacks.size();
1101 for (int i = 0; i < count; i++) {
1102 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1103 if (cb != null) {
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001104 cb.onFinishedGoingToSleep(arg1);
Jim Miller20daffd2013-10-07 14:59:53 -07001105 }
1106 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001107 updateFingerprintListeningState();
Jim Miller20daffd2013-10-07 14:59:53 -07001108 }
1109
Jorim Jaggif1518da2015-07-30 11:56:36 -07001110 private void handleScreenTurnedOn() {
1111 final int count = mCallbacks.size();
1112 for (int i = 0; i < count; i++) {
1113 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1114 if (cb != null) {
1115 cb.onScreenTurnedOn();
1116 }
1117 }
1118 }
1119
1120 private void handleScreenTurnedOff() {
Kevin Chyn0c45b072017-04-24 16:27:11 -07001121 mHardwareUnavailableRetryCount = 0;
Jorim Jaggif1518da2015-07-30 11:56:36 -07001122 final int count = mCallbacks.size();
1123 for (int i = 0; i < count; i++) {
1124 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1125 if (cb != null) {
1126 cb.onScreenTurnedOff();
1127 }
1128 }
1129 }
1130
Selim Cinek99415392016-09-09 14:58:41 -07001131 private void handleDreamingStateChanged(int dreamStart) {
1132 final int count = mCallbacks.size();
Kevin Chyn36778ff2017-09-07 19:55:38 -07001133 mIsDreaming = dreamStart == 1;
Selim Cinek99415392016-09-09 14:58:41 -07001134 for (int i = 0; i < count; i++) {
1135 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1136 if (cb != null) {
Kevin Chyn36778ff2017-09-07 19:55:38 -07001137 cb.onDreamingStateChanged(mIsDreaming);
Selim Cinek99415392016-09-09 14:58:41 -07001138 }
1139 }
Kevin Chyn20a68dc2017-09-21 11:41:01 -07001140 updateFingerprintListeningState();
Selim Cinek99415392016-09-09 14:58:41 -07001141 }
1142
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001143 private void handleUserInfoChanged(int userId) {
1144 for (int i = 0; i < mCallbacks.size(); i++) {
1145 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1146 if (cb != null) {
1147 cb.onUserInfoChanged(userId);
1148 }
1149 }
1150 }
1151
Jorim Jaggidadafd42016-09-30 07:20:25 -07001152 private void handleUserUnlocked() {
1153 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1154 for (int i = 0; i < mCallbacks.size(); i++) {
1155 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1156 if (cb != null) {
1157 cb.onUserUnlocked();
1158 }
1159 }
1160 }
1161
Lucas Dupin7517b5d2017-08-22 12:51:25 -07001162 @VisibleForTesting
1163 protected KeyguardUpdateMonitor(Context context) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001164 mContext = context;
Wink Savilled09c4ca2014-11-22 10:08:16 -08001165 mSubscriptionManager = SubscriptionManager.from(context);
Michael Jurkafff56142012-11-28 16:51:00 -08001166 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Rakesh Iyera7aa4d62016-01-19 17:27:23 -08001167 mStrongAuthTracker = new StrongAuthTracker(context);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001168
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001169 // Since device can't be un-provisioned, we only need to register a content observer
1170 // to update mDeviceProvisioned when we are...
1171 if (!mDeviceProvisioned) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001172 watchForDeviceProvisioning();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001173 }
Jim Miller47088bb2009-11-24 00:40:16 -08001174
Jim Millerbbf1a742012-07-17 18:30:30 -07001175 // Take a guess at initial SIM state, battery status and PLMN until we get an update
Adrian Roos7b043112015-07-10 13:00:33 -07001176 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001177
Jim Millerbbf1a742012-07-17 18:30:30 -07001178 // Watch for interesting updates
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001179 final IntentFilter filter = new IntentFilter();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001180 filter.addAction(Intent.ACTION_TIME_TICK);
1181 filter.addAction(Intent.ACTION_TIME_CHANGED);
1182 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1183 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jason Monk052082c2015-06-11 11:35:23 -04001184 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001185 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Etan Cohen47051d82015-07-06 16:19:04 -07001186 filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
Jim Millerc23024d2010-02-24 15:37:00 -08001187 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Jim Miller47088bb2009-11-24 00:40:16 -08001188 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Alex Chauff7653d2018-02-01 17:18:08 +00001189 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jason Monk7bb59302018-05-10 19:38:18 -07001190 context.registerReceiver(mBroadcastReceiver, filter, null, mHandler);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001191
Adam Cohenc276e822012-11-08 13:01:08 -08001192 final IntentFilter bootCompleteFilter = new IntentFilter();
1193 bootCompleteFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1194 bootCompleteFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
Jason Monk7bb59302018-05-10 19:38:18 -07001195 context.registerReceiver(mBroadcastReceiver, bootCompleteFilter, null, mHandler);
Adam Cohenc276e822012-11-08 13:01:08 -08001196
Adrian Roos48c796c2014-09-01 14:59:23 +02001197 final IntentFilter allUserFilter = new IntentFilter();
1198 allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
1199 allUserFilter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
1200 allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
1201 allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
1202 allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
Jorim Jaggidadafd42016-09-30 07:20:25 -07001203 allUserFilter.addAction(ACTION_USER_UNLOCKED);
Adrian Roos48c796c2014-09-01 14:59:23 +02001204 context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
Jason Monk7bb59302018-05-10 19:38:18 -07001205 null, mHandler);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001206
Wink Saville071743f2015-01-12 17:11:04 -08001207 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001208 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001209 ActivityManager.getService().registerUserSwitchObserver(
Sudheer Shanka2c4522c2016-08-27 20:53:28 -07001210 new UserSwitchObserver() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001211 @Override
1212 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001213 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001214 newUserId, 0, reply));
1215 }
1216 @Override
1217 public void onUserSwitchComplete(int newUserId) throws RemoteException {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001218 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
Adrian Roosbe47b072014-09-03 00:08:56 +02001219 newUserId, 0));
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001220 }
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001221 }, TAG);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001222 } catch (RemoteException e) {
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001223 e.rethrowAsRuntimeException();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001224 }
Adrian Roos46842d92014-03-27 14:58:03 +01001225
Jorim Jaggi237b0612015-05-01 14:28:49 -07001226 mTrustManager = (TrustManager) context.getSystemService(Context.TRUST_SERVICE);
1227 mTrustManager.registerTrustListener(this);
Michal Karpinskic52f8672016-11-18 11:32:45 +00001228 mLockPatternUtils = new LockPatternUtils(context);
1229 mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker);
Jim Millerf41fc962014-06-18 16:33:51 -07001230
Kevin Chyn36778ff2017-09-07 19:55:38 -07001231 mDreamManager = IDreamManager.Stub.asInterface(
1232 ServiceManager.getService(DreamService.DREAM_SERVICE));
1233
Jorim Jaggi3f124262016-11-22 13:45:17 +01001234 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
1235 mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
1236 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001237 updateFingerprintListeningState();
Jorim Jaggi3a464782015-08-28 16:59:13 -07001238 if (mFpm != null) {
1239 mFpm.addLockoutResetCallback(mLockoutResetCallback);
1240 }
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001241
Winson Chung2cf6ad82017-11-09 17:36:59 -08001242 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jorim Jaggie8fde5d2016-06-30 23:41:37 -07001243 mUserManager = context.getSystemService(UserManager.class);
Alex Chauff7653d2018-02-01 17:18:08 +00001244 mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
1245 mLogoutEnabled = mDevicePolicyManager.isLogoutEnabled();
Jorim Jaggiea657062015-04-28 13:45:11 -07001246 }
1247
1248 private void updateFingerprintListeningState() {
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001249 // If this message exists, we should not authenticate again until this message is
1250 // consumed by the handler
1251 if (mHandler.hasMessages(MSG_FINGERPRINT_AUTHENTICATION_CONTINUE)) {
1252 return;
1253 }
Kevin Chyn0c45b072017-04-24 16:27:11 -07001254 mHandler.removeCallbacks(mRetryFingerprintAuthentication);
Jorim Jaggiea657062015-04-28 13:45:11 -07001255 boolean shouldListenForFingerprint = shouldListenForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001256 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING && !shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001257 stopListeningForFingerprint();
Jorim Jaggi86bed402015-08-20 18:20:02 -07001258 } else if (mFingerprintRunningState != FINGERPRINT_STATE_RUNNING
1259 && shouldListenForFingerprint) {
Jorim Jaggiea657062015-04-28 13:45:11 -07001260 startListeningForFingerprint();
1261 }
1262 }
1263
Kevin Chyn129f60f2017-08-11 17:24:42 -07001264 private boolean shouldListenForFingerprintAssistant() {
1265 return mAssistantVisible && mKeyguardOccluded
1266 && !mUserFingerprintAuthenticated.get(getCurrentUser(), false)
1267 && !mUserHasTrust.get(getCurrentUser(), false);
1268 }
1269
Jorim Jaggiea657062015-04-28 13:45:11 -07001270 private boolean shouldListenForFingerprint() {
Kevin Chynf3b8fbd2017-05-03 22:24:31 -07001271 return (mKeyguardIsVisible || !mDeviceInteractive ||
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001272 (mBouncer && !mKeyguardGoingAway) || mGoingToSleep ||
Kevin Chyn36778ff2017-09-07 19:55:38 -07001273 shouldListenForFingerprintAssistant() || (mKeyguardOccluded && mIsDreaming))
Kevin Chyn0f3e0b12017-07-20 16:56:11 -07001274 && !mSwitchingUser && !isFingerprintDisabled(getCurrentUser())
1275 && !mKeyguardGoingAway;
Jim Miller9f0753f2015-03-23 23:59:22 -07001276 }
1277
Jim Millerce7eb6d2015-04-03 19:29:13 -07001278 private void startListeningForFingerprint() {
Jorim Jaggi86bed402015-08-20 18:20:02 -07001279 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING) {
1280 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING_RESTARTING);
1281 return;
1282 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001283 if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
Jorim Jaggi2aad7ee2015-04-14 15:25:06 -07001284 int userId = ActivityManager.getCurrentUser();
Jorim Jaggi71448a72015-08-18 19:49:04 -07001285 if (isUnlockWithFingerprintPossible(userId)) {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001286 if (mFingerprintCancelSignal != null) {
Jim Miller9f0753f2015-03-23 23:59:22 -07001287 mFingerprintCancelSignal.cancel();
1288 }
Jim Millerce7eb6d2015-04-03 19:29:13 -07001289 mFingerprintCancelSignal = new CancellationSignal();
Kevin Chynaae4a152018-01-18 11:48:09 -08001290 mFpm.authenticate(null, mFingerprintCancelSignal, 0, mAuthenticationCallback, null,
1291 userId);
Jorim Jaggi86bed402015-08-20 18:20:02 -07001292 setFingerprintRunningState(FINGERPRINT_STATE_RUNNING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001293 }
1294 }
1295
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -07001296 public boolean isUnlockWithFingerprintPossible(int userId) {
Selim Cinek3122fa82015-06-18 01:38:59 -07001297 return mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
1298 && mFpm.getEnrolledFingerprints(userId).size() > 0;
1299 }
1300
Jorim Jaggiea657062015-04-28 13:45:11 -07001301 private void stopListeningForFingerprint() {
Jim Millerce7eb6d2015-04-03 19:29:13 -07001302 if (DEBUG) Log.v(TAG, "stopListeningForFingerprint()");
Jorim Jaggi86bed402015-08-20 18:20:02 -07001303 if (mFingerprintRunningState == FINGERPRINT_STATE_RUNNING) {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001304 if (mFingerprintCancelSignal != null) {
1305 mFingerprintCancelSignal.cancel();
1306 mFingerprintCancelSignal = null;
1307 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001308 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
Jim Miller9f0753f2015-03-23 23:59:22 -07001309 }
Jorim Jaggi86bed402015-08-20 18:20:02 -07001310 if (mFingerprintRunningState == FINGERPRINT_STATE_CANCELLING_RESTARTING) {
1311 setFingerprintRunningState(FINGERPRINT_STATE_CANCELLING);
1312 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001313 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001314
Michael Jurkafff56142012-11-28 16:51:00 -08001315 private boolean isDeviceProvisionedInSettingsDb() {
1316 return Settings.Global.getInt(mContext.getContentResolver(),
1317 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1318 }
1319
Jim Millerbbf1a742012-07-17 18:30:30 -07001320 private void watchForDeviceProvisioning() {
Michael Jurkafff56142012-11-28 16:51:00 -08001321 mDeviceProvisionedObserver = new ContentObserver(mHandler) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001322 @Override
1323 public void onChange(boolean selfChange) {
1324 super.onChange(selfChange);
Michael Jurkafff56142012-11-28 16:51:00 -08001325 mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001326 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001327 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001328 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001329 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001330 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001331 };
1332
1333 mContext.getContentResolver().registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001334 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
Michael Jurkafff56142012-11-28 16:51:00 -08001335 false, mDeviceProvisionedObserver);
Jim Millerbbf1a742012-07-17 18:30:30 -07001336
1337 // prevent a race condition between where we check the flag and where we register the
1338 // observer by grabbing the value once again...
Michael Jurkafff56142012-11-28 16:51:00 -08001339 boolean provisioned = isDeviceProvisionedInSettingsDb();
Jim Millerbbf1a742012-07-17 18:30:30 -07001340 if (provisioned != mDeviceProvisioned) {
1341 mDeviceProvisioned = provisioned;
1342 if (mDeviceProvisioned) {
Jim Miller90873d52013-09-26 18:11:38 -07001343 mHandler.sendEmptyMessage(MSG_DEVICE_PROVISIONED);
Jim Millerbbf1a742012-07-17 18:30:30 -07001344 }
1345 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001346 }
1347
Jim Millerbbf1a742012-07-17 18:30:30 -07001348 /**
Jorim Jaggid11d1a92016-08-16 16:02:32 -07001349 * Update the state whether Keyguard currently has a lockscreen wallpaper.
1350 *
1351 * @param hasLockscreenWallpaper Whether Keyguard has a lockscreen wallpaper.
1352 */
1353 public void setHasLockscreenWallpaper(boolean hasLockscreenWallpaper) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001354 checkIsHandlerThread();
Jorim Jaggid11d1a92016-08-16 16:02:32 -07001355 if (hasLockscreenWallpaper != mHasLockscreenWallpaper) {
1356 mHasLockscreenWallpaper = hasLockscreenWallpaper;
1357 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1358 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1359 if (cb != null) {
1360 cb.onHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
1361 }
1362 }
1363 }
1364 }
1365
1366 /**
1367 * @return Whether Keyguard has a lockscreen wallpaper.
1368 */
1369 public boolean hasLockscreenWallpaper() {
1370 return mHasLockscreenWallpaper;
1371 }
1372
1373 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001374 * Handle {@link #MSG_DPM_STATE_CHANGED}
1375 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001376 private void handleDevicePolicyManagerStateChanged() {
Adrian Roos733b6632015-08-21 14:32:35 -07001377 updateFingerprintListeningState();
Jim Millerdcb3d842012-08-23 19:18:12 -07001378 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1379 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1380 if (cb != null) {
1381 cb.onDevicePolicyManagerStateChanged();
1382 }
Jim Millerb0304762012-03-13 20:01:25 -07001383 }
1384 }
1385
Jim Millerbbf1a742012-07-17 18:30:30 -07001386 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001387 * Handle {@link #MSG_USER_SWITCHING}
Jim Millerbbf1a742012-07-17 18:30:30 -07001388 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001389 private void handleUserSwitching(int userId, IRemoteCallback reply) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001390 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001391 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1392 if (cb != null) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001393 cb.onUserSwitching(userId);
Jim Millerdcb3d842012-08-23 19:18:12 -07001394 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001395 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001396 try {
1397 reply.sendResult(null);
1398 } catch (RemoteException e) {
1399 }
Amith Yamasani52c489c2012-03-28 11:42:42 -07001400 }
1401
Jim Millerbbf1a742012-07-17 18:30:30 -07001402 /**
Chris Wrenf41c61b2012-11-29 15:19:54 -05001403 * Handle {@link #MSG_USER_SWITCH_COMPLETE}
1404 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001405 private void handleUserSwitchComplete(int userId) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001406 for (int i = 0; i < mCallbacks.size(); i++) {
1407 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1408 if (cb != null) {
1409 cb.onUserSwitchComplete(userId);
1410 }
1411 }
1412 }
1413
1414 /**
Jim Miller90873d52013-09-26 18:11:38 -07001415 * This is exposed since {@link Intent#ACTION_BOOT_COMPLETED} is not sticky. If
1416 * keyguard crashes sometime after boot, then it will never receive this
1417 * broadcast and hence not handle the event. This method is ultimately called by
1418 * PhoneWindowManager in this case.
1419 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001420 public void dispatchBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001421 mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
Jim Miller90873d52013-09-26 18:11:38 -07001422 }
1423
1424 /**
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001425 * Handle {@link #MSG_BOOT_COMPLETED}
1426 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001427 private void handleBootCompleted() {
Jim Millere5f910a2013-10-16 18:15:46 -07001428 if (mBootCompleted) return;
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001429 mBootCompleted = true;
Adam Cohenefb3ffb2012-11-06 16:55:32 -08001430 for (int i = 0; i < mCallbacks.size(); i++) {
1431 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1432 if (cb != null) {
1433 cb.onBootCompleted();
1434 }
1435 }
1436 }
1437
1438 /**
Jim Miller5ecd8112013-01-09 18:50:26 -08001439 * We need to store this state in the KeyguardUpdateMonitor since this class will not be
Adam Cohen4eb36cf2012-11-07 11:45:30 -08001440 * destroyed.
1441 */
1442 public boolean hasBootCompleted() {
1443 return mBootCompleted;
1444 }
1445
1446 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001447 * Handle {@link #MSG_DEVICE_PROVISIONED}
1448 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001449 private void handleDeviceProvisioned() {
Jim Millerbbf1a742012-07-17 18:30:30 -07001450 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001451 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1452 if (cb != null) {
1453 cb.onDeviceProvisioned();
1454 }
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001455 }
Michael Jurkafff56142012-11-28 16:51:00 -08001456 if (mDeviceProvisionedObserver != null) {
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001457 // We don't need the observer anymore...
Michael Jurkafff56142012-11-28 16:51:00 -08001458 mContext.getContentResolver().unregisterContentObserver(mDeviceProvisionedObserver);
1459 mDeviceProvisionedObserver = null;
Nick Pelly24d7b5f2011-10-11 12:51:09 -07001460 }
1461 }
1462
Jim Millerbbf1a742012-07-17 18:30:30 -07001463 /**
1464 * Handle {@link #MSG_PHONE_STATE_CHANGED}
1465 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001466 private void handlePhoneStateChanged(String newState) {
Jim Millerc23024d2010-02-24 15:37:00 -08001467 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001468 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) {
1469 mPhoneState = TelephonyManager.CALL_STATE_IDLE;
1470 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) {
1471 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK;
1472 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) {
1473 mPhoneState = TelephonyManager.CALL_STATE_RINGING;
1474 }
Jim Millerbbf1a742012-07-17 18:30:30 -07001475 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001476 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1477 if (cb != null) {
1478 cb.onPhoneStateChanged(mPhoneState);
1479 }
Jim Millerc23024d2010-02-24 15:37:00 -08001480 }
1481 }
1482
Jim Millerbbf1a742012-07-17 18:30:30 -07001483 /**
1484 * Handle {@link #MSG_RINGER_MODE_CHANGED}
1485 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001486 private void handleRingerModeChange(int mode) {
Jim Miller47088bb2009-11-24 00:40:16 -08001487 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")");
Jim Miller3f5f83b2011-09-26 15:17:05 -07001488 mRingMode = mode;
Jim Millerbbf1a742012-07-17 18:30:30 -07001489 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001490 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1491 if (cb != null) {
1492 cb.onRingerModeChanged(mode);
1493 }
Jim Miller47088bb2009-11-24 00:40:16 -08001494 }
1495 }
1496
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001497 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001498 * Handle {@link #MSG_TIME_UPDATE}
1499 */
1500 private void handleTimeUpdate() {
1501 if (DEBUG) Log.d(TAG, "handleTimeUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001502 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001503 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1504 if (cb != null) {
1505 cb.onTimeChanged();
1506 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001507 }
1508 }
1509
1510 /**
1511 * Handle {@link #MSG_BATTERY_UPDATE}
1512 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001513 private void handleBatteryUpdate(BatteryStatus status) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001514 if (DEBUG) Log.d(TAG, "handleBatteryUpdate");
Jim Millerbbf1a742012-07-17 18:30:30 -07001515 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);
1516 mBatteryStatus = status;
Jim Miller16464b82011-10-20 21:10:13 -07001517 if (batteryUpdateInteresting) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001518 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001519 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1520 if (cb != null) {
1521 cb.onRefreshBatteryInfo(status);
1522 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001523 }
1524 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001525 }
1526
1527 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001528 * Handle {@link #MSG_SIM_STATE_CHANGE}
1529 */
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001530 @VisibleForTesting
Adrian Roos30a2ae62018-04-25 19:09:50 +02001531 void handleSimStateChange(int subId, int slotId, State state) {
1532 checkIsHandlerThread();
Jim Miller52a61332014-11-12 19:29:51 -08001533 if (DEBUG_SIM_STATES) {
1534 Log.d(TAG, "handleSimStateChange(subId=" + subId + ", slotId="
1535 + slotId + ", state=" + state +")");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001536 }
1537
Wink Savillea54bf652014-12-11 13:37:50 -08001538 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Jim Miller52a61332014-11-12 19:29:51 -08001539 Log.w(TAG, "invalid subId in handleSimStateChange()");
1540 return;
1541 }
1542
1543 SimData data = mSimDatas.get(subId);
1544 final boolean changed;
1545 if (data == null) {
1546 data = new SimData(state, slotId, subId);
1547 mSimDatas.put(subId, data);
1548 changed = true; // no data yet; force update
1549 } else {
1550 changed = (data.simState != state || data.subId != subId || data.slotId != slotId);
1551 data.simState = state;
1552 data.subId = subId;
1553 data.slotId = slotId;
1554 }
1555 if (changed && state != State.UNKNOWN) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001556 for (int i = 0; i < mCallbacks.size(); i++) {
Jim Millerdcb3d842012-08-23 19:18:12 -07001557 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1558 if (cb != null) {
Jim Miller52a61332014-11-12 19:29:51 -08001559 cb.onSimStateChanged(subId, slotId, state);
Jim Millerdcb3d842012-08-23 19:18:12 -07001560 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001561 }
1562 }
1563 }
1564
Jim Millerbbf1a742012-07-17 18:30:30 -07001565 /**
Etan Cohen47051d82015-07-06 16:19:04 -07001566 * Handle {@link #MSG_SERVICE_STATE_CHANGE}
1567 */
1568 private void handleServiceStateChange(int subId, ServiceState serviceState) {
1569 if (DEBUG) {
1570 Log.d(TAG,
1571 "handleServiceStateChange(subId=" + subId + ", serviceState=" + serviceState);
1572 }
1573
1574 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1575 Log.w(TAG, "invalid subId in handleServiceStateChange()");
1576 return;
1577 }
1578
1579 mServiceStates.put(subId, serviceState);
1580
1581 for (int j = 0; j < mCallbacks.size(); j++) {
1582 KeyguardUpdateMonitorCallback cb = mCallbacks.get(j).get();
1583 if (cb != null) {
1584 cb.onRefreshCarrierInfo();
1585 }
1586 }
1587 }
1588
Lucas Dupinf8463ee2018-06-11 16:18:15 -07001589 public boolean isKeyguardVisible() {
1590 return mKeyguardIsVisible;
1591 }
1592
Etan Cohen47051d82015-07-06 16:19:04 -07001593 /**
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001594 * Notifies that the visibility state of Keyguard has changed.
1595 *
1596 * <p>Needs to be called from the main thread.
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001597 */
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001598 public void onKeyguardVisibilityChanged(boolean showing) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001599 checkIsHandlerThread();
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001600 if (DEBUG) Log.d(TAG, "onKeyguardVisibilityChanged(" + showing + ")");
1601 mKeyguardIsVisible = showing;
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001602 for (int i = 0; i < mCallbacks.size(); i++) {
1603 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1604 if (cb != null) {
Jorim Jaggi6a15d522015-09-22 15:55:33 -07001605 cb.onKeyguardVisibilityChangedRaw(showing);
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001606 }
1607 }
Jorim Jaggiea657062015-04-28 13:45:11 -07001608 updateFingerprintListeningState();
Danielle Millettf6d0fc12012-10-23 16:16:52 -04001609 }
1610
Brian Colonna7fce3802013-09-17 15:51:32 -04001611 /**
Selim Cinek1fcafc42015-07-20 14:39:25 -07001612 * Handle {@link #MSG_KEYGUARD_RESET}
1613 */
1614 private void handleKeyguardReset() {
1615 if (DEBUG) Log.d(TAG, "handleKeyguardReset");
Adrian Roosf6d51ac2015-09-02 13:26:25 -07001616 updateFingerprintListeningState();
Jorim Jaggi031f7952016-09-01 16:39:26 -07001617 mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
1618 }
1619
1620 private boolean resolveNeedsSlowUnlockTransition() {
1621 if (mUserManager.isUserUnlocked(getCurrentUser())) {
1622 return false;
1623 }
1624 Intent homeIntent = new Intent(Intent.ACTION_MAIN)
1625 .addCategory(Intent.CATEGORY_HOME);
1626 ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
1627 0 /* flags */);
1628 return FALLBACK_HOME_COMPONENT.equals(resolveInfo.getComponentInfo().getComponentName());
Selim Cinek1fcafc42015-07-20 14:39:25 -07001629 }
1630
1631 /**
Adrian Roosb6011622014-05-14 15:52:53 +02001632 * Handle {@link #MSG_KEYGUARD_BOUNCER_CHANGED}
1633 * @see #sendKeyguardBouncerChanged(boolean)
1634 */
1635 private void handleKeyguardBouncerChanged(int bouncer) {
1636 if (DEBUG) Log.d(TAG, "handleKeyguardBouncerChanged(" + bouncer + ")");
1637 boolean isBouncer = (bouncer == 1);
1638 mBouncer = isBouncer;
1639 for (int i = 0; i < mCallbacks.size(); i++) {
1640 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1641 if (cb != null) {
1642 cb.onKeyguardBouncerChanged(isBouncer);
1643 }
1644 }
Jorim Jaggi3cf7eef2015-09-10 14:36:19 -07001645 updateFingerprintListeningState();
Adrian Roosb6011622014-05-14 15:52:53 +02001646 }
1647
1648 /**
Brian Colonna7fce3802013-09-17 15:51:32 -04001649 * Handle {@link #MSG_REPORT_EMERGENCY_CALL_ACTION}
1650 */
1651 private void handleReportEmergencyCallAction() {
1652 for (int i = 0; i < mCallbacks.size(); i++) {
1653 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1654 if (cb != null) {
1655 cb.onEmergencyCallAction();
1656 }
1657 }
1658 }
1659
Lucas Dupin4272f442018-01-13 22:00:35 -08001660 private boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001661 final boolean nowPluggedIn = current.isPluggedIn();
1662 final boolean wasPluggedIn = old.isPluggedIn();
Lucas Dupin4272f442018-01-13 22:00:35 -08001663 final boolean stateChangedWhilePluggedIn = wasPluggedIn && nowPluggedIn
Jim Miller16464b82011-10-20 21:10:13 -07001664 && (old.status != current.status);
1665
1666 // change in plug state is always interesting
1667 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001668 return true;
1669 }
1670
Lucas Dupin3fcdd472018-01-19 19:06:45 -08001671 // change in battery level
1672 if (old.level != current.level) {
Jim Miller16464b82011-10-20 21:10:13 -07001673 return true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001674 }
Adrian Roos76dc5a52015-07-21 16:20:36 -07001675
1676 // change in charging current while plugged in
Adrian Roos0c859ae2015-11-23 16:47:50 -08001677 if (nowPluggedIn && current.maxChargingWattage != old.maxChargingWattage) {
Adrian Roos76dc5a52015-07-21 16:20:36 -07001678 return true;
1679 }
1680
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001681 return false;
1682 }
1683
1684 /**
Jim Millerbbf1a742012-07-17 18:30:30 -07001685 * Remove the given observer's callback.
1686 *
Jim Miller6212cc02012-09-05 17:35:31 -07001687 * @param callback The callback to remove
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001688 */
Jim Miller6212cc02012-09-05 17:35:31 -07001689 public void removeCallback(KeyguardUpdateMonitorCallback callback) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001690 checkIsHandlerThread();
Jim Miller6212cc02012-09-05 17:35:31 -07001691 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback);
1692 for (int i = mCallbacks.size() - 1; i >= 0; i--) {
1693 if (mCallbacks.get(i).get() == callback) {
1694 mCallbacks.remove(i);
1695 }
1696 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001697 }
1698
1699 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001700 * Register to receive notifications about general keyguard information
1701 * (see {@link InfoCallback}.
Jim Miller6212cc02012-09-05 17:35:31 -07001702 * @param callback The callback to register
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001703 */
Jim Millerbbf1a742012-07-17 18:30:30 -07001704 public void registerCallback(KeyguardUpdateMonitorCallback callback) {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001705 checkIsHandlerThread();
Jim Miller6212cc02012-09-05 17:35:31 -07001706 if (DEBUG) Log.v(TAG, "*** register callback for " + callback);
1707 // Prevent adding duplicate callbacks
1708 for (int i = 0; i < mCallbacks.size(); i++) {
1709 if (mCallbacks.get(i).get() == callback) {
1710 if (DEBUG) Log.e(TAG, "Object tried to add another callback",
1711 new Exception("Called by"));
1712 return;
Jim Millerdcb3d842012-08-23 19:18:12 -07001713 }
1714 }
Jim Miller6212cc02012-09-05 17:35:31 -07001715 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback));
1716 removeCallback(null); // remove unused references
1717 sendUpdates(callback);
1718 }
1719
Evan Rosky18396452016-07-27 15:19:37 -07001720 public boolean isSwitchingUser() {
1721 return mSwitchingUser;
1722 }
1723
Adrian Roos30a2ae62018-04-25 19:09:50 +02001724 @AnyThread
Evan Rosky18396452016-07-27 15:19:37 -07001725 public void setSwitchingUser(boolean switching) {
1726 mSwitchingUser = switching;
Selim Cinekbbe19242017-12-08 15:42:08 -08001727 // Since this comes in on a binder thread, we need to post if first
1728 mHandler.post(mUpdateFingerprintListeningState);
Evan Rosky18396452016-07-27 15:19:37 -07001729 }
1730
Jim Miller6212cc02012-09-05 17:35:31 -07001731 private void sendUpdates(KeyguardUpdateMonitorCallback callback) {
1732 // Notify listener of the current state
1733 callback.onRefreshBatteryInfo(mBatteryStatus);
1734 callback.onTimeChanged();
1735 callback.onRingerModeChanged(mRingMode);
1736 callback.onPhoneStateChanged(mPhoneState);
Jason Monk9ff69bd2014-12-02 16:43:17 -05001737 callback.onRefreshCarrierInfo();
Jim Miller6212cc02012-09-05 17:35:31 -07001738 callback.onClockVisibilityChanged();
Lucas Dupin16cfe452018-02-08 13:14:50 -08001739 callback.onKeyguardVisibilityChangedRaw(mKeyguardIsVisible);
Jim Miller52a61332014-11-12 19:29:51 -08001740 for (Entry<Integer, SimData> data : mSimDatas.entrySet()) {
1741 final SimData state = data.getValue();
1742 callback.onSimStateChanged(state.subId, state.slotId, state.simState);
1743 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001744 }
1745
Selim Cinek1fcafc42015-07-20 14:39:25 -07001746 public void sendKeyguardReset() {
1747 mHandler.obtainMessage(MSG_KEYGUARD_RESET).sendToTarget();
1748 }
1749
Adrian Roosb6011622014-05-14 15:52:53 +02001750 /**
1751 * @see #handleKeyguardBouncerChanged(int)
1752 */
1753 public void sendKeyguardBouncerChanged(boolean showingBouncer) {
1754 if (DEBUG) Log.d(TAG, "sendKeyguardBouncerChanged(" + showingBouncer + ")");
1755 Message message = mHandler.obtainMessage(MSG_KEYGUARD_BOUNCER_CHANGED);
1756 message.arg1 = showingBouncer ? 1 : 0;
1757 message.sendToTarget();
1758 }
1759
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001760 /**
Jim Miller90d5d462011-11-17 16:57:01 -08001761 * 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 -08001762 * have the information earlier than waiting for the intent
1763 * broadcast from the telephony code.
Jim Miller90d5d462011-11-17 16:57:01 -08001764 *
1765 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going
1766 * through mHandler, this *must* be called from the UI thread.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001767 */
Adrian Roos30a2ae62018-04-25 19:09:50 +02001768 @MainThread
Jim Miller52a61332014-11-12 19:29:51 -08001769 public void reportSimUnlocked(int subId) {
1770 if (DEBUG_SIM_STATES) Log.v(TAG, "reportSimUnlocked(subId=" + subId + ")");
Sanket Padawe7e460252017-03-10 16:18:20 -08001771 int slotId = SubscriptionManager.getSlotIndex(subId);
Jim Miller52a61332014-11-12 19:29:51 -08001772 handleSimStateChange(subId, slotId, State.READY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001773 }
1774
Brian Colonna7fce3802013-09-17 15:51:32 -04001775 /**
1776 * Report that the emergency call button has been pressed and the emergency dialer is
1777 * about to be displayed.
1778 *
1779 * @param bypassHandler runs immediately.
1780 *
1781 * NOTE: Must be called from UI thread if bypassHandler == true.
1782 */
1783 public void reportEmergencyCallAction(boolean bypassHandler) {
1784 if (!bypassHandler) {
1785 mHandler.obtainMessage(MSG_REPORT_EMERGENCY_CALL_ACTION).sendToTarget();
1786 } else {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001787 checkIsHandlerThread();
Brian Colonna7fce3802013-09-17 15:51:32 -04001788 handleReportEmergencyCallAction();
1789 }
1790 }
1791
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001792 /**
1793 * @return Whether the device is provisioned (whether they have gone through
1794 * the setup wizard)
1795 */
1796 public boolean isDeviceProvisioned() {
1797 return mDeviceProvisioned;
1798 }
1799
Kensuke Matsui21d1bf12017-03-14 13:27:20 +09001800 public ServiceState getServiceState(int subId) {
1801 return mServiceStates.get(subId);
1802 }
1803
Jim Millerf41fc962014-06-18 16:33:51 -07001804 public void clearFingerprintRecognized() {
Jim Miller9f0753f2015-03-23 23:59:22 -07001805 mUserFingerprintAuthenticated.clear();
Kevin Chyn3fdbbf82017-05-06 15:11:53 -07001806 mTrustManager.clearAllFingerprints();
Jim Millerf41fc962014-06-18 16:33:51 -07001807 }
1808
Jim Miller52a61332014-11-12 19:29:51 -08001809 public boolean isSimPinVoiceSecure() {
1810 // TODO: only count SIMs that handle voice
1811 return isSimPinSecure();
Jim Millerdcb3d842012-08-23 19:18:12 -07001812 }
1813
1814 public boolean isSimPinSecure() {
Jim Miller52a61332014-11-12 19:29:51 -08001815 // True if any SIM is pin secure
1816 for (SubscriptionInfo info : getSubscriptionInfo(false /* forceReload */)) {
1817 if (isSimPinSecure(getSimState(info.getSubscriptionId()))) return true;
1818 }
1819 return false;
1820 }
1821
Jason Monk9ff69bd2014-12-02 16:43:17 -05001822 public State getSimState(int subId) {
Jim Miller52a61332014-11-12 19:29:51 -08001823 if (mSimDatas.containsKey(subId)) {
1824 return mSimDatas.get(subId).simState;
1825 } else {
1826 return State.UNKNOWN;
1827 }
1828 }
1829
Winson Chungaa357452017-10-31 11:35:30 -07001830 private final SysUiTaskStackChangeListener
1831 mTaskStackListener = new SysUiTaskStackChangeListener() {
Kevin Chyn2fefd462017-04-28 12:18:19 -07001832 @Override
1833 public void onTaskStackChangedBackground() {
1834 try {
1835 ActivityManager.StackInfo info = ActivityManager.getService().getStackInfo(
Wale Ogunwale68278562017-09-23 17:13:55 -07001836 WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_ASSISTANT);
Kevin Chyn2fefd462017-04-28 12:18:19 -07001837 if (info == null) {
1838 return;
1839 }
1840 mHandler.sendMessage(mHandler.obtainMessage(MSG_ASSISTANT_STACK_CHANGED,
1841 info.visible));
1842 } catch (RemoteException e) {
1843 Log.e(TAG, "unable to check task stack", e);
1844 }
1845 }
1846 };
1847
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001848 /**
Richard Choue0381b82018-04-24 03:48:59 +00001849 * @return true if and only if the state has changed for the specified {@code slotId}
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001850 */
Richard Choue0381b82018-04-24 03:48:59 +00001851 private boolean refreshSimState(int subId, int slotId) {
Jim Miller52a61332014-11-12 19:29:51 -08001852
1853 // This is awful. It exists because there are two APIs for getting the SIM status
1854 // that don't return the complete set of values and have different types. In Keyguard we
1855 // need IccCardConstants, but TelephonyManager would only give us
1856 // TelephonyManager.SIM_STATE*, so we retrieve it manually.
xinhe18b9c3c2014-12-02 15:03:20 -08001857 final TelephonyManager tele = TelephonyManager.from(mContext);
Richard Choue0381b82018-04-24 03:48:59 +00001858 int simState = tele.getSimState(slotId);
1859 State state;
1860 try {
1861 state = State.intToState(simState);
1862 } catch(IllegalArgumentException ex) {
1863 Log.w(TAG, "Unknown sim state: " + simState);
1864 state = State.UNKNOWN;
John Spurlock5b13e922015-01-07 11:04:58 -05001865 }
Richard Choue0381b82018-04-24 03:48:59 +00001866 SimData data = mSimDatas.get(subId);
1867 final boolean changed;
1868 if (data == null) {
1869 data = new SimData(state, slotId, subId);
1870 mSimDatas.put(subId, data);
1871 changed = true; // no data yet; force update
1872 } else {
1873 changed = data.simState != state;
1874 data.simState = state;
Jorim Jaggi01ba98b2015-01-13 21:33:45 +01001875 }
Richard Choue0381b82018-04-24 03:48:59 +00001876 return changed;
Jim Millerdcb3d842012-08-23 19:18:12 -07001877 }
1878
1879 public static boolean isSimPinSecure(IccCardConstants.State state) {
1880 final IccCardConstants.State simState = state;
1881 return (simState == IccCardConstants.State.PIN_REQUIRED
1882 || simState == IccCardConstants.State.PUK_REQUIRED
1883 || simState == IccCardConstants.State.PERM_DISABLED);
Jim Millerb0304762012-03-13 20:01:25 -07001884 }
Jim Miller8f09fd22013-03-14 19:04:28 -07001885
1886 public DisplayClientState getCachedDisplayClientState() {
1887 return mDisplayClientState;
1888 }
Jim Miller20daffd2013-10-07 14:59:53 -07001889
1890 // TODO: use these callbacks elsewhere in place of the existing notifyScreen*()
1891 // (KeyguardViewMediator, KeyguardHostView)
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001892 public void dispatchStartedWakingUp() {
1893 synchronized (this) {
1894 mDeviceInteractive = true;
1895 }
1896 mHandler.sendEmptyMessage(MSG_STARTED_WAKING_UP);
1897 }
1898
Jorim Jaggi95e40382015-09-16 15:53:42 -07001899 public void dispatchStartedGoingToSleep(int why) {
1900 mHandler.sendMessage(mHandler.obtainMessage(MSG_STARTED_GOING_TO_SLEEP, why, 0));
1901 }
1902
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001903 public void dispatchFinishedGoingToSleep(int why) {
1904 synchronized(this) {
1905 mDeviceInteractive = false;
1906 }
1907 mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISHED_GOING_TO_SLEEP, why, 0));
1908 }
1909
Jim Miller20daffd2013-10-07 14:59:53 -07001910 public void dispatchScreenTurnedOn() {
1911 synchronized (this) {
1912 mScreenOn = true;
1913 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001914 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_ON);
Jim Miller20daffd2013-10-07 14:59:53 -07001915 }
1916
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001917 public void dispatchScreenTurnedOff() {
Jim Miller20daffd2013-10-07 14:59:53 -07001918 synchronized(this) {
1919 mScreenOn = false;
1920 }
Jorim Jaggif1518da2015-07-30 11:56:36 -07001921 mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
Jim Miller20daffd2013-10-07 14:59:53 -07001922 }
1923
Selim Cinek99415392016-09-09 14:58:41 -07001924 public void dispatchDreamingStarted() {
1925 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 1, 0));
1926 }
1927
1928 public void dispatchDreamingStopped() {
1929 mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 0, 0));
1930 }
1931
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001932 public boolean isDeviceInteractive() {
1933 return mDeviceInteractive;
Jim Miller20daffd2013-10-07 14:59:53 -07001934 }
Jim Miller52a61332014-11-12 19:29:51 -08001935
Jorim Jaggi95e40382015-09-16 15:53:42 -07001936 public boolean isGoingToSleep() {
1937 return mGoingToSleep;
1938 }
1939
Jim Miller52a61332014-11-12 19:29:51 -08001940 /**
1941 * Find the next SubscriptionId for a SIM in the given state, favoring lower slot numbers first.
1942 * @param state
Wink Savilled09c4ca2014-11-22 10:08:16 -08001943 * @return subid or {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID} if none found
Jim Miller52a61332014-11-12 19:29:51 -08001944 */
1945 public int getNextSubIdForState(State state) {
1946 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
Wink Savilled09c4ca2014-11-22 10:08:16 -08001947 int resultId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Jim Miller52a61332014-11-12 19:29:51 -08001948 int bestSlotId = Integer.MAX_VALUE; // Favor lowest slot first
1949 for (int i = 0; i < list.size(); i++) {
1950 final SubscriptionInfo info = list.get(i);
1951 final int id = info.getSubscriptionId();
Sanket Padawe7e460252017-03-10 16:18:20 -08001952 int slotId = SubscriptionManager.getSlotIndex(id);
Jim Miller52a61332014-11-12 19:29:51 -08001953 if (state == getSimState(id) && bestSlotId > slotId ) {
1954 resultId = id;
1955 bestSlotId = slotId;
1956 }
1957 }
1958 return resultId;
1959 }
1960
1961 public SubscriptionInfo getSubscriptionInfoForSubId(int subId) {
1962 List<SubscriptionInfo> list = getSubscriptionInfo(false /* forceReload */);
1963 for (int i = 0; i < list.size(); i++) {
1964 SubscriptionInfo info = list.get(i);
1965 if (subId == info.getSubscriptionId()) return info;
1966 }
1967 return null; // not found
1968 }
Jason Monkab525272015-07-13 17:02:49 -04001969
Alex Chauff7653d2018-02-01 17:18:08 +00001970 /**
1971 * @return a cached version of DevicePolicyManager.isLogoutEnabled()
1972 */
1973 public boolean isLogoutEnabled() {
1974 return mLogoutEnabled;
1975 }
1976
1977 private void updateLogoutEnabled() {
Adrian Roos30a2ae62018-04-25 19:09:50 +02001978 checkIsHandlerThread();
Alex Chauff7653d2018-02-01 17:18:08 +00001979 boolean logoutEnabled = mDevicePolicyManager.isLogoutEnabled();
1980 if (mLogoutEnabled != logoutEnabled) {
1981 mLogoutEnabled = logoutEnabled;
1982 for (int i = 0; i < mCallbacks.size(); i++) {
1983 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
1984 if (cb != null) {
1985 cb.onLogoutEnabledChanged();
1986 }
1987 }
1988 }
1989 }
1990
Adrian Roos30a2ae62018-04-25 19:09:50 +02001991 private void checkIsHandlerThread() {
1992 if (sDisableHandlerCheckForTesting) {
1993 return;
1994 }
1995 if (!mHandler.getLooper().isCurrentThread()) {
1996 Log.wtf(TAG, "must call on mHandler's thread "
1997 + mHandler.getLooper().getThread() + ", not " + Thread.currentThread());
1998 }
1999 }
2000
2001 /**
2002 * Turn off the handler check for testing.
2003 *
2004 * This is necessary because currently tests are not too careful about which thread they call
2005 * into this class on.
2006 *
2007 * Note that this must be called before scheduling any work involving KeyguardUpdateMonitor
2008 * instances.
2009 *
2010 * TODO: fix the tests and remove this.
2011 */
2012 @VisibleForTesting
2013 public static void disableHandlerCheckForTesting(Instrumentation instrumentation) {
2014 Preconditions.checkNotNull(instrumentation, "Must only call this method in tests!");
2015 // Don't need synchronization here *if* the callers follow the contract and call this only
2016 // before scheduling work for KeyguardUpdateMonitor on other threads, because the scheduling
2017 // of that work forces a happens-before relationship.
2018 sDisableHandlerCheckForTesting = true;
2019 }
2020
Jason Monkab525272015-07-13 17:02:49 -04002021 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2022 pw.println("KeyguardUpdateMonitor state:");
2023 pw.println(" SIM States:");
2024 for (SimData data : mSimDatas.values()) {
2025 pw.println(" " + data.toString());
2026 }
2027 pw.println(" Subs:");
2028 if (mSubscriptionInfo != null) {
2029 for (int i = 0; i < mSubscriptionInfo.size(); i++) {
2030 pw.println(" " + mSubscriptionInfo.get(i));
2031 }
2032 }
2033 pw.println(" Service states:");
2034 for (int subId : mServiceStates.keySet()) {
2035 pw.println(" " + subId + "=" + mServiceStates.get(subId));
2036 }
Jim Millerd72d5ac2015-09-29 18:55:32 -07002037 if (mFpm != null && mFpm.isHardwareDetected()) {
2038 final int userId = ActivityManager.getCurrentUser();
2039 final int strongAuthFlags = mStrongAuthTracker.getStrongAuthForUser(userId);
2040 pw.println(" Fingerprint state (user=" + userId + ")");
2041 pw.println(" allowed=" + isUnlockingWithFingerprintAllowed());
2042 pw.println(" auth'd=" + mUserFingerprintAuthenticated.get(userId));
2043 pw.println(" authSinceBoot="
2044 + getStrongAuthTracker().hasUserAuthenticatedSinceBoot());
2045 pw.println(" disabled(DPM)=" + isFingerprintDisabled(userId));
2046 pw.println(" possible=" + isUnlockWithFingerprintPossible(userId));
2047 pw.println(" strongAuthFlags=" + Integer.toHexString(strongAuthFlags));
Jim Millerd72d5ac2015-09-29 18:55:32 -07002048 pw.println(" trustManaged=" + getUserTrustIsManaged(userId));
2049 }
Jason Monkab525272015-07-13 17:02:49 -04002050 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08002051}