2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.keyguard;
19 import android.content.Context;
20 import android.graphics.Rect;
21 import android.util.AttributeSet;
22 import android.view.KeyEvent;
23 import android.view.MotionEvent;
24 import android.view.View;
27 * A Pin based Keyguard input view
29 public abstract class KeyguardPinBasedInputView extends KeyguardAbsKeyInputView
30 implements View.OnKeyListener, View.OnTouchListener {
32 protected PasswordTextView mPasswordEntry;
33 private View mOkButton;
34 private View mDeleteButton;
35 private View mButton0;
36 private View mButton1;
37 private View mButton2;
38 private View mButton3;
39 private View mButton4;
40 private View mButton5;
41 private View mButton6;
42 private View mButton7;
43 private View mButton8;
44 private View mButton9;
46 public KeyguardPinBasedInputView(Context context) {
50 public KeyguardPinBasedInputView(Context context, AttributeSet attrs) {
51 super(context, attrs);
56 mPasswordEntry.requestFocus();
61 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
62 // send focus to the password field
63 return mPasswordEntry.requestFocus(direction, previouslyFocusedRect);
67 protected void resetState() {
68 setPasswordEntryEnabled(true);
72 protected void setPasswordEntryEnabled(boolean enabled) {
73 mPasswordEntry.setEnabled(enabled);
74 mOkButton.setEnabled(enabled);
78 protected void setPasswordEntryInputEnabled(boolean enabled) {
79 mPasswordEntry.setEnabled(enabled);
80 mOkButton.setEnabled(enabled);
84 public boolean onKeyDown(int keyCode, KeyEvent event) {
85 if (KeyEvent.isConfirmKey(keyCode)) {
86 performClick(mOkButton);
88 } else if (keyCode == KeyEvent.KEYCODE_DEL) {
89 performClick(mDeleteButton);
92 if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
93 int number = keyCode - KeyEvent.KEYCODE_0;
94 performNumberClick(number);
97 if (keyCode >= KeyEvent.KEYCODE_NUMPAD_0 && keyCode <= KeyEvent.KEYCODE_NUMPAD_9) {
98 int number = keyCode - KeyEvent.KEYCODE_NUMPAD_0;
99 performNumberClick(number);
102 return super.onKeyDown(keyCode, event);
106 protected int getPromtReasonStringRes(int reason) {
108 case PROMPT_REASON_RESTART:
109 return R.string.kg_prompt_reason_restart_pin;
110 case PROMPT_REASON_TIMEOUT:
111 return R.string.kg_prompt_reason_timeout_pin;
112 case PROMPT_REASON_DEVICE_ADMIN:
113 return R.string.kg_prompt_reason_device_admin;
114 case PROMPT_REASON_USER_REQUEST:
115 return R.string.kg_prompt_reason_user_request;
116 case PROMPT_REASON_NONE:
119 return R.string.kg_prompt_reason_timeout_pin;
123 private void performClick(View view) {
127 private void performNumberClick(int number) {
130 performClick(mButton0);
133 performClick(mButton1);
136 performClick(mButton2);
139 performClick(mButton3);
142 performClick(mButton4);
145 performClick(mButton5);
148 performClick(mButton6);
151 performClick(mButton7);
154 performClick(mButton8);
157 performClick(mButton9);
163 protected void resetPasswordText(boolean animate, boolean announce) {
164 mPasswordEntry.reset(animate, announce);
168 protected String getPasswordText() {
169 return mPasswordEntry.getText();
173 protected void onFinishInflate() {
174 mPasswordEntry = (PasswordTextView) findViewById(getPasswordTextViewId());
175 mPasswordEntry.setOnKeyListener(this);
177 // Set selected property on so the view can send accessibility events.
178 mPasswordEntry.setSelected(true);
180 mPasswordEntry.setUserActivityListener(new PasswordTextView.UserActivityListener() {
182 public void onUserActivity() {
187 mOkButton = findViewById(R.id.key_enter);
188 if (mOkButton != null) {
189 mOkButton.setOnTouchListener(this);
190 mOkButton.setOnClickListener(new View.OnClickListener() {
192 public void onClick(View v) {
193 if (mPasswordEntry.isEnabled()) {
194 verifyPasswordAndUnlock();
198 mOkButton.setOnHoverListener(new LiftToActivateListener(getContext()));
201 mDeleteButton = findViewById(R.id.delete_button);
202 mDeleteButton.setVisibility(View.VISIBLE);
203 mDeleteButton.setOnTouchListener(this);
204 mDeleteButton.setOnClickListener(new OnClickListener() {
206 public void onClick(View v) {
207 // check for time-based lockouts
208 if (mPasswordEntry.isEnabled()) {
209 mPasswordEntry.deleteLastChar();
213 mDeleteButton.setOnLongClickListener(new View.OnLongClickListener() {
215 public boolean onLongClick(View v) {
216 // check for time-based lockouts
217 if (mPasswordEntry.isEnabled()) {
218 resetPasswordText(true /* animate */, true /* announce */);
225 mButton0 = findViewById(R.id.key0);
226 mButton1 = findViewById(R.id.key1);
227 mButton2 = findViewById(R.id.key2);
228 mButton3 = findViewById(R.id.key3);
229 mButton4 = findViewById(R.id.key4);
230 mButton5 = findViewById(R.id.key5);
231 mButton6 = findViewById(R.id.key6);
232 mButton7 = findViewById(R.id.key7);
233 mButton8 = findViewById(R.id.key8);
234 mButton9 = findViewById(R.id.key9);
236 mPasswordEntry.requestFocus();
237 super.onFinishInflate();
241 public boolean onTouch(View v, MotionEvent event) {
242 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
249 public boolean onKey(View v, int keyCode, KeyEvent event) {
250 if (event.getAction() == KeyEvent.ACTION_DOWN) {
251 return onKeyDown(keyCode, event);