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.AccessibilityManager;
34 import android.widget.TextView;
35 
36 import androidx.annotation.Nullable;
37 
38 import com.android.settingslib.Utils;
39 import com.android.systemui.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         setOnHoverListener(new LiftToActivateListener(
109                 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE)));
110 
111         mPM = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
112         LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
113                 Context.LAYOUT_INFLATER_SERVICE);
114         inflater.inflate(contentResource, this, true);
115 
116         mDigitText = (TextView) findViewById(R.id.digit_text);
117         mDigitText.setText(Integer.toString(mDigit));
118         mKlondikeText = (TextView) findViewById(R.id.klondike_text);
119 
120         if (mDigit >= 0) {
121             if (sKlondike == null) {
122                 sKlondike = getResources().getStringArray(R.array.lockscreen_num_pad_klondike);
123             }
124             if (sKlondike != null && sKlondike.length > mDigit) {
125                 String klondike = sKlondike[mDigit];
126                 final int len = klondike.length();
127                 if (len > 0) {
128                     mKlondikeText.setText(klondike);
129                 } else if (mKlondikeText.getVisibility() != View.GONE) {
130                     mKlondikeText.setVisibility(View.INVISIBLE);
131                 }
132             }
133         }
134 
135         setContentDescription(mDigitText.getText().toString());
136 
137         Drawable background = getBackground();
138         if (background instanceof GradientDrawable) {
139             mAnimator = new NumPadAnimator(context, background.mutate(),
140                     R.style.NumPadKey, mDigitText, null);
141         } else {
142             mAnimator = null;
143         }
144     }
145 
146     @Override
onConfigurationChanged(Configuration newConfig)147     protected void onConfigurationChanged(Configuration newConfig) {
148         mOrientation = newConfig.orientation;
149     }
150 
151     /**
152      * Reload colors from resources.
153      **/
reloadColors()154     public void reloadColors() {
155         int textColor = Utils.getColorAttr(getContext(), NUM_PAD_KEY)
156                 .getDefaultColor();
157         int klondikeColor = Utils.getColorAttr(getContext(), android.R.attr.textColorSecondary)
158                 .getDefaultColor();
159         mDigitText.setTextColor(textColor);
160         mKlondikeText.setTextColor(klondikeColor);
161 
162         if (mAnimator != null) mAnimator.reloadColors(getContext());
163     }
164 
165     @Override
onTouchEvent(MotionEvent event)166     public boolean onTouchEvent(MotionEvent event) {
167         switch(event.getActionMasked()) {
168             case MotionEvent.ACTION_DOWN:
169                 doHapticKeyClick();
170                 if (mAnimator != null && mAnimationsEnabled) mAnimator.expand();
171                 break;
172             case MotionEvent.ACTION_UP:
173             case MotionEvent.ACTION_CANCEL:
174                 if (mAnimator != null && mAnimationsEnabled) mAnimator.contract();
175                 break;
176         }
177         return super.onTouchEvent(event);
178     }
179 
180     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)181     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
182         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
183         measureChildren(widthMeasureSpec, heightMeasureSpec);
184 
185         // Set width/height to the same value to ensure a smooth circle for the bg, but shrink
186         // the height to match the old pin bouncer.
187         // This is only used for PIN/PUK; the main PIN pad now uses ConstraintLayout, which will
188         // force our width/height to conform to the ratio in the layout.
189         int width = getMeasuredWidth();
190 
191         boolean shortenHeight = mAnimator == null
192                 || mOrientation == Configuration.ORIENTATION_LANDSCAPE;
193         int height = shortenHeight ? (int) (width * .66f) : width;
194 
195         setMeasuredDimension(getMeasuredWidth(), height);
196     }
197 
198     @Override
onLayout(boolean changed, int l, int t, int r, int b)199     protected void onLayout(boolean changed, int l, int t, int r, int b) {
200         int digitHeight = mDigitText.getMeasuredHeight();
201         int klondikeHeight = mKlondikeText.getMeasuredHeight();
202         int totalHeight = digitHeight + klondikeHeight;
203         int top = getHeight() / 2 - totalHeight / 2;
204         int centerX = getWidth() / 2;
205         int left = centerX - mDigitText.getMeasuredWidth() / 2;
206         int bottom = top + digitHeight;
207         mDigitText.layout(left, top, left + mDigitText.getMeasuredWidth(), bottom);
208         top = (int) (bottom - klondikeHeight * 0.35f);
209         bottom = top + klondikeHeight;
210 
211         left = centerX - mKlondikeText.getMeasuredWidth() / 2;
212         mKlondikeText.layout(left, top, left + mKlondikeText.getMeasuredWidth(), bottom);
213 
214         int width = r - l;
215         int height = b - t;
216         if (mAnimator != null) mAnimator.onLayout(width, height);
217     }
218 
219     @Override
hasOverlappingRendering()220     public boolean hasOverlappingRendering() {
221         return false;
222     }
223 
224     // Cause a VIRTUAL_KEY vibration
doHapticKeyClick()225     public void doHapticKeyClick() {
226         performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY,
227                 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
228     }
229 
230     @Override
setProgress(float progress)231     public void setProgress(float progress) {
232         if (mAnimator != null) {
233             mAnimator.setProgress(progress);
234         }
235     }
236 
237     /**
238      * Controls the animation when a key is pressed
239      */
setAnimationEnabled(boolean enabled)240     public void setAnimationEnabled(boolean enabled) {
241         mAnimationsEnabled = enabled;
242     }
243 }
244