am 565ced45: Merge "Refactor StringUtils.findPrefixedString to findValueOfKey"

* commit '565ced4546420e9e17c7747f9e207ca6773598e3':
  Refactor StringUtils.findPrefixedString to findValueOfKey
main
Tadashi G. Takaoka 2013-05-10 02:50:12 -07:00 committed by Android Git Automerger
commit a01ab9ea35
8 changed files with 103 additions and 37 deletions

View File

@ -19,20 +19,20 @@
--> -->
<resources> <resources>
<!-- Build.HARDWARE,keyboard_height_in_dp --> <!-- Build condition,keyboard_height_in_dp -->
<string-array name="keyboard_heights" translatable="false"> <string-array name="keyboard_heights" translatable="false">
<!-- Preferable keyboard height in absolute scale: 1.285in --> <!-- Preferable keyboard height in absolute scale: 1.285in -->
<!-- Droid --> <!-- Droid -->
<item>sholes,227.0167</item> <item>HARDWARE=sholes,227.0167</item>
<!-- Nexus One --> <!-- Nexus One -->
<item>mahimahi,217.5932</item> <item>HARDWARE=mahimahi,217.5932</item>
<!-- Nexus S --> <!-- Nexus S -->
<item>herring,200.8554</item> <item>HARDWARE=herring,200.8554</item>
<!-- Galaxy Nexus --> <!-- Galaxy Nexus -->
<item>tuna,202.5869</item> <item>HARDWARE=tuna,202.5869</item>
<!-- Preferable keyboard height in absolute scale: 48.0mm --> <!-- Preferable keyboard height in absolute scale: 48.0mm -->
<!-- Xoom --> <!-- Xoom -->
<item>stingray,283.1337</item> <item>HARDWARE=stingray,283.1337</item>
<!-- Default value for unknown device: empty string --> <!-- Default value for unknown device: empty string -->
<item>DEFAULT,</item> <item>DEFAULT,</item>
</string-array> </string-array>

View File

@ -18,16 +18,16 @@
*/ */
--> -->
<resources> <resources>
<!-- Build.HARDWARE,duration_in_milliseconds --> <!-- Build condition,duration_in_milliseconds -->
<string-array name="keypress_vibration_durations" translatable="false"> <string-array name="keypress_vibration_durations" translatable="false">
<!-- Nexus S --> <!-- Nexus S -->
<item>herring,5</item> <item>HARDWARE=herring,5</item>
<!-- Galaxy Nexus --> <!-- Galaxy Nexus -->
<item>tuna,5</item> <item>HARDWARE=tuna,5</item>
<!-- Nexus 4 --> <!-- Nexus 4 -->
<item>mako,5</item> <item>HARDWARE=mako,5</item>
<!-- Nexus 10 --> <!-- Nexus 10 -->
<item>manta,16</item> <item>HARDWARE=manta,16</item>
<!-- Default value for unknown device --> <!-- Default value for unknown device -->
<item>DEFAULT,20</item> <item>DEFAULT,20</item>
</string-array> </string-array>

View File

@ -18,14 +18,14 @@
*/ */
--> -->
<resources> <resources>
<!-- Build condition,volume -->
<string-array name="keypress_volumes" translatable="false"> <string-array name="keypress_volumes" translatable="false">
<!-- Build.HARDWARE,volume --> <item>HARDWARE=herring,0.5f</item>
<item>herring,0.5f</item> <item>HARDWARE=tuna,0.5f</item>
<item>tuna,0.5f</item> <item>HARDWARE=stingray,0.4f</item>
<item>stingray,0.4f</item> <item>HARDWARE=grouper,0.3f</item>
<item>grouper,0.3f</item> <item>HARDWARE=mako,0.3f</item>
<item>mako,0.3f</item> <item>HARDWARE=manta,0.2f</item>
<item>manta,0.2f</item>
<!-- Default value for unknown device --> <!-- Default value for unknown device -->
<item>DEFAULT,0.2f</item> <item>DEFAULT,0.2f</item>
</string-array> </string-array>

View File

