Merge "Make all debug feature flags boot aware." into udc-dev

This commit is contained in:
Stefan Andonian authored and Android (Google) Code Review committed 2023-04-13 22:05:36 +00:00
commit 4c28cd49ca
3 files changed
+23 -22

No files matched your search

@@ -16,12 +16,10 @@
package com.android.launcher3.uioverrides.flags; 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.config.FeatureFlags.FlagState.TEAMFOOD;
import static com.android.launcher3.uioverrides.flags.FlagsFactory.TEAMFOOD_FLAG; import static com.android.launcher3.uioverrides.flags.FlagsFactory.TEAMFOOD_FLAG;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.os.Process; import android.os.Process;
import android.text.Html; import android.text.Html;
@@ -52,31 +50,27 @@ public final class FlagTogglerPrefUi {
private final PreferenceFragmentCompat mFragment; private final PreferenceFragmentCompat mFragment;
private final Context mContext; private final Context mContext;
private final SharedPreferences mSharedPreferences;
private final PreferenceDataStore mDataStore = new PreferenceDataStore() { private final PreferenceDataStore mDataStore = new PreferenceDataStore() {
@Override @Override
public void putBoolean(String key, boolean value) { public void putBoolean(String key, boolean value) {
mSharedPreferences.edit().putBoolean(key, value).apply(); FlagsFactory.getSharedPreferences().edit().putBoolean(key, value).apply();
updateMenu(); updateMenu();
} }
@Override @Override
public boolean getBoolean(String key, boolean defaultValue) { public boolean getBoolean(String key, boolean defaultValue) {
return mSharedPreferences.getBoolean(key, defaultValue); return FlagsFactory.getSharedPreferences().getBoolean(key, defaultValue);
} }
}; };
public FlagTogglerPrefUi(PreferenceFragmentCompat fragment) { public FlagTogglerPrefUi(PreferenceFragmentCompat fragment) {
mFragment = fragment; mFragment = fragment;
mContext = fragment.getActivity(); mContext = fragment.getActivity();
mSharedPreferences = mContext.getSharedPreferences(
FLAGS_PREF_NAME, Context.MODE_PRIVATE);
} }
public void applyTo(PreferenceGroup parent) { public void applyTo(PreferenceGroup parent) {
Set<String> modifiedPrefs = mSharedPreferences.getAll().keySet(); Set<String> modifiedPrefs = FlagsFactory.getSharedPreferences().getAll().keySet();
List<DebugFlag> flags = FlagsFactory.getDebugFlags(); List<DebugFlag> flags = FlagsFactory.getDebugFlags();
flags.sort((f1, f2) -> { flags.sort((f1, f2) -> {
// Sort first by any prefs that the user has changed, then alphabetically. // 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) { public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder); super.onBindViewHolder(holder);
holder.itemView.setOnLongClickListener(v -> { holder.itemView.setOnLongClickListener(v -> {
mSharedPreferences.edit().remove(flag.key).apply(); FlagsFactory.getSharedPreferences().edit().remove(flag.key).apply();
setChecked(getFlagStateFromSharedPrefs(flag)); setChecked(getFlagStateFromSharedPrefs(flag));
updateSummary(this, flag); updateSummary(this, flag);
updateMenu(); updateMenu();
@@ -133,7 +127,7 @@ public final class FlagTogglerPrefUi {
private void updateSummary(SwitchPreference switchPreference, DebugFlag flag) { private void updateSummary(SwitchPreference switchPreference, DebugFlag flag) {
String summary = flag.defaultValue == TEAMFOOD String summary = flag.defaultValue == TEAMFOOD
? "<font color='blue'><b>[TEAMFOOD]</b> </font>" : ""; ? "<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>"; summary += "<font color='red'><b>[OVERRIDDEN]</b> </font>";
} }
if (!TextUtils.isEmpty(summary)) { if (!TextUtils.isEmpty(summary)) {
@@ -156,7 +150,7 @@ public final class FlagTogglerPrefUi {
public void onOptionsItemSelected(MenuItem item) { public void onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_apply_flags) { if (item.getItemId() == R.id.menu_apply_flags) {
mSharedPreferences.edit().commit(); FlagsFactory.getSharedPreferences().edit().commit();
Log.e(TAG, Log.e(TAG,
"Killing launcher process " + Process.myPid() + " to apply new flag values"); "Killing launcher process " + Process.myPid() + " to apply new flag values");
System.exit(0); System.exit(0);
@@ -21,7 +21,6 @@ import static android.app.ActivityThread.currentApplication;
import static com.android.launcher3.BuildConfig.IS_DEBUG_DEVICE; 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.DISABLED;
import static com.android.launcher3.config.FeatureFlags.FlagState.ENABLED; 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 static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import android.content.Context; import android.content.Context;
@@ -30,6 +29,8 @@ import android.provider.DeviceConfig;
import android.provider.DeviceConfig.Properties; import android.provider.DeviceConfig.Properties;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import com.android.launcher3.config.FeatureFlags.BooleanFlag; import com.android.launcher3.config.FeatureFlags.BooleanFlag;
import com.android.launcher3.config.FeatureFlags.FlagState; import com.android.launcher3.config.FeatureFlags.FlagState;
import com.android.launcher3.config.FeatureFlags.IntFlag; import com.android.launcher3.config.FeatureFlags.IntFlag;
@@ -52,10 +53,11 @@ public class FlagsFactory {
private static final FlagsFactory INSTANCE = new FlagsFactory(); private static final FlagsFactory INSTANCE = new FlagsFactory();
private static final boolean FLAG_AUTO_APPLY_ENABLED = true; 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"; public static final String NAMESPACE_LAUNCHER = "launcher";
private static final List<DebugFlag> sDebugFlags = new ArrayList<>(); private static final List<DebugFlag> sDebugFlags = new ArrayList<>();
private static SharedPreferences sSharedPreferences;
static final BooleanFlag TEAMFOOD_FLAG = getReleaseFlag( static final BooleanFlag TEAMFOOD_FLAG = getReleaseFlag(
0, "LAUNCHER_TEAMFOOD", DISABLED, "Enable this flag to opt-in all team food flags"); 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( public static BooleanFlag getDebugFlag(
int bugId, String key, FlagState flagState, String description) { int bugId, String key, FlagState flagState, String description) {
if (IS_DEBUG_DEVICE) { if (IS_DEBUG_DEVICE) {
SharedPreferences prefs = currentApplication()
.getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE);
boolean defaultValue = getEnabledValue(flagState); boolean defaultValue = getEnabledValue(flagState);
boolean currentValue = prefs.getBoolean(key, defaultValue); boolean currentValue = getSharedPreferences().getBoolean(key, defaultValue);
DebugFlag flag = new DebugFlag(key, description, flagState, currentValue); DebugFlag flag = new DebugFlag(key, description, flagState, currentValue);
sDebugFlags.add(flag); sDebugFlags.add(flag);
return flag; return flag;
@@ -115,9 +115,7 @@ public class FlagsFactory {
boolean defaultValueInCode = getEnabledValue(flagState); boolean defaultValueInCode = getEnabledValue(flagState);
boolean defaultValue = DeviceConfig.getBoolean(NAMESPACE_LAUNCHER, key, defaultValueInCode); boolean defaultValue = DeviceConfig.getBoolean(NAMESPACE_LAUNCHER, key, defaultValueInCode);
if (IS_DEBUG_DEVICE) { if (IS_DEBUG_DEVICE) {
SharedPreferences prefs = currentApplication() boolean currentValue = getSharedPreferences().getBoolean(key, defaultValue);
.getSharedPreferences(FLAGS_PREF_NAME, Context.MODE_PRIVATE);
boolean currentValue = prefs.getBoolean(key, defaultValue);
DebugFlag flag = new DeviceFlag(key, description, flagState, currentValue, DebugFlag flag = new DeviceFlag(key, description, flagState, currentValue,
defaultValueInCode); defaultValueInCode);
sDebugFlags.add(flag); 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 * Dumps the current flags state to the print writer
*/ */
@@ -39,8 +39,6 @@ import java.util.function.ToIntFunction;
*/ */
public final class FeatureFlags { public final class FeatureFlags {
public static final String FLAGS_PREF_NAME = "featureFlags";
@VisibleForTesting @VisibleForTesting
public static Predicate<BooleanFlag> sBooleanReader = f -> f.mCurrentValue; public static Predicate<BooleanFlag> sBooleanReader = f -> f.mCurrentValue;
@VisibleForTesting @VisibleForTesting