Skip to content

Commit b1758cf

Browse files
adampAndroid (Google) Code Review
authored andcommitted
Merge "Make MediaRouteButton focusable by default" into jb-mr1-dev
2 parents ed9960f + e468187 commit b1758cf

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

core/java/android/app/MediaRouteActionProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public View onCreateActionView(MenuItem item) {
8181
}
8282
mMenuItem = item;
8383
mView = new MediaRouteButton(mContext);
84+
mView.setCheatSheetEnabled(true);
8485
mView.setRouteTypes(mRouteTypes);
8586
mView.setExtendedSettingsClickListener(mExtendedSettingsListener);
8687
mView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,

core/java/android/app/MediaRouteButton.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
import android.content.ContextWrapper;
2424
import android.content.res.TypedArray;
2525
import android.graphics.Canvas;
26+
import android.graphics.Rect;
2627
import android.graphics.drawable.Drawable;
2728
import android.media.MediaRouter;
2829
import android.media.MediaRouter.RouteGroup;
2930
import android.media.MediaRouter.RouteInfo;
31+
import android.text.TextUtils;
3032
import android.util.AttributeSet;
3133
import android.util.Log;
34+
import android.view.Gravity;
35+
import android.view.HapticFeedbackConstants;
3236
import android.view.SoundEffectConstants;
3337
import android.view.View;
38+
import android.widget.Toast;
3439

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

4854
private int mMinWidth;
4955
private int mMinHeight;
@@ -82,6 +88,7 @@ public MediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
8288
a.recycle();
8389

8490
setClickable(true);
91+
setLongClickable(true);
8592

8693
setRouteTypes(routeTypes);
8794
}
@@ -129,6 +136,52 @@ public boolean performClick() {
129136
return handled;
130137
}
131138

139+
void setCheatSheetEnabled(boolean enable) {
140+
mCheatSheetEnabled = enable;
141+
}
142+
143+
@Override
144+
public boolean performLongClick() {
145+
if (super.performLongClick()) {
146+
return true;
147+
}
148+
149+
if (!mCheatSheetEnabled) {
150+
return false;
151+
}
152+
153+
final CharSequence contentDesc = getContentDescription();
154+
if (TextUtils.isEmpty(contentDesc)) {
155+
// Don't show the cheat sheet if we have no description
156+
return false;
157+
}
158+
159+
final int[] screenPos = new int[2];
160+
final Rect displayFrame = new Rect();
161+
getLocationOnScreen(screenPos);
162+
getWindowVisibleDisplayFrame(displayFrame);
163+
164+
final Context context = getContext();
165+
final int width = getWidth();
166+
final int height = getHeight();
167+
final int midy = screenPos[1] + height / 2;
168+
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
169+
170+
Toast cheatSheet = Toast.makeText(context, contentDesc, Toast.LENGTH_SHORT);
171+
if (midy < displayFrame.height()) {
172+
// Show along the top; follow action buttons
173+
cheatSheet.setGravity(Gravity.TOP | Gravity.END,
174+
screenWidth - screenPos[0] - width / 2, height);
175+
} else {
176+
// Show along the bottom center
177+
cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
178+
}
179+
cheatSheet.show();
180+
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
181+
182+
return true;
183+
}
184+
132185
public void setRouteTypes(int types) {
133186
if (types == mRouteTypes) {
134187
// Already registered; nothing to do.

core/res/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3606,4 +3606,8 @@
36063606

36073607
<!-- "Done" button for MediaRouter chooser dialog when grouping routes. [CHAR LIMIT=NONE] -->
36083608
<string name="media_route_chooser_grouping_done">Done</string>
3609+
3610+
<!-- Content description of a MediaRouteButton for accessibility support -->
3611+
<string name="media_route_button_content_description">Media output</string>
3612+
36093613
</resources>

core/res/res/values/styles.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,13 +2448,17 @@ please see styles_device_defaults.xml.
24482448
<item name="android:externalRouteEnabledDrawable">@drawable/ic_media_route_holo_dark</item>
24492449
<item name="android:minWidth">56dp</item>
24502450
<item name="android:minHeight">48dp</item>
2451+
<item name="android:focusable">true</item>
2452+
<item name="android:contentDescription">@android:string/media_route_button_content_description</item>
24512453
</style>
24522454

24532455
<style name="Widget.Holo.Light.MediaRouteButton">
24542456
<item name="android:background">?android:attr/selectableItemBackground</item>
24552457
<item name="android:externalRouteEnabledDrawable">@drawable/ic_media_route_holo_light</item>
24562458
<item name="android:minWidth">56dp</item>
24572459
<item name="android:minHeight">48dp</item>
2460+
<item name="android:focusable">true</item>
2461+
<item name="android:contentDescription">@android:string/media_route_button_content_description</item>
24582462
</style>
24592463

24602464
</resources>

0 commit comments

Comments
 (0)