Fix talkback accessibility by setting playback control labels.

b/17974328

Change-Id: I88bf9d3574e5a9b6f4738bc0e3bed178fc9637fc
diff --git a/v17/leanback/res/values/strings.xml b/v17/leanback/res/values/strings.xml
index bad8000..289927f 100644
--- a/v17/leanback/res/values/strings.xml
+++ b/v17/leanback/res/values/strings.xml
@@ -25,4 +25,46 @@
     <string name="lb_search_bar_hint_with_title">Search <xliff:g id="search context">%1$s</xliff:g></string>
     <!-- Hint showing in the empty search bar using a provided context (usually the application name) while in voice input mode [CHAR LIMIT=40] -->
     <string name="lb_search_bar_hint_with_title_speech">Speak to search <xliff:g id="search context">%1$s</xliff:g></string>
+
+    <!-- Talkback label for the control button to start media playback -->
+    <string name="lb_playback_controls_play">Play</string>
+    <!-- Talkback label for the control button to pause media playback -->
+    <string name="lb_playback_controls_pause">Pause</string>
+    <!-- Talkback label for the control button to fast forward media playback -->
+    <string name="lb_playback_controls_fast_forward">Fast Forward</string>
+    <!-- Talkback label for the control button to start rewind playback -->
+    <string name="lb_playback_controls_rewind">Rewind</string>
+    <!-- Talkback label for the control button to skip to the next media item -->
+    <string name="lb_playback_controls_skip_next">Skip Next</string>
+    <!-- Talkback label for the control button to skip to the previous media item -->
+    <string name="lb_playback_controls_skip_previous">Skip Previous</string>
+    <!-- Talkback label for the control button to display more actions -->
+    <string name="lb_playback_controls_more_actions">More Actions</string>
+    <!-- Talkback label for the control button to deselect the thumb up rating for the current media item -->
+    <string name="lb_playback_controls_thumb_up">Deselect Thumb Up</string>
+    <!-- Talkback label for the control button to select the thumb up rating for the current media item -->
+    <string name="lb_playback_controls_thumb_up_outline">Select Thumb Up</string>
+    <!-- Talkback label for the control button to deselect the thumb down rating for the current media item -->
+    <string name="lb_playback_controls_thumb_down">Deselect Thumb Down</string>
+    <!-- Talkback label for the control button to select the thumb down rating for the current media item -->
+    <string name="lb_playback_controls_thumb_down_outline">Select Thumb Down</string>
+    <!-- Talkback label for the control button to switch to a media playback mode where item playback does not repeat -->
+    <string name="lb_playback_controls_repeat_none">Repeat None</string>
+    <!-- Talkback label for the control button to switch to a media playback mode where a set of media items will be repeated -->
+    <string name="lb_playback_controls_repeat_all">Repeat All</string>
+    <!-- Talkback label for the control button to switch to a media playback mode where a single media item will be repeated -->
+    <string name="lb_playback_controls_repeat_one">Repeat One</string>
+    <!-- Talkback label for the control button to enable random selection of the next media item -->
+    <string name="lb_playback_controls_shuffle_enable">Enable Shuffle</string>
+    <!-- Talkback label for the control button to play media items in sequential order -->
+    <string name="lb_playback_controls_shuffle_disable">Disable Shuffle</string>
+    <!-- Talkback label for the control button to enable playback of media items of high quality -->
+    <string name="lb_playback_controls_high_quality_enable">Enable High Quality</string>
+    <!-- Talkback label for the control button to disable playback of media items of high quality -->
+    <string name="lb_playback_controls_high_quality_disable">Disable High Quality</string>
+    <!-- Talkback label for the control button to enable closed captioning -->
+    <string name="lb_playback_controls_closed_captioning_enable">Enable Closed Captioning</string>
+    <!-- Talkback label for the control button to disable closed captioning -->
+    <string name="lb_playback_controls_closed_captioning_disable">Disable Closed Captioning</string>
+
 </resources>
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/ControlButtonPresenterSelector.java b/v17/leanback/src/android/support/v17/leanback/widget/ControlButtonPresenterSelector.java
index de95729..ac14ac6 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/ControlButtonPresenterSelector.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/ControlButtonPresenterSelector.java
@@ -14,9 +14,11 @@
 package android.support.v17.leanback.widget;
 
 import android.support.v17.leanback.R;
