diff --git a/Android.bp b/Android.bp index 7687270fc6f7f..51cbba7cfa683 100644 --- a/Android.bp +++ b/Android.bp @@ -798,7 +798,10 @@ java_defaults { "--multi-dex", ], - plugins: ["view-inspector-annotation-processor"], + plugins: [ + "view-inspector-annotation-processor", + "staledataclass-annotation-processor", + ], } filegroup { diff --git a/core/java/android/service/autofill/FillContext.java b/core/java/android/service/autofill/FillContext.java index 70f434c128639..8331550a7ef56 100644 --- a/core/java/android/service/autofill/FillContext.java +++ b/core/java/android/service/autofill/FillContext.java @@ -30,6 +30,8 @@ import android.util.ArrayMap; import android.util.SparseIntArray; import android.view.autofill.AutofillId; +import com.android.internal.util.DataClass; + import java.util.LinkedList; /** @@ -46,58 +48,36 @@ import java.util.LinkedList; * to {@link FillResponse.Builder#setClientState(Bundle)} to avoid interpreting * the UI state again while saving. */ +@DataClass( + genHiddenConstructor = true, + genAidl = false) public final class FillContext implements Parcelable { + + /** + * The id of the {@link FillRequest fill request} this context + * corresponds to. This is useful to associate your custom client + * state with every request to avoid reinterpreting the UI when saving + * user data. + */ private final int mRequestId; + + /** + * The screen content. + */ private final @NonNull AssistStructure mStructure; + + /** + * The AutofillId of the view that triggered autofill. + */ private final @NonNull AutofillId mFocusedId; /** * Lookup table AutofillId->ViewNode to speed up {@link #findViewNodesByAutofillIds} * This is purely a cache and can be deleted at any time */ - @Nullable private ArrayMap mViewNodeLookupTable; + private transient @Nullable ArrayMap mViewNodeLookupTable; - /** @hide */ - public FillContext(int requestId, @NonNull AssistStructure structure, - @NonNull AutofillId autofillId) { - mRequestId = requestId; - mStructure = structure; - mFocusedId = autofillId; - } - - private FillContext(Parcel parcel) { - this(parcel.readInt(), parcel.readParcelable(null), parcel.readParcelable(null)); - } - - /** - * Gets the id of the {@link FillRequest fill request} this context - * corresponds to. This is useful to associate your custom client - * state with every request to avoid reinterpreting the UI when saving - * user data. - * - * @return The request id. - */ - public int getRequestId() { - return mRequestId; - } - - /** - * @return The screen content. - */ - @NonNull - public AssistStructure getStructure() { - return mStructure; - } - - /** - * @return the AutofillId of the view that triggered autofill. - */ - @NonNull - public AutofillId getFocusedId() { - return mFocusedId; - } - @Override public String toString() { if (!sDebug) return super.toString(); @@ -105,18 +85,6 @@ public final class FillContext implements Parcelable { return "FillContext [reqId=" + mRequestId + ", focusedId=" + mFocusedId + "]"; } - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel parcel, int flags) { - parcel.writeInt(mRequestId); - parcel.writeParcelable(mStructure, flags); - parcel.writeParcelable(mFocusedId, flags); - } - /** * Finds {@link ViewNode ViewNodes} that have the requested ids. * @@ -190,18 +158,119 @@ public final class FillContext implements Parcelable { return foundNodes; } - public static final @android.annotation.NonNull Parcelable.Creator CREATOR = - new Parcelable.Creator() { - @Override - @NonNull - public FillContext createFromParcel(Parcel parcel) { - return new FillContext(parcel); - } + + // Code below generated by codegen v1.0.0. + // + // DO NOT MODIFY! + // + // To regenerate run: + // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/service/autofill/FillContext.java + // + // CHECKSTYLE:OFF Generated code + + /** + * Creates a new FillContext. + * + * @param requestId + * The id of the {@link FillRequest fill request} this context + * corresponds to. This is useful to associate your custom client + * state with every request to avoid reinterpreting the UI when saving + * user data. + * @param structure + * The screen content. + * @param focusedId + * The AutofillId of the view that triggered autofill. + * @hide + */ + @DataClass.Generated.Member + public FillContext( + int requestId, + @NonNull AssistStructure structure, + @NonNull AutofillId focusedId) { + this.mRequestId = requestId; + this.mStructure = structure; + com.android.internal.util.AnnotationValidations.validate( + NonNull.class, null, mStructure); + this.mFocusedId = focusedId; + com.android.internal.util.AnnotationValidations.validate( + NonNull.class, null, mFocusedId); + + // onConstructed(); // You can define this method to get a callback + } + + /** + * The id of the {@link FillRequest fill request} this context + * corresponds to. This is useful to associate your custom client + * state with every request to avoid reinterpreting the UI when saving + * user data. + */ + @DataClass.Generated.Member + public int getRequestId() { + return mRequestId; + } + + /** + * The screen content. + */ + @DataClass.Generated.Member + public @NonNull AssistStructure getStructure() { + return mStructure; + } + + /** + * The AutofillId of the view that triggered autofill. + */ + @DataClass.Generated.Member + public @NonNull AutofillId getFocusedId() { + return mFocusedId; + } + + @Override + @DataClass.Generated.Member + public void writeToParcel(Parcel dest, int flags) { + // You can override field parcelling by defining methods like: + // void parcelFieldName(Parcel dest, int flags) { ... } + + dest.writeInt(mRequestId); + dest.writeTypedObject(mStructure, flags); + dest.writeTypedObject(mFocusedId, flags); + } + + @Override + @DataClass.Generated.Member + public int describeContents() { return 0; } + + @DataClass.Generated.Member + public static final @NonNull Parcelable.Creator CREATOR + = new Parcelable.Creator() { @Override - @NonNull public FillContext[] newArray(int size) { return new FillContext[size]; } + + @Override + @SuppressWarnings({"unchecked", "RedundantCast"}) + public FillContext createFromParcel(Parcel in) { + // You can override field unparcelling by defining methods like: + // static FieldType unparcelFieldName(Parcel in) { ... } + + int requestId = in.readInt(); + AssistStructure structure = (AssistStructure) in.readTypedObject(AssistStructure.CREATOR); + AutofillId focusedId = (AutofillId) in.readTypedObject(AutofillId.CREATOR); + return new FillContext( + requestId, + structure, + focusedId); + } }; + + @DataClass.Generated( + time = 1565152135263L, + codegenVersion = "1.0.0", + sourceFile = "frameworks/base/core/java/android/service/autofill/FillContext.java", + inputSignatures = "private final int mRequestId\nprivate final @android.annotation.NonNull android.app.assist.AssistStructure mStructure\nprivate final @android.annotation.NonNull android.view.autofill.AutofillId mFocusedId\nprivate transient @android.annotation.Nullable android.util.ArrayMap mViewNodeLookupTable\npublic @java.lang.Override java.lang.String toString()\npublic @android.annotation.NonNull android.app.assist.AssistStructure.ViewNode[] findViewNodesByAutofillIds(android.view.autofill.AutofillId[])\nclass FillContext extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true, genAidl=false)") + @Deprecated + private void __metadata() {} + } diff --git a/core/java/android/service/autofill/FillRequest.java b/core/java/android/service/autofill/FillRequest.java index 91f77ea0462da..e53ebada55fb9 100644 --- a/core/java/android/service/autofill/FillRequest.java +++ b/core/java/android/service/autofill/FillRequest.java @@ -24,6 +24,7 @@ import android.os.Parcel; import android.os.Parcelable; import android.view.View; +import com.android.internal.util.DataClass; import com.android.internal.util.Preconditions; import java.lang.annotation.Retention; @@ -39,6 +40,10 @@ import java.util.List; * * @see AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback) */ +@DataClass( + genToString = true, + genHiddenConstructor = true, + genHiddenConstDefs = true) public final class FillRequest implements Parcelable { /** @@ -63,54 +68,45 @@ public final class FillRequest implements Parcelable { * is called. For example, standard {@link android.widget.TextView} views show an * {@code AUTOFILL} option in the overflow menu that triggers such request. */ - public static final int FLAG_MANUAL_REQUEST = 0x1; + public static final @RequestFlags int FLAG_MANUAL_REQUEST = 0x1; /** * Indicates this request was made using * compatibility mode. */ - public static final int FLAG_COMPATIBILITY_MODE_REQUEST = 0x2; + public static final @RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST = 0x2; /** @hide */ public static final int INVALID_REQUEST_ID = Integer.MIN_VALUE; - /** @hide */ - @IntDef(flag = true, prefix = { "FLAG_" }, value = { - FLAG_MANUAL_REQUEST, FLAG_COMPATIBILITY_MODE_REQUEST - }) - @Retention(RetentionPolicy.SOURCE) - @interface RequestFlags{} - - private final int mId; - private final @RequestFlags int mFlags; - private final @NonNull ArrayList mContexts; - private final @Nullable Bundle mClientState; - - private FillRequest(@NonNull Parcel parcel) { - mId = parcel.readInt(); - mContexts = new ArrayList<>(); - parcel.readParcelableList(mContexts, null); - - mClientState = parcel.readBundle(); - mFlags = parcel.readInt(); - } - - /** @hide */ - public FillRequest(int id, @NonNull ArrayList contexts, - @Nullable Bundle clientState, @RequestFlags int flags) { - mId = id; - mFlags = Preconditions.checkFlagsArgument(flags, - FLAG_MANUAL_REQUEST | FLAG_COMPATIBILITY_MODE_REQUEST); - mContexts = Preconditions.checkCollectionElementsNotNull(contexts, "contexts"); - mClientState = clientState; - } - /** * Gets the unique id of this request. */ - public int getId() { - return mId; - } + private final int mId; + + /** + * Gets the contexts associated with each previous fill request. + * + *

Note: Starting on Android {@link android.os.Build.VERSION_CODES#Q}, it could also + * include contexts from requests whose {@link SaveInfo} had the + * {@link SaveInfo#FLAG_DELAY_SAVE} flag. + */ + private final @NonNull List mFillContexts; + + /** + * Gets the latest client state bundle set by the service in a + * {@link FillResponse.Builder#setClientState(Bundle) fill response}. + * + *

Note: Prior to Android {@link android.os.Build.VERSION_CODES#P}, only client state + * bundles set by {@link FillResponse.Builder#setClientState(Bundle)} were considered. On + * Android {@link android.os.Build.VERSION_CODES#P} and higher, bundles set in the result of + * an authenticated request through the + * {@link android.view.autofill.AutofillManager#EXTRA_CLIENT_STATE} extra are + * also considered (and take precedence when set). + * + * @return The client state. + */ + private final @Nullable Bundle mClientState; /** * Gets the flags associated with this request. @@ -118,8 +114,105 @@ public final class FillRequest implements Parcelable { * @return any combination of {@link #FLAG_MANUAL_REQUEST} and * {@link #FLAG_COMPATIBILITY_MODE_REQUEST}. */ - public @RequestFlags int getFlags() { - return mFlags; + private final @RequestFlags int mFlags; + + private void onConstructed() { + Preconditions.checkCollectionElementsNotNull(mFillContexts, "contexts"); + } + + + + // Code below generated by codegen v1.0.0. + // + // DO NOT MODIFY! + // + // To regenerate run: + // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/service/autofill/FillRequest.java + // + // CHECKSTYLE:OFF Generated code + + /** @hide */ + @IntDef(flag = true, prefix = "FLAG_", value = { + FLAG_MANUAL_REQUEST, + FLAG_COMPATIBILITY_MODE_REQUEST + }) + @Retention(RetentionPolicy.SOURCE) + @DataClass.Generated.Member + public @interface RequestFlags {} + + /** @hide */ + @DataClass.Generated.Member + public static String requestFlagsToString(@RequestFlags int value) { + return com.android.internal.util.BitUtils.flagsToString( + value, FillRequest::singleRequestFlagsToString); + } + + @DataClass.Generated.Member + static String singleRequestFlagsToString(@RequestFlags int value) { + switch (value) { + case FLAG_MANUAL_REQUEST: + return "FLAG_MANUAL_REQUEST"; + case FLAG_COMPATIBILITY_MODE_REQUEST: + return "FLAG_COMPATIBILITY_MODE_REQUEST"; + default: return Integer.toHexString(value); + } + } + + /** + * Creates a new FillRequest. + * + * @param id + * Gets the unique id of this request. + * @param fillContexts + * Gets the contexts associated with each previous fill request. + * + *

Note: Starting on Android {@link android.os.Build.VERSION_CODES#Q}, it could also + * include contexts from requests whose {@link SaveInfo} had the + * {@link SaveInfo#FLAG_DELAY_SAVE} flag. + * @param clientState + * Gets the latest client state bundle set by the service in a + * {@link FillResponse.Builder#setClientState(Bundle) fill response}. + * + *

Note: Prior to Android {@link android.os.Build.VERSION_CODES#P}, only client state + * bundles set by {@link FillResponse.Builder#setClientState(Bundle)} were considered. On + * Android {@link android.os.Build.VERSION_CODES#P} and higher, bundles set in the result of + * an authenticated request through the + * {@link android.view.autofill.AutofillManager#EXTRA_CLIENT_STATE} extra are + * also considered (and take precedence when set). + * @param flags + * Gets the flags associated with this request. + * + * @return any combination of {@link #FLAG_MANUAL_REQUEST} and + * {@link #FLAG_COMPATIBILITY_MODE_REQUEST}. + * @hide + */ + @DataClass.Generated.Member + public FillRequest( + int id, + @NonNull List fillContexts, + @Nullable Bundle clientState, + @RequestFlags int flags) { + this.mId = id; + this.mFillContexts = fillContexts; + com.android.internal.util.AnnotationValidations.validate( + NonNull.class, null, mFillContexts); + this.mClientState = clientState; + this.mFlags = flags; + + Preconditions.checkFlagsArgument( + mFlags, + FLAG_MANUAL_REQUEST + | FLAG_COMPATIBILITY_MODE_REQUEST); + + onConstructed(); + } + + /** + * Gets the unique id of this request. + */ + @DataClass.Generated.Member + public int getId() { + return mId; } /** @@ -129,13 +222,9 @@ public final class FillRequest implements Parcelable { * include contexts from requests whose {@link SaveInfo} had the * {@link SaveInfo#FLAG_DELAY_SAVE} flag. */ + @DataClass.Generated.Member public @NonNull List getFillContexts() { - return mContexts; - } - - @Override - public String toString() { - return "FillRequest: [id=" + mId + ", flags=" + mFlags + ", ctxts= " + mContexts + "]"; + return mFillContexts; } /** @@ -151,33 +240,89 @@ public final class FillRequest implements Parcelable { * * @return The client state. */ + @DataClass.Generated.Member public @Nullable Bundle getClientState() { return mClientState; } - @Override - public int describeContents() { - return 0; + /** + * Gets the flags associated with this request. + * + * @return any combination of {@link #FLAG_MANUAL_REQUEST} and + * {@link #FLAG_COMPATIBILITY_MODE_REQUEST}. + */ + @DataClass.Generated.Member + public @RequestFlags int getFlags() { + return mFlags; } @Override - public void writeToParcel(Parcel parcel, int flags) { - parcel.writeInt(mId); - parcel.writeParcelableList(mContexts, flags); - parcel.writeBundle(mClientState); - parcel.writeInt(mFlags); + @DataClass.Generated.Member + public String toString() { + // You can override field toString logic by defining methods like: + // String fieldNameToString() { ... } + + return "FillRequest { " + + "id = " + mId + ", " + + "fillContexts = " + mFillContexts + ", " + + "clientState = " + mClientState + ", " + + "flags = " + requestFlagsToString(mFlags) + + " }"; } - public static final @android.annotation.NonNull Parcelable.Creator CREATOR = - new Parcelable.Creator() { - @Override - public FillRequest createFromParcel(Parcel parcel) { - return new FillRequest(parcel); - } + @Override + @DataClass.Generated.Member + public void writeToParcel(Parcel dest, int flags) { + // You can override field parcelling by defining methods like: + // void parcelFieldName(Parcel dest, int flags) { ... } + byte flg = 0; + if (mClientState != null) flg |= 0x4; + dest.writeByte(flg); + dest.writeInt(mId); + dest.writeParcelableList(mFillContexts, flags); + if (mClientState != null) dest.writeBundle(mClientState); + dest.writeInt(mFlags); + } + + @Override + @DataClass.Generated.Member + public int describeContents() { return 0; } + + @DataClass.Generated.Member + public static final @NonNull Parcelable.Creator CREATOR + = new Parcelable.Creator() { @Override public FillRequest[] newArray(int size) { return new FillRequest[size]; } + + @Override + @SuppressWarnings({"unchecked", "RedundantCast"}) + public FillRequest createFromParcel(Parcel in) { + // You can override field unparcelling by defining methods like: + // static FieldType unparcelFieldName(Parcel in) { ... } + + byte flg = in.readByte(); + int id = in.readInt(); + List fillContexts = new ArrayList<>(); + in.readParcelableList(fillContexts, FillContext.class.getClassLoader()); + Bundle clientState = (flg & 0x4) == 0 ? null : in.readBundle(); + int flags = in.readInt(); + return new FillRequest( + id, + fillContexts, + clientState, + flags); + } }; + + @DataClass.Generated( + time = 1565152134349L, + codegenVersion = "1.0.0", + sourceFile = "frameworks/base/core/java/android/service/autofill/FillRequest.java", + inputSignatures = "public static final @android.service.autofill.FillRequest.RequestFlags int FLAG_MANUAL_REQUEST\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST\npublic static final int INVALID_REQUEST_ID\nprivate final int mId\nprivate final @android.annotation.NonNull java.util.List mFillContexts\nprivate final @android.annotation.Nullable android.os.Bundle mClientState\nprivate final @android.service.autofill.FillRequest.RequestFlags int mFlags\nprivate void onConstructed()\nclass FillRequest extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genToString=true, genHiddenConstructor=true, genHiddenConstDefs=true)") + @Deprecated + private void __metadata() {} + } diff --git a/core/java/com/android/internal/util/DataClass.java b/core/java/com/android/internal/util/DataClass.java index da33f996b6a78..43539c7c40116 100644 --- a/core/java/com/android/internal/util/DataClass.java +++ b/core/java/com/android/internal/util/DataClass.java @@ -64,11 +64,21 @@ public @interface DataClass { */ boolean genGetters() default true; + /** + * {@link #genGetters} with @hide + */ + boolean genHiddenGetters() default false; + /** * Generates setters for each field. */ boolean genSetters() default false; + /** + * {@link #genSetters} with @hide + */ + boolean genHiddenSetters() default false; + /** * Generates a public constructor with each field initialized from a parameter and optionally * some user-defined state validation at the end. @@ -84,6 +94,11 @@ public @interface DataClass { */ boolean genConstructor() default true; + /** + * {@link #genConstructor} with @hide + */ + boolean genHiddenConstructor() default false; + /** * Generates a Builder for your class. * @@ -92,6 +107,11 @@ public @interface DataClass { */ boolean genBuilder() default false; + /** + * {@link #genBuilder} with @hide + */ + boolean genHiddenBuilder() default false; + /** * Generates a structural {@link Object#equals} + {@link Object#hashCode}. * @@ -125,6 +145,11 @@ public @interface DataClass { */ boolean genCopyConstructor() default false; + /** + * {@link #genCopyConstructor} with @hide + */ + boolean genHiddenCopyConstructor() default false; + /** * Generates constant annotations({@link IntDef}/{@link StringDef}) for any constant groups * with common prefix. @@ -146,6 +171,11 @@ public @interface DataClass { */ boolean genConstDefs() default true; + /** + * {@link #genConstDefs} with @hide + */ + boolean genHiddenConstDefs() default false; + /** * Allows specifying custom parcelling logic based on reusable diff --git a/tools/codegen/src/com/android/codegen/ClassPrinter.kt b/tools/codegen/src/com/android/codegen/ClassPrinter.kt index 22b5d8815e7d3..1f0d4b8a7ec96 100644 --- a/tools/codegen/src/com/android/codegen/ClassPrinter.kt +++ b/tools/codegen/src/com/android/codegen/ClassPrinter.kt @@ -122,9 +122,13 @@ class ClassPrinter( if (cliArgs.contains("--$kebabCase")) return true val annotationKey = "gen$upperCamelCase" + val annotationHiddenKey = "genHidden$upperCamelCase" if (dataClassAnnotationFeatures.containsKey(annotationKey)) { return dataClassAnnotationFeatures[annotationKey]!! } + if (dataClassAnnotationFeatures.containsKey(annotationHiddenKey)) { + return dataClassAnnotationFeatures[annotationHiddenKey]!! + } if (cliArgs.contains("--all")) return true if (hidden) return true @@ -144,11 +148,17 @@ class ClassPrinter( } } - val FeatureFlag.hidden - get(): Boolean = when { - cliArgs.contains("--hidden-$kebabCase") -> true - this == FeatureFlag.BUILD_UPON -> FeatureFlag.BUILDER.hidden - else -> false + val FeatureFlag.hidden: Boolean + get(): Boolean { + val annotationHiddenKey = "genHidden$upperCamelCase" + if (dataClassAnnotationFeatures.containsKey(annotationHiddenKey)) { + return dataClassAnnotationFeatures[annotationHiddenKey]!! + } + return when { + cliArgs.contains("--hidden-$kebabCase") -> true + this == FeatureFlag.BUILD_UPON -> FeatureFlag.BUILDER.hidden + else -> false + } } var currentIndent = INDENT_SINGLE diff --git a/tools/codegen/src/com/android/codegen/FieldInfo.kt b/tools/codegen/src/com/android/codegen/FieldInfo.kt index 6b0009ccff76d..ba00264f5f5e2 100644 --- a/tools/codegen/src/com/android/codegen/FieldInfo.kt +++ b/tools/codegen/src/com/android/codegen/FieldInfo.kt @@ -84,7 +84,6 @@ data class FieldInfo( classInfo.classAst.methods.find { it.nameAsString == "default$NameUpperCamel" && it.parameters.isEmpty() }?.run { return "$nameAsString()" } - if (FieldClass == "List") return "${classPrinter.memberRef("java.util.Collections.emptyList")}()" return null } val hasDefault get() = defaultExpr != null diff --git a/tools/codegen/src/com/android/codegen/Generators.kt b/tools/codegen/src/com/android/codegen/Generators.kt index c6e0a064f9b4a..914e475cfe41c 100644 --- a/tools/codegen/src/com/android/codegen/Generators.kt +++ b/tools/codegen/src/com/android/codegen/Generators.kt @@ -68,12 +68,15 @@ fun ClassPrinter.generateConstDef(consts: List +"$name${if_(!isLast, ",")}" @@ -85,6 +88,9 @@ fun ClassPrinter.generateConstDef(consts: List args.joinToString(", ") {getFullClassName(it)}}?.let { "<$it>" } ?: "") + }) + (type.typeArguments.orElse(null)?.let { args -> args.joinToString(",") {getFullClassName(it)}}?.let { "<$it>" } ?: "") } else getFullClassName(type.asString()) } diff --git a/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt b/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt index 26b15aecbe330..d00def625a394 100644 --- a/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt +++ b/tools/processors/staledataclass/src/android/processor/staledataclass/StaleDataclassProcessor.kt @@ -66,10 +66,14 @@ class StaleDataclassProcessor: AbstractProcessor() { if (dataClassAnnotation == null) { dataClassAnnotation = annotations.find { it.qualifiedName.toString() == DATACLASS_ANNOTATION_NAME - } + } ?: return true } - val generatedAnnotatedElements = roundEnv.getElementsAnnotatedWith(generatedAnnotation) + val generatedAnnotatedElements = if (generatedAnnotation != null) { + roundEnv.getElementsAnnotatedWith(generatedAnnotation) + } else { + emptySet() + } generatedAnnotatedElements.forEach { processSingleFile(it) }