Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.glwallpaper; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ValueAnimator; |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 22 | import android.util.Log; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 23 | |
| 24 | import 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 | */ |
| 30 | class 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 Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 34 | private static final boolean DEBUG = true; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 35 | |
| 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 Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 61 | 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 Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 68 | } |
| 69 | mIsCanceled = false; |
| 70 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public void onAnimationStart(Animator animation) { |
| 74 | if (mRevealListener != null) { |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 75 | if (DEBUG) { |
| 76 | Log.d(TAG, "transition start"); |
| 77 | } |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 78 | mRevealListener.onRevealStart(true /* animate */); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 79 | } |
| 80 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 81 | }); |
| 82 | } |
| 83 | |
| 84 | private void animate() { |
| 85 | mAnimator.cancel(); |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 86 | mAnimator.setFloatValues(mReveal, mAwake ? MAX_REVEAL : MIN_REVEAL); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 87 | mAnimator.start(); |
| 88 | } |
| 89 | |
| 90 | public float getReveal() { |
| 91 | return mReveal; |
| 92 | } |
| 93 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 94 | void updateAwake(boolean awake, long duration) { |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 95 | if (DEBUG) { |
| 96 | Log.d(TAG, "updateAwake: awake=" + awake + ", duration=" + duration); |
| 97 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 98 | mAwake = awake; |
| 99 | mAnimator.setDuration(duration); |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 100 | 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 Wu | b378059 | 2019-07-04 20:50:00 +0800 | [diff] [blame] | 105 | mRevealListener.onRevealStateChanged(); |
| 106 | mRevealListener.onRevealEnd(); |
| 107 | } else { |
| 108 | animate(); |
| 109 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 110 | } |
| 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 Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Called back while reveal starts. |
| 124 | */ |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 125 | void onRevealStart(boolean animate); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Called back while reveal ends. |
| 129 | */ |
| 130 | void onRevealEnd(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 131 | } |
| 132 | } |