Fixing some issues where view was too short when created

The view was always created too small because we didn't
set the constraints in the beginning

Test: add new media notification, not clipped off
Bug: 154137987
Change-Id: Ifb9c95ba41be7392ec4b61e2b801b7420b811d3a
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
index 3d638dd..2a9ba83 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
@@ -396,6 +396,8 @@
         // TODO: b/156036025 bring back media guts
 
         makeActive();
+        mMediaNotifView.updateState(R.id.collapsed, collapsedSet);
+        mMediaNotifView.updateState(R.id.expanded, expandedSet);
     }
 
     private Drawable createRoundedBitmap(Icon icon) {
@@ -753,16 +755,18 @@
         }
     }
 
-    public MediaMeasurementInput getLastMeasureInput() {
-        return mLastMeasureInput;
+    private void remeasureInternal(MediaMeasurementInput input) {
+        int width = input.getWidth();
+        setPlayerWidth(width);
+        mMediaNotifView.measure(input.getWidthMeasureSpec(), input.getHeightMeasureSpec());
     }
 
-    private void remeasureInternal(MediaMeasurementInput input) {
+    public void setPlayerWidth(int width) {
         ConstraintSet expandedSet = mMediaNotifView.getConstraintSet(R.id.expanded);
         ConstraintSet collapsedSet = mMediaNotifView.getConstraintSet(R.id.collapsed);
-        int width = input.getWidth();
         collapsedSet.setGuidelineBegin(R.id.view_width, width);
         expandedSet.setGuidelineBegin(R.id.view_width, width);
-        mMediaNotifView.measure(input.getWidthMeasureSpec(), input.getHeightMeasureSpec());
+        mMediaNotifView.updateState(R.id.collapsed, collapsedSet);
+        mMediaNotifView.updateState(R.id.expanded, expandedSet);
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt b/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt
index 391b112..b4c0c33 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaHierarchyManager.kt
@@ -132,9 +132,21 @@
         return viewHost
     }
 
-    private fun createUniqueObjectHost(host: MediaState): UniqueObjectHostView {
+    private fun createUniqueObjectHost(host: MediaHost): UniqueObjectHostView {
         val viewHost = UniqueObjectHostView(context)
         viewHost.measurementCache = mediaMeasurementProvider.obtainCache(host)
+        viewHost.firstMeasureListener =  { input ->
+            if (host.location == currentAttachmentLocation) {
+                // The first measurement of the attached view is happening, Let's make
+                // sure the player width is updated
+                val measuringInput = host.getMeasuringInput(input)
+                mediaViewManager.remeasureAllPlayers(
+                        measuringInput,
+                        animate = false,
+                        duration = 0,
+                        startDelay = 0)
+            }
+        }
 
         viewHost.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
             override fun onViewAttachedToWindow(p0: View?) {
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaViewManager.kt b/packages/SystemUI/src/com/android/systemui/media/MediaViewManager.kt
index 6dd9787..a364e6b 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaViewManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaViewManager.kt
@@ -63,6 +63,7 @@
                 removed?.apply {
                     mediaContent.removeView(removed.view)
                     removed.onDestroy()
+                    updateMediaPaddings()
                 }
             }
         })
@@ -106,8 +107,9 @@
             } else {
                 mediaContent.addView(existingPlayer.view)
             }
+            updatePlayerToCurrentState(existingPlayer)
         } else if (existingPlayer.isPlaying &&
-                mediaContent.indexOfChild(existingPlayer.view) != 0) {
+                    mediaContent.indexOfChild(existingPlayer.view) != 0) {
             if (visualStabilityManager.isReorderingAllowed) {
                 mediaContent.removeView(existingPlayer.view)
                 mediaContent.addView(existingPlayer.view, 0)
@@ -116,9 +118,20 @@
             }
         }
         existingPlayer.bind(data)
+        // Resetting the progress to make sure it's taken into account for the latest
+        // motion model
+        existingPlayer.view.progress = currentState?.expansion ?: 0.0f
         updateMediaPaddings()
     }
 
+    private fun updatePlayerToCurrentState(existingPlayer: MediaControlPanel) {
+        if (desiredState != null && desiredState!!.measurementInput != null) {
+            // make sure the player width is set to the current state
+            val measurementInput = desiredState!!.measurementInput!!
+            existingPlayer.setPlayerWidth(measurementInput.width)
+        }
+    }
+
     private fun updateMediaPaddings() {
         val padding = context.resources.getDimensionPixelSize(R.dimen.qs_media_padding)
         val childCount = mediaContent.childCount
@@ -153,9 +166,14 @@
             // This is a hosting view, let's remeasure our players
             desiredState = targetState
             val measurementInput = targetState.measurementInput
-            for (mediaPlayer in mediaPlayers.values) {
-                mediaPlayer.remeasure(measurementInput, animate, duration, startDelay)
-            }
+            remeasureAllPlayers(measurementInput, animate, duration, startDelay)
+        }
+    }
+
+    fun remeasureAllPlayers(measurementInput: MediaMeasurementInput?,
+                                    animate: Boolean, duration: Long, startDelay: Long) {
+        for (mediaPlayer in mediaPlayers.values) {
+            mediaPlayer.remeasure(measurementInput, animate, duration, startDelay)
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/util/animation/UniqueObjectHostView.kt b/packages/SystemUI/src/com/android/systemui/util/animation/UniqueObjectHostView.kt
index 9c993aa..376d091 100644
--- a/packages/SystemUI/src/com/android/systemui/util/animation/UniqueObjectHostView.kt
+++ b/packages/SystemUI/src/com/android/systemui/util/animation/UniqueObjectHostView.kt
@@ -30,12 +30,10 @@
     context: Context
 ) : FrameLayout(context) {
     lateinit var measurementCache : GuaranteedMeasurementCache
+    var firstMeasureListener: ((MeasurementInput) -> Unit)? = null
 
     @SuppressLint("DrawAllocation")
     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
-        if (isCurrentHost()) {
-            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
-        }
         val paddingHorizontal = paddingStart + paddingEnd
         val paddingVertical = paddingTop + paddingBottom
         val width = MeasureSpec.getSize(widthMeasureSpec) - paddingHorizontal
@@ -43,13 +41,18 @@
         val height = MeasureSpec.getSize(heightMeasureSpec) - paddingVertical
         val heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.getMode(heightMeasureSpec))
         val measurementInput = MeasurementInputData(widthSpec, heightSpec)
-        if (!isCurrentHost() || !measurementCache.contains(measurementInput)) {
+        firstMeasureListener?.apply {
+            invoke(measurementInput)
+            firstMeasureListener = null
+        }
+        if (!isCurrentHost()) {
             // We're not currently the host, let's get the dimension from our cache (this might
-            // perform a measuring if the cache doesn't have it yet
+            // perform a measuring if the cache doesn't have it yet)
             val (cachedWidth, cachedHeight) = measurementCache.obtainMeasurement(measurementInput)
             setMeasuredDimension(cachedWidth + paddingHorizontal, cachedHeight + paddingVertical)
         } else {
-            // Let's update what we have in the cache if it's present
+            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
+            // Let's update our cache
             val child = getChildAt(0)!!
             val output = MeasurementOutput(child.measuredWidth, child.measuredHeight)
             measurementCache.putMeasurement(measurementInput, output)