Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.media |
| 18 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 19 | import android.animation.Animator |
| 20 | import android.animation.AnimatorListenerAdapter |
| 21 | import android.animation.ValueAnimator |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 22 | import android.annotation.IntDef |
| 23 | import android.content.Context |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 24 | import android.graphics.Rect |
| 25 | import android.util.MathUtils |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 26 | import android.view.View |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 27 | import android.view.ViewGroup |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 28 | import android.view.ViewGroupOverlay |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 29 | import com.android.systemui.Interpolators |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame^] | 30 | import com.android.systemui.keyguard.WakefulnessLifecycle |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 31 | import com.android.systemui.plugins.statusbar.StatusBarStateController |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 32 | import com.android.systemui.statusbar.NotificationLockscreenUserManager |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 33 | import com.android.systemui.statusbar.StatusBarState |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 34 | import com.android.systemui.statusbar.SysuiStatusBarStateController |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 35 | import com.android.systemui.statusbar.notification.stack.StackStateAnimator |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 36 | import com.android.systemui.statusbar.phone.KeyguardBypassController |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 37 | import com.android.systemui.statusbar.policy.KeyguardStateController |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 38 | import com.android.systemui.util.animation.UniqueObjectHostView |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 39 | import javax.inject.Inject |
| 40 | import javax.inject.Singleton |
| 41 | |
| 42 | /** |
| 43 | * This manager is responsible for placement of the unique media view between the different hosts |
| 44 | * and animate the positions of the views to achieve seamless transitions. |
| 45 | */ |
| 46 | @Singleton |
| 47 | class MediaHierarchyManager @Inject constructor( |
| 48 | private val context: Context, |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 49 | private val statusBarStateController: SysuiStatusBarStateController, |
| 50 | private val keyguardStateController: KeyguardStateController, |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 51 | private val bypassController: KeyguardBypassController, |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 52 | private val mediaViewManager: MediaViewManager, |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame^] | 53 | private val notifLockscreenUserManager: NotificationLockscreenUserManager, |
| 54 | wakefulnessLifecycle: WakefulnessLifecycle |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 55 | ) { |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 56 | /** |
| 57 | * The root overlay of the hierarchy. This is where the media notification is attached to |
| 58 | * whenever the view is transitioning from one host to another. It also make sure that the |
| 59 | * view is always in its final state when it is attached to a view host. |
| 60 | */ |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 61 | private var rootOverlay: ViewGroupOverlay? = null |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 62 | |
| 63 | private var rootView: View? = null |
| 64 | private var currentBounds = Rect() |
| 65 | private var animationStartBounds: Rect = Rect() |
| 66 | private var targetBounds: Rect = Rect() |
| 67 | private val mediaFrame |
Robert Snoeberger | 52dfb62 | 2020-05-21 16:31:29 -0400 | [diff] [blame] | 68 | get() = mediaViewManager.mediaFrame |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 69 | private var statusbarState: Int = statusBarStateController.state |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 70 | private var animator = ValueAnimator.ofFloat(0.0f, 1.0f).apply { |
| 71 | interpolator = Interpolators.FAST_OUT_SLOW_IN |
| 72 | addUpdateListener { |
| 73 | updateTargetState() |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 74 | interpolateBounds(animationStartBounds, targetBounds, animatedFraction, |
| 75 | result = currentBounds) |
| 76 | applyState(currentBounds) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 77 | } |
| 78 | addListener(object : AnimatorListenerAdapter() { |
| 79 | private var cancelled: Boolean = false |
| 80 | |
| 81 | override fun onAnimationCancel(animation: Animator?) { |
| 82 | cancelled = true |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 83 | animationPending = false |
| 84 | rootView?.removeCallbacks(startAnimation) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 85 | } |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 86 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 87 | override fun onAnimationEnd(animation: Animator?) { |
| 88 | if (!cancelled) { |
| 89 | applyTargetStateIfNotAnimating() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | override fun onAnimationStart(animation: Animator?) { |
| 94 | cancelled = false |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 95 | animationPending = false |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 96 | } |
| 97 | }) |
| 98 | } |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 99 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 100 | private val mediaHosts = arrayOfNulls<MediaHost>(LOCATION_LOCKSCREEN + 1) |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 101 | /** |
| 102 | * The last location where this view was at before going to the desired location. This is |
| 103 | * useful for guided transitions. |
| 104 | */ |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 105 | @MediaLocation |
| 106 | private var previousLocation = -1 |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 107 | /** |
| 108 | * The desired location where the view will be at the end of the transition. |
| 109 | */ |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 110 | @MediaLocation |
| 111 | private var desiredLocation = -1 |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * The current attachment location where the view is currently attached. |
| 115 | * Usually this matches the desired location except for animations whenever a view moves |
| 116 | * to the new desired location, during which it is in [IN_OVERLAY]. |
| 117 | */ |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 118 | @MediaLocation |
| 119 | private var currentAttachmentLocation = -1 |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 120 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Are we currently waiting on an animation to start? |
| 123 | */ |
| 124 | private var animationPending: Boolean = false |
| 125 | private val startAnimation: Runnable = Runnable { animator.start() } |
| 126 | |
| 127 | /** |
| 128 | * The expansion of quick settings |
| 129 | */ |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 130 | var qsExpansion: Float = 0.0f |
| 131 | set(value) { |
| 132 | if (field != value) { |
| 133 | field = value |
| 134 | updateDesiredLocation() |
| 135 | if (getQSTransformationProgress() >= 0) { |
| 136 | updateTargetState() |
| 137 | applyTargetStateIfNotAnimating() |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame^] | 142 | /** |
| 143 | * Are location changes currently blocked? |
| 144 | */ |
| 145 | private val blockLocationChanges: Boolean |
| 146 | get() { |
| 147 | return goingToSleep || dozeAnimationRunning |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Are we currently going to sleep |
| 152 | */ |
| 153 | private var goingToSleep: Boolean = false |
| 154 | set(value) { |
| 155 | if (field != value) { |
| 156 | field = value |
| 157 | if (!value) { |
| 158 | updateDesiredLocation() |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Is the doze animation currently Running |
| 165 | */ |
| 166 | private var dozeAnimationRunning: Boolean = false |
| 167 | private set(value) { |
| 168 | if (field != value) { |
| 169 | field = value |
| 170 | if (!value) { |
| 171 | updateDesiredLocation() |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 176 | init { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 177 | statusBarStateController.addCallback(object : StatusBarStateController.StateListener { |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 178 | override fun onStatePreChange(oldState: Int, newState: Int) { |
| 179 | // We're updating the location before the state change happens, since we want the |
| 180 | // location of the previous state to still be up to date when the animation starts |
| 181 | statusbarState = newState |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 182 | updateDesiredLocation() |
| 183 | } |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 184 | |
| 185 | override fun onStateChanged(newState: Int) { |
| 186 | updateTargetState() |
| 187 | } |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame^] | 188 | |
| 189 | override fun onDozeAmountChanged(linear: Float, eased: Float) { |
| 190 | dozeAnimationRunning = linear != 0.0f && linear != 1.0f |
| 191 | } |
| 192 | |
| 193 | override fun onDozingChanged(isDozing: Boolean) { |
| 194 | if (!isDozing) { |
| 195 | dozeAnimationRunning = false |
| 196 | } |
| 197 | } |
| 198 | }) |
| 199 | |
| 200 | wakefulnessLifecycle.addObserver(object : WakefulnessLifecycle.Observer { |
| 201 | override fun onFinishedGoingToSleep() { |
| 202 | goingToSleep = false |
| 203 | } |
| 204 | |
| 205 | override fun onStartedGoingToSleep() { |
| 206 | goingToSleep = true |
| 207 | } |
| 208 | |
| 209 | override fun onFinishedWakingUp() { |
| 210 | goingToSleep = false |
| 211 | } |
| 212 | |
| 213 | override fun onStartedWakingUp() { |
| 214 | goingToSleep = false |
| 215 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 216 | }) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 219 | /** |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 220 | * Register a media host and create a view can be attached to a view hierarchy |
| 221 | * and where the players will be placed in when the host is the currently desired state. |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 222 | * |
| 223 | * @return the hostView associated with this location |
| 224 | */ |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 225 | fun register(mediaObject: MediaHost): UniqueObjectHostView { |
| 226 | val viewHost = createUniqueObjectHost() |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 227 | mediaObject.hostView = viewHost |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 228 | mediaHosts[mediaObject.location] = mediaObject |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 229 | if (mediaObject.location == desiredLocation) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 230 | // In case we are overriding a view that is already visible, make sure we attach it |
| 231 | // to this new host view in the below call |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 232 | desiredLocation = -1 |
| 233 | } |
| 234 | if (mediaObject.location == currentAttachmentLocation) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 235 | currentAttachmentLocation = -1 |
| 236 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 237 | updateDesiredLocation() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 238 | return viewHost |
| 239 | } |
| 240 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 241 | private fun createUniqueObjectHost(): UniqueObjectHostView { |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 242 | val viewHost = UniqueObjectHostView(context) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 243 | viewHost.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { |
| 244 | override fun onViewAttachedToWindow(p0: View?) { |
| 245 | if (rootOverlay == null) { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 246 | rootView = viewHost.viewRootImpl.view |
| 247 | rootOverlay = (rootView!!.overlay as ViewGroupOverlay) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 248 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 249 | viewHost.removeOnAttachStateChangeListener(this) |
| 250 | } |
| 251 | |
| 252 | override fun onViewDetachedFromWindow(p0: View?) { |
| 253 | } |
| 254 | }) |
| 255 | return viewHost |
| 256 | } |
| 257 | |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Updates the location that the view should be in. If it changes, an animation may be triggered |
| 260 | * going from the old desired location to the new one. |
| 261 | */ |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 262 | private fun updateDesiredLocation() { |
| 263 | val desiredLocation = calculateLocation() |
| 264 | if (desiredLocation != this.desiredLocation) { |
| 265 | if (this.desiredLocation >= 0) { |
| 266 | previousLocation = this.desiredLocation |
| 267 | } |
| 268 | val isNewView = this.desiredLocation == -1 |
| 269 | this.desiredLocation = desiredLocation |
| 270 | // Let's perform a transition |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 271 | val animate = shouldAnimateTransition(desiredLocation, previousLocation) |
| 272 | val (animDuration, delay) = getAnimationParams(previousLocation, desiredLocation) |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 273 | val host = getHost(desiredLocation) |
| 274 | mediaViewManager.onDesiredLocationChanged(desiredLocation, host, animate, animDuration, |
| 275 | delay) |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 276 | performTransitionToNewLocation(isNewView, animate) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 280 | private fun performTransitionToNewLocation(isNewView: Boolean, animate: Boolean) { |
| 281 | if (previousLocation < 0 || isNewView) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 282 | cancelAnimationAndApplyDesiredState() |
| 283 | return |
| 284 | } |
| 285 | val currentHost = getHost(desiredLocation) |
| 286 | val previousHost = getHost(previousLocation) |
| 287 | if (currentHost == null || previousHost == null) { |
| 288 | cancelAnimationAndApplyDesiredState() |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 289 | return |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 290 | } |
| 291 | updateTargetState() |
| 292 | if (isCurrentlyInGuidedTransformation()) { |
| 293 | applyTargetStateIfNotAnimating() |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 294 | } else if (animate) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 295 | animator.cancel() |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 296 | if (currentAttachmentLocation == IN_OVERLAY || |
| 297 | !previousHost.hostView.isAttachedToWindow) { |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 298 | // Let's animate to the new position, starting from the current position |
| 299 | // We also go in here in case the view was detached, since the bounds wouldn't |
| 300 | // be correct anymore |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 301 | animationStartBounds.set(currentBounds) |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 302 | } else { |
| 303 | // otherwise, let's take the freshest state, since the current one could |
| 304 | // be outdated |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 305 | animationStartBounds.set(previousHost.currentBounds) |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 306 | } |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 307 | adjustAnimatorForTransition(desiredLocation, previousLocation) |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 308 | rootView?.let { |
| 309 | // Let's delay the animation start until we finished laying out |
| 310 | animationPending = true |
| 311 | it.postOnAnimation(startAnimation) |
| 312 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 313 | } else { |
| 314 | cancelAnimationAndApplyDesiredState() |
| 315 | } |
| 316 | } |
| 317 | |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 318 | private fun shouldAnimateTransition( |
| 319 | @MediaLocation currentLocation: Int, |
| 320 | @MediaLocation previousLocation: Int |
| 321 | ): Boolean { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 322 | if (isCurrentlyInGuidedTransformation()) { |
| 323 | return false |
| 324 | } |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 325 | if (currentLocation == LOCATION_QQS && |
| 326 | previousLocation == LOCATION_LOCKSCREEN && |
| 327 | (statusBarStateController.leaveOpenOnKeyguardHide() || |
| 328 | statusbarState == StatusBarState.SHADE_LOCKED)) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 329 | // Usually listening to the isShown is enough to determine this, but there is some |
| 330 | // non-trivial reattaching logic happening that will make the view not-shown earlier |
| 331 | return true |
| 332 | } |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 333 | return mediaFrame.isShown || animator.isRunning || animationPending |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 336 | private fun adjustAnimatorForTransition(desiredLocation: Int, previousLocation: Int) { |
| 337 | val (animDuration, delay) = getAnimationParams(previousLocation, desiredLocation) |
| 338 | animator.apply { |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 339 | duration = animDuration |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 340 | startDelay = delay |
| 341 | } |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | private fun getAnimationParams(previousLocation: Int, desiredLocation: Int): Pair<Long, Long> { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 345 | var animDuration = 200L |
| 346 | var delay = 0L |
| 347 | if (previousLocation == LOCATION_LOCKSCREEN && desiredLocation == LOCATION_QQS) { |
| 348 | // Going to the full shade, let's adjust the animation duration |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 349 | if (statusbarState == StatusBarState.SHADE && |
| 350 | keyguardStateController.isKeyguardFadingAway) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 351 | delay = keyguardStateController.keyguardFadingAwayDelay |
| 352 | } |
| 353 | animDuration = StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE.toLong() |
| 354 | } else if (previousLocation == LOCATION_QQS && desiredLocation == LOCATION_LOCKSCREEN) { |
| 355 | animDuration = StackStateAnimator.ANIMATION_DURATION_APPEAR_DISAPPEAR.toLong() |
| 356 | } |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 357 | return animDuration to delay |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | private fun applyTargetStateIfNotAnimating() { |
| 361 | if (!animator.isRunning) { |
| 362 | // Let's immediately apply the target state (which is interpolated) if there is |
| 363 | // no animation running. Otherwise the animation update will already update |
| 364 | // the location |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 365 | applyState(targetBounds) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 369 | /** |
| 370 | * Updates the state that the view wants to be in at the end of the animation. |
| 371 | */ |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 372 | private fun updateTargetState() { |
| 373 | if (isCurrentlyInGuidedTransformation()) { |
| 374 | val progress = getTransformationProgress() |
| 375 | val currentHost = getHost(desiredLocation)!! |
| 376 | val previousHost = getHost(previousLocation)!! |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 377 | val newBounds = currentHost.currentBounds |
| 378 | val previousBounds = previousHost.currentBounds |
| 379 | targetBounds = interpolateBounds(previousBounds, newBounds, progress) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 380 | } else { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 381 | val bounds = getHost(desiredLocation)?.currentBounds ?: return |
| 382 | targetBounds.set(bounds) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 386 | private fun interpolateBounds( |
| 387 | startBounds: Rect, |
| 388 | endBounds: Rect, |
| 389 | progress: Float, |
| 390 | result: Rect? = null |
| 391 | ): Rect { |
| 392 | val left = MathUtils.lerp(startBounds.left.toFloat(), |
| 393 | endBounds.left.toFloat(), progress).toInt() |
| 394 | val top = MathUtils.lerp(startBounds.top.toFloat(), |
| 395 | endBounds.top.toFloat(), progress).toInt() |
| 396 | val right = MathUtils.lerp(startBounds.right.toFloat(), |
| 397 | endBounds.right.toFloat(), progress).toInt() |
| 398 | val bottom = MathUtils.lerp(startBounds.bottom.toFloat(), |
| 399 | endBounds.bottom.toFloat(), progress).toInt() |
| 400 | val resultBounds = result ?: Rect() |
| 401 | resultBounds.set(left, top, right, bottom) |
| 402 | return resultBounds |
| 403 | } |
| 404 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 405 | /** |
| 406 | * @return true if this transformation is guided by an external progress like a finger |
| 407 | */ |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 408 | private fun isCurrentlyInGuidedTransformation(): Boolean { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 409 | return getTransformationProgress() >= 0 |
| 410 | } |
| 411 | |
| 412 | /** |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 413 | * @return the current transformation progress if we're in a guided transformation and -1 |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 414 | * otherwise |
| 415 | */ |
| 416 | private fun getTransformationProgress(): Float { |
| 417 | val progress = getQSTransformationProgress() |
| 418 | if (progress >= 0) { |
| 419 | return progress |
| 420 | } |
| 421 | return -1.0f |
| 422 | } |
| 423 | |
| 424 | private fun getQSTransformationProgress(): Float { |
| 425 | val currentHost = getHost(desiredLocation) |
| 426 | val previousHost = getHost(previousLocation) |
| 427 | if (currentHost?.location == LOCATION_QS) { |
| 428 | if (previousHost?.location == LOCATION_QQS) { |
| 429 | return qsExpansion |
| 430 | } |
| 431 | } |
| 432 | return -1.0f |
| 433 | } |
| 434 | |
| 435 | private fun getHost(@MediaLocation location: Int): MediaHost? { |
| 436 | if (location < 0) { |
| 437 | return null |
| 438 | } |
| 439 | return mediaHosts[location] |
| 440 | } |
| 441 | |
| 442 | private fun cancelAnimationAndApplyDesiredState() { |
| 443 | animator.cancel() |
| 444 | getHost(desiredLocation)?.let { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 445 | applyState(it.currentBounds, immediately = true) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 449 | /** |
| 450 | * Apply the current state to the view, updating it's bounds and desired state |
| 451 | */ |
| 452 | private fun applyState(bounds: Rect, immediately: Boolean = false) { |
| 453 | currentBounds.set(bounds) |
| 454 | val currentlyInGuidedTransformation = isCurrentlyInGuidedTransformation() |
| 455 | val startLocation = if (currentlyInGuidedTransformation) previousLocation else -1 |
| 456 | val progress = if (currentlyInGuidedTransformation) getTransformationProgress() else 1.0f |
| 457 | val endLocation = desiredLocation |
| 458 | mediaViewManager.setCurrentState(startLocation, endLocation, progress, immediately) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 459 | updateHostAttachment() |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 460 | if (currentAttachmentLocation == IN_OVERLAY) { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 461 | mediaFrame.setLeftTopRightBottom( |
| 462 | currentBounds.left, |
| 463 | currentBounds.top, |
| 464 | currentBounds.right, |
| 465 | currentBounds.bottom) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 466 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | private fun updateHostAttachment() { |
| 470 | val inOverlay = isTransitionRunning() && rootOverlay != null |
| 471 | val newLocation = if (inOverlay) IN_OVERLAY else desiredLocation |
| 472 | if (currentAttachmentLocation != newLocation) { |
| 473 | currentAttachmentLocation = newLocation |
| 474 | |
| 475 | // Remove the carousel from the old host |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 476 | (mediaFrame.parent as ViewGroup?)?.removeView(mediaFrame) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 477 | |
| 478 | // Add it to the new one |
| 479 | val targetHost = getHost(desiredLocation)!!.hostView |
| 480 | if (inOverlay) { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 481 | rootOverlay!!.add(mediaFrame) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 482 | } else { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 483 | targetHost.addView(mediaFrame) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 488 | private fun isTransitionRunning(): Boolean { |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 489 | return isCurrentlyInGuidedTransformation() && getTransformationProgress() != 1.0f || |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 490 | animator.isRunning || animationPending |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 493 | @MediaLocation |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 494 | private fun calculateLocation(): Int { |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame^] | 495 | if (blockLocationChanges) { |
| 496 | // Keep the current location until we're allowed to again |
| 497 | return desiredLocation |
| 498 | } |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 499 | val onLockscreen = (!bypassController.bypassEnabled && |
| 500 | (statusbarState == StatusBarState.KEYGUARD || |
| 501 | statusbarState == StatusBarState.FULLSCREEN_USER_SWITCHER)) |
| 502 | val allowedOnLockscreen = notifLockscreenUserManager.shouldShowLockscreenNotifications() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 503 | return when { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 504 | qsExpansion > 0.0f && !onLockscreen -> LOCATION_QS |
| 505 | qsExpansion > 0.4f && onLockscreen -> LOCATION_QS |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 506 | onLockscreen && allowedOnLockscreen -> LOCATION_LOCKSCREEN |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 507 | else -> LOCATION_QQS |
| 508 | } |
| 509 | } |
| 510 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 511 | companion object { |
| 512 | /** |
| 513 | * Attached in expanded quick settings |
| 514 | */ |
| 515 | const val LOCATION_QS = 0 |
| 516 | |
| 517 | /** |
| 518 | * Attached in the collapsed QS |
| 519 | */ |
| 520 | const val LOCATION_QQS = 1 |
| 521 | |
| 522 | /** |
| 523 | * Attached on the lock screen |
| 524 | */ |
| 525 | const val LOCATION_LOCKSCREEN = 2 |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 526 | |
| 527 | /** |
| 528 | * Attached at the root of the hierarchy in an overlay |
| 529 | */ |
| 530 | const val IN_OVERLAY = -1000 |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 531 | } |
Robert Snoeberger | 52dfb62 | 2020-05-21 16:31:29 -0400 | [diff] [blame] | 532 | } |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 533 | |
| 534 | @IntDef(prefix = ["LOCATION_"], value = [MediaHierarchyManager.LOCATION_QS, |
| 535 | MediaHierarchyManager.LOCATION_QQS, MediaHierarchyManager.LOCATION_LOCKSCREEN]) |
| 536 | @Retention(AnnotationRetention.SOURCE) |
| 537 | annotation class MediaLocation |