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