blob: f815b5d476ec47fe04ac5945bb147ce4c3498be2 [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 Wue88b1652019-09-19 22:12:46 +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 Wue88b1652019-09-19 22:12:46 +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 Wufa42c512019-05-15 19:52:51 +080061 if (!mIsCanceled && mRevealListener != null) {
Ahan Wue88b1652019-09-19 22:12:46 +080062 if (DEBUG) {
63 Log.d(TAG, "transition end");
64 }
Ahan Wufa42c512019-05-15 19:52:51 +080065 mRevealListener.onRevealEnd();
Ahan Wu67e7f102019-01-14 20:38:14 +080066 }
67 mIsCanceled = false;
68 }
Ahan Wufa42c512019-05-15 19:52:51 +080069
70 @Override
71 public void onAnimationStart(Animator animation) {
72 if (mRevealListener != null) {
Ahan Wue88b1652019-09-19 22:12:46 +080073 if (DEBUG) {
74 Log.d(TAG, "transition start");
75 }
Ahan Wu0e174802019-07-23 20:41:52 +080076 mRevealListener.onRevealStart(true /* animate */);
Ahan Wufa42c512019-05-15 19:52:51 +080077 }
78 }
Ahan Wu67e7f102019-01-14 20:38:14 +080079 });
80 }
81
Ahan Wu67e7f102019-01-14 20:38:14 +080082 public float getReveal() {
83 return mReveal;
84 }
85
Ahan Wu67e7f102019-01-14 20:38:14 +080086 void updateAwake(boolean awake, long duration) {
Ahan Wue88b1652019-09-19 22:12:46 +080087 if (DEBUG) {
88 Log.d(TAG, "updateAwake: awake=" + awake + ", duration=" + duration);
89 }
Ahan Wu4d5d47f2019-12-04 20:14:08 +080090 mAnimator.cancel();
Ahan Wu67e7f102019-01-14 20:38:14 +080091 mAwake = awake;
Ahan Wu0e174802019-07-23 20:41:52 +080092 if (duration == 0) {
93 // We are transiting from home to aod or aod to home directly,
94 // we don't need to do transition in these cases.
95 mReveal = mAwake ? MAX_REVEAL : MIN_REVEAL;
96 mRevealListener.onRevealStart(false /* animate */);
Ahan Wub3780592019-07-04 20:50:00 +080097 mRevealListener.onRevealStateChanged();
98 mRevealListener.onRevealEnd();
99 } else {
Ahan Wu4d5d47f2019-12-04 20:14:08 +0800100 mAnimator.setDuration(duration);
101 mAnimator.setFloatValues(mReveal, mAwake ? MAX_REVEAL : MIN_REVEAL);
102 mAnimator.start();
Ahan Wub3780592019-07-04 20:50:00 +0800103 }
Ahan Wu67e7f102019-01-14 20:38:14 +0800104 }
105
106 /**
107 * A listener to trace value changes of reveal.
108 */
109 public interface RevealStateListener {
110
111 /**
112 * Called back while reveal status changes.
113 */
114 void onRevealStateChanged();
Ahan Wufa42c512019-05-15 19:52:51 +0800115
116 /**
117 * Called back while reveal starts.
118 */
Ahan Wu0e174802019-07-23 20:41:52 +0800119 void onRevealStart(boolean animate);
Ahan Wufa42c512019-05-15 19:52:51 +0800120
121 /**
122 * Called back while reveal ends.
123 */
124 void onRevealEnd();
Ahan Wu67e7f102019-01-14 20:38:14 +0800125 }
126}