+import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.accessibility.AccessibilityEvent;
 import android.widget.ImageView;
 
 /**
@@ -83,12 +85,20 @@
             Action action = (Action) item;
             ActionViewHolder vh = (ActionViewHolder) viewHolder;
             vh.mIcon.setImageDrawable(action.getIcon());
+            CharSequence contentDescription = !TextUtils.isEmpty(action.getLabel1()) ?
+                action.getLabel1() : action.getLabel2();
+            if (!TextUtils.equals(vh.mFocusableView.getContentDescription(), contentDescription)) {
+                vh.mFocusableView.setContentDescription(contentDescription);
+                vh.mFocusableView.sendAccessibilityEvent(
+                        AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
+            }
         }
 
         @Override
         public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
             ActionViewHolder vh = (ActionViewHolder) viewHolder;
             vh.mIcon.setImageDrawable(null);
+            vh.mFocusableView.setContentDescription(null);
         }
 
         @Override
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/PlaybackControlsRow.java b/v17/leanback/src/android/support/v17/leanback/widget/PlaybackControlsRow.java
index abcc213..03648c6 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/PlaybackControlsRow.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/PlaybackControlsRow.java
@@ -48,6 +48,7 @@
     public static abstract class MultiAction extends Action {
         private int mIndex;
         private Drawable[] mDrawables;
+        private String[] mLabels;
 
         /**
          * Constructor
@@ -66,6 +67,11 @@
             setIndex(0);
         }
 
+        public void setLabels(String[] labels) {
+            mLabels = labels;
+            setIndex(0);
+        }
+
         /**
          * Returns the number of drawables.
          */
