Merge "Duration format for tunable settings"

This commit is contained in:
TreeHugger Robot
2018-01-09 03:19:03 +00:00
committed by Android (Google) Code Review
4 changed files with 62 additions and 29 deletions

View File

@@ -17,6 +17,9 @@ package android.util;
import android.text.TextUtils;
import java.time.Duration;
import java.time.format.DateTimeParseException;
/**
* Parses a list of key=value pairs, separated by some delimiter, and puts the results in
* an internal Map. Values can be then queried by key, or if not found, a default value
@@ -189,4 +192,24 @@ public class KeyValueListParser {
public String keyAt(int index) {
return mValues.keyAt(index);
}
/**
* {@hide}
* Parse a duration in millis based on java.time.Duration or just a number (millis)
*/
public long getDurationMillis(String key, long def) {
String value = mValues.get(key);
if (value != null) {
try {
if (value.startsWith("P") || value.startsWith("p")) {
return Duration.parse(value).toMillis();
} else {
return Long.parseLong(value);
}
} catch (NumberFormatException | DateTimeParseException e) {
// fallthrough
}
}
return def;
}
}