Merge "Add method to read trunk stable flag" into main

This commit is contained in:
Treehugger Robot
2023-10-02 13:17:53 +00:00
committed by Gerrit Code Review
2 changed files with 55 additions and 0 deletions

View File

@@ -64,6 +64,9 @@ public final class DeviceConfigUtils {
@VisibleForTesting @VisibleForTesting
public static final long DEFAULT_PACKAGE_VERSION = 1000; public static final long DEFAULT_PACKAGE_VERSION = 1000;
private static final String CORE_NETWORKING_TRUNK_STABLE_NAMESPACE = "android_core_networking";
private static final String CORE_NETWORKING_TRUNK_STABLE_FLAG_PACKAGE = "com.android.net.flags";
@VisibleForTesting @VisibleForTesting
public static void resetPackageVersionCacheForTest() { public static void resetPackageVersionCacheForTest() {
sPackageVersion = -1; sPackageVersion = -1;
@@ -420,4 +423,31 @@ public final class DeviceConfigUtils {
return pkgs.get(0).activityInfo.applicationInfo.packageName; return pkgs.get(0).activityInfo.applicationInfo.packageName;
} }
/**
* Check whether one specific trunk stable flag in android_core_networking namespace is enabled.
* This method reads trunk stable feature flag value from DeviceConfig directly since
* java_aconfig_library soong module is not available in the mainline branch.
* After the mainline branch support the aconfig soong module, this function must be removed and
* java_aconfig_library must be used instead to check if the feature is enabled.
*
* @param flagName The name of the trunk stable flag
* @return true if this feature is enabled, or false if disabled.
*/
public static boolean isTrunkStableFeatureEnabled(final String flagName) {
return isTrunkStableFeatureEnabled(
CORE_NETWORKING_TRUNK_STABLE_NAMESPACE,
CORE_NETWORKING_TRUNK_STABLE_FLAG_PACKAGE,
flagName
);
}
private static boolean isTrunkStableFeatureEnabled(final String namespace,
final String packageName, final String flagName) {
return DeviceConfig.getBoolean(
namespace,
packageName + "." + flagName,
false /* defaultValue */
);
}
} }

View File

@@ -71,6 +71,10 @@ import java.util.Arrays;
public class DeviceConfigUtilsTest { public class DeviceConfigUtilsTest {
private static final String TEST_NAME_SPACE = "connectivity"; private static final String TEST_NAME_SPACE = "connectivity";
private static final String TEST_EXPERIMENT_FLAG = "experiment_flag"; private static final String TEST_EXPERIMENT_FLAG = "experiment_flag";
private static final String CORE_NETWORKING_TRUNK_STABLE_NAMESPACE = "android_core_networking";
private static final String TEST_TRUNK_STABLE_FLAG = "trunk_stable_feature";
private static final String TEST_CORE_NETWORKING_TRUNK_STABLE_FLAG_PROPERTY =
"com.android.net.flags.trunk_stable_feature";
private static final int TEST_FLAG_VALUE = 28; private static final int TEST_FLAG_VALUE = 28;
private static final String TEST_FLAG_VALUE_STRING = "28"; private static final String TEST_FLAG_VALUE_STRING = "28";
private static final int TEST_DEFAULT_FLAG_VALUE = 0; private static final int TEST_DEFAULT_FLAG_VALUE = 0;
@@ -506,4 +510,25 @@ public class DeviceConfigUtilsTest {
verify(mContext, never()).getPackageName(); verify(mContext, never()).getPackageName();
verify(mPm, never()).getPackageInfo(anyString(), anyInt()); verify(mPm, never()).getPackageInfo(anyString(), anyInt());
} }
@Test
public void testIsCoreNetworkingTrunkStableFeatureEnabled() {
doReturn(null).when(() -> DeviceConfig.getProperty(
CORE_NETWORKING_TRUNK_STABLE_NAMESPACE,
TEST_CORE_NETWORKING_TRUNK_STABLE_FLAG_PROPERTY));
assertFalse(DeviceConfigUtils.isTrunkStableFeatureEnabled(
TEST_TRUNK_STABLE_FLAG));
doReturn("false").when(() -> DeviceConfig.getProperty(
CORE_NETWORKING_TRUNK_STABLE_NAMESPACE,
TEST_CORE_NETWORKING_TRUNK_STABLE_FLAG_PROPERTY));
assertFalse(DeviceConfigUtils.isTrunkStableFeatureEnabled(
TEST_TRUNK_STABLE_FLAG));
doReturn("true").when(() -> DeviceConfig.getProperty(
CORE_NETWORKING_TRUNK_STABLE_NAMESPACE,
TEST_CORE_NETWORKING_TRUNK_STABLE_FLAG_PROPERTY));
assertTrue(DeviceConfigUtils.isTrunkStableFeatureEnabled(
TEST_TRUNK_STABLE_FLAG));
}
} }