Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 86165e35 authored by Donghyun Cho's avatar Donghyun Cho
Browse files

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
parent 10657346
Loading
Loading
Loading
Loading
+62 −39
Original line number Diff line number Diff line
@@ -18,19 +18,23 @@ package com.android.internal.app;

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 @@ import android.widget.SeekBar;
 *
 * 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 @@ public class MediaRouteControllerDialog extends Dialog {
    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 @@ public class MediaRouteControllerDialog extends Dialog {

    private View mControlView;

    private Button mDisconnectButton;

    public MediaRouteControllerDialog(Context context, int theme) {
        super(context, theme);

@@ -132,14 +135,28 @@ public class MediaRouteControllerDialog extends Dialog {

    @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 @@ public class MediaRouteControllerDialog extends Dialog {
            }
        });

        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 @@ public class MediaRouteControllerDialog extends Dialog {
        }
    }


    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
@@ -248,24 +254,41 @@ public class MediaRouteControllerDialog extends Dialog {
        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 getIconDrawable() {
        if (mRoute.isConnecting()) {
            if (mMediaRouteConnectingDrawable == null) {
                mMediaRouteConnectingDrawable = getContext().getDrawable(
                        R.drawable.ic_media_route_connecting_holo_dark);
    private Drawable obtainMediaRouteButtonDrawable() {
        Context context = getContext();
        TypedValue value = new TypedValue();
        if (!context.getTheme().resolveAttribute(R.attr.mediaRouteButtonStyle, value, true)) {
            return null;
        }
            return mMediaRouteConnectingDrawable;
        } else {
            if (mMediaRouteOnDrawable == null) {
                mMediaRouteOnDrawable = getContext().getDrawable(
                        R.drawable.ic_media_route_on_holo_dark);
        int[] drawableAttrs = new int[] { R.attr.externalRouteEnabledDrawable };
        TypedArray a = context.obtainStyledAttributes(value.data, drawableAttrs);
        Drawable drawable = a.getDrawable(0);
        a.recycle();
        return drawable;
    }
            return mMediaRouteOnDrawable;

    private Drawable getIconDrawable() {
        if (!(mMediaRouteButtonDrawable instanceof StateListDrawable)) {
            return mMediaRouteButtonDrawable;
        } else if (mRoute.isConnecting()) {
            StateListDrawable stateListDrawable = (StateListDrawable) mMediaRouteButtonDrawable;
            stateListDrawable.setState(mMediaRouteConnectingState);
            return stateListDrawable.getCurrent();
        } else {
            StateListDrawable stateListDrawable = (StateListDrawable) mMediaRouteButtonDrawable;
            stateListDrawable.setState(mMediaRouteOnState);
            return stateListDrawable.getCurrent();
        }
    }

+0 −1
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ public class MediaRouteControllerDialogFragment extends DialogFragment {
     */
    public MediaRouteControllerDialogFragment() {
        setCancelable(true);
        setStyle(STYLE_NORMAL, android.R.style.Theme_DeviceDefault_Dialog);
    }

    /**
+1 −13
Original line number Diff line number Diff line
@@ -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>
+1 −1
Original line number Diff line number Diff line
@@ -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" />