1 /*
2  * Copyright (C) 2018 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.charging;
18 
19 import android.animation.AnimatorSet;
20 import android.animation.ObjectAnimator;
21 import android.animation.ValueAnimator;
22 import android.content.Context;
23 import android.graphics.Color;
24 import android.util.AttributeSet;
25 import android.view.animation.PathInterpolator;
26 import android.widget.FrameLayout;
27 import android.widget.TextView;
28 
29 import com.android.systemui.Interpolators;
30 import com.android.systemui.R;
31 
32 import java.text.NumberFormat;
33 
34 /**
35  * @hide
36  */
37 public class WirelessChargingLayout extends FrameLayout {
38     private final static int UNKNOWN_BATTERY_LEVEL = -1;
39 
WirelessChargingLayout(Context context)40     public WirelessChargingLayout(Context context) {
41         super(context);
42         init(context, null, false);
43     }
44 
WirelessChargingLayout(Context context, int batteryLevel, boolean isDozing)45     public WirelessChargingLayout(Context context, int batteryLevel, boolean isDozing) {
46         super(context);
47         init(context, null, batteryLevel, isDozing);
48     }
49 
WirelessChargingLayout(Context context, AttributeSet attrs)50     public WirelessChargingLayout(Context context, AttributeSet attrs) {
51         super(context, attrs);
52         init(context, attrs, false);
53     }
54 
init(Context c, AttributeSet attrs, boolean isDozing)55     private void init(Context c, AttributeSet attrs, boolean isDozing) {
56         init(c, attrs, -1, false);
57     }
58 
init(Context context, AttributeSet attrs, int batteryLevel, boolean isDozing)59     private void init(Context context, AttributeSet attrs, int batteryLevel, boolean isDozing) {
60         final int mBatteryLevel = batteryLevel;
61         inflate(context, R.layout.wireless_charging_layout, this);
62 
63         // where the circle animation occurs:
64         final WirelessChargingView mChargingView = findViewById(R.id.wireless_charging_view);
65 
66         // amount of battery:
67         final TextView mPercentage = findViewById(R.id.wireless_charging_percentage);
68 
69         if (isDozing) {
70             mChargingView.setPaintColor(Color.WHITE);
71             mPercentage.setTextColor(Color.WHITE);
72         }
73 
74         if (batteryLevel != UNKNOWN_BATTERY_LEVEL) {
75             mPercentage.setText(NumberFormat.getPercentInstance().format(mBatteryLevel / 100f));
76             mPercentage.setAlpha(0);
77         }
78 
79         final long chargingAnimationFadeStartOffset = (long) context.getResources().getInteger(
80                 R.integer.wireless_charging_fade_offset);
81         final long chargingAnimationFadeDuration = (long) context.getResources().getInteger(
82                 R.integer.wireless_charging_fade_duration);
83         final float batteryLevelTextSizeStart = context.getResources().getFloat(
84                 R.dimen.wireless_charging_anim_battery_level_text_size_start);
85         final float batteryLevelTextSizeEnd = context.getResources().getFloat(
86                 R.dimen.wireless_charging_anim_battery_level_text_size_end);
87 
88         // Animation Scale: battery percentage text scales from 0% to 100%
89         ValueAnimator textSizeAnimator = ObjectAnimator.ofFloat(mPercentage, "textSize",
90                 batteryLevelTextSizeStart, batteryLevelTextSizeEnd);
91         textSizeAnimator.setInterpolator(new PathInterpolator(0, 0, 0, 1));
92         textSizeAnimator.setDuration((long) context.getResources().getInteger(
93                 R.integer.wireless_charging_battery_level_text_scale_animation_duration));
94 
95         // Animation Opacity: battery percentage text transitions from 0 to 1 opacity
96         ValueAnimator textOpacityAnimator = ObjectAnimator.ofFloat(mPercentage, "alpha", 0, 1);
97         textOpacityAnimator.setInterpolator(Interpolators.LINEAR);
98         textOpacityAnimator.setDuration((long) context.getResources().getInteger(
99                 R.integer.wireless_charging_battery_level_text_opacity_duration));
100         textOpacityAnimator.setStartDelay((long) context.getResources().getInteger(
101                 R.integer.wireless_charging_anim_opacity_offset));
102 
103         // Animation Opacity: battery percentage text fades from 1 to 0 opacity
104         ValueAnimator textFadeAnimator = ObjectAnimator.ofFloat(mPercentage, "alpha", 1, 0);
105         textFadeAnimator.setDuration(chargingAnimationFadeDuration);
106         textFadeAnimator.setInterpolator(Interpolators.LINEAR);
107         textFadeAnimator.setStartDelay(chargingAnimationFadeStartOffset);
108 
109         // Animation Opacity: wireless charging circle animation fades from 1 to 0 opacity
110         ValueAnimator circleFadeAnimator = ObjectAnimator.ofFloat(mChargingView, "alpha",
111                 1, 0);
112         circleFadeAnimator.setDuration(chargingAnimationFadeDuration);
113         circleFadeAnimator.setInterpolator(Interpolators.LINEAR);
114         circleFadeAnimator.setStartDelay(chargingAnimationFadeStartOffset);
115 
116         // play all animations together
117         AnimatorSet animatorSet = new AnimatorSet();
118         animatorSet.playTogether(textSizeAnimator, textOpacityAnimator, textFadeAnimator,
119                 circleFadeAnimator);
120         animatorSet.start();
121     }
122 }