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