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.os.PowerManager; |
| 22 | import android.os.SystemClock; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 24 | import android.view.HapticFeedbackConstants; |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
Selim Cinek | 8b9f948 | 2016-07-19 15:18:30 -0700 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 27 | import android.view.View; |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 29 | import android.widget.TextView; |
| 30 | |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 31 | import com.android.internal.widget.LockPatternUtils; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 32 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 33 | public class NumPadKey extends ViewGroup { |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 34 | // list of "ABC", etc per digit, starting with '0' |
| 35 | static String sKlondike[]; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 36 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 37 | private int mDigit = -1; |
| 38 | private int mTextViewResId; |
| 39 | private PasswordTextView mTextView; |
| 40 | private TextView mDigitText; |
| 41 | private TextView mKlondikeText; |
| 42 | private boolean mEnableHaptics; |
| 43 | private PowerManager mPM; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 44 | |
| 45 | private View.OnClickListener mListener = new View.OnClickListener() { |
| 46 | @Override |
| 47 | public void onClick(View thisView) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 48 | if (mTextView == null && mTextViewResId > 0) { |
| 49 | final View v = NumPadKey.this.getRootView().findViewById(mTextViewResId); |
| 50 | if (v != null && v instanceof PasswordTextView) { |
| 51 | mTextView = (PasswordTextView) v; |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 52 | } |
| 53 | } |
Daniel Sandler | 6a64ac5 | 2012-11-01 11:58:13 -0400 | [diff] [blame] | 54 | if (mTextView != null && mTextView.isEnabled()) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 55 | mTextView.append(Character.forDigit(mDigit, 10)); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 56 | } |
Lucas Dupin | e3c9ccc | 2018-10-23 14:44:04 -0700 | [diff] [blame] | 57 | userActivity(); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 61 | public void userActivity() { |
| 62 | mPM.userActivity(SystemClock.uptimeMillis(), false); |
| 63 | } |
| 64 | |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 65 | public NumPadKey(Context context) { |
| 66 | this(context, null); |
| 67 | } |
| 68 | |
| 69 | public NumPadKey(Context context, AttributeSet attrs) { |
| 70 | this(context, attrs, 0); |
| 71 | } |
| 72 | |
| 73 | public NumPadKey(Context context, AttributeSet attrs, int defStyle) { |
Evan Rosky | 9042750 | 2016-04-01 16:04:23 -0700 | [diff] [blame] | 74 | this(context, attrs, defStyle, R.layout.keyguard_num_pad_key); |
| 75 | } |
| 76 | |
| 77 | protected NumPadKey(Context context, AttributeSet attrs, int defStyle, int contentResource) { |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 78 | super(context, attrs, defStyle); |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 79 | setFocusable(true); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 80 | |
| 81 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumPadKey); |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 82 | |
| 83 | try { |
| 84 | mDigit = a.getInt(R.styleable.NumPadKey_digit, mDigit); |
| 85 | mTextViewResId = a.getResourceId(R.styleable.NumPadKey_textView, 0); |
| 86 | } finally { |
| 87 | a.recycle(); |
| 88 | } |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 89 | |
| 90 | setOnClickListener(mListener); |
alanv | c7375af | 2012-10-30 17:07:37 -0700 | [diff] [blame] | 91 | setOnHoverListener(new LiftToActivateListener(context)); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 92 | |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 93 | mEnableHaptics = new LockPatternUtils(context).isTactileFeedbackEnabled(); |
| 94 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 95 | mPM = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 96 | LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( |
| 97 | Context.LAYOUT_INFLATER_SERVICE); |
Evan Rosky | 9042750 | 2016-04-01 16:04:23 -0700 | [diff] [blame] | 98 | inflater.inflate(contentResource, this, true); |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 99 | |
| 100 | mDigitText = (TextView) findViewById(R.id.digit_text); |
| 101 | mDigitText.setText(Integer.toString(mDigit)); |
| 102 | mKlondikeText = (TextView) findViewById(R.id.klondike_text); |
| 103 | |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 104 | if (mDigit >= 0) { |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 105 | if (sKlondike == null) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 106 | sKlondike = getResources().getStringArray(R.array.lockscreen_num_pad_klondike); |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 107 | } |
| 108 | if (sKlondike != null && sKlondike.length > mDigit) { |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 109 | String klondike = sKlondike[mDigit]; |
| 110 | final int len = klondike.length(); |
| 111 | if (len > 0) { |
| 112 | mKlondikeText.setText(klondike); |
| 113 | } else { |
| 114 | mKlondikeText.setVisibility(View.INVISIBLE); |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 115 | } |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 116 | } |
| 117 | } |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 118 | |
Evan Rosky | 9042750 | 2016-04-01 16:04:23 -0700 | [diff] [blame] | 119 | a = context.obtainStyledAttributes(attrs, android.R.styleable.View); |
| 120 | if (!a.hasValueOrEmpty(android.R.styleable.View_background)) { |
Lucas Dupin | e3c9ccc | 2018-10-23 14:44:04 -0700 | [diff] [blame] | 121 | setBackground(mContext.getDrawable(R.drawable.ripple_drawable_pin)); |
Evan Rosky | 9042750 | 2016-04-01 16:04:23 -0700 | [diff] [blame] | 122 | } |
| 123 | a.recycle(); |
Adrian Roos | 42661cba | 2015-02-17 20:46:01 +0100 | [diff] [blame] | 124 | setContentDescription(mDigitText.getText().toString()); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 125 | } |
| 126 | |
alanv | 0cbbc57 | 2012-11-02 11:56:27 -0700 | [diff] [blame] | 127 | @Override |
Selim Cinek | 8b9f948 | 2016-07-19 15:18:30 -0700 | [diff] [blame] | 128 | public boolean onTouchEvent(MotionEvent event) { |
| 129 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 130 | doHapticKeyClick(); |
| 131 | } |
| 132 | return super.onTouchEvent(event); |
| 133 | } |
| 134 | |
| 135 | @Override |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 136 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 137 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 138 | measureChildren(widthMeasureSpec, heightMeasureSpec); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Selim Cinek | 4e8b9ed | 2014-06-20 16:37:04 -0700 | [diff] [blame] | 141 | @Override |
| 142 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 143 | int digitHeight = mDigitText.getMeasuredHeight(); |
| 144 | int klondikeHeight = mKlondikeText.getMeasuredHeight(); |
| 145 | int totalHeight = digitHeight + klondikeHeight; |
| 146 | int top = getHeight() / 2 - totalHeight / 2; |
| 147 | int centerX = getWidth() / 2; |
| 148 | int left = centerX - mDigitText.getMeasuredWidth() / 2; |
| 149 | int bottom = top + digitHeight; |
| 150 | mDigitText.layout(left, top, left + mDigitText.getMeasuredWidth(), bottom); |
| 151 | top = (int) (bottom - klondikeHeight * 0.35f); |
| 152 | bottom = top + klondikeHeight; |
| 153 | |
| 154 | left = centerX - mKlondikeText.getMeasuredWidth() / 2; |
| 155 | mKlondikeText.layout(left, top, left + mKlondikeText.getMeasuredWidth(), bottom); |
Daniel Sandler | 69bdee7 | 2012-10-23 16:45:50 -0400 | [diff] [blame] | 156 | } |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 157 | |
Selim Cinek | 6380482 | 2014-09-10 16:39:01 +0200 | [diff] [blame] | 158 | @Override |
| 159 | public boolean hasOverlappingRendering() { |
| 160 | return false; |
| 161 | } |
| 162 | |
Daniel Sandler | acb60fb | 2012-10-25 10:46:37 -0400 | [diff] [blame] | 163 | // Cause a VIRTUAL_KEY vibration |
| 164 | public void doHapticKeyClick() { |
| 165 | if (mEnableHaptics) { |
| 166 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, |
| 167 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING |
| 168 | | HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); |
| 169 | } |
| 170 | } |
Daniel Sandler | d569274 | 2012-10-24 00:21:32 -0400 | [diff] [blame] | 171 | } |