@@ -81,6 +87,13 @@
         }
 
         /**
+         * Returns the label at the given index.
+         */
+        public String getLabel(int index) {
+            return mLabels[index];
+        }
+
+        /**
          * Increments the index, wrapping to zero once the end is reached.
          */
         public void nextIndex() {
@@ -93,6 +106,9 @@
         public void setIndex(int index) {
             mIndex = index;
             setIcon(mDrawables[mIndex]);
+            if (mLabels != null) {
+                setLabel1(mLabels[mIndex]);
+            }
         }
 
         /**
@@ -129,11 +145,11 @@
             drawables[PAUSE] = getStyledDrawable(context,
                     R.styleable.lbPlaybackControlsActionIcons_pause);
             setDrawables(drawables);
-        }
 
-        @Override
-        public String toString() {
-            return "PlayPauseAction";
+            String[] labels = new String[drawables.length];
+            labels[PLAY] = context.getString(R.string.lb_playback_controls_play);
+            labels[PAUSE] = context.getString(R.string.lb_playback_controls_pause);
+            setLabels(labels);
         }
     }
 
@@ -149,11 +165,7 @@
             super(R.id.lb_control_fast_forward);
             setIcon(getStyledDrawable(context,
                     R.styleable.lbPlaybackControlsActionIcons_fast_forward));
-        }
-
-        @Override
-        public String toString() {
-            return "FastForwardAction";
+            setLabel1(context.getString(R.string.lb_playback_controls_fast_forward));
         }
     }
 
@@ -169,11 +181,7 @@
             super(R.id.lb_control_fast_rewind);
             setIcon(getStyledDrawable(context,
                     R.styleable.lbPlaybackControlsActionIcons_rewind));
-        }
-
-        @Override
-        public String toString() {
-            return "RewindAction";
+            setLabel1(context.getString(R.string.lb_playback_controls_rewind));
         }
     }
 
@@ -189,11 +197,7 @@
             super(R.id.lb_control_skip_next);
             setIcon(getStyledDrawable(context,
                     R.styleable.lbPlaybackControlsActionIcons_skip_next));
-        }
-
-        @Override
-        public String toString() {
-            return "SkipNextAction";
+            setLabel1(context.getString(R.string.lb_playback_controls_skip_next));
         }
     }
 
@@ -209,11 +213,7 @@
             super(R.id.lb_control_skip_previous);
             setIcon(getStyledDrawable(context,
                     R.styleable.lbPlaybackControlsActionIcons_skip_previous));
-        }
-
-        @Override
-        public String toString() {
-            return "SkipPreviousAction";
+            setLabel1(context.getString(R.string.lb_playback_controls_skip_previous));
         }
     }
 
@@ -228,11 +228,7 @@
         public MoreActions(Context context) {
             super(R.id.lb_control_more_actions);
             setIcon(context.getResources().getDrawable(R.drawable.lb_ic_more));
-        }
-
-        @Override
-        public String toString() {
-            return "MoreActions";
+            setLabel1(context.getString(R.string.lb_playback_controls_more_actions));
         }
     }
 
@@ -271,11 +267,10 @@
             super(R.id.lb_control_thumbs_up, context,
                     R.styleable.lbPlaybackControlsActionIcons_thumb_up,
                     R.styleable.lbPlaybackControlsActionIcons_thumb_up_outline);
-        }
-
-        @Override
-        public String toString() {
-            return "ThumbsUpAction";
+            String[] labels = new String[getNumberOfDrawables()];
+            labels[SOLID] = context.getString(R.string.lb_playback_controls_thumb_up);
+            labels[OUTLINE] = context.getString(R.string.lb_playback_controls_thumb_up_outline);
+            setLabels(labels);
         }
     }
 
@@ -287,11 +282,10 @@
             super(R.id.lb_control_thumbs_down, context,
                     R.styleable.lbPlaybackControlsActionIcons_thumb_down,
                     R.styleable.lbPlaybackControlsActionIcons_thumb_down_outline);
-        }
-
-        @Override
-        public String toString() {
-            return "ThumbsDownAction";
+            String[] labels = new String[getNumberOfDrawables()];
+            labels[SOLID] = context.getString(R.string.lb_playback_controls_thumb_down);
+            labels[OUTLINE] = context.getString(R.string.lb_playback_controls_thumb_down_outline);
+            setLabels(labels);
         }
     }
 
@@ -351,11 +345,13 @@
             drawables[ONE] = new BitmapDrawable(context.getResources(),
                     createBitmap(repeatOneDrawable.getBitmap(), repeatOneColor));
             setDrawables(drawables);
-        }
 
-        @Override
-        public String toString() {
-            return "RepeatAction";
+            String[] labels = new String[drawables.length];
+            // Note, labels denote the action taken when clicked
+            labels[NONE] = context.getString(R.string.lb_playback_controls_repeat_all);
+            labels[ALL] = context.getString(R.string.lb_playback_controls_repeat_one);
+            labels[ONE] = context.getString(R.string.lb_playback_controls_repeat_none);
+            setLabels(labels);
         }
     }
 
@@ -389,11 +385,11 @@
             drawables[ON] = new BitmapDrawable(context.getResources(),
                     createBitmap(uncoloredDrawable.getBitmap(), highlightColor));
             setDrawables(drawables);
-        }
 
-        @Override
-        public String toString() {
-            return "ShuffleAction";
+            String[] labels = new String[drawables.length];
+            labels[OFF] = context.getString(R.string.lb_playback_controls_shuffle_enable);
+            labels[ON] = context.getString(R.string.lb_playback_controls_shuffle_disable);
+            setLabels(labels);
         }
     }
 
@@ -427,11 +423,11 @@
             drawables[ON] = new BitmapDrawable(context.getResources(),
                     createBitmap(uncoloredDrawable.getBitmap(), highlightColor));
             setDrawables(drawables);
-        }
 
-        @Override
-        public String toString() {
-            return "HighQualityAction";
+            String[] labels = new String[drawables.length];
+            labels[OFF] = context.getString(R.string.lb_playback_controls_high_quality_enable);
+            labels[ON] = context.getString(R.string.lb_playback_controls_high_quality_disable);
+            setLabels(labels);
         }
     }
 
@@ -465,11 +461,11 @@
             drawables[ON] = new BitmapDrawable(context.getResources(),
                     createBitmap(uncoloredDrawable.getBitmap(), highlightColor));
             setDrawables(drawables);
-        }
 
-        @Override
-        public String toString() {
-            return "ClosedCaptioningAction";
+            String[] labels = new String[drawables.length];
+            labels[OFF] = context.getString(R.string.lb_playback_controls_closed_captioning_enable);
+            labels[ON] = context.getString(R.string.lb_playback_controls_closed_captioning_disable);
+            setLabels(labels);
         }
     }