diff options
author | 2013-05-13 10:25:42 +0900 | |
---|---|---|
committer | 2013-05-13 14:38:21 +0900 | |
commit | d69fa0a09af7e9a524751dac1522c951abd92530 (patch) | |
tree | 7c6fb80ea0a1d61372e3c146b49caef34c001171 /java | |
parent | 7d72ca0b20334aba077e3a01d7b12f6f34618076 (diff) | |
download | latinime-d69fa0a09af7e9a524751dac1522c951abd92530.tar.gz latinime-d69fa0a09af7e9a524751dac1522c951abd92530.tar.xz latinime-d69fa0a09af7e9a524751dac1522c951abd92530.zip |
Support multiple condition for getDeviceOverrideValue
Bug: 8556975
Change-Id: I29d3a305b6ac3d8e3620c6d8592d85047d62bf48
Diffstat (limited to 'java')
7 files changed, 118 insertions, 36 deletions
diff --git a/java/res/values/keyboard-heights.xml b/java/res/values/keyboard-heights.xml index 1c0277c2c..c651a89b3 100644 --- a/java/res/values/keyboard-heights.xml +++ b/java/res/values/keyboard-heights.xml @@ -34,6 +34,6 @@ <!-- Xoom --> <item>HARDWARE=stingray,283.1337</item> <!-- Default value for unknown device: empty string --> - <item>DEFAULT,</item> + <item>,</item> </string-array> </resources> diff --git a/java/res/values/keypress-vibration-durations.xml b/java/res/values/keypress-vibration-durations.xml index 45c51e71b..53aad72b7 100644 --- a/java/res/values/keypress-vibration-durations.xml +++ b/java/res/values/keypress-vibration-durations.xml @@ -29,6 +29,6 @@ <!-- Nexus 10 --> <item>HARDWARE=manta,16</item> <!-- Default value for unknown device --> - <item>DEFAULT,20</item> + <item>,20</item> </string-array> </resources> diff --git a/java/res/values/keypress-volumes.xml b/java/res/values/keypress-volumes.xml index 7061f1310..a096c341b 100644 --- a/java/res/values/keypress-volumes.xml +++ b/java/res/values/keypress-volumes.xml @@ -27,6 +27,6 @@ <item>HARDWARE=mako,0.3f</item> <item>HARDWARE=manta,0.2f</item> <!-- Default value for unknown device --> - <item>DEFAULT,0.2f</item> + <item>,0.2f</item> </string-array> </resources> diff --git a/java/res/values/phantom-sudden-move-event-device-list.xml b/java/res/values/phantom-sudden-move-event-device-list.xml index d0895b158..53002b31e 100644 --- a/java/res/values/phantom-sudden-move-event-device-list.xml +++ b/java/res/values/phantom-sudden-move-event-device-list.xml @@ -24,6 +24,6 @@ <!-- Xoom --> <item>HARDWARE=stingray,true</item> <!-- Default value for unknown device --> - <item>DEFAULT,false</item> + <item>,false</item> </string-array> </resources> diff --git a/java/res/values/sudden-jumping-touch-event-device-list.xml b/java/res/values/sudden-jumping-touch-event-device-list.xml index 73e30c17d..3a9c379a7 100644 --- a/java/res/values/sudden-jumping-touch-event-device-list.xml +++ b/java/res/values/sudden-jumping-touch-event-device-list.xml @@ -26,6 +26,6 @@ <!-- Droid --> <item>HARDWARE=sholes,true</item> <!-- Default value for unknown device --> - <item>DEFAULT,false</item> + <item>,false</item> </string-array> </resources> diff --git a/java/src/com/android/inputmethod/latin/ResourceUtils.java b/java/src/com/android/inputmethod/latin/ResourceUtils.java index f0bfe75fe..488a0e313 100644 --- a/java/src/com/android/inputmethod/latin/ResourceUtils.java +++ b/java/src/com/android/inputmethod/latin/ResourceUtils.java @@ -19,9 +19,13 @@ package com.android.inputmethod.latin; import android.content.res.Resources; import android.content.res.TypedArray; import android.os.Build; +import android.text.TextUtils; import android.util.Log; import android.util.TypedValue; +import com.android.inputmethod.annotations.UsedForTesting; + +import java.util.ArrayList; import java.util.HashMap; public final class ResourceUtils { @@ -35,10 +39,31 @@ public final class ResourceUtils { // This utility class is not publicly instantiable. } - private static final String DEFAULT_KEY = "DEFAULT"; private static final HashMap<String, String> sDeviceOverrideValueMap = CollectionUtils.newHashMap(); + private static final String[] BUILD_KEYS_AND_VALUES = { + "HARDWARE", Build.HARDWARE, + "MODEL", Build.MODEL, + "MANUFACTURER", Build.MANUFACTURER + }; + private static final HashMap<String, String> sBuildKeyValues; + private static final String sBuildKeyValuesDebugString; + + static { + sBuildKeyValues = CollectionUtils.newHashMap(); + final ArrayList<String> keyValuePairs = CollectionUtils.newArrayList(); + final int keyCount = BUILD_KEYS_AND_VALUES.length / 2; + for (int i = 0; i < keyCount; i++) { + final int index = i * 2; + final String key = BUILD_KEYS_AND_VALUES[index]; + final String value = BUILD_KEYS_AND_VALUES[index + 1]; + sBuildKeyValues.put(key, value); + keyValuePairs.add(key + '=' + value); + } + sBuildKeyValuesDebugString = "[" + TextUtils.join(" ", keyValuePairs) + "]"; + } + public static String getDeviceOverrideValue(final Resources res, final int overrideResId) { final int orientation = res.getConfiguration().orientation; final String key = overrideResId + "-" + orientation; @@ -47,34 +72,115 @@ public final class ResourceUtils { } final String[] overrideArray = res.getStringArray(overrideResId); - final String hardwareKey = "HARDWARE=" + Build.HARDWARE; - final String overrideValue = StringUtils.findValueOfKey(hardwareKey, overrideArray); + final String overrideValue = findConstantForKeyValuePairs(sBuildKeyValues, overrideArray); // The overrideValue might be an empty string. if (overrideValue != null) { if (DEBUG) { Log.d(TAG, "Find override value:" + " resource="+ res.getResourceEntryName(overrideResId) - + " " + hardwareKey + " override=" + overrideValue); + + " build=" + sBuildKeyValuesDebugString + + " override=" + overrideValue); } sDeviceOverrideValueMap.put(key, overrideValue); return overrideValue; } - final String defaultValue = StringUtils.findValueOfKey(DEFAULT_KEY, overrideArray); + final String defaultValue = findDefaultConstant(overrideArray); // The defaultValue might be an empty string. if (defaultValue == null) { Log.w(TAG, "Couldn't find override value nor default value:" + " resource="+ res.getResourceEntryName(overrideResId) - + " " + hardwareKey); + + " build=" + sBuildKeyValuesDebugString); } else if (DEBUG) { Log.d(TAG, "Found default value:" + " resource="+ res.getResourceEntryName(overrideResId) - + " " + hardwareKey + " " + DEFAULT_KEY + "=" + defaultValue); + + " build=" + sBuildKeyValuesDebugString + " default=" + defaultValue); } sDeviceOverrideValueMap.put(key, defaultValue); return defaultValue; } + /** + * Find the condition that fulfills specified key value pairs from an array of + * "condition,constant", and return the corresponding string constant. A condition is + * "pattern1[:pattern2...] (or an empty string for the default). A pattern is "key=value" + * string. The condition matches only if all patterns of the condition are true for the + * specified key value pairs. + * + * For example, "condition,constant" has the following format. + * (See {@link ResourceUtilsTests#testFindConstantForKeyValuePairsCombined()}) + * - HARDWARE=mako,constantForNexus4 + * - MODEL=Nexus 4:MANUFACTURER=LGE,constantForNexus4 + * - ,defaultConstant + * + * @param keyValuePairs attributes to be used to look for a matched condition. + * @param conditionConstantArray an array of "condition,constant" elements to be searched. + * @return the constant part of the matched "condition,constant" element. Returns null if no + * condition matches. + */ + @UsedForTesting + static String findConstantForKeyValuePairs(final HashMap<String, String> keyValuePairs, + final String[] conditionConstantArray) { + if (conditionConstantArray == null || keyValuePairs == null) { + return null; + } + for (final String conditionConstant : conditionConstantArray) { + final int posComma = conditionConstant.indexOf(','); + if (posComma < 0) { + throw new RuntimeException("Array element has no comma: " + conditionConstant); + } + final String condition = conditionConstant.substring(0, posComma); + if (condition.isEmpty()) { + // Default condition. The default condition should be searched by + // {@link #findConstantForDefault(String[])}. + continue; + } + if (fulfillsCondition(keyValuePairs, condition)) { + return conditionConstant.substring(posComma + 1); + } + } + return null; + } + + private static boolean fulfillsCondition(final HashMap<String,String> keyValuePairs, + final String condition) { + final String[] patterns = condition.split(":"); + // Check all patterns in a condition are true + for (final String pattern : patterns) { + final int posEqual = pattern.indexOf('='); + if (posEqual < 0) { + throw new RuntimeException("Pattern has no '=': " + condition); + } + final String key = pattern.substring(0, posEqual); + final String value = keyValuePairs.get(key); + if (value == null) { + throw new RuntimeException("Found unknown key: " + condition); + } + final String patternValue = pattern.substring(posEqual + 1); + if (!value.equals(patternValue)) { + return false; + } + } + return true; + } + + @UsedForTesting + static String findDefaultConstant(final String[] conditionConstantArray) { + if (conditionConstantArray == null) { + return null; + } + for (final String condition : conditionConstantArray) { + final int posComma = condition.indexOf(','); + if (posComma < 0) { + throw new RuntimeException("Array element has no comma: " + condition); + } + if (posComma == 0) { // condition is empty. + return condition.substring(posComma + 1); + } + } + return null; + } + public static boolean isValidFraction(final float fraction) { return fraction >= 0.0f; } diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java index 5ff101f7a..ab050d7a3 100644 --- a/java/src/com/android/inputmethod/latin/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/StringUtils.java @@ -65,30 +65,6 @@ public final class StringUtils { } /** - * Find a value that has a specified key from an array of key-comma-value. - * - * @param key a key string to find. - * @param array an array of key-comma-value string to be searched. - * @return the value part of the first string that has a specified key. - * Returns null if it couldn't be found. - */ - public static String findValueOfKey(final String key, final String[] array) { - if (array == null) { - return null; - } - for (final String element : array) { - final int posComma = element.indexOf(','); - if (posComma < 0) { - throw new RuntimeException("Element has no comma: " + element); - } - if (element.substring(0, posComma).equals(key)) { - return element.substring(posComma + 1); - } - } - return null; - } - - /** * Remove duplicates from an array of strings. * * This method will always keep the first occurrence of all strings at their position |