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

Commit 85f1024f authored by Stefan Andonian's avatar Stefan Andonian
Browse files

Make all debug feature flags boot aware.

Bug: 276090194
Test: FeatureFlags worked on device.
Change-Id: I710801d63e6f4283bf15daea2b5be5874de21cc8
parent eac832ae
Loading
Loading
Loading
Loading
+6 −12
Original line number Diff line number Diff line
@@ -16,12 +16,10 @@

package com.android.launcher3.uioverrides.flags;

import static com.android.launcher3.config.FeatureFlags.FLAGS_PREF_NAME;
import static com.android.launcher3.config.FeatureFlags.FlagState.TEAMFOOD;
import static com.android.launcher3.uioverrides.flags.FlagsFactory.TEAMFOOD_FLAG;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Process;
import android.text.Html;
@@ -52,31 +50,27 @@ public final class FlagTogglerPrefUi {

    private final PreferenceFragmentCompat mFragment;
    private final Context mContext;
    private final SharedPreferences mSharedPreferences;

    private final PreferenceDataStore mDataStore = new PreferenceDataStore() {

        @Override
        public void putBoolean(String key, boolean value) {
            mSharedPreferences.edit().putBoolean(key, value).apply();
            FlagsFactory.getSharedPreferences().edit().putBoolean(key, value).apply();
            updateMenu();
        }

        @Override
        public boolean getBoolean(String key, boolean defaultValue) {
            return mSharedPreferences.getBoolean(key, defaultValue);
            return FlagsFactory.getSharedPreferences().getBoolean(key, defaultValue);
        }
    };

    public FlagTogglerPrefUi(PreferenceFragmentCompat fragment) {
        mFragment = fragment;
        mContext = fragment.getActivity();
        mSharedPreferences = mContext.getSharedPreferences(
                FLAGS_PREF_NAME, Context.MODE_PRIVATE);
    }

    public void applyTo(PreferenceGroup parent) {
        Set<String> modifiedPrefs = mSharedPreferences.getAll().keySet();
        Set<String> modifiedPrefs = FlagsFactory.getSharedPreferences().getAll().keySet();
        List<DebugFlag> flags = FlagsFactory.getDebugFlags();
        flags.sort((f1, f2) -> {
            // Sort first by any prefs that the user has changed, then alphabetically.
@@ -102,7 +96,7 @@ public final class FlagTogglerPrefUi {
                public void onBindViewHolder(PreferenceViewHolder holder) {
                    super.onBindViewHolder(holder);
                    holder.itemView.setOnLongClickListener(v -> {
                        mSharedPreferences.edit().remove(flag.key).apply();
                        FlagsFactory.getSharedPreferences().edit().remove(flag.key).apply();
                        setChecked(getFlagStateFromSharedPrefs(flag));
                        updateSummary(this, flag);
                        updateMenu();
@@ -133,7 +127,7 @@ public final class FlagTogglerPrefUi {
    private void updateSummary(SwitchPreference switchPreference, DebugFlag flag) {
        String summary = flag.defaultValue == TEAMFOOD
                ? "<font color='blue'><b>[TEAMFOOD]</b> </font>" : "";
        if (mSharedPreferences.contains(flag.key)) {
        if (FlagsFactory.getSharedPreferences().contains(flag.key)) {
            summary += "<font color='red'><b>[OVERRIDDEN]</b> </font>";
        }
        if (!TextUtils.isEmpty(summary)) {
@@ -156,7 +150,7 @@ public final class FlagTogglerPrefUi {

    public void onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_apply_flags) {
            mSharedPreferences.edit().commit();
            FlagsFactory.getSharedPreferences().edit().commit();
            Log.e(TAG,
                    "Killing launcher process " + Process.myPid() + " to apply new flag values");
            System.exit(0);
+17 −8
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static android.app.ActivityThread.currentApplication;
import static com.android.launcher3.BuildConfig.IS_DEBUG_DEVICE;
import static com.android.launcher3.config.FeatureFlags.FlagState.DISABLED;
import static com.android.launcher3.config.FeatureFlags.FlagState.ENABLED;
import static com.android.launcher3.config.FeatureFlags.FlagState.TEAMFOOD;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;

import android.content.Context;
@@ -30,6 +29,8 @@ import android.provider.DeviceConfig;
import android.provider.DeviceConfig.Properties;
import android.util.Log;

import androidx.annotation.NonNull;

import com.android.launcher3.config.FeatureFlags.BooleanFlag;
import com.android.launcher3.config.FeatureFlags.FlagState;
import com.android.launcher3.config.FeatureFlags.IntFlag;
@@ -52,10 +53,11 @@ public class FlagsFactory {
    private static final FlagsFactory INSTANCE = new FlagsFactory();
    private static final boolean FLAG_AUTO_APPLY_ENABLED = true;

    public static final String FLAGS_PREF_NAME = "featureFlags";
    private static final String FLAGS_PREF_NAME = "featureFlags";
    public static final String NAMESPACE_LAUNCHER = "launcher";

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

    static final BooleanFlag TEAMFOOD_FLAG = getReleaseFlag(
            0, "LAUNCHER_TEAMFOOD", DISABLED, "Enable this flag to opt-in all team food flags");
@@ -93,10 +95,8 @@ public class FlagsFactory {
    public static BooleanFlag getDebugFlag(
            int bugId, String key, FlagState flagState, String description) {
        if (IS_DEBUG_DEVICE) {
            SharedPreferences prefs = currentApplication()
                    .getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE);
            boolean defaultValue = getEnabledValue(flagState);
            boolean currentValue = prefs.getBoolean(key, defaultValue);
            boolean currentValue = getSharedPreferences().getBoolean(key, defaultValue);
            DebugFlag flag = new DebugFlag(key, description, flagState, currentValue);
            sDebugFlags.add(flag);
            return flag;
@@ -115,9 +115,7 @@ public class FlagsFactory {
        boolean defaultValueInCode = getEnabledValue(flagState);
        boolean defaultValue = DeviceConfig.getBoolean(NAMESPACE_LAUNCHER, key, defaultValueInCode);
        if (IS_DEBUG_DEVICE) {
            SharedPreferences prefs = currentApplication()
                    .getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE);
            boolean currentValue = prefs.getBoolean(key, defaultValue);
            boolean currentValue = getSharedPreferences().getBoolean(key, defaultValue);
            DebugFlag flag = new DeviceFlag(key, description, flagState, currentValue,
                    defaultValueInCode);
            sDebugFlags.add(flag);
@@ -145,6 +143,17 @@ public class FlagsFactory {
        }
    }

    /** Returns the SharedPreferences instance backing Debug FeatureFlags. */
    @NonNull
    static SharedPreferences getSharedPreferences() {
        if (sSharedPreferences == null) {
            sSharedPreferences = currentApplication()
                    .createDeviceProtectedStorageContext()
                    .getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE);
        }
        return sSharedPreferences;
    }

    /**
     * Dumps the current flags state to the print writer
     */
+0 −2
Original line number Diff line number Diff line
@@ -39,8 +39,6 @@ import java.util.function.ToIntFunction;
 */
public final class FeatureFlags {

    public static final String FLAGS_PREF_NAME = "featureFlags";

    @VisibleForTesting
    public static Predicate<BooleanFlag> sBooleanReader = f -> f.mCurrentValue;
    @VisibleForTesting