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 | e88b165 | 2019-09-19 22:12:46 +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 | e88b165 | 2019-09-19 22:12:46 +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 | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 61 | if (!mIsCanceled && mRevealListener != null) { |
Ahan Wu | e88b165 | 2019-09-19 22:12:46 +0800 | [diff] [blame] | 62 | if (DEBUG) { |
| 63 | Log.d(TAG, "transition end"); |
| 64 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 65 | mRevealListener.onRevealEnd(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 66 | } |
| 67 | mIsCanceled = false; |
| 68 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 69 | |
| 70 | @Override |
| 71 | public void onAnimationStart(Animator animation) { |
| 72 | if (mRevealListener != null) { |
Ahan Wu | e88b165 | 2019-09-19 22:12:46 +0800 | [diff] [blame] | 73 | if (DEBUG) { |
| 74 | Log.d(TAG, "transition start"); |
| 75 | } |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 76 | mRevealListener.onRevealStart(true /* animate */); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 77 | } |
| 78 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 79 | }); |
| 80 | } |
| 81 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 82 | public float getReveal() { |
| 83 | return mReveal; |
| 84 | } |
| 85 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 86 | void updateAwake(boolean awake, long duration) { |
Ahan Wu | e88b165 | 2019-09-19 22:12:46 +0800 | [diff] [blame] | 87 | if (DEBUG) { |
| 88 | Log.d(TAG, "updateAwake: awake=" + awake + ", duration=" + duration); |
| 89 | } |
Ahan Wu | 4d5d47f | 2019-12-04 20:14:08 +0800 | [diff] [blame] | 90 | mAnimator.cancel(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 91 | mAwake = awake; |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 92 | 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 Wu | b378059 | 2019-07-04 20:50:00 +0800 | [diff] [blame] | 97 | mRevealListener.onRevealStateChanged(); |
| 98 | mRevealListener.onRevealEnd(); |
| 99 | } else { |
Ahan Wu | 4d5d47f | 2019-12-04 20:14:08 +0800 | [diff] [blame] | 100 | mAnimator.setDuration(duration); |
| 101 | mAnimator.setFloatValues(mReveal, mAwake ? MAX_REVEAL : MIN_REVEAL); |
| 102 | mAnimator.start(); |
Ahan Wu | b378059 | 2019-07-04 20:50:00 +0800 | [diff] [blame] | 103 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 104 | } |
| 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 Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Called back while reveal starts. |
| 118 | */ |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 119 | void onRevealStart(boolean animate); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Called back while reveal ends. |
| 123 | */ |
| 124 | void onRevealEnd(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 125 | } |
| 126 | } |