Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4170751a authored by Chris Wren's avatar Chris Wren
Browse files

add integer array values to KeyValueListParser

Test: atest NotificationSnoozeTest KeyValueListParserTest AlwaysOnDisplayPolicyTest
Change-Id: Ia26cd38258c2cf83558a9a39a6b1c2ec6dedfcb1
parent dcb520b4
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -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.
+28 −0
Original line number Diff line number Diff line
@@ -148,6 +148,34 @@ public class KeyValueListParser {
        return def;
    }

    /**
     * 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.
     */
+109 −0
Original line number Diff line number Diff line
/*
 * 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]);
    }
}
+2 −18
Original line number Diff line number Diff line
@@ -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));
            }
+1 −17
Original line number Diff line number Diff line
@@ -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;
Loading