Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 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 | |
Howard Chen | a7e62a8 | 2019-05-24 02:04:50 +0800 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Fan Zhang | 93d87e6 | 2017-11-13 17:38:53 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 21 | import android.os.SystemProperties; |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 22 | import android.provider.Settings; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 23 | import android.text.TextUtils; |
| 24 | |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 25 | import java.util.HashMap; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * Util class to get feature flag information. |
| 30 | * |
| 31 | * @hide |
| 32 | */ |
Howard Chen | a7e62a8 | 2019-05-24 02:04:50 +0800 | [diff] [blame] | 33 | @TestApi |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 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."; |
Stanley Tng | fe8c833 | 2018-06-19 08:48:10 -0700 | [diff] [blame] | 38 | public static final String PERSIST_PREFIX = "persist." + FFLAG_OVERRIDE_PREFIX; |
linanson | 5fbcaf4 | 2019-01-22 19:17:39 +0800 | [diff] [blame] | 39 | public static final String SEAMLESS_TRANSFER = "settings_seamless_transfer"; |
Stanley Tng | fe8c833 | 2018-06-19 08:48:10 -0700 | [diff] [blame] | 40 | public static final String HEARING_AID_SETTINGS = "settings_bluetooth_hearing_aid"; |
Beth Thibodeau | 5898ac4 | 2018-10-26 13:00:09 -0400 | [diff] [blame] | 41 | public static final String SCREENRECORD_LONG_PRESS = "settings_screenrecord_long_press"; |
Po-Chien Hsueh | c5b6931 | 2019-04-02 13:59:03 +0800 | [diff] [blame] | 42 | public static final String DYNAMIC_SYSTEM = "settings_dynamic_system"; |
Quang Luong | 3fbca10 | 2019-06-24 14:46:45 -0700 | [diff] [blame] | 43 | public static final String SETTINGS_WIFITRACKER2 = "settings_wifitracker2"; |
Abhijeet Kaur | b984771 | 2019-11-19 16:35:47 +0000 | [diff] [blame] | 44 | public static final String SETTINGS_FUSE_FLAG = "settings_fuse"; |
Steve Elliott | b8cede4 | 2020-01-13 13:14:39 -0500 | [diff] [blame] | 45 | public static final String NOTIF_CONVO_BYPASS_SHORTCUT_REQ = |
| 46 | "settings_notif_convo_bypass_shortcut_req"; |
Al Sutton | 27c64a3 | 2020-01-21 14:55:56 +0000 | [diff] [blame] | 47 | /** @hide */ |
Ruslan Tkhakokhov | 6fb2bb1 | 2020-01-27 09:26:40 +0000 | [diff] [blame] | 48 | public static final String SETTINGS_DO_NOT_RESTORE_PRESERVED = |
| 49 | "settings_do_not_restore_preserved"; |
Sunny Shao | c83401d | 2020-02-11 17:48:22 +0800 | [diff] [blame] | 50 | /** @hide */ |
| 51 | public static final String SETTINGS_SCHEDULES_FLAG = "settings_schedules"; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 52 | |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 53 | private static final Map<String, String> DEFAULT_FLAGS; |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 54 | |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 55 | static { |
| 56 | DEFAULT_FLAGS = new HashMap<>(); |
ryanywlin | 651d033 | 2018-03-27 14:49:11 +0800 | [diff] [blame] | 57 | DEFAULT_FLAGS.put("settings_audio_switcher", "true"); |
Johnson Lu | 73eb2bf | 2018-12-05 14:36:54 +0800 | [diff] [blame] | 58 | DEFAULT_FLAGS.put("settings_systemui_theme", "true"); |
Abhijeet Kaur | 01f58ba | 2020-02-14 14:36:50 +0000 | [diff] [blame] | 59 | DEFAULT_FLAGS.put(SETTINGS_FUSE_FLAG, "true"); |
Po-Chien Hsueh | c5b6931 | 2019-04-02 13:59:03 +0800 | [diff] [blame] | 60 | DEFAULT_FLAGS.put(DYNAMIC_SYSTEM, "false"); |
linanson | 5fbcaf4 | 2019-01-22 19:17:39 +0800 | [diff] [blame] | 61 | DEFAULT_FLAGS.put(SEAMLESS_TRANSFER, "false"); |
Johnson Lu | 73eb2bf | 2018-12-05 14:36:54 +0800 | [diff] [blame] | 62 | DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false"); |
Beth Thibodeau | 5898ac4 | 2018-10-26 13:00:09 -0400 | [diff] [blame] | 63 | DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false"); |
cosmohsieh | 205e5b9 | 2019-03-14 13:50:17 +0800 | [diff] [blame] | 64 | DEFAULT_FLAGS.put("settings_wifi_details_datausage_header", "false"); |
Beverly | 4844d72 | 2019-06-10 11:52:16 -0400 | [diff] [blame] | 65 | DEFAULT_FLAGS.put("settings_skip_direction_mutable", "true"); |
Arc Wang | 8de1caf | 2020-03-02 15:38:06 +0800 | [diff] [blame] | 66 | DEFAULT_FLAGS.put(SETTINGS_WIFITRACKER2, "true"); |
Sunny Shao | b2e8915 | 2019-11-12 14:38:56 +0800 | [diff] [blame] | 67 | DEFAULT_FLAGS.put("settings_controller_loading_enhancement", "false"); |
Yi-Ling Chuang | afc4d88 | 2019-12-13 12:06:53 +0800 | [diff] [blame] | 68 | DEFAULT_FLAGS.put("settings_conditionals", "false"); |
Julia Reynolds | dcd70d6 | 2020-01-15 10:33:43 -0500 | [diff] [blame] | 69 | DEFAULT_FLAGS.put(NOTIF_CONVO_BYPASS_SHORTCUT_REQ, "true"); |
Ruslan Tkhakokhov | 2b5cd2d | 2020-03-12 00:03:38 +0000 | [diff] [blame] | 70 | // This flags guards a feature introduced in R and will be removed in the next release |
| 71 | // (b/148367230). |
| 72 | DEFAULT_FLAGS.put(SETTINGS_DO_NOT_RESTORE_PRESERVED, "true"); |
Zhen Zhang | 7f5b5d1 | 2020-01-28 15:11:58 -0800 | [diff] [blame] | 73 | |
Zhen Zhang | e373051 | 2020-03-16 17:05:24 +0000 | [diff] [blame] | 74 | DEFAULT_FLAGS.put("settings_tether_all_in_one", "false"); |
Sunny Shao | c83401d | 2020-02-11 17:48:22 +0800 | [diff] [blame] | 75 | DEFAULT_FLAGS.put(SETTINGS_SCHEDULES_FLAG, "false"); |
Yi-Ling Chuang | e2b34e4 | 2020-02-13 18:05:38 +0800 | [diff] [blame] | 76 | DEFAULT_FLAGS.put("settings_contextual_home2", "false"); |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Whether or not a flag is enabled. |
| 81 | * |
| 82 | * @param feature the flag name |
| 83 | * @return true if the flag is enabled (either by default in system, or override by user) |
| 84 | */ |
Fan Zhang | 93d87e6 | 2017-11-13 17:38:53 -0800 | [diff] [blame] | 85 | public static boolean isEnabled(Context context, String feature) { |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 86 | // Override precedence: |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 87 | // Settings.Global -> sys.fflag.override.* -> static list |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 88 | |
| 89 | // Step 1: check if feature flag is set in Settings.Global. |
| 90 | String value; |
| 91 | if (context != null) { |
| 92 | value = Settings.Global.getString(context.getContentResolver(), feature); |
| 93 | if (!TextUtils.isEmpty(value)) { |
| 94 | return Boolean.parseBoolean(value); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Step 2: check if feature flag has any override. Flag name: sys.fflag.override.<feature> |
| 99 | value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature); |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 100 | if (!TextUtils.isEmpty(value)) { |
| 101 | return Boolean.parseBoolean(value); |
| 102 | } |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 103 | // Step 3: check if feature flag has any default value. |
| 104 | value = getAllFeatureFlags().get(feature); |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 105 | return Boolean.parseBoolean(value); |
| 106 | } |
| 107 | |
| 108 | /** |
Fan Zhang | cfd0274 | 2017-10-03 14:37:44 -0700 | [diff] [blame] | 109 | * Override feature flag to new state. |
| 110 | */ |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 111 | public static void setEnabled(Context context, String feature, boolean enabled) { |
Fan Zhang | cfd0274 | 2017-10-03 14:37:44 -0700 | [diff] [blame] | 112 | SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false"); |
| 113 | } |
| 114 | |
| 115 | /** |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 116 | * Returns all feature flags in their raw form. |
| 117 | */ |
| 118 | public static Map<String, String> getAllFeatureFlags() { |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 119 | return DEFAULT_FLAGS; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 120 | } |
| 121 | } |