From 24fab555f93ae64040d02cf92f12e77f75d30d88 Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Mon, 9 Jun 2014 15:48:20 +0900
Subject: [PATCH] Refactor KeyCodeDescriptionMapper class a bit

This CL adds getDescriptionForCode() method.

Bug: 15500282
Change-Id: I3bb92c8f356ea623fc992c924506b11e09256e98
---
 .../strings-talkback-descriptions.xml         |  3 +-
 .../KeyCodeDescriptionMapper.java             | 68 +++++++------------
 2 files changed, 28 insertions(+), 43 deletions(-)

diff --git a/java/res/values-kk/strings-talkback-descriptions.xml b/java/res/values-kk/strings-talkback-descriptions.xml
index 13adf830d4..39388a4d77 100644
--- a/java/res/values-kk/strings-talkback-descriptions.xml
+++ b/java/res/values-kk/strings-talkback-descriptions.xml
@@ -27,7 +27,8 @@
     <skip />
     <!-- no translation found for spoken_auto_correct_obscured (6276420476908833791) -->
     <skip />
-    <string name="spoken_description_unknown" msgid="3197434010402179157">"Перне коды %d"</string>
+    <!-- no translation found for spoken_description_unknown (5139930082759824442) -->
+    <skip />
     <string name="spoken_description_shift" msgid="244197883292549308">"Shift"</string>
     <string name="spoken_description_shift_shifted" msgid="1681877323344195035">"Shift қосулы (өшіру үшін түрту)"</string>
     <string name="spoken_description_caps_lock" msgid="3276478269526304432">"Caps lock қосулы (өшіру үшін түрту)"</string>
diff --git a/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java b/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java
index 3a64531289..7a3510ee12 100644
--- a/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java
+++ b/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java
@@ -28,6 +28,7 @@ import com.android.inputmethod.keyboard.Keyboard;
 import com.android.inputmethod.keyboard.KeyboardId;
 import com.android.inputmethod.latin.Constants;
 import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.utils.StringUtils;
 
 import java.util.Locale;
 
@@ -79,14 +80,6 @@ final class KeyCodeDescriptionMapper {
     /**
      * Returns the localized description of the action performed by a specified
      * key based on the current keyboard state.
-     * <p>
-     * The order of precedence for key descriptions is:
-     * <ol>
-     * <li>Manually-defined based on the key label</li>
-     * <li>Automatic or manually-defined based on the key code</li>
-     * <li>Automatically based on the key label</li>
-     * <li>{code null} for keys with no label or key code defined</li>
-     * </p>
      *
      * @param context The package's context.
      * @param keyboard The keyboard on which the key resides.
@@ -121,7 +114,20 @@ final class KeyCodeDescriptionMapper {
 
         // Just attempt to speak the description.
         if (code != Constants.CODE_UNSPECIFIED) {
-            return getDescriptionForKeyCode(context, keyboard, key, shouldObscure);
+            // If the key description should be obscured, now is the time to do it.
+            final boolean isDefinedNonCtrl = Character.isDefined(code)
+                    && !Character.isISOControl(code);
+            if (shouldObscure && isDefinedNonCtrl) {
+                return context.getString(OBSCURED_KEY_RES_ID);
+            }
+            final String description = getDescriptionForCodePoint(context, code);
+            if (description != null) {
+                return description;
+            }
+            if (!TextUtils.isEmpty(key.getLabel())) {
+                return key.getLabel();
+            }
+            return context.getString(R.string.spoken_description_unknown);
         }
         return null;
     }
@@ -247,57 +253,35 @@ final class KeyCodeDescriptionMapper {
 
     /**
      * Returns a localized character sequence describing what will happen when
-     * the specified key is pressed based on its key code.
-     * <p>
-     * The order of precedence for key code descriptions is:
-     * <ol>
-     * <li>Manually-defined shift-locked description</li>
-     * <li>Manually-defined shifted description</li>
-     * <li>Manually-defined normal description</li>
-     * <li>Automatic based on the character represented by the key code</li>
-     * <li>Fall-back for undefined or control characters</li>
-     * </ol>
-     * </p>
+     * the specified key is pressed based on its key code point.
      *
      * @param context The package's context.
-     * @param keyboard The keyboard on which the key resides.
-     * @param key The key from which to obtain a description.
-     * @param shouldObscure {@true} if text (e.g. non-control) characters should be obscured.
-     * @return a character sequence describing the action performed by pressing the key
+     * @param codePoint The code point from which to obtain a description.
+     * @return a character sequence describing the code point.
      */
-    private String getDescriptionForKeyCode(final Context context, final Keyboard keyboard,
-            final Key key, final boolean shouldObscure) {
-        final int code = key.getCode();
-
+    public String getDescriptionForCodePoint(final Context context, final int codePoint) {
         // If the key description should be obscured, now is the time to do it.
-        final boolean isDefinedNonCtrl = Character.isDefined(code) && !Character.isISOControl(code);
-        if (shouldObscure && isDefinedNonCtrl) {
-            return context.getString(OBSCURED_KEY_RES_ID);
-        }
-        final int index = mKeyCodeMap.indexOfKey(code);
+        final int index = mKeyCodeMap.indexOfKey(codePoint);
         if (index >= 0) {
             return context.getString(mKeyCodeMap.valueAt(index));
         }
-        final String accentedLetter = getSpokenAccentedLetterDescription(context, code);
+        final String accentedLetter = getSpokenAccentedLetterDescription(context, codePoint);
         if (accentedLetter != null) {
             return accentedLetter;
         }
         // Here, <code>code</code> may be a base (non-accented) letter.
-        final String unsupportedSymbol = getSpokenSymbolDescription(context, code);
+        final String unsupportedSymbol = getSpokenSymbolDescription(context, codePoint);
         if (unsupportedSymbol != null) {
             return unsupportedSymbol;
         }
-        final String emojiDescription = getSpokenEmojiDescription(context, code);
+        final String emojiDescription = getSpokenEmojiDescription(context, codePoint);
         if (emojiDescription != null) {
             return emojiDescription;
         }
-        if (isDefinedNonCtrl) {
-            return Character.toString((char) code);
-        }
-        if (!TextUtils.isEmpty(key.getLabel())) {
-            return key.getLabel();
+        if (Character.isDefined(codePoint) && !Character.isISOControl(codePoint)) {
+            return StringUtils.newSingleCodePointString(codePoint);
         }
-        return context.getString(R.string.spoken_description_unknown, code);
+        return null;
     }
 
     // TODO: Remove this method once TTS supports those accented letters' verbalization.
-- 
GitLab