Ahan Wu | 9a8e260 | 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; |
| 22 | |
| 23 | import com.android.systemui.Interpolators; |
| 24 | |
| 25 | /** |
| 26 | * Use ValueAnimator and appropriate interpolator to control the progress of reveal transition. |
| 27 | * The transition will happen while getting awake and quit events. |
| 28 | */ |
| 29 | class ImageRevealHelper { |
| 30 | private static final String TAG = ImageRevealHelper.class.getSimpleName(); |
| 31 | private static final float MAX_REVEAL = 0f; |
| 32 | private static final float MIN_REVEAL = 1f; |
| 33 | private static final int REVEAL_DURATION = 1000; |
| 34 | |
| 35 | private final ValueAnimator mAnimator; |
| 36 | private final RevealStateListener mRevealListener; |
| 37 | private float mReveal = MIN_REVEAL; |
| 38 | private boolean mAwake = false; |
| 39 | |
| 40 | ImageRevealHelper(RevealStateListener listener) { |
| 41 | mRevealListener = listener; |
| 42 | mAnimator = ValueAnimator.ofFloat(); |
| 43 | mAnimator.setDuration(REVEAL_DURATION); |
| 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) { |
| 61 | if (!mIsCanceled) { |
| 62 | mAwake = !mAwake; |
| 63 | } |
| 64 | mIsCanceled = false; |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | private void animate() { |
| 70 | mAnimator.cancel(); |
| 71 | mAnimator.setFloatValues(mReveal, !mAwake ? MIN_REVEAL : MAX_REVEAL); |
| 72 | mAnimator.start(); |
| 73 | } |
| 74 | |
| 75 | public float getReveal() { |
| 76 | return mReveal; |
| 77 | } |
| 78 | |
| 79 | public boolean isAwake() { |
| 80 | return mAwake; |
| 81 | } |
| 82 | |
| 83 | void updateAwake(boolean awake) { |
| 84 | mAwake = awake; |
| 85 | animate(); |
| 86 | } |
| 87 | |
| 88 | void sleep() { |
| 89 | mReveal = MIN_REVEAL; |
| 90 | mAwake = false; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * A listener to trace value changes of reveal. |
| 95 | */ |
| 96 | public interface RevealStateListener { |
| 97 | |
| 98 | /** |
| 99 | * Called back while reveal status changes. |
| 100 | */ |
| 101 | void onRevealStateChanged(); |
| 102 | } |
| 103 | } |