blob: bebaa4b688db1d78d258c52522904f38769bd0ca [file] [log] [blame]
Kevin Chyn8110ebb2019-10-02 11:16:31 -07001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.biometrics;
18
19import android.content.Context;
20import android.text.InputType;
21import android.util.AttributeSet;
22import android.view.KeyEvent;
23import android.view.inputmethod.EditorInfo;
24import android.view.inputmethod.InputMethodManager;
25import android.widget.EditText;
26import android.widget.TextView;
27
28import com.android.internal.widget.LockPatternChecker;
Rubin Xua58125d2019-09-06 20:11:48 +010029import com.android.internal.widget.LockscreenCredential;
Kevin Chyn8110ebb2019-10-02 11:16:31 -070030import com.android.systemui.R;
31
32/**
33 * Pin and Password UI
34 */
35public class AuthCredentialPasswordView extends AuthCredentialView
36 implements TextView.OnEditorActionListener {
37
38 private static final String TAG = "BiometricPrompt/AuthCredentialPasswordView";
39
40 private final InputMethodManager mImm;
41 private EditText mPasswordField;
42
43 public AuthCredentialPasswordView(Context context,
44 AttributeSet attrs) {
45 super(context, attrs);
46 mImm = mContext.getSystemService(InputMethodManager.class);
47 }
48
49 @Override
50 protected void onFinishInflate() {
51 super.onFinishInflate();
52 mPasswordField = findViewById(R.id.lockPassword);
53 mPasswordField.setOnEditorActionListener(this);
Kevin Chyn8110ebb2019-10-02 11:16:31 -070054 mPasswordField.setOnKeyListener((v, keyCode, event) -> {
55 if (keyCode != KeyEvent.KEYCODE_BACK) {
56 return false;
57 }
58 if (event.getAction() == KeyEvent.ACTION_UP) {
59 mContainerView.animateAway(AuthDialogCallback.DISMISSED_USER_CANCELED);
60 }
61 return true;
62 });
63 }
64
65 @Override
66 protected void onAttachedToWindow() {
67 super.onAttachedToWindow();
68
Kevin Chync70d6b82019-10-03 15:32:37 -070069 if (mCredentialType == Utils.CREDENTIAL_PIN) {
70 mPasswordField.setInputType(
71 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
72 }
73
Kevin Chyn8110ebb2019-10-02 11:16:31 -070074 // Wait a bit to focus the field so the focusable flag on the window is already set then.
75 post(() -> {
76 mPasswordField.requestFocus();
77 mImm.showSoftInput(mPasswordField, InputMethodManager.SHOW_IMPLICIT);
78 });
79 }
80
81 @Override
82 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
83 // Check if this was the result of hitting the enter key
84 final boolean isSoftImeEvent = event == null
85 && (actionId == EditorInfo.IME_NULL
86 || actionId == EditorInfo.IME_ACTION_DONE
87 || actionId == EditorInfo.IME_ACTION_NEXT);
88 final boolean isKeyboardEnterKey = event != null
89 && KeyEvent.isConfirmKey(event.getKeyCode())
90 && event.getAction() == KeyEvent.ACTION_DOWN;
91 if (isSoftImeEvent || isKeyboardEnterKey) {
92 checkPasswordAndUnlock();
93 return true;
94 }
95 return false;
96 }
97
98 private void checkPasswordAndUnlock() {
Rubin Xua58125d2019-09-06 20:11:48 +010099 try (LockscreenCredential password = mCredentialType == Utils.CREDENTIAL_PIN
100 ? LockscreenCredential.createPinOrNone(mPasswordField.getText())
101 : LockscreenCredential.createPasswordOrNone(mPasswordField.getText())) {
102 if (password.isNone()) {
103 return;
104 }
Kevin Chyn8110ebb2019-10-02 11:16:31 -0700105
Rubin Xua58125d2019-09-06 20:11:48 +0100106 mPendingLockCheck = LockPatternChecker.checkCredential(mLockPatternUtils,
107 password, mUserId, this::onCredentialChecked);
108 }
Kevin Chyn8110ebb2019-10-02 11:16:31 -0700109 }
110
111 @Override
112 protected void onCredentialChecked(boolean matched, int timeoutMs) {
113 super.onCredentialChecked(matched, timeoutMs);
114
115 if (matched) {
116 mImm.hideSoftInputFromWindow(getWindowToken(), 0 /* flags */);
117 } else {
118 mPasswordField.setText("");
119 }
120 }
121}