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

Commit 9f726bec authored by Zhi Dou's avatar Zhi Dou Committed by Automerger Merge Worker
Browse files

Merge "update java local flag read code" into main am: 5878efc6 am: 30e1a30d

parents 2ab11224 30e1a30d
Loading
Loading
Loading
Loading
+32 −8
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ pub fn generate_java_code<I>(
    codegen_mode: CodegenMode,
    flag_ids: HashMap<String, u16>,
    allow_instrumentation: bool,
    package_fingerprint: u64,
) -> Result<Vec<OutputFile>>
where
    I: Iterator<Item = ProtoParsedFlag>,
@@ -46,6 +47,7 @@ where
    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 is_platform_container = matches!(container.as_str(), "system" | "product" | "vendor");
    let context = Context {
        flag_elements,
        namespace_flags,
@@ -56,6 +58,8 @@ where
        library_exported,
        allow_instrumentation,
        container,
        is_platform_container,
        package_fingerprint: format!("0x{:X}L", package_fingerprint),
    };
    let mut template = TinyTemplate::new();
    template.add_template("Flags.java", include_str!("../../templates/Flags.java.template"))?;
@@ -123,6 +127,8 @@ struct Context {
    pub library_exported: bool,
    pub allow_instrumentation: bool,
    pub container: String,
    pub is_platform_container: bool,
    pub package_fingerprint: String,
}

#[derive(Serialize, Debug)]
@@ -502,6 +508,7 @@ mod tests {
            mode,
            flag_ids,
            true,
            5801144784618221668,
        )
        .unwrap();
        let expect_flags_content = EXPECTED_FLAG_COMMON_CONTENT.to_string()
