blob: 5fdac810af2919bb87305445e134acc06049df8a [file] [log] [blame]
Fan Zhang690986d2017-08-17 11:22:31 -07001/*
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
17package android.util;
18
Howard Chena7e62a82019-05-24 02:04:50 +080019import android.annotation.TestApi;
Fan Zhang93d87e62017-11-13 17:38:53 -080020import android.content.Context;
Fan Zhang690986d2017-08-17 11:22:31 -070021import android.os.SystemProperties;
Fan Zhangaa1387c2017-11-17 11:07:29 -080022import android.provider.Settings;
Fan Zhang690986d2017-08-17 11:22:31 -070023import android.text.TextUtils;
24
Fan Zhang30806362017-12-07 11:15:26 -080025import java.util.HashMap;
Fan Zhang690986d2017-08-17 11:22:31 -070026import java.util.Map;
27
28/**
29 * Util class to get feature flag information.
30 *
31 * @hide
32 */
Howard Chena7e62a82019-05-24 02:04:50 +080033@TestApi
Fan Zhang690986d2017-08-17 11:22:31 -070034public class FeatureFlagUtils {
35
36 public static final String FFLAG_PREFIX = "sys.fflag.";
37 public static final String FFLAG_OVERRIDE_PREFIX = FFLAG_PREFIX + "override.";
Stanley Tngfe8c8332018-06-19 08:48:10 -070038 public static final String PERSIST_PREFIX = "persist." + FFLAG_OVERRIDE_PREFIX;
linanson5fbcaf42019-01-22 19:17:39 +080039 public static final String SEAMLESS_TRANSFER = "settings_seamless_transfer";
Stanley Tngfe8c8332018-06-19 08:48:10 -070040 public static final String HEARING_AID_SETTINGS = "settings_bluetooth_hearing_aid";
Beth Thibodeau5898ac42018-10-26 13:00:09 -040041 public static final String SCREENRECORD_LONG_PRESS = "settings_screenrecord_long_press";
Po-Chien Hsuehc5b69312019-04-02 13:59:03 +080042 public static final String DYNAMIC_SYSTEM = "settings_dynamic_system";
Quang Luong3fbca102019-06-24 14:46:45 -070043 public static final String SETTINGS_WIFITRACKER2 = "settings_wifitracker2";
Abhijeet Kaurb9847712019-11-19 16:35:47 +000044 public static final String SETTINGS_FUSE_FLAG = "settings_fuse";
Steve Elliottb8cede42020-01-13 13:14:39 -050045 public static final String NOTIF_CONVO_BYPASS_SHORTCUT_REQ =
46 "settings_notif_convo_bypass_shortcut_req";
Al Sutton27c64a32020-01-21 14:55:56 +000047 /** @hide */
48 public static final String BACKUP_NO_KV_DATA_CHANGE_CALLS =
49 "backup_enable_no_data_notification_calls";
Ruslan Tkhakokhov6fb2bb12020-01-27 09:26:40 +000050 /** @hide */
51 public static final String SETTINGS_DO_NOT_RESTORE_PRESERVED =
52 "settings_do_not_restore_preserved";
Fan Zhang690986d2017-08-17 11:22:31 -070053
Fan Zhang30806362017-12-07 11:15:26 -080054 private static final Map<String, String> DEFAULT_FLAGS;
Ahan Wu723a80e2018-11-07 20:39:32 +080055
Fan Zhang30806362017-12-07 11:15:26 -080056 static {
57 DEFAULT_FLAGS = new HashMap<>();
ryanywlin651d0332018-03-27 14:49:11 +080058 DEFAULT_FLAGS.put("settings_audio_switcher", "true");
Johnson Lu73eb2bf2018-12-05 14:36:54 +080059 DEFAULT_FLAGS.put("settings_systemui_theme", "true");
Abhijeet Kaurb9847712019-11-19 16:35:47 +000060 DEFAULT_FLAGS.put(SETTINGS_FUSE_FLAG, "false");
Po-Chien Hsuehc5b69312019-04-02 13:59:03 +080061 DEFAULT_FLAGS.put(DYNAMIC_SYSTEM, "false");
linanson5fbcaf42019-01-22 19:17:39 +080062 DEFAULT_FLAGS.put(SEAMLESS_TRANSFER, "false");
Johnson Lu73eb2bf2018-12-05 14:36:54 +080063 DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false");
Beth Thibodeau5898ac42018-10-26 13:00:09 -040064 DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false");
cosmohsieh205e5b92019-03-14 13:50:17 +080065 DEFAULT_FLAGS.put("settings_wifi_details_datausage_header", "false");
Beverly4844d722019-06-10 11:52:16 -040066 DEFAULT_FLAGS.put("settings_skip_direction_mutable", "true");
Quang Luong3fbca102019-06-24 14:46:45 -070067 DEFAULT_FLAGS.put(SETTINGS_WIFITRACKER2, "false");
Sunny Shaob2e89152019-11-12 14:38:56 +080068 DEFAULT_FLAGS.put("settings_controller_loading_enhancement", "false");
Yi-Ling Chuangafc4d882019-12-13 12:06:53 +080069 DEFAULT_FLAGS.put("settings_conditionals", "false");
Julia Reynoldsdcd70d62020-01-15 10:33:43 -050070 DEFAULT_FLAGS.put(NOTIF_CONVO_BYPASS_SHORTCUT_REQ, "true");
Al Sutton27c64a32020-01-21 14:55:56 +000071
72 // Disabled until backup transports support it.
73 DEFAULT_FLAGS.put(BACKUP_NO_KV_DATA_CHANGE_CALLS, "false");
Ruslan Tkhakokhov6fb2bb12020-01-27 09:26:40 +000074 // Disabled by default until b/148278926 is resolved. This flags guards a feature
75 // introduced in R and will be removed in the next release (b/148367230).
76 DEFAULT_FLAGS.put(SETTINGS_DO_NOT_RESTORE_PRESERVED, "false");
Zhen Zhang7f5b5d12020-01-28 15:11:58 -080077
78 DEFAULT_FLAGS.put("settings_tether_all_in_one", "false");
Fan Zhang30806362017-12-07 11:15:26 -080079 }
80
Fan Zhang690986d2017-08-17 11:22:31 -070081 /**
82 * Whether or not a flag is enabled.
83 *
84 * @param feature the flag name
85 * @return true if the flag is enabled (either by default in system, or override by user)
86 */
Fan Zhang93d87e62017-11-13 17:38:53 -080087 public static boolean isEnabled(Context context, String feature) {
Fan Zhangaa1387c2017-11-17 11:07:29 -080088 // Override precedence:
Fan Zhang30806362017-12-07 11:15:26 -080089 // Settings.Global -> sys.fflag.override.* -> static list
Fan Zhangaa1387c2017-11-17 11:07:29 -080090
91 // Step 1: check if feature flag is set in Settings.Global.
92 String value;
93 if (context != null) {
94 value = Settings.Global.getString(context.getContentResolver(), feature);
95 if (!TextUtils.isEmpty(value)) {
96 return Boolean.parseBoolean(value);
97 }
98 }
99
100 // Step 2: check if feature flag has any override. Flag name: sys.fflag.override.<feature>
101 value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature);
Fan Zhang690986d2017-08-17 11:22:31 -0700102 if (!TextUtils.isEmpty(value)) {
103 return Boolean.parseBoolean(value);
104 }
Fan Zhang30806362017-12-07 11:15:26 -0800105 // Step 3: check if feature flag has any default value.
106 value = getAllFeatureFlags().get(feature);
Fan Zhang690986d2017-08-17 11:22:31 -0700107 return Boolean.parseBoolean(value);
108 }
109
110 /**
Fan Zhangcfd02742017-10-03 14:37:44 -0700111 * Override feature flag to new state.
112 */
Fan Zhangaa1387c2017-11-17 11:07:29 -0800113 public static void setEnabled(Context context, String feature, boolean enabled) {
Fan Zhangcfd02742017-10-03 14:37:44 -0700114 SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false");
115 }
116
117 /**
Fan Zhang690986d2017-08-17 11:22:31 -0700118 * Returns all feature flags in their raw form.
119 */
120 public static Map<String, String> getAllFeatureFlags() {
Fan Zhang30806362017-12-07 11:15:26 -0800121 return DEFAULT_FLAGS;
Fan Zhang690986d2017-08-17 11:22:31 -0700122 }
123}