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; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
| 28 | * Util class to get feature flag information. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | public 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 Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 37 | private static final Map<String, String> DEFAULT_FLAGS; |
| 38 | static { |
| 39 | DEFAULT_FLAGS = new HashMap<>(); |
jackqdyulei | 65fe455 | 2017-12-15 10:59:15 -0800 | [diff] [blame] | 40 | DEFAULT_FLAGS.put("settings_connected_device_v2", "true"); |
jackqdyulei | b98408c | 2018-01-23 10:22:16 -0800 | [diff] [blame] | 41 | DEFAULT_FLAGS.put("settings_battery_v2", "true"); |
jackqdyulei | ee0e2e9 | 2017-12-08 09:45:23 -0800 | [diff] [blame] | 42 | DEFAULT_FLAGS.put("settings_battery_display_app_list", "false"); |
Joachim Sauer | ee90161 | 2018-01-22 15:52:26 +0000 | [diff] [blame] | 43 | DEFAULT_FLAGS.put("settings_zone_picker_v2", "true"); |
Fan Zhang | ab4ed11 | 2018-02-07 15:39:32 -0800 | [diff] [blame] | 44 | DEFAULT_FLAGS.put("settings_about_phone_v2", "true"); |
Daniel Nishi | 79764127 | 2018-01-02 16:48:33 -0800 | [diff] [blame] | 45 | DEFAULT_FLAGS.put("settings_bluetooth_while_driving", "false"); |
Eric Schwarzenbach | 9aa7459 | 2018-02-06 17:20:22 -0800 | [diff] [blame] | 46 | DEFAULT_FLAGS.put("settings_data_usage_v2", "false"); |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Whether or not a flag is enabled. |
| 51 | * |
| 52 | * @param feature the flag name |
| 53 | * @return true if the flag is enabled (either by default in system, or override by user) |
| 54 | */ |
Fan Zhang | 93d87e6 | 2017-11-13 17:38:53 -0800 | [diff] [blame] | 55 | public static boolean isEnabled(Context context, String feature) { |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 56 | // Override precedence: |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 57 | // Settings.Global -> sys.fflag.override.* -> static list |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 58 | |
| 59 | // Step 1: check if feature flag is set in Settings.Global. |
| 60 | String value; |
| 61 | if (context != null) { |
| 62 | value = Settings.Global.getString(context.getContentResolver(), feature); |
| 63 | if (!TextUtils.isEmpty(value)) { |
| 64 | return Boolean.parseBoolean(value); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Step 2: check if feature flag has any override. Flag name: sys.fflag.override.<feature> |
| 69 | value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature); |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 70 | if (!TextUtils.isEmpty(value)) { |
| 71 | return Boolean.parseBoolean(value); |
| 72 | } |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 73 | // Step 3: check if feature flag has any default value. |
| 74 | value = getAllFeatureFlags().get(feature); |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 75 | return Boolean.parseBoolean(value); |
| 76 | } |
| 77 | |
| 78 | /** |
Fan Zhang | cfd0274 | 2017-10-03 14:37:44 -0700 | [diff] [blame] | 79 | * Override feature flag to new state. |
| 80 | */ |
Fan Zhang | aa1387c | 2017-11-17 11:07:29 -0800 | [diff] [blame] | 81 | public static void setEnabled(Context context, String feature, boolean enabled) { |
Fan Zhang | cfd0274 | 2017-10-03 14:37:44 -0700 | [diff] [blame] | 82 | SystemProperties.set(FFLAG_OVERRIDE_PREFIX + feature, enabled ? "true" : "false"); |
| 83 | } |
| 84 | |
| 85 | /** |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 86 | * Returns all feature flags in their raw form. |
| 87 | */ |
| 88 | public static Map<String, String> getAllFeatureFlags() { |
Fan Zhang | 3080636 | 2017-12-07 11:15:26 -0800 | [diff] [blame] | 89 | return DEFAULT_FLAGS; |
Fan Zhang | 690986d | 2017-08-17 11:22:31 -0700 | [diff] [blame] | 90 | } |
| 91 | } |