blob: 61e2fc095edb20afecc8258f165b94aaf8165135 [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)
Edward Savage-Jonesb08a1462016-10-05 10:29:02 +0200176 .setCancelable(false)
Jim Miller7751ff62014-01-14 18:57:03 -0800177 .setNeutralButton(R.string.ok, null)
178 .create();
179 if (!(mContext instanceof Activity)) {
180 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
181 }
182 dialog.show();
183 }
184
Andres Morales23974272015-05-14 22:42:26 -0700185 private void showTimeoutDialog(int timeoutMs) {
186 int timeoutInSeconds = (int) timeoutMs / 1000;
Jim Miller7751ff62014-01-14 18:57:03 -0800187 int messageId = 0;
188
189 switch (mSecurityModel.getSecurityMode()) {
190 case Pattern:
191 messageId = R.string.kg_too_many_failed_pattern_attempts_dialog_message;
192 break;
193 case PIN:
194 messageId = R.string.kg_too_many_failed_pin_attempts_dialog_message;
195 break;
196 case Password:
197 messageId = R.string.kg_too_many_failed_password_attempts_dialog_message;
198 break;
199 // These don't have timeout dialogs.
Jim Miller7751ff62014-01-14 18:57:03 -0800200 case Invalid:
201 case None:
202 case SimPin:
203 case SimPuk:
204 break;
205 }
206
207 if (messageId != 0) {
208 final String message = mContext.getString(messageId,
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800209 KeyguardUpdateMonitor.getInstance(mContext).getFailedUnlockAttempts(
210 KeyguardUpdateMonitor.getCurrentUser()),
Jim Miller7751ff62014-01-14 18:57:03 -0800211 timeoutInSeconds);
212 showDialog(null, message);
213 }
214 }
215
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700216 private void showAlmostAtWipeDialog(int attempts, int remaining, int userType) {
217 String message = null;
218 switch (userType) {
219 case USER_TYPE_PRIMARY:
220 message = mContext.getString(R.string.kg_failed_attempts_almost_at_wipe,
221 attempts, remaining);
222 break;
223 case USER_TYPE_SECONDARY_USER:
224 message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_user,
225 attempts, remaining);
226 break;
227 case USER_TYPE_WORK_PROFILE:
228 message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_profile,
229 attempts, remaining);
230 break;
231 }
Jim Miller7751ff62014-01-14 18:57:03 -0800232 showDialog(null, message);
233 }
234
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700235 private void showWipeDialog(int attempts, int userType) {
236 String message = null;
237 switch (userType) {
238 case USER_TYPE_PRIMARY:
239 message = mContext.getString(R.string.kg_failed_attempts_now_wiping,
240 attempts);
241 break;
242 case USER_TYPE_SECONDARY_USER:
243 message = mContext.getString(R.string.kg_failed_attempts_now_erasing_user,
244 attempts);
245 break;
246 case USER_TYPE_WORK_PROFILE:
247 message = mContext.getString(R.string.kg_failed_attempts_now_erasing_profile,
248 attempts);
249 break;
250 }
Jim Miller7751ff62014-01-14 18:57:03 -0800251 showDialog(null, message);
252 }
253
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800254 private void reportFailedUnlockAttempt(int userId, int timeoutMs) {
Jim Miller7751ff62014-01-14 18:57:03 -0800255 final KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800256 final int failedAttempts = monitor.getFailedUnlockAttempts(userId) + 1; // +1 for this time
Jim Miller7751ff62014-01-14 18:57:03 -0800257
258 if (DEBUG) Log.d(TAG, "reportFailedPatternAttempt: #" + failedAttempts);
259
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700260 final DevicePolicyManager dpm = mLockPatternUtils.getDevicePolicyManager();
261 final int failedAttemptsBeforeWipe =
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800262 dpm.getMaximumFailedPasswordsForWipe(null, userId);
Jim Miller7751ff62014-01-14 18:57:03 -0800263
Jim Miller7751ff62014-01-14 18:57:03 -0800264 final int remainingBeforeWipe = failedAttemptsBeforeWipe > 0 ?
265 (failedAttemptsBeforeWipe - failedAttempts)
266 : Integer.MAX_VALUE; // because DPM returns 0 if no restriction
Jim Miller7751ff62014-01-14 18:57:03 -0800267 if (remainingBeforeWipe < LockPatternUtils.FAILED_ATTEMPTS_BEFORE_WIPE_GRACE) {
Esteban Talaverafe0f24c2014-08-06 16:20:56 +0100268 // The user has installed a DevicePolicyManager that requests a user/profile to be wiped
269 // N attempts. Once we get below the grace period, we post this dialog every time as a
270 // clear warning until the deletion fires.
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700271 // Check which profile has the strictest policy for failed password attempts
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800272 final int expiringUser = dpm.getProfileWithMinimumFailedPasswordsForWipe(userId);
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700273 int userType = USER_TYPE_PRIMARY;
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800274 if (expiringUser == userId) {
Xiaohui Chencc791bc2015-08-26 14:54:34 -0700275 // TODO: http://b/23522538
276 if (expiringUser != UserHandle.USER_SYSTEM) {
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700277 userType = USER_TYPE_SECONDARY_USER;
278 }
279 } else if (expiringUser != UserHandle.USER_NULL) {
280 userType = USER_TYPE_WORK_PROFILE;
281 } // If USER_NULL, which shouldn't happen, leave it as USER_TYPE_PRIMARY
Jim Miller7751ff62014-01-14 18:57:03 -0800282 if (remainingBeforeWipe > 0) {
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700283 showAlmostAtWipeDialog(failedAttempts, remainingBeforeWipe, userType);
Jim Miller7751ff62014-01-14 18:57:03 -0800284 } else {
285 // Too many attempts. The device will be wiped shortly.
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700286 Slog.i(TAG, "Too many unlock attempts; user " + expiringUser + " will be wiped!");
287 showWipeDialog(failedAttempts, userType);
Jim Miller7751ff62014-01-14 18:57:03 -0800288 }
Jim Miller7751ff62014-01-14 18:57:03 -0800289 }
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800290 monitor.reportFailedStrongAuthUnlockAttempt(userId);
291 mLockPatternUtils.reportFailedPasswordAttempt(userId);
Andres Morales23974272015-05-14 22:42:26 -0700292 if (timeoutMs > 0) {
293 showTimeoutDialog(timeoutMs);
Jim Miller7751ff62014-01-14 18:57:03 -0800294 }
295 }
296
297 /**
298 * Shows the primary security screen for the user. This will be either the multi-selector
299 * or the user's security method.
300 * @param turningOff true if the device is being turned off
301 */
302 void showPrimarySecurityScreen(boolean turningOff) {
303 SecurityMode securityMode = mSecurityModel.getSecurityMode();
304 if (DEBUG) Log.v(TAG, "showPrimarySecurityScreen(turningOff=" + turningOff + ")");
Jim Miller7751ff62014-01-14 18:57:03 -0800305 showSecurityScreen(securityMode);
306 }
307
308 /**
Jim Millerba7d94b2014-02-05 17:30:50 -0800309 * Shows the next security screen if there is one.
310 * @param authenticated true if the user entered the correct authentication
Jim Millerba7d94b2014-02-05 17:30:50 -0800311 * @return true if keyguard is done
312 */
Jim Miller7751ff62014-01-14 18:57:03 -0800313 boolean showNextSecurityScreenOrFinish(boolean authenticated) {
314 if (DEBUG) Log.d(TAG, "showNextSecurityScreenOrFinish(" + authenticated + ")");
315 boolean finish = false;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700316 boolean strongAuth = false;
Selim Cineke8bae622015-07-15 13:24:06 -0700317 if (mUpdateMonitor.getUserCanSkipBouncer(
318 KeyguardUpdateMonitor.getCurrentUser())) {
Adrian Roos336be7f2014-05-19 17:11:18 +0200319 finish = true;
320 } else if (SecurityMode.None == mCurrentSecuritySelection) {
Jim Miller7751ff62014-01-14 18:57:03 -0800321 SecurityMode securityMode = mSecurityModel.getSecurityMode();
Jim Miller7751ff62014-01-14 18:57:03 -0800322 if (SecurityMode.None == securityMode) {
323 finish = true; // no security required
324 } else {
325 showSecurityScreen(securityMode); // switch to the alternate security view
326 }
327 } else if (authenticated) {
328 switch (mCurrentSecuritySelection) {
329 case Pattern:
330 case Password:
331 case PIN:
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700332 strongAuth = true;
Jim Miller7751ff62014-01-14 18:57:03 -0800333 finish = true;
334 break;
335
336 case SimPin:
337 case SimPuk:
338 // Shortcut for SIM PIN/PUK to go to directly to user's security screen or home
339 SecurityMode securityMode = mSecurityModel.getSecurityMode();
Selim Cinek245273e2015-06-24 13:17:56 -0400340 if (securityMode != SecurityMode.None
341 || !mLockPatternUtils.isLockScreenDisabled(
342 KeyguardUpdateMonitor.getCurrentUser())) {
Jim Miller7751ff62014-01-14 18:57:03 -0800343 showSecurityScreen(securityMode);
344 } else {
345 finish = true;
346 }
347 break;
348
349 default:
350 Log.v(TAG, "Bad security screen " + mCurrentSecuritySelection + ", fail safe");
351 showPrimarySecurityScreen(false);
352 break;
353 }
Jim Miller7751ff62014-01-14 18:57:03 -0800354 }
355 if (finish) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700356 mSecurityCallback.finish(strongAuth);
Jim Miller7751ff62014-01-14 18:57:03 -0800357 }
358 return finish;
359 }
360
361 /**
362 * Switches to the given security view unless it's already being shown, in which case
363 * this is a no-op.
364 *
365 * @param securityMode
366 */
367 private void showSecurityScreen(SecurityMode securityMode) {
368 if (DEBUG) Log.d(TAG, "showSecurityScreen(" + securityMode + ")");
369
370 if (securityMode == mCurrentSecuritySelection) return;
371
372 KeyguardSecurityView oldView = getSecurityView(mCurrentSecuritySelection);
373 KeyguardSecurityView newView = getSecurityView(securityMode);
374
375 // Emulate Activity life cycle
376 if (oldView != null) {
377 oldView.onPause();
378 oldView.setKeyguardCallback(mNullCallback); // ignore requests from old view
379 }
Jorim Jaggif4797922014-08-04 22:49:41 +0200380 if (securityMode != SecurityMode.None) {
381 newView.onResume(KeyguardSecurityView.VIEW_REVEALED);
382 newView.setKeyguardCallback(mCallback);
383 }
Jim Miller7751ff62014-01-14 18:57:03 -0800384
385 // Find and show this child.
386 final int childCount = mSecurityViewFlipper.getChildCount();
387
388 final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
389 for (int i = 0; i < childCount; i++) {
390 if (mSecurityViewFlipper.getChildAt(i).getId() == securityViewIdForMode) {
391 mSecurityViewFlipper.setDisplayedChild(i);
392 break;
393 }
394 }
395
396 mCurrentSecuritySelection = securityMode;
Jorim Jaggif4797922014-08-04 22:49:41 +0200397 mSecurityCallback.onSecurityModeChanged(securityMode,
398 securityMode != SecurityMode.None && newView.needsInput());
Jim Miller7751ff62014-01-14 18:57:03 -0800399 }
400
401 private KeyguardSecurityViewFlipper getFlipper() {
Chris Wren052999f2012-11-02 14:36:56 -0400402 for (int i = 0; i < getChildCount(); i++) {
403 View child = getChildAt(i);
404 if (child instanceof KeyguardSecurityViewFlipper) {
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500405 return (KeyguardSecurityViewFlipper) child;
Chris Wren052999f2012-11-02 14:36:56 -0400406 }
407 }
408 return null;
409 }
410
Jim Miller7751ff62014-01-14 18:57:03 -0800411 private KeyguardSecurityCallback mCallback = new KeyguardSecurityCallback() {
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200412 public void userActivity() {
Jim Miller7751ff62014-01-14 18:57:03 -0800413 if (mSecurityCallback != null) {
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200414 mSecurityCallback.userActivity();
Jim Miller7751ff62014-01-14 18:57:03 -0800415 }
416 }
417
418 public void dismiss(boolean authenticated) {
419 mSecurityCallback.dismiss(authenticated);
420 }
421
422 public boolean isVerifyUnlockOnly() {
423 return mIsVerifyUnlockOnly;
424 }
425
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800426 public void reportUnlockAttempt(int userId, boolean success, int timeoutMs) {
Jim Miller7751ff62014-01-14 18:57:03 -0800427 KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
428 if (success) {
429 monitor.clearFailedUnlockAttempts();
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800430 mLockPatternUtils.reportSuccessfulPasswordAttempt(userId);
Jim Miller7751ff62014-01-14 18:57:03 -0800431 } else {
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800432 KeyguardSecurityContainer.this.reportFailedUnlockAttempt(userId, timeoutMs);
Jim Miller7751ff62014-01-14 18:57:03 -0800433 }
434 }
435
Andrew Lee72b46d42015-01-30 13:23:21 -0800436 public void reset() {
437 mSecurityCallback.reset();
438 }
Jim Miller7751ff62014-01-14 18:57:03 -0800439 };
440
441 // The following is used to ignore callbacks from SecurityViews that are no longer current
442 // (e.g. face unlock). This avoids unwanted asynchronous events from messing with the
443 // state for the current security method.
444 private KeyguardSecurityCallback mNullCallback = new KeyguardSecurityCallback() {
445 @Override
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200446 public void userActivity() { }
Jim Miller7751ff62014-01-14 18:57:03 -0800447 @Override
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800448 public void reportUnlockAttempt(int userId, boolean success, int timeoutMs) { }
Jim Miller7751ff62014-01-14 18:57:03 -0800449 @Override
450 public boolean isVerifyUnlockOnly() { return false; }
451 @Override
452 public void dismiss(boolean securityVerified) { }
Andrew Lee72b46d42015-01-30 13:23:21 -0800453 @Override
454 public void reset() {}
Jim Miller7751ff62014-01-14 18:57:03 -0800455 };
456
457 private int getSecurityViewIdForMode(SecurityMode securityMode) {
458 switch (securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800459 case Pattern: return R.id.keyguard_pattern_view;
460 case PIN: return R.id.keyguard_pin_view;
461 case Password: return R.id.keyguard_password_view;
Jim Miller7751ff62014-01-14 18:57:03 -0800462 case SimPin: return R.id.keyguard_sim_pin_view;
463 case SimPuk: return R.id.keyguard_sim_puk_view;
464 }
465 return 0;
466 }
467
Evan Roskyeff74b42016-03-25 12:57:14 -0700468 protected int getLayoutIdFor(SecurityMode securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800469 switch (securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800470 case Pattern: return R.layout.keyguard_pattern_view;
471 case PIN: return R.layout.keyguard_pin_view;
472 case Password: return R.layout.keyguard_password_view;
Jim Miller7751ff62014-01-14 18:57:03 -0800473 case SimPin: return R.layout.keyguard_sim_pin_view;
474 case SimPuk: return R.layout.keyguard_sim_puk_view;
475 default:
476 return 0;
477 }
478 }
479
480 public SecurityMode getSecurityMode() {
481 return mSecurityModel.getSecurityMode();
482 }
483
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100484 public SecurityMode getCurrentSecurityMode() {
485 return mCurrentSecuritySelection;
486 }
487
Jim Miller7751ff62014-01-14 18:57:03 -0800488 public void verifyUnlock() {
489 mIsVerifyUnlockOnly = true;
490 showSecurityScreen(getSecurityMode());
491 }
492
493 public SecurityMode getCurrentSecuritySelection() {
494 return mCurrentSecuritySelection;
495 }
496
497 public void dismiss(boolean authenticated) {
498 mCallback.dismiss(authenticated);
499 }
500
501 public boolean needsInput() {
502 return mSecurityViewFlipper.needsInput();
503 }
504
505 @Override
506 public void setKeyguardCallback(KeyguardSecurityCallback callback) {
507 mSecurityViewFlipper.setKeyguardCallback(callback);
508 }
509
510 @Override
511 public void reset() {
512 mSecurityViewFlipper.reset();
513 }
514
515 @Override
516 public KeyguardSecurityCallback getCallback() {
517 return mSecurityViewFlipper.getCallback();
518 }
519
520 @Override
Selim Cinek3122fa82015-06-18 01:38:59 -0700521 public void showPromptReason(int reason) {
522 if (mCurrentSecuritySelection != SecurityMode.None) {
Adrian Roos569edb82016-03-03 15:39:03 -0800523 if (reason != PROMPT_REASON_NONE) {
524 Log.i(TAG, "Strong auth required, reason: " + reason);
525 }
Selim Cinek3122fa82015-06-18 01:38:59 -0700526 getSecurityView(mCurrentSecuritySelection).showPromptReason(reason);
527 }
528 }
529
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700530
531 public void showMessage(String message, int color) {
532 if (mCurrentSecuritySelection != SecurityMode.None) {
533 getSecurityView(mCurrentSecuritySelection).showMessage(message, color);
534 }
535 }
536
Selim Cinek3122fa82015-06-18 01:38:59 -0700537 @Override
Jim Miller7751ff62014-01-14 18:57:03 -0800538 public void showUsabilityHint() {
539 mSecurityViewFlipper.showUsabilityHint();
540 }
541
Jim Miller838906b2012-10-19 18:41:25 -0700542}
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500543