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

Commit 1585fa86 authored by Dave Mankoff's avatar Dave Mankoff Committed by Android (Google) Code Review
Browse files

Merge changes Icde45251,Ic5356aa0 into sc-v2-dev

* changes:
  Cache flag values when they are read.
  Restart SystemUI when a flag changes.
parents d41437ab c9d778db
Loading
Loading
Loading
Loading
+37 −5
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import com.android.systemui.dagger.SysUISingleton;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
@@ -54,6 +55,8 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {
    private static final String FLAGS_PERMISSION = "com.android.systemui.permission.FLAGS";
    private final SystemPropertiesHelper mSystemPropertiesHelper;

    private final Map<Integer, Boolean> mBooleanFlagCache = new HashMap<>();

    @Inject
    public FeatureFlagManager(SystemPropertiesHelper systemPropertiesHelper, Context context) {
        mSystemPropertiesHelper = systemPropertiesHelper;
@@ -64,31 +67,48 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {

    /** Return a {@link BooleanFlag}'s value. */
    public boolean isEnabled(int id, boolean defaultValue) {
        if (!mBooleanFlagCache.containsKey(id)) {
            Boolean result = isEnabledInternal(id);
            mBooleanFlagCache.put(id, result == null ? defaultValue : result);
        }

        return mBooleanFlagCache.get(id);
    }

    /** Returns the stored value or null if not set. */
    private Boolean isEnabledInternal(int id) {
        String data = mSystemPropertiesHelper.get(keyToSysPropKey(id));
        if (data.isEmpty()) {
            return defaultValue;
            return null;
        }
        JSONObject json;
        try {
            json = new JSONObject(data);
            if (!assertType(json, TYPE_BOOLEAN)) {
                return defaultValue;
                return null;
            }

            return json.getBoolean(FIELD_VALUE);
        } catch (JSONException e) {
            eraseFlag(id);
            return defaultValue;
            eraseInternal(id);  // Don't restart SystemUI in this case.
        }
        return null;
    }

    /** Set whether a given {@link BooleanFlag} is enabled or not. */
    public void setEnabled(int id, boolean value) {
        Boolean currentValue = isEnabledInternal(id);
        if (currentValue != null && currentValue == value) {
            return;
        }

        JSONObject json = new JSONObject();
        try {
            json.put(FIELD_TYPE, TYPE_BOOLEAN);
            json.put(FIELD_VALUE, value);
            mSystemPropertiesHelper.set(keyToSysPropKey(id), json.toString());
            Log.i(TAG, "Set id " + id + " to " + value);
            restartSystemUI();
        } catch (JSONException e) {
            // no-op
        }
@@ -96,6 +116,12 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {

    /** Erase a flag's overridden value if there is one. */
    public void eraseFlag(int id) {
        eraseInternal(id);
        restartSystemUI();
    }

    /** Works just like {@link #eraseFlag(int)} except that it doesn't restart SystemUI. */
    private void eraseInternal(int id) {
        // We can't actually "erase" things from sysprops, but we can set them to empty!
        mSystemPropertiesHelper.set(keyToSysPropKey(id), "");
        Log.i(TAG, "Erase id " + id);
@@ -105,6 +131,12 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {

    public void removeListener(Listener run) {}

    private void restartSystemUI() {
        Log.i(TAG, "Restarting SystemUI");
        // SysUI starts back when up exited. Is there a better way to do this?
        System.exit(0);
    }

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