KeyguardPINView.java revision 5ecd81154fa039961f65bb4e36d18ac555b0d1d6
1/*
2 * Copyright (C) 2012 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.keyguard;
18
19import android.content.Context;
20import android.text.Editable;
21import android.text.InputType;
22import android.text.TextWatcher;
23import android.text.method.DigitsKeyListener;
24import android.util.AttributeSet;
25import android.view.View;
26import android.widget.TextView.OnEditorActionListener;
27
28/**
29 * Displays a PIN pad for unlocking.
30 */
31public class KeyguardPINView extends KeyguardAbsKeyInputView
32        implements KeyguardSecurityView, OnEditorActionListener, TextWatcher {
33
34    public KeyguardPINView(Context context) {
35        this(context, null);
36    }
37
38    public KeyguardPINView(Context context, AttributeSet attrs) {
39        super(context, attrs);
40    }
41
42    protected void resetState() {
43        if (KeyguardUpdateMonitor.getInstance(mContext).getMaxBiometricUnlockAttemptsReached()) {
44            mSecurityMessageDisplay.setMessage(R.string.faceunlock_multiple_failures, true);
45        } else {
46            mSecurityMessageDisplay.setMessage(R.string.kg_pin_instructions, false);
47        }
48        mPasswordEntry.setEnabled(true);
49    }
50
51    @Override
52    protected int getPasswordTextViewId() {
53        return R.id.pinEntry;
54    }
55
56    @Override
57    protected void onFinishInflate() {
58        super.onFinishInflate();
59
60        final View ok = findViewById(R.id.key_enter);
61        if (ok != null) {
62            ok.setOnClickListener(new View.OnClickListener() {
63                @Override
64                public void onClick(View v) {
65                    doHapticKeyClick();
66                    if (mPasswordEntry.isEnabled()) {
67                        verifyPasswordAndUnlock();
68                    }
69                }
70            });
71            ok.setOnHoverListener(new LiftToActivateListener(getContext()));
72        }
73
74        // The delete button is of the PIN keyboard itself in some (e.g. tablet) layouts,
75        // not a separate view
76        View pinDelete = findViewById(R.id.delete_button);
77        if (pinDelete != null) {
78            pinDelete.setVisibility(View.VISIBLE);
79            pinDelete.setOnClickListener(new OnClickListener() {
80                public void onClick(View v) {
81                    // check for time-based lockouts
82                    if (mPasswordEntry.isEnabled()) {
83                        CharSequence str = mPasswordEntry.getText();
84                        if (str.length() > 0) {
85                            mPasswordEntry.setText(str.subSequence(0, str.length()-1));
86                        }
87                    }
88                    doHapticKeyClick();
89                }
90            });
91            pinDelete.setOnLongClickListener(new View.OnLongClickListener() {
92                public boolean onLongClick(View v) {
93                    // check for time-based lockouts
94                    if (mPasswordEntry.isEnabled()) {
95                        mPasswordEntry.setText("");
96                    }
97                    doHapticKeyClick();
98                    return true;
99                }
100            });
101        }
102
103        mPasswordEntry.setKeyListener(DigitsKeyListener.getInstance());
104        mPasswordEntry.setInputType(InputType.TYPE_CLASS_NUMBER
105                | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
106
107        mPasswordEntry.requestFocus();
108    }
109
110    @Override
111    public void showUsabilityHint() {
112    }
113
114    @Override
115    public int getWrongPasswordStringId() {
116        return R.string.kg_wrong_pin;
117    }
118}
119