Have TalkBack announce results and formula changes

Bug: 21048155
Bug: 19189356

Incrementally announce additions to the formula.

Explicitly announce result after hitting equals.

Announce when display is cleared.

Remove old FIXME comment after a bit more testing.

Change-Id: I836ff6672de3f891888b2724470290c8721d4fcf
diff --git a/src/com/android/calculator2/CalculatorText.java b/src/com/android/calculator2/CalculatorText.java
index b0bbb7c..4b0b0c9 100644
--- a/src/com/android/calculator2/CalculatorText.java
+++ b/src/com/android/calculator2/CalculatorText.java
@@ -204,6 +204,39 @@
         return lastFitTextSize;
     }
 
+    private static boolean startsWith(CharSequence whole, CharSequence prefix) {
+        int wholeLen = whole.length();
+        int prefixLen = prefix.length();
+        if (prefixLen > wholeLen) {
+            return false;
+        }
+        for (int i = 0; i < prefixLen; ++i) {
+            if (prefix.charAt(i) != whole.charAt(i)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Functionally equivalent to setText(), but explicitly announce changes.
+     * If the new text is an extension of the old one, announce the addition.
+     * Otherwise, e.g. after deletion, announce the entire new text.
+     */
+    public void changeTextTo(CharSequence newText) {
+        CharSequence oldText = getText();
+        if (startsWith(newText, oldText)) {
+            int newLen = newText.length();
+            int oldLen = oldText.length();
+            if (oldLen != newLen) {
+                announceForAccessibility(newText.subSequence(oldLen, newLen));
+            }
+        } else {
+            announceForAccessibility(newText);
+        }
+        setText(newText);
+    }
+
     public boolean stopActionMode() {
         if (mActionMode != null) {
             mActionMode.finish();