Revert "Revert "MediaRouter: expand group volume section as much as it can""

This reverts commit e8085c5c2db384e2578dac00be505e90f45c2e4c.

Note: wide dialog width issue is fixed.

Change-Id: I616fae39515f67385eafc184c89b8ccd110ecfb5
diff --git a/v7/mediarouter/res/layout/mr_controller_material_dialog_b.xml b/v7/mediarouter/res/layout/mr_controller_material_dialog_b.xml
index f4ad723..5971a71 100644
--- a/v7/mediarouter/res/layout/mr_controller_material_dialog_b.xml
+++ b/v7/mediarouter/res/layout/mr_controller_material_dialog_b.xml
@@ -79,6 +79,7 @@
             <ListView android:id="@+id/mr_volume_group_list"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content"
+                      android:maxHeight="256dp"
                       android:background="?attr/colorPrimaryDark"
                       android:visibility="gone" />
         </LinearLayout>
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
index c012364..8306a83 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
@@ -109,7 +109,6 @@
 
     private FrameLayout mCustomControlLayout;
     private FrameLayout mDefaultControlLayout;
-    private boolean mNeedToAdjustControlFrameLayout = true;
     private ImageView mArtView;
     private TextView mTitleView;
     private TextView mSubtitleView;
@@ -274,13 +273,6 @@
         mCloseButton.setOnClickListener(listener);
         mCustomControlLayout = (FrameLayout) findViewById(R.id.mr_custom_control);
         mDefaultControlLayout = (FrameLayout) findViewById(R.id.mr_default_control);
-        ViewTreeObserver observer = mDefaultControlLayout.getViewTreeObserver();
-        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
-            @Override
-            public void onGlobalLayout() {
-                updateControlFrameLayout();
-            }
-        });
         mArtView = (ImageView) findViewById(R.id.mr_art);
 
         mMediaControlLayout = (LinearLayout) findViewById(R.id.mr_media_control);
@@ -355,7 +347,7 @@
                     mGroupExpandCollapseButton.setImageDrawable(expandGroupDrawable);
                     mVolumeGroupList.setVisibility(View.GONE);
                 }
-                mNeedToAdjustControlFrameLayout = true;
+                updateControlFrameLayout();
             }
         });
 
@@ -365,7 +357,6 @@
             mCustomControlLayout.setVisibility(View.VISIBLE);
             mArtView.setVisibility(View.GONE);
         }
-
         mCreated = true;
         update();
     }
@@ -402,6 +393,7 @@
         mDisconnectButton.setLayoutParams(lp);
 
         updateArtView();
+        updateControlFrameLayout();
     }
 
     @Override
@@ -559,27 +551,27 @@
     }
 
     private void updateControlFrameLayout() {
-        if (!mNeedToAdjustControlFrameLayout) {
-            return;
-        }
         int height;
         if (mArtView.getVisibility() == View.GONE) {
             height = LinearLayout.LayoutParams.WRAP_CONTENT;
-        } else if (!mIsGroupExpanded) {
-            height = mArtView.getHeight() + mMediaControlLayout.getHeight();
         } else {
-            if (mVolumeGroupList.getAdapter().getCount() <= 2) {
-                // Push the controls up and cover the artwork.
-                height = mArtView.getHeight() + mMediaControlLayout.getHeight()
-                        - mVolumeGroupList.getHeight();
+            measureandGetDecorView();
+            if (!mIsGroupExpanded) {
+                height = mArtView.getMeasuredHeight() + mMediaControlLayout.getMeasuredHeight();
             } else {
-                // If there are 3 or more, the controls completely cover the artwork.
-                height = mMediaControlLayout.getHeight();
+                if (mVolumeGroupList.getAdapter().getCount() <= 2
+                        && mArtView.getMeasuredHeight() > mVolumeGroupList.getMeasuredHeight()) {
+                    // Push the controls up and partially cover the artwork.
+                    height = mArtView.getMeasuredHeight() + mMediaControlLayout.getMeasuredHeight()
+                            - mVolumeGroupList.getMeasuredHeight();
+                } else {
+                    // Completely cover the artwork.
+                    height = LinearLayout.LayoutParams.WRAP_CONTENT;
+                }
             }
         }
-        mDefaultControlLayout.setLayoutParams(
-                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, height));
-        mNeedToAdjustControlFrameLayout = false;
+        mDefaultControlLayout.setLayoutParams(new LinearLayout.LayoutParams(
+                LinearLayout.LayoutParams.FILL_PARENT, height));
     }
 
     private boolean isVolumeControlAvailable() {
@@ -587,8 +579,17 @@
                 MediaRouter.RouteInfo.PLAYBACK_VOLUME_VARIABLE;
     }
 
+    private View measureandGetDecorView() {
+        int orientation = getContext().getResources().getConfiguration().orientation;
+        int dialogWidth = orientation == Configuration.ORIENTATION_LANDSCAPE
+                ? mDialogWidthLandscape : mDialogWidthPortrait;
+        View decorView = getWindow().getDecorView();
+        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(dialogWidth, MeasureSpec.EXACTLY);
+        decorView.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
+        return decorView;
+    }
+
     private void updateArtView() {
-        mNeedToAdjustControlFrameLayout = true;
         if (!(mArtView.getDrawable() instanceof BitmapDrawable)) {
             mArtView.setVisibility(View.GONE);
             return;
@@ -600,11 +601,7 @@
         }
         int desiredArtHeight = getDesiredArtHeight(art.getWidth(), art.getHeight());
         DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
-        int dialogWidth = displayMetrics.widthPixels < displayMetrics.heightPixels
-                ? mDialogWidthPortrait : mDialogWidthLandscape;
-        View decorView = getWindow().getDecorView();
-        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(dialogWidth, MeasureSpec.EXACTLY);
-        decorView.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
+        View decorView = measureandGetDecorView();
         // Show art if and only if it fits in the screen.
         if (mArtView.getVisibility() == View.GONE) {
             if (decorView.getMeasuredHeight() + desiredArtHeight <= displayMetrics.heightPixels) {
@@ -881,6 +878,7 @@
                 mArtView.setImageBitmap(art);
                 mArtView.setBackgroundColor(mBackgroundColor);
                 updateArtView();
+                updateControlFrameLayout();
             }
         }
     }