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