blob: 39c9632278d6cccf4ad2b515b6f44adc6a06efc6 [file] [log] [blame]
Ahan Wu67e7f102019-01-14 20:38:14 +08001/*
2 * Copyright (C) 2019 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.glwallpaper;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
Ahan Wu3573cdd2019-10-04 11:59:10 +080022import android.util.Log;
Ahan Wu67e7f102019-01-14 20:38:14 +080023
24import com.android.systemui.Interpolators;
25
26/**
27 * Use ValueAnimator and appropriate interpolator to control the progress of reveal transition.
28 * The transition will happen while getting awake and quit events.
29 */
30class ImageRevealHelper {
31 private static final String TAG = ImageRevealHelper.class.getSimpleName();
32 private static final float MAX_REVEAL = 0f;
33 private static final float MIN_REVEAL = 1f;
Ahan Wu3573cdd2019-10-04 11:59:10 +080034 private static final boolean DEBUG = true;
Ahan Wu67e7f102019-01-14 20:38:14 +080035
36 private final ValueAnimator mAnimator;
37 private final RevealStateListener mRevealListener;
38 private float mReveal = MAX_REVEAL;
39 private boolean mAwake = false;
40
41 ImageRevealHelper(RevealStateListener listener) {
42 mRevealListener = listener;
43 mAnimator = ValueAnimator.ofFloat();
44 mAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
45 mAnimator.addUpdateListener(animator -> {
46 mReveal = (float) animator.getAnimatedValue();
47 if (mRevealListener != null) {
48 mRevealListener.onRevealStateChanged();
49 }
50 });
51 mAnimator.addListener(new AnimatorListenerAdapter() {
52 private boolean mIsCanceled;
53
54 @Override
55 public void onAnimationCancel(Animator animation) {
56 mIsCanceled = true;
57 }
58
59 @Override
60 public void onAnimationEnd(Animator animation) {
Ahan Wu3573cdd2019-10-04 11:59:10 +080061 if (mRevealListener != null) {
62 if (DEBUG) {
63 Log.d(TAG, "transition end, cancel=" + mIsCanceled + ", reveal=" + mReveal);
64 }
65 if (!mIsCanceled) {
66 mRevealListener.onRevealEnd();
67 }
Ahan Wu67e7f102019-01-14 20:38:14 +080068 }
69 mIsCanceled = false;
70 }
Ahan Wufa42c512019-05-15 19:52:51 +080071
72 @Override
73 public void onAnimationStart(Animator animation) {
74 if (mRevealListener != null) {
Ahan Wu3573cdd2019-10-04 11:59:10 +080075 if (DEBUG) {
76 Log.d(TAG, "transition start");
77 }
Ahan Wu0e174802019-07-23 20:41:52 +080078 mRevealListener.onRevealStart(true /* animate */);
Ahan Wufa42c512019-05-15 19:52:51 +080079 }
80 }
Ahan Wu67e7f102019-01-14 20:38:14 +080081 });
82 }
83
84 private void animate() {
85 mAnimator.cancel();
Ahan Wu0e174802019-07-23 20:41:52 +080086 mAnimator.setFloatValues(mReveal, mAwake ? MAX_REVEAL : MIN_REVEAL);
Ahan Wu67e7f102019-01-14 20:38:14 +080087 mAnimator.start();
88 }
89
90 public float getReveal() {
91 return mReveal;
92 }
93
Ahan Wu67e7f102019-01-14 20:38:14 +080094 void updateAwake(boolean awake, long duration) {
Ahan Wu3573cdd2019-10-04 11:59:10 +080095 if (DEBUG) {
96 Log.d(TAG, "updateAwake: awake=" + awake + ", duration=" + duration);
97 }
Ahan Wu67e7f102019-01-14 20:38:14 +080098 mAwake = awake;
99 mAnimator.setDuration(duration);
Ahan Wu0e174802019-07-23 20:41:52 +0800100 if (duration == 0) {
101 // We are transiting from home to aod or aod to home directly,
102 // we don't need to do transition in these cases.
103 mReveal = mAwake ? MAX_REVEAL : MIN_REVEAL;
104 mRevealListener.onRevealStart(false /* animate */);
Ahan Wub3780592019-07-04 20:50:00 +0800105 mRevealListener.onRevealStateChanged();
106 mRevealListener.onRevealEnd();
107 } else {
108 animate();
109 }
Ahan Wu67e7f102019-01-14 20:38:14 +0800110 }
111
112 /**
113 * A listener to trace value changes of reveal.
114 */
115 public interface RevealStateListener {
116
117 /**
118 * Called back while reveal status changes.
119 */
120 void onRevealStateChanged();
Ahan Wufa42c512019-05-15 19:52:51 +0800121
122 /**
123 * Called back while reveal starts.
124 */
Ahan Wu0e174802019-07-23 20:41:52 +0800125 void onRevealStart(boolean animate);
Ahan Wufa42c512019-05-15 19:52:51 +0800126
127 /**
128 * Called back while reveal ends.
129 */
130 void onRevealEnd();
Ahan Wu67e7f102019-01-14 20:38:14 +0800131 }
132}