From 6d3d2ae0647792c60728b916e25321e93f6f971f Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Fri, 10 May 2013 17:44:39 +0900 Subject: [PATCH] Refactor StringUtils.findPrefixedString to findValueOfKey Bug: 8556975 Change-Id: I275c99db8a0d10594a5a8c0c27ebc1a87495c307 --- java/res/values/keyboard-heights.xml | 12 ++-- .../values/keypress-vibration-durations.xml | 10 ++-- java/res/values/keypress-volumes.xml | 14 ++--- .../phantom-sudden-move-event-device-list.xml | 4 +- ...sudden-jumping-touch-event-device-list.xml | 6 +- .../inputmethod/latin/ResourceUtils.java | 14 ++--- .../inputmethod/latin/StringUtils.java | 21 ++++--- .../inputmethod/latin/StringUtilsTests.java | 59 +++++++++++++++++++ 8 files changed, 103 insertions(+), 37 deletions(-) diff --git a/java/res/values/keyboard-heights.xml b/java/res/values/keyboard-heights.xml index 418d3e5b6..1c0277c2c 100644 --- a/java/res/values/keyboard-heights.xml +++ b/java/res/values/keyboard-heights.xml @@ -19,20 +19,20 @@ --> - + - sholes,227.0167 + HARDWARE=sholes,227.0167 - mahimahi,217.5932 + HARDWARE=mahimahi,217.5932 - herring,200.8554 + HARDWARE=herring,200.8554 - tuna,202.5869 + HARDWARE=tuna,202.5869 - stingray,283.1337 + HARDWARE=stingray,283.1337 DEFAULT, diff --git a/java/res/values/keypress-vibration-durations.xml b/java/res/values/keypress-vibration-durations.xml index 10400be83..45c51e71b 100644 --- a/java/res/values/keypress-vibration-durations.xml +++ b/java/res/values/keypress-vibration-durations.xml @@ -18,16 +18,16 @@ */ --> - + - herring,5 + HARDWARE=herring,5 - tuna,5 + HARDWARE=tuna,5 - mako,5 + HARDWARE=mako,5 - manta,16 + HARDWARE=manta,16 DEFAULT,20 diff --git a/java/res/values/keypress-volumes.xml b/java/res/values/keypress-volumes.xml index 047fe0c5f..7061f1310 100644 --- a/java/res/values/keypress-volumes.xml +++ b/java/res/values/keypress-volumes.xml @@ -18,14 +18,14 @@ */ --> + - - herring,0.5f - tuna,0.5f - stingray,0.4f - grouper,0.3f - mako,0.3f - manta,0.2f + HARDWARE=herring,0.5f + HARDWARE=tuna,0.5f + HARDWARE=stingray,0.4f + HARDWARE=grouper,0.3f + HARDWARE=mako,0.3f + HARDWARE=manta,0.2f DEFAULT,0.2f 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 22f510229..d0895b158 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 @@ --> - - stingray,true + HARDWARE=stingray,true DEFAULT,false 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 3fdc0c7e3..73e30c17d 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 @@ --> - - mahimahi,true + HARDWARE=mahimahi,true - sholes,true + HARDWARE=sholes,true DEFAULT,false diff --git a/java/src/com/android/inputmethod/latin/ResourceUtils.java b/java/src/com/android/inputmethod/latin/ResourceUtils.java index b74b979b5..f0bfe75fe 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 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 d5ee58a63..5ff101f7a 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 1e3cc8ad4..b6a17a3a3 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"); + } + } }