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 package com.android.keyguard;
17 
18 import static com.android.systemui.bouncer.shared.constants.KeyguardBouncerConstants.ColorId.NUM_PAD_KEY;
19 
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.content.res.TypedArray;
23 import android.graphics.drawable.Drawable;
24 import android.graphics.drawable.GradientDrawable;
25 import android.os.PowerManager;
26 import android.os.SystemClock;
27 import android.util.AttributeSet;
28 import android.view.HapticFeedbackConstants;
29 import android.view.LayoutInflater;
30 import android.view.MotionEvent;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.view.accessibility.AccessibilityNodeInfo;
34 import android.widget.TextView;
35 
36 import androidx.annotation.Nullable;
37 
38 import com.android.settingslib.Utils;
39 import com.android.systemui.res.R;
40 
41 /**
42  * Viewgroup for the bouncer numpad button, specifically for digits.
43  */
44 public class NumPadKey extends ViewGroup implements NumPadAnimationListener {
45     // list of "ABC", etc per digit, starting with '0'
46     static String sKlondike[];
47 
48     private final TextView mDigitText;
49     private final TextView mKlondikeText;
50     private final PowerManager mPM;
51 
52     private int mDigit = -1;
53     private int mTextViewResId;
54     private PasswordTextView mTextView;
55     private boolean mAnimationsEnabled = true;
56 
57     @Nullable
58     private NumPadAnimator mAnimator;
59     private int mOrientation;
60 
61     private View.OnClickListener mListener = new View.OnClickListener() {
62         @Override
63         public void onClick(View thisView) {
64             if (mTextView == null && mTextViewResId > 0) {
65                 final View v = NumPadKey.this.getRootView().findViewById(mTextViewResId);
66                 if (v != null && v instanceof PasswordTextView) {
67                     mTextView = (PasswordTextView) v;
68                 }
69             }
70             if (mTextView != null && mTextView.isEnabled()) {
71                 mTextView.append(Character.forDigit(mDigit, 10));
72             }
73             userActivity();
74         }
75     };
76 
userActivity()77     public void userActivity() {
78         mPM.userActivity(SystemClock.uptimeMillis(), false);
79     }
80 
NumPadKey(Context context)81     public NumPadKey(Context context) {
82         this(context, null);
83     }
84 
NumPadKey(Context context, AttributeSet attrs)85     public NumPadKey(Context context, AttributeSet attrs) {
86         this(context, attrs, R.attr.numPadKeyStyle);
87     }
88 
NumPadKey(Context context, AttributeSet attrs, int defStyle)89     public NumPadKey(Context context, AttributeSet attrs, int defStyle) {
90         this(context, attrs, defStyle, R.layout.keyguard_num_pad_key);
91     }
92 
NumPadKey(Context context, AttributeSet attrs, int defStyle, int contentResource)93     protected NumPadKey(Context context, AttributeSet attrs, int defStyle, int contentResource) {
94         super(context, attrs, defStyle);
95         setFocusable(true);
96 
97         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumPadKey, defStyle,
98                 contentResource);
99 
100         try {
101             mDigit = a.getInt(R.styleable.NumPadKey_digit, mDigit);
102             mTextViewResId = a.getResourceId(R.styleable.NumPadKey_textView, 0);
103         } finally {
104             a.recycle();
105         }
106 
107         setOnClickListener(mListener);
108 
109         mPM = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
110         LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
111                 Context.LAYOUT_INFLATER_SERVICE);
112         inflater.inflate(contentResource, this, true);
113 
114         mDigitText = (TextView) findViewById(R.id.digit_text);
115         mDigitText.setText(Integer.toString(mDigit));
116         mKlondikeText = (TextView) findViewById(R.id.klondike_text);
117 
118         if (mDigit >= 0) {
119             if (sKlondike == null) {
120                 sKlondike = getResources().getStringArray(R.array.lockscreen_num_pad_klondike);
121             }
122             if (sKlondike != null && sKlondike.length > mDigit) {
123                 String klondike = sKlondike[mDigit];
124                 final int len = klondike.length();
125                 if (len > 0) {
126                     mKlondikeText.setText(klondike);
127                 } else if (mKlondikeText.getVisibility() != View.GONE) {
128                     mKlondikeText.setVisibility(View.INVISIBLE);
129                 }
130             }
131         }
132 
133         setContentDescription(mDigitText.getText().toString());
134 
135         Drawable background = getBackground();
136         if (background instanceof GradientDrawable) {
137             mAnimator = new NumPadAnimator(context, background.mutate(),
138                     R.style.NumPadKey, mDigitText, null);
139         } else {
140             mAnimator = null;
141         }
142     }
143 
144     @Override
onConfigurationChanged(Configuration newConfig)145     protected void onConfigurationChanged(Configuration newConfig) {
146         mOrientation = newConfig.orientation;
147     }
148 
149     /**
150      * Reload colors from resources.
151      **/
reloadColors()152     public void reloadColors() {
153         int textColor = Utils.getColorAttr(getContext(), NUM_PAD_KEY)
154                 .getDefaultColor();
155         int klondikeColor = Utils.getColorAttr(getContext(), android.R.attr.textColorSecondary)
156                 .getDefaultColor();
157         mDigitText.setTextColor(textColor);
158         mKlondikeText.setTextColor(klondikeColor);
159 
160         if (mAnimator != null) mAnimator.reloadColors(getContext());
161     }
162 
163     @Override
onTouchEvent(MotionEvent event)164     public boolean onTouchEvent(MotionEvent event) {
165         switch(event.getActionMasked()) {
166             case MotionEvent.ACTION_DOWN:
167                 doHapticKeyClick();
168                 if (mAnimator != null && mAnimationsEnabled) mAnimator.expand();
169                 break;
170             case MotionEvent.ACTION_UP:
171             case MotionEvent.ACTION_CANCEL:
172                 if (mAnimator != null && mAnimationsEnabled) mAnimator.contract();
173                 break;
174         }
175         return super.onTouchEvent(event);
176     }
177 
178     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)179     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
180         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
181         measureChildren(widthMeasureSpec, heightMeasureSpec);
182 
183         // Set width/height to the same value to ensure a smooth circle for the bg, but shrink
184         // the height to match the old pin bouncer.
185         // This is only used for PIN/PUK; the main PIN pad now uses ConstraintLayout, which will
186         // force our width/height to conform to the ratio in the layout.
187         int width = getMeasuredWidth();
188 
189         boolean shortenHeight = mAnimator == null
190                 || mOrientation == Configuration.ORIENTATION_LANDSCAPE;
191         int height = shortenHeight ? (int) (width * .66f) : width;
192 
193         setMeasuredDimension(getMeasuredWidth(), height);
194     }
195 
196     @Override
onLayout(boolean changed, int l, int t, int r, int b)197     protected void onLayout(boolean changed, int l, int t, int r, int b) {
198         int digitHeight = mDigitText.getMeasuredHeight();
199         int klondikeHeight = mKlondikeText.getMeasuredHeight();
200         int totalHeight = digitHeight + klondikeHeight;
201         int top = getHeight() / 2 - totalHeight / 2;
202         int centerX = getWidth() / 2;
203         int left = centerX - mDigitText.getMeasuredWidth() / 2;
204         int bottom = top + digitHeight;
205         mDigitText.layout(left, top, left + mDigitText.getMeasuredWidth(), bottom);
206         top = (int) (bottom - klondikeHeight * 0.35f);
207         bottom = top + klondikeHeight;
208 
209         left = centerX - mKlondikeText.getMeasuredWidth() / 2;
210         mKlondikeText.layout(left, top, left + mKlondikeText.getMeasuredWidth(), bottom);
211 
212         int width = r - l;
213         int height = b - t;
214         if (mAnimator != null) mAnimator.onLayout(width, height);
215     }
216 
217     @Override
hasOverlappingRendering()218     public boolean hasOverlappingRendering() {
219         return false;
220     }
221 
222     // Cause a VIRTUAL_KEY vibration
doHapticKeyClick()223     public void doHapticKeyClick() {
224         performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY,
225                 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
226     }
227 
228     @Override
setProgress(float progress)229     public void setProgress(float progress) {
230         if (mAnimator != null) {
231             mAnimator.setProgress(progress);
232         }
233     }
234 
235     /**
236      * Controls the animation when a key is pressed
237      */
setAnimationEnabled(boolean enabled)238     public void setAnimationEnabled(boolean enabled) {
239         mAnimationsEnabled = enabled;
240     }
241 
242     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)243     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
244         super.onInitializeAccessibilityNodeInfo(info);
245         info.setTextEntryKey(true);
246     }
247 }
248