2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.annotation.TestApi;
20 import android.content.Context;
21 import android.os.SystemProperties;
22 import android.provider.Settings;
23 import android.text.TextUtils;
25 import java.util.HashMap;
29 * Util class to get feature flag information.
34 public class FeatureFlagUtils {
36 public static final String FFLAG_PREFIX = "sys.fflag.";
37 public static final String FFLAG_OVERRIDE_PREFIX = FFLAG_PREFIX + "override.";
38 public static final String PERSIST_PREFIX = "persist." + FFLAG_OVERRIDE_PREFIX;
39 public static final String SEAMLESS_TRANSFER = "settings_seamless_transfer";
40 public static final String HEARING_AID_SETTINGS = "settings_bluetooth_hearing_aid";
41 public static final String SCREENRECORD_LONG_PRESS = "settings_screenrecord_long_press";
42 public static final String PIXEL_WALLPAPER_CATEGORY_SWITCH =
43 "settings_pixel_wallpaper_category_switch";
44 public static final String DYNAMIC_SYSTEM = "settings_dynamic_system";
46 private static final Map<String, String> DEFAULT_FLAGS;
49 DEFAULT_FLAGS = new HashMap<>();
50 DEFAULT_FLAGS.put("settings_audio_switcher", "true");
51 DEFAULT_FLAGS.put("settings_mobile_network_v2", "true");
52 DEFAULT_FLAGS.put("settings_network_and_internet_v2", "true");
53 DEFAULT_FLAGS.put("settings_systemui_theme", "true");
54 DEFAULT_FLAGS.put(DYNAMIC_SYSTEM, "false");
55 DEFAULT_FLAGS.put(SEAMLESS_TRANSFER, "false");
56 DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false");
57 DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false");
58 DEFAULT_FLAGS.put(PIXEL_WALLPAPER_CATEGORY_SWITCH, "false");
59 DEFAULT_FLAGS.put("settings_wifi_details_datausage_header", "false");
63 * Whether or not a flag is enabled.
65 * @param feature the flag name
66 * @return true if the flag is enabled (either by default in system, or override by user)
68 public static boolean isEnabled(Context context, String feature) {
69 // Override precedence:
70 // Settings.Global -> sys.fflag.override.* -> static list
72 // Step 1: check if feature flag is set in Settings.Global.
74 if (context != null) {
75 value = Settings.Global.getString(context.getContentResolver(), feature);
76 if (!TextUtils.isEmpty(value)) {
77 return Boolean.parseBoolean(value);
81 // Step 2: check if feature flag has any override. Flag name: sys.fflag.override.<feature>
82 value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature);
83 if (!TextUtils.isEmpty(value)) {
84 return Boolean.parseBoolean(value);
86 // Step 3: check if feature flag has any default value.
87 value = getAllFeatureFlags().get(feature);
88 return Boolean.parseBoolean(value);
92 * Override feature flag to new state.
94 public static void setEnabled(Context context, String feature, boolean enabled) {
95 SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false");
99 * Returns all feature flags in their raw form.
101 public static Map<String, String> getAllFeatureFlags() {
102 return DEFAULT_FLAGS;