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