Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.os.Debug; |
| 23 | import android.os.PowerManager; |
| 24 | import android.os.SystemClock; |
Selim Cinek | c199ef3 | 2014-12-09 18:49:42 +0100 | [diff] [blame] | 25 | import android.os.UserHandle; |
| 26 | import android.provider.Settings; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 28 | import android.view.HapticFeedbackConstants; |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 29 | import android.view.KeyEvent; |
| 30 | import android.view.LayoutInflater; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 31 | import android.view.View; |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 32 | import android.view.ViewGroup; |
| 33 | import android.view.accessibility.AccessibilityEvent; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 34 | import android.widget.TextView; |
| 35 | |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 36 | import com.android.internal.widget.LockPatternUtils; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 37 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 38 | public class NumPadKey extends ViewGroup { |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 39 | // list of "ABC", etc per digit, starting with '0' |
| 40 | static String sKlondike[]; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 41 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 42 | private int mDigit = -1; |
| 43 | private int mTextViewResId; |
| 44 | private PasswordTextView mTextView; |
| 45 | private TextView mDigitText; |
| 46 | private TextView mKlondikeText; |
| 47 | private boolean mEnableHaptics; |
| 48 | private PowerManager mPM; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 49 | |
| 50 | private View.OnClickListener mListener = new View.OnClickListener() { |
| 51 | @Override |
| 52 | public void onClick(View thisView) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 53 | if (mTextView == null && mTextViewResId > 0) { |
| 54 | final View v = NumPadKey.this.getRootView().findViewById(mTextViewResId); |
| 55 | if (v != null && v instanceof PasswordTextView) { |
| 56 | mTextView = (PasswordTextView) v; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 57 | } |
| 58 | } |
Daniel Sandler | 6a64ac5 | 2012-11-01 11:58:13 -0400 | [diff] [blame] | 59 | if (mTextView != null && mTextView.isEnabled()) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 60 | mTextView.append(Character.forDigit(mDigit, 10)); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 61 | } |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 62 | userActivity(); |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 63 | doHapticKeyClick(); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 64 | } |
| 65 | }; |
| 66 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 67 | public void userActivity() { |
| 68 | mPM.userActivity(SystemClock.uptimeMillis(), false); |
| 69 | } |
| 70 | |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 71 | public NumPadKey(Context context) { |
| 72 | this(context, null); |
| 73 | } |
| 74 | |
| 75 | public NumPadKey(Context context, AttributeSet attrs) { |
| 76 | this(context, attrs, 0); |
| 77 | } |
| 78 | |
| 79 | public NumPadKey(Context context, AttributeSet attrs, int defStyle) { |
| 80 | super(context, attrs, defStyle); |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 81 | setFocusable(true); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 82 | |
| 83 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumPadKey); |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 84 | |
| 85 | try { |
| 86 | mDigit = a.getInt(R.styleable.NumPadKey_digit, mDigit); |
| 87 | mTextViewResId = a.getResourceId(R.styleable.NumPadKey_textView, 0); |
| 88 | } finally { |
| 89 | a.recycle(); |
| 90 | } |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 91 | |
| 92 | setOnClickListener(mListener); |
alanv | c7375af | 2012-10-30 17:07:37 -0700 | [diff] [blame] | 93 | setOnHoverListener(new LiftToActivateListener(context)); |
alanv | 0cbbc57 | 2012-11-02 11:56:27 -0700 | [diff] [blame] | 94 | setAccessibilityDelegate(new ObscureSpeechDelegate(context)); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 95 | |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 96 | mEnableHaptics = new LockPatternUtils(context).isTactileFeedbackEnabled(); |
| 97 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 98 | mPM = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 99 | LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( |
| 100 | Context.LAYOUT_INFLATER_SERVICE); |
| 101 | inflater.inflate(R.layout.keyguard_num_pad_key, this, true); |
| 102 | |
| 103 | mDigitText = (TextView) findViewById(R.id.digit_text); |
| 104 | mDigitText.setText(Integer.toString(mDigit)); |
| 105 | mKlondikeText = (TextView) findViewById(R.id.klondike_text); |
| 106 | |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 107 | if (mDigit >= 0) { |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 108 | if (sKlondike == null) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 109 | sKlondike = getResources().getStringArray(R.array.lockscreen_num_pad_klondike); |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 110 | } |
| 111 | if (sKlondike != null && sKlondike.length > mDigit) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 112 | String klondike = sKlondike[mDigit]; |
| 113 | final int len = klondike.length(); |
| 114 | if (len > 0) { |
| 115 | mKlondikeText.setText(klondike); |
| 116 | } else { |
| 117 | mKlondikeText.setVisibility(View.INVISIBLE); |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 118 | } |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 119 | } |
| 120 | } |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 121 | |
| 122 | setBackground(mContext.getDrawable(R.drawable.ripple_drawable)); |
Selim Cinek | c199ef3 | 2014-12-09 18:49:42 +0100 | [diff] [blame] | 123 | updateContentDescription(); |
| 124 | } |
| 125 | |
| 126 | public void updateContentDescription() { |
| 127 | if (shouldSpeakPasswordsForAccessibility()) { |
| 128 | setContentDescription( |
| 129 | mDigitText.getText().toString() + mKlondikeText.getText().toString()); |
| 130 | } else { |
| 131 | setContentDescription(getContext().getString( |
| 132 | com.android.internal.R.string.keyboard_password_character_no_headset)); |
| 133 | } |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 134 | } |
| 135 | |
alanv | 0cbbc57 | 2012-11-02 11:56:27 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void onDetachedFromWindow() { |
| 138 | super.onDetachedFromWindow(); |
| 139 | |
| 140 | // Reset the "announced headset" flag when detached. |
| 141 | ObscureSpeechDelegate.sAnnouncedHeadset = false; |
| 142 | } |
| 143 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 144 | @Override |
| 145 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 146 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 147 | measureChildren(widthMeasureSpec, heightMeasureSpec); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 150 | @Override |
| 151 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 152 | int digitHeight = mDigitText.getMeasuredHeight(); |
| 153 | int klondikeHeight = mKlondikeText.getMeasuredHeight(); |
| 154 | int totalHeight = digitHeight + klondikeHeight; |
| 155 | int top = getHeight() / 2 - totalHeight / 2; |
| 156 | int centerX = getWidth() / 2; |
| 157 | int left = centerX - mDigitText.getMeasuredWidth() / 2; |
| 158 | int bottom = top + digitHeight; |
| 159 | mDigitText.layout(left, top, left + mDigitText.getMeasuredWidth(), bottom); |
| 160 | top = (int) (bottom - klondikeHeight * 0.35f); |
| 161 | bottom = top + klondikeHeight; |
| 162 | |
| 163 | left = centerX - mKlondikeText.getMeasuredWidth() / 2; |
| 164 | mKlondikeText.layout(left, top, left + mKlondikeText.getMeasuredWidth(), bottom); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 165 | } |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 166 | |
Selim Cinek | c199ef3 | 2014-12-09 18:49:42 +0100 | [diff] [blame] | 167 | /** |
| 168 | * @return true if the user has explicitly allowed accessibility services |
| 169 | * to speak passwords. |
| 170 | */ |
| 171 | private boolean shouldSpeakPasswordsForAccessibility() { |
| 172 | return (Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 173 | Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0, UserHandle.USER_CURRENT) == 1); |
| 174 | } |
| 175 | |
Selim Cinek | 6380482 | 2014-09-10 16:39:01 +0200 | [diff] [blame] | 176 | @Override |
| 177 | public boolean hasOverlappingRendering() { |
| 178 | return false; |
| 179 | } |
| 180 | |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 181 | // Cause a VIRTUAL_KEY vibration |
| 182 | public void doHapticKeyClick() { |
| 183 | if (mEnableHaptics) { |
| 184 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, |
| 185 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING |
| 186 | | HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); |
| 187 | } |
| 188 | } |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 189 | } |