From 53a237323a3f5ba6e5ece341874812ac91bbbfc0 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 14 Feb 2014 18:44:34 +0900 Subject: [PATCH] Add sanity check to KeybaordTextsTable for debugging Bug: 13007104 Change-Id: Id12f75da7a1c6a2a078d41721ee4016794e9dd2a --- .../keyboard/internal/KeyboardTextsSet.java | 4 +-- .../keyboard/internal/KeyboardTextsTable.java | 29 ++++++++++++++----- .../keyboard/internal/KeyboardTextsTable.tmpl | 29 ++++++++++++++----- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java index 27e3e3006..89221ba24 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java @@ -85,13 +85,13 @@ public final class KeyboardTextsSet { do { level++; if (level >= MAX_STRING_REFERENCE_INDIRECTION) { - throw new RuntimeException("too many @string/resource indirection: " + text); + throw new RuntimeException("Too many " + PREFIX_TEXT + "name indirection: " + text); } final int prefixLen = PREFIX_TEXT.length(); final int size = text.length(); if (size < prefixLen) { - return TextUtils.isEmpty(text) ? null : text; + break; } sb = null; diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java index a7bcef0f1..93d80c909 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java @@ -43,22 +43,36 @@ import java.util.HashMap; public final class KeyboardTextsTable { // Name to index map. private static final HashMap sNameToIndexesMap = CollectionUtils.newHashMap(); - // Language to texts map. - private static final HashMap sLanguageToTextsMap = + // Language to texts table map. + private static final HashMap sLanguageToTextsTableMap = + CollectionUtils.newHashMap(); + // TODO: Remove this variable after debugging. + // Texts table to language maps. + private static final HashMap sTextsTableToLanguageMap = CollectionUtils.newHashMap(); public static String getText(final String name, final String[] textsTable) { final Integer indexObj = sNameToIndexesMap.get(name); if (indexObj == null) { - throw new RuntimeException("Unknown text name: " + name); + throw new RuntimeException("Unknown text name=" + name + " language=" + + sTextsTableToLanguageMap.get(textsTable)); } final int index = indexObj; final String text = (index < textsTable.length) ? textsTable[index] : null; - return (text != null) ? text : LANGUAGE_DEFAULT[index]; + if (text != null) { + return text; + } + // Sanity check. + if (index >= 0 && index < LANGUAGE_DEFAULT.length) { + return LANGUAGE_DEFAULT[index]; + } + // Throw exception for debugging purpose. + throw new RuntimeException("Illegal index=" + index + " for name=" + name + + " language=" + sTextsTableToLanguageMap.get(textsTable)); } public static String[] getTextsTable(final String language) { - final String[] textsTable = sLanguageToTextsMap.get(language); + final String[] textsTable = sLanguageToTextsTableMap.get(language); return textsTable != null ? textsTable : LANGUAGE_DEFAULT; } @@ -3632,8 +3646,9 @@ public final class KeyboardTextsTable { for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { final String language = (String)LANGUAGES_AND_TEXTS[i]; - final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1]; - sLanguageToTextsMap.put(language, texts); + final String[] textsTable = (String[])LANGUAGES_AND_TEXTS[i + 1]; + sLanguageToTextsTableMap.put(language, textsTable); + sTextsTableToLanguageMap.put(textsTable, language); } } } diff --git a/tools/make-keyboard-text/res/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl b/tools/make-keyboard-text/res/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl index f56cfd91b..7ec42ceea 100644 --- a/tools/make-keyboard-text/res/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl +++ b/tools/make-keyboard-text/res/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl @@ -43,22 +43,36 @@ import java.util.HashMap; public final class KeyboardTextsTable { // Name to index map. private static final HashMap sNameToIndexesMap = CollectionUtils.newHashMap(); - // Language to texts map. - private static final HashMap sLanguageToTextsMap = + // Language to texts table map. + private static final HashMap sLanguageToTextsTableMap = + CollectionUtils.newHashMap(); + // TODO: Remove this variable after debugging. + // Texts table to language maps. + private static final HashMap sTextsTableToLanguageMap = CollectionUtils.newHashMap(); public static String getText(final String name, final String[] textsTable) { final Integer indexObj = sNameToIndexesMap.get(name); if (indexObj == null) { - throw new RuntimeException("Unknown text name: " + name); + throw new RuntimeException("Unknown text name=" + name + " language=" + + sTextsTableToLanguageMap.get(textsTable)); } final int index = indexObj; final String text = (index < textsTable.length) ? textsTable[index] : null; - return (text != null) ? text : LANGUAGE_DEFAULT[index]; + if (text != null) { + return text; + } + // Sanity check. + if (index >= 0 && index < LANGUAGE_DEFAULT.length) { + return LANGUAGE_DEFAULT[index]; + } + // Throw exception for debugging purpose. + throw new RuntimeException("Illegal index=" + index + " for name=" + name + + " language=" + sTextsTableToLanguageMap.get(textsTable)); } public static String[] getTextsTable(final String language) { - final String[] textsTable = sLanguageToTextsMap.get(language); + final String[] textsTable = sLanguageToTextsTableMap.get(language); return textsTable != null ? textsTable : LANGUAGE_DEFAULT; } @@ -89,8 +103,9 @@ public final class KeyboardTextsTable { for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { final String language = (String)LANGUAGES_AND_TEXTS[i]; - final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1]; - sLanguageToTextsMap.put(language, texts); + final String[] textsTable = (String[])LANGUAGES_AND_TEXTS[i + 1]; + sLanguageToTextsTableMap.put(language, textsTable); + sTextsTableToLanguageMap.put(textsTable, language); } } }