MediaRouter: Apply dialog width spec

Bug: 23117157, Bug: 23117610
Change-Id: I5f1b4688c79927ff5db633666c9040405816b09a
diff --git a/v7/mediarouter/res/values/dimens.xml b/v7/mediarouter/res/values/dimens.xml
index d253f4c..d66bf53 100644
--- a/v7/mediarouter/res/values/dimens.xml
+++ b/v7/mediarouter/res/values/dimens.xml
@@ -15,6 +15,8 @@
 -->
 
 <resources>
+    <dimen name="mr_dialog_content_width_portrait">288dp</dimen>
+    <dimen name="mr_dialog_content_width_landscape">356dp</dimen>
     <dimen name="mr_media_route_controller_art_max_height">320dp</dimen>
     <dimen name="mr_list_item_margin">24dp</dimen>
 </resources>
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
index f116436..1f28e13 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
@@ -22,6 +22,8 @@
 import android.app.Dialog;
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.content.res.Configuration;
+import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
@@ -77,6 +79,10 @@
     private ListView mListView;
     private boolean mAttachedToWindow;
 
+    private int mOrientation;
+    private int mDialogWidthPortrait;
+    private int mDialogWidthLandscape;
+
     public MediaRouteChooserDialog(Context context) {
         this(context, 0);
     }
@@ -160,6 +166,13 @@
 
         setContentView(R.layout.mr_chooser_dialog);
         setTitle(R.string.mr_chooser_title);
+        View decorView = getWindow().getDecorView();
+        int dialogHorizontalPadding = decorView.getPaddingLeft() + decorView.getPaddingRight();
+        Resources res = getContext().getResources();
+        mDialogWidthPortrait = res.getDimensionPixelSize(
+                R.dimen.mr_dialog_content_width_portrait) + dialogHorizontalPadding;
+        mDialogWidthLandscape = res.getDimensionPixelSize(
+                R.dimen.mr_dialog_content_width_landscape) + dialogHorizontalPadding;
 
         mRoutes = new ArrayList<MediaRouter.RouteInfo>();
         mAdapter = new RouteAdapter(getContext(), mRoutes);
@@ -169,12 +182,31 @@
         mListView.setEmptyView(findViewById(android.R.id.empty));
     }
 
