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

Commit 49df6f1b authored by Terry Wang's avatar Terry Wang Committed by Android (Google) Code Review
Browse files

Merge changes from topic "Refactor-AppSearch"

* changes:
  Adds support for index Document properties in AppSearch.Document.
  Adds ttl and support for index byte array in AppSearch.Document.
  Refactor AppSearch to split into AppSearchDocument and AppSearchEmail.
parents b13ce8b6 8eb86e8e
Loading
Loading
Loading
Loading
+0 −854

File deleted.

Preview size limit exceeded, changes collapsed.

+724 −0

File added.

Preview size limit exceeded, changes collapsed.

+255 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.app.appsearch;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.appsearch.AppSearchSchema.PropertyConfig;

/**
 * Encapsulates a {@link AppSearchDocument} that represent an email.
 *
 * <p>This class is a higher level implement of {@link AppSearchDocument}.
 *
 * <p>This class will eventually migrate to Jetpack, where it will become public API.
 *
 * @hide
 */
public class AppSearchEmail extends AppSearchDocument {
    private static final String KEY_FROM = "from";
    private static final String KEY_TO = "to";
    private static final String KEY_CC = "cc";
    private static final String KEY_BCC = "bcc";
    private static final String KEY_SUBJECT = "subject";
    private static final String KEY_BODY = "body";

    /** The name of the schema type for {@link AppSearchEmail} documents.*/
    public static final String SCHEMA_TYPE = "builtin:Email";

    public static final AppSearchSchema SCHEMA = AppSearchSchema.newBuilder(SCHEMA_TYPE)
            .addProperty(AppSearchSchema.newPropertyBuilder(KEY_FROM)
                    .setDataType(PropertyConfig.DATA_TYPE_STRING)
                    .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
                    .setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)
                    .setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)
                    .build()

            ).addProperty(AppSearchSchema.newPropertyBuilder(KEY_TO)
                    .setDataType(PropertyConfig.DATA_TYPE_STRING)
                    .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
                    .setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)
                    .setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)
                    .build()

            ).addProperty(AppSearchSchema.newPropertyBuilder(KEY_CC)
                    .setDataType(PropertyConfig.DATA_TYPE_STRING)
                    .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
                    .setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)
                    .setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)
                    .build()

            ).addProperty(AppSearchSchema.newPropertyBuilder(KEY_BCC)
                    .setDataType(PropertyConfig.DATA_TYPE_STRING)
                    .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
                    .setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)
                    .setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)
                    .build()

            ).addProperty(AppSearchSchema.newPropertyBuilder(KEY_SUBJECT)
                    .setDataType(PropertyConfig.DATA_TYPE_STRING)
                    .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
                    .setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)
                    .setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)
                    .build()

            ).addProperty(AppSearchSchema.newPropertyBuilder(KEY_BODY)
                    .setDataType(PropertyConfig.DATA_TYPE_STRING)
                    .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
                    .setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)
                    .setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)
                    .build()

            ).build();

    /**
     * Creates a new {@link AppSearchEmail} from the contents of an existing
     * {@link AppSearchDocument}.
     *
     * @param document The {@link AppSearchDocument} containing the email content.
     */
    public AppSearchEmail(@NonNull AppSearchDocument document) {
        super(document);
    }

    /**
     * Get the from address of {@link AppSearchEmail}.
     *
     * @return Returns the subject of {@link AppSearchEmail} or {@code null} if it's not been set
     *         yet.
     * @hide
     */
    @Nullable
    public String getFrom() {
        return getPropertyString(KEY_FROM);
    }

    /**
     * Get the destination addresses of {@link AppSearchEmail}.
     *
     * @return Returns the destination addresses of {@link AppSearchEmail} or {@code null} if it's
     *         not been set yet.
     * @hide
     */
    @Nullable
    public String[] getTo() {
        return getPropertyStringArray(KEY_TO);
    }

    /**
     * Get the CC list of {@link AppSearchEmail}.
     *
     * @return Returns the CC list of {@link AppSearchEmail} or {@code null} if it's not been set
     *         yet.
     * @hide
     */
    @Nullable
    public String[] getCc() {
        return getPropertyStringArray(KEY_CC);
    }

    /**
     * Get the BCC list of {@link AppSearchEmail}.
     *
     * @return Returns the BCC list of {@link AppSearchEmail} or {@code null} if it's not been set
     *         yet.
     * @hide
     */
    @Nullable
    public String[] getBcc() {
        return getPropertyStringArray(KEY_BCC);
    }

    /**
     * Get the subject of {@link AppSearchEmail}.
     *
     * @return Returns the value subject of {@link AppSearchEmail} or {@code null} if it's not been
     *         set yet.
     * @hide
     */
    @Nullable
    public String getSubject() {
        return getPropertyString(KEY_SUBJECT);
    }

    /**
     * Get the body of {@link AppSearchEmail}.
     *
     * @return Returns the body of {@link AppSearchEmail} or {@code null} if it's not been set yet.
     * @hide
     */
    @Nullable
    public String getBody() {
        return getPropertyString(KEY_BODY);
    }

    /**
     * The builder class for {@link AppSearchEmail}.
     * @hide
     */
    public static class Builder extends AppSearchDocument.Builder<AppSearchEmail.Builder> {

        /**
         * Create a new {@link AppSearchEmail.Builder}
         * @param uri The Uri of the Email.
         * @hide
         */
        public Builder(@NonNull String uri) {
            super(uri, SCHEMA_TYPE);
        }

        /**
         * Set the from address of {@link AppSearchEmail}
         * @hide
         */
        @NonNull
        public AppSearchEmail.Builder setFrom(@NonNull String from) {
            setProperty(KEY_FROM, from);
            return this;
        }

        /**
         * Set the destination address of {@link AppSearchEmail}
         * @hide
         */
        @NonNull
        public AppSearchEmail.Builder setTo(@NonNull String... to) {
            setProperty(KEY_TO, to);
            return this;
        }

        /**
         * Set the CC list of {@link AppSearchEmail}
         * @hide
         */
        @NonNull
        public AppSearchEmail.Builder setCc(@NonNull String... cc) {
            setProperty(KEY_CC, cc);
            return this;
        }

        /**
         * Set the BCC list of {@link AppSearchEmail}
         * @hide
         */
        @NonNull
        public AppSearchEmail.Builder setBcc(@NonNull String... bcc) {
            setProperty(KEY_BCC, bcc);
            return this;
        }

        /**
         * Set the subject of {@link AppSearchEmail}
         * @hide
         */
        @NonNull
        public AppSearchEmail.Builder setSubject(@NonNull String subject) {
            setProperty(KEY_SUBJECT, subject);
            return this;
        }

        /**
         * Set the body of {@link AppSearchEmail}
         * @hide
         */
        @NonNull
        public AppSearchEmail.Builder setBody(@NonNull String body) {
            setProperty(KEY_BODY, body);
            return this;
        }

        /**
         * Builds the {@link AppSearchEmail} object.
         *
         * @hide
         */
        @NonNull
        @Override
        public AppSearchEmail build() {
            return new AppSearchEmail(super.build());
        }
    }
}
+6 −6
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package android.app.appsearch;
import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
import android.annotation.SystemService;
import android.app.appsearch.AppSearch.Document;
import android.content.Context;
import android.os.RemoteException;