@@ -513,10 +520,13 @@ 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.aconfig.storage.StorageInternalReader;

        import android.os.Build;
        import android.os.flagging.PlatformAconfigPackageInternal;
        import android.os.flagging.AconfigStorageReadException;
        import android.util.Log;
        /** @hide */
        public final class FeatureFlagsImpl implements FeatureFlags {
            private static final String TAG = "com.android.aconfig.test.FeatureFlagsImpl";
            private static volatile boolean isCached = false;
            private static boolean disabledRw = false;
            private static boolean disabledRwExported = false;
@@ -524,16 +534,27 @@ mod tests {
            private static boolean enabledRw = true;
            private void init() {
                try {
                    StorageInternalReader reader = new StorageInternalReader("system", "com.android.aconfig.test");
                    PlatformAconfigPackageInternal reader = PlatformAconfigPackageInternal.load("system", "com.android.aconfig.test", 0x5081CE7221C77064L);
                    AconfigStorageReadException error = reader.getException();
                    if (error == null) {
                        disabledRw = reader.getBooleanFlagValue(1);
                        disabledRwExported = reader.getBooleanFlagValue(2);
                        enabledRw = reader.getBooleanFlagValue(8);
                        disabledRwInOtherNamespace = reader.getBooleanFlagValue(3);
                    } else if (Build.VERSION.SDK_INT > 35 && error.getErrorCode() == 5 /* fingerprint doesn't match*/) {
                        disabledRw = reader.getBooleanFlagValue("disabled_rw", false);
                        disabledRwExported = reader.getBooleanFlagValue("disabled_rw_exported", false);
                        enabledRw = reader.getBooleanFlagValue("enabled_rw", true);
                        disabledRwInOtherNamespace = reader.getBooleanFlagValue("disabled_rw_in_other_namespace", false);
                    } else {
                        Log.e(TAG, error.getMessage());
                    }
                } catch (Exception e) {
                    // pass
                    Log.e(TAG, e.getMessage());
                } catch (NoClassDefFoundError e) {
                    // for mainline module running on older devices.
                    // This should be replaces to version check, after the version bump.
                    Log.e(TAG, e.getMessage());
                }
                isCached = true;
            }
@@ -653,6 +674,7 @@ mod tests {
            mode,
            flag_ids,
            true,
            5801144784618221668,
        )
        .unwrap();

@@ -851,6 +873,7 @@ mod tests {
            mode,
            flag_ids,
            true,
            5801144784618221668,
        )
        .unwrap();

@@ -972,6 +995,7 @@ mod tests {
            mode,
            flag_ids,
            true,
            5801144784618221668,
        )
        .unwrap();
        let expect_featureflags_content = r#"
+4 −0
Original line number Diff line number Diff line
@@ -225,6 +225,9 @@ pub fn create_java_lib(
        bail!("no parsed flags, or the parsed flags use different packages");
    };
    let package = package.to_string();
    let mut flag_names =
        modified_parsed_flags.iter().map(|pf| pf.name().to_string()).collect::<Vec<_>>();
    let package_fingerprint = compute_flags_fingerprint(&mut flag_names);
    let flag_ids = assign_flag_ids(&package, modified_parsed_flags.iter())?;
    generate_java_code(
        &package,
@@ -232,6 +235,7 @@ pub fn create_java_lib(
        codegen_mode,
        flag_ids,
        allow_instrumentation,
        package_fingerprint,
    )
}

+30 −4
Original line number Diff line number Diff line
@@ -5,11 +5,19 @@ package {package_name};
// TODO(b/303773055): Remove the annotation after access issue is resolved.
import android.compat.annotation.UnsupportedAppUsage;
{{ -if runtime_lookup_required }}
import android.aconfig.storage.StorageInternalReader;
import android.os.Build;
{{ if is_platform_container }}
import android.os.flagging.PlatformAconfigPackageInternal;
{{ -else }}
import android.os.flagging.AconfigPackageInternal;
{{ -endif }}
import android.os.flagging.AconfigStorageReadException;
import android.util.Log;
{{ -endif }}
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags \{
{{ -if runtime_lookup_required }}
    private static final String TAG = "{package_name}.FeatureFlagsImpl";
    private static volatile boolean isCached = false;
{{ for flag in flag_elements }}
{{ -if flag.is_read_write }}
@@ -19,19 +27,37 @@ public final class FeatureFlagsImpl implements FeatureFlags \{

    private void init() \{
        try \{
            StorageInternalReader reader = new StorageInternalReader("{container}", "{package_name}");
{{ if is_platform_container }}
            PlatformAconfigPackageInternal reader = PlatformAconfigPackageInternal.load("{container}", "{package_name}", {package_fingerprint});
{{ -else }}
            AconfigPackageInternal reader = AconfigPackageInternal.load("{container}", "{package_name}", {package_fingerprint});
{{ -endif }}
            AconfigStorageReadException error = reader.getException();
            if (error == null) \{
            {{ for namespace_with_flags in namespace_flags }}
            {{ -for flag in namespace_with_flags.flags }}
            {{ if flag.is_read_write }}
            {{ -if flag.is_read_write }}
                {flag.method_name} = reader.getBooleanFlagValue({flag.flag_offset});
            {{ endif }}
            {{ -endfor }}
            {{ -endfor }}
            } else if (Build.VERSION.SDK_INT > 35 && error.getErrorCode() == 5 /* fingerprint doesn't match*/) \{
            {{ for namespace_with_flags in namespace_flags }}
            {{ -for flag in namespace_with_flags.flags }}
            {{ -if flag.is_read_write }}
                {flag.method_name} = reader.getBooleanFlagValue("{flag.flag_name}", {flag.default_value});
            {{ -endif }}
            {{ -endfor }}
            {{ -endfor }}
            } else \{
                Log.e(TAG, error.getMessage());
            }
        } catch (Exception e) \{
            // pass
            Log.e(TAG, e.getMessage());
        } catch (NoClassDefFoundError e) \{
            // for mainline module running on older devices.
            // This should be replaces to version check, after the version bump.
            Log.e(TAG, e.getMessage());
        }
        isCached = true;
    }
+3 −22
Original line number Diff line number Diff line
@@ -167,27 +167,8 @@ java_library {
    ],
    sdk_version: "core_current",
    host_supported: true,
    min_sdk_version: "29",
    apex_available: [
        "//apex_available:platform",
        "//apex_available:anyapex",
    ],
}

java_library {
    name: "aconfig_storage_reader_java_none",
    srcs: [
        "srcs/android/aconfig/storage/StorageInternalReader.java",
        "srcs/android/os/flagging/PlatformAconfigPackageInternal.java",
    ],
    libs: [
        "unsupportedappusage-sdk-none",
        "fake_device_config",
    ],
    static_libs: [
        "aconfig_storage_file_java_none",
    visibility: [
        "//frameworks/base",
        "//build/make/tools/aconfig/aconfig_storage_read_api/tests",
    ],
    sdk_version: "none",
    system_modules: "core-all-system-modules",
    host_supported: true,
}
+6 −6
Original line number Diff line number Diff line
@@ -45,14 +45,14 @@ public class PlatformAconfigPackageInternal {
    private final FlagValueList mFlagValueList;
    private final int mPackageId;
    private final int mPackageBooleanStartOffset;
    private final AconfigStorageException mException;
    private final AconfigStorageReadException mException;

    private PlatformAconfigPackageInternal(
            FlagValueList flagValueList,
            FlagTable flagTable,
            int packageBooleanStartOffset,
            int packageId,
            AconfigStorageException exception) {
            AconfigStorageReadException exception) {
        this.mFlagValueList = flagValueList;
        this.mFlagTable = flagTable;
        this.mPackageBooleanStartOffset = packageBooleanStartOffset;
@@ -150,7 +150,7 @@ public class PlatformAconfigPackageInternal {

            if (pNode == null) {
                return createExceptionInstance(
                        AconfigStorageException.ERROR_PACKAGE_NOT_FOUND,
                        AconfigStorageReadException.ERROR_PACKAGE_NOT_FOUND,
                        "package "
                                + packageName
                                + " in container "
@@ -165,7 +165,7 @@ public class PlatformAconfigPackageInternal {
                        fileProvider.getFlagTable(container),
                        pNode.getBooleanStartIndex(),
                        pNode.getPackageId(),
                        new AconfigStorageException(
                        new AconfigStorageReadException(
                                AconfigStorageException.ERROR_FILE_FINGERPRINT_MISMATCH,
                                "The fingerprint provided for the Aconfig package "
                                        + packageName
@@ -244,7 +244,7 @@ public class PlatformAconfigPackageInternal {
     * @hide
     */
    @UnsupportedAppUsage
    public Exception getException() {
    public AconfigStorageReadException getException() {
        return mException;
    }

@@ -259,6 +259,6 @@ public class PlatformAconfigPackageInternal {
    private static PlatformAconfigPackageInternal createExceptionInstance(
            int errorCode, String message) {
        return new PlatformAconfigPackageInternal(
                null, null, 0, 0, new AconfigStorageException(errorCode, message));
                null, null, 0, 0, new AconfigStorageReadException(errorCode, message));
    }
}
Loading