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_PROVIDER_MODEL = "settings_provider_model";
48     /** @hide */
49     public static final String SETTINGS_USE_NEW_BACKUP_ELIGIBILITY_RULES
50             = "settings_use_new_backup_eligibility_rules";
51     /** @hide */
52     public static final String SETTINGS_ENABLE_SECURITY_HUB = "settings_enable_security_hub";
53 
54     /** @hide */
55     public static final String SETTINGS_SUPPORT_LARGE_SCREEN = "settings_support_large_screen";
56 
57     /** @hide */
58     public static final String SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS =
59             "settings_enable_monitor_phantom_procs";
60 
61     private static final Map<String, String> DEFAULT_FLAGS;
62 
63     static {
64         DEFAULT_FLAGS = new HashMap<>();
65         DEFAULT_FLAGS.put("settings_audio_switcher", "true");
66         DEFAULT_FLAGS.put("settings_systemui_theme", "true");
DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, R)67         DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false");
68         DEFAULT_FLAGS.put("settings_wifi_details_datausage_header", "false");
69         DEFAULT_FLAGS.put("settings_skip_direction_mutable", "true");
DEFAULT_FLAGS.put(SETTINGS_WIFITRACKER2, R)70         DEFAULT_FLAGS.put(SETTINGS_WIFITRACKER2, "true");
71         DEFAULT_FLAGS.put("settings_controller_loading_enhancement", "true");
72         DEFAULT_FLAGS.put("settings_conditionals", "false");
73         // This flags guards a feature introduced in R and will be removed in the next release
74         // (b/148367230).
DEFAULT_FLAGS.put(SETTINGS_DO_NOT_RESTORE_PRESERVED, R)75         DEFAULT_FLAGS.put(SETTINGS_DO_NOT_RESTORE_PRESERVED, "true");
76 
77         DEFAULT_FLAGS.put("settings_tether_all_in_one", "false");
78         DEFAULT_FLAGS.put("settings_contextual_home", "false");
DEFAULT_FLAGS.put(SETTINGS_PROVIDER_MODEL, R)79         DEFAULT_FLAGS.put(SETTINGS_PROVIDER_MODEL, "true");
DEFAULT_FLAGS.put(SETTINGS_USE_NEW_BACKUP_ELIGIBILITY_RULES, R)80         DEFAULT_FLAGS.put(SETTINGS_USE_NEW_BACKUP_ELIGIBILITY_RULES, "true");
DEFAULT_FLAGS.put(SETTINGS_ENABLE_SECURITY_HUB, R)81         DEFAULT_FLAGS.put(SETTINGS_ENABLE_SECURITY_HUB, "true");
DEFAULT_FLAGS.put(SETTINGS_SUPPORT_LARGE_SCREEN, R)82         DEFAULT_FLAGS.put(SETTINGS_SUPPORT_LARGE_SCREEN, "true");
DEFAULT_FLAGS.put(SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS, R)83         DEFAULT_FLAGS.put(SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS, "true");
84     }
85 
86     private static final Set<String> PERSISTENT_FLAGS;
87     static {
88         PERSISTENT_FLAGS = new HashSet<>();
89         PERSISTENT_FLAGS.add(SETTINGS_PROVIDER_MODEL);
90         PERSISTENT_FLAGS.add(SETTINGS_SUPPORT_LARGE_SCREEN);
91         PERSISTENT_FLAGS.add(SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS);
92     }
93 
94     /**
95      * Whether or not a flag is enabled.
96      *
97      * @param feature the flag name
98      * @return true if the flag is enabled (either by default in system, or override by user)
99      */
isEnabled(Context context, String feature)100     public static boolean isEnabled(Context context, String feature) {
101         // Override precedence:
102         // Settings.Global -> sys.fflag.override.* -> static list
103 
104         // Step 1: check if feature flag is set in Settings.Global.
105         String value;
106         if (context != null) {
107             value = Settings.Global.getString(context.getContentResolver(), feature);
108             if (!TextUtils.isEmpty(value)) {
109                 return Boolean.parseBoolean(value);
110             }
111         }
112 
113         // Step 2: check if feature flag has any override.
114         // Flag name: [persist.]sys.fflag.override.<feature>
115         value = SystemProperties.get(getSystemPropertyPrefix(feature) + feature);
116         if (!TextUtils.isEmpty(value)) {
117             return Boolean.parseBoolean(value);
118         }
119         // Step 3: check if feature flag has any default value.
120         value = getAllFeatureFlags().get(feature);
121         return Boolean.parseBoolean(value);
122     }
123 
124     /**
125      * Override feature flag to new state.
126      */
setEnabled(Context context, String feature, boolean enabled)127     public static void setEnabled(Context context, String feature, boolean enabled) {
128         SystemProperties.set(getSystemPropertyPrefix(feature) + feature,
129                 enabled ? "true" : "false");
130     }
131 
132     /**
133      * Returns all feature flags in their raw form.
134      */
getAllFeatureFlags()135     public static Map<String, String> getAllFeatureFlags() {
136         return DEFAULT_FLAGS;
137     }
138 
getSystemPropertyPrefix(String feature)139     private static String getSystemPropertyPrefix(String feature) {
140         return PERSISTENT_FLAGS.contains(feature) ? PERSIST_PREFIX : FFLAG_OVERRIDE_PREFIX;
141     }
142 }
143