@@ -150,25 +149,26 @@ public class AppSearchManager {
    }

    /**
     * Index {@link android.app.appsearch.AppSearch.Document Documents} into AppSearch.
     * Index {@link AppSearchDocument Documents} into AppSearch.
     *
     * <p>You should not call this method directly; instead, use the
     * {@code AppSearch#putDocuments()} API provided by JetPack.
     *
     * <p>Each {@link AppSearch.Document Document's} {@code schemaType} field must be set to the
     * <p>Each {@link AppSearchDocument Document's} {@code schemaType} field must be set to the
     * name of a schema type previously registered via the {@link #setSchema} method.
     *
     * @param documents {@link Document Documents} that need to be indexed.
     * @param documents {@link AppSearchDocument Documents} that need to be indexed.
     * @return An {@link AppSearchBatchResult} mapping the document URIs to {@link Void} if they
     *     were successfully indexed, or a {@link Throwable} describing the failure if they could
     *     not be indexed.
     * @hide
     */
    public AppSearchBatchResult<String, Void> putDocuments(@NonNull List<Document> documents) {
    public AppSearchBatchResult<String, Void> putDocuments(
            @NonNull List<AppSearchDocument> documents) {
        // TODO(b/146386470): Transmit these documents as a RemoteStream instead of sending them in
        // one big list.
        List<byte[]> documentsBytes = new ArrayList<>(documents.size());
        for (Document document : documents) {
        for (AppSearchDocument document : documents) {
            documentsBytes.add(document.getProto().toByteArray());
        }
        AndroidFuture<AppSearchBatchResult> future = new AndroidFuture<>();
+2 −2
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ public final class MatchInfo {

    private final String mPropertyPath;
    private final SnippetMatchProto mSnippetMatch;
    private final AppSearch.Document mDocument;
    private final AppSearchDocument mDocument;
    /**
     * List of content with same property path in a document when there are multiple matches in
     * repeated sections.
@@ -79,7 +79,7 @@ public final class MatchInfo {

    /** @hide */
    public MatchInfo(@NonNull String propertyPath, @NonNull SnippetMatchProto snippetMatch,
            @NonNull AppSearch.Document document) {
            @NonNull AppSearchDocument document) {
        mPropertyPath = propertyPath;
        mSnippetMatch = snippetMatch;
        mDocument = document;
Loading