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
17package com.android.systemui.charging;
18
19import android.animation.AnimatorSet;
20import android.animation.ObjectAnimator;
21import android.animation.ValueAnimator;
22import android.content.Context;
23import android.graphics.Color;
24import android.util.AttributeSet;
25import android.view.animation.PathInterpolator;
26import android.widget.FrameLayout;
27import android.widget.TextView;
28
29import com.android.systemui.Interpolators;
30import com.android.systemui.R;
31
32import java.text.NumberFormat;
33
34/**
35 * @hide
36 */
37public class WirelessChargingLayout extends FrameLayout {
38    private final static int UNKNOWN_BATTERY_LEVEL = -1;
39
40    public WirelessChargingLayout(Context context) {
41        super(context);
42        init(context, null, false);
43    }
44
45    public WirelessChargingLayout(Context context, int batteryLevel, boolean isDozing) {
46        super(context);
47        init(context, null, batteryLevel, isDozing);
48    }
49
50    public WirelessChargingLayout(Context context, AttributeSet attrs) {
51        super(context, attrs);
52        init(context, attrs, false);
53    }
54
55    private void init(Context c, AttributeSet attrs, boolean isDozing) {
56        init(c, attrs, -1, false);
57    }
58
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}