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

Commit 1297f400 authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Moving flag creation to a separate file" into tm-qpr-dev

parents a695986d 98204add
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.launcher3.uioverrides.flags;

import com.android.launcher3.config.FeatureFlags.BooleanFlag;

class DebugFlag extends BooleanFlag {

    public final String key;
    public final String description;

    public final boolean defaultValue;

    boolean mHasBeenChangedAtLeastOnce;

    public DebugFlag(String key, String description, boolean defaultValue, boolean currentValue) {
        super(currentValue);
        this.key = key;
        this.defaultValue = defaultValue;
        this.description = description;
    }

    @Override
    public String toString() {
        return key + ": defaultValue=" + defaultValue + ", mCurrentValue=" + get();
    }
}
+1 −2
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.settings;
package com.android.launcher3.uioverrides.flags;

import static android.content.Intent.ACTION_PACKAGE_ADDED;
import static android.content.Intent.ACTION_PACKAGE_CHANGED;
@@ -63,7 +63,6 @@ import androidx.preference.SwitchPreference;
import com.android.launcher3.LauncherPrefs;
import com.android.launcher3.R;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.config.FlagTogglerPrefUi;
import com.android.launcher3.secondarydisplay.SecondaryDisplayLauncher;
import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper;
import com.android.launcher3.util.OnboardingPrefs;
+33 −0
Original line number Diff line number Diff line
@@ -14,38 +14,20 @@
 * limitations under the License.
 */

package com.android.launcher3.uioverrides;
package com.android.launcher3.uioverrides.flags;

import android.annotation.TargetApi;
import android.os.Build;
import android.provider.DeviceConfig;

import com.android.launcher3.config.FeatureFlags.DebugFlag;

@TargetApi(Build.VERSION_CODES.P)
public class DeviceFlag extends DebugFlag {

    public static final String NAMESPACE_LAUNCHER = "launcher";
class DeviceFlag extends DebugFlag {

    private final boolean mDefaultValueInCode;

    public DeviceFlag(String key, boolean defaultValue, String description) {
        super(key, getDeviceValue(key, defaultValue), description);
        mDefaultValueInCode = defaultValue;
    }

    @Override
    protected StringBuilder appendProps(StringBuilder src) {
        return super.appendProps(src).append(", mDefaultValueInCode=").append(mDefaultValueInCode);
    public DeviceFlag(String key, String description, boolean defaultValue,
            boolean currentValue, boolean defaultValueInCode) {
        super(key, description, defaultValue, currentValue);
        mDefaultValueInCode = defaultValueInCode;
    }

    @Override
    public boolean get() {
        // Override this method in order to let Robolectric ShadowDeviceFlag to stub it.
        return super.get();
    }

    protected static boolean getDeviceValue(String key, boolean defaultValue) {
        return DeviceConfig.getBoolean(NAMESPACE_LAUNCHER, key, defaultValue);
    public String toString() {
        return super.toString() + ", mDefaultValueInCode=" + mDefaultValueInCode;
    }
}
+7 −7
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 */

package com.android.launcher3.config;
package com.android.launcher3.uioverrides.flags;

import static com.android.launcher3.config.FeatureFlags.FLAGS_PREF_NAME;

@@ -33,7 +33,7 @@ import androidx.preference.PreferenceGroup;
import androidx.preference.SwitchPreference;

import com.android.launcher3.R;
import com.android.launcher3.config.FeatureFlags.DebugFlag;
import com.android.launcher3.config.FeatureFlags;

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

        @Override
        public void putBoolean(String key, boolean value) {
            for (DebugFlag flag : FeatureFlags.getDebugFlags()) {
            for (DebugFlag flag : FlagsFactory.getDebugFlags()) {
                if (flag.key.equals(key)) {
                    SharedPreferences prefs = mContext.getSharedPreferences(
                            FLAGS_PREF_NAME, Context.MODE_PRIVATE);
@@ -71,7 +71,7 @@ public final class FlagTogglerPrefUi {

        @Override
        public boolean getBoolean(String key, boolean defaultValue) {
            for (DebugFlag flag : FeatureFlags.getDebugFlags()) {
            for (DebugFlag flag : FlagsFactory.getDebugFlags()) {
                if (flag.key.equals(key)) {
                    return mContext.getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE)
                            .getBoolean(key, flag.defaultValue);
@@ -93,7 +93,7 @@ public final class FlagTogglerPrefUi {
        // 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 : FeatureFlags.getDebugFlags()) {
        for (DebugFlag flag : FlagsFactory.getDebugFlags()) {
            SwitchPreference switchPreference = new SwitchPreference(mContext);
            switchPreference.setKey(flag.key);
            switchPreference.setDefaultValue(flag.defaultValue);
@@ -149,7 +149,7 @@ public final class FlagTogglerPrefUi {
    }

    private boolean anyChanged() {
        for (DebugFlag flag : FeatureFlags.getDebugFlags()) {
        for (DebugFlag flag : FlagsFactory.getDebugFlags()) {
            if (getFlagStateFromSharedPrefs(flag) != flag.get()) {
                return true;
            }
+124 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.launcher3.uioverrides.flags;

import static android.app.ActivityThread.currentApplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.DeviceConfig;

import com.android.launcher3.Utilities;
import com.android.launcher3.config.FeatureFlags.BooleanFlag;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Helper class to create various flags for system build
 */
public class FlagsFactory {

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

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

    /**
     * Creates a new debug flag
     */
    public static BooleanFlag getDebugFlag(
            int bugId, String key, boolean defaultValue, String description) {
        if (Utilities.IS_DEBUG_DEVICE) {
            SharedPreferences prefs = currentApplication()
                    .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 {
            return new BooleanFlag(defaultValue);
        }
    }

    /**
     * Creates a new release flag
     */
    public static BooleanFlag getReleaseFlag(
            int bugId, String key, boolean defaultValueInCode, String description) {
        boolean defaultValue = DeviceConfig.getBoolean(NAMESPACE_LAUNCHER, key, defaultValueInCode);
        if (Utilities.IS_DEBUG_DEVICE) {
            SharedPreferences prefs = currentApplication()
                    .getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE);
            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 {
            return new BooleanFlag(defaultValue);
        }
    }

    static List<DebugFlag> getDebugFlags() {
        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;
    }

    /**
     * Dumps the current flags state to the print writer
     */
    public static void dump(PrintWriter pw) {
        if (!Utilities.IS_DEBUG_DEVICE) {
            return;
        }
        pw.println("DeviceFlags:");
        synchronized (sDebugFlags) {
            for (DebugFlag flag : sDebugFlags) {
                if (flag instanceof DeviceFlag) {
                    pw.println("  " + flag);
                }
            }
        }
        pw.println("DebugFlags:");
        synchronized (sDebugFlags) {
            for (DebugFlag flag : sDebugFlags) {
                if (!(flag instanceof DeviceFlag)) {
                    pw.println("  " + flag);
                }
            }
        }
    }
}
Loading