diff --git a/java/res/values/keyboard-heights.xml b/java/res/values/keyboard-heights.xml index 418d3e5b6cc4e6fb7cc0c4e620e09de31f0eeda2..1c0277c2c8d12b311e0c42a6f4ccaa94fc62919f 100644 --- a/java/res/values/keyboard-heights.xml +++ b/java/res/values/keyboard-heights.xml @@ -19,20 +19,20 @@ --> <resources> - <!-- Build.HARDWARE,keyboard_height_in_dp --> + <!-- Build condition,keyboard_height_in_dp --> <string-array name="keyboard_heights" translatable="false"> <!-- Preferable keyboard height in absolute scale: 1.285in --> <!-- Droid --> - <item>sholes,227.0167</item> + <item>HARDWARE=sholes,227.0167</item> <!-- Nexus One --> - <item>mahimahi,217.5932</item> + <item>HARDWARE=mahimahi,217.5932</item> <!-- Nexus S --> - <item>herring,200.8554</item> + <item>HARDWARE=herring,200.8554</item> <!-- Galaxy Nexus --> - <item>tuna,202.5869</item> + <item>HARDWARE=tuna,202.5869</item> <!-- Preferable keyboard height in absolute scale: 48.0mm --> <!-- Xoom --> - <item>stingray,283.1337</item> + <item>HARDWARE=stingray,283.1337</item> <!-- Default value for unknown device: empty string --> <item>DEFAULT,</item> </string-array> diff --git a/java/res/values/keypress-vibration-durations.xml b/java/res/values/keypress-vibration-durations.xml index 10400be83176d0e1cb9d51963929769f06e004f4..45c51e71bdfcdfdb025b7355a2efc4576a8a7aae 100644 --- a/java/res/values/keypress-vibration-durations.xml +++ b/java/res/values/keypress-vibration-durations.xml @@ -18,16 +18,16 @@ */ --> <resources> - <!-- Build.HARDWARE,duration_in_milliseconds --> + <!-- Build condition,duration_in_milliseconds --> <string-array name="keypress_vibration_durations" translatable="false"> <!-- Nexus S --> - <item>herring,5</item> + <item>HARDWARE=herring,5</item> <!-- Galaxy Nexus --> - <item>tuna,5</item> + <item>HARDWARE=tuna,5</item> <!-- Nexus 4 --> - <item>mako,5</item> + <item>HARDWARE=mako,5</item> <!-- Nexus 10 --> - <item>manta,16</item> + <item>HARDWARE=manta,16</item> <!-- Default value for unknown device --> <item>DEFAULT,20</item> </string-array> diff --git a/java/res/values/keypress-volumes.xml b/java/res/values/keypress-volumes.xml index 047fe0c5f6868c4819753404c6a5f7acb50607c8..7061f13107e5b652d1240f2e25975df8569ab739 100644 --- a/java/res/values/keypress-volumes.xml +++ b/java/res/values/keypress-volumes.xml @@ -18,14 +18,14 @@ */ --> <resources> + <!-- Build condition,volume --> <string-array name="keypress_volumes" translatable="false"> - <!-- Build.HARDWARE,volume --> - <item>herring,0.5f</item> - <item>tuna,0.5f</item> - <item>stingray,0.4f</item> - <item>grouper,0.3f</item> - <item>mako,0.3f</item> - <item>manta,0.2f</item> + <item>HARDWARE=herring,0.5f</item> + <item>HARDWARE=tuna,0.5f</item> + <item>HARDWARE=stingray,0.4f</item> + <item>HARDWARE=grouper,0.3f</item> + <item>HARDWARE=mako,0.3f</item> + <item>HARDWARE=manta,0.2f</item> <!-- Default value for unknown device --> <item>DEFAULT,0.2f</item> </string-array> 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 22f510229c3c5a87ee08dfb7513b95add59ae063..d0895b15811e481512263005b77431a82577c169 100644 --- a/java/res/values/phantom-sudden-move-event-device-list.xml +++ b/java/res/values/phantom-sudden-move-event-device-list.xml @@ -19,10 +19,10 @@ --> <resources> <string-array name="phantom_sudden_move_event_device_list" translatable="false"> - <!-- "Build.HARDWARE,true" that needs "phantom sudden move event" hack. + <!-- "Build condition,true" that needs "phantom sudden move event" hack. See {@link com.android.inputmethod.keyboard.PointerTracker}. --> <!-- Xoom --> - <item>stingray,true</item> + <item>HARDWARE=stingray,true</item> <!-- Default value for unknown device --> <item>DEFAULT,false</item> </string-array> 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 3fdc0c7e3bfdc49285fecfdea11a0d40478179b6..73e30c17d8618801b4633ba46ae71c07e0a69594 100644 --- a/java/res/values/sudden-jumping-touch-event-device-list.xml +++ b/java/res/values/sudden-jumping-touch-event-device-list.xml @@ -19,12 +19,12 @@ --> <resources> <string-array name="sudden_jumping_touch_event_device_list" translatable="false"> - <!-- "Build.HARDWARE,true" that needs "sudden jump touch event" hack. + <!-- "Build condition,true" that needs "sudden jump touch event" hack. See {@link com.android.inputmethod.keyboard.SuddenJumpingTouchEventHandler}. --> <!-- Nexus One --> - <item>mahimahi,true</item> + <item>HARDWARE=mahimahi,true</item> <!-- Droid --> - <item>sholes,true</item> + <item>HARDWARE=sholes,true</item> <!-- Default value for unknown device --> <item>DEFAULT,false</item> </string-array> diff --git a/java/src/com/android/inputmethod/latin/ResourceUtils.java b/java/src/com/android/inputmethod/latin/ResourceUtils.java index b74b979b5f0269a62fa8684e24ed2aae6557a987..f0bfe75fe432cd59f8568ef4b7bc731e8d9e8306 100644 --- a/java/src/com/android/inputmethod/latin/ResourceUtils.java +++ b/java/src/com/android/inputmethod/latin/ResourceUtils.java @@ -35,8 +35,7 @@ public final class ResourceUtils { // This utility class is not publicly instantiable. } - private static final String DEFAULT_PREFIX = "DEFAULT,"; - private static final String HARDWARE_PREFIX = Build.HARDWARE + ","; + private static final String DEFAULT_KEY = "DEFAULT"; private static final HashMap<String, String> sDeviceOverrideValueMap = CollectionUtils.newHashMap(); @@ -48,28 +47,29 @@ public final class ResourceUtils { } final String[] overrideArray = res.getStringArray(overrideResId); - final String overrideValue = StringUtils.findPrefixedString(HARDWARE_PREFIX, overrideArray); + final String hardwareKey = "HARDWARE=" + Build.HARDWARE; + final String overrideValue = StringUtils.findValueOfKey(hardwareKey, overrideArray); // The overrideValue might be an empty string. if (overrideValue != null) { if (DEBUG) { Log.d(TAG, "Find override value:" + " resource="+ res.getResourceEntryName(overrideResId) - + " Build.HARDWARE=" + Build.HARDWARE + " override=" + overrideValue); + + " " + hardwareKey + " override=" + overrideValue); } sDeviceOverrideValueMap.put(key, overrideValue); return overrideValue; } - final String defaultValue = StringUtils.findPrefixedString(DEFAULT_PREFIX, overrideArray); + final String defaultValue = StringUtils.findValueOfKey(DEFAULT_KEY, 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) - + " Build.HARDWARE=" + Build.HARDWARE); + + " " + hardwareKey); } else if (DEBUG) { Log.d(TAG, "Found default value:" + " resource="+ res.getResourceEntryName(overrideResId) - + " Build.HARDWARE=" + Build.HARDWARE + " default=" + defaultValue); + + " " + hardwareKey + " " + DEFAULT_KEY + "=" + defaultValue); } sDeviceOverrideValueMap.put(key, defaultValue); return defaultValue; diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java index d5ee58a63c45ff903e8a841ea9d1d3588928700e..5ff101f7af7954a0101c96634c380abe0228c557 100644 --- a/java/src/com/android/inputmethod/latin/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/StringUtils.java @@ -65,17 +65,24 @@ public final class StringUtils { } /** - * Find a string that start with specified prefix from an array. + * Find a value that has a specified key from an array of key-comma-value. * - * @param prefix a prefix string to find. - * @param array an string array to be searched. - * @return the rest part of the string that starts with the prefix. + * @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 findPrefixedString(final String prefix, final String[] array) { + public static String findValueOfKey(final String key, final String[] array) { + if (array == null) { + return null; + } for (final String element : array) { - if (element.startsWith(prefix)) { - return element.substring(prefix.length()); + 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; diff --git a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java index 1e3cc8ad473bff22d93a75633c31545fbea63654..b6a17a3a3639a189ed5575f467f191a8d0afcced 100644 --- a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java @@ -237,4 +237,63 @@ public class StringUtilsTests extends AndroidTestCase { // code for now True is acceptable. assertTrue(StringUtils.lastPartLooksLikeURL(".abc/def")); } + + public void testFindValueOfKey() { + final String nullKey = null; + final String emptyKey = ""; + + final String[] nullArray = null; + assertNull(StringUtils.findValueOfKey("anyKey", nullArray)); + assertNull(StringUtils.findValueOfKey(emptyKey, nullArray)); + assertNull(StringUtils.findValueOfKey(nullKey, nullArray)); + + final String[] emptyArray = {}; + assertNull(StringUtils.findValueOfKey("anyKey", emptyArray)); + assertNull(StringUtils.findValueOfKey(emptyKey, emptyArray)); + assertNull(StringUtils.findValueOfKey(nullKey, emptyArray)); + + final String[] array = { + "DEFAULT,defaultValue", + "HARDWARE=grouper,0.3", + "HARDWARE=mako,0.4", + "HARDWARE=manta,0.2" + }; + assertEquals(StringUtils.findValueOfKey("HARDWARE=grouper", array), "0.3"); + assertEquals(StringUtils.findValueOfKey("HARDWARE=mako", array), "0.4"); + assertEquals(StringUtils.findValueOfKey("HARDWARE=manta", array), "0.2"); + assertEquals(StringUtils.findValueOfKey("DEFAULT", array), "defaultValue"); + + assertNull(StringUtils.findValueOfKey("hardware=grouper", array)); + assertNull(StringUtils.findValueOfKey("HARDWARE=MAKO", array)); + assertNull(StringUtils.findValueOfKey("HARDWARE=mantaray", array)); + assertNull(StringUtils.findValueOfKey(emptyKey, array)); + assertNull(StringUtils.findValueOfKey(nullKey, array)); + + final String[] containsNullKey = { + "DEFAULT,defaultValue", + ",emptyValue" + }; + assertEquals(StringUtils.findValueOfKey(emptyKey, containsNullKey), "emptyValue"); + + final String[] containsMultipleSameKeys = { + "key1,value1", + "key2,value2", + "key3,value3", + "key2,value4" + }; + assertEquals(StringUtils.findValueOfKey("key2", containsMultipleSameKeys), "value2"); + + final String[] containNoCommaElement = { + "key1,value1", + "key2-and-value2", + "key3,value3" + }; + assertEquals(StringUtils.findValueOfKey("key1", containNoCommaElement), "value1"); + try { + final String valueOfKey3 = StringUtils.findValueOfKey("key3", containNoCommaElement); + fail("finding valueOfKey3=" + valueOfKey3 + " must fail"); + } catch (final RuntimeException e) { + assertEquals(e.getMessage(), "Element has no comma: key2-and-value2"); + } + } }