diff options
| author | 2024-11-20 03:11:33 +0000 | |
|---|---|---|
| committer | 2024-11-20 03:11:33 +0000 | |
| commit | e7784d1336364996a8caa280f05b88f70c1a5521 (patch) | |
| tree | aa688c5d83711c0ca6ad4a95fc7c5343fae22f9a | |
| parent | 46033aa5e7982e536b81ddadeeead7afe28a0696 (diff) | |
| parent | d02a9cf4d2c0033885c132b05b02f9b674b66699 (diff) | |
Merge "Revert "remove file check"" into main
| -rw-r--r-- | tools/aconfig/aconfig/src/codegen/java.rs | 97 | ||||
| -rw-r--r-- | tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template | 80 |
2 files changed, 136 insertions, 41 deletions
diff --git a/tools/aconfig/aconfig/src/codegen/java.rs b/tools/aconfig/aconfig/src/codegen/java.rs index e2bb1adbde..bfdf1a73c2 100644 --- a/tools/aconfig/aconfig/src/codegen/java.rs +++ b/tools/aconfig/aconfig/src/codegen/java.rs @@ -513,11 +513,19 @@ mod tests { package com.android.aconfig.test; // TODO(b/303773055): Remove the annotation after access issue is resolved. import android.compat.annotation.UnsupportedAppUsage; + import android.os.Binder; + import android.provider.DeviceConfig; + import android.provider.DeviceConfig.Properties; import android.aconfig.storage.StorageInternalReader; + import java.nio.file.Files; + import java.nio.file.Paths; /** @hide */ public final class FeatureFlagsImpl implements FeatureFlags { + private static final boolean isReadFromNew = Files.exists(Paths.get("/metadata/aconfig/boot/enable_only_new_storage")); private static volatile boolean isCached = false; + private static volatile boolean aconfig_test_is_cached = false; + private static volatile boolean other_namespace_is_cached = false; private static boolean disabledRw = false; private static boolean disabledRwExported = false; private static boolean disabledRwInOtherNamespace = false; @@ -536,6 +544,55 @@ mod tests { disabledRwInOtherNamespace = foundPackage ? reader.getBooleanFlagValue(3) : false; isCached = true; } + private void load_overrides_aconfig_test() { + final long ident = Binder.clearCallingIdentity(); + try { + Properties properties = DeviceConfig.getProperties("aconfig_test"); + disabledRw = + properties.getBoolean(Flags.FLAG_DISABLED_RW, false); + disabledRwExported = + properties.getBoolean(Flags.FLAG_DISABLED_RW_EXPORTED, false); + enabledRw = + properties.getBoolean(Flags.FLAG_ENABLED_RW, true); + } catch (NullPointerException e) { + throw new RuntimeException( + "Cannot read value from namespace aconfig_test " + + "from DeviceConfig. It could be that the code using flag " + + "executed before SettingsProvider initialization. Please use " + + "fixed read-only flag by adding is_fixed_read_only: true in " + + "flag declaration.", + e + ); + } catch (SecurityException e) { + // for isolated process case, skip loading flag value from the storage, use the default + } finally { + Binder.restoreCallingIdentity(ident); + } + aconfig_test_is_cached = true; + } + + private void load_overrides_other_namespace() { + final long ident = Binder.clearCallingIdentity(); + try { + Properties properties = DeviceConfig.getProperties("other_namespace"); + disabledRwInOtherNamespace = + properties.getBoolean(Flags.FLAG_DISABLED_RW_IN_OTHER_NAMESPACE, false); + } catch (NullPointerException e) { + throw new RuntimeException( + "Cannot read value from namespace other_namespace " + + "from DeviceConfig. It could be that the code using flag " + + "executed before SettingsProvider initialization. Please use " + + "fixed read-only flag by adding is_fixed_read_only: true in " + + "flag declaration.", + e + ); + } catch (SecurityException e) { + // for isolated process case, skip loading flag value from the storage, use the default + } finally { + Binder.restoreCallingIdentity(ident); + } + other_namespace_is_cached = true; + } @Override @com.android.aconfig.annotations.AconfigFlagAccessor @@ -547,8 +604,14 @@ mod tests { @com.android.aconfig.annotations.AconfigFlagAccessor @UnsupportedAppUsage public boolean disabledRw() { - if (!isCached) { - init(); + if (isReadFromNew) { + if (!isCached) { + init(); + } + } else { + if (!aconfig_test_is_cached) { + load_overrides_aconfig_test(); + } } return disabledRw; } @@ -556,8 +619,14 @@ mod tests { @com.android.aconfig.annotations.AconfigFlagAccessor @UnsupportedAppUsage public boolean disabledRwExported() { - if (!isCached) { - init(); + if (isReadFromNew) { + if (!isCached) { + init(); + } + } else { + if (!aconfig_test_is_cached) { + load_overrides_aconfig_test(); + } } return disabledRwExported; } @@ -565,8 +634,14 @@ mod tests { @com.android.aconfig.annotations.AconfigFlagAccessor @UnsupportedAppUsage public boolean disabledRwInOtherNamespace() { - if (!isCached) { - init(); + if (isReadFromNew) { + if (!isCached) { + init(); + } + } else { + if (!other_namespace_is_cached) { + load_overrides_other_namespace(); + } } return disabledRwInOtherNamespace; } @@ -598,8 +673,14 @@ mod tests { @com.android.aconfig.annotations.AconfigFlagAccessor @UnsupportedAppUsage public boolean enabledRw() { - if (!isCached) { - init(); + if (isReadFromNew) { + if (!isCached) { + init(); + } + } else { + if (!aconfig_test_is_cached) { + load_overrides_aconfig_test(); + } } return enabledRw; } diff --git a/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template b/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template index d782f811e7..cb52150f02 100644 --- a/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template +++ b/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template @@ -1,22 +1,41 @@ package {package_name}; {{ -if not is_test_mode }} {{ -if allow_instrumentation }} -{{ if not library_exported- }}{#- only new storage for prod mode #} +{{ if not library_exported- }} // TODO(b/303773055): Remove the annotation after access issue is resolved. import android.compat.annotation.UnsupportedAppUsage; +{{ -endif }} + {{ -if runtime_lookup_required }} +import android.os.Binder; +import android.provider.DeviceConfig; +import android.provider.DeviceConfig.Properties; + +{{ -if not library_exported }} import android.aconfig.storage.StorageInternalReader; +import java.nio.file.Files; +import java.nio.file.Paths; +{{ -endif }} + {{ -endif }} /** @hide */ public final class FeatureFlagsImpl implements FeatureFlags \{ {{ -if runtime_lookup_required }} +{{ -if not library_exported }} + private static final boolean isReadFromNew = Files.exists(Paths.get("/metadata/aconfig/boot/enable_only_new_storage")); private static volatile boolean isCached = false; +{{ -endif }} +{{ -for namespace_with_flags in namespace_flags }} + private static volatile boolean {namespace_with_flags.namespace}_is_cached = false; +{{ -endfor- }} + {{ for flag in flag_elements }} {{ -if flag.is_read_write }} private static boolean {flag.method_name} = {flag.default_value}; {{ -endif }} {{ -endfor }} +{{ if not library_exported }} private void init() \{ StorageInternalReader reader = null; boolean foundPackage = true; @@ -34,37 +53,9 @@ public final class FeatureFlagsImpl implements FeatureFlags \{ {{ -endfor }} isCached = true; } -{{ -endif }}{#- end of runtime_lookup_required #} -{{ -for flag in flag_elements }} - @Override - @com.android.aconfig.annotations.AconfigFlagAccessor - @UnsupportedAppUsage - public boolean {flag.method_name}() \{ -{{ -if flag.is_read_write }} - if (!isCached) \{ - init(); - } - return {flag.method_name}; -{{ -else }} - return {flag.default_value}; -{{ -endif }} - } -{{ endfor }} -} -{{ -else- }}{#- device config for exproted mode #} -import android.os.Binder; -import android.provider.DeviceConfig; -import android.provider.DeviceConfig.Properties; -/** @hide */ -public final class FeatureFlagsImpl implements FeatureFlags \{ -{{ -for namespace_with_flags in namespace_flags }} - private static volatile boolean {namespace_with_flags.namespace}_is_cached = false; -{{ -endfor- }} -{{ for flag in flag_elements }} -{{ -if flag.is_read_write }} - private static boolean {flag.method_name} = {flag.default_value}; -{{ -endif }} -{{ -endfor }} +{{ endif }} + + {{ for namespace_with_flags in namespace_flags }} private void load_overrides_{namespace_with_flags.namespace}() \{ final long ident = Binder.clearCallingIdentity(); @@ -93,17 +84,40 @@ public final class FeatureFlagsImpl implements FeatureFlags \{ {namespace_with_flags.namespace}_is_cached = true; } {{ endfor- }} + +{{ -endif }}{#- end of runtime_lookup_required #} {{ -for flag in flag_elements }} @Override +{{ -if not library_exported }} + @com.android.aconfig.annotations.AconfigFlagAccessor + @UnsupportedAppUsage +{{ -endif }} public boolean {flag.method_name}() \{ +{{ -if not library_exported }} +{{ -if flag.is_read_write }} + if (isReadFromNew) \{ + if (!isCached) \{ + init(); + } + } else \{ + if (!{flag.device_config_namespace}_is_cached) \{ + load_overrides_{flag.device_config_namespace}(); + } + } + return {flag.method_name}; +{{ -else }} + return {flag.default_value}; +{{ -endif }} +{{ else }} if (!{flag.device_config_namespace}_is_cached) \{ load_overrides_{flag.device_config_namespace}(); } return {flag.method_name}; +{{ -endif }} } {{ endfor }} } -{{ -endif- }} {#- end exported mode #} + {{ else }} {#- else for allow_instrumentation is not enabled #} {{ if not library_exported- }} // TODO(b/303773055): Remove the annotation after access issue is resolved. |