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.util.TypedValue;
26 import android.view.ContextThemeWrapper;
27 import android.view.View;
28 import android.view.animation.PathInterpolator;
29 import android.widget.FrameLayout;
30 import android.widget.ImageView;
31 import android.widget.TextView;
32 
33 import com.android.app.animation.Interpolators;
34 import com.android.settingslib.Utils;
35 import com.android.systemui.R;
36 import com.android.systemui.shared.recents.utilities.Utilities;
37 import com.android.systemui.surfaceeffects.ripple.RippleShader;
38 import com.android.systemui.surfaceeffects.ripple.RippleShader.RippleShape;
39 import com.android.systemui.surfaceeffects.ripple.RippleView;
40 
41 import java.text.NumberFormat;
42 
43 /**
44  * @hide
45  */
46 final class WirelessChargingLayout extends FrameLayout {
47     private static final long CIRCLE_RIPPLE_ANIMATION_DURATION = 1500;
48     private static final long ROUNDED_BOX_RIPPLE_ANIMATION_DURATION = 3000;
49     private static final int SCRIM_COLOR = 0x4C000000;
50     private static final int SCRIM_FADE_DURATION = 300;
51     private RippleView mRippleView;
52     // This is only relevant to the rounded box ripple.
53     private RippleShader.SizeAtProgress[] mSizeAtProgressArray;
54 
WirelessChargingLayout(Context context, int transmittingBatteryLevel, int batteryLevel, boolean isDozing, RippleShape rippleShape)55     WirelessChargingLayout(Context context, int transmittingBatteryLevel, int batteryLevel,
56             boolean isDozing, RippleShape rippleShape) {
57         super(context);
58         init(context, null, transmittingBatteryLevel, batteryLevel, isDozing, rippleShape);
59     }
60 
WirelessChargingLayout(Context context)61     private WirelessChargingLayout(Context context) {
62         super(context);
63         init(context, null, /* isDozing= */ false, RippleShape.CIRCLE);
64     }
65 
WirelessChargingLayout(Context context, AttributeSet attrs)66     private WirelessChargingLayout(Context context, AttributeSet attrs) {
67         super(context, attrs);
68         init(context, attrs, /* isDozing= */false, RippleShape.CIRCLE);
69     }
70 
init(Context c, AttributeSet attrs, boolean isDozing, RippleShape rippleShape)71     private void init(Context c, AttributeSet attrs, boolean isDozing, RippleShape rippleShape) {
72         init(c, attrs, -1, -1, isDozing, rippleShape);
73     }
74 
init(Context context, AttributeSet attrs, int transmittingBatteryLevel, int batteryLevel, boolean isDozing, RippleShape rippleShape)75     private void init(Context context, AttributeSet attrs, int transmittingBatteryLevel,
76             int batteryLevel, boolean isDozing, RippleShape rippleShape) {
77         final boolean showTransmittingBatteryLevel =
78                 (transmittingBatteryLevel != WirelessChargingAnimation.UNKNOWN_BATTERY_LEVEL);
79 
80         // set style based on background
81         int style = R.style.ChargingAnim_WallpaperBackground;
82         if (isDozing) {
83             style = R.style.ChargingAnim_DarkBackground;
84         }
85 
86         inflate(new ContextThemeWrapper(context, style), R.layout.wireless_charging_layout, this);
87 
88         // amount of battery:
89         final TextView percentage = findViewById(R.id.wireless_charging_percentage);
90 
91         if (batteryLevel != WirelessChargingAnimation.UNKNOWN_BATTERY_LEVEL) {
92             percentage.setText(NumberFormat.getPercentInstance().format(batteryLevel / 100f));
93             percentage.setAlpha(0);
94         }
95 
96         final long chargingAnimationFadeStartOffset = context.getResources().getInteger(
97                 R.integer.wireless_charging_fade_offset);
98         final long chargingAnimationFadeDuration = context.getResources().getInteger(
99                 R.integer.wireless_charging_fade_duration);
100         final float batteryLevelTextSizeStart = context.getResources().getFloat(
101                 R.dimen.wireless_charging_anim_battery_level_text_size_start);
102         final float batteryLevelTextSizeEnd = context.getResources().getFloat(
103                 R.dimen.wireless_charging_anim_battery_level_text_size_end) * (
104                 showTransmittingBatteryLevel ? 0.75f : 1.0f);
105 
106         // Animation Scale: battery percentage text scales from 0% to 100%
107         ValueAnimator textSizeAnimator = ObjectAnimator.ofFloat(percentage, "textSize",
108                 batteryLevelTextSizeStart, batteryLevelTextSizeEnd);
109         textSizeAnimator.setInterpolator(new PathInterpolator(0, 0, 0, 1));
110         textSizeAnimator.setDuration(context.getResources().getInteger(
111                 R.integer.wireless_charging_battery_level_text_scale_animation_duration));
112 
113         // Animation Opacity: battery percentage text transitions from 0 to 1 opacity
114         ValueAnimator textOpacityAnimator = ObjectAnimator.ofFloat(percentage, "alpha", 0, 1);
115         textOpacityAnimator.setInterpolator(Interpolators.LINEAR);
116         textOpacityAnimator.setDuration(context.getResources().getInteger(
117                 R.integer.wireless_charging_battery_level_text_opacity_duration));
118         textOpacityAnimator.setStartDelay(context.getResources().getInteger(
119                 R.integer.wireless_charging_anim_opacity_offset));
120 
121         // Animation Opacity: battery percentage text fades from 1 to 0 opacity
122         ValueAnimator textFadeAnimator = ObjectAnimator.ofFloat(percentage, "alpha", 1, 0);
123         textFadeAnimator.setDuration(chargingAnimationFadeDuration);
124         textFadeAnimator.setInterpolator(Interpolators.LINEAR);
125         textFadeAnimator.setStartDelay(chargingAnimationFadeStartOffset);
126 
127         // play all animations together
128         AnimatorSet animatorSet = new AnimatorSet();
129         animatorSet.playTogether(textSizeAnimator, textOpacityAnimator, textFadeAnimator);
130 
131         // For tablet docking animation, we don't play the background scrim.
132         // TODO(b/270524780): use utility to check for tablet instead.
133         if (!Utilities.isLargeScreen(context)) {
134             ValueAnimator scrimFadeInAnimator = ObjectAnimator.ofArgb(this,
135                     "backgroundColor", Color.TRANSPARENT, SCRIM_COLOR);
136             scrimFadeInAnimator.setDuration(SCRIM_FADE_DURATION);
137             scrimFadeInAnimator.setInterpolator(Interpolators.LINEAR);
138             ValueAnimator scrimFadeOutAnimator = ObjectAnimator.ofArgb(this,
139                     "backgroundColor", SCRIM_COLOR, Color.TRANSPARENT);
140             scrimFadeOutAnimator.setDuration(SCRIM_FADE_DURATION);
141             scrimFadeOutAnimator.setInterpolator(Interpolators.LINEAR);
142             scrimFadeOutAnimator.setStartDelay((rippleShape == RippleShape.CIRCLE
143                     ? CIRCLE_RIPPLE_ANIMATION_DURATION : ROUNDED_BOX_RIPPLE_ANIMATION_DURATION)
144                     - SCRIM_FADE_DURATION);
145             AnimatorSet animatorSetScrim = new AnimatorSet();
146             animatorSetScrim.playTogether(scrimFadeInAnimator, scrimFadeOutAnimator);
147             animatorSetScrim.start();
148         }
149 
150         mRippleView = findViewById(R.id.wireless_charging_ripple);
151         mRippleView.setupShader(rippleShape);
152         int color = Utils.getColorAttr(mRippleView.getContext(),
153                 android.R.attr.colorAccent).getDefaultColor();
154         if (mRippleView.getRippleShape() == RippleShape.ROUNDED_BOX) {
155             mRippleView.setDuration(ROUNDED_BOX_RIPPLE_ANIMATION_DURATION);
156             mRippleView.setSparkleStrength(0.22f);
157             mRippleView.setColor(color, 102); // 40% of opacity.
158             mRippleView.setBaseRingFadeParams(
159                     /* fadeInStart = */ 0f,
160                     /* fadeInEnd = */ 0f,
161                     /* fadeOutStart = */ 0.2f,
162                     /* fadeOutEnd= */ 0.47f
163             );
164             mRippleView.setSparkleRingFadeParams(
165                     /* fadeInStart = */ 0f,
166                     /* fadeInEnd = */ 0f,
167                     /* fadeOutStart = */ 0.2f,
168                     /* fadeOutEnd= */ 1f
169             );
170             mRippleView.setCenterFillFadeParams(
171                     /* fadeInStart = */ 0f,
172                     /* fadeInEnd = */ 0f,
173                     /* fadeOutStart = */ 0f,
174                     /* fadeOutEnd= */ 0.2f
175             );
176             mRippleView.setBlur(6.5f, 2.5f);
177         } else {
178             mRippleView.setDuration(CIRCLE_RIPPLE_ANIMATION_DURATION);
179             mRippleView.setColor(color, RippleShader.RIPPLE_DEFAULT_ALPHA);
180         }
181 
182         OnAttachStateChangeListener listener = new OnAttachStateChangeListener() {
183             @Override
184             public void onViewAttachedToWindow(View view) {
185                 mRippleView.startRipple();
186                 mRippleView.removeOnAttachStateChangeListener(this);
187             }
188 
189             @Override
190             public void onViewDetachedFromWindow(View view) {}
191         };
192         mRippleView.addOnAttachStateChangeListener(listener);
193 
194         if (!showTransmittingBatteryLevel) {
195             animatorSet.start();
196             return;
197         }
198 
199         // amount of transmitting battery:
200         final TextView transmittingPercentage = findViewById(
201                 R.id.reverse_wireless_charging_percentage);
202         transmittingPercentage.setVisibility(VISIBLE);
203         transmittingPercentage.setText(
204                 NumberFormat.getPercentInstance().format(transmittingBatteryLevel / 100f));
205         transmittingPercentage.setAlpha(0);
206 
207         // Animation Scale: transmitting battery percentage text scales from 0% to 100%
208         ValueAnimator textSizeAnimatorTransmitting = ObjectAnimator.ofFloat(transmittingPercentage,
209                 "textSize", batteryLevelTextSizeStart, batteryLevelTextSizeEnd);
210         textSizeAnimatorTransmitting.setInterpolator(new PathInterpolator(0, 0, 0, 1));
211         textSizeAnimatorTransmitting.setDuration(context.getResources().getInteger(
212                 R.integer.wireless_charging_battery_level_text_scale_animation_duration));
213 
214         // Animation Opacity: transmitting battery percentage text transitions from 0 to 1 opacity
215         ValueAnimator textOpacityAnimatorTransmitting = ObjectAnimator.ofFloat(
216                 transmittingPercentage, "alpha", 0, 1);
217         textOpacityAnimatorTransmitting.setInterpolator(Interpolators.LINEAR);
218         textOpacityAnimatorTransmitting.setDuration(context.getResources().getInteger(
219                 R.integer.wireless_charging_battery_level_text_opacity_duration));
220         textOpacityAnimatorTransmitting.setStartDelay(
221                 context.getResources().getInteger(R.integer.wireless_charging_anim_opacity_offset));
222 
223         // Animation Opacity: transmitting battery percentage text fades from 1 to 0 opacity
224         ValueAnimator textFadeAnimatorTransmitting = ObjectAnimator.ofFloat(transmittingPercentage,
225                 "alpha", 1, 0);
226         textFadeAnimatorTransmitting.setDuration(chargingAnimationFadeDuration);
227         textFadeAnimatorTransmitting.setInterpolator(Interpolators.LINEAR);
228         textFadeAnimatorTransmitting.setStartDelay(chargingAnimationFadeStartOffset);
229 
230         // play all animations together
231         AnimatorSet animatorSetTransmitting = new AnimatorSet();
232         animatorSetTransmitting.playTogether(textSizeAnimatorTransmitting,
233                 textOpacityAnimatorTransmitting, textFadeAnimatorTransmitting);
234 
235         // transmitting battery icon
236         final ImageView chargingViewIcon = findViewById(R.id.reverse_wireless_charging_icon);
237         chargingViewIcon.setVisibility(VISIBLE);
238         final int padding = Math.round(
239                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, batteryLevelTextSizeEnd,
240                         getResources().getDisplayMetrics()));
241         chargingViewIcon.setPadding(padding, 0, padding, 0);
242 
243         // Animation Opacity: transmitting battery icon transitions from 0 to 1 opacity
244         ValueAnimator textOpacityAnimatorIcon = ObjectAnimator.ofFloat(chargingViewIcon, "alpha", 0,
245                 1);
246         textOpacityAnimatorIcon.setInterpolator(Interpolators.LINEAR);
247         textOpacityAnimatorIcon.setDuration(context.getResources().getInteger(
248                 R.integer.wireless_charging_battery_level_text_opacity_duration));
249         textOpacityAnimatorIcon.setStartDelay(
250                 context.getResources().getInteger(R.integer.wireless_charging_anim_opacity_offset));
251 
252         // Animation Opacity: transmitting battery icon fades from 1 to 0 opacity
253         ValueAnimator textFadeAnimatorIcon = ObjectAnimator.ofFloat(chargingViewIcon, "alpha", 1,
254                 0);
255         textFadeAnimatorIcon.setDuration(chargingAnimationFadeDuration);
256         textFadeAnimatorIcon.setInterpolator(Interpolators.LINEAR);
257         textFadeAnimatorIcon.setStartDelay(chargingAnimationFadeStartOffset);
258 
259         // play all animations together
260         AnimatorSet animatorSetIcon = new AnimatorSet();
261         animatorSetIcon.playTogether(textOpacityAnimatorIcon, textFadeAnimatorIcon);
262 
263         animatorSet.start();
264         animatorSetTransmitting.start();
265         animatorSetIcon.start();
266     }
267 
268     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)269     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
270         if (mRippleView != null) {
271             int width = getMeasuredWidth();
272             int height = getMeasuredHeight();
273             mRippleView.setCenter(width * 0.5f, height * 0.5f);
274             if (mRippleView.getRippleShape() == RippleShape.ROUNDED_BOX) {
275                 updateRippleSizeAtProgressList(width, height);
276             } else {
277                 float maxSize = Math.max(width, height);
278                 mRippleView.setMaxSize(maxSize, maxSize);
279             }
280         }
281 
282         super.onLayout(changed, left, top, right, bottom);
283     }
284 
updateRippleSizeAtProgressList(float width, float height)285     private void updateRippleSizeAtProgressList(float width, float height) {
286         if (mSizeAtProgressArray == null) {
287             float maxSize = Math.max(width, height);
288             mSizeAtProgressArray = new RippleShader.SizeAtProgress[] {
289                     // Those magic numbers are introduced for visual polish. It starts from a pill
290                     // shape and expand to a full circle.
291                     new RippleShader.SizeAtProgress(0f, 0f, 0f),
292                     new RippleShader.SizeAtProgress(0.3f, width * 0.4f, height * 0.4f),
293                     new RippleShader.SizeAtProgress(1f, maxSize, maxSize)
294             };
295         } else {
296             // Same multipliers, just need to recompute with the new width and height.
297             RippleShader.SizeAtProgress first = mSizeAtProgressArray[0];
298             first.setT(0f);
299             first.setWidth(0f);
300             first.setHeight(0f);
301 
302             RippleShader.SizeAtProgress second = mSizeAtProgressArray[1];
303             second.setT(0.3f);
304             second.setWidth(width * 0.4f);
305             second.setHeight(height * 0.4f);
306 
307             float maxSize = Math.max(width, height);
308             RippleShader.SizeAtProgress last = mSizeAtProgressArray[2];
309             last.setT(1f);
310             last.setWidth(maxSize);
311             last.setHeight(maxSize);
312         }
313 
314         mRippleView.setSizeAtProgresses(mSizeAtProgressArray);
315     }
316 }
317