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

Commit e5604e51 authored by Andre Le's avatar Andre Le Committed by Android (Google) Code Review
Browse files

Merge "CastDetailsView: Introduce MediaRouteControllerContentManager.Delegate" into main

parents aaa454ed 69dff236
Loading
Loading
Loading
Loading
+46 −2
Original line number Diff line number Diff line
@@ -28,19 +28,40 @@ import com.android.internal.R;
 * This class manages the content display within the media route controller UI.
 */
public class MediaRouteControllerContentManager {
    /**
     * A delegate interface that a MediaRouteController UI should implement. It allows the content
     * manager to inform the UI of any UI changes that need to be made in response to content
     * updates.
     */
    public interface Delegate {
        /**
         * Updates the title of the cast device
         */
        void setCastDeviceTitle(CharSequence title);

        /**
         * Dismiss the UI to transition to a different workflow.
         */
        void dismissView();
    }

    private final Delegate mDelegate;

    // 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.
    private static final int VOLUME_UPDATE_DELAY_MILLIS = 250;

    private final MediaRouter mRouter;
    private final MediaRouter.RouteInfo mRoute;

    private LinearLayout mVolumeLayout;
    private SeekBar mVolumeSlider;
    private boolean mVolumeSliderTouched;

    public MediaRouteControllerContentManager(Context context) {
        MediaRouter mRouter = context.getSystemService(MediaRouter.class);
    public MediaRouteControllerContentManager(Context context, Delegate delegate) {
        mDelegate = delegate;
        mRouter = context.getSystemService(MediaRouter.class);
        mRoute = mRouter.getSelectedRoute();
    }

@@ -49,6 +70,7 @@ public class MediaRouteControllerContentManager {
     * given container view.
     */
    public void bindViews(View containerView) {
        mDelegate.setCastDeviceTitle(mRoute.getName());
        mVolumeLayout = containerView.findViewById(R.id.media_route_volume_layout);
        mVolumeSlider = containerView.findViewById(R.id.media_route_volume_slider);
        mVolumeSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@@ -88,6 +110,14 @@ public class MediaRouteControllerContentManager {
        });
    }

    /**
     * Updates all the views to reflect new states.
     */
    public void update() {
        mDelegate.setCastDeviceTitle(mRoute.getName());
        updateVolume();
    }

    /**
     * Updates the volume layout and slider.
     */
@@ -103,6 +133,20 @@ public class MediaRouteControllerContentManager {
        }
    }

    /**
     * Callback function to triggered after the disconnect button is clicked.
     */
    public void onDisconnectButtonClick() {
        if (mRoute.isSelected()) {
            if (mRoute.isBluetooth()) {
                mRouter.getDefaultRoute().select();
            } else {
                mRouter.getFallbackRoute().select();
            }
        }
        mDelegate.dismissView();
    }

    private boolean isVolumeControlAvailable() {
        return mRoute.getVolumeHandling() == MediaRouter.RouteInfo.PLAYBACK_VOLUME_VARIABLE;
    }
+19 −18
Original line number Diff line number Diff line
@@ -46,7 +46,10 @@ import com.android.internal.R;
 *
 * TODO: Move this back into the API, as in the support library media router.
 */
public class MediaRouteControllerDialog extends AlertDialog {
public class MediaRouteControllerDialog extends AlertDialog implements
        MediaRouteControllerContentManager.Delegate {
    // TODO(b/360050020): Eventually these 3 variables should be in the content manager instead of
    //  here. So these should be removed when the migration is completed.
    private final MediaRouter mRouter;
    private final MediaRouterCallback mCallback;
    private final MediaRouter.RouteInfo mRoute;
@@ -63,7 +66,7 @@ public class MediaRouteControllerDialog extends AlertDialog {
    public MediaRouteControllerDialog(Context context, int theme) {
        super(context, theme);

        mContentManager = new MediaRouteControllerContentManager(context);
        mContentManager = new MediaRouteControllerContentManager(context, this);
        mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        mCallback = new MediaRouterCallback();
        mRoute = mRouter.getSelectedRoute();
@@ -71,24 +74,13 @@ public class MediaRouteControllerDialog extends AlertDialog {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTitle(mRoute.getName());
        Resources res = getContext().getResources();
        setButton(BUTTON_NEGATIVE, res.getString(R.string.media_route_controller_disconnect),
                (dialogInterface, id) -> {
                    if (mRoute.isSelected()) {
                        if (mRoute.isBluetooth()) {
                            mRouter.getDefaultRoute().select();
                        } else {
                            mRouter.getFallbackRoute().select();
                        }
                    }
                    dismiss();
                });
                (dialogInterface, id) -> mContentManager.onDisconnectButtonClick());
        View customView = getLayoutInflater().inflate(R.layout.media_route_controller_dialog, null);
        setView(customView, 0, 0, 0, 0);
        super.onCreate(savedInstanceState);

        mContentManager.bindViews(customView);
        super.onCreate(savedInstanceState);

        View customPanelView = getWindow().findViewById(R.id.customPanel);
        if (customPanelView != null) {
@@ -135,13 +127,22 @@ public class MediaRouteControllerDialog extends AlertDialog {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void setCastDeviceTitle(CharSequence title) {
        setTitle(title);
    }

    @Override
    public void dismissView() {
        dismiss();
    }

    private void update() {
        if (!mRoute.isSelected() || mRoute.isDefault()) {
            dismiss();
            dismissView();
        }

        setTitle(mRoute.getName());
        mContentManager.updateVolume();
        mContentManager.update();

        Drawable icon = getIconDrawable();
        if (icon != mCurrentIconDrawable) {