From 761d3ff06c74a4da50a395f6269ea8b81d60f51f Mon Sep 17 00:00:00 2001 From: Amith Yamasani Date: Thu, 14 Dec 2017 17:50:03 -0800 Subject: Duration format for tunable settings For JobScheduler, DeviceIdle and AppStandby constants, allow using a more compact format than milliseconds, which are a PITA to calculate. So instead of 18640000000... whatever, you can use PT2H (for 2 hours), or P2D (for 2 days), etc. Uses Duration.parse() to do the parsing. See Duration for format. Test: adb shell settings put global app_standby_constants screen_thresholds=0/PT2H/PT12H/P2D Fixes: 71554131 Change-Id: I5141854ec7df6de266725a67f1f3e2a6e0b4c1c1 --- core/java/android/util/KeyValueListParser.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'core/java/android') diff --git a/core/java/android/util/KeyValueListParser.java b/core/java/android/util/KeyValueListParser.java index 0a00794a1471..7eef63efd14d 100644 --- a/core/java/android/util/KeyValueListParser.java +++ b/core/java/android/util/KeyValueListParser.java @@ -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; + } } -- cgit v1.2.3