blob: b1d42e7eb3ba62a0baf411accb10d4738566e854 [file] [log] [blame]
Jim Miller5a8daad2014-01-14 18:57:03 -08001/*
2 * Copyright (C) 2014 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 */
Jim Miller5ecd8112013-01-09 18:50:26 -080016package com.android.keyguard;
Jim Miller838906b2012-10-19 18:41:25 -070017
Jim Miller7751ff62014-01-14 18:57:03 -080018import android.app.Activity;
19import android.app.AlertDialog;
Amith Yamasani3a3d2122014-10-29 11:41:31 -070020import android.app.admin.DevicePolicyManager;
Jim Miller838906b2012-10-19 18:41:25 -070021import android.content.Context;
Amith Yamasani3a3d2122014-10-29 11:41:31 -070022import android.os.UserHandle;
Jim Miller838906b2012-10-19 18:41:25 -070023import android.util.AttributeSet;
Jim Miller7751ff62014-01-14 18:57:03 -080024import android.util.Log;
25import android.util.Slog;
26import android.view.LayoutInflater;
Chris Wren052999f2012-11-02 14:36:56 -040027import android.view.View;
Jim Miller7751ff62014-01-14 18:57:03 -080028import android.view.WindowManager;
Jim Miller838906b2012-10-19 18:41:25 -070029import android.widget.FrameLayout;
30
Jim Miller7751ff62014-01-14 18:57:03 -080031import com.android.internal.widget.LockPatternUtils;
32import com.android.keyguard.KeyguardSecurityModel.SecurityMode;
33
34public class KeyguardSecurityContainer extends FrameLayout implements KeyguardSecurityView {
Jorim Jaggi5cf17872014-03-26 18:31:48 +010035 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller7751ff62014-01-14 18:57:03 -080036 private static final String TAG = "KeyguardSecurityView";
Amith Yamasani3a3d2122014-10-29 11:41:31 -070037
38 private static final int USER_TYPE_PRIMARY = 1;
39 private static final int USER_TYPE_WORK_PROFILE = 2;
40 private static final int USER_TYPE_SECONDARY_USER = 3;
41
Jim Miller7751ff62014-01-14 18:57:03 -080042 private KeyguardSecurityModel mSecurityModel;
Jim Miller7751ff62014-01-14 18:57:03 -080043 private LockPatternUtils mLockPatternUtils;
44
45 private KeyguardSecurityViewFlipper mSecurityViewFlipper;
46 private boolean mIsVerifyUnlockOnly;
47 private SecurityMode mCurrentSecuritySelection = SecurityMode.Invalid;
Jim Miller7751ff62014-01-14 18:57:03 -080048 private SecurityCallback mSecurityCallback;
49
Adrian Roos336be7f2014-05-19 17:11:18 +020050 private final KeyguardUpdateMonitor mUpdateMonitor;
51
Jim Miller7751ff62014-01-14 18:57:03 -080052 // Used to notify the container when something interesting happens.
53 public interface SecurityCallback {
Jim Millerba7d94b2014-02-05 17:30:50 -080054 public boolean dismiss(boolean authenticated);
Jorim Jaggib690f0d2014-07-03 23:25:44 +020055 public void userActivity();
Jim Millerba7d94b2014-02-05 17:30:50 -080056 public void onSecurityModeChanged(SecurityMode securityMode, boolean needsInput);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -070057
58 /**
59 * @param strongAuth wheher the user has authenticated with strong authentication like
60 * pattern, password or PIN but not by trust agents or fingerprint
61 */
62 public void finish(boolean strongAuth);
Andrew Lee72b46d42015-01-30 13:23:21 -080063 public void reset();
Jim Miller7751ff62014-01-14 18:57:03 -080064 }
65
Jim Miller838906b2012-10-19 18:41:25 -070066 public KeyguardSecurityContainer(Context context, AttributeSet attrs) {
67 this(context, attrs, 0);
68 }
69
70 public KeyguardSecurityContainer(Context context) {
Esteban Talaverafe0f24c2014-08-06 16:20:56 +010071 this(context, null, 0);
Jim Miller838906b2012-10-19 18:41:25 -070072 }
73
74 public KeyguardSecurityContainer(Context context, AttributeSet attrs, int defStyle) {
75 super(context, attrs, defStyle);
Jim Miller7751ff62014-01-14 18:57:03 -080076 mSecurityModel = new KeyguardSecurityModel(context);
77 mLockPatternUtils = new LockPatternUtils(context);
Adrian Roos336be7f2014-05-19 17:11:18 +020078 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Chris Wrenc3451462012-10-30 11:22:58 -040079 }
80
Jim Miller7751ff62014-01-14 18:57:03 -080081 public void setSecurityCallback(SecurityCallback callback) {
82 mSecurityCallback = callback;
83 }
84
85 @Override
86 public void onResume(int reason) {
Jorim Jaggif4797922014-08-04 22:49:41 +020087 if (mCurrentSecuritySelection != SecurityMode.None) {
88 getSecurityView(mCurrentSecuritySelection).onResume(reason);
89 }
Jim Miller7751ff62014-01-14 18:57:03 -080090 }
91
92 @Override
93 public void onPause() {
Jorim Jaggif4797922014-08-04 22:49:41 +020094 if (mCurrentSecuritySelection != SecurityMode.None) {
95 getSecurityView(mCurrentSecuritySelection).onPause();
96 }
Jim Miller7751ff62014-01-14 18:57:03 -080097 }
98
Jorim Jaggic14f8292014-05-27 02:25:45 +020099 public void startAppearAnimation() {
Jorim Jaggif4797922014-08-04 22:49:41 +0200100 if (mCurrentSecuritySelection != SecurityMode.None) {
101 getSecurityView(mCurrentSecuritySelection).startAppearAnimation();
102 }
Jorim Jaggic14f8292014-05-27 02:25:45 +0200103 }
104
Jorim Jaggi76a16232014-08-08 17:00:47 +0200105 public boolean startDisappearAnimation(Runnable onFinishRunnable) {
106 if (mCurrentSecuritySelection != SecurityMode.None) {
107 return getSecurityView(mCurrentSecuritySelection).startDisappearAnimation(
108 onFinishRunnable);
109 }
110 return false;
111 }
112
Jim Miller7751ff62014-01-14 18:57:03 -0800113 public void announceCurrentSecurityMethod() {
114 View v = (View) getSecurityView(mCurrentSecuritySelection);
115 if (v != null) {
116 v.announceForAccessibility(v.getContentDescription());
117 }
118 }
119
Jorim Jaggic1dff8c2015-02-02 14:45:39 +0100120 public CharSequence getCurrentSecurityModeContentDescription() {
121 View v = (View) getSecurityView(mCurrentSecuritySelection);
122 if (v != null) {
123 return v.getContentDescription();
124 }
125 return "";
126 }
127
Jim Miller7751ff62014-01-14 18:57:03 -0800128 private KeyguardSecurityView getSecurityView(SecurityMode securityMode) {
129 final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
130 KeyguardSecurityView view = null;
131 final int children = mSecurityViewFlipper.getChildCount();
132 for (int child = 0; child < children; child++) {
133 if (mSecurityViewFlipper.getChildAt(child).getId() == securityViewIdForMode) {
134 view = ((KeyguardSecurityView)mSecurityViewFlipper.getChildAt(child));
135 break;
136 }
137 }
138 int layoutId = getLayoutIdFor(securityMode);
139 if (view == null && layoutId != 0) {
140 final LayoutInflater inflater = LayoutInflater.from(mContext);
141 if (DEBUG) Log.v(TAG, "inflating id = " + layoutId);
142 View v = inflater.inflate(layoutId, mSecurityViewFlipper, false);
143 mSecurityViewFlipper.addView(v);
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100144 updateSecurityView(v);
Jim Miller7751ff62014-01-14 18:57:03 -0800145 view = (KeyguardSecurityView)v;
146 }
147
Jim Miller7751ff62014-01-14 18:57:03 -0800148 return view;
149 }
150
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100151 private void updateSecurityView(View view) {
Jim Miller7751ff62014-01-14 18:57:03 -0800152 if (view instanceof KeyguardSecurityView) {
153 KeyguardSecurityView ksv = (KeyguardSecurityView) view;
154 ksv.setKeyguardCallback(mCallback);
155 ksv.setLockPatternUtils(mLockPatternUtils);
Jim Miller7751ff62014-01-14 18:57:03 -0800156 } else {
157 Log.w(TAG, "View " + view + " is not a KeyguardSecurityView");
158 }
159 }
160
161 protected void onFinishInflate() {
162 mSecurityViewFlipper = (KeyguardSecurityViewFlipper) findViewById(R.id.view_flipper);
163 mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils);
164 }
165
166 public void setLockPatternUtils(LockPatternUtils utils) {
167 mLockPatternUtils = utils;
168 mSecurityModel.setLockPatternUtils(utils);
169 mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils);
170 }
171
172 private void showDialog(String title, String message) {
173 final AlertDialog dialog = new AlertDialog.Builder(mContext)
174 .setTitle(title)
175 .setMessage(message)
176 .setNeutralButton(R.string.ok, null)
177 .create();
178 if (!(mContext instanceof Activity)) {
179 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
180 }
181 dialog.show();
182 }
183
Andres Morales23974272015-05-14 22:42:26 -0700184 private void showTimeoutDialog(int timeoutMs) {
185 int timeoutInSeconds = (int) timeoutMs / 1000;
Jim Miller7751ff62014-01-14 18:57:03 -0800186 int messageId = 0;
187
188 switch (mSecurityModel.getSecurityMode()) {
189 case Pattern:
190 messageId = R.string.kg_too_many_failed_pattern_attempts_dialog_message;
191 break;
192 case PIN:
193 messageId = R.string.kg_too_many_failed_pin_attempts_dialog_message;
194 break;
195 case Password:
196 messageId = R.string.kg_too_many_failed_password_attempts_dialog_message;
197 break;
198 // These don't have timeout dialogs.
Jim Miller7751ff62014-01-14 18:57:03 -0800199 case Invalid:
200 case None:
201 case SimPin:
202 case SimPuk:
203 break;
204 }
205
206 if (messageId != 0) {
207 final String message = mContext.getString(messageId,
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800208 KeyguardUpdateMonitor.getInstance(mContext).getFailedUnlockAttempts(
209 KeyguardUpdateMonitor.getCurrentUser()),
Jim Miller7751ff62014-01-14 18:57:03 -0800210 timeoutInSeconds);
211 showDialog(null, message);
212 }
213 }
214
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700215 private void showAlmostAtWipeDialog(int attempts, int remaining, int userType) {
216 String message = null;
217 switch (userType) {
218 case USER_TYPE_PRIMARY:
219 message = mContext.getString(R.string.kg_failed_attempts_almost_at_wipe,
220 attempts, remaining);
221 break;
222 case USER_TYPE_SECONDARY_USER:
223 message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_user,
224 attempts, remaining);
225 break;
226 case USER_TYPE_WORK_PROFILE:
227 message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_profile,
228 attempts, remaining);
229 break;
230 }
Jim Miller7751ff62014-01-14 18:57:03 -0800231 showDialog(null, message);
232 }
233
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700234 private void showWipeDialog(int attempts, int userType) {
235 String message = null;
236 switch (userType) {
237 case USER_TYPE_PRIMARY:
238 message = mContext.getString(R.string.kg_failed_attempts_now_wiping,
239 attempts);
240 break;
241 case USER_TYPE_SECONDARY_USER:
242 message = mContext.getString(R.string.kg_failed_attempts_now_erasing_user,
243 attempts);
244 break;
245 case USER_TYPE_WORK_PROFILE:
246 message = mContext.getString(R.string.kg_failed_attempts_now_erasing_profile,
247 attempts);
248 break;
249 }
Jim Miller7751ff62014-01-14 18:57:03 -0800250 showDialog(null, message);
251 }
252
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800253 private void reportFailedUnlockAttempt(int userId, int timeoutMs) {
Jim Miller7751ff62014-01-14 18:57:03 -0800254 final KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800255 final int failedAttempts = monitor.getFailedUnlockAttempts(userId) + 1; // +1 for this time
Jim Miller7751ff62014-01-14 18:57:03 -0800256
257 if (DEBUG) Log.d(TAG, "reportFailedPatternAttempt: #" + failedAttempts);
258
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700259 final DevicePolicyManager dpm = mLockPatternUtils.getDevicePolicyManager();
260 final int failedAttemptsBeforeWipe =
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800261 dpm.getMaximumFailedPasswordsForWipe(null, userId);
Jim Miller7751ff62014-01-14 18:57:03 -0800262
Jim Miller7751ff62014-01-14 18:57:03 -0800263 final int remainingBeforeWipe = failedAttemptsBeforeWipe > 0 ?
264 (failedAttemptsBeforeWipe - failedAttempts)
265 : Integer.MAX_VALUE; // because DPM returns 0 if no restriction
Jim Miller7751ff62014-01-14 18:57:03 -0800266 if (remainingBeforeWipe < LockPatternUtils.FAILED_ATTEMPTS_BEFORE_WIPE_GRACE) {
Esteban Talaverafe0f24c2014-08-06 16:20:56 +0100267 // The user has installed a DevicePolicyManager that requests a user/profile to be wiped
268 // N attempts. Once we get below the grace period, we post this dialog every time as a
269 // clear warning until the deletion fires.
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700270 // Check which profile has the strictest policy for failed password attempts
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800271 final int expiringUser = dpm.getProfileWithMinimumFailedPasswordsForWipe(userId);
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700272 int userType = USER_TYPE_PRIMARY;
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800273 if (expiringUser == userId) {
Xiaohui Chencc791bc2015-08-26 14:54:34 -0700274 // TODO: http://b/23522538
275 if (expiringUser != UserHandle.USER_SYSTEM) {
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700276 userType = USER_TYPE_SECONDARY_USER;
277 }
278 } else if (expiringUser != UserHandle.USER_NULL) {
279 userType = USER_TYPE_WORK_PROFILE;
280 } // If USER_NULL, which shouldn't happen, leave it as USER_TYPE_PRIMARY
Jim Miller7751ff62014-01-14 18:57:03 -0800281 if (remainingBeforeWipe > 0) {
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700282 showAlmostAtWipeDialog(failedAttempts, remainingBeforeWipe, userType);
Jim Miller7751ff62014-01-14 18:57:03 -0800283 } else {
284 // Too many attempts. The device will be wiped shortly.
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700285 Slog.i(TAG, "Too many unlock attempts; user " + expiringUser + " will be wiped!");
286 showWipeDialog(failedAttempts, userType);
Jim Miller7751ff62014-01-14 18:57:03 -0800287 }
Jim Miller7751ff62014-01-14 18:57:03 -0800288 }
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800289 monitor.reportFailedStrongAuthUnlockAttempt(userId);
290 mLockPatternUtils.reportFailedPasswordAttempt(userId);
Andres Morales23974272015-05-14 22:42:26 -0700291 if (timeoutMs > 0) {
292 showTimeoutDialog(timeoutMs);
Jim Miller7751ff62014-01-14 18:57:03 -0800293 }
294 }
295
296 /**
297 * Shows the primary security screen for the user. This will be either the multi-selector
298 * or the user's security method.
299 * @param turningOff true if the device is being turned off
300 */
301 void showPrimarySecurityScreen(boolean turningOff) {
302 SecurityMode securityMode = mSecurityModel.getSecurityMode();
303 if (DEBUG) Log.v(TAG, "showPrimarySecurityScreen(turningOff=" + turningOff + ")");
Jim Miller7751ff62014-01-14 18:57:03 -0800304 showSecurityScreen(securityMode);
305 }
306
307 /**
Jim Millerba7d94b2014-02-05 17:30:50 -0800308 * Shows the next security screen if there is one.
309 * @param authenticated true if the user entered the correct authentication
Jim Millerba7d94b2014-02-05 17:30:50 -0800310 * @return true if keyguard is done
311 */
Jim Miller7751ff62014-01-14 18:57:03 -0800312 boolean showNextSecurityScreenOrFinish(boolean authenticated) {
313 if (DEBUG) Log.d(TAG, "showNextSecurityScreenOrFinish(" + authenticated + ")");
314 boolean finish = false;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700315 boolean strongAuth = false;
Selim Cineke8bae622015-07-15 13:24:06 -0700316 if (mUpdateMonitor.getUserCanSkipBouncer(
317 KeyguardUpdateMonitor.getCurrentUser())) {
Adrian Roos336be7f2014-05-19 17:11:18 +0200318 finish = true;
319 } else if (SecurityMode.None == mCurrentSecuritySelection) {
Jim Miller7751ff62014-01-14 18:57:03 -0800320 SecurityMode securityMode = mSecurityModel.getSecurityMode();
Jim Miller7751ff62014-01-14 18:57:03 -0800321 if (SecurityMode.None == securityMode) {
322 finish = true; // no security required
323 } else {
324 showSecurityScreen(securityMode); // switch to the alternate security view
325 }
326 } else if (authenticated) {
327 switch (mCurrentSecuritySelection) {
328 case Pattern:
329 case Password:
330 case PIN:
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700331 strongAuth = true;
Jim Miller7751ff62014-01-14 18:57:03 -0800332 finish = true;
333 break;
334
335 case SimPin:
336 case SimPuk:
337 // Shortcut for SIM PIN/PUK to go to directly to user's security screen or home
338 SecurityMode securityMode = mSecurityModel.getSecurityMode();
Selim Cinek245273e2015-06-24 13:17:56 -0400339 if (securityMode != SecurityMode.None
340 || !mLockPatternUtils.isLockScreenDisabled(
341 KeyguardUpdateMonitor.getCurrentUser())) {
Jim Miller7751ff62014-01-14 18:57:03 -0800342 showSecurityScreen(securityMode);
343 } else {
344 finish = true;
345 }
346 break;
347
348 default:
349 Log.v(TAG, "Bad security screen " + mCurrentSecuritySelection + ", fail safe");
350 showPrimarySecurityScreen(false);
351 break;
352 }
Jim Miller7751ff62014-01-14 18:57:03 -0800353 }
354 if (finish) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700355 mSecurityCallback.finish(strongAuth);
Jim Miller7751ff62014-01-14 18:57:03 -0800356 }
357 return finish;
358 }
359
360 /**
361 * Switches to the given security view unless it's already being shown, in which case
362 * this is a no-op.
363 *
364 * @param securityMode
365 */
366 private void showSecurityScreen(SecurityMode securityMode) {
367 if (DEBUG) Log.d(TAG, "showSecurityScreen(" + securityMode + ")");
368
369 if (securityMode == mCurrentSecuritySelection) return;
370
371 KeyguardSecurityView oldView = getSecurityView(mCurrentSecuritySelection);
372 KeyguardSecurityView newView = getSecurityView(securityMode);
373
374 // Emulate Activity life cycle
375 if (oldView != null) {
376 oldView.onPause();
377 oldView.setKeyguardCallback(mNullCallback); // ignore requests from old view
378 }
Jorim Jaggif4797922014-08-04 22:49:41 +0200379 if (securityMode != SecurityMode.None) {
380 newView.onResume(KeyguardSecurityView.VIEW_REVEALED);
381 newView.setKeyguardCallback(mCallback);
382 }
Jim Miller7751ff62014-01-14 18:57:03 -0800383
384 // Find and show this child.
385 final int childCount = mSecurityViewFlipper.getChildCount();
386
387 final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
388 for (int i = 0; i < childCount; i++) {
389 if (mSecurityViewFlipper.getChildAt(i).getId() == securityViewIdForMode) {
390 mSecurityViewFlipper.setDisplayedChild(i);
391 break;
392 }
393 }
394
395 mCurrentSecuritySelection = securityMode;
Jorim Jaggif4797922014-08-04 22:49:41 +0200396 mSecurityCallback.onSecurityModeChanged(securityMode,
397 securityMode != SecurityMode.None && newView.needsInput());
Jim Miller7751ff62014-01-14 18:57:03 -0800398 }
399
400 private KeyguardSecurityViewFlipper getFlipper() {
Chris Wren052999f2012-11-02 14:36:56 -0400401 for (int i = 0; i < getChildCount(); i++) {
402 View child = getChildAt(i);
403 if (child instanceof KeyguardSecurityViewFlipper) {
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500404 return (KeyguardSecurityViewFlipper) child;
Chris Wren052999f2012-11-02 14:36:56 -0400405 }
406 }
407 return null;
408 }
409
Jim Miller7751ff62014-01-14 18:57:03 -0800410 private KeyguardSecurityCallback mCallback = new KeyguardSecurityCallback() {
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200411 public void userActivity() {
Jim Miller7751ff62014-01-14 18:57:03 -0800412 if (mSecurityCallback != null) {
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200413 mSecurityCallback.userActivity();
Jim Miller7751ff62014-01-14 18:57:03 -0800414 }
415 }
416
417 public void dismiss(boolean authenticated) {
418 mSecurityCallback.dismiss(authenticated);
419 }
420
421 public boolean isVerifyUnlockOnly() {
422 return mIsVerifyUnlockOnly;
423 }
424
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800425 public void reportUnlockAttempt(int userId, boolean success, int timeoutMs) {
Jim Miller7751ff62014-01-14 18:57:03 -0800426 KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
427 if (success) {
428 monitor.clearFailedUnlockAttempts();
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800429 mLockPatternUtils.reportSuccessfulPasswordAttempt(userId);
Jim Miller7751ff62014-01-14 18:57:03 -0800430 } else {
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800431 KeyguardSecurityContainer.this.reportFailedUnlockAttempt(userId, timeoutMs);
Jim Miller7751ff62014-01-14 18:57:03 -0800432 }
433 }
434
Andrew Lee72b46d42015-01-30 13:23:21 -0800435 public void reset() {
436 mSecurityCallback.reset();
437 }
Jim Miller7751ff62014-01-14 18:57:03 -0800438 };
439
440 // The following is used to ignore callbacks from SecurityViews that are no longer current
441 // (e.g. face unlock). This avoids unwanted asynchronous events from messing with the
442 // state for the current security method.
443 private KeyguardSecurityCallback mNullCallback = new KeyguardSecurityCallback() {
444 @Override
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200445 public void userActivity() { }
Jim Miller7751ff62014-01-14 18:57:03 -0800446 @Override
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800447 public void reportUnlockAttempt(int userId, boolean success, int timeoutMs) { }
Jim Miller7751ff62014-01-14 18:57:03 -0800448 @Override
449 public boolean isVerifyUnlockOnly() { return false; }
450 @Override
451 public void dismiss(boolean securityVerified) { }
Andrew Lee72b46d42015-01-30 13:23:21 -0800452 @Override
453 public void reset() {}
Jim Miller7751ff62014-01-14 18:57:03 -0800454 };
455
456 private int getSecurityViewIdForMode(SecurityMode securityMode) {
457 switch (securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800458 case Pattern: return R.id.keyguard_pattern_view;
459 case PIN: return R.id.keyguard_pin_view;
460 case Password: return R.id.keyguard_password_view;
Jim Miller7751ff62014-01-14 18:57:03 -0800461 case SimPin: return R.id.keyguard_sim_pin_view;
462 case SimPuk: return R.id.keyguard_sim_puk_view;
463 }
464 return 0;
465 }
466
467 private int getLayoutIdFor(SecurityMode securityMode) {
468 switch (securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800469 case Pattern: return R.layout.keyguard_pattern_view;
470 case PIN: return R.layout.keyguard_pin_view;
471 case Password: return R.layout.keyguard_password_view;
Jim Miller7751ff62014-01-14 18:57:03 -0800472 case SimPin: return R.layout.keyguard_sim_pin_view;
473 case SimPuk: return R.layout.keyguard_sim_puk_view;
474 default:
475 return 0;
476 }
477 }
478
479 public SecurityMode getSecurityMode() {
480 return mSecurityModel.getSecurityMode();
481 }
482
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100483 public SecurityMode getCurrentSecurityMode() {
484 return mCurrentSecuritySelection;
485 }
486
Jim Miller7751ff62014-01-14 18:57:03 -0800487 public void verifyUnlock() {
488 mIsVerifyUnlockOnly = true;
489 showSecurityScreen(getSecurityMode());
490 }
491
492 public SecurityMode getCurrentSecuritySelection() {
493 return mCurrentSecuritySelection;
494 }
495
496 public void dismiss(boolean authenticated) {
497 mCallback.dismiss(authenticated);
498 }
499
500 public boolean needsInput() {
501 return mSecurityViewFlipper.needsInput();
502 }
503
504 @Override
505 public void setKeyguardCallback(KeyguardSecurityCallback callback) {
506 mSecurityViewFlipper.setKeyguardCallback(callback);
507 }
508
509 @Override
510 public void reset() {
511 mSecurityViewFlipper.reset();
512 }
513
514 @Override
515 public KeyguardSecurityCallback getCallback() {
516 return mSecurityViewFlipper.getCallback();
517 }
518
519 @Override
Selim Cinek3122fa82015-06-18 01:38:59 -0700520 public void showPromptReason(int reason) {
521 if (mCurrentSecuritySelection != SecurityMode.None) {
Adrian Roos569edb82016-03-03 15:39:03 -0800522 if (reason != PROMPT_REASON_NONE) {
523 Log.i(TAG, "Strong auth required, reason: " + reason);
524 }
Selim Cinek3122fa82015-06-18 01:38:59 -0700525 getSecurityView(mCurrentSecuritySelection).showPromptReason(reason);
526 }
527 }
528
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700529
530 public void showMessage(String message, int color) {
531 if (mCurrentSecuritySelection != SecurityMode.None) {
532 getSecurityView(mCurrentSecuritySelection).showMessage(message, color);
533 }
534 }
535
Selim Cinek3122fa82015-06-18 01:38:59 -0700536 @Override
Jim Miller7751ff62014-01-14 18:57:03 -0800537 public void showUsabilityHint() {
538 mSecurityViewFlipper.showUsabilityHint();
539 }
540
Jim Miller838906b2012-10-19 18:41:25 -0700541}
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500542