@ -19,10 +19,10 @@
--> -->
<resources> <resources>
<string-array name="phantom_sudden_move_event_device_list" translatable="false"> <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}. --> See {@link com.android.inputmethod.keyboard.PointerTracker}. -->
<!-- Xoom --> <!-- Xoom -->
<item>stingray,true</item> <item>HARDWARE=stingray,true</item>
<!-- Default value for unknown device --> <!-- Default value for unknown device -->
<item>DEFAULT,false</item> <item>DEFAULT,false</item>
</string-array> </string-array>

View File

@ -19,12 +19,12 @@
--> -->
<resources> <resources>
<string-array name="sudden_jumping_touch_event_device_list" translatable="false"> <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}. --> See {@link com.android.inputmethod.keyboard.SuddenJumpingTouchEventHandler}. -->
<!-- Nexus One --> <!-- Nexus One -->
<item>mahimahi,true</item> <item>HARDWARE=mahimahi,true</item>
<!-- Droid --> <!-- Droid -->
<item>sholes,true</item> <item>HARDWARE=sholes,true</item>
<!-- Default value for unknown device --> <!-- Default value for unknown device -->
<item>DEFAULT,false</item> <item>DEFAULT,false</item>
</string-array> </string-array>

View File

@ -35,8 +35,7 @@ public final class ResourceUtils {
// This utility class is not publicly instantiable. // This utility class is not publicly instantiable.
} }
private static final String DEFAULT_PREFIX = "DEFAULT,"; private static final String DEFAULT_KEY = "DEFAULT";
private static final String HARDWARE_PREFIX = Build.HARDWARE + ",";
private static final HashMap<String, String> sDeviceOverrideValueMap = private static final HashMap<String, String> sDeviceOverrideValueMap =
CollectionUtils.newHashMap(); CollectionUtils.newHashMap();
@ -48,28 +47,29 @@ public final class ResourceUtils {
} }
final String[] overrideArray = res.getStringArray(overrideResId); 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. // The overrideValue might be an empty string.
if (overrideValue != null) { if (overrideValue != null) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "Find override value:" Log.d(TAG, "Find override value:"
+ " resource="+ res.getResourceEntryName(overrideResId) + " resource="+ res.getResourceEntryName(overrideResId)
+ " Build.HARDWARE=" + Build.HARDWARE + " override=" + overrideValue); + " " + hardwareKey + " override=" + overrideValue);
} }
sDeviceOverrideValueMap.put(key, overrideValue); sDeviceOverrideValueMap.put(key, overrideValue);
return 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. // The defaultValue might be an empty string.
if (defaultValue == null) { if (defaultValue == null) {
Log.w(TAG, "Couldn't find override value nor default value:" Log.w(TAG, "Couldn't find override value nor default value:"
+ " resource="+ res.getResourceEntryName(overrideResId) + " resource="+ res.getResourceEntryName(overrideResId)
+ " Build.HARDWARE=" + Build.HARDWARE); + " " + hardwareKey);
} else if (DEBUG) { } else if (DEBUG) {
Log.d(TAG, "Found default value:" Log.d(TAG, "Found default value:"
+ " resource="+ res.getResourceEntryName(overrideResId) + " resource="+ res.getResourceEntryName(overrideResId)
+ " Build.HARDWARE=" + Build.HARDWARE + " default=" + defaultValue); + " " + hardwareKey + " " + DEFAULT_KEY + "=" + defaultValue);
} }
sDeviceOverrideValueMap.put(key, defaultValue); sDeviceOverrideValueMap.put(key, defaultValue);
return defaultValue; return defaultValue;

View File

@ -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 key a key string to find.
* @param array an string array to be searched. * @param array an array of key-comma-value string to be searched.
* @return the rest part of the string that starts with the prefix. * @return the value part of the first string that has a specified key.
* Returns null if it couldn't be found. * 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) { for (final String element : array) {
if (element.startsWith(prefix)) { final int posComma = element.indexOf(',');
return element.substring(prefix.length()); 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; return null;

View File

@ -237,4 +237,63 @@ public class StringUtilsTests extends AndroidTestCase {
// code for now True is acceptable. // code for now True is acceptable.
assertTrue(StringUtils.lastPartLooksLikeURL(".abc/def")); 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");
}
}
} }