blob: b313514c8686c20eed5c23edd105e3184ee886f0 [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.";
36
Fan Zhang30806362017-12-07 11:15:26 -080037 private static final Map<String, String> DEFAULT_FLAGS;
38 static {
39 DEFAULT_FLAGS = new HashMap<>();
40 DEFAULT_FLAGS.put("device_info_v2", "true");
Doris Lingc5c9b7f2018-01-12 16:02:27 -080041 DEFAULT_FLAGS.put("settings_app_info_v2", "true");
jackqdyulei65fe4552017-12-15 10:59:15 -080042 DEFAULT_FLAGS.put("settings_connected_device_v2", "true");
jackqdyuleib98408c2018-01-23 10:22:16 -080043 DEFAULT_FLAGS.put("settings_battery_v2", "true");
jackqdyuleiee0e2e92017-12-08 09:45:23 -080044 DEFAULT_FLAGS.put("settings_battery_display_app_list", "false");
Fan Zhang73748262018-01-05 13:10:16 -080045 DEFAULT_FLAGS.put("settings_security_settings_v2", "true");
Joachim Saueree901612018-01-22 15:52:26 +000046 DEFAULT_FLAGS.put("settings_zone_picker_v2", "true");
Doris Lingc359dfa2018-01-10 17:42:06 -080047 DEFAULT_FLAGS.put("settings_suggestion_ui_v2", "false");
Daniel Nishi9ecc5452018-01-17 15:07:26 -080048 DEFAULT_FLAGS.put("settings_about_phone_v2", "false");
Fan Zhang30806362017-12-07 11:15:26 -080049 }
50
Fan Zhang690986d2017-08-17 11:22:31 -070051 /**
52 * Whether or not a flag is enabled.
53 *
54 * @param feature the flag name
55 * @return true if the flag is enabled (either by default in system, or override by user)
56 */
Fan Zhang93d87e62017-11-13 17:38:53 -080057 public static boolean isEnabled(Context context, String feature) {
Fan Zhangaa1387c2017-11-17 11:07:29 -080058 // Override precedence:
Fan Zhang30806362017-12-07 11:15:26 -080059 // Settings.Global -> sys.fflag.override.* -> static list
Fan Zhangaa1387c2017-11-17 11:07:29 -080060
61 // Step 1: check if feature flag is set in Settings.Global.
62 String value;
63 if (context != null) {
64 value = Settings.Global.getString(context.getContentResolver(), feature);
65 if (!TextUtils.isEmpty(value)) {
66 return Boolean.parseBoolean(value);
67 }
68 }
69
70 // Step 2: check if feature flag has any override. Flag name: sys.fflag.override.<feature>
71 value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature);
Fan Zhang690986d2017-08-17 11:22:31 -070072 if (!TextUtils.isEmpty(value)) {
73 return Boolean.parseBoolean(value);
74 }
Fan Zhang30806362017-12-07 11:15:26 -080075 // Step 3: check if feature flag has any default value.
76 value = getAllFeatureFlags().get(feature);
Fan Zhang690986d2017-08-17 11:22:31 -070077 return Boolean.parseBoolean(value);
78 }
79
80 /**
Fan Zhangcfd02742017-10-03 14:37:44 -070081 * Override feature flag to new state.
82 */
Fan Zhangaa1387c2017-11-17 11:07:29 -080083 public static void setEnabled(Context context, String feature, boolean enabled) {
Fan Zhangcfd02742017-10-03 14:37:44 -070084 SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false");
85 }
86
87 /**
Fan Zhang690986d2017-08-17 11:22:31 -070088 * Returns all feature flags in their raw form.
89 */
90 public static Map<String, String> getAllFeatureFlags() {
Fan Zhang30806362017-12-07 11:15:26 -080091 return DEFAULT_FLAGS;
Fan Zhang690986d2017-08-17 11:22:31 -070092 }
93}