Use AlertDialog for MediaRouteControllerDialog

Rather than using custom layout for MediaRouteControllerDialog, use
AlertDialog to lay out inner views and buttons properly. Also, obtain
the media route icon from the current theme and animate it if necessary.

Bug: 33253732
Test: Launched the dialog and verified.
Change-Id: I4bec689ce808285e084fc9a90820b429e63e765a
diff --git a/core/java/com/android/internal/app/MediaRouteControllerDialog.java b/core/java/com/android/internal/app/MediaRouteControllerDialog.java
index b0e0373..5ce3e54 100644
--- a/core/java/com/android/internal/app/MediaRouteControllerDialog.java
+++ b/core/java/com/android/internal/app/MediaRouteControllerDialog.java
@@ -18,19 +18,23 @@
 
 import com.android.internal.R;
 
-import android.app.Dialog;
+import android.app.AlertDialog;
 import android.app.MediaRouteActionProvider;
 import android.app.MediaRouteButton;
 import android.content.Context;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.content.res.TypedArray;
+import android.graphics.drawable.AnimationDrawable;
 import android.graphics.drawable.Drawable;
+import android.graphics.drawable.StateListDrawable;
 import android.media.MediaRouter;
 import android.media.MediaRouter.RouteGroup;
 import android.media.MediaRouter.RouteInfo;
 import android.os.Bundle;
+import android.util.TypedValue;
 import android.view.KeyEvent;
 import android.view.View;
-import android.view.Window;
-import android.widget.Button;
 import android.widget.FrameLayout;
 import android.widget.LinearLayout;
 import android.widget.SeekBar;
@@ -46,7 +50,7 @@
  *
  * TODO: Move this back into the API, as in the support library media router.
  */
-public class MediaRouteControllerDialog extends Dialog {
+public class MediaRouteControllerDialog extends AlertDialog {
     // Time to wait before updating the volume when the user lets go of the seek bar
     // to allow the route provider time to propagate the change and publish a new
     // route descriptor.
@@ -57,8 +61,9 @@
     private final MediaRouter.RouteInfo mRoute;
 
     private boolean mCreated;
-    private Drawable mMediaRouteConnectingDrawable;
-    private Drawable mMediaRouteOnDrawable;
+    private Drawable mMediaRouteButtonDrawable;
+    private int[] mMediaRouteConnectingState = { R.attr.state_checked, R.attr.state_enabled };
+    private int[] mMediaRouteOnState = { R.attr.state_activated, R.attr.state_enabled };
     private Drawable mCurrentIconDrawable;
 
     private boolean mVolumeControlEnabled = true;
@@ -68,8 +73,6 @@
 
     private View mControlView;
 
-    private Button mDisconnectButton;
-
     public MediaRouteControllerDialog(Context context, int theme) {
         super(context, theme);
 
@@ -132,14 +135,28 @@
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
+        setTitle(mRoute.getName());
+        Resources res = getContext().getResources();
+        setButton(BUTTON_NEGATIVE, res.getString(R.string.media_route_controller_disconnect),
+                new OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialogInterface, int id) {
+                        if (mRoute.isSelected()) {
+                            mRouter.getDefaultRoute().select();
+                        }
+                        dismiss();
+                    }
+                });
+        View customView = getLayoutInflater().inflate(R.layout.media_route_controller_dialog, null);
+        setView(customView, 0, 0, 0, 0);
         super.onCreate(savedInstanceState);
 
-        getWindow().requestFeature(Window.FEATURE_LEFT_ICON);
-
-        setContentView(R.layout.media_route_controller_dialog);
-
-        mVolumeLayout = (LinearLayout)findViewById(R.id.media_route_volume_layout);
-        mVolumeSlider = (SeekBar)findViewById(R.id.media_route_volume_slider);
+        View customPanelView = getWindow().findViewById(R.id.customPanel);
+        if (customPanelView != null) {
+            customPanelView.setMinimumHeight(0);
+        }
+        mVolumeLayout = (LinearLayout) customView.findViewById(R.id.media_route_volume_layout);
+        mVolumeSlider = (SeekBar) customView.findViewById(R.id.media_route_volume_slider);
         mVolumeSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
             private final Runnable mStopTrackingTouch = new Runnable() {
                 @Override
@@ -176,22 +193,12 @@
             }
         });
 
-        mDisconnectButton = (Button)findViewById(R.id.media_route_disconnect_button);
-        mDisconnectButton.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                if (mRoute.isSelected()) {
-                    mRouter.getDefaultRoute().select();
-                }
-                dismiss();
-            }
-        });
-
+        mMediaRouteButtonDrawable = obtainMediaRouteButtonDrawable();
         mCreated = true;
         if (update()) {
             mControlView = onCreateMediaControlView(savedInstanceState);
             FrameLayout controlFrame =
-                    (FrameLayout)findViewById(R.id.media_route_control_frame);
+                    (FrameLayout) customView.findViewById(R.id.media_route_control_frame);
             if (mControlView != null) {
                 controlFrame.addView(mControlView);
                 controlFrame.setVisibility(View.VISIBLE);
@@ -201,7 +208,6 @@
         }
     }
 
