Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.statusbar.phone; |
| 18 | |
| 19 | import android.animation.ValueAnimator; |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 20 | import android.os.Bundle; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
| 22 | import android.os.SystemClock; |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame^] | 23 | import android.util.TimeUtils; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 24 | |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame^] | 25 | import com.android.systemui.Dumpable; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 26 | import com.android.systemui.Interpolators; |
| 27 | |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame^] | 28 | import java.io.FileDescriptor; |
| 29 | import java.io.PrintWriter; |
| 30 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Class to control all aspects about light bar changes. |
| 33 | */ |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame^] | 34 | public class LightBarTransitionsController implements Dumpable { |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 35 | |
| 36 | public static final long DEFAULT_TINT_ANIMATION_DURATION = 120; |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 37 | private static final String EXTRA_DARK_INTENSITY = "dark_intensity"; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 38 | |
| 39 | private final Handler mHandler; |
| 40 | private final DarkIntensityApplier mApplier; |
| 41 | |
| 42 | private boolean mTransitionDeferring; |
| 43 | private long mTransitionDeferringStartTime; |
| 44 | private long mTransitionDeferringDuration; |
| 45 | private boolean mTransitionPending; |
| 46 | private boolean mTintChangePending; |
| 47 | private float mPendingDarkIntensity; |
| 48 | private ValueAnimator mTintAnimator; |
| 49 | private float mDarkIntensity; |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 50 | private float mNextDarkIntensity; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 51 | |
| 52 | private final Runnable mTransitionDeferringDoneRunnable = new Runnable() { |
| 53 | @Override |
| 54 | public void run() { |
| 55 | mTransitionDeferring = false; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | public LightBarTransitionsController(DarkIntensityApplier applier) { |
| 60 | mApplier = applier; |
| 61 | mHandler = new Handler(); |
| 62 | } |
| 63 | |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 64 | public void saveState(Bundle outState) { |
| 65 | float intensity = mTintAnimator != null && mTintAnimator.isRunning() |
| 66 | ? mNextDarkIntensity : mDarkIntensity; |
| 67 | outState.putFloat(EXTRA_DARK_INTENSITY, intensity); |
| 68 | } |
| 69 | |
| 70 | public void restoreState(Bundle savedInstanceState) { |
| 71 | setIconTintInternal(savedInstanceState.getFloat(EXTRA_DARK_INTENSITY, 0)); |
| 72 | } |
| 73 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 74 | public void appTransitionPending() { |
| 75 | mTransitionPending = true; |
| 76 | } |
| 77 | |
| 78 | public void appTransitionCancelled() { |
| 79 | if (mTransitionPending && mTintChangePending) { |
| 80 | mTintChangePending = false; |
| 81 | animateIconTint(mPendingDarkIntensity, 0 /* delay */, DEFAULT_TINT_ANIMATION_DURATION); |
| 82 | } |
| 83 | mTransitionPending = false; |
| 84 | } |
| 85 | |
| 86 | public void appTransitionStarting(long startTime, long duration) { |
| 87 | if (mTransitionPending && mTintChangePending) { |
| 88 | mTintChangePending = false; |
| 89 | animateIconTint(mPendingDarkIntensity, |
| 90 | Math.max(0, startTime - SystemClock.uptimeMillis()), |
| 91 | duration); |
| 92 | |
| 93 | } else if (mTransitionPending) { |
| 94 | |
| 95 | // If we don't have a pending tint change yet, the change might come in the future until |
| 96 | // startTime is reached. |
| 97 | mTransitionDeferring = true; |
| 98 | mTransitionDeferringStartTime = startTime; |
| 99 | mTransitionDeferringDuration = duration; |
| 100 | mHandler.removeCallbacks(mTransitionDeferringDoneRunnable); |
| 101 | mHandler.postAtTime(mTransitionDeferringDoneRunnable, startTime); |
| 102 | } |
| 103 | mTransitionPending = false; |
| 104 | } |
| 105 | |
| 106 | public void setIconsDark(boolean dark, boolean animate) { |
| 107 | if (!animate) { |
| 108 | setIconTintInternal(dark ? 1.0f : 0.0f); |
| 109 | } else if (mTransitionPending) { |
| 110 | deferIconTintChange(dark ? 1.0f : 0.0f); |
| 111 | } else if (mTransitionDeferring) { |
| 112 | animateIconTint(dark ? 1.0f : 0.0f, |
| 113 | Math.max(0, mTransitionDeferringStartTime - SystemClock.uptimeMillis()), |
| 114 | mTransitionDeferringDuration); |
| 115 | } else { |
| 116 | animateIconTint(dark ? 1.0f : 0.0f, 0 /* delay */, DEFAULT_TINT_ANIMATION_DURATION); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public float getCurrentDarkIntensity() { |
| 121 | return mDarkIntensity; |
| 122 | } |
| 123 | |
| 124 | private void deferIconTintChange(float darkIntensity) { |
| 125 | if (mTintChangePending && darkIntensity == mPendingDarkIntensity) { |
| 126 | return; |
| 127 | } |
| 128 | mTintChangePending = true; |
| 129 | mPendingDarkIntensity = darkIntensity; |
| 130 | } |
| 131 | |
| 132 | private void animateIconTint(float targetDarkIntensity, long delay, |
| 133 | long duration) { |
| 134 | if (mTintAnimator != null) { |
| 135 | mTintAnimator.cancel(); |
| 136 | } |
| 137 | if (mDarkIntensity == targetDarkIntensity) { |
| 138 | return; |
| 139 | } |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 140 | mNextDarkIntensity = targetDarkIntensity; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 141 | mTintAnimator = ValueAnimator.ofFloat(mDarkIntensity, targetDarkIntensity); |
| 142 | mTintAnimator.addUpdateListener( |
| 143 | animation -> setIconTintInternal((Float) animation.getAnimatedValue())); |
| 144 | mTintAnimator.setDuration(duration); |
| 145 | mTintAnimator.setStartDelay(delay); |
| 146 | mTintAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
| 147 | mTintAnimator.start(); |
| 148 | } |
| 149 | |
| 150 | private void setIconTintInternal(float darkIntensity) { |
| 151 | mDarkIntensity = darkIntensity; |
| 152 | mApplier.applyDarkIntensity(darkIntensity); |
| 153 | } |
| 154 | |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame^] | 155 | @Override |
| 156 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 157 | pw.print(" mTransitionDeferring="); pw.print(mTransitionDeferring); |
| 158 | if (mTransitionDeferring) { |
| 159 | pw.println(); |
| 160 | pw.print(" mTransitionDeferringStartTime="); |
| 161 | pw.println(TimeUtils.formatUptime(mTransitionDeferringStartTime)); |
| 162 | |
| 163 | pw.print(" mTransitionDeferringDuration="); |
| 164 | TimeUtils.formatDuration(mTransitionDeferringDuration, pw); |
| 165 | pw.println(); |
| 166 | } |
| 167 | pw.print(" mTransitionPending="); pw.print(mTransitionPending); |
| 168 | pw.print(" mTintChangePending="); pw.println(mTintChangePending); |
| 169 | |
| 170 | pw.print(" mPendingDarkIntensity="); pw.print(mPendingDarkIntensity); |
| 171 | pw.print(" mDarkIntensity="); pw.print(mDarkIntensity); |
| 172 | pw.print(" mNextDarkIntensity="); pw.println(mNextDarkIntensity); |
| 173 | } |
| 174 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Interface to apply a specific dark intensity. |
| 177 | */ |
| 178 | public interface DarkIntensityApplier { |
| 179 | void applyDarkIntensity(float darkIntensity); |
| 180 | } |
| 181 | } |