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