-
     @Override
     public void onAttachedToWindow() {
         super.onAttachedToWindow();
@@ -248,24 +254,41 @@
         Drawable icon = getIconDrawable();
         if (icon != mCurrentIconDrawable) {
             mCurrentIconDrawable = icon;
-            getWindow().setFeatureDrawable(Window.FEATURE_LEFT_ICON, icon);
+            if (icon instanceof AnimationDrawable) {
+                AnimationDrawable animDrawable = (AnimationDrawable) icon;
+                if (!animDrawable.isRunning()) {
+                    animDrawable.start();
+                }
+            }
+            setIcon(icon);
         }
         return true;
     }
 
+    private Drawable obtainMediaRouteButtonDrawable() {
+        Context context = getContext();
+        TypedValue value = new TypedValue();
+        if (!context.getTheme().resolveAttribute(R.attr.mediaRouteButtonStyle, value, true)) {
+            return null;
+        }
+        int[] drawableAttrs = new int[] { R.attr.externalRouteEnabledDrawable };
+        TypedArray a = context.obtainStyledAttributes(value.data, drawableAttrs);
+        Drawable drawable = a.getDrawable(0);
+        a.recycle();
+        return drawable;
+    }
+
     private Drawable getIconDrawable() {
-        if (mRoute.isConnecting()) {
-            if (mMediaRouteConnectingDrawable == null) {
-                mMediaRouteConnectingDrawable = getContext().getDrawable(
-                        R.drawable.ic_media_route_connecting_holo_dark);
-            }
-            return mMediaRouteConnectingDrawable;
+        if (!(mMediaRouteButtonDrawable instanceof StateListDrawable)) {
+            return mMediaRouteButtonDrawable;
+        } else if (mRoute.isConnecting()) {
+            StateListDrawable stateListDrawable = (StateListDrawable) mMediaRouteButtonDrawable;
+            stateListDrawable.setState(mMediaRouteConnectingState);
+            return stateListDrawable.getCurrent();
         } else {
-            if (mMediaRouteOnDrawable == null) {
-                mMediaRouteOnDrawable = getContext().getDrawable(
-                        R.drawable.ic_media_route_on_holo_dark);
-            }
-            return mMediaRouteOnDrawable;
+            StateListDrawable stateListDrawable = (StateListDrawable) mMediaRouteButtonDrawable;
+            stateListDrawable.setState(mMediaRouteOnState);
+            return stateListDrawable.getCurrent();
         }
     }
 
diff --git a/core/java/com/android/internal/app/MediaRouteControllerDialogFragment.java b/core/java/com/android/internal/app/MediaRouteControllerDialogFragment.java
index 466c015..4c30501 100644
--- a/core/java/com/android/internal/app/MediaRouteControllerDialogFragment.java
+++ b/core/java/com/android/internal/app/MediaRouteControllerDialogFragment.java
@@ -39,7 +39,6 @@
      */
     public MediaRouteControllerDialogFragment() {
         setCancelable(true);
-        setStyle(STYLE_NORMAL, android.R.style.Theme_DeviceDefault_Dialog);
     }
 
     /**
diff --git a/core/res/res/layout/media_route_controller_dialog.xml b/core/res/res/layout/media_route_controller_dialog.xml
index 0bf70da..24a2535 100644
--- a/core/res/res/layout/media_route_controller_dialog.xml
+++ b/core/res/res/layout/media_route_controller_dialog.xml
@@ -47,17 +47,5 @@
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:visibility="gone" />
-
-        <!-- Disconnect button. -->
-        <LinearLayout android:layout_width="match_parent"
-                      android:layout_height="wrap_content"
-                      style="?attr/buttonBarStyle">
-            <Button android:id="@+id/media_route_disconnect_button"
-                    android:layout_width="match_parent"
-                    android:layout_height="match_parent"
-                    style="?attr/buttonBarButtonStyle"
-                    android:gravity="center"
-                    android:text="@string/media_route_controller_disconnect" />
-        </LinearLayout>
     </LinearLayout>
-</ScrollView>
\ No newline at end of file
+</ScrollView>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index dcb44d3..d33bc79 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -1467,10 +1467,10 @@
   <java-symbol type="id" name="media_route_volume_layout" />
   <java-symbol type="id" name="media_route_volume_slider" />
   <java-symbol type="id" name="media_route_control_frame" />
-  <java-symbol type="id" name="media_route_disconnect_button" />
   <java-symbol type="id" name="media_route_extended_settings_button" />
   <java-symbol type="string" name="media_route_chooser_title" />
   <java-symbol type="string" name="media_route_chooser_title_for_remote_display" />
+  <java-symbol type="string" name="media_route_controller_disconnect" />
   <java-symbol type="string" name="bluetooth_a2dp_audio_route_name" />
 
   <java-symbol type="dimen" name="config_minScalingSpan" />