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 
17 package com.android.systemui.car.hvac.referenceui;
18 
19 import android.animation.ValueAnimator;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.graphics.drawable.GradientDrawable;
23 import android.util.AttributeSet;
24 import android.widget.ImageView;
25 
26 import com.android.systemui.R;
27 
28 /**
29  * Represents a single bar in the fan speed bar.
30  */
31 public class FanSpeedBarSegment extends ImageView {
32     private boolean mTurnedOn;
33 
34     private int mDotExpandedSize;
35     private int mDotSize;
36 
37     private ValueAnimator mDotWidthExpandAnimator;
38 
39     private final ValueAnimator.AnimatorUpdateListener mExpandListener =
40             new ValueAnimator.AnimatorUpdateListener() {
41                 @Override
42                 public void onAnimationUpdate(ValueAnimator animation) {
43                     int size = (int) animation.getAnimatedValue();
44                     GradientDrawable drawable = (GradientDrawable) getDrawable();
45                     drawable.setCornerRadius(size / 2);
46                     drawable.setSize(size, size);
47                 }
48             };
49 
FanSpeedBarSegment(Context context)50     public FanSpeedBarSegment(Context context) {
51         super(context);
52         initView();
53     }
54 
FanSpeedBarSegment(Context context, AttributeSet attrs)55     public FanSpeedBarSegment(Context context, AttributeSet attrs) {
56         super(context, attrs);
57         initView();
58     }
59 
FanSpeedBarSegment(Context context, AttributeSet attrs, int defStyleAttr)60     public FanSpeedBarSegment(Context context, AttributeSet attrs, int defStyleAttr) {
61         super(context, attrs, defStyleAttr);
62         initView();
63     }
64 
65     /**
66      * Plays the animation for the fan speed bar segment to be turned on.
67      */
playTurnOnAnimation(int delayMs, int duration)68     public void playTurnOnAnimation(int delayMs, int duration) {
69         getContext().getMainExecutor().execute(() -> {
70             mDotWidthExpandAnimator.setStartDelay(delayMs);
71             mDotWidthExpandAnimator.setDuration(duration);
72             mDotWidthExpandAnimator.start();
73             mTurnedOn = true;
74         });
75     }
76 
77     /**
78      * Plays the animation for the fan speed bar segment to be turned off.
79      */
playTurnOffAnimation(int delayMs, int duration)80     public void playTurnOffAnimation(int delayMs, int duration) {
81         getContext().getMainExecutor().execute(() -> {
82             mDotWidthExpandAnimator.setStartDelay(delayMs);
83             mDotWidthExpandAnimator.setDuration(duration);
84             mDotWidthExpandAnimator.reverse();
85             mTurnedOn = false;
86         });
87     }
88 
isTurnedOn()89     public boolean isTurnedOn() {
90         return mTurnedOn;
91     }
92 
initView()93     private void initView() {
94         setScaleType(ScaleType.CENTER);
95 
96         Resources res = getResources();
97         mDotExpandedSize = res.getDimensionPixelSize(R.dimen.hvac_fan_speed_dot_expanded_size);
98         mDotSize = res.getDimensionPixelSize(R.dimen.hvac_fan_speed_dot_size);
99         mDotWidthExpandAnimator = ValueAnimator.ofInt(mDotSize, mDotExpandedSize);
100         mDotWidthExpandAnimator.addUpdateListener(mExpandListener);
101 
102         GradientDrawable dot = new GradientDrawable();
103         dot.setColor(res.getColor(R.color.hvac_fanspeed_segment_color));
104         dot.setSize(mDotSize, mDotSize);
105         dot.setCornerRadius(mDotSize / 2);
106         setImageDrawable(dot);
107     }
108 }
109