Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7a48926e authored by Marybeth Fair's avatar Marybeth Fair
Browse files

aconfig: add full sdk check

Update sdk check to support minor versions (using SDK_INT_FULL).

Tried a couple of different ways to represent the two types of API
levels - wanted to do an enum with values, but that wasn't parsing
into-json / from-json correctly. I debated just using u32 and then
checking if the value was > 100_000, but thought that was a bit hacky.

I went with this approach because I think it's clearer to have the
logic of finalized file -> SDK check all in the same place (here), and
then there's less logic in the template, we just place this check in
directly. Now if we have to add another check type, we don't have to
update the template.

All of the changes to the SDK_INT_FULL are behind the build flag. There
are some changes here which update the regular SDK_INT check, but they
should be very small code updates with no logic changes (as shown by the
tests) - the only actual change is to CustomFeatureFlags which is used
in testing.

Flag: Build.RELEASE_ACONFIG_SUPPORT_MINOR_SDK
Test: atest convert_finalized_flags.test
Change-Id: I05ec21aaf032eaebfbe651e4c0c2a834e3f19e18
parent d19d3e3f
Loading
Loading
Loading
Loading
+19 −19
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ use crate::codegen;
use crate::codegen::CodegenMode;
use crate::commands::{should_include_flag, OutputFile};
use aconfig_protos::{ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag};
use convert_finalized_flags::{FinalizedFlag, FinalizedFlagMap};
use convert_finalized_flags::{ApiLevel, FinalizedFlag, FinalizedFlagMap};
use std::collections::HashMap;

// Arguments to configure codegen for generate_java_code.
@@ -169,7 +169,7 @@ struct FlagElement {
    pub method_name: String,
    pub properties: String,
    pub finalized_sdk_present: bool,
    pub finalized_sdk_value: i32,
    pub finalized_sdk_check: String,
}

fn create_flag_element(
@@ -199,16 +199,16 @@ fn create_flag_element(
    };

    // An empty map is provided if check_api_level is disabled.
    let mut finalized_sdk_present: bool = false;
    let mut finalized_sdk_value: i32 = 0;
    if !finalized_flags.is_empty() {
    let (finalized_sdk_present, finalized_sdk_value) = if !finalized_flags.is_empty() {
        let finalized_sdk = finalized_flags.get_finalized_level(&FinalizedFlag {
            flag_name: pf.name().to_string(),
            package_name: package.to_string(),
        });
        finalized_sdk_present = finalized_sdk.is_some();
        finalized_sdk_value = finalized_sdk.map(|f| f.0).unwrap_or_default();
    }
        (finalized_sdk.is_some(), finalized_sdk.unwrap_or(ApiLevel(0)))
    } else {
        (false, ApiLevel(0))
    };
    let finalized_sdk_check = finalized_sdk_value.conditional();

    FlagElement {
        container: pf.container().to_string(),
@@ -222,7 +222,7 @@ fn create_flag_element(
        method_name: format_java_method_name(pf.name()),
        properties: format_property_name(pf.namespace()),
        finalized_sdk_present,
        finalized_sdk_value,
        finalized_sdk_check,
    }
}

@@ -903,9 +903,9 @@ mod tests {
                )
            );

            private Map<String, Integer> mFinalizedFlags = new HashMap<>(
            private Map<String, Boolean> mFinalizedFlags = new HashMap<>(
                Map.ofEntries(
                    Map.entry("", Integer.MAX_VALUE)
                    Map.entry("", false)
                )
            );

@@ -913,7 +913,7 @@ mod tests {
                if (!mFinalizedFlags.containsKey(flagName)) {
                    return false;
                }
                return Build.VERSION.SDK_INT >= mFinalizedFlags.get(flagName);
                return mFinalizedFlags.get(flagName);
            }
        }
    "#;
