Debian Codegraph @

core/java/android/util/FeatureFlagUtils.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package android.util;
18
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;
24
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29
30 /**
31 * Util class to get feature flag information.
32 *
33 * @hide
34 */
35 @TestApi
36 public class FeatureFlagUtils {
37
38 public static final String FFLAG_PREFIX = "sys.fflag.";
39 public static final String FFLAG_OVERRIDE_PREFIX = FFLAG_PREFIX + "override.";
40 public static final String PERSIST_PREFIX = "persist." + FFLAG_OVERRIDE_PREFIX;
41 public static final String HEARING_AID_SETTINGS = "settings_bluetooth_hearing_aid";
42 public static final String SETTINGS_WIFITRACKER2 = "settings_wifitracker2";
43 /** @hide */
44 public static final String SETTINGS_DO_NOT_RESTORE_PRESERVED =
45 "settings_do_not_restore_preserved";
46 /** @hide */
47 public static final String SETTINGS_USE_NEW_BACKUP_ELIGIBILITY_RULES
48 = "settings_use_new_backup_eligibility_rules";
49 /** @hide */
50 public static final String SETTINGS_ENABLE_SECURITY_HUB = "settings_enable_security_hub";
51 /** @hide */
52 public static final String SETTINGS_SUPPORT_LARGE_SCREEN = "settings_support_large_screen";
53
54 /**
55 * Support locale opt-out and opt-in switch for per app's language.
56 * @hide
57 */
58 public static final String SETTINGS_APP_LOCALE_OPT_IN_ENABLED =
59 "settings_app_locale_opt_in_enabled";
60
61 /**
62 * Launch the Volume panel in SystemUI.
63 * @hide
64 */
65 public static final String SETTINGS_VOLUME_PANEL_IN_SYSTEMUI =
66 "settings_volume_panel_in_systemui";
67
68 /** @hide */
69 public static final String SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS =
70 "settings_enable_monitor_phantom_procs";
71
72 /**
73 * Support dark theme activation at Bedtime.
74 * @hide
75 */
76 public static final String SETTINGS_APP_ALLOW_DARK_THEME_ACTIVATION_AT_BEDTIME =
77 "settings_app_allow_dark_theme_activation_at_bedtime";
78
79 /**
80 * Flag to decouple bluetooth LE Audio Broadcast from Unicast
81 * If the flag is true, the broadcast feature will be enabled when the phone
82 * is connected to the BLE device.
83 * If the flag is false, it is not necessary to connect the BLE device.
84 * @hide
85 */
86 public static final String SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST =
87 "settings_need_connected_ble_device_for_broadcast";
88
89 /**
90 * Hide back key in the Settings two pane design.
91 * @hide
92 */
93 public static final String SETTINGS_HIDE_SECOND_LAYER_PAGE_NAVIGATE_UP_BUTTON_IN_TWO_PANE =
94 "settings_hide_second_layer_page_navigate_up_button_in_two_pane";
95
96 private static final Map<String, String> DEFAULT_FLAGS;
97
98 static {
99 DEFAULT_FLAGS = new HashMap<>();
100 DEFAULT_FLAGS.put("settings_audio_switcher", "true");
101 DEFAULT_FLAGS.put("settings_systemui_theme", "true");
102 DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false");
103 DEFAULT_FLAGS.put("settings_wifi_details_datausage_header", "false");
104 DEFAULT_FLAGS.put("settings_skip_direction_mutable", "true");
105 DEFAULT_FLAGS.put(SETTINGS_WIFITRACKER2, "true");
106 DEFAULT_FLAGS.put("settings_controller_loading_enhancement", "true");
107 DEFAULT_FLAGS.put("settings_conditionals", "false");
108 // This flags guards a feature introduced in R and will be removed in the next release
109 // (b/148367230).
110 DEFAULT_FLAGS.put(SETTINGS_DO_NOT_RESTORE_PRESERVED, "true");
111
112 DEFAULT_FLAGS.put("settings_tether_all_in_one", "false");
113 DEFAULT_FLAGS.put("settings_contextual_home", "false");
114 DEFAULT_FLAGS.put(SETTINGS_USE_NEW_BACKUP_ELIGIBILITY_RULES, "true");
115 DEFAULT_FLAGS.put(SETTINGS_ENABLE_SECURITY_HUB, "true");
116 DEFAULT_FLAGS.put(SETTINGS_SUPPORT_LARGE_SCREEN, "true");
117 DEFAULT_FLAGS.put("settings_search_always_expand", "true");
118 DEFAULT_FLAGS.put(SETTINGS_APP_LOCALE_OPT_IN_ENABLED, "true");
119 DEFAULT_FLAGS.put(SETTINGS_VOLUME_PANEL_IN_SYSTEMUI, "false");
120 DEFAULT_FLAGS.put(SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS, "true");
121 DEFAULT_FLAGS.put(SETTINGS_APP_ALLOW_DARK_THEME_ACTIVATION_AT_BEDTIME, "true");
122 DEFAULT_FLAGS.put(SETTINGS_HIDE_SECOND_LAYER_PAGE_NAVIGATE_UP_BUTTON_IN_TWO_PANE, "true");
123 DEFAULT_FLAGS.put(SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST, "true");
124 }
125
126 private static final Set<String> PERSISTENT_FLAGS;
127 static {
128 PERSISTENT_FLAGS = new HashSet<>();
129 PERSISTENT_FLAGS.add(SETTINGS_APP_LOCALE_OPT_IN_ENABLED);
130 PERSISTENT_FLAGS.add(SETTINGS_SUPPORT_LARGE_SCREEN);
131 PERSISTENT_FLAGS.add(SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS);
132 PERSISTENT_FLAGS.add(SETTINGS_APP_ALLOW_DARK_THEME_ACTIVATION_AT_BEDTIME);
133 PERSISTENT_FLAGS.add(SETTINGS_HIDE_SECOND_LAYER_PAGE_NAVIGATE_UP_BUTTON_IN_TWO_PANE);
134 }
135
136 /**
137 * Whether or not a flag is enabled.
138 *
139 * @param feature the flag name
140 * @return true if the flag is enabled (either by default in system, or override by user)
141 */
142 public static boolean isEnabled(Context context, String feature) {
143 // Override precedence:
144 // Settings.Global -> sys.fflag.override.* -> static list
145
146 // Step 1: check if feature flag is set in Settings.Global.
147 String value;
148 if (context != null) {
149 value = Settings.Global.getString(context.getContentResolver(), feature);
150 if (!TextUtils.isEmpty(value)) {
151 return Boolean.parseBoolean(value);
152 }
153 }
154
155 // Step 2: check if feature flag has any override.
156 // Flag name: [persist.]sys.fflag.override.<feature>
157 value = SystemProperties.get(getSystemPropertyPrefix(feature) + feature);
158 if (!TextUtils.isEmpty(value)) {
159 return Boolean.parseBoolean(value);
160 }
161 // Step 3: check if feature flag has any default value.
162 value = getAllFeatureFlags().get(feature);
163 return Boolean.parseBoolean(value);
164 }
165
166 /**
167 * Override feature flag to new state.
168 */
169 public static void setEnabled(Context context, String feature, boolean enabled) {
170 SystemProperties.set(getSystemPropertyPrefix(feature) + feature,
171 enabled ? "true" : "false");
172 }
173
174 /**
175 * Returns all feature flags in their raw form.
176 */
177 public static Map<String, String> getAllFeatureFlags() {
178 return DEFAULT_FLAGS;
179 }
180
181 private static String getSystemPropertyPrefix(String feature) {
182 return PERSISTENT_FLAGS.contains(feature) ? PERSIST_PREFIX : FFLAG_OVERRIDE_PREFIX;
183 }
184 }