MediaRouter: Announce play/pause action for accessibility

Bug: 23974907
Change-Id: I8a26235755a1acbc2556d5dda1ddf9b291ac1aec
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
index 80b3de1..4befb24 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
@@ -36,6 +36,7 @@
 import android.support.v4.media.session.MediaControllerCompat;
 import android.support.v4.media.session.MediaSessionCompat;
 import android.support.v4.media.session.PlaybackStateCompat;
+import android.support.v4.view.accessibility.AccessibilityEventCompat;
 import android.support.v7.graphics.Palette;
 import android.support.v7.media.MediaRouteSelector;
 import android.support.v7.media.MediaRouter;
@@ -49,6 +50,8 @@
 import android.view.View.MeasureSpec;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
+import android.view.accessibility.AccessibilityEvent;
+import android.view.accessibility.AccessibilityManager;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.FrameLayout;
@@ -143,6 +146,8 @@
     private Uri mArtIconUri;
     private boolean mIsGroupExpanded;
 
+    private final AccessibilityManager mAccessibilityManager;
+
     public MediaRouteControllerDialog(Context context) {
         this(context, 0);
     }
@@ -158,6 +163,8 @@
         setMediaSession(mRouter.getMediaSessionToken());
         mVolumeGroupListPaddingTop = context.getResources().getDimensionPixelSize(
                 R.dimen.mr_controller_volume_group_list_padding_top);
+        mAccessibilityManager =
+                (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
     }
 
     /**
@@ -742,11 +749,23 @@
                 dismiss();
             } else if (id == R.id.mr_control_play_pause) {
                 if (mMediaController != null && mState != null) {
-                    if (mState.getState() == PlaybackStateCompat.STATE_PLAYING) {
+                    boolean isPlaying = mState.getState() == PlaybackStateCompat.STATE_PLAYING;
+                    if (isPlaying) {
                         mMediaController.getTransportControls().pause();
                     } else {
                         mMediaController.getTransportControls().play();
                     }
+                    // Announce the action for accessibility.
+                    if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) {
+                        AccessibilityEvent event = AccessibilityEvent.obtain(
+                                AccessibilityEventCompat.TYPE_ANNOUNCEMENT);
+                        event.setPackageName(getContext().getPackageName());
+                        event.setClassName(getClass().getName());
+                        int resId = isPlaying ?
+                                R.string.mr_controller_pause : R.string.mr_controller_play;
+                        event.getText().add(getContext().getString(resId));
+                        mAccessibilityManager.sendAccessibilityEvent(event);
+                    }
                 }
             } else if (id == R.id.mr_close) {
                 dismiss();