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