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

Commit 89802651 authored by Dave Mankoff's avatar Dave Mankoff
Browse files

Add ability to read and write flags to sysprops.

Bug: 202860494
Test: manual
Change-Id: I660931492e8a2ffd1373162aab60e4abd4566db6
parent 5eac15cf
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -18,6 +18,12 @@ package com.android.systemui.flags;

/**
 * List of {@link Flag} objects for use in SystemUI.
 *
 * Flag Ids are integers. They must be unique.
 *
 * On public release builds, flags will always return their default value. There is no way to
 * change their value on release builds.
 */
public class Flags {
    public static final BooleanFlag THE_FIRST_FLAG = new BooleanFlag(1, false);
}
+48 −12
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@ package com.android.systemui.flags;

import com.android.systemui.dagger.SysUISingleton;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

/**
@@ -25,28 +28,61 @@ import javax.inject.Inject;
 */
@SysUISingleton
public class FeatureFlagManager implements FlagReader, FlagWriter {
    private static final String SYSPROP_PREFIX = "persist.systemui.flag_";
    private static final String FIELD_TYPE = "type";
    private static final String FIELD_VALUE = "value";
    private static final String TYPE_BOOLEAN = "boolean";
    private final SystemPropertiesHelper mSystemPropertiesHelper;

    @Inject
    public FeatureFlagManager() {}
    public FeatureFlagManager(SystemPropertiesHelper systemPropertiesHelper) {
        mSystemPropertiesHelper = systemPropertiesHelper;
    }

    /** Return a {@link BooleanFlag}'s value. */
    public boolean isEnabled(int key, boolean defaultValue) {
        return isEnabled(Integer.toString(key), defaultValue);
        String data = mSystemPropertiesHelper.get(keyToSysPropKey(key));
        if (data.isEmpty()) {
            return defaultValue;
        }
        JSONObject json;
        try {
            json = new JSONObject(data);
            if (!assertType(json, TYPE_BOOLEAN)) {
                return defaultValue;
            }
            return json.getBoolean(FIELD_VALUE);
        } catch (JSONException e) {
            // TODO: delete the property
            return defaultValue;
        }

    public boolean isEnabled(String key, boolean defaultValue) {
        // TODO
        return false;
    }

    public void setEnabled(int key, boolean value) {
        setEnabled(Integer.toString(key), value);
        JSONObject json = new JSONObject();
        try {
            json.put(FIELD_TYPE, TYPE_BOOLEAN);
            json.put(FIELD_VALUE, value);
            mSystemPropertiesHelper.set(keyToSysPropKey(key), json.toString());
        } catch (JSONException e) {
            // no-op
        }

    public void setEnabled(String key, boolean value) {
        // TODO
    }

    public void addListener(Listener run) {}

    public void removeListener(Listener run) {}

    private static String keyToSysPropKey(int key) {
        return SYSPROP_PREFIX + key;
    }

    private static boolean assertType(JSONObject json, String type) {
        try {
            return json.getString(FIELD_TYPE).equals(TYPE_BOOLEAN);
        } catch (JSONException e) {
            return false;
        }
    }

}
+9 −1
Original line number Diff line number Diff line
@@ -26,11 +26,19 @@ import javax.inject.Inject
 */
@SysUISingleton
open class SystemPropertiesHelper @Inject constructor() {
    fun get(name: String): String {
        return SystemProperties.get(name)
    }

    fun getBoolean(name: String, default: Boolean): Boolean {
        return SystemProperties.getBoolean(name, default)
    }

    fun set(name: String, value: String) {
        SystemProperties.set(name, value)
    }

    fun set(name: String, value: Int) {
        SystemProperties.set(name, value.toString())
        set(name, value.toString())
    }
}
 No newline at end of file
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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 com.android.systemui.flags;

import static com.google.common.truth.Truth.assertThat;

import androidx.test.filters.SmallTest;

import com.android.systemui.SysuiTestCase;

import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;

@SmallTest
public class FeatureFlagManagerTest extends SysuiTestCase {
    FeatureFlagManager mFeatureFlagManager;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        mFeatureFlagManager = new FeatureFlagManager();
    }

    @Test
    public void testIsEnabled() {
        mFeatureFlagManager.setEnabled(1, true);
        // Again, nothing changes.
        assertThat(mFeatureFlagManager.isEnabled(1, false)).isFalse();
    }
}