• 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.animation.AnimatorSet;
19 import android.animation.ValueAnimator;
20 import android.content.Context;
21 import android.content.res.ColorStateList;
22 import android.content.res.TypedArray;
23 import android.graphics.drawable.GradientDrawable;
24 import android.graphics.drawable.RippleDrawable;
25 import android.view.ContextThemeWrapper;
26 
27 import androidx.annotation.StyleRes;
28 
29 import com.android.systemui.R;
30 import com.android.systemui.animation.Interpolators;
31 import com.android.systemui.util.Utils;
32 
33 /**
34  * Provides background color and radius animations for key pad buttons.
35  */
36 class NumPadAnimator {
37     private AnimatorSet mAnimator;
38     private ValueAnimator mExpandAnimator;
39     private ValueAnimator mContractAnimator;
40     private GradientDrawable mBackground;
41     private RippleDrawable mRipple;
42     private int mNormalColor;
43     private int mHighlightColor;
44     private int mStyle;
45 
NumPadAnimator(Context context, final RippleDrawable drawable, @StyleRes int style)46     NumPadAnimator(Context context, final RippleDrawable drawable, @StyleRes int style) {
47         mStyle = style;
48         mRipple = (RippleDrawable) drawable.mutate();
49         mBackground = (GradientDrawable) mRipple.findDrawableByLayerId(R.id.background);
50 
51         reloadColors(context);
52 
53         // Actual values will be updated later, usually during an onLayout() call
54         mAnimator = new AnimatorSet();
55         mExpandAnimator = ValueAnimator.ofFloat(0f, 1f);
56         mExpandAnimator.setDuration(50);
57         mExpandAnimator.setInterpolator(Interpolators.LINEAR);
58         mExpandAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
59                 public void onAnimationUpdate(ValueAnimator anim) {
60                     mBackground.setCornerRadius((float) anim.getAnimatedValue());
61                     mRipple.invalidateSelf();
62                 }
63         });
64 
65         mContractAnimator = ValueAnimator.ofFloat(1f, 0f);
66         mContractAnimator.setStartDelay(33);
67         mContractAnimator.setDuration(417);
68         mContractAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
69         mContractAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
70                 public void onAnimationUpdate(ValueAnimator anim) {
71                     mBackground.setCornerRadius((float) anim.getAnimatedValue());
72                     mRipple.invalidateSelf();
73                 }
74         });
75         mAnimator.playSequentially(mExpandAnimator, mContractAnimator);
76     }
77 
onLayout(int height)78     void onLayout(int height) {
79         float startRadius = height / 2f;
80         float endRadius = height / 4f;
81         mBackground.setCornerRadius(startRadius);
82         mExpandAnimator.setFloatValues(startRadius, endRadius);
83         mContractAnimator.setFloatValues(endRadius, startRadius);
84     }
85 
start()86     void start() {
87         mAnimator.cancel();
88         mAnimator.start();
89     }
90 
91     /**
92      * Reload colors from resources.
93      **/
reloadColors(Context context)94     void reloadColors(Context context) {
95         int[] customAttrs = {android.R.attr.colorControlNormal,
96                 android.R.attr.colorControlHighlight};
97 
98         ContextThemeWrapper ctw = new ContextThemeWrapper(context, mStyle);
99         TypedArray a = ctw.obtainStyledAttributes(customAttrs);
100         mNormalColor = Utils.getPrivateAttrColorIfUnset(ctw, a, 0, 0,
101                 com.android.internal.R.attr.colorSurface);
102         mHighlightColor = a.getColor(1, 0);
103         a.recycle();
104 
105         mBackground.setColor(mNormalColor);
106         mRipple.setColor(ColorStateList.valueOf(mHighlightColor));
107     }
108 }
109 
110