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 | afae4e7 | 2020-06-16 18:21:41 -0700 | [diff] [blame] | 52 | private val mediaCarouselController: MediaCarouselController, |
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 |
Selim Cinek | afae4e7 | 2020-06-16 18:21:41 -0700 | [diff] [blame] | 68 | get() = mediaCarouselController.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 | /** |
Selim Cinek | f630a82 | 2020-06-22 11:26:09 -0700 | [diff] [blame] | 143 | * Is the shade currently collapsing from the expanded qs? If we're on the lockscreen and in qs, |
| 144 | * we wouldn't want to transition in that case. |
| 145 | */ |
| 146 | var collapsingShadeFromQS: Boolean = false |
| 147 | set(value) { |
| 148 | if (field != value) { |
| 149 | field = value |
| 150 | updateDesiredLocation(forceNoAnimation = true) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 155 | * Are location changes currently blocked? |
| 156 | */ |
| 157 | private val blockLocationChanges: Boolean |
| 158 | get() { |
| 159 | return goingToSleep || dozeAnimationRunning |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Are we currently going to sleep |
| 164 | */ |
| 165 | private var goingToSleep: Boolean = false |
| 166 | set(value) { |
| 167 | if (field != value) { |
| 168 | field = value |
| 169 | if (!value) { |
| 170 | updateDesiredLocation() |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
Selim Cinek | f630a82 | 2020-06-22 11:26:09 -0700 | [diff] [blame] | 176 | * Are we currently fullyAwake |
| 177 | */ |
| 178 | private var fullyAwake: Boolean = false |
| 179 | set(value) { |
| 180 | if (field != value) { |
| 181 | field = value |
| 182 | if (value) { |
| 183 | updateDesiredLocation(forceNoAnimation = true) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 189 | * Is the doze animation currently Running |
| 190 | */ |
| 191 | private var dozeAnimationRunning: Boolean = false |
| 192 | private set(value) { |
| 193 | if (field != value) { |
| 194 | field = value |
| 195 | if (!value) { |
| 196 | updateDesiredLocation() |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 201 | init { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 202 | statusBarStateController.addCallback(object : StatusBarStateController.StateListener { |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 203 | override fun onStatePreChange(oldState: Int, newState: Int) { |
| 204 | // We're updating the location before the state change happens, since we want the |
| 205 | // location of the previous state to still be up to date when the animation starts |
| 206 | statusbarState = newState |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 207 | updateDesiredLocation() |
| 208 | } |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 209 | |
| 210 | override fun onStateChanged(newState: Int) { |
| 211 | updateTargetState() |
| 212 | } |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 213 | |
| 214 | override fun onDozeAmountChanged(linear: Float, eased: Float) { |
| 215 | dozeAnimationRunning = linear != 0.0f && linear != 1.0f |
| 216 | } |
| 217 | |
| 218 | override fun onDozingChanged(isDozing: Boolean) { |
| 219 | if (!isDozing) { |
| 220 | dozeAnimationRunning = false |
Lucas Dupin | 7d52dd6 | 2020-06-18 18:11:22 -0700 | [diff] [blame] | 221 | } else { |
| 222 | updateDesiredLocation() |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | }) |
| 226 | |
| 227 | wakefulnessLifecycle.addObserver(object : WakefulnessLifecycle.Observer { |
| 228 | override fun onFinishedGoingToSleep() { |
| 229 | goingToSleep = false |
| 230 | } |
| 231 | |
| 232 | override fun onStartedGoingToSleep() { |
| 233 | goingToSleep = true |
Selim Cinek | f630a82 | 2020-06-22 11:26:09 -0700 | [diff] [blame] | 234 | fullyAwake = false |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | override fun onFinishedWakingUp() { |
| 238 | goingToSleep = false |
Selim Cinek | f630a82 | 2020-06-22 11:26:09 -0700 | [diff] [blame] | 239 | fullyAwake = true |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | override fun onStartedWakingUp() { |
| 243 | goingToSleep = false |
| 244 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 245 | }) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 248 | /** |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 249 | * Register a media host and create a view can be attached to a view hierarchy |
| 250 | * 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] | 251 | * |
| 252 | * @return the hostView associated with this location |
| 253 | */ |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 254 | fun register(mediaObject: MediaHost): UniqueObjectHostView { |
| 255 | val viewHost = createUniqueObjectHost() |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 256 | mediaObject.hostView = viewHost |
Selim Cinek | 7ba605b | 2020-06-19 18:34:23 -0700 | [diff] [blame] | 257 | mediaObject.addVisibilityChangeListener { |
| 258 | // Never animate because of a visibility change, only state changes should do that |
| 259 | updateDesiredLocation(forceNoAnimation = true) |
| 260 | } |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 261 | mediaHosts[mediaObject.location] = mediaObject |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 262 | if (mediaObject.location == desiredLocation) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 263 | // In case we are overriding a view that is already visible, make sure we attach it |
| 264 | // to this new host view in the below call |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 265 | desiredLocation = -1 |
| 266 | } |
| 267 | if (mediaObject.location == currentAttachmentLocation) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 268 | currentAttachmentLocation = -1 |
| 269 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 270 | updateDesiredLocation() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 271 | return viewHost |
| 272 | } |
| 273 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 274 | private fun createUniqueObjectHost(): UniqueObjectHostView { |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 275 | val viewHost = UniqueObjectHostView(context) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 276 | viewHost.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { |
| 277 | override fun onViewAttachedToWindow(p0: View?) { |
| 278 | if (rootOverlay == null) { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 279 | rootView = viewHost.viewRootImpl.view |
| 280 | rootOverlay = (rootView!!.overlay as ViewGroupOverlay) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 281 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 282 | viewHost.removeOnAttachStateChangeListener(this) |
| 283 | } |
| 284 | |
| 285 | override fun onViewDetachedFromWindow(p0: View?) { |
| 286 | } |
| 287 | }) |
| 288 | return viewHost |
| 289 | } |
| 290 | |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Updates the location that the view should be in. If it changes, an animation may be triggered |
| 293 | * going from the old desired location to the new one. |
Selim Cinek | 7ba605b | 2020-06-19 18:34:23 -0700 | [diff] [blame] | 294 | * |
| 295 | * @param forceNoAnimation optional parameter telling the system not to animate |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 296 | */ |
Selim Cinek | 7ba605b | 2020-06-19 18:34:23 -0700 | [diff] [blame] | 297 | private fun updateDesiredLocation(forceNoAnimation: Boolean = false) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 298 | val desiredLocation = calculateLocation() |
| 299 | if (desiredLocation != this.desiredLocation) { |
| 300 | if (this.desiredLocation >= 0) { |
| 301 | previousLocation = this.desiredLocation |
| 302 | } |
| 303 | val isNewView = this.desiredLocation == -1 |
| 304 | this.desiredLocation = desiredLocation |
| 305 | // Let's perform a transition |
Selim Cinek | 7ba605b | 2020-06-19 18:34:23 -0700 | [diff] [blame] | 306 | val animate = !forceNoAnimation && |
| 307 | shouldAnimateTransition(desiredLocation, previousLocation) |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 308 | val (animDuration, delay) = getAnimationParams(previousLocation, desiredLocation) |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 309 | val host = getHost(desiredLocation) |
Selim Cinek | afae4e7 | 2020-06-16 18:21:41 -0700 | [diff] [blame] | 310 | mediaCarouselController.onDesiredLocationChanged(desiredLocation, host, animate, |
| 311 | animDuration, delay) |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 312 | performTransitionToNewLocation(isNewView, animate) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 316 | private fun performTransitionToNewLocation(isNewView: Boolean, animate: Boolean) { |
| 317 | if (previousLocation < 0 || isNewView) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 318 | cancelAnimationAndApplyDesiredState() |
| 319 | return |
| 320 | } |
| 321 | val currentHost = getHost(desiredLocation) |
| 322 | val previousHost = getHost(previousLocation) |
| 323 | if (currentHost == null || previousHost == null) { |
| 324 | cancelAnimationAndApplyDesiredState() |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 325 | return |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 326 | } |
| 327 | updateTargetState() |
| 328 | if (isCurrentlyInGuidedTransformation()) { |
| 329 | applyTargetStateIfNotAnimating() |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 330 | } else if (animate) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 331 | animator.cancel() |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 332 | if (currentAttachmentLocation == IN_OVERLAY || |
| 333 | !previousHost.hostView.isAttachedToWindow) { |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 334 | // Let's animate to the new position, starting from the current position |
| 335 | // We also go in here in case the view was detached, since the bounds wouldn't |
| 336 | // be correct anymore |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 337 | animationStartBounds.set(currentBounds) |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 338 | } else { |
| 339 | // otherwise, let's take the freshest state, since the current one could |
| 340 | // be outdated |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 341 | animationStartBounds.set(previousHost.currentBounds) |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 342 | } |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 343 | adjustAnimatorForTransition(desiredLocation, previousLocation) |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 344 | rootView?.let { |
| 345 | // Let's delay the animation start until we finished laying out |
| 346 | animationPending = true |
| 347 | it.postOnAnimation(startAnimation) |
| 348 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 349 | } else { |
| 350 | cancelAnimationAndApplyDesiredState() |
| 351 | } |
| 352 | } |
| 353 | |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 354 | private fun shouldAnimateTransition( |
| 355 | @MediaLocation currentLocation: Int, |
| 356 | @MediaLocation previousLocation: Int |
| 357 | ): Boolean { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 358 | if (isCurrentlyInGuidedTransformation()) { |
| 359 | return false |
| 360 | } |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 361 | if (currentLocation == LOCATION_QQS && |
| 362 | previousLocation == LOCATION_LOCKSCREEN && |
| 363 | (statusBarStateController.leaveOpenOnKeyguardHide() || |
| 364 | statusbarState == StatusBarState.SHADE_LOCKED)) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 365 | // Usually listening to the isShown is enough to determine this, but there is some |
| 366 | // non-trivial reattaching logic happening that will make the view not-shown earlier |
| 367 | return true |
| 368 | } |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 369 | return mediaFrame.isShown || animator.isRunning || animationPending |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 372 | private fun adjustAnimatorForTransition(desiredLocation: Int, previousLocation: Int) { |
| 373 | val (animDuration, delay) = getAnimationParams(previousLocation, desiredLocation) |
| 374 | animator.apply { |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 375 | duration = animDuration |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 376 | startDelay = delay |
| 377 | } |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | private fun getAnimationParams(previousLocation: Int, desiredLocation: Int): Pair<Long, Long> { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 381 | var animDuration = 200L |
| 382 | var delay = 0L |
| 383 | if (previousLocation == LOCATION_LOCKSCREEN && desiredLocation == LOCATION_QQS) { |
| 384 | // Going to the full shade, let's adjust the animation duration |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 385 | if (statusbarState == StatusBarState.SHADE && |
| 386 | keyguardStateController.isKeyguardFadingAway) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 387 | delay = keyguardStateController.keyguardFadingAwayDelay |
| 388 | } |
| 389 | animDuration = StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE.toLong() |
| 390 | } else if (previousLocation == LOCATION_QQS && desiredLocation == LOCATION_LOCKSCREEN) { |
| 391 | animDuration = StackStateAnimator.ANIMATION_DURATION_APPEAR_DISAPPEAR.toLong() |
| 392 | } |
Selim Cinek | 5480962 | 2020-04-30 19:04:44 -0700 | [diff] [blame] | 393 | return animDuration to delay |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | private fun applyTargetStateIfNotAnimating() { |
| 397 | if (!animator.isRunning) { |
| 398 | // Let's immediately apply the target state (which is interpolated) if there is |
| 399 | // no animation running. Otherwise the animation update will already update |
| 400 | // the location |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 401 | applyState(targetBounds) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Selim Cinek | f418bb0 | 2020-05-04 17:16:58 -0700 | [diff] [blame] | 405 | /** |
| 406 | * Updates the state that the view wants to be in at the end of the animation. |
| 407 | */ |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 408 | private fun updateTargetState() { |
| 409 | if (isCurrentlyInGuidedTransformation()) { |
| 410 | val progress = getTransformationProgress() |
| 411 | val currentHost = getHost(desiredLocation)!! |
| 412 | val previousHost = getHost(previousLocation)!! |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 413 | val newBounds = currentHost.currentBounds |
| 414 | val previousBounds = previousHost.currentBounds |
| 415 | targetBounds = interpolateBounds(previousBounds, newBounds, progress) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 416 | } else { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 417 | val bounds = getHost(desiredLocation)?.currentBounds ?: return |
| 418 | targetBounds.set(bounds) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 422 | private fun interpolateBounds( |
| 423 | startBounds: Rect, |
| 424 | endBounds: Rect, |
| 425 | progress: Float, |
| 426 | result: Rect? = null |
| 427 | ): Rect { |
| 428 | val left = MathUtils.lerp(startBounds.left.toFloat(), |
| 429 | endBounds.left.toFloat(), progress).toInt() |
| 430 | val top = MathUtils.lerp(startBounds.top.toFloat(), |
| 431 | endBounds.top.toFloat(), progress).toInt() |
| 432 | val right = MathUtils.lerp(startBounds.right.toFloat(), |
| 433 | endBounds.right.toFloat(), progress).toInt() |
| 434 | val bottom = MathUtils.lerp(startBounds.bottom.toFloat(), |
| 435 | endBounds.bottom.toFloat(), progress).toInt() |
| 436 | val resultBounds = result ?: Rect() |
| 437 | resultBounds.set(left, top, right, bottom) |
| 438 | return resultBounds |
| 439 | } |
| 440 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 441 | /** |
| 442 | * @return true if this transformation is guided by an external progress like a finger |
| 443 | */ |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 444 | private fun isCurrentlyInGuidedTransformation(): Boolean { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 445 | return getTransformationProgress() >= 0 |
| 446 | } |
| 447 | |
| 448 | /** |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 449 | * @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] | 450 | * otherwise |
| 451 | */ |
| 452 | private fun getTransformationProgress(): Float { |
| 453 | val progress = getQSTransformationProgress() |
| 454 | if (progress >= 0) { |
| 455 | return progress |
| 456 | } |
| 457 | return -1.0f |
| 458 | } |
| 459 | |
| 460 | private fun getQSTransformationProgress(): Float { |
| 461 | val currentHost = getHost(desiredLocation) |
| 462 | val previousHost = getHost(previousLocation) |
| 463 | if (currentHost?.location == LOCATION_QS) { |
| 464 | if (previousHost?.location == LOCATION_QQS) { |
| 465 | return qsExpansion |
| 466 | } |
| 467 | } |
| 468 | return -1.0f |
| 469 | } |
| 470 | |
| 471 | private fun getHost(@MediaLocation location: Int): MediaHost? { |
| 472 | if (location < 0) { |
| 473 | return null |
| 474 | } |
| 475 | return mediaHosts[location] |
| 476 | } |
| 477 | |
| 478 | private fun cancelAnimationAndApplyDesiredState() { |
| 479 | animator.cancel() |
| 480 | getHost(desiredLocation)?.let { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 481 | applyState(it.currentBounds, immediately = true) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 485 | /** |
| 486 | * Apply the current state to the view, updating it's bounds and desired state |
| 487 | */ |
| 488 | private fun applyState(bounds: Rect, immediately: Boolean = false) { |
| 489 | currentBounds.set(bounds) |
| 490 | val currentlyInGuidedTransformation = isCurrentlyInGuidedTransformation() |
| 491 | val startLocation = if (currentlyInGuidedTransformation) previousLocation else -1 |
| 492 | val progress = if (currentlyInGuidedTransformation) getTransformationProgress() else 1.0f |
| 493 | val endLocation = desiredLocation |
Selim Cinek | afae4e7 | 2020-06-16 18:21:41 -0700 | [diff] [blame] | 494 | mediaCarouselController.setCurrentState(startLocation, endLocation, progress, immediately) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 495 | updateHostAttachment() |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 496 | if (currentAttachmentLocation == IN_OVERLAY) { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 497 | mediaFrame.setLeftTopRightBottom( |
| 498 | currentBounds.left, |
| 499 | currentBounds.top, |
| 500 | currentBounds.right, |
| 501 | currentBounds.bottom) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 502 | } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | private fun updateHostAttachment() { |
| 506 | val inOverlay = isTransitionRunning() && rootOverlay != null |
| 507 | val newLocation = if (inOverlay) IN_OVERLAY else desiredLocation |
| 508 | if (currentAttachmentLocation != newLocation) { |
| 509 | currentAttachmentLocation = newLocation |
| 510 | |
| 511 | // Remove the carousel from the old host |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 512 | (mediaFrame.parent as ViewGroup?)?.removeView(mediaFrame) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 513 | |
| 514 | // Add it to the new one |
| 515 | val targetHost = getHost(desiredLocation)!!.hostView |
| 516 | if (inOverlay) { |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 517 | rootOverlay!!.add(mediaFrame) |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 518 | } else { |
Selim Cinek | 2a1cab1 | 2020-06-01 19:19:58 -0700 | [diff] [blame] | 519 | // When adding back to the host, let's make sure to reset the bounds. |
| 520 | // Usually adding the view will trigger a layout that does this automatically, |
| 521 | // but we sometimes suppress this. |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 522 | targetHost.addView(mediaFrame) |
Selim Cinek | 2a1cab1 | 2020-06-01 19:19:58 -0700 | [diff] [blame] | 523 | val left = targetHost.paddingLeft |
| 524 | val top = targetHost.paddingTop |
| 525 | mediaFrame.setLeftTopRightBottom( |
| 526 | left, |
| 527 | top, |
| 528 | left + currentBounds.width(), |
| 529 | top + currentBounds.height()) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 534 | private fun isTransitionRunning(): Boolean { |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 535 | return isCurrentlyInGuidedTransformation() && getTransformationProgress() != 1.0f || |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 536 | animator.isRunning || animationPending |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 539 | @MediaLocation |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 540 | private fun calculateLocation(): Int { |
Selim Cinek | 7f65760 | 2020-05-21 12:37:14 -0700 | [diff] [blame] | 541 | if (blockLocationChanges) { |
| 542 | // Keep the current location until we're allowed to again |
| 543 | return desiredLocation |
| 544 | } |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 545 | val onLockscreen = (!bypassController.bypassEnabled && |
| 546 | (statusbarState == StatusBarState.KEYGUARD || |
| 547 | statusbarState == StatusBarState.FULLSCREEN_USER_SWITCHER)) |
| 548 | val allowedOnLockscreen = notifLockscreenUserManager.shouldShowLockscreenNotifications() |
Lucas Dupin | 7d52dd6 | 2020-06-18 18:11:22 -0700 | [diff] [blame] | 549 | val location = when { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 550 | qsExpansion > 0.0f && !onLockscreen -> LOCATION_QS |
| 551 | qsExpansion > 0.4f && onLockscreen -> LOCATION_QS |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 552 | onLockscreen && allowedOnLockscreen -> LOCATION_LOCKSCREEN |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 553 | else -> LOCATION_QQS |
| 554 | } |
Lucas Dupin | 7d52dd6 | 2020-06-18 18:11:22 -0700 | [diff] [blame] | 555 | // When we're on lock screen and the player is not active, we should keep it in QS. |
| 556 | // Otherwise it will try to animate a transition that doesn't make sense. |
| 557 | if (location == LOCATION_LOCKSCREEN && getHost(location)?.visible != true && |
| 558 | !statusBarStateController.isDozing) { |
| 559 | return LOCATION_QS |
| 560 | } |
Selim Cinek | f630a82 | 2020-06-22 11:26:09 -0700 | [diff] [blame] | 561 | if (location == LOCATION_LOCKSCREEN && desiredLocation == LOCATION_QS && |
| 562 | collapsingShadeFromQS) { |
| 563 | // When collapsing on the lockscreen, we want to remain in QS |
| 564 | return LOCATION_QS |
| 565 | } |
| 566 | if (location != LOCATION_LOCKSCREEN && desiredLocation == LOCATION_LOCKSCREEN |
| 567 | && !fullyAwake) { |
| 568 | // When unlocking from dozing / while waking up, the media shouldn't be transitioning |
| 569 | // in an animated way. Let's keep it in the lockscreen until we're fully awake and |
| 570 | // reattach it without an animation |
| 571 | return LOCATION_LOCKSCREEN |
| 572 | } |
Lucas Dupin | 7d52dd6 | 2020-06-18 18:11:22 -0700 | [diff] [blame] | 573 | return location |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 576 | companion object { |
| 577 | /** |
| 578 | * Attached in expanded quick settings |
| 579 | */ |
| 580 | const val LOCATION_QS = 0 |
| 581 | |
| 582 | /** |
| 583 | * Attached in the collapsed QS |
| 584 | */ |
| 585 | const val LOCATION_QQS = 1 |
| 586 | |
| 587 | /** |
| 588 | * Attached on the lock screen |
| 589 | */ |
| 590 | const val LOCATION_LOCKSCREEN = 2 |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 591 | |
| 592 | /** |
| 593 | * Attached at the root of the hierarchy in an overlay |
| 594 | */ |
| 595 | const val IN_OVERLAY = -1000 |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 596 | } |
Robert Snoeberger | 52dfb62 | 2020-05-21 16:31:29 -0400 | [diff] [blame] | 597 | } |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 598 | |
| 599 | @IntDef(prefix = ["LOCATION_"], value = [MediaHierarchyManager.LOCATION_QS, |
| 600 | MediaHierarchyManager.LOCATION_QQS, MediaHierarchyManager.LOCATION_LOCKSCREEN]) |
| 601 | @Retention(AnnotationRetention.SOURCE) |
| 602 | annotation class MediaLocation |