diff options
6 files changed, 143 insertions, 89 deletions
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index e62576c32c13..8a75a53c3602 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -9570,8 +9570,8 @@ public final class Settings { * The following keys are supported: * * <pre> - * screen_brightness_array (string) - * dimming_scrim_array (string) + * screen_brightness_array (int[]) + * dimming_scrim_array (int[]) * prox_screen_off_delay (long) * prox_cooldown_trigger (long) * prox_cooldown_period (long) @@ -11123,7 +11123,7 @@ public final class Settings { * * <pre> * default (int) - * options_array (string) + * options_array (int[]) * </pre> * * All delays in integer minutes. Array order is respected. diff --git a/core/java/android/util/KeyValueListParser.java b/core/java/android/util/KeyValueListParser.java index d50395e223e5..0a00794a1471 100644 --- a/core/java/android/util/KeyValueListParser.java +++ b/core/java/android/util/KeyValueListParser.java @@ -149,6 +149,34 @@ public class KeyValueListParser { } /** + * Get the value for key as an integer array. + * + * The value should be encoded as "0:1:2:3:4" + * + * @param key The key to lookup. + * @param def The value to return if the key was not found. + * @return the int[] value associated with the key. + */ + public int[] getIntArray(String key, int[] def) { + String value = mValues.get(key); + if (value != null) { + try { + String[] parts = value.split(":"); + if (parts.length > 0) { + int[] ret = new int[parts.length]; + for (int i = 0; i < parts.length; i++) { + ret[i] = Integer.parseInt(parts[i]); + } + return ret; + } + } catch (NumberFormatException e) { + // fallthrough + } + } + return def; + } + + /** * @return the number of keys. */ public int size() { diff --git a/core/tests/coretests/src/android/util/KeyValueListParserTest.java b/core/tests/coretests/src/android/util/KeyValueListParserTest.java new file mode 100644 index 000000000000..038f0b7efaa3 --- /dev/null +++ b/core/tests/coretests/src/android/util/KeyValueListParserTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.util; + +import static org.junit.Assert.assertEquals; + +import android.platform.test.annotations.Presubmit; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Tests for {@link KeyValueListParser}. + */ +@RunWith(AndroidJUnit4.class) +@SmallTest +@Presubmit +public class KeyValueListParserTest { + private static final String TAG = "KeyValueListParserTest"; + private static final int[] DEFAULT = {1, 2, 3, 4}; + + private KeyValueListParser mParser; + + @Before + public void setUp() { + mParser = new KeyValueListParser(','); + } + + @Test + public void testParseIntArrayNullInput() throws Exception { + mParser.setString(null); + int[] result = mParser.getIntArray("test", DEFAULT); + assertEquals(DEFAULT, result); + } + + @Test + public void testParseIntArrayEmptyInput() throws Exception { + mParser.setString("test="); + int[] result = mParser.getIntArray("test", DEFAULT); + assertEquals(DEFAULT, result); + } + + @Test + public void testParseIntArrayNullKey() throws Exception { + mParser.setString("foo=bar,test=100:200,baz=123"); + int[] result = mParser.getIntArray(null, DEFAULT); + assertEquals(DEFAULT, result); + } + + @Test + public void testParseIntArrayComplexInput() throws Exception { + mParser.setString("foo=bar,test=100:200,baz=123"); + int[] result = mParser.getIntArray("test", DEFAULT); + assertEquals(2, result.length); + assertEquals(100, result[0]); // respect order + assertEquals(200, result[1]); + } + + @Test + public void testParseIntArrayLeadingSep() throws Exception { + mParser.setString("test=:4:5:6"); + int[] result = mParser.getIntArray("test", DEFAULT); + assertEquals(DEFAULT, result); + } + + @Test + public void testParseIntArrayEmptyItem() throws Exception { + mParser.setString("test=:4::6"); + int[] result = mParser.getIntArray("test", DEFAULT); + assertEquals(DEFAULT, result); + } + + @Test + public void testParseIntArrayTrailingSep() throws Exception { + mParser.setString("test=4:5:6:"); + int[] result = mParser.getIntArray("test", DEFAULT); + assertEquals(3, result.length); + assertEquals(4, result[0]); // respect order + assertEquals(5, result[1]); + assertEquals(6, result[2]); + } + + @Test + public void testParseIntArrayGoodData() throws Exception { + mParser.setString("test=4:5:6"); + int[] result = mParser.getIntArray("test", DEFAULT); + assertEquals(3, result.length); + assertEquals(4, result[0]); // respect order + assertEquals(5, result[1]); + assertEquals(6, result[2]); + } +} diff --git a/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java b/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java index debda2109637..cc2244a4e934 100644 --- a/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java +++ b/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java @@ -30,8 +30,6 @@ import android.util.Log; import com.android.systemui.R; -import java.util.Arrays; - /** * Class to store the policy for AOD, which comes from * {@link android.provider.Settings.Global} @@ -102,20 +100,6 @@ public class AlwaysOnDisplayPolicy { mSettingsObserver.observe(); } - private int[] parseIntArray(final String key, final int[] defaultArray) { - final String value = mParser.getString(key, null); - if (value != null) { - try { - return Arrays.stream(value.split(":")).map(String::trim).mapToInt( - Integer::parseInt).toArray(); - } catch (NumberFormatException e) { - return defaultArray; - } - } else { - return defaultArray; - } - } - private final class SettingsObserver extends ContentObserver { private final Uri ALWAYS_ON_DISPLAY_CONSTANTS_URI = Settings.Global.getUriFor(Settings.Global.ALWAYS_ON_DISPLAY_CONSTANTS); @@ -154,10 +138,10 @@ public class AlwaysOnDisplayPolicy { DEFAULT_PROX_COOLDOWN_TRIGGER_MS); proxCooldownPeriodMs = mParser.getLong(KEY_PROX_COOLDOWN_PERIOD_MS, DEFAULT_PROX_COOLDOWN_PERIOD_MS); - screenBrightnessArray = parseIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY, + screenBrightnessArray = mParser.getIntArray(KEY_SCREEN_BRIGHTNESS_ARRAY, resources.getIntArray( R.array.config_doze_brightness_sensor_to_brightness)); - dimmingScrimArray = parseIntArray(KEY_DIMMING_SCRIM_ARRAY, + dimmingScrimArray = mParser.getIntArray(KEY_DIMMING_SCRIM_ARRAY, resources.getIntArray( R.array.config_doze_brightness_sensor_to_scrim_opacity)); } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationSnooze.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationSnooze.java index 492ab44d499b..aea0127764b0 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationSnooze.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationSnooze.java @@ -16,7 +16,6 @@ package com.android.systemui.statusbar; */ import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; @@ -234,7 +233,7 @@ public class NotificationSnooze extends LinearLayout final int defaultSnooze = mParser.getInt(KEY_DEFAULT_SNOOZE, resources.getInteger(R.integer.config_notification_snooze_time_default)); - final int[] snoozeTimes = parseIntArray(KEY_OPTIONS, + final int[] snoozeTimes = mParser.getIntArray(KEY_OPTIONS, resources.getIntArray(R.array.config_notification_snooze_times)); for (int i = 0; i < snoozeTimes.length && i < sAccessibilityActions.length; i++) { @@ -248,21 +247,6 @@ public class NotificationSnooze extends LinearLayout return options; } - @VisibleForTesting - int[] parseIntArray(final String key, final int[] defaultArray) { - final String value = mParser.getString(key, null); - if (value != null) { - try { - return Arrays.stream(value.split(":")).map(String::trim).mapToInt( - Integer::parseInt).toArray(); - } catch (NumberFormatException e) { - return defaultArray; - } - } else { - return defaultArray; - } - } - private SnoozeOption createOption(int minutes, int accessibilityActionId) { Resources res = getResources(); boolean showInHours = minutes >= 60; diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationSnoozeTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationSnoozeTest.java index 6b31c967a247..756bb1c104ee 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationSnoozeTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationSnoozeTest.java @@ -62,57 +62,6 @@ public class NotificationSnoozeTest extends SysuiTestCase { } @Test - public void testParseIntArrayNull() throws Exception { - when(mMockParser.getString(anyString(), isNull())).thenReturn(null); - mNotificationSnooze.setKeyValueListParser(mMockParser); - - int[] result = mNotificationSnooze.parseIntArray("foo", RES_OPTIONS); - assertEquals(RES_OPTIONS, result); - } - - @Test - public void testParseIntArrayLeadingSep() throws Exception { - when(mMockParser.getString(anyString(), isNull())).thenReturn(":4:5:6"); - mNotificationSnooze.setKeyValueListParser(mMockParser); - - int[] result = mNotificationSnooze.parseIntArray("foo", RES_OPTIONS); - assertEquals(RES_OPTIONS, result); - } - - @Test - public void testParseIntArrayEmptyItem() throws Exception { - when(mMockParser.getString(anyString(), isNull())).thenReturn("4::6"); - mNotificationSnooze.setKeyValueListParser(mMockParser); - - int[] result = mNotificationSnooze.parseIntArray("foo", RES_OPTIONS); - assertEquals(RES_OPTIONS, result); - } - - @Test - public void testParseIntArrayTrailingSep() throws Exception { - when(mMockParser.getString(anyString(), isNull())).thenReturn("4:5:6:"); - mNotificationSnooze.setKeyValueListParser(mMockParser); - - int[] result = mNotificationSnooze.parseIntArray("foo", RES_OPTIONS); - assertEquals(3, result.length); - assertEquals(4, result[0]); // respect order - assertEquals(5, result[1]); - assertEquals(6, result[2]); - } - - @Test - public void testParseIntArrayGoodData() throws Exception { - when(mMockParser.getString(anyString(), isNull())).thenReturn("4:5:6"); - mNotificationSnooze.setKeyValueListParser(mMockParser); - - int[] result = mNotificationSnooze.parseIntArray("foo", RES_OPTIONS); - assertEquals(3, result.length); - assertEquals(4, result[0]); // respect order - assertEquals(5, result[1]); - assertEquals(6, result[2]); - } - - @Test public void testGetOptionsWithNoConfig() throws Exception { ArrayList<SnoozeOption> result = mNotificationSnooze.getDefaultSnoozeOptions(); assertEquals(3, result.size()); |