2323import android .content .ContextWrapper ;
2424import android .content .res .TypedArray ;
2525import android .graphics .Canvas ;
26+ import android .graphics .Rect ;
2627import android .graphics .drawable .Drawable ;
2728import android .media .MediaRouter ;
2829import android .media .MediaRouter .RouteGroup ;
2930import android .media .MediaRouter .RouteInfo ;
31+ import android .text .TextUtils ;
3032import android .util .AttributeSet ;
3133import android .util .Log ;
34+ import android .view .Gravity ;
35+ import android .view .HapticFeedbackConstants ;
3236import android .view .SoundEffectConstants ;
3337import android .view .View ;
38+ import android .widget .Toast ;
3439
3540public 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.
0 commit comments