From ba5b3020722a1ae5e444e460eabc2c6bc2d5ab8b Mon Sep 17 00:00:00 2001 From: Jared Duke Date: Tue, 26 Oct 2021 14:50:29 -0700 Subject: [PATCH] Avoid intermediate exceptions in Settings queries Update the implementations of all Settings.*.getInt and Settings.*.getLong variants to avoid calling parse on null string values. This avoids an unnecessary intermediate NumberFormatException and associated overhead for a relatively common case, and matches the behavior of Settings.*.getFloat. There should be no user-facing behavioral differences with this change. Also consolidate the parsing logic into common methods as they're repeated 3x for every type. Bug: 204223378 Test: m RunSettingsRoboTests Change-Id: I5438c37f37d34645e7e9b02a43add505c376b360 --- core/java/android/provider/Settings.java | 195 ++++++++++------------- 1 file changed, 84 insertions(+), 111 deletions(-) diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 9d4a24936eb80..0348107e151d8 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -3316,6 +3316,66 @@ public final class Settings { } } + private static float parseFloatSetting(String settingValue, String settingName) + throws SettingNotFoundException { + if (settingValue == null) { + throw new SettingNotFoundException(settingName); + } + try { + return Float.parseFloat(settingValue); + } catch (NumberFormatException e) { + throw new SettingNotFoundException(settingName); + } + } + + private static float parseFloatSettingWithDefault(String settingValue, float defaultValue) { + try { + return settingValue != null ? Float.parseFloat(settingValue) : defaultValue; + } catch (NumberFormatException e) { + return defaultValue; + } + } + + private static int parseIntSetting(String settingValue, String settingName) + throws SettingNotFoundException { + if (settingValue == null) { + throw new SettingNotFoundException(settingName); + } + try { + return Integer.parseInt(settingValue); + } catch (NumberFormatException e) { + throw new SettingNotFoundException(settingName); + } + } + + private static int parseIntSettingWithDefault(String settingValue, int defaultValue) { + try { + return settingValue != null ? Integer.parseInt(settingValue) : defaultValue; + } catch (NumberFormatException e) { + return defaultValue; + } + } + + private static long parseLongSetting(String settingValue, String settingName) + throws SettingNotFoundException { + if (settingValue == null) { + throw new SettingNotFoundException(settingName); + } + try { + return Long.parseLong(settingValue); + } catch (NumberFormatException e) { + throw new SettingNotFoundException(settingName); + } + } + + private static long parseLongSettingWithDefault(String settingValue, long defaultValue) { + try { + return settingValue != null ? Long.parseLong(settingValue) : defaultValue; + } catch (NumberFormatException e) { + return defaultValue; + } + } + /** * System settings, containing miscellaneous system preferences. This * table holds simple name/value pairs. There are convenience @@ -3603,11 +3663,7 @@ public final class Settings { @UnsupportedAppUsage public static int getIntForUser(ContentResolver cr, String name, int def, int userHandle) { String v = getStringForUser(cr, name, userHandle); - try { - return v != null ? Integer.parseInt(v) : def; - } catch (NumberFormatException e) { - return def; - } + return parseIntSettingWithDefault(v, def); } /** @@ -3638,11 +3694,7 @@ public final class Settings { public static int getIntForUser(ContentResolver cr, String name, int userHandle) throws SettingNotFoundException { String v = getStringForUser(cr, name, userHandle); - try { - return Integer.parseInt(v); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + return parseIntSetting(v, name); } /** @@ -3690,14 +3742,8 @@ public final class Settings { /** @hide */ public static long getLongForUser(ContentResolver cr, String name, long def, int userHandle) { - String valString = getStringForUser(cr, name, userHandle); - long value; - try { - value = valString != null ? Long.parseLong(valString) : def; - } catch (NumberFormatException e) { - value = def; - } - return value; + String v = getStringForUser(cr, name, userHandle); + return parseLongSettingWithDefault(v, def); } /** @@ -3725,12 +3771,8 @@ public final class Settings { /** @hide */ public static long getLongForUser(ContentResolver cr, String name, int userHandle) throws SettingNotFoundException { - String valString = getStringForUser(cr, name, userHandle); - try { - return Long.parseLong(valString); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + String v = getStringForUser(cr, name, userHandle); + return parseLongSetting(v, name); } /** @@ -3778,11 +3820,7 @@ public final class Settings { public static float getFloatForUser(ContentResolver cr, String name, float def, int userHandle) { String v = getStringForUser(cr, name, userHandle); - try { - return v != null ? Float.parseFloat(v) : def; - } catch (NumberFormatException e) { - return def; - } + return parseFloatSettingWithDefault(v, def); } /** @@ -3812,14 +3850,7 @@ public final class Settings { public static float getFloatForUser(ContentResolver cr, String name, int userHandle) throws SettingNotFoundException { String v = getStringForUser(cr, name, userHandle); - if (v == null) { - throw new SettingNotFoundException(name); - } - try { - return Float.parseFloat(v); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + return parseFloatSetting(v, name); } /** @@ -6070,11 +6101,7 @@ public final class Settings { @UnsupportedAppUsage public static int getIntForUser(ContentResolver cr, String name, int def, int userHandle) { String v = getStringForUser(cr, name, userHandle); - try { - return v != null ? Integer.parseInt(v) : def; - } catch (NumberFormatException e) { - return def; - } + return parseIntSettingWithDefault(v, def); } /** @@ -6104,11 +6131,7 @@ public final class Settings { public static int getIntForUser(ContentResolver cr, String name, int userHandle) throws SettingNotFoundException { String v = getStringForUser(cr, name, userHandle); - try { - return Integer.parseInt(v); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + return parseIntSetting(v, name); } /** @@ -6157,14 +6180,8 @@ public final class Settings { @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) public static long getLongForUser(ContentResolver cr, String name, long def, int userHandle) { - String valString = getStringForUser(cr, name, userHandle); - long value; - try { - value = valString != null ? Long.parseLong(valString) : def; - } catch (NumberFormatException e) { - value = def; - } - return value; + String v = getStringForUser(cr, name, userHandle); + return parseLongSettingWithDefault(v, def); } /** @@ -6192,12 +6209,8 @@ public final class Settings { /** @hide */ public static long getLongForUser(ContentResolver cr, String name, int userHandle) throws SettingNotFoundException { - String valString = getStringForUser(cr, name, userHandle); - try { - return Long.parseLong(valString); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + String v = getStringForUser(cr, name, userHandle); + return parseLongSetting(v, name); } /** @@ -6246,11 +6259,7 @@ public final class Settings { public static float getFloatForUser(ContentResolver cr, String name, float def, int userHandle) { String v = getStringForUser(cr, name, userHandle); - try { - return v != null ? Float.parseFloat(v) : def; - } catch (NumberFormatException e) { - return def; - } + return parseFloatSettingWithDefault(v, def); } /** @@ -6280,14 +6289,7 @@ public final class Settings { public static float getFloatForUser(ContentResolver cr, String name, int userHandle) throws SettingNotFoundException { String v = getStringForUser(cr, name, userHandle); - if (v == null) { - throw new SettingNotFoundException(name); - } - try { - return Float.parseFloat(v); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + return parseFloatSetting(v, name); } /** @@ -15429,11 +15431,7 @@ public final class Settings { */ public static int getInt(ContentResolver cr, String name, int def) { String v = getString(cr, name); - try { - return v != null ? Integer.parseInt(v) : def; - } catch (NumberFormatException e) { - return def; - } + return parseIntSettingWithDefault(v, def); } /** @@ -15457,11 +15455,7 @@ public final class Settings { public static int getInt(ContentResolver cr, String name) throws SettingNotFoundException { String v = getString(cr, name); - try { - return Integer.parseInt(v); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + return parseIntSetting(v, name); } /** @@ -15496,14 +15490,8 @@ public final class Settings { * or not a valid {@code long}. */ public static long getLong(ContentResolver cr, String name, long def) { - String valString = getString(cr, name); - long value; - try { - value = valString != null ? Long.parseLong(valString) : def; - } catch (NumberFormatException e) { - value = def; - } - return value; + String v = getString(cr, name); + return parseLongSettingWithDefault(v, def); } /** @@ -15525,12 +15513,8 @@ public final class Settings { */ public static long getLong(ContentResolver cr, String name) throws SettingNotFoundException { - String valString = getString(cr, name); - try { - return Long.parseLong(valString); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + String v = getString(cr, name); + return parseLongSetting(v, name); } /** @@ -15566,11 +15550,7 @@ public final class Settings { */ public static float getFloat(ContentResolver cr, String name, float def) { String v = getString(cr, name); - try { - return v != null ? Float.parseFloat(v) : def; - } catch (NumberFormatException e) { - return def; - } + return parseFloatSettingWithDefault(v, def); } /** @@ -15594,14 +15574,7 @@ public final class Settings { public static float getFloat(ContentResolver cr, String name) throws SettingNotFoundException { String v = getString(cr, name); - if (v == null) { - throw new SettingNotFoundException(name); - } - try { - return Float.parseFloat(v); - } catch (NumberFormatException e) { - throw new SettingNotFoundException(name); - } + return parseFloatSetting(v, name); } /**