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 | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 20 | import android.content.Context; |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 21 | import android.os.Bundle; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.SystemClock; |
Charles Chen | f3d295c | 2018-11-30 18:15:21 +0800 | [diff] [blame] | 24 | import android.util.MathUtils; |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame] | 25 | import android.util.TimeUtils; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 26 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 27 | import com.android.systemui.Dependency; |
Jorim Jaggi | ff46d4c | 2017-07-17 16:46:00 +0200 | [diff] [blame] | 28 | import com.android.systemui.Dumpable; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 29 | import com.android.systemui.Interpolators; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 30 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 31 | import com.android.systemui.statusbar.CommandQueue; |
| 32 | import com.android.systemui.statusbar.CommandQueue.Callbacks; |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 33 | import com.android.systemui.statusbar.policy.KeyguardStateController; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 34 | |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame] | 35 | import java.io.FileDescriptor; |
| 36 | import java.io.PrintWriter; |
| 37 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Class to control all aspects about light bar changes. |
| 40 | */ |
Lucas Dupin | 51d24e0 | 2018-11-13 10:46:51 -0800 | [diff] [blame] | 41 | public class LightBarTransitionsController implements Dumpable, Callbacks, |
| 42 | StatusBarStateController.StateListener { |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 43 | |
Matthew Ng | 355fe21 | 2018-12-14 17:42:38 -0800 | [diff] [blame] | 44 | public static final int DEFAULT_TINT_ANIMATION_DURATION = 120; |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 45 | private static final String EXTRA_DARK_INTENSITY = "dark_intensity"; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 46 | |
| 47 | private final Handler mHandler; |
| 48 | private final DarkIntensityApplier mApplier; |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 49 | private final KeyguardStateController mKeyguardStateController; |
Lucas Dupin | 51d24e0 | 2018-11-13 10:46:51 -0800 | [diff] [blame] | 50 | private final StatusBarStateController mStatusBarStateController; |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 51 | private final CommandQueue mCommandQueue; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 52 | |
| 53 | private boolean mTransitionDeferring; |
| 54 | private long mTransitionDeferringStartTime; |
| 55 | private long mTransitionDeferringDuration; |
| 56 | private boolean mTransitionPending; |
| 57 | private boolean mTintChangePending; |
| 58 | private float mPendingDarkIntensity; |
| 59 | private ValueAnimator mTintAnimator; |
| 60 | private float mDarkIntensity; |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 61 | private float mNextDarkIntensity; |
Lucas Dupin | 51d24e0 | 2018-11-13 10:46:51 -0800 | [diff] [blame] | 62 | private float mDozeAmount; |
Charles Chen | f3d295c | 2018-11-30 18:15:21 +0800 | [diff] [blame] | 63 | private int mDisplayId; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 64 | private final Runnable mTransitionDeferringDoneRunnable = new Runnable() { |
| 65 | @Override |
| 66 | public void run() { |
| 67 | mTransitionDeferring = false; |
| 68 | } |
| 69 | }; |
| 70 | |
Matthew Ng | 355fe21 | 2018-12-14 17:42:38 -0800 | [diff] [blame] | 71 | private final Context mContext; |
| 72 | |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 73 | public LightBarTransitionsController(Context context, DarkIntensityApplier applier, |
| 74 | CommandQueue commandQueue) { |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 75 | mApplier = applier; |
| 76 | mHandler = new Handler(); |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 77 | mKeyguardStateController = Dependency.get(KeyguardStateController.class); |
Lucas Dupin | 51d24e0 | 2018-11-13 10:46:51 -0800 | [diff] [blame] | 78 | mStatusBarStateController = Dependency.get(StatusBarStateController.class); |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 79 | mCommandQueue = commandQueue; |
| 80 | mCommandQueue.addCallback(this); |
Jason Monk | af08c15 | 2018-12-04 11:12:39 -0500 | [diff] [blame] | 81 | mStatusBarStateController.addCallback(this); |
Lucas Dupin | 51d24e0 | 2018-11-13 10:46:51 -0800 | [diff] [blame] | 82 | mDozeAmount = mStatusBarStateController.getDozeAmount(); |
Matthew Ng | 355fe21 | 2018-12-14 17:42:38 -0800 | [diff] [blame] | 83 | mContext = context; |
Charles Chen | f3d295c | 2018-11-30 18:15:21 +0800 | [diff] [blame] | 84 | mDisplayId = mContext.getDisplayId(); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | public void destroy(Context context) { |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 88 | mCommandQueue.removeCallback(this); |
Jason Monk | af08c15 | 2018-12-04 11:12:39 -0500 | [diff] [blame] | 89 | mStatusBarStateController.removeCallback(this); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 92 | public void saveState(Bundle outState) { |
| 93 | float intensity = mTintAnimator != null && mTintAnimator.isRunning() |
| 94 | ? mNextDarkIntensity : mDarkIntensity; |
| 95 | outState.putFloat(EXTRA_DARK_INTENSITY, intensity); |
| 96 | } |
| 97 | |
| 98 | public void restoreState(Bundle savedInstanceState) { |
| 99 | setIconTintInternal(savedInstanceState.getFloat(EXTRA_DARK_INTENSITY, 0)); |
| 100 | } |
| 101 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 102 | @Override |
Charles Chen | f3d295c | 2018-11-30 18:15:21 +0800 | [diff] [blame] | 103 | public void appTransitionPending(int displayId, boolean forced) { |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 104 | if (mDisplayId != displayId || mKeyguardStateController.isKeyguardGoingAway() && !forced) { |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 105 | return; |
| 106 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 107 | mTransitionPending = true; |
| 108 | } |
| 109 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 110 | @Override |
Charles Chen | f3d295c | 2018-11-30 18:15:21 +0800 | [diff] [blame] | 111 | public void appTransitionCancelled(int displayId) { |
| 112 | if (mDisplayId != displayId) { |
| 113 | return; |
| 114 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 115 | if (mTransitionPending && mTintChangePending) { |
| 116 | mTintChangePending = false; |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 117 | animateIconTint(mPendingDarkIntensity, 0 /* delay */, |
| 118 | mApplier.getTintAnimationDuration()); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 119 | } |
| 120 | mTransitionPending = false; |
| 121 | } |
| 122 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 123 | @Override |
Charles Chen | f3d295c | 2018-11-30 18:15:21 +0800 | [diff] [blame] | 124 | public void appTransitionStarting(int displayId, long startTime, long duration, |
| 125 | boolean forced) { |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 126 | if (mDisplayId != displayId || mKeyguardStateController.isKeyguardGoingAway() && !forced) { |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 127 | return; |
| 128 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 129 | if (mTransitionPending && mTintChangePending) { |
| 130 | mTintChangePending = false; |
| 131 | animateIconTint(mPendingDarkIntensity, |
| 132 | Math.max(0, startTime - SystemClock.uptimeMillis()), |
| 133 | duration); |
| 134 | |
| 135 | } else if (mTransitionPending) { |
| 136 | |
| 137 | // If we don't have a pending tint change yet, the change might come in the future until |
| 138 | // startTime is reached. |
| 139 | mTransitionDeferring = true; |
| 140 | mTransitionDeferringStartTime = startTime; |
| 141 | mTransitionDeferringDuration = duration; |
| 142 | mHandler.removeCallbacks(mTransitionDeferringDoneRunnable); |
| 143 | mHandler.postAtTime(mTransitionDeferringDoneRunnable, startTime); |
| 144 | } |
| 145 | mTransitionPending = false; |
| 146 | } |
| 147 | |
| 148 | public void setIconsDark(boolean dark, boolean animate) { |
| 149 | if (!animate) { |
| 150 | setIconTintInternal(dark ? 1.0f : 0.0f); |
Jorim Jaggi | ff46d4c | 2017-07-17 16:46:00 +0200 | [diff] [blame] | 151 | mNextDarkIntensity = dark ? 1.0f : 0.0f; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 152 | } else if (mTransitionPending) { |
| 153 | deferIconTintChange(dark ? 1.0f : 0.0f); |
| 154 | } else if (mTransitionDeferring) { |
| 155 | animateIconTint(dark ? 1.0f : 0.0f, |
| 156 | Math.max(0, mTransitionDeferringStartTime - SystemClock.uptimeMillis()), |
| 157 | mTransitionDeferringDuration); |
| 158 | } else { |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 159 | animateIconTint(dark ? 1.0f : 0.0f, 0 /* delay */, mApplier.getTintAnimationDuration()); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | public float getCurrentDarkIntensity() { |
| 164 | return mDarkIntensity; |
| 165 | } |
| 166 | |
| 167 | private void deferIconTintChange(float darkIntensity) { |
| 168 | if (mTintChangePending && darkIntensity == mPendingDarkIntensity) { |
| 169 | return; |
| 170 | } |
| 171 | mTintChangePending = true; |
| 172 | mPendingDarkIntensity = darkIntensity; |
| 173 | } |
| 174 | |
| 175 | private void animateIconTint(float targetDarkIntensity, long delay, |
| 176 | long duration) { |
Jorim Jaggi | ff46d4c | 2017-07-17 16:46:00 +0200 | [diff] [blame] | 177 | if (mNextDarkIntensity == targetDarkIntensity) { |
| 178 | return; |
| 179 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 180 | if (mTintAnimator != null) { |
| 181 | mTintAnimator.cancel(); |
| 182 | } |
Jason Monk | 49fa016 | 2017-01-11 09:21:56 -0500 | [diff] [blame] | 183 | mNextDarkIntensity = targetDarkIntensity; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 184 | mTintAnimator = ValueAnimator.ofFloat(mDarkIntensity, targetDarkIntensity); |
| 185 | mTintAnimator.addUpdateListener( |
| 186 | animation -> setIconTintInternal((Float) animation.getAnimatedValue())); |
| 187 | mTintAnimator.setDuration(duration); |
| 188 | mTintAnimator.setStartDelay(delay); |
Lucas Dupin | 0c1157e | 2017-07-18 21:40:28 -0700 | [diff] [blame] | 189 | mTintAnimator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 190 | mTintAnimator.start(); |
| 191 | } |
| 192 | |
| 193 | private void setIconTintInternal(float darkIntensity) { |
| 194 | mDarkIntensity = darkIntensity; |
Lucas Dupin | 51d24e0 | 2018-11-13 10:46:51 -0800 | [diff] [blame] | 195 | dispatchDark(); |
| 196 | } |
| 197 | |
| 198 | private void dispatchDark() { |
| 199 | mApplier.applyDarkIntensity(MathUtils.lerp(mDarkIntensity, 0f, mDozeAmount)); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Adrian Roos | 1bf6077 | 2017-02-03 15:47:43 -0800 | [diff] [blame] | 202 | @Override |
| 203 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 204 | pw.print(" mTransitionDeferring="); pw.print(mTransitionDeferring); |
| 205 | if (mTransitionDeferring) { |
| 206 | pw.println(); |
| 207 | pw.print(" mTransitionDeferringStartTime="); |
| 208 | pw.println(TimeUtils.formatUptime(mTransitionDeferringStartTime)); |
| 209 | |
| 210 | pw.print(" mTransitionDeferringDuration="); |
| 211 | TimeUtils.formatDuration(mTransitionDeferringDuration, pw); |
| 212 | pw.println(); |
| 213 | } |
| 214 | pw.print(" mTransitionPending="); pw.print(mTransitionPending); |
| 215 | pw.print(" mTintChangePending="); pw.println(mTintChangePending); |
| 216 | |
| 217 | pw.print(" mPendingDarkIntensity="); pw.print(mPendingDarkIntensity); |
| 218 | pw.print(" mDarkIntensity="); pw.print(mDarkIntensity); |
| 219 | pw.print(" mNextDarkIntensity="); pw.println(mNextDarkIntensity); |
| 220 | } |
| 221 | |
Lucas Dupin | 51d24e0 | 2018-11-13 10:46:51 -0800 | [diff] [blame] | 222 | @Override |
| 223 | public void onStateChanged(int newState) { } |
| 224 | |
| 225 | @Override |
| 226 | public void onDozeAmountChanged(float linear, float eased) { |
| 227 | mDozeAmount = eased; |
| 228 | dispatchDark(); |
| 229 | } |
| 230 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 231 | /** |
| 232 | * Interface to apply a specific dark intensity. |
| 233 | */ |
| 234 | public interface DarkIntensityApplier { |
| 235 | void applyDarkIntensity(float darkIntensity); |
Winson Chung | e89f4b5 | 2019-03-29 15:01:49 -0700 | [diff] [blame] | 236 | int getTintAnimationDuration(); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 237 | } |
| 238 | } |