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

Commit 881a0b52 authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Refine TC request classes." into rvc-dev am: 031fe36d am: 94903244 am: 5c5425a8

Change-Id: Ife8b75ca378d666e7347c3896f9ace06750db41c
parents f8c2e8fe 5c5425a8
Loading
Loading
Loading
Loading
+12 −56
Original line number Diff line number Diff line
@@ -21,15 +21,12 @@ import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StringDef;
import android.annotation.UserIdInt;
import android.app.Person;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.text.SpannedString;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Preconditions;

import java.lang.annotation.Retention;
@@ -317,13 +314,9 @@ public final class ConversationActions implements Parcelable {
        @NonNull
        @Hint
        private final List<String> mHints;
        @Nullable
        private String mCallingPackageName;
        @UserIdInt
        private int mUserId = UserHandle.USER_NULL;
        @NonNull
        private Bundle mExtras;
        private boolean mUseDefaultTextClassifier;
        @Nullable private SystemTextClassifierMetadata mSystemTcMetadata;

        private Request(
                @NonNull List<Message> conversation,
@@ -345,10 +338,8 @@ public final class ConversationActions implements Parcelable {
            int maxSuggestions = in.readInt();
            List<String> hints = new ArrayList<>();
            in.readStringList(hints);
            String callingPackageName = in.readString();
            int userId = in.readInt();
            Bundle extras = in.readBundle();
            boolean useDefaultTextClassifier = in.readBoolean();
            SystemTextClassifierMetadata systemTcMetadata = in.readParcelable(null);

            Request request = new Request(
                    conversation,
@@ -356,9 +347,7 @@ public final class ConversationActions implements Parcelable {
                    maxSuggestions,
                    hints,
                    extras);
            request.setCallingPackageName(callingPackageName);
            request.setUserId(userId);
            request.setUseDefaultTextClassifier(useDefaultTextClassifier);
            request.setSystemTextClassifierMetadata(systemTcMetadata);
            return request;
        }

@@ -368,10 +357,8 @@ public final class ConversationActions implements Parcelable {
            parcel.writeParcelable(mTypeConfig, flags);
            parcel.writeInt(mMaxSuggestions);
            parcel.writeStringList(mHints);
            parcel.writeString(mCallingPackageName);
            parcel.writeInt(mUserId);
            parcel.writeBundle(mExtras);
            parcel.writeBoolean(mUseDefaultTextClassifier);
            parcel.writeParcelable(mSystemTcMetadata, flags);
        }

        @Override
@@ -420,63 +407,32 @@ public final class ConversationActions implements Parcelable {
            return mHints;
        }

        /**
         * Sets the name of the package that is sending this request.
         * <p>
         * Package-private for SystemTextClassifier's use.
         * @hide
         */
        @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
        public void setCallingPackageName(@Nullable String callingPackageName) {
            mCallingPackageName = callingPackageName;
        }

        /**
         * Returns the name of the package that sent this request.
         * This returns {@code null} if no calling package name is set.
         */
        @Nullable
        public String getCallingPackageName() {
            return mCallingPackageName;
            return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : null;
        }

        /**
         * Sets the id of the user that sent this request.
         * <p>
         * Package-private for SystemTextClassifier's use.
         * @hide
         */
        void setUserId(@UserIdInt int userId) {
            mUserId = userId;
        }

        /**
         * Returns the id of the user that sent this request.
         * @hide
         */
        @UserIdInt
        public int getUserId() {
            return mUserId;
        }

        /**
         * Sets whether to use the default text classifier to handle this request.
         * This will be ignored if it is not the system text classifier to handle this request.
         * Sets the information about the {@link SystemTextClassifier} that sent this request.
         *
         * @hide
         */
        void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) {
            mUseDefaultTextClassifier = useDefaultTextClassifier;
        void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcData) {
            mSystemTcMetadata = systemTcData;
        }

        /**
         * Returns whether to use the default text classifier to handle this request. This
         * will be ignored if it is not the system text classifier to handle this request.
         * Returns the information about the {@link SystemTextClassifier} that sent this request.
         *
         * @hide
         */
        public boolean getUseDefaultTextClassifier() {
            return mUseDefaultTextClassifier;
        @Nullable
        public SystemTextClassifierMetadata getSystemTextClassifierMetadata() {
            return mSystemTcMetadata;
        }

        /**
+21 −46
Original line number Diff line number Diff line
@@ -19,10 +19,8 @@ package android.view.textclassifier;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.view.textclassifier.TextClassifier.EntityType;
import android.view.textclassifier.TextClassifier.WidgetType;

@@ -129,7 +127,6 @@ public final class SelectionEvent implements Parcelable {
    private String mWidgetType = TextClassifier.WIDGET_TYPE_UNKNOWN;
    private @InvocationMethod int mInvocationMethod;
    @Nullable private String mWidgetVersion;
    private @UserIdInt int mUserId = UserHandle.USER_NULL;
    @Nullable private String mResultId;
    private long mEventTime;
    private long mDurationSinceSessionStart;
@@ -140,7 +137,7 @@ public final class SelectionEvent implements Parcelable {
    private int mEnd;
    private int mSmartStart;
    private int mSmartEnd;
    private boolean mUseDefaultTextClassifier;
    @Nullable private SystemTextClassifierMetadata mSystemTcMetadata;

    SelectionEvent(
            int start, int end,
@@ -161,6 +158,7 @@ public final class SelectionEvent implements Parcelable {
        mEventType = in.readInt();
        mEntityType = in.readString();
        mWidgetVersion = in.readInt() > 0 ? in.readString() : null;
        // TODO: remove mPackageName once aiai does not need it
        mPackageName = in.readString();
        mWidgetType = in.readString();
        mInvocationMethod = in.readInt();
@@ -175,8 +173,7 @@ public final class SelectionEvent implements Parcelable {
        mEnd = in.readInt();
        mSmartStart = in.readInt();
        mSmartEnd = in.readInt();
        mUserId = in.readInt();
        mUseDefaultTextClassifier = in.readBoolean();
        mSystemTcMetadata = in.readParcelable(null);
    }

    @Override
@@ -189,6 +186,7 @@ public final class SelectionEvent implements Parcelable {
        if (mWidgetVersion != null) {
            dest.writeString(mWidgetVersion);
        }
        // TODO: remove mPackageName once aiai does not need it
        dest.writeString(mPackageName);
        dest.writeString(mWidgetType);
        dest.writeInt(mInvocationMethod);
@@ -205,8 +203,7 @@ public final class SelectionEvent implements Parcelable {
        dest.writeInt(mEnd);
        dest.writeInt(mSmartStart);
        dest.writeInt(mSmartEnd);
        dest.writeInt(mUserId);
        dest.writeBoolean(mUseDefaultTextClassifier);
        dest.writeParcelable(mSystemTcMetadata, flags);
    }

    @Override
@@ -409,45 +406,26 @@ public final class SelectionEvent implements Parcelable {
     */
    @NonNull
    public String getPackageName() {
        return mPackageName;
        return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : "";
    }

    /**
     * Sets the id of this event's user.
     * <p>
     * Package-private for SystemTextClassifier's use.
     */
    void setUserId(@UserIdInt int userId) {
        mUserId = userId;
    }

    /**
     * Returns the id of this event's user.
     * @hide
     */
    @UserIdInt
    public int getUserId() {
        return mUserId;
    }

    /**
     * Sets whether to use the default text classifier to handle this request.
     * This will be ignored if it is not the system text classifier to handle this request.
     * Sets the information about the {@link SystemTextClassifier} that sent this request.
     *
     * @hide
     */
    void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) {
        mUseDefaultTextClassifier = useDefaultTextClassifier;
    void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcMetadata) {
        mSystemTcMetadata = systemTcMetadata;
    }

    /**
     * Returns whether to use the default text classifier to handle this request. This
     * will be ignored if it is not the system text classifier to handle this request.
     * Returns the information about the {@link SystemTextClassifier} that sent this request.
     *
     * @hide
     */
    public boolean getUseDefaultTextClassifier() {
        return mUseDefaultTextClassifier;
    @Nullable
    public SystemTextClassifierMetadata getSystemTextClassifierMetadata() {
        return mSystemTcMetadata;
    }

    /**
@@ -476,7 +454,7 @@ public final class SelectionEvent implements Parcelable {
        mPackageName = context.getPackageName();
        mWidgetType = context.getWidgetType();
        mWidgetVersion = context.getWidgetVersion();
        mUserId = context.getUserId();
        mSystemTcMetadata = context.getSystemTextClassifierMetadata();
    }

    /**
@@ -663,10 +641,9 @@ public final class SelectionEvent implements Parcelable {
    @Override
    public int hashCode() {
        return Objects.hash(mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType,
                mWidgetVersion, mPackageName, mUserId, mWidgetType, mInvocationMethod, mResultId,
                mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mResultId,
                mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent,
                mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd,
                mUseDefaultTextClassifier);
                mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mSystemTcMetadata);
    }

    @Override
@@ -685,7 +662,6 @@ public final class SelectionEvent implements Parcelable {
                && Objects.equals(mEntityType, other.mEntityType)
                && Objects.equals(mWidgetVersion, other.mWidgetVersion)
                && Objects.equals(mPackageName, other.mPackageName)
                && mUserId == other.mUserId
                && Objects.equals(mWidgetType, other.mWidgetType)
                && mInvocationMethod == other.mInvocationMethod
                && Objects.equals(mResultId, other.mResultId)
@@ -698,7 +674,7 @@ public final class SelectionEvent implements Parcelable {
                && mEnd == other.mEnd
                && mSmartStart == other.mSmartStart
                && mSmartEnd == other.mSmartEnd
                && mUseDefaultTextClassifier == other.mUseDefaultTextClassifier;
                && mSystemTcMetadata == other.mSystemTcMetadata;
    }

    @Override
@@ -706,15 +682,14 @@ public final class SelectionEvent implements Parcelable {
        return String.format(Locale.US,
                "SelectionEvent {absoluteStart=%d, absoluteEnd=%d, eventType=%d, entityType=%s, "
                        + "widgetVersion=%s, packageName=%s, widgetType=%s, invocationMethod=%s, "
                        + "userId=%d, resultId=%s, eventTime=%d, durationSinceSessionStart=%d, "
                        + "resultId=%s, eventTime=%d, durationSinceSessionStart=%d, "
                        + "durationSincePreviousEvent=%d, eventIndex=%d,"
                        + "sessionId=%s, start=%d, end=%d, smartStart=%d, smartEnd=%d, "
                        + "mUseDefaultTextClassifier=%b}",
                        + "systemTcMetadata=%s}",
                mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType,
                mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod,
                mUserId, mResultId, mEventTime, mDurationSinceSessionStart,
                mDurationSincePreviousEvent, mEventIndex,
                mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mUseDefaultTextClassifier);
                mResultId, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent,
                mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mSystemTcMetadata);
    }

    public static final @android.annotation.NonNull Creator<SelectionEvent> CREATOR = new Creator<SelectionEvent>() {
+31 −38
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package android.view.textclassifier;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.annotation.WorkerThread;
import android.content.Context;
import android.os.Bundle;
@@ -39,7 +38,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Proxy to the system's default TextClassifier.
 * proxy to the request to TextClassifierService via the TextClassificationManagerService.
 *
 * @hide
 */
@VisibleForTesting(visibility = Visibility.PACKAGE)
@@ -50,14 +50,19 @@ public final class SystemTextClassifier implements TextClassifier {
    private final ITextClassifierService mManagerService;
    private final TextClassificationConstants mSettings;
    private final TextClassifier mFallback;
    private final String mPackageName;
    // NOTE: Always set this before sending a request to the manager service otherwise the manager
    // service will throw a remote exception.
    @UserIdInt
    private final int mUserId;
    private final boolean mUseDefault;
    private TextClassificationSessionId mSessionId;
    // NOTE: Always set this before sending a request to the manager service otherwise the
    // manager service will throw a remote exception.
    @NonNull
    private final SystemTextClassifierMetadata mSystemTcMetadata;

    /**
     * Constructor of {@link SystemTextClassifier}
     *
     * @param context the context of the request.
     * @param settings TextClassifier specific settings.
     * @param useDefault whether to use the default text classifier to handle this request
     */
    public SystemTextClassifier(
            Context context,
            TextClassificationConstants settings,
@@ -66,9 +71,11 @@ public final class SystemTextClassifier implements TextClassifier {
                ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE));
        mSettings = Objects.requireNonNull(settings);
        mFallback = TextClassifier.NO_OP;
        mPackageName = Objects.requireNonNull(context.getOpPackageName());
        mUserId = context.getUserId();
        mUseDefault = useDefault;
        // NOTE: Always set this before sending a request to the manager service otherwise the
        // manager service will throw a remote exception.
        mSystemTcMetadata = new SystemTextClassifierMetadata(
                Objects.requireNonNull(context.getOpPackageName()), context.getUserId(),
                useDefault);
    }

    /**
@@ -80,9 +87,7 @@ public final class SystemTextClassifier implements TextClassifier {
        Objects.requireNonNull(request);
        Utils.checkMainThread();
        try {
            request.setCallingPackageName(mPackageName);
            request.setUserId(mUserId);
            request.setUseDefaultTextClassifier(mUseDefault);
            request.setSystemTextClassifierMetadata(mSystemTcMetadata);
            final BlockingCallback<TextSelection> callback =
                    new BlockingCallback<>("textselection");
            mManagerService.onSuggestSelection(mSessionId, request, callback);
@@ -105,9 +110,7 @@ public final class SystemTextClassifier implements TextClassifier {
        Objects.requireNonNull(request);
        Utils.checkMainThread();
        try {
            request.setCallingPackageName(mPackageName);
            request.setUserId(mUserId);
            request.setUseDefaultTextClassifier(mUseDefault);
            request.setSystemTextClassifierMetadata(mSystemTcMetadata);
            final BlockingCallback<TextClassification> callback =
                    new BlockingCallback<>("textclassification");
            mManagerService.onClassifyText(mSessionId, request, callback);
@@ -137,9 +140,7 @@ public final class SystemTextClassifier implements TextClassifier {
        }

        try {
            request.setCallingPackageName(mPackageName);
            request.setUserId(mUserId);
            request.setUseDefaultTextClassifier(mUseDefault);
            request.setSystemTextClassifierMetadata(mSystemTcMetadata);
            final BlockingCallback<TextLinks> callback =
                    new BlockingCallback<>("textlinks");
            mManagerService.onGenerateLinks(mSessionId, request, callback);
@@ -159,8 +160,7 @@ public final class SystemTextClassifier implements TextClassifier {
        Utils.checkMainThread();

        try {
            event.setUserId(mUserId);
            event.setUseDefaultTextClassifier(mUseDefault);
            event.setSystemTextClassifierMetadata(mSystemTcMetadata);
            mManagerService.onSelectionEvent(mSessionId, event);
        } catch (RemoteException e) {
            Log.e(LOG_TAG, "Error reporting selection event.", e);
@@ -173,12 +173,11 @@ public final class SystemTextClassifier implements TextClassifier {
        Utils.checkMainThread();

        try {
            final TextClassificationContext tcContext = event.getEventContext() == null
                    ? new TextClassificationContext.Builder(mPackageName, WIDGET_TYPE_UNKNOWN)
                            .build()
            final TextClassificationContext tcContext =
                    event.getEventContext() == null ? new TextClassificationContext.Builder(
                            mSystemTcMetadata.getCallingPackageName(), WIDGET_TYPE_UNKNOWN).build()
                            : event.getEventContext();
            tcContext.setUserId(mUserId);
            tcContext.setUseDefaultTextClassifier(mUseDefault);
            tcContext.setSystemTextClassifierMetadata(mSystemTcMetadata);
            event.setEventContext(tcContext);
            mManagerService.onTextClassifierEvent(mSessionId, event);
        } catch (RemoteException e) {
@@ -192,9 +191,7 @@ public final class SystemTextClassifier implements TextClassifier {
        Utils.checkMainThread();

        try {
            request.setCallingPackageName(mPackageName);
            request.setUserId(mUserId);
            request.setUseDefaultTextClassifier(mUseDefault);
            request.setSystemTextClassifierMetadata(mSystemTcMetadata);
            final BlockingCallback<TextLanguage> callback =
                    new BlockingCallback<>("textlanguage");
            mManagerService.onDetectLanguage(mSessionId, request, callback);
@@ -214,9 +211,7 @@ public final class SystemTextClassifier implements TextClassifier {
        Utils.checkMainThread();

        try {
            request.setCallingPackageName(mPackageName);
            request.setUserId(mUserId);
            request.setUseDefaultTextClassifier(mUseDefault);
            request.setSystemTextClassifierMetadata(mSystemTcMetadata);
            final BlockingCallback<ConversationActions> callback =
                    new BlockingCallback<>("conversation-actions");
            mManagerService.onSuggestConversationActions(mSessionId, request, callback);
@@ -256,10 +251,8 @@ public final class SystemTextClassifier implements TextClassifier {
        printWriter.println("SystemTextClassifier:");
        printWriter.increaseIndent();
        printWriter.printPair("mFallback", mFallback);
        printWriter.printPair("mPackageName", mPackageName);
        printWriter.printPair("mSessionId", mSessionId);
        printWriter.printPair("mUserId", mUserId);
        printWriter.printPair("mUseDefault",  mUseDefault);
        printWriter.printPair("mSystemTcMetadata",  mSystemTcMetadata);
        printWriter.decreaseIndent();
        printWriter.println();
    }
@@ -275,7 +268,7 @@ public final class SystemTextClassifier implements TextClassifier {
            @NonNull TextClassificationSessionId sessionId) {
        mSessionId = Objects.requireNonNull(sessionId);
        try {
            classificationContext.setUserId(mUserId);
            classificationContext.setSystemTextClassifierMetadata(mSystemTcMetadata);
            mManagerService.onCreateTextClassificationSession(classificationContext, mSessionId);
        } catch (RemoteException e) {
            Log.e(LOG_TAG, "Error starting a new classification session.", e);
+19 −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.view.textclassifier;

parcelable SystemTextClassifierMetadata;
 No newline at end of file
+121 −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.view.textclassifier;

import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting.Visibility;

import java.util.Locale;
import java.util.Objects;

/**
 * SystemTextClassifier specific information.
 * <p>
 * This contains information requires for the TextClassificationManagerService to process the
 * requests from the application, e.g. user id, calling package name and etc. Centrialize the data
 * into this class helps to extend the scalability if we want to add new fields.
 * @hide
 */
@VisibleForTesting(visibility = Visibility.PACKAGE)
public final class SystemTextClassifierMetadata implements Parcelable {

    /* The name of the package that sent the TC request. */
    @NonNull
    private final String mCallingPackageName;
    /* The id of the user that sent the TC request. */
    @UserIdInt
    private final int mUserId;
    /* Whether to use the default text classifier to handle the request. */
    private final boolean mUseDefaultTextClassifier;

    public SystemTextClassifierMetadata(@NonNull String packageName, @UserIdInt int userId,
            boolean useDefaultTextClassifier) {
        Objects.requireNonNull(packageName);
        mCallingPackageName = packageName;
        mUserId = userId;
        mUseDefaultTextClassifier = useDefaultTextClassifier;
    }

    /**
     * Returns the id of the user that sent the TC request.
     */
    @UserIdInt
    public int getUserId() {
        return mUserId;
    }

    /**
     * Returns the name of the package that sent the TC request.
     * This returns {@code null} if no calling package name is set.
     */
    @NonNull
    public String getCallingPackageName() {
        return mCallingPackageName;
    }

    /**
     * Returns whether to use the default text classifier to handle TC request.
     */
    public boolean useDefaultTextClassifier() {
        return mUseDefaultTextClassifier;
    }

    @Override
    public String toString() {
        return String.format(Locale.US,
                "SystemTextClassifierMetadata {callingPackageName=%s, userId=%d, "
                        + "useDefaultTextClassifier=%b}",
                mCallingPackageName, mUserId, mUseDefaultTextClassifier);
    }

    private static SystemTextClassifierMetadata readFromParcel(Parcel in) {
        final String packageName = in.readString();
        final int userId = in.readInt();
        final boolean useDefaultTextClassifier = in.readBoolean();
        return new SystemTextClassifierMetadata(packageName, userId, useDefaultTextClassifier);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mCallingPackageName);
        dest.writeInt(mUserId);
        dest.writeBoolean(mUseDefaultTextClassifier);
    }

    public static final @NonNull Creator<SystemTextClassifierMetadata> CREATOR =
            new Creator<SystemTextClassifierMetadata>() {
        @Override
        public SystemTextClassifierMetadata createFromParcel(Parcel in) {
            return readFromParcel(in);
        }

        @Override
        public SystemTextClassifierMetadata[] newArray(int size) {
            return new SystemTextClassifierMetadata[size];
        }
    };
}
Loading