Don't use announceForAccessibility

announceForAccessibility is API 16+. It's just a convenience
method for sending accessibility event, so I copied the code
into a helper function.

b/17606966

Change-Id: I99e7de645409168a79030c7645672eb93ca8773d
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index ac06a42..6b25b8f 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -76,6 +76,8 @@
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewParent;
+import android.view.accessibility.AccessibilityEvent;
+import android.view.accessibility.AccessibilityManager;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputConnection;
 import android.widget.AdapterView;
@@ -183,6 +185,7 @@
     private ReplacementDrawableSpan mMoreChip;
     private TextView mMoreItem;
 
+    private boolean mIsAccessibilityOn;
     private int mCurrentSuggestionCount;
 
     // VisibleForTesting
@@ -299,6 +302,15 @@
         setDropdownChipLayouter(new DropdownChipLayouter(LayoutInflater.from(context), context));
     }
 
+    @Override
+    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+        super.onLayout(changed, left, top, right, bottom);
+
+        final AccessibilityManager accessibilityManager =
+                (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
+        mIsAccessibilityOn = accessibilityManager.isEnabled();
+    }
+
     private int calculateTextHeight() {
         final Rect textBounds = new Rect();
         final TextPaint paint = getPaint();
@@ -513,7 +525,7 @@
                     // the count at the bottom of this function.
                     if (mCurrentSuggestionCount == 0) {
                         // Announce the new number of possible choices for accessibility.
-                        announceForAccessibility(getContext().getString(
+                        announceForAccessibilityCompat(getContext().getString(
                                 R.string.accessbility_suggestion_dropdown_opened));
                     }
                 }
@@ -530,6 +542,20 @@
         baseAdapter.setDropdownChipLayouter(mDropdownChipLayouter);
     }
 
+    private void announceForAccessibilityCompat(String text) {
+        if (mIsAccessibilityOn && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
+            final ViewParent parent = getParent();
+            if (parent != null) {
+                AccessibilityEvent event = AccessibilityEvent.obtain(
+                        AccessibilityEvent.TYPE_ANNOUNCEMENT);
+                onInitializeAccessibilityEvent(event);
+                event.getText().add(text);
+                event.setContentDescription(null);
+                parent.requestSendAccessibilityEvent(this, event);
+            }
+        }
+    }
+
     protected void scrollBottomIntoView() {
         if (mScrollView != null && mShouldShrink) {
             final int[] location = new int[2];