Merge "Update Framework from Jetpack." into sc-dev

This commit is contained in:
Alexander Dorokhine
2021-03-15 23:02:25 +00:00
committed by Android (Google) Code Review
14 changed files with 168 additions and 54 deletions

View File

@@ -180,14 +180,15 @@ package android.app.appsearch {
method public int getScore();
method public long getTtlMillis();
method @NonNull public String getUri();
field public static final String DEFAULT_NAMESPACE = "";
field @Deprecated public static final String DEFAULT_NAMESPACE = "";
}
public static class GenericDocument.Builder<BuilderType extends android.app.appsearch.GenericDocument.Builder> {
ctor public GenericDocument.Builder(@NonNull String, @NonNull String);
ctor @Deprecated public GenericDocument.Builder(@NonNull String, @NonNull String);
ctor public GenericDocument.Builder(@NonNull String, @NonNull String, @NonNull String);
method @NonNull public android.app.appsearch.GenericDocument build();
method @NonNull public BuilderType setCreationTimestampMillis(long);
method @NonNull public BuilderType setNamespace(@NonNull String);
method @Deprecated @NonNull public BuilderType setNamespace(@NonNull String);
method @NonNull public BuilderType setPropertyBoolean(@NonNull String, @NonNull boolean...);
method @NonNull public BuilderType setPropertyBytes(@NonNull String, @NonNull byte[]...);
method @NonNull public BuilderType setPropertyDocument(@NonNull String, @NonNull android.app.appsearch.GenericDocument...);
@@ -206,12 +207,13 @@ package android.app.appsearch {
}
public static final class GetByUriRequest.Builder {
ctor public GetByUriRequest.Builder();
ctor @Deprecated public GetByUriRequest.Builder();
ctor public GetByUriRequest.Builder(@NonNull String);
method @NonNull public android.app.appsearch.GetByUriRequest.Builder addProjection(@NonNull String, @NonNull java.util.Collection<java.lang.String>);
method @NonNull public android.app.appsearch.GetByUriRequest.Builder addUris(@NonNull java.lang.String...);
method @NonNull public android.app.appsearch.GetByUriRequest.Builder addUris(@NonNull java.util.Collection<java.lang.String>);
method @NonNull public android.app.appsearch.GetByUriRequest build();
method @NonNull public android.app.appsearch.GetByUriRequest.Builder setNamespace(@NonNull String);
method @Deprecated @NonNull public android.app.appsearch.GetByUriRequest.Builder setNamespace(@NonNull String);
}
public class GlobalSearchSession implements java.io.Closeable {
@@ -242,11 +244,12 @@ package android.app.appsearch {
}
public static final class RemoveByUriRequest.Builder {
ctor public RemoveByUriRequest.Builder();
ctor @Deprecated public RemoveByUriRequest.Builder();
ctor public RemoveByUriRequest.Builder(@NonNull String);
method @NonNull public android.app.appsearch.RemoveByUriRequest.Builder addUris(@NonNull java.lang.String...);
method @NonNull public android.app.appsearch.RemoveByUriRequest.Builder addUris(@NonNull java.util.Collection<java.lang.String>);
method @NonNull public android.app.appsearch.RemoveByUriRequest build();
method @NonNull public android.app.appsearch.RemoveByUriRequest.Builder setNamespace(@NonNull String);
method @Deprecated @NonNull public android.app.appsearch.RemoveByUriRequest.Builder setNamespace(@NonNull String);
}
public final class ReportUsageRequest {
@@ -256,9 +259,10 @@ package android.app.appsearch {
}
public static final class ReportUsageRequest.Builder {
ctor public ReportUsageRequest.Builder();
ctor @Deprecated public ReportUsageRequest.Builder();
ctor public ReportUsageRequest.Builder(@NonNull String);
method @NonNull public android.app.appsearch.ReportUsageRequest build();
method @NonNull public android.app.appsearch.ReportUsageRequest.Builder setNamespace(@NonNull String);
method @Deprecated @NonNull public android.app.appsearch.ReportUsageRequest.Builder setNamespace(@NonNull String);
method @NonNull public android.app.appsearch.ReportUsageRequest.Builder setUri(@NonNull String);
method @NonNull public android.app.appsearch.ReportUsageRequest.Builder setUsageTimeMillis(long);
}

View File

@@ -152,14 +152,14 @@ public class AppSearchEmail extends GenericDocument {
/** The builder class for {@link AppSearchEmail}. */
public static class Builder extends GenericDocument.Builder<AppSearchEmail.Builder> {
/**
* Creates a new {@link AppSearchEmail.Builder}
*
* @param namespace The namespace of the Email.
* @param uri The Uri of the Email.
*/
public Builder(@NonNull String uri) {
super(uri, SCHEMA_TYPE);
public Builder(@NonNull String namespace, @NonNull String uri) {
super(namespace, uri, SCHEMA_TYPE);
}
/** Sets the from address of {@link AppSearchEmail} */

View File

@@ -46,8 +46,14 @@ import java.util.Set;
public class GenericDocument {
private static final String TAG = "AppSearchGenericDocumen";
/** The default empty namespace. */
public static final String DEFAULT_NAMESPACE = "";
/**
* The default empty namespace.
*
* <p>TODO(b/181887768): This exists only for dogfooder transition and must be removed.
*
* @deprecated This exists only for dogfooder transition and must be removed.
*/
@Deprecated public static final String DEFAULT_NAMESPACE = "";
/** The maximum number of elements in a repeatable field. */
private static final int MAX_REPEATED_PROPERTY_LENGTH = 100;
@@ -141,7 +147,7 @@ public class GenericDocument {
/** Returns the namespace of the {@link GenericDocument}. */
@NonNull
public String getNamespace() {
return mBundle.getString(NAMESPACE_FIELD, DEFAULT_NAMESPACE);
return mBundle.getString(NAMESPACE_FIELD, /*defaultValue=*/ "");
}
/** Returns the {@link AppSearchSchema} type of the {@link GenericDocument}. */
@@ -579,6 +585,9 @@ public class GenericDocument {
*
* <p>Once {@link #build} is called, the instance can no longer be used.
*
* <p>TODO(b/181887768): This method exists only for dogfooder transition and must be
* removed.
*
* @param uri the URI to set for the {@link GenericDocument}.
* @param schemaType the {@link AppSearchSchema} type of the {@link GenericDocument}. The
* provided {@code schemaType} must be defined using {@link AppSearchSession#setSchema}
@@ -586,7 +595,10 @@ public class GenericDocument {
* using {@link AppSearchSession#put}. Otherwise, the document will be rejected by
* {@link AppSearchSession#put} with result code {@link
* AppSearchResult#RESULT_NOT_FOUND}.
* @deprecated Please supply the namespace in {@link #Builder(String, String, String)}
* instead. This method exists only for dogfooder transition and must be removed.
*/
@Deprecated
@SuppressWarnings("unchecked")
public Builder(@NonNull String uri, @NonNull String schemaType) {
Preconditions.checkNotNull(uri);
@@ -603,6 +615,41 @@ public class GenericDocument {
mBundle.putBundle(PROPERTIES_FIELD, mProperties);
}
/**
* Creates a new {@link GenericDocument.Builder}.
*
* <p>Once {@link #build} is called, the instance can no longer be used.
*
* <p>URIs are unique within a namespace.
*
* <p>The number of namespaces per app should be kept small for efficiency reasons.
*
* @param namespace the namespace to set for the {@link GenericDocument}.
* @param uri the URI to set for the {@link GenericDocument}.
* @param schemaType the {@link AppSearchSchema} type of the {@link GenericDocument}. The
* provided {@code schemaType} must be defined using {@link AppSearchSession#setSchema}
* prior to inserting a document of this {@code schemaType} into the AppSearch index
* using {@link AppSearchSession#put}. Otherwise, the document will be rejected by
* {@link AppSearchSession#put} with result code {@link
* AppSearchResult#RESULT_NOT_FOUND}.
*/
@SuppressWarnings("unchecked")
public Builder(@NonNull String namespace, @NonNull String uri, @NonNull String schemaType) {
Preconditions.checkNotNull(namespace);
Preconditions.checkNotNull(uri);
Preconditions.checkNotNull(schemaType);
mBuilderTypeInstance = (BuilderType) this;
mBundle.putString(GenericDocument.NAMESPACE_FIELD, namespace);
mBundle.putString(GenericDocument.URI_FIELD, uri);
mBundle.putString(GenericDocument.SCHEMA_TYPE_FIELD, schemaType);
// Set current timestamp for creation timestamp by default.
mBundle.putLong(
GenericDocument.CREATION_TIMESTAMP_MILLIS_FIELD, System.currentTimeMillis());
mBundle.putLong(GenericDocument.TTL_MILLIS_FIELD, DEFAULT_TTL_MILLIS);
mBundle.putInt(GenericDocument.SCORE_FIELD, DEFAULT_SCORE);
mBundle.putBundle(PROPERTIES_FIELD, mProperties);
}
/**
* Sets the app-defined namespace this document resides in. No special values are reserved
* or understood by the infrastructure.
@@ -611,8 +658,14 @@ public class GenericDocument {
*
* <p>The number of namespaces per app should be kept small for efficiency reasons.
*
* <p>TODO(b/181887768): This method exists only for dogfooder transition and must be
* removed.
*
* @throws IllegalStateException if the builder has already been used.
* @deprecated Please supply the namespace in {@link #Builder(String, String, String)}
* instead. This method exists only for dogfooder transition and must be removed.
*/
@Deprecated
@NonNull
public BuilderType setNamespace(@NonNull String namespace) {
Preconditions.checkState(!mBuilt, "Builder has already been used");

View File

@@ -107,19 +107,40 @@ public final class GetByUriRequest {
* <p>Once {@link #build} is called, the instance can no longer be used.
*/
public static final class Builder {
private String mNamespace = GenericDocument.DEFAULT_NAMESPACE;
private String mNamespace;
private final Set<String> mUris = new ArraySet<>();
private final Map<String, List<String>> mProjectionTypePropertyPaths = new ArrayMap<>();
private boolean mBuilt = false;
/**
* TODO(b/181887768): This method exists only for dogfooder transition and must be removed.
*
* @deprecated Please supply the namespace in {@link #Builder(String)} instead. This method
* exists only for dogfooder transition and must be removed.
*/
@Deprecated
public Builder() {
mNamespace = GenericDocument.DEFAULT_NAMESPACE;
}
/** Creates a {@link GetByUriRequest.Builder} instance. */
public Builder(@NonNull String namespace) {
mNamespace = Preconditions.checkNotNull(namespace);
}
/**
* Sets the namespace to retrieve documents for.
*
* <p>If this is not called, the namespace defaults to {@link
* GenericDocument#DEFAULT_NAMESPACE}.
* <p>If this is not called, the namespace defaults to an empty string.
*
* <p>TODO(b/181887768): This method exists only for dogfooder transition and must be
* removed.
*
* @throws IllegalStateException if the builder has already been used.
* @deprecated Please supply the namespace in {@link #Builder(String)} instead. This method
* exists only for dogfooder transition and must
*/
@Deprecated
@NonNull
public Builder setNamespace(@NonNull String namespace) {
Preconditions.checkState(!mBuilt, "Builder has already been used");

View File

@@ -59,17 +59,39 @@ public final class RemoveByUriRequest {
* <p>Once {@link #build} is called, the instance can no longer be used.
*/
public static final class Builder {
private String mNamespace = GenericDocument.DEFAULT_NAMESPACE;
private String mNamespace;
private final Set<String> mUris = new ArraySet<>();
private boolean mBuilt = false;
/**
* TODO(b/181887768): This method exists only for dogfooder transition and must be removed.
*
* @deprecated Please supply the namespace in {@link #Builder(String)} instead. This method
* exists only for dogfooder transition and must be removed.
*/
@Deprecated
public Builder() {
mNamespace = GenericDocument.DEFAULT_NAMESPACE;
}
/** Creates a {@link RemoveByUriRequest.Builder} instance. */
public Builder(@NonNull String namespace) {
mNamespace = Preconditions.checkNotNull(namespace);
}
/**
* Sets the namespace to remove documents for.
*
* <p>If this is not set, it defaults to {@link GenericDocument#DEFAULT_NAMESPACE}.
* <p>If this is not set, it defaults to an empty string.
*
* <p>TODO(b/181887768): This method exists only for dogfooder transition and must be
* removed.
*
* @throws IllegalStateException if the builder has already been used.
* @deprecated Please supply the namespace in {@link #Builder(String)} instead. This method
* exists only for dogfooder transition and must
*/
@Deprecated
@NonNull
public Builder setNamespace(@NonNull String namespace) {
Preconditions.checkState(!mBuilt, "Builder has already been used");

View File

@@ -62,18 +62,40 @@ public final class ReportUsageRequest {
/** Builder for {@link ReportUsageRequest} objects. */
public static final class Builder {
private String mNamespace = GenericDocument.DEFAULT_NAMESPACE;
private String mNamespace;
private String mUri;
private Long mUsageTimeMillis;
private boolean mBuilt = false;
/**
* TODO(b/181887768): This method exists only for dogfooder transition and must be removed.
*
* @deprecated Please supply the namespace in {@link #Builder(String)} instead. This method
* exists only for dogfooder transition and must be removed.
*/
@Deprecated
public Builder() {
mNamespace = GenericDocument.DEFAULT_NAMESPACE;
}
/** Creates a {@link ReportUsageRequest.Builder} instance. */
public Builder(@NonNull String namespace) {
mNamespace = Preconditions.checkNotNull(namespace);
}
/**
* Sets which namespace the document being used belongs to.
*
* <p>If this is not set, it defaults to {@link GenericDocument#DEFAULT_NAMESPACE}.
* <p>If this is not set, it defaults to an empty string.
*
* <p>TODO(b/181887768): This method exists only for dogfooder transition and must be
* removed.
*
* @throws IllegalStateException if the builder has already been used
* @deprecated Please supply the namespace in {@link #Builder(String)} instead. This method
* exists only for dogfooder transition and must
*/
@Deprecated
@NonNull
public ReportUsageRequest.Builder setNamespace(@NonNull String namespace) {
Preconditions.checkState(!mBuilt, "Builder has already been used");

View File

@@ -102,8 +102,8 @@ public final class GenericDocumentToProtoConverter {
public static GenericDocument toGenericDocument(@NonNull DocumentProto proto) {
Preconditions.checkNotNull(proto);
GenericDocument.Builder<?> documentBuilder =
new GenericDocument.Builder<>(proto.getUri(), proto.getSchema())
.setNamespace(proto.getNamespace())
new GenericDocument.Builder<>(
proto.getNamespace(), proto.getUri(), proto.getSchema())
.setScore(proto.getScore())
.setTtlMillis(proto.getTtlMs())
.setCreationTimestampMillis(proto.getCreationTimestampMs());

View File

@@ -47,10 +47,7 @@ public class AppSearchTestUtils {
AppSearchBatchResult<String, GenericDocument> result =
checkIsBatchResultSuccess(
session.getByUri(
new GetByUriRequest.Builder()
.setNamespace(namespace)
.addUris(uris)
.build()));
new GetByUriRequest.Builder(namespace).addUris(uris).build()));
assertThat(result.getSuccesses()).hasSize(uris.length);
assertThat(result.getFailures()).isEmpty();
List<GenericDocument> list = new ArrayList<>(uris.length);

View File

@@ -61,7 +61,7 @@ public class AppSearchSessionUnitTest {
public void testPutDocument_throwsNullException() throws Exception {
// Create a document
AppSearchEmail inEmail =
new AppSearchEmail.Builder("uri1")
new AppSearchEmail.Builder("namespace", "uri1")
.setFrom("from@example.com")
.setTo("to1@example.com", "to2@example.com")
.setSubject("testPut example")

View File

@@ -25,7 +25,7 @@ public class AppSearchEmailTest {
@Test
public void testBuildEmailAndGetValue() {
AppSearchEmail email =
new AppSearchEmail.Builder("uri")
new AppSearchEmail.Builder("namespace", "uri")
.setFrom("FakeFromAddress")
.setCc("CC1", "CC2")
// Score and Property are mixed into the middle to make sure
@@ -37,6 +37,7 @@ public class AppSearchEmailTest {
.setBody("EmailBody")
.build();
assertThat(email.getNamespace()).isEqualTo("namespace");
assertThat(email.getUri()).isEqualTo("uri");
assertThat(email.getFrom()).isEqualTo("FakeFromAddress");
assertThat(email.getTo()).isNull();

View File

@@ -27,13 +27,13 @@ public class GenericDocumentTest {
@Test
public void testRecreateFromParcel() {
GenericDocument inDoc =
new GenericDocument.Builder<>("uri1", "schema1")
new GenericDocument.Builder<>("namespace", "uri1", "schema1")
.setScore(42)
.setPropertyString("propString", "Hello")
.setPropertyBytes("propBytes", new byte[][] {{1, 2}})
.setPropertyDocument(
"propDocument",
new GenericDocument.Builder<>("uri2", "schema2")
new GenericDocument.Builder<>("namespace", "uri2", "schema2")
.setPropertyString("propString", "Goodbye")
.setPropertyBytes("propBytes", new byte[][] {{3, 4}})
.build())

View File

@@ -31,8 +31,8 @@ public class PutDocumentsRequestTest {
public void addGenericDocument_byCollection() {
Set<AppSearchEmail> emails =
ImmutableSet.of(
new AppSearchEmail.Builder("test1").build(),
new AppSearchEmail.Builder("test2").build());
new AppSearchEmail.Builder("namespace", "test1").build(),
new AppSearchEmail.Builder("namespace", "test2").build());
PutDocumentsRequest request =
new PutDocumentsRequest.Builder().addGenericDocuments(emails).build();

View File

@@ -415,9 +415,7 @@ public class AppSearchImplTest {
+ AppSearchImpl.CHECK_OPTIMIZE_INTERVAL;
i++) {
GenericDocument document =
new GenericDocument.Builder<>("uri" + i, "type")
.setNamespace("namespace")
.build();
new GenericDocument.Builder<>("namespace", "uri" + i, "type").build();
mAppSearchImpl.putDocument("package", "database", document);
}
@@ -477,7 +475,7 @@ public class AppSearchImplTest {
// Insert document
GenericDocument document =
new GenericDocument.Builder<>("uri", "type").setNamespace("namespace").build();
new GenericDocument.Builder<>("namespace", "uri", "type").build();
mAppSearchImpl.putDocument("package", "database", document);
// Rewrite SearchSpec
@@ -517,11 +515,11 @@ public class AppSearchImplTest {
// Insert documents
GenericDocument document1 =
new GenericDocument.Builder<>("uri", "typeA").setNamespace("namespace").build();
new GenericDocument.Builder<>("namespace", "uri", "typeA").build();
mAppSearchImpl.putDocument("package", "database1", document1);
GenericDocument document2 =
new GenericDocument.Builder<>("uri", "typeB").setNamespace("namespace").build();
new GenericDocument.Builder<>("namespace", "uri", "typeB").build();
mAppSearchImpl.putDocument("package", "database2", document2);
// Rewrite SearchSpec
@@ -561,7 +559,7 @@ public class AppSearchImplTest {
// Insert document
GenericDocument document =
new GenericDocument.Builder<>("uri", "type").setNamespace("namespace").build();
new GenericDocument.Builder<>("namespace", "uri", "type").build();
mAppSearchImpl.putDocument("package", "database", document);
// If 'allowedPrefixedSchemas' is empty, this returns false since there's nothing to
@@ -614,7 +612,7 @@ public class AppSearchImplTest {
// Insert package1 document
GenericDocument document =
new GenericDocument.Builder<>("uri", "schema1").setNamespace("namespace").build();
new GenericDocument.Builder<>("namespace", "uri", "schema1").build();
mAppSearchImpl.putDocument("package1", "database1", document);
// No query filters specified, package2 shouldn't be able to query for package1's documents.
@@ -625,8 +623,7 @@ public class AppSearchImplTest {
assertThat(searchResultPage.getResults()).isEmpty();
// Insert package2 document
document =
new GenericDocument.Builder<>("uri", "schema2").setNamespace("namespace").build();
document = new GenericDocument.Builder<>("namespace", "uri", "schema2").build();
mAppSearchImpl.putDocument("package2", "database2", document);
// No query filters specified. package2 should only get its own documents back.
@@ -665,7 +662,7 @@ public class AppSearchImplTest {
// Insert package1 document
GenericDocument document =
new GenericDocument.Builder<>("uri", "schema1").setNamespace("namespace").build();
new GenericDocument.Builder<>("namespace", "uri", "schema1").build();
mAppSearchImpl.putDocument("package1", "database1", document);
// "package1" filter specified, but package2 shouldn't be able to query for package1's
@@ -680,8 +677,7 @@ public class AppSearchImplTest {
assertThat(searchResultPage.getResults()).isEmpty();
// Insert package2 document
document =
new GenericDocument.Builder<>("uri", "schema2").setNamespace("namespace").build();
document = new GenericDocument.Builder<>("namespace", "uri", "schema2").build();
mAppSearchImpl.putDocument("package2", "database2", document);
// "package2" filter specified, package2 should only get its own documents back.
@@ -1128,9 +1124,7 @@ public class AppSearchImplTest {
appSearchImpl.putDocument(
"package",
"database",
new GenericDocument.Builder<>("uri", "type")
.setNamespace("namespace")
.build());
new GenericDocument.Builder<>("namespace", "uri", "type").build());
});
expectThrows(

View File

@@ -36,23 +36,23 @@ public class GenericDocumentToProtoConverterTest {
private static final byte[] BYTE_ARRAY_2 = new byte[] {(byte) 4, (byte) 5, (byte) 6, (byte) 7};
private static final GenericDocument DOCUMENT_PROPERTIES_1 =
new GenericDocument.Builder<GenericDocument.Builder<?>>(
"sDocumentProperties1", "sDocumentPropertiesSchemaType1")
"namespace", "sDocumentProperties1", "sDocumentPropertiesSchemaType1")
.setCreationTimestampMillis(12345L)
.build();
private static final GenericDocument DOCUMENT_PROPERTIES_2 =
new GenericDocument.Builder<GenericDocument.Builder<?>>(
"sDocumentProperties2", "sDocumentPropertiesSchemaType2")
"namespace", "sDocumentProperties2", "sDocumentPropertiesSchemaType2")
.setCreationTimestampMillis(6789L)
.build();
@Test
public void testDocumentProtoConvert() {
GenericDocument document =
new GenericDocument.Builder<GenericDocument.Builder<?>>("uri1", "schemaType1")
new GenericDocument.Builder<GenericDocument.Builder<?>>(
"namespace", "uri1", "schemaType1")
.setCreationTimestampMillis(5L)
.setScore(1)
.setTtlMillis(1L)
.setNamespace("namespace")
.setPropertyLong("longKey1", 1L)
.setPropertyDouble("doubleKey1", 1.0)
.setPropertyBoolean("booleanKey1", true)