+    /**
+     * Called by {@link MediaRouteChooserDialogFragment} when the device configuration is changed.
+     */
+    void onConfigurationChanged(Configuration newConfig) {
+        onOrientationChanged(newConfig.orientation);
+    }
+
+    private void onOrientationChanged(int orientation) {
+        if (!mAttachedToWindow || mOrientation == orientation) {
+            return;
+        }
+        mOrientation = orientation;
+        getWindow().setLayout(
+                mOrientation == Configuration.ORIENTATION_PORTRAIT
+                        ? mDialogWidthPortrait : mDialogWidthLandscape,
+                ViewGroup.LayoutParams.WRAP_CONTENT);
+    }
+
     @Override
     public void onAttachedToWindow() {
         super.onAttachedToWindow();
 
         mAttachedToWindow = true;
         mRouter.addCallback(mSelector, mCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
+        onOrientationChanged(getContext().getResources().getConfiguration().orientation);
         refreshRoutes();
     }
 
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialogFragment.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialogFragment.java
index 3b5239a..4d94c58 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialogFragment.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialogFragment.java
@@ -18,6 +18,7 @@
 
 import android.app.Dialog;
 import android.content.Context;
+import android.content.res.Configuration;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.support.v7.media.MediaRouteSelector;
@@ -32,6 +33,7 @@
 public class MediaRouteChooserDialogFragment extends DialogFragment {
     private final String ARGUMENT_SELECTOR = "selector";
 
+    private MediaRouteChooserDialog mDialog;
     private MediaRouteSelector mSelector;
 
     /**
@@ -108,8 +110,16 @@
 
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        MediaRouteChooserDialog dialog = onCreateChooserDialog(getActivity(), savedInstanceState);
-        dialog.setRouteSelector(getRouteSelector());
-        return dialog;
+        mDialog = onCreateChooserDialog(getActivity(), savedInstanceState);
+        mDialog.setRouteSelector(getRouteSelector());
+        return mDialog;
+    }
+
+    @Override
+    public void onConfigurationChanged(Configuration newConfig) {
+        super.onConfigurationChanged(newConfig);
+        if (mDialog != null) {
+            mDialog.onConfigurationChanged(newConfig);
+        }
     }
 }
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
index e4c7c7e..38cfc98 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
@@ -20,6 +20,8 @@
 
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.res.Configuration;
+import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -83,6 +85,9 @@
 
     private boolean mCreated;
     private boolean mAttachedToWindow;
+    private int mOrientation;
+    private int mDialogWidthPortrait;
+    private int mDialogWidthLandscape;
 
     private View mControlView;
 
@@ -231,6 +236,13 @@
         super.onCreate(savedInstanceState);
 
         setContentView(R.layout.mr_controller_material_dialog_b);
+        View decorView = getWindow().getDecorView();
+        int dialogHorizontalPadding = decorView.getPaddingLeft() + decorView.getPaddingRight();
+        Resources res = getContext().getResources();
+        mDialogWidthPortrait = res.getDimensionPixelSize(
+                R.dimen.mr_dialog_content_width_portrait) + dialogHorizontalPadding;
+        mDialogWidthLandscape = res.getDimensionPixelSize(
+                R.dimen.mr_dialog_content_width_landscape) + dialogHorizontalPadding;
 
         ClickListener listener = new ClickListener();
 
@@ -328,6 +340,25 @@
         }
     }
 
+    /**
+     * Called by {@link MediaRouteControllerDialogFragment} when the device configuration
+     * is changed.
+     */
+    void onConfigurationChanged(Configuration newConfig) {
+        onOrientationChanged(newConfig.orientation);
+    }
+
+    private void onOrientationChanged(int orientation) {
+        if (!mAttachedToWindow || mOrientation == orientation) {
+            return;
+        }
+        mOrientation = orientation;
+        getWindow().setLayout(
+                mOrientation == Configuration.ORIENTATION_LANDSCAPE
+                        ? mDialogWidthLandscape : mDialogWidthPortrait,
+                ViewGroup.LayoutParams.WRAP_CONTENT);
+    }
+
     @Override
     public void onAttachedToWindow() {
         super.onAttachedToWindow();
@@ -336,6 +367,7 @@
         mRouter.addCallback(MediaRouteSelector.EMPTY, mCallback,
                 MediaRouter.CALLBACK_FLAG_UNFILTERED_EVENTS);
         setMediaSession(mRouter.getMediaSessionToken());
+        onOrientationChanged(getContext().getResources().getConfiguration().orientation);
     }
 
     @Override
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialogFragment.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialogFragment.java
index beb9832..8156492 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialogFragment.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialogFragment.java
@@ -18,6 +18,7 @@
 
 import android.app.Dialog;
 import android.content.Context;
+import android.content.res.Configuration;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 
@@ -29,6 +30,7 @@
  * </p>
  */
 public class MediaRouteControllerDialogFragment extends DialogFragment {
+    private MediaRouteControllerDialog mDialog;
     /**
      * Creates a media route controller dialog fragment.
      * <p>
@@ -51,7 +53,15 @@
     }
 
     @Override
+    public void onConfigurationChanged(Configuration newConfig) {
+        if (mDialog != null) {
+            mDialog.onConfigurationChanged(newConfig);
+        }
+    }
+
+    @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        return onCreateControllerDialog(getActivity(), savedInstanceState);
+        mDialog = onCreateControllerDialog(getActivity(), savedInstanceState);
+        return mDialog;
     }
 }