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

Commit fb575b4b authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Removing flag management UI states from flag definition

Bug: 270386012
Test: Verified that the flag UI is unchanged
Change-Id: I98c687a81a209f155bf4ea902ad148635a629a56
parent c6c33dd1
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -24,8 +24,6 @@ class DebugFlag extends BooleanFlag {

    public final boolean defaultValue;

    boolean mHasBeenChangedAtLeastOnce;

    public DebugFlag(String key, String description, boolean defaultValue, boolean currentValue) {
        super(currentValue);
        this.key = key;
+18 −20
Original line number Diff line number Diff line
@@ -35,6 +35,9 @@ import androidx.preference.SwitchPreference;
import com.android.launcher3.R;
import com.android.launcher3.config.FeatureFlags;

import java.util.List;
import java.util.Set;

/**
 * Dev-build only UI allowing developers to toggle flag settings. See {@link FeatureFlags}.
 */
@@ -50,31 +53,15 @@ public final class FlagTogglerPrefUi {

        @Override
        public void putBoolean(String key, boolean value) {
            for (DebugFlag flag : FlagsFactory.getDebugFlags()) {
                if (flag.key.equals(key)) {
                    SharedPreferences prefs = mContext.getSharedPreferences(
                            FLAGS_PREF_NAME, Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = prefs.edit();
                    // We keep the key in the prefs even if it has the default value, because it's a
                    // signal that it has been changed at one point.
                    if (!prefs.contains(key) && value == flag.defaultValue) {
                        editor.remove(key).apply();
                        flag.mHasBeenChangedAtLeastOnce = false;
                    } else {
                        editor.putBoolean(key, value).apply();
                        flag.mHasBeenChangedAtLeastOnce = true;
                    }
            mSharedPreferences.edit().putBoolean(key, value).apply();
            updateMenu();
        }
            }
        }

        @Override
        public boolean getBoolean(String key, boolean defaultValue) {
            for (DebugFlag flag : FlagsFactory.getDebugFlags()) {
                if (flag.key.equals(key)) {
                    return mContext.getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE)
                            .getBoolean(key, flag.defaultValue);
                    return mSharedPreferences.getBoolean(key, flag.defaultValue);
                }
            }
            return defaultValue;
@@ -89,11 +76,22 @@ public final class FlagTogglerPrefUi {
    }

    public void applyTo(PreferenceGroup parent) {
        Set<String> modifiedPrefs = mSharedPreferences.getAll().keySet();
        List<DebugFlag> flags = FlagsFactory.getDebugFlags();
        flags.sort((f1, f2) -> {
            // Sort first by any prefs that the user has changed, then alphabetically.
            int changeComparison = Boolean.compare(
                    modifiedPrefs.contains(f2.key), modifiedPrefs.contains(f1.key));
            return changeComparison != 0
                    ? changeComparison
                    : f1.key.compareToIgnoreCase(f2.key);
        });

        // For flag overrides we only want to store when the engineer chose to override the
        // flag with a different value than the default. That way, when we flip flags in
        // future, engineers will pick up the new value immediately. To accomplish this, we use a
        // custom preference data store.
        for (DebugFlag flag : FlagsFactory.getDebugFlags()) {
        for (DebugFlag flag : flags) {
            SwitchPreference switchPreference = new SwitchPreference(mContext);
            switchPreference.setKey(flag.key);
            switchPreference.setDefaultValue(flag.defaultValue);
+1 −14
Original line number Diff line number Diff line
@@ -53,7 +53,6 @@ public class FlagsFactory {

    private static final List<DebugFlag> sDebugFlags = new ArrayList<>();


    private final Set<String> mKeySet = new HashSet<>();
    private boolean mRestartRequested = false;

@@ -75,7 +74,6 @@ public class FlagsFactory {
                    .getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE);
            boolean currentValue = prefs.getBoolean(key, defaultValue);
            DebugFlag flag = new DebugFlag(key, description, defaultValue, currentValue);
            flag.mHasBeenChangedAtLeastOnce = prefs.contains(key);
            sDebugFlags.add(flag);
            return flag;
        } else {
@@ -96,7 +94,6 @@ public class FlagsFactory {
            boolean currentValue = prefs.getBoolean(key, defaultValue);
            DebugFlag flag = new DeviceFlag(key, description, defaultValue, currentValue,
                    defaultValueInCode);
            flag.mHasBeenChangedAtLeastOnce = prefs.contains(key);
            sDebugFlags.add(flag);
            return flag;
        } else {
@@ -117,19 +114,9 @@ public class FlagsFactory {
        if (!Utilities.IS_DEBUG_DEVICE) {
            return Collections.emptyList();
        }
        List<DebugFlag> flags;
        synchronized (sDebugFlags) {
            flags = new ArrayList<>(sDebugFlags);
        }
        flags.sort((f1, f2) -> {
            // Sort first by any prefs that the user has changed, then alphabetically.
            int changeComparison = Boolean.compare(
                    f2.mHasBeenChangedAtLeastOnce, f1.mHasBeenChangedAtLeastOnce);
            return changeComparison != 0
                    ? changeComparison
                    : f1.key.compareToIgnoreCase(f2.key);
        });
        return flags;
            return new ArrayList<>(sDebugFlags);
        }
    }

    /**