1 /*
2  * Copyright (C) 2023 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 
18 package com.android.systemui.biometrics.ui.binder
19 
20 import android.view.KeyEvent
21 import android.widget.ImeAwareEditText
22 import com.android.internal.widget.LockscreenCredential
23 import com.android.systemui.res.R
24 import com.android.systemui.biometrics.ui.CredentialPasswordView
25 import com.android.systemui.biometrics.ui.IPinPad
26 import com.android.systemui.biometrics.ui.PinPadClickListener
27 
28 /** Binder for IPinPad */
29 object PinPadViewBinder {
30     /** Implements a PinPadClickListener inside a pin pad */
31     @JvmStatic
bindnull32     fun bind(view: IPinPad, credentialPasswordView: CredentialPasswordView) {
33         val passwordField: ImeAwareEditText =
34             credentialPasswordView.requireViewById(R.id.lockPassword)
35         view.setPinPadClickListener(
36             object : PinPadClickListener {
37 
38                 override fun onDigitKeyClick(digit: String?) {
39                     passwordField.append(digit)
40                 }
41 
42                 override fun onBackspaceClick() {
43                     val pin = LockscreenCredential.createPinOrNone(passwordField.text)
44                     if (pin.size() > 0) {
45                         passwordField.text.delete(
46                             passwordField.selectionEnd - 1,
47                             passwordField.selectionEnd
48                         )
49                     }
50                     pin.zeroize()
51                 }
52 
53                 override fun onEnterKeyClick() {
54                     passwordField.dispatchKeyEvent(
55                         KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0)
56                     )
57                     passwordField.dispatchKeyEvent(
58                         KeyEvent(0, 0, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0)
59                     )
60                 }
61             }
62         )
63     }
64 }
65