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

Commit 63933094 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add a method to get all compile-time available system features" into main

parents e6ba96dd 568adec5
Loading
Loading
Loading
Loading
+43 −1
Original line number Diff line number Diff line
@@ -20,7 +20,10 @@ import com.google.common.base.CaseFormat
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.ParameterizedTypeName
import com.squareup.javapoet.TypeSpec
import java.util.HashMap
import java.util.Map
import javax.lang.model.element.Modifier

/*
@@ -49,6 +52,7 @@ import javax.lang.model.element.Modifier
 *     public static boolean hasFeatureAutomotive(Context context);
 *     public static boolean hasFeatureLeanback(Context context);
 *     public static Boolean maybeHasFeature(String feature, int version);
 *     public static ArrayMap<String, FeatureInfo> getCompileTimeAvailableFeatures();
 * }
 * </pre>
 */
@@ -58,6 +62,7 @@ object SystemFeaturesGenerator {
    private const val READONLY_ARG = "--readonly="
    private val PACKAGEMANAGER_CLASS = ClassName.get("android.content.pm", "PackageManager")
    private val CONTEXT_CLASS = ClassName.get("android.content", "Context")
    private val FEATUREINFO_CLASS = ClassName.get("android.content.pm", "FeatureInfo")
    private val ASSUME_TRUE_CLASS =
        ClassName.get("com.android.aconfig.annotations", "AssumeTrueForR8")
    private val ASSUME_FALSE_CLASS =
@@ -142,6 +147,7 @@ object SystemFeaturesGenerator {

        addFeatureMethodsToClass(classBuilder, features.values)
        addMaybeFeatureMethodToClass(classBuilder, features.values)
        addGetFeaturesMethodToClass(classBuilder, features.values)

        // TODO(b/203143243): Add validation of build vs runtime values to ensure consistency.
        JavaFile.builder(outputClassName.packageName(), classBuilder.build())
@@ -225,7 +231,7 @@ object SystemFeaturesGenerator {
    /*
     * Adds a generic query method to the class with the form: {@code public static boolean
     * maybeHasFeature(String featureName, int version)}, returning null if the feature version is
     * undefined or not readonly.
     * undefined or not (compile-time) readonly.
     *
     * This method is useful for internal usage within the framework, e.g., from the implementation
     * of {@link android.content.pm.PackageManager#hasSystemFeature(Context)}, when we may only
@@ -274,5 +280,41 @@ object SystemFeaturesGenerator {
        builder.addMethod(methodBuilder.build())
    }

    /*
     * Adds a method to get all compile-time enabled features.
     *
     * This method is useful for internal usage within the framework to augment
     * any system features that are parsed from the various partitions.
     */
    private fun addGetFeaturesMethodToClass(
        builder: TypeSpec.Builder,
        features: Collection<FeatureInfo>,
    ) {
        val methodBuilder =
                MethodSpec.methodBuilder("getCompileTimeAvailableFeatures")
                .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                .addAnnotation(ClassName.get("android.annotation", "NonNull"))
                .addJavadoc("Gets features marked as available at compile-time, keyed by name." +
                        "\n\n@hide")
                .returns(ParameterizedTypeName.get(
                        ClassName.get(Map::class.java),
                        ClassName.get(String::class.java),
                        FEATUREINFO_CLASS))

        val availableFeatures = features.filter { it.readonly && it.version != null }
        methodBuilder.addStatement("Map<String, FeatureInfo> features = new \$T<>(\$L)",
                HashMap::class.java, availableFeatures.size)
        if (!availableFeatures.isEmpty()) {
            methodBuilder.addStatement("FeatureInfo fi = new FeatureInfo()")
        }
        for (feature in availableFeatures) {
            methodBuilder.addStatement("fi.name = \$T.\$N", PACKAGEMANAGER_CLASS, feature.name)
            methodBuilder.addStatement("fi.version = \$L", feature.version)
            methodBuilder.addStatement("features.put(fi.name, new FeatureInfo(fi))")
        }
        methodBuilder.addStatement("return features")
        builder.addMethod(methodBuilder.build())
    }

    private data class FeatureInfo(val name: String, val version: Int?, val readonly: Boolean)
}
+22 −0
Original line number Diff line number Diff line
@@ -8,11 +8,15 @@
//            --feature-apis=WATCH,PC
package com.android.systemfeatures;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import com.android.aconfig.annotations.AssumeFalseForR8;
import com.android.aconfig.annotations.AssumeTrueForR8;
import java.util.HashMap;
import java.util.Map;

/**
 * @hide
@@ -83,4 +87,22 @@ public final class RoFeatures {
        }
        return null;
    }

    /**
     * Gets features marked as available at compile-time, keyed by name.
     *
     * @hide
     */
    @NonNull
    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
        Map<String, FeatureInfo> features = new HashMap<>(2);
        FeatureInfo fi = new FeatureInfo();
        fi.name = PackageManager.FEATURE_WATCH;
        fi.version = 1;
        features.put(fi.name, new FeatureInfo(fi));
        fi.name = PackageManager.FEATURE_WIFI;
        fi.version = 0;
        features.put(fi.name, new FeatureInfo(fi));
        return features;
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -4,9 +4,13 @@
//            --feature-apis=WATCH
package com.android.systemfeatures;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import java.util.HashMap;
import java.util.Map;

/**
 * @hide
@@ -32,4 +36,15 @@ public final class RoNoFeatures {
    public static Boolean maybeHasFeature(String featureName, int version) {
        return null;
    }

    /**
     * Gets features marked as available at compile-time, keyed by name.
     *
     * @hide
     */
    @NonNull
    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
        Map<String, FeatureInfo> features = new HashMap<>(0);
        return features;
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -7,9 +7,13 @@
//            --feature=AUTO:
package com.android.systemfeatures;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import java.util.HashMap;
import java.util.Map;

/**
 * @hide
@@ -62,4 +66,15 @@ public final class RwFeatures {
    public static Boolean maybeHasFeature(String featureName, int version) {
        return null;
    }

    /**
     * Gets features marked as available at compile-time, keyed by name.
     *
     * @hide
     */
    @NonNull
    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
        Map<String, FeatureInfo> features = new HashMap<>(0);
        return features;
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -3,8 +3,12 @@
//            --readonly=false
package com.android.systemfeatures;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.FeatureInfo;
import java.util.HashMap;
import java.util.Map;

/**
 * @hide
@@ -21,4 +25,15 @@ public final class RwNoFeatures {
    public static Boolean maybeHasFeature(String featureName, int version) {
        return null;
    }

    /**
     * Gets features marked as available at compile-time, keyed by name.
     *
     * @hide
     */
    @NonNull
    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
        Map<String, FeatureInfo> features = new HashMap<>(0);
        return features;
    }
}
Loading