Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.biometrics; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.text.InputType; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.KeyEvent; |
| 23 | import android.view.inputmethod.EditorInfo; |
| 24 | import android.view.inputmethod.InputMethodManager; |
| 25 | import android.widget.EditText; |
| 26 | import android.widget.TextView; |
| 27 | |
| 28 | import com.android.internal.widget.LockPatternChecker; |
| 29 | import com.android.internal.widget.LockPatternUtils; |
| 30 | import com.android.systemui.R; |
| 31 | |
| 32 | /** |
| 33 | * Pin and Password UI |
| 34 | */ |
| 35 | public 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 Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 54 | 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 Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame^] | 69 | if (mCredentialType == Utils.CREDENTIAL_PIN) { |
| 70 | mPasswordField.setInputType( |
| 71 | InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD); |
| 72 | } |
| 73 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 74 | // 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() { |
| 99 | final byte[] password = LockPatternUtils.charSequenceToByteArray(mPasswordField.getText()); |
| 100 | if (password == null || password.length == 0) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | mPendingLockCheck = LockPatternChecker.checkPassword(mLockPatternUtils, |
| 105 | password, mUserId, this::onCredentialChecked); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void onCredentialChecked(boolean matched, int timeoutMs) { |
| 110 | super.onCredentialChecked(matched, timeoutMs); |
| 111 | |
| 112 | if (matched) { |
| 113 | mImm.hideSoftInputFromWindow(getWindowToken(), 0 /* flags */); |
| 114 | } else { |
| 115 | mPasswordField.setText(""); |
| 116 | } |
| 117 | } |
| 118 | } |