@@ -960,7 +960,7 @@ mod tests {
            assign_flag_ids(crate::test::TEST_PACKAGE, modified_parsed_flags.iter()).unwrap();
        let mut finalized_flags = FinalizedFlagMap::new();
        finalized_flags.insert_if_new(
            ApiLevel(36),
            ApiLevel::from_sdk_int(36),
            FinalizedFlag {
                flag_name: "disabled_rw_exported".to_string(),
                package_name: "com.android.aconfig.test".to_string(),
@@ -1124,10 +1124,10 @@ mod tests {
                )
            );

            private Map<String, Integer> mFinalizedFlags = new HashMap<>(
            private Map<String, Boolean> mFinalizedFlags = new HashMap<>(
                Map.ofEntries(
                    Map.entry(Flags.FLAG_DISABLED_RW_EXPORTED, 36),
                    Map.entry("", Integer.MAX_VALUE)
                    Map.entry(Flags.FLAG_DISABLED_RW_EXPORTED, Build.VERSION.SDK_INT >= 36 ? true : false),
                    Map.entry("", false)
                )
            );

@@ -1135,7 +1135,7 @@ mod tests {
                if (!mFinalizedFlags.containsKey(flagName)) {
                    return false;
                }
                return Build.VERSION.SDK_INT >= mFinalizedFlags.get(flagName);
                return mFinalizedFlags.get(flagName);
            }
        }
    "#;
@@ -1184,7 +1184,7 @@ mod tests {
            assign_flag_ids(crate::test::TEST_PACKAGE, modified_parsed_flags.iter()).unwrap();
        let mut finalized_flags = FinalizedFlagMap::new();
        finalized_flags.insert_if_new(
            ApiLevel(36),
            ApiLevel::from_sdk_int(36),
            FinalizedFlag {
                flag_name: "disabled_rw".to_string(),
                package_name: "com.android.aconfig.test".to_string(),
@@ -1640,7 +1640,7 @@ mod tests {
            assign_flag_ids(crate::test::TEST_PACKAGE, modified_parsed_flags.iter()).unwrap();
        let mut finalized_flags = FinalizedFlagMap::new();
        finalized_flags.insert_if_new(
            ApiLevel(36),
            ApiLevel::from_sdk_int(36),
            FinalizedFlag {
                flag_name: "disabled_rw_exported".to_string(),
                package_name: "com.android.aconfig.test".to_string(),
+4 −4
Original line number Diff line number Diff line
@@ -82,14 +82,14 @@ public class CustomFeatureFlags implements FeatureFlags \{
    );

{{ -if library_exported }}
    private Map<String, Integer> mFinalizedFlags = new HashMap<>(
    private Map<String, Boolean> mFinalizedFlags = new HashMap<>(
        Map.ofEntries(
            {{ -for item in flag_elements }}
            {{ -if item.finalized_sdk_present }}
            Map.entry(Flags.FLAG_{item.flag_name_constant_suffix}, {item.finalized_sdk_value}),
            Map.entry(Flags.FLAG_{item.flag_name_constant_suffix}, {item.finalized_sdk_check|unescaped} ? true : false),
            {{ -endif }}
            {{ -endfor }}
            Map.entry("", Integer.MAX_VALUE){# The empty entry to avoid empty entries #}
            Map.entry("", false){# The empty entry to avoid empty entries #}
        )
    );

@@ -97,7 +97,7 @@ public class CustomFeatureFlags implements FeatureFlags \{
        if (!mFinalizedFlags.containsKey(flagName)) \{
            return false;
        }
        return Build.VERSION.SDK_INT >= mFinalizedFlags.get(flagName);
        return mFinalizedFlags.get(flagName);
    }
{{ -endif }}
}
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ public final class ExportedFlags \{
{{ -for flag in flag_elements }}
    public static boolean {flag.method_name}() \{
        {{ -if flag.finalized_sdk_present }}
        if (Build.VERSION.SDK_INT >= {flag.finalized_sdk_value}) \{
        if ({flag.finalized_sdk_check|unescaped}) \{
          return true;
        }
        {{ -endif}}  {#- end finalized_sdk_present#}
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ public final class FeatureFlagsImpl implements FeatureFlags \{
            {{ -for namespace_with_flags in namespace_flags }}
            {{ -for flag in namespace_with_flags.flags }}
            {{ -if flag.finalized_sdk_present }}
            {flag.method_name} = Build.VERSION.SDK_INT >= {flag.finalized_sdk_value} ? true : reader.getBooleanFlagValue("{flag.flag_name}", {flag.default_value});
            {flag.method_name} = {flag.finalized_sdk_check|unescaped} ? true : reader.getBooleanFlagValue("{flag.flag_name}", {flag.default_value});
            {{ - else }} {#- else finalized_sdk_present #}
            {flag.method_name} = reader.getBooleanFlagValue("{flag.flag_name}", {flag.default_value});
            {{ -endif}}  {#- end finalized_sdk_present#}
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ public final class Flags \{
    public static boolean {item.method_name}() \{
        {{ if library_exported- }}
        {{ -if item.finalized_sdk_present }}
        if (Build.VERSION.SDK_INT >= {item.finalized_sdk_value}) \{
        if ({item.finalized_sdk_check|unescaped}) \{
          return true;
        }
        {{ -endif}}  {#- end finalized_sdk_present#}
Loading