Only read the use_new_storage_value flag once per process.

Change-Id: Ib088fbcc2283718a309097c7c45fb01d5a983a25
Test: m
Bug: 312235596
diff --git a/tools/aconfig/aconfig/src/codegen/java.rs b/tools/aconfig/aconfig/src/codegen/java.rs
index a74ef85..dbc4ab5 100644
--- a/tools/aconfig/aconfig/src/codegen/java.rs
+++ b/tools/aconfig/aconfig/src/codegen/java.rs
@@ -698,6 +698,8 @@
         StorageInternalReader reader;
         boolean readFromNewStorage;
 
+        boolean useNewStorageValueAndDiscardOld = false;
+
         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";
@@ -713,6 +715,9 @@
                     reader = null;
                 }
             }
+
+            useNewStorageValueAndDiscardOld =
+                DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.use_new_storage_value", false);
         }
 
         private void load_overrides_aconfig_test() {
@@ -746,7 +751,7 @@
                         Log.i(TAG, String.format(MISMATCH_LOG, "disabledRw", val, disabledRw));
                     }
 
-                    if (DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.use_new_storage_value", false)) {
+                    if (useNewStorageValueAndDiscardOld) {
                         disabledRw = val;
                     }
 
@@ -757,7 +762,7 @@
                         Log.i(TAG, String.format(MISMATCH_LOG, "disabledRwExported", val, disabledRwExported));
                     }
 
-                    if (DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.use_new_storage_value", false)) {
+                    if (useNewStorageValueAndDiscardOld) {
                         disabledRwExported = val;
                     }
 
@@ -768,7 +773,7 @@
                         Log.i(TAG, String.format(MISMATCH_LOG, "enabledRw", val, enabledRw));
                     }
 
-                    if (DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.use_new_storage_value", false)) {
+                    if (useNewStorageValueAndDiscardOld) {
                         enabledRw = val;
                     }
 
@@ -805,7 +810,7 @@
                         Log.i(TAG, String.format(MISMATCH_LOG, "disabledRwInOtherNamespace", val, disabledRwInOtherNamespace));
                     }
 
-                    if (DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.use_new_storage_value", false)) {
+                    if (useNewStorageValueAndDiscardOld) {
                         disabledRwInOtherNamespace = val;
                     }
 
diff --git a/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template b/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template
index 96e7623..9970b1f 100644
--- a/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template
+++ b/tools/aconfig/aconfig/templates/FeatureFlagsImpl.java.template
@@ -35,6 +35,8 @@
     StorageInternalReader reader;
     boolean readFromNewStorage;
 
+    boolean useNewStorageValueAndDiscardOld = false;
+
     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";
@@ -50,6 +52,9 @@
                 reader = null;
             }
         }
+
+        useNewStorageValueAndDiscardOld =
+            DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.use_new_storage_value", false);
     }
 
 {{ -endif }}
@@ -91,7 +96,7 @@
                     Log.i(TAG, String.format(MISMATCH_LOG, "{flag.method_name}", val, {flag.method_name}));
                 }
 
-                if (DeviceConfig.getBoolean("core_experiments_team_internal", "com.android.providers.settings.use_new_storage_value", false)) \{
+                if (useNewStorageValueAndDiscardOld) \{
                     {flag.method_name} = val;
                 }