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

Commit e4681878 authored by Adam Powell's avatar Adam Powell
Browse files

Make MediaRouteButton focusable by default

Bug 6921236

Add a default contentDescription for MediaRouteButton.

Add long-press "cheat sheet" support to MediaRouteButtons created by
MediaRouteActionProvider for use in the action bar to match standard
action button behavior.

Change-Id: I313cfcb2349210765b084c3388095de4624009e8
parent 97425536
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ public class MediaRouteActionProvider extends ActionProvider {
        }
        mMenuItem = item;
        mView = new MediaRouteButton(mContext);
        mView.setCheatSheetEnabled(true);
        mView.setRouteTypes(mRouteTypes);
        mView.setExtendedSettingsClickListener(mExtendedSettingsListener);
        mView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
+53 −0
Original line number Diff line number Diff line
@@ -23,14 +23,19 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.media.MediaRouter;
import android.media.MediaRouter.RouteGroup;
import android.media.MediaRouter.RouteInfo;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
import android.view.View;
import android.widget.Toast;

public class MediaRouteButton extends View {
    private static final String TAG = "MediaRouteButton";
@@ -44,6 +49,7 @@ public class MediaRouteButton extends View {
    private Drawable mRemoteIndicator;
    private boolean mRemoteActive;
    private boolean mToggleMode;
    private boolean mCheatSheetEnabled;

    private int mMinWidth;
    private int mMinHeight;
@@ -82,6 +88,7 @@ public class MediaRouteButton extends View {
        a.recycle();

        setClickable(true);
        setLongClickable(true);

        setRouteTypes(routeTypes);
    }
@@ -129,6 +136,52 @@ public class MediaRouteButton extends View {
        return handled;
    }

    void setCheatSheetEnabled(boolean enable) {
        mCheatSheetEnabled = enable;
    }

    @Override
    public boolean performLongClick() {
        if (super.performLongClick()) {
            return true;
        }

        if (!mCheatSheetEnabled) {
            return false;
        }

        final CharSequence contentDesc = getContentDescription();
        if (TextUtils.isEmpty(contentDesc)) {
            // Don't show the cheat sheet if we have no description
            return false;
        }

        final int[] screenPos = new int[2];
        final Rect displayFrame = new Rect();
        getLocationOnScreen(screenPos);
        getWindowVisibleDisplayFrame(displayFrame);

        final Context context = getContext();
        final int width = getWidth();
        final int height = getHeight();
        final int midy = screenPos[1] + height / 2;
        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;

        Toast cheatSheet = Toast.makeText(context, contentDesc, Toast.LENGTH_SHORT);
        if (midy < displayFrame.height()) {
            // Show along the top; follow action buttons
            cheatSheet.setGravity(Gravity.TOP | Gravity.END,
                    screenWidth - screenPos[0] - width / 2, height);
        } else {
            // Show along the bottom center
            cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
        }
        cheatSheet.show();
        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);

        return true;
    }

    public void setRouteTypes(int types) {
        if (types == mRouteTypes) {
            // Already registered; nothing to do.
+4 −0
Original line number Diff line number Diff line
@@ -3606,4 +3606,8 @@

    <!-- "Done" button for MediaRouter chooser dialog when grouping routes. [CHAR LIMIT=NONE] -->
    <string name="media_route_chooser_grouping_done">Done</string>

    <!-- Content description of a MediaRouteButton for accessibility support -->
    <string name="media_route_button_content_description">Media output</string>

</resources>
+4 −0
Original line number Diff line number Diff line
@@ -2448,6 +2448,8 @@ please see styles_device_defaults.xml.
        <item name="android:externalRouteEnabledDrawable">@drawable/ic_media_route_holo_dark</item>
        <item name="android:minWidth">56dp</item>
        <item name="android:minHeight">48dp</item>
        <item name="android:focusable">true</item>
        <item name="android:contentDescription">@android:string/media_route_button_content_description</item>
    </style>

    <style name="Widget.Holo.Light.MediaRouteButton">
@@ -2455,6 +2457,8 @@ please see styles_device_defaults.xml.
        <item name="android:externalRouteEnabledDrawable">@drawable/ic_media_route_holo_light</item>
        <item name="android:minWidth">56dp</item>
        <item name="android:minHeight">48dp</item>
        <item name="android:focusable">true</item>
        <item name="android:contentDescription">@android:string/media_route_button_content_description</item>
    </style>

</resources>