Loading tools/systemfeatures/src/com/android/systemfeatures/SystemFeaturesGenerator.kt +43 −1 Original line number Diff line number Diff line Loading @@ -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 /* Loading Loading @@ -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> */ Loading @@ -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 = Loading Loading @@ -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()) Loading Loading @@ -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 Loading Loading @@ -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) } tools/systemfeatures/tests/golden/RoFeatures.java.gen +22 −0 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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; } } tools/systemfeatures/tests/golden/RoNoFeatures.java.gen +15 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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; } } tools/systemfeatures/tests/golden/RwFeatures.java.gen +15 −0 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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; } } tools/systemfeatures/tests/golden/RwNoFeatures.java.gen +15 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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
tools/systemfeatures/src/com/android/systemfeatures/SystemFeaturesGenerator.kt +43 −1 Original line number Diff line number Diff line Loading @@ -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 /* Loading Loading @@ -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> */ Loading @@ -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 = Loading Loading @@ -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()) Loading Loading @@ -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 Loading Loading @@ -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) }
tools/systemfeatures/tests/golden/RoFeatures.java.gen +22 −0 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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; } }
tools/systemfeatures/tests/golden/RoNoFeatures.java.gen +15 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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; } }
tools/systemfeatures/tests/golden/RwFeatures.java.gen +15 −0 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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; } }
tools/systemfeatures/tests/golden/RwNoFeatures.java.gen +15 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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; } }