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 | |
Fan Zhang | 93d87e6 | 2017-11-13 17:38:53 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 20 | import android.os.SystemProperties; |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 21 | import android.provider.Settings; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
| 23 | |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 24 | import java.util.HashMap; |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 25 | import java.util.HashSet; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 26 | import java.util.Map; |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 27 | import java.util.Set; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Util class to get feature flag information. |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
| 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"; |
yuanjiahsu | 4c7f6c2 | 2018-11-01 22:25:28 +0800 | [diff] [blame] | 41 | public static final String SAFETY_HUB = "settings_safety_hub"; |
Beth Thibodeau | 5898ac4 | 2018-10-26 13:00:09 -0400 | [diff] [blame] | 42 | public static final String SCREENRECORD_LONG_PRESS = "settings_screenrecord_long_press"; |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 43 | public static final String AOD_IMAGEWALLPAPER_ENABLED = "settings_aod_imagewallpaper_enabled"; |
Aaron Heuckroth | 166392f | 2019-01-17 16:50:59 -0500 | [diff] [blame] | 44 | public static final String GLOBAL_ACTIONS_GRID_ENABLED = "settings_global_actions_grid_enabled"; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 45 | |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 46 | private static final Map<String, String> DEFAULT_FLAGS; |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 47 | private static final Set<String> OBSERVABLE_FLAGS; |
| 48 | |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 49 | static { |
| 50 | DEFAULT_FLAGS = new HashMap<>(); |
ryanywlin | 651d033 | 2018-03-27 14:49:11 +0800 | [diff] [blame] | 51 | DEFAULT_FLAGS.put("settings_audio_switcher", "true"); |
Emily Chuang | 7e15b7c | 2018-11-12 17:50:41 +0800 | [diff] [blame] | 52 | DEFAULT_FLAGS.put("settings_dynamic_homepage", "true"); |
jackqdyulei | 7c75c90 | 2018-11-08 14:25:14 -0800 | [diff] [blame] | 53 | DEFAULT_FLAGS.put("settings_mobile_network_v2", "true"); |
Antony Sargent | dbaeac0 | 2018-10-25 15:47:10 -0700 | [diff] [blame] | 54 | DEFAULT_FLAGS.put("settings_network_and_internet_v2", "false"); |
jackqdyulei | 0e42f8a | 2019-01-24 17:21:11 -0800 | [diff] [blame] | 55 | DEFAULT_FLAGS.put("settings_slice_injection", "true"); |
Johnson Lu | 73eb2bf | 2018-12-05 14:36:54 +0800 | [diff] [blame] | 56 | DEFAULT_FLAGS.put("settings_systemui_theme", "true"); |
Johnson Lu | b71cf3b | 2019-01-23 16:04:41 +0800 | [diff] [blame] | 57 | DEFAULT_FLAGS.put("settings_wifi_dpp", "true"); |
| 58 | DEFAULT_FLAGS.put("settings_wifi_mac_randomization", "true"); |
| 59 | DEFAULT_FLAGS.put("settings_wifi_sharing", "true"); |
Doris Ling | b97faba | 2019-01-30 12:51:03 -0800 | [diff] [blame] | 60 | DEFAULT_FLAGS.put("settings_mainline_module", "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"); |
yuanjiahsu | 4c7f6c2 | 2018-11-01 22:25:28 +0800 | [diff] [blame] | 63 | DEFAULT_FLAGS.put(SAFETY_HUB, "false"); |
Beth Thibodeau | 5898ac4 | 2018-10-26 13:00:09 -0400 | [diff] [blame] | 64 | DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false"); |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 65 | DEFAULT_FLAGS.put(AOD_IMAGEWALLPAPER_ENABLED, "false"); |
Aaron Heuckroth | 166392f | 2019-01-17 16:50:59 -0500 | [diff] [blame] | 66 | DEFAULT_FLAGS.put(GLOBAL_ACTIONS_GRID_ENABLED, "false"); |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 67 | |
| 68 | OBSERVABLE_FLAGS = new HashSet<>(); |
| 69 | OBSERVABLE_FLAGS.add(AOD_IMAGEWALLPAPER_ENABLED); |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 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 | */ |
Fan Zhang | 93d87e6 | 2017-11-13 17:38:53 -0800 | [diff] [blame] | 78 | public static boolean isEnabled(Context context, String feature) { |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 79 | // Override precedence: |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 80 | // Settings.Global -> sys.fflag.override.* -> static list |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 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); |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 93 | if (!TextUtils.isEmpty(value)) { |
| 94 | return Boolean.parseBoolean(value); |
| 95 | } |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 96 | // Step 3: check if feature flag has any default value. |
| 97 | value = getAllFeatureFlags().get(feature); |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 98 | return Boolean.parseBoolean(value); |
| 99 | } |
| 100 | |
| 101 | /** |
Fan Zhang | cfd0274 | 2017-10-03 14:37:44 -0700 | [diff] [blame] | 102 | * Override feature flag to new state. |
| 103 | */ |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 104 | public static void setEnabled(Context context, String feature, boolean enabled) { |
Fan Zhang | cfd0274 | 2017-10-03 14:37:44 -0700 | [diff] [blame] | 105 | SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false"); |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 106 | |
| 107 | // Also update Settings.Global if needed so that we can observe it via observer. |
| 108 | if (OBSERVABLE_FLAGS.contains(feature)) { |
| 109 | setObservableFlag(context, feature, enabled); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private static void setObservableFlag(Context context, String feature, boolean enabled) { |
| 114 | Settings.Global.putString( |
| 115 | context.getContentResolver(), feature, enabled ? "true" : "false"); |
Fan Zhang | cfd0274 | 2017-10-03 14:37:44 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 119 | * Returns all feature flags in their raw form. |
| 120 | */ |
| 121 | public static Map<String, String> getAllFeatureFlags() { |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 122 | return DEFAULT_FLAGS; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 123 | } |
| 124 | } |