Handling multiple players better

Previously the animation was completely broken with multiple players
since they were all laid out at 0 instead of the actual position.
Measuring the full layout is a bit too expansive, so we're
introducing some workarounds to only measure the players.

Test: add multiple players, observe transitions
Bug: 154137987
Change-Id: I1c424c980cf3b64f5a9d63ba058aa7e47f6e4156
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
index 7228271..60c2ed2 100644
--- a/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/media/MediaControlPanel.java
@@ -47,6 +47,7 @@
 import android.widget.TextView;
 
 import androidx.annotation.Nullable;
+import androidx.annotation.UiThread;
 import androidx.constraintlayout.motion.widget.Key;
 import androidx.constraintlayout.motion.widget.KeyAttributes;
 import androidx.constraintlayout.motion.widget.KeyFrames;
@@ -111,7 +112,6 @@
     private int mAlbumArtSize;
     private int mAlbumArtRadius;
     private int mViewWidth;
-    private MediaMeasurementInput mLastMeasureInput;
 
     public static final String MEDIA_PREFERENCES = "media_control_prefs";
     public static final String MEDIA_PREFERENCE_KEY = "browser_components";
@@ -188,7 +188,6 @@
         mActivityStarter = activityStarter;
         mSeekBarViewModel = new SeekBarViewModel(backgroundExecutor);
         mSeekBarObserver = new SeekBarObserver(getView());
-        // TODO: we should pause this whenever the screen is off / panel is collapsed etc.
         mSeekBarViewModel.getProgress().observeForever(mSeekBarObserver);
         SeekBar bar = getView().findViewById(R.id.media_progress_bar);
         bar.setOnSeekBarChangeListener(mSeekBarViewModel.getSeekBarListener());
@@ -261,7 +260,7 @@
         // Try to find a browser service component for this app
         // TODO also check for a media button receiver intended for restarting (b/154127084)
         // Only check if we haven't tried yet or the session token changed
-        final String pkgName = mController.getPackageName();
+        final String pkgName = data.getPackageName();
         if (mServiceComponent == null && !mCheckedForResumption) {
             Log.d(TAG, "Checking for service component");
             PackageManager pm = mContext.getPackageManager();
@@ -301,8 +300,7 @@
 
         // App icon
         ImageView appIcon = mMediaNotifView.requireViewById(R.id.icon);
-        // TODO: look at iconDrawable
-        Drawable iconDrawable = data.getAppIcon();
+        Drawable iconDrawable = data.getAppIcon().mutate();
         iconDrawable.setTint(mForegroundColor);
         appIcon.setImageDrawable(iconDrawable);
 
@@ -389,7 +387,7 @@
         }
 
         // Seek Bar
-        final MediaController controller = new MediaController(getContext(), data.getToken());
+        final MediaController controller = getController();
         mBackgroundExecutor.execute(
                 () -> mSeekBarViewModel.updateController(controller, data.getForegroundColor()));
 
@@ -397,10 +395,13 @@
         // TODO: b/156036025 bring back media guts
 
         makeActive();
+
+        // Update both constraint sets to regenerate the animation.
         mMediaNotifView.updateState(R.id.collapsed, collapsedSet);
         mMediaNotifView.updateState(R.id.expanded, expandedSet);
     }
 
+    @UiThread
     private Drawable createRoundedBitmap(Icon icon) {
         if (icon == null) {
             return null;
@@ -746,27 +747,14 @@
      */
     protected void removePlayer() { }
 
-    public void remeasure(@Nullable MediaMeasurementInput input, boolean animate, long duration,
-            long startDelay) {
-        // Let's remeasure if our width changed. Our height is dependent on the expansion, so we
-        // won't animate if it changed
-        if (input != null && !input.sameAs(mLastMeasureInput)) {
-            mLastMeasureInput = input;
-            if (animate) {
-                mLayoutAnimationHelper.animatePendingSizeChange(duration, startDelay);
-            }
-            remeasureInternal(input);
-            mMediaNotifView.layout(0, 0, mMediaNotifView.getMeasuredWidth(),
-                    mMediaNotifView.getMeasuredHeight());
+    public void measure(@Nullable MediaMeasurementInput input) {
+        if (input != null) {
+            int width = input.getWidth();
+            setPlayerWidth(width);
+            mMediaNotifView.measure(input.getWidthMeasureSpec(), input.getHeightMeasureSpec());
         }
     }
 
-    private void remeasureInternal(MediaMeasurementInput input) {
-        int width = input.getWidth();
-        setPlayerWidth(width);
-        mMediaNotifView.measure(input.getWidthMeasureSpec(), input.getHeightMeasureSpec());
-    }
-
     public void setPlayerWidth(int width) {
         ConstraintSet expandedSet = mMediaNotifView.getConstraintSet(R.id.expanded);
         ConstraintSet collapsedSet = mMediaNotifView.getConstraintSet(R.id.collapsed);
@@ -775,4 +763,8 @@
         mMediaNotifView.updateState(R.id.collapsed, collapsedSet);
         mMediaNotifView.updateState(R.id.expanded, expandedSet);
     }
+
+    public void animatePendingSizeChange(long duration, long startDelay) {
+        mLayoutAnimationHelper.animatePendingSizeChange(duration, startDelay);
+    }
 }