blob: bad26edb9649b5733d58d0c5c59a8b511c9fd409 [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
Fan Zhang93d87e62017-11-13 17:38:53 -080019import android.content.Context;
Fan Zhang690986d2017-08-17 11:22:31 -070020import android.os.SystemProperties;
Fan Zhangaa1387c2017-11-17 11:07:29 -080021import android.provider.Settings;
Fan Zhang690986d2017-08-17 11:22:31 -070022import android.text.TextUtils;
23
Fan Zhang30806362017-12-07 11:15:26 -080024import java.util.HashMap;
Fan Zhang690986d2017-08-17 11:22:31 -070025import java.util.Map;
26
27/**
28 * Util class to get feature flag information.
29 *
30 * @hide
31 */
32public class FeatureFlagUtils {
33
34 public static final String FFLAG_PREFIX = "sys.fflag.";
35 public static final String FFLAG_OVERRIDE_PREFIX = FFLAG_PREFIX + "override.";
Stanley Tngfe8c8332018-06-19 08:48:10 -070036 public static final String PERSIST_PREFIX = "persist." + FFLAG_OVERRIDE_PREFIX;
37 public static final String HEARING_AID_SETTINGS = "settings_bluetooth_hearing_aid";
yuanjiahsu88363e92018-09-06 19:23:52 +080038 public static final String EMERGENCY_DIAL_SHORTCUTS = "settings_emergency_dial_shortcuts";
yuanjiahsu4c7f6c22018-11-01 22:25:28 +080039 public static final String SAFETY_HUB = "settings_safety_hub";
Beth Thibodeau5898ac42018-10-26 13:00:09 -040040 public static final String SCREENRECORD_LONG_PRESS = "settings_screenrecord_long_press";
Fan Zhang690986d2017-08-17 11:22:31 -070041
Fan Zhang30806362017-12-07 11:15:26 -080042 private static final Map<String, String> DEFAULT_FLAGS;
43 static {
44 DEFAULT_FLAGS = new HashMap<>();
ryanywlin651d0332018-03-27 14:49:11 +080045 DEFAULT_FLAGS.put("settings_audio_switcher", "true");
Lucas Dupin94eca2c2018-05-24 20:04:30 -070046 DEFAULT_FLAGS.put("settings_systemui_theme", "true");
Emily Chuang7e15b7c2018-11-12 17:50:41 +080047 DEFAULT_FLAGS.put("settings_dynamic_homepage", "true");
jackqdyulei7c75c902018-11-08 14:25:14 -080048 DEFAULT_FLAGS.put("settings_mobile_network_v2", "true");
Doris Ling1098ab02018-08-30 15:07:16 -070049 DEFAULT_FLAGS.put("settings_data_usage_v2", "false");
jackqdyuleic2da1a22018-09-07 13:37:32 -070050 DEFAULT_FLAGS.put("settings_seamless_transfer", "false");
Stanley Tngafa1f9c2018-09-27 17:44:14 -070051 DEFAULT_FLAGS.put(HEARING_AID_SETTINGS, "false");
yuanjiahsu8d09dd42018-10-26 21:09:23 +080052 DEFAULT_FLAGS.put(EMERGENCY_DIAL_SHORTCUTS, "true");
Antony Sargentdbaeac02018-10-25 15:47:10 -070053 DEFAULT_FLAGS.put("settings_network_and_internet_v2", "false");
yuanjiahsu4c7f6c22018-11-01 22:25:28 +080054 DEFAULT_FLAGS.put(SAFETY_HUB, "false");
Beth Thibodeau5898ac42018-10-26 13:00:09 -040055 DEFAULT_FLAGS.put(SCREENRECORD_LONG_PRESS, "false");
Fan Zhang30806362017-12-07 11:15:26 -080056 }
57
Fan Zhang690986d2017-08-17 11:22:31 -070058 /**
59 * Whether or not a flag is enabled.
60 *
61 * @param feature the flag name
62 * @return true if the flag is enabled (either by default in system, or override by user)
63 */
Fan Zhang93d87e62017-11-13 17:38:53 -080064 public static boolean isEnabled(Context context, String feature) {
Fan Zhangaa1387c2017-11-17 11:07:29 -080065 // Override precedence:
Fan Zhang30806362017-12-07 11:15:26 -080066 // Settings.Global -> sys.fflag.override.* -> static list
Fan Zhangaa1387c2017-11-17 11:07:29 -080067
68 // Step 1: check if feature flag is set in Settings.Global.
69 String value;
70 if (context != null) {
71 value = Settings.Global.getString(context.getContentResolver(), feature);
72 if (!TextUtils.isEmpty(value)) {
73 return Boolean.parseBoolean(value);
74 }
75 }
76
77 // Step 2: check if feature flag has any override. Flag name: sys.fflag.override.<feature>
78 value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature);
Fan Zhang690986d2017-08-17 11:22:31 -070079 if (!TextUtils.isEmpty(value)) {
80 return Boolean.parseBoolean(value);
81 }
Fan Zhang30806362017-12-07 11:15:26 -080082 // Step 3: check if feature flag has any default value.
83 value = getAllFeatureFlags().get(feature);
Fan Zhang690986d2017-08-17 11:22:31 -070084 return Boolean.parseBoolean(value);
85 }
86
87 /**
Fan Zhangcfd02742017-10-03 14:37:44 -070088 * Override feature flag to new state.
89 */
Fan Zhangaa1387c2017-11-17 11:07:29 -080090 public static void setEnabled(Context context, String feature, boolean enabled) {
Fan Zhangcfd02742017-10-03 14:37:44 -070091 SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false");
92 }
93
94 /**
Fan Zhang690986d2017-08-17 11:22:31 -070095 * Returns all feature flags in their raw form.
96 */
97 public static Map<String, String> getAllFeatureFlags() {
Fan Zhang30806362017-12-07 11:15:26 -080098 return DEFAULT_FLAGS;
Fan Zhang690986d2017-08-17 11:22:31 -070099 }
100}