• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.ColorStateList;
20 import android.content.res.Configuration;
21 import android.graphics.drawable.Drawable;
22 import android.graphics.drawable.RippleDrawable;
23 import android.graphics.drawable.VectorDrawable;
24 import android.util.AttributeSet;
25 import android.view.MotionEvent;
26 
27 import androidx.annotation.Nullable;
28 
29 import com.android.settingslib.Utils;
30 
31 /**
32  * Similar to the {@link NumPadKey}, but displays an image.
33  */
34 public class NumPadButton extends AlphaOptimizedImageButton {
35 
36     @Nullable
37     private NumPadAnimator mAnimator;
38     private int mOrientation;
39 
NumPadButton(Context context, AttributeSet attrs)40     public NumPadButton(Context context, AttributeSet attrs) {
41         super(context, attrs);
42 
43         Drawable background = getBackground();
44         if (background instanceof RippleDrawable) {
45             mAnimator = new NumPadAnimator(context, (RippleDrawable) getBackground(),
46                     attrs.getStyleAttribute());
47         } else {
48             mAnimator = null;
49         }
50     }
51 
52     @Override
onConfigurationChanged(Configuration newConfig)53     protected void onConfigurationChanged(Configuration newConfig) {
54         mOrientation = newConfig.orientation;
55     }
56 
57     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)58     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
59         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
60 
61         // Set width/height to the same value to ensure a smooth circle for the bg, but shrink
62         // the height to match the old pin bouncer
63         int width = getMeasuredWidth();
64 
65         boolean shortenHeight = mAnimator == null
66                 || mOrientation == Configuration.ORIENTATION_LANDSCAPE;
67         int height = shortenHeight ? (int) (width * .66f) : width;
68 
69         setMeasuredDimension(getMeasuredWidth(), height);
70     }
71 
72     @Override
onLayout(boolean changed, int l, int t, int r, int b)73     protected void onLayout(boolean changed, int l, int t, int r, int b) {
74         super.onLayout(changed, l, t, r, b);
75 
76         if (mAnimator != null) mAnimator.onLayout(b - t);
77     }
78 
79     @Override
onTouchEvent(MotionEvent event)80     public boolean onTouchEvent(MotionEvent event) {
81         if (event.getActionMasked() == MotionEvent.ACTION_DOWN && mAnimator != null) {
82             mAnimator.start();
83         }
84         return super.onTouchEvent(event);
85     }
86 
87     /**
88      * Reload colors from resources.
89      **/
reloadColors()90     public void reloadColors() {
91         if (mAnimator != null) mAnimator.reloadColors(getContext());
92 
93         int textColor = Utils.getColorAttrDefaultColor(getContext(),
94                 android.R.attr.colorBackground);
95         ((VectorDrawable) getDrawable()).setTintList(ColorStateList.valueOf(textColor));
96     }
97 }
98