Merge "read new stoarge based on flag value" into main
diff --git a/tools/aconfig/aconfig/src/codegen/java.rs b/tools/aconfig/aconfig/src/codegen/java.rs
index 419cfa9..7238590 100644
--- a/tools/aconfig/aconfig/src/codegen/java.rs
+++ b/tools/aconfig/aconfig/src/codegen/java.rs
@@ -46,7 +46,6 @@
     let runtime_lookup_required =
         flag_elements.iter().any(|elem| elem.is_read_write) || library_exported;
     let container = (flag_elements.first().expect("zero template flags").container).to_string();
-
     let context = Context {
         flag_elements,
         namespace_flags,
@@ -693,7 +692,6 @@
             + r#"
             import android.aconfig.storage.StorageInternalReader;
             import android.util.Log;
-            import java.io.File;
             "#
             + expected_featureflagsmpl_content_1
             + r#"
@@ -701,10 +699,13 @@
         boolean readFromNewStorage;
 
         private final static String TAG = "AconfigJavaCodegen";
+        private final static String SUCCESS_LOG = "success: %s value matches";
+        private final static String MISMATCH_LOG = "error: %s value mismatch, new storage value is %s, old storage value is %s";
+        private final static String ERROR_LOG = "error: failed to read flag value";
 
-        public FeatureFlagsImpl() {
-            File file = new File("/metadata/aconfig_test_missions/mission_1");
-            if (file.exists()) {
+        private void init() {
+            if (reader != null) return;
+            if (DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.storage_test_mission_1", false)) {
                 readFromNewStorage = true;
                 try {
                     reader = new StorageInternalReader("system", "com.android.aconfig.test");
@@ -716,56 +717,13 @@
 
         private void load_overrides_aconfig_test() {
             try {
-                boolean val;
                 Properties properties = DeviceConfig.getProperties("aconfig_test");
                 disabledRw =
                     properties.getBoolean(Flags.FLAG_DISABLED_RW, false);
-                if (readFromNewStorage && reader != null) {
-                    try {
-                        val = reader.getBooleanFlagValue(1);
-                        if (val == disabledRw) {
-                            Log.i(TAG, "success: disabledRw value matches");
-                        } else {
-                            Log.i(TAG, String.format(
-                                "error: disabledRw value mismatch, new storage value is %s, old storage value is %s",
-                                val, disabledRw));
-                        }
-                    } catch (Exception e) {
-                        Log.e(TAG, "error: failed to read flag value of disabledRw", e);
-                    }
-                }
                 disabledRwExported =
                     properties.getBoolean(Flags.FLAG_DISABLED_RW_EXPORTED, false);
-                if (readFromNewStorage && reader != null) {
-                    try {
-                        val = reader.getBooleanFlagValue(2);
-                        if (val == disabledRwExported) {
-                            Log.i(TAG, "success: disabledRwExported value matches");
-                        } else {
-                            Log.i(TAG, String.format(
-                                "error: disabledRwExported value mismatch, new storage value is %s, old storage value is %s",
-                                val, disabledRwExported));
-                        }
-                    } catch (Exception e) {
-                        Log.e(TAG, "error: failed to read flag value of disabledRwExported", e);
-                    }
-                }
                 enabledRw =
                     properties.getBoolean(Flags.FLAG_ENABLED_RW, true);
-                if (readFromNewStorage && reader != null) {
-                    try {
-                        val = reader.getBooleanFlagValue(8);
-                        if (val == enabledRw) {
-                            Log.i(TAG, "success: enabledRw value matches");
-                        } else {
-                            Log.i(TAG, String.format(
-                                "error: enabledRw value mismatch, new storage value is %s, old storage value is %s",
-                                val, enabledRw));
-                        }
-                    } catch (Exception e) {
-                        Log.e(TAG, "error: failed to read flag value of enabledRw", e);
-                    }
-                }
             } catch (NullPointerException e) {
                 throw new RuntimeException(
                     "Cannot read value from namespace aconfig_test "
@@ -777,28 +735,39 @@
                 );
             }
             aconfig_test_is_cached = true;
+            init();
+            if (readFromNewStorage && reader != null) {
+                boolean val;
+                try {
+                    val = reader.getBooleanFlagValue(1);
+                    if (val == disabledRw) {
+                        Log.i(TAG, String.format(SUCCESS_LOG, "disabledRw"));
+                    } else {
+                        Log.i(TAG, String.format(MISMATCH_LOG, "disabledRw", val, disabledRw));
+                    }
+                    val = reader.getBooleanFlagValue(2);
+                    if (val == disabledRwExported) {
+                        Log.i(TAG, String.format(SUCCESS_LOG, "disabledRwExported"));
+                    } else {
+                        Log.i(TAG, String.format(MISMATCH_LOG, "disabledRwExported", val, disabledRwExported));
+                    }
+                    val = reader.getBooleanFlagValue(8);
+                    if (val == enabledRw) {
+                        Log.i(TAG, String.format(SUCCESS_LOG, "enabledRw"));
+                    } else {
+                        Log.i(TAG, String.format(MISMATCH_LOG, "enabledRw", val, enabledRw));
+                    }
+                } catch (Exception e) {
+                    Log.e(TAG, ERROR_LOG, e);
+                }
+            }
         }
 
         private void load_overrides_other_namespace() {
             try {
-                boolean val;
                 Properties properties = DeviceConfig.getProperties("other_namespace");
                 disabledRwInOtherNamespace =
                     properties.getBoolean(Flags.FLAG_DISABLED_RW_IN_OTHER_NAMESPACE, false);
-                if (readFromNewStorage && reader != null) {
-                    try {
-                        val = reader.getBooleanFlagValue(3);
-                        if (val == disabledRwInOtherNamespace) {
-                            Log.i(TAG, "success: disabledRwInOtherNamespace value matches");
-                        } else {
-                            Log.i(TAG, String.format(
-                                "error: disabledRwInOtherNamespace value mismatch, new storage value is %s, old storage value is %s",
-                                val, disabledRwInOtherNamespace));
-                        }
-                    } catch (Exception e) {
-                        Log.e(TAG, "error: failed to read flag value of disabledRwInOtherNamespace", e);
-                    }
-                }
             } catch (NullPointerException e) {
                 throw new RuntimeException(
                     "Cannot read value from namespace other_namespace "
@@ -810,6 +779,20 @@
                 );
             }
             other_namespace_is_cached = true;
+            init();
+            if (readFromNewStorage && reader != null) {
+                boolean val;
+                try {
+                    val = reader.getBooleanFlagValue(3);
+                    if (val == disabledRwInOtherNamespace) {
+                        Log.i(TAG, String.format(SUCCESS_LOG, "disabledRwInOtherNamespace"));
+                    } else {
+                        Log.i(TAG, String.format(MISMATCH_LOG, "disabledRwInOtherNamespace", val, disabledRwInOtherNamespace));
+                    }
+                } catch (Exception e) {
+                    Log.e(TAG, ERROR_LOG, e);
+                }
+            }
         }"# + expected_featureflagsmpl_content_2;
 
         let mut file_set = HashMap::from([
diff --git a/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template b/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template
index e3b5d8e..d93d302 100644
--- a/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template
+++ b/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template
@@ -14,8 +14,6 @@
 {{ -if allow_instrumentation }}
 import android.aconfig.storage.StorageInternalReader;
 import android.util.Log;
-
-import java.io.File;
 {{ -endif }}
 {{ -endif }}
 
@@ -38,10 +36,13 @@
     boolean readFromNewStorage;
 
     private final static String TAG = "AconfigJavaCodegen";
+    private final static String SUCCESS_LOG = "success: %s value matches";
+    private final static String MISMATCH_LOG = "error: %s value mismatch, new storage value is %s, old storage value is %s";
+    private final static String ERROR_LOG = "error: failed to read flag value";
 
-    public FeatureFlagsImpl() \{
-        File file = new File("/metadata/aconfig_test_missions/mission_1");
-        if (file.exists()) \{
+    private void init() \{
+        if (reader != null) return;
+        if (DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.storage_test_mission_1", false)) \{
             readFromNewStorage = true;
             try \{
                 reader = new StorageInternalReader("{container}", "{package_name}");
@@ -50,39 +51,17 @@
             }
         }
     }
+
 {{ -endif }}
 {{ -endif }}
 {{ for namespace_with_flags in namespace_flags }}
     private void load_overrides_{namespace_with_flags.namespace}() \{
         try \{
-{{ -if not library_exported }}
-{{ -if allow_instrumentation }}
-            boolean val;
-{{ -endif }}
-{{ -endif }}
             Properties properties = DeviceConfig.getProperties("{namespace_with_flags.namespace}");
 {{ -for flag in namespace_with_flags.flags }}
 {{ -if flag.is_read_write }}
             {flag.method_name} =
                 properties.getBoolean(Flags.FLAG_{flag.flag_name_constant_suffix}, {flag.default_value});
-{{ -if not library_exported }}
-{{ -if allow_instrumentation }}
-            if (readFromNewStorage && reader != null) \{
-                try \{
-                    val = reader.getBooleanFlagValue({flag.flag_offset});
-                    if (val == {flag.method_name}) \{
-                        Log.i(TAG, "success: {flag.method_name} value matches");
-                    } else \{
-                        Log.i(TAG, String.format(
-                            "error: {flag.method_name} value mismatch, new storage value is %s, old storage value is %s",
-                            val, {flag.method_name}));
-                    }
-                } catch (Exception e) \{
-                    Log.e(TAG, "error: failed to read flag value of {flag.method_name}", e);
-                }
-            }
-{{ -endif }}
-{{ -endif }}
 {{ -endif }}
 {{ -endfor }}
         } catch (NullPointerException e) \{
@@ -96,6 +75,29 @@
             );
         }
         {namespace_with_flags.namespace}_is_cached = true;
+{{ -if not library_exported }}
+{{ -if allow_instrumentation }}
+        init();
+        if (readFromNewStorage && reader != null) \{
+            boolean val;
+            try \{
+{{ -for flag in namespace_with_flags.flags }}
+{{ -if flag.is_read_write }}
+
+                val = reader.getBooleanFlagValue({flag.flag_offset});
+                if (val == {flag.method_name}) \{
+                    Log.i(TAG, String.format(SUCCESS_LOG, "{flag.method_name}"));
+                } else \{
+                    Log.i(TAG, String.format(MISMATCH_LOG, "{flag.method_name}", val, {flag.method_name}));
+                }
+{{ -endif }}
+{{ -endfor }}
+            } catch (Exception e) \{
+                    Log.e(TAG, ERROR_LOG, e);
+            }
+        }
+{{ -endif }}
+{{ -endif }}
     }
 {{ endfor- }}
 {{ -endif }}{#- end of runtime_lookup_required #}