1 /* 2 * Copyright (C) 2014 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.keyguard; 18 19 import static com.android.keyguard.KeyguardSecurityView.PROMPT_REASON_DEVICE_ADMIN; 20 import static com.android.keyguard.KeyguardSecurityView.PROMPT_REASON_NONE; 21 import static com.android.keyguard.KeyguardSecurityView.PROMPT_REASON_PREPARE_FOR_UPDATE; 22 import static com.android.keyguard.KeyguardSecurityView.PROMPT_REASON_RESTART; 23 import static com.android.keyguard.KeyguardSecurityView.PROMPT_REASON_TIMEOUT; 24 import static com.android.keyguard.KeyguardSecurityView.PROMPT_REASON_USER_REQUEST; 25 26 import android.content.Context; 27 import android.graphics.Rect; 28 import android.util.AttributeSet; 29 import android.view.KeyEvent; 30 import android.view.View; 31 32 import com.android.internal.widget.LockscreenCredential; 33 import com.android.systemui.R; 34 35 /** 36 * A Pin based Keyguard input view 37 */ 38 public abstract class KeyguardPinBasedInputView extends KeyguardAbsKeyInputView { 39 40 protected PasswordTextView mPasswordEntry; 41 private NumPadButton mOkButton; 42 private NumPadButton mDeleteButton; 43 private NumPadKey[] mButtons = new NumPadKey[10]; 44 KeyguardPinBasedInputView(Context context)45 public KeyguardPinBasedInputView(Context context) { 46 this(context, null); 47 } 48 KeyguardPinBasedInputView(Context context, AttributeSet attrs)49 public KeyguardPinBasedInputView(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 } 52 53 @Override onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)54 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 55 // send focus to the password field 56 return mPasswordEntry.requestFocus(direction, previouslyFocusedRect); 57 } 58 59 @Override resetState()60 protected void resetState() { 61 } 62 63 @Override setPasswordEntryEnabled(boolean enabled)64 protected void setPasswordEntryEnabled(boolean enabled) { 65 mPasswordEntry.setEnabled(enabled); 66 mOkButton.setEnabled(enabled); 67 if (enabled && !mPasswordEntry.hasFocus()) { 68 mPasswordEntry.requestFocus(); 69 } 70 } 71 72 @Override setPasswordEntryInputEnabled(boolean enabled)73 protected void setPasswordEntryInputEnabled(boolean enabled) { 74 mPasswordEntry.setEnabled(enabled); 75 mOkButton.setEnabled(enabled); 76 if (enabled && !mPasswordEntry.hasFocus()) { 77 mPasswordEntry.requestFocus(); 78 } 79 } 80 81 @Override onKeyDown(int keyCode, KeyEvent event)82 public boolean onKeyDown(int keyCode, KeyEvent event) { 83 if (KeyEvent.isConfirmKey(keyCode)) { 84 mOkButton.performClick(); 85 return true; 86 } else if (keyCode == KeyEvent.KEYCODE_DEL) { 87 mDeleteButton.performClick(); 88 return true; 89 } 90 if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) { 91 int number = keyCode - KeyEvent.KEYCODE_0; 92 performNumberClick(number); 93 return true; 94 } 95 if (keyCode >= KeyEvent.KEYCODE_NUMPAD_0 && keyCode <= KeyEvent.KEYCODE_NUMPAD_9) { 96 int number = keyCode - KeyEvent.KEYCODE_NUMPAD_0; 97 performNumberClick(number); 98 return true; 99 } 100 return super.onKeyDown(keyCode, event); 101 } 102 103 @Override getPromptReasonStringRes(int reason)104 protected int getPromptReasonStringRes(int reason) { 105 switch (reason) { 106 case PROMPT_REASON_RESTART: 107 return R.string.kg_prompt_reason_restart_pin; 108 case PROMPT_REASON_TIMEOUT: 109 return R.string.kg_prompt_reason_timeout_pin; 110 case PROMPT_REASON_DEVICE_ADMIN: 111 return R.string.kg_prompt_reason_device_admin; 112 case PROMPT_REASON_USER_REQUEST: 113 return R.string.kg_prompt_reason_user_request; 114 case PROMPT_REASON_PREPARE_FOR_UPDATE: 115 return R.string.kg_prompt_reason_timeout_pin; 116 case PROMPT_REASON_NONE: 117 return 0; 118 default: 119 return R.string.kg_prompt_reason_timeout_pin; 120 } 121 } 122 performNumberClick(int number)123 private void performNumberClick(int number) { 124 if (number >= 0 && number <= 9) { 125 mButtons[number].performClick(); 126 } 127 } 128 129 @Override resetPasswordText(boolean animate, boolean announce)130 protected void resetPasswordText(boolean animate, boolean announce) { 131 mPasswordEntry.reset(animate, announce); 132 } 133 134 @Override getEnteredCredential()135 protected LockscreenCredential getEnteredCredential() { 136 return LockscreenCredential.createPinOrNone(mPasswordEntry.getText()); 137 } 138 139 @Override onFinishInflate()140 protected void onFinishInflate() { 141 mPasswordEntry = findViewById(getPasswordTextViewId()); 142 143 // Set selected property on so the view can send accessibility events. 144 mPasswordEntry.setSelected(true); 145 146 mOkButton = findViewById(R.id.key_enter); 147 148 mDeleteButton = findViewById(R.id.delete_button); 149 mDeleteButton.setVisibility(View.VISIBLE); 150 151 mButtons[0] = findViewById(R.id.key0); 152 mButtons[1] = findViewById(R.id.key1); 153 mButtons[2] = findViewById(R.id.key2); 154 mButtons[3] = findViewById(R.id.key3); 155 mButtons[4] = findViewById(R.id.key4); 156 mButtons[5] = findViewById(R.id.key5); 157 mButtons[6] = findViewById(R.id.key6); 158 mButtons[7] = findViewById(R.id.key7); 159 mButtons[8] = findViewById(R.id.key8); 160 mButtons[9] = findViewById(R.id.key9); 161 162 mPasswordEntry.requestFocus(); 163 super.onFinishInflate(); 164 reloadColors(); 165 } 166 getButtons()167 NumPadKey[] getButtons() { 168 return mButtons; 169 } 170 171 /** 172 * Reload colors from resources. 173 **/ reloadColors()174 public void reloadColors() { 175 for (NumPadKey key : mButtons) { 176 key.reloadColors(); 177 } 178 mPasswordEntry.reloadColors(); 179 mDeleteButton.reloadColors(); 180 mOkButton.reloadColors(); 181 } 182 183 @Override getTitle()184 public CharSequence getTitle() { 185 return getContext().getString( 186 com.android.internal.R.string.keyguard_accessibility_pin_unlock); 187 } 188 } 189