1 /* 2 * Copyright (C) 2024 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 com.android.compatibility.common.deviceinfo; 18 19 import android.os.Build; 20 import android.os.flagging.AconfigPackage; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.annotation.RequiresApi; 25 26 import com.android.compatibility.common.util.SystemUtil; 27 28 /** 29 * Utility methods for collecting feature flag information. 30 */ 31 public final class FeatureFlagUtils { 32 FeatureFlagUtils()33 private FeatureFlagUtils() {} 34 35 /** 36 * Check if a feature flag is enabled. 37 * 38 * @param packageName the name of the aconfig package 39 * @param flagName the name of the feature flag 40 * 41 * @return whether the feature flag is enabled, or {@code null} if unknown 42 */ 43 @Nullable 44 @RequiresApi(Build.VERSION_CODES.BAKLAVA) isFeatureFlagEnabled(@onNull String packageName, @NonNull String flagName)45 public static Boolean isFeatureFlagEnabled(@NonNull String packageName, 46 @NonNull String flagName) { 47 AconfigPackage aconfigPackage = AconfigPackage.load(packageName); 48 boolean defaultTrueValue = aconfigPackage.getBooleanFlagValue(flagName, true); 49 boolean defaultFalseValue = aconfigPackage.getBooleanFlagValue(flagName, false); 50 if (defaultTrueValue != defaultFalseValue) { 51 return null; 52 } 53 return defaultFalseValue; 54 } 55 56 /** 57 * Check if a feature flag is enabled on V, before we had the {@link AconfigPackage} API. 58 * 59 * @param featureFlagName the name of the feature flag 60 * @param mountPoint the mount point to check (e.g. {@code "system"}), or 61 * {@code "device_config"} for device config 62 * 63 * @return whether the feature flag is enabled, or {@code null} if unknown 64 */ 65 @Nullable isFeatureFlagEnabledOnV(@onNull String featureFlagName, @NonNull String mountPoint)66 public static Boolean isFeatureFlagEnabledOnV(@NonNull String featureFlagName, 67 @NonNull String mountPoint) { 68 final String[] lines = SystemUtil.runShellCommand("printflags").trim().split("\n"); 69 String featureFlag = ""; 70 for (String line : lines) { 71 if (line.contains(featureFlagName)) { 72 featureFlag = line; 73 break; 74 } 75 } 76 if (featureFlag.contains("ENABLED (" + mountPoint + ")")) { 77 return true; 78 } else if (featureFlag.contains("DISABLED (" + mountPoint + ")")) { 79 return false; 80 } else { 81 return null; 82 } 83 } 84 } 85