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

Commit 09f8edbc authored by Victor Gabriel Savu's avatar Victor Gabriel Savu
Browse files

Add support for @IntegerPolicyDefinition

Bug: 443256730
Test: atest frameworks/base/tools/processors/devicepolicy/test/src/android/processor/devicepolicy/test/PolicyProcessorTest.kt
Flag: android.app.admin.flags.policy_streamlining
Change-Id: I1b8ae7f719198819de64f838a79cb7c26dac724e
parent 11ad5223
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.processor.devicepolicy;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Metadata for an integer policy.
 */
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.FIELD})
public @interface IntegerPolicyDefinition {
    /**
     * Base data for all policies.
     */
    PolicyDefinition base();
}
+68 −0
Original line number Diff line number Diff line
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.processor.devicepolicy

import com.android.json.stream.JsonWriter
import javax.annotation.processing.ProcessingEnvironment
import javax.lang.model.element.Element
import javax.lang.model.type.TypeMirror

/**
 * Process elements with @IntegerPolicyDefinition.
 *
 * Since this annotation holds no data and we don't export any type-specific information, this only
 * contains type-specific checks.
 */
class IntegerProcessor(processingEnv: ProcessingEnvironment) : Processor<IntegerPolicyDefinition>(processingEnv) {
    private companion object {
        const val SIMPLE_TYPE_INTEGER = "java.lang.Integer"
    }

    /** Represents a built-in Integer */
    val integerType: TypeMirror =
        processingEnv.elementUtils.getTypeElement(SIMPLE_TYPE_INTEGER).asType()

    /**
     * Process an {@link Element} representing a {@link android.app.admin.PolicyIdentifier} into useful data.
     *
     * @return null on error, {@link IntegerPolicyMetadata} otherwise.
     */
    override fun processMetadata(element: Element): Pair<PolicyMetadata, PolicyDefinition>? {
        val integerDefinition = element.getAnnotation(IntegerPolicyDefinition::class.java)
            ?: throw IllegalStateException("Processor should only be called on elements with @IntegerPolicyMetadata")

        val actualType = policyType(element)
        if (!processingEnv.typeUtils.isSameType(actualType, integerType)) {
            printError(
                element,
                "@IntegerPolicyDefinition can only be applied to policies of type $integerType, but got $actualType."
            )
        }

        return Pair(IntegerPolicyMetadata(), integerDefinition.base)
    }

    override fun annotationClass(): Class<IntegerPolicyDefinition> {
        return IntegerPolicyDefinition::class.java
    }
}

class IntegerPolicyMetadata() : PolicyMetadata() {
    override fun dump(writer: JsonWriter) {
        // Nothing to include for IntegerPolicyMetadata.
    }
}
 No newline at end of file
+3 −1
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ class PolicyProcessor : AbstractProcessor() {
    override fun getSupportedAnnotationTypes() = LinkedHashSet<String>().apply {
        add(BooleanPolicyDefinition::class.java.name)
        add(EnumPolicyDefinition::class.java.name)
        add(IntegerPolicyDefinition::class.java.name)

        // Only processed to report errors.
        add(PolicyDefinition::class.java.name)
@@ -62,7 +63,8 @@ class PolicyProcessor : AbstractProcessor() {

        val policies = listOf(
            runProcessor(roundEnvironment, BooleanProcessor(processingEnv)),
            runProcessor(roundEnvironment, EnumProcessor(processingEnv))
            runProcessor(roundEnvironment, EnumProcessor(processingEnv)),
            runProcessor(roundEnvironment, IntegerProcessor(processingEnv)),
        ).flatten()

        try {
+12 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.app.admin;
import android.annotation.IntDef;
import android.processor.devicepolicy.BooleanPolicyDefinition;
import android.processor.devicepolicy.EnumPolicyDefinition;
import android.processor.devicepolicy.IntegerPolicyDefinition;
import android.processor.devicepolicy.PolicyDefinition;

import java.lang.annotation.Retention;
@@ -80,4 +81,15 @@ public final class PolicyIdentifier<T> {
    )
    public static final PolicyIdentifier<Integer> TEST_POLICY_2 = new PolicyIdentifier<>(
            TEST_POLICY_2_KEY);

    private static final String TEST_POLICY_3_KEY = "test_policy_3_key";

    /**
     * Test policy 3
     */
    @IntegerPolicyDefinition(
            base = @PolicyDefinition
    )
    public static final PolicyIdentifier<Integer> TEST_POLICY_3 = new PolicyIdentifier<>(
            TEST_POLICY_3_KEY);
}
+5 −0
Original line number Diff line number Diff line
@@ -29,6 +29,11 @@
          "documentation": " Third entry\n"
        }
      ]
    },
    {
      "name": "android.app.admin.PolicyIdentifier<T>.TEST_POLICY_3",
      "type": "java.lang.Integer",
      "documentation": " Test policy 3\n"
    }
  ]
}
 No newline at end of file