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.Map; 27 28 /** 29 * Util class to get feature flag information. 30 * 31 * @hide 32 */ 33 @TestApi 34 public class FeatureFlagUtils { 35 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 DYNAMIC_SYSTEM = "settings_dynamic_system"; 43 public static final String SETTINGS_WIFITRACKER2 = "settings_wifitracker2"; 44 public static final String SETTINGS_FUSE_FLAG = "settings_fuse"; 45 /** @hide */ 46 public static final String SETTINGS_DO_NOT_RESTORE_PRESERVED = 47 "settings_do_not_restore_preserved"; 48 49 private static final Map<String, String> DEFAULT_FLAGS; 50 51 static { 52 DEFAULT_FLAGS = new HashMap<>(); 53 DEFAULT_FLAGS.put("settings_audio_switcher", "true"); 54 DEFAULT_FLAGS.put("settings_systemui_theme", "true"); DEFAULT_FLAGS.put(SETTINGS_FUSE_FLAG, "true")55 DEFAULT_FLAGS.put(SETTINGS_FUSE_FLAG, "true"); DEFAULT_FLAGS.put(DYNAMIC_SYSTEM, "false")56 DEFAULT_FLAGS.put(DYNAMIC_SYSTEM, "false"); DEFAULT_FLAGS.put(SEAMLESS_TRANSFER, "false")57 DEFAULT_FLAGS.put(SEAMLESS_TRANSFER, "false"); DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false")58 DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false"); DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false")59 DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false"); 60 DEFAULT_FLAGS.put("settings_wifi_details_datausage_header", "false"); 61 DEFAULT_FLAGS.put("settings_skip_direction_mutable", "true"); DEFAULT_FLAGS.put(SETTINGS_WIFITRACKER2, "true")62 DEFAULT_FLAGS.put(SETTINGS_WIFITRACKER2, "true"); 63 DEFAULT_FLAGS.put("settings_controller_loading_enhancement", "true"); 64 DEFAULT_FLAGS.put("settings_conditionals", "false"); 65 // This flags guards a feature introduced in R and will be removed in the next release 66 // (b/148367230). DEFAULT_FLAGS.put(SETTINGS_DO_NOT_RESTORE_PRESERVED, "true")67 DEFAULT_FLAGS.put(SETTINGS_DO_NOT_RESTORE_PRESERVED, "true"); 68 69 DEFAULT_FLAGS.put("settings_tether_all_in_one", "false"); 70 } 71 72 /** 73 * Whether or not a flag is enabled. 74 * 75 * @param feature the flag name 76 * @return true if the flag is enabled (either by default in system, or override by user) 77 */ isEnabled(Context context, String feature)78 public static boolean isEnabled(Context context, String feature) { 79 // Override precedence: 80 // Settings.Global -> sys.fflag.override.* -> static list 81 82 // Step 1: check if feature flag is set in Settings.Global. 83 String value; 84 if (context != null) { 85 value = Settings.Global.getString(context.getContentResolver(), feature); 86 if (!TextUtils.isEmpty(value)) { 87 return Boolean.parseBoolean(value); 88 } 89 } 90 91 // Step 2: check if feature flag has any override. Flag name: sys.fflag.override.<feature> 92 value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature); 93 if (!TextUtils.isEmpty(value)) { 94 return Boolean.parseBoolean(value); 95 } 96 // Step 3: check if feature flag has any default value. 97 value = getAllFeatureFlags().get(feature); 98 return Boolean.parseBoolean(value); 99 } 100 101 /** 102 * Override feature flag to new state. 103 */ setEnabled(Context context, String feature, boolean enabled)104 public static void setEnabled(Context context, String feature, boolean enabled) { 105 SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false"); 106 } 107 108 /** 109 * Returns all feature flags in their raw form. 110 */ getAllFeatureFlags()111 public static Map<String, String> getAllFeatureFlags() { 112 return DEFAULT_FLAGS; 113 } 114 } 115