Loading core/java/android/view/textclassifier/ConversationActions.java +12 −56 Original line number Original line Diff line number Diff line Loading @@ -21,15 +21,12 @@ import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.StringDef; import android.annotation.StringDef; import android.annotation.UserIdInt; import android.app.Person; import android.app.Person; import android.os.Bundle; import android.os.Bundle; import android.os.Parcel; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable; import android.os.UserHandle; import android.text.SpannedString; import android.text.SpannedString; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.Preconditions; import com.android.internal.util.Preconditions; import java.lang.annotation.Retention; import java.lang.annotation.Retention; Loading Loading @@ -317,13 +314,9 @@ public final class ConversationActions implements Parcelable { @NonNull @NonNull @Hint @Hint private final List<String> mHints; private final List<String> mHints; @Nullable private String mCallingPackageName; @UserIdInt private int mUserId = UserHandle.USER_NULL; @NonNull @NonNull private Bundle mExtras; private Bundle mExtras; private boolean mUseDefaultTextClassifier; @Nullable private SystemTextClassifierMetadata mSystemTcMetadata; private Request( private Request( @NonNull List<Message> conversation, @NonNull List<Message> conversation, Loading @@ -345,10 +338,8 @@ public final class ConversationActions implements Parcelable { int maxSuggestions = in.readInt(); int maxSuggestions = in.readInt(); List<String> hints = new ArrayList<>(); List<String> hints = new ArrayList<>(); in.readStringList(hints); in.readStringList(hints); String callingPackageName = in.readString(); int userId = in.readInt(); Bundle extras = in.readBundle(); Bundle extras = in.readBundle(); boolean useDefaultTextClassifier = in.readBoolean(); SystemTextClassifierMetadata systemTcMetadata = in.readParcelable(null); Request request = new Request( Request request = new Request( conversation, conversation, Loading @@ -356,9 +347,7 @@ public final class ConversationActions implements Parcelable { maxSuggestions, maxSuggestions, hints, hints, extras); extras); request.setCallingPackageName(callingPackageName); request.setSystemTextClassifierMetadata(systemTcMetadata); request.setUserId(userId); request.setUseDefaultTextClassifier(useDefaultTextClassifier); return request; return request; } } Loading @@ -368,10 +357,8 @@ public final class ConversationActions implements Parcelable { parcel.writeParcelable(mTypeConfig, flags); parcel.writeParcelable(mTypeConfig, flags); parcel.writeInt(mMaxSuggestions); parcel.writeInt(mMaxSuggestions); parcel.writeStringList(mHints); parcel.writeStringList(mHints); parcel.writeString(mCallingPackageName); parcel.writeInt(mUserId); parcel.writeBundle(mExtras); parcel.writeBundle(mExtras); parcel.writeBoolean(mUseDefaultTextClassifier); parcel.writeParcelable(mSystemTcMetadata, flags); } } @Override @Override Loading Loading @@ -420,63 +407,32 @@ public final class ConversationActions implements Parcelable { return mHints; 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. * Returns the name of the package that sent this request. * This returns {@code null} if no calling package name is set. * This returns {@code null} if no calling package name is set. */ */ @Nullable @Nullable public String getCallingPackageName() { public String getCallingPackageName() { return mCallingPackageName; return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : null; } } /** /** * Sets the id of the user that sent this request. * Sets the information about the {@link SystemTextClassifier} 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. * * * @hide * @hide */ */ void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcData) { mUseDefaultTextClassifier = useDefaultTextClassifier; mSystemTcMetadata = systemTcData; } } /** /** * Returns whether to use the default text classifier to handle this request. This * Returns the information about the {@link SystemTextClassifier} that sent this request. * will be ignored if it is not the system text classifier to handle this request. * * * @hide * @hide */ */ public boolean getUseDefaultTextClassifier() { @Nullable return mUseDefaultTextClassifier; public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { return mSystemTcMetadata; } } /** /** Loading core/java/android/view/textclassifier/SelectionEvent.java +21 −46 Original line number Original line Diff line number Diff line Loading @@ -19,10 +19,8 @@ package android.view.textclassifier; import android.annotation.IntDef; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.UserIdInt; import android.os.Parcel; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable; import android.os.UserHandle; import android.view.textclassifier.TextClassifier.EntityType; import android.view.textclassifier.TextClassifier.EntityType; import android.view.textclassifier.TextClassifier.WidgetType; import android.view.textclassifier.TextClassifier.WidgetType; Loading Loading @@ -129,7 +127,6 @@ public final class SelectionEvent implements Parcelable { private String mWidgetType = TextClassifier.WIDGET_TYPE_UNKNOWN; private String mWidgetType = TextClassifier.WIDGET_TYPE_UNKNOWN; private @InvocationMethod int mInvocationMethod; private @InvocationMethod int mInvocationMethod; @Nullable private String mWidgetVersion; @Nullable private String mWidgetVersion; private @UserIdInt int mUserId = UserHandle.USER_NULL; @Nullable private String mResultId; @Nullable private String mResultId; private long mEventTime; private long mEventTime; private long mDurationSinceSessionStart; private long mDurationSinceSessionStart; Loading @@ -140,7 +137,7 @@ public final class SelectionEvent implements Parcelable { private int mEnd; private int mEnd; private int mSmartStart; private int mSmartStart; private int mSmartEnd; private int mSmartEnd; private boolean mUseDefaultTextClassifier; @Nullable private SystemTextClassifierMetadata mSystemTcMetadata; SelectionEvent( SelectionEvent( int start, int end, int start, int end, Loading @@ -161,6 +158,7 @@ public final class SelectionEvent implements Parcelable { mEventType = in.readInt(); mEventType = in.readInt(); mEntityType = in.readString(); mEntityType = in.readString(); mWidgetVersion = in.readInt() > 0 ? in.readString() : null; mWidgetVersion = in.readInt() > 0 ? in.readString() : null; // TODO: remove mPackageName once aiai does not need it mPackageName = in.readString(); mPackageName = in.readString(); mWidgetType = in.readString(); mWidgetType = in.readString(); mInvocationMethod = in.readInt(); mInvocationMethod = in.readInt(); Loading @@ -175,8 +173,7 @@ public final class SelectionEvent implements Parcelable { mEnd = in.readInt(); mEnd = in.readInt(); mSmartStart = in.readInt(); mSmartStart = in.readInt(); mSmartEnd = in.readInt(); mSmartEnd = in.readInt(); mUserId = in.readInt(); mSystemTcMetadata = in.readParcelable(null); mUseDefaultTextClassifier = in.readBoolean(); } } @Override @Override Loading @@ -189,6 +186,7 @@ public final class SelectionEvent implements Parcelable { if (mWidgetVersion != null) { if (mWidgetVersion != null) { dest.writeString(mWidgetVersion); dest.writeString(mWidgetVersion); } } // TODO: remove mPackageName once aiai does not need it dest.writeString(mPackageName); dest.writeString(mPackageName); dest.writeString(mWidgetType); dest.writeString(mWidgetType); dest.writeInt(mInvocationMethod); dest.writeInt(mInvocationMethod); Loading @@ -205,8 +203,7 @@ public final class SelectionEvent implements Parcelable { dest.writeInt(mEnd); dest.writeInt(mEnd); dest.writeInt(mSmartStart); dest.writeInt(mSmartStart); dest.writeInt(mSmartEnd); dest.writeInt(mSmartEnd); dest.writeInt(mUserId); dest.writeParcelable(mSystemTcMetadata, flags); dest.writeBoolean(mUseDefaultTextClassifier); } } @Override @Override Loading Loading @@ -409,45 +406,26 @@ public final class SelectionEvent implements Parcelable { */ */ @NonNull @NonNull public String getPackageName() { public String getPackageName() { return mPackageName; return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : ""; } } /** /** * Sets the id of this event's user. * Sets the information about the {@link SystemTextClassifier} that sent this request. * <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. * * * @hide * @hide */ */ void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcMetadata) { mUseDefaultTextClassifier = useDefaultTextClassifier; mSystemTcMetadata = systemTcMetadata; } } /** /** * Returns whether to use the default text classifier to handle this request. This * Returns the information about the {@link SystemTextClassifier} that sent this request. * will be ignored if it is not the system text classifier to handle this request. * * * @hide * @hide */ */ public boolean getUseDefaultTextClassifier() { @Nullable return mUseDefaultTextClassifier; public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { return mSystemTcMetadata; } } /** /** Loading Loading @@ -476,7 +454,7 @@ public final class SelectionEvent implements Parcelable { mPackageName = context.getPackageName(); mPackageName = context.getPackageName(); mWidgetType = context.getWidgetType(); mWidgetType = context.getWidgetType(); mWidgetVersion = context.getWidgetVersion(); mWidgetVersion = context.getWidgetVersion(); mUserId = context.getUserId(); mSystemTcMetadata = context.getSystemTextClassifierMetadata(); } } /** /** Loading Loading @@ -663,10 +641,9 @@ public final class SelectionEvent implements Parcelable { @Override @Override public int hashCode() { public int hashCode() { return Objects.hash(mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, return Objects.hash(mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mWidgetVersion, mPackageName, mUserId, mWidgetType, mInvocationMethod, mResultId, mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mResultId, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mSystemTcMetadata); mUseDefaultTextClassifier); } } @Override @Override Loading @@ -685,7 +662,6 @@ public final class SelectionEvent implements Parcelable { && Objects.equals(mEntityType, other.mEntityType) && Objects.equals(mEntityType, other.mEntityType) && Objects.equals(mWidgetVersion, other.mWidgetVersion) && Objects.equals(mWidgetVersion, other.mWidgetVersion) && Objects.equals(mPackageName, other.mPackageName) && Objects.equals(mPackageName, other.mPackageName) && mUserId == other.mUserId && Objects.equals(mWidgetType, other.mWidgetType) && Objects.equals(mWidgetType, other.mWidgetType) && mInvocationMethod == other.mInvocationMethod && mInvocationMethod == other.mInvocationMethod && Objects.equals(mResultId, other.mResultId) && Objects.equals(mResultId, other.mResultId) Loading @@ -698,7 +674,7 @@ public final class SelectionEvent implements Parcelable { && mEnd == other.mEnd && mEnd == other.mEnd && mSmartStart == other.mSmartStart && mSmartStart == other.mSmartStart && mSmartEnd == other.mSmartEnd && mSmartEnd == other.mSmartEnd && mUseDefaultTextClassifier == other.mUseDefaultTextClassifier; && mSystemTcMetadata == other.mSystemTcMetadata; } } @Override @Override Loading @@ -706,15 +682,14 @@ public final class SelectionEvent implements Parcelable { return String.format(Locale.US, return String.format(Locale.US, "SelectionEvent {absoluteStart=%d, absoluteEnd=%d, eventType=%d, entityType=%s, " "SelectionEvent {absoluteStart=%d, absoluteEnd=%d, eventType=%d, entityType=%s, " + "widgetVersion=%s, packageName=%s, widgetType=%s, invocationMethod=%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," + "durationSincePreviousEvent=%d, eventIndex=%d," + "sessionId=%s, start=%d, end=%d, smartStart=%d, smartEnd=%d, " + "sessionId=%s, start=%d, end=%d, smartStart=%d, smartEnd=%d, " + "mUseDefaultTextClassifier=%b}", + "systemTcMetadata=%s}", mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mUserId, mResultId, mEventTime, mDurationSinceSessionStart, mResultId, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, mDurationSincePreviousEvent, mEventIndex, mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mSystemTcMetadata); mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mUseDefaultTextClassifier); } } public static final @android.annotation.NonNull Creator<SelectionEvent> CREATOR = new Creator<SelectionEvent>() { public static final @android.annotation.NonNull Creator<SelectionEvent> CREATOR = new Creator<SelectionEvent>() { Loading core/java/android/view/textclassifier/SystemTextClassifier.java +31 −38 Original line number Original line Diff line number Diff line Loading @@ -18,7 +18,6 @@ package android.view.textclassifier; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.UserIdInt; import android.annotation.WorkerThread; import android.annotation.WorkerThread; import android.content.Context; import android.content.Context; import android.os.Bundle; import android.os.Bundle; Loading @@ -39,7 +38,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit; /** /** * Proxy to the system's default TextClassifier. * proxy to the request to TextClassifierService via the TextClassificationManagerService. * * @hide * @hide */ */ @VisibleForTesting(visibility = Visibility.PACKAGE) @VisibleForTesting(visibility = Visibility.PACKAGE) Loading @@ -50,14 +50,19 @@ public final class SystemTextClassifier implements TextClassifier { private final ITextClassifierService mManagerService; private final ITextClassifierService mManagerService; private final TextClassificationConstants mSettings; private final TextClassificationConstants mSettings; private final TextClassifier mFallback; 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; 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( public SystemTextClassifier( Context context, Context context, TextClassificationConstants settings, TextClassificationConstants settings, Loading @@ -66,9 +71,11 @@ public final class SystemTextClassifier implements TextClassifier { ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE)); ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE)); mSettings = Objects.requireNonNull(settings); mSettings = Objects.requireNonNull(settings); mFallback = TextClassifier.NO_OP; mFallback = TextClassifier.NO_OP; mPackageName = Objects.requireNonNull(context.getOpPackageName()); // NOTE: Always set this before sending a request to the manager service otherwise the mUserId = context.getUserId(); // manager service will throw a remote exception. mUseDefault = useDefault; mSystemTcMetadata = new SystemTextClassifierMetadata( Objects.requireNonNull(context.getOpPackageName()), context.getUserId(), useDefault); } } /** /** Loading @@ -80,9 +87,7 @@ public final class SystemTextClassifier implements TextClassifier { Objects.requireNonNull(request); Objects.requireNonNull(request); Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextSelection> callback = final BlockingCallback<TextSelection> callback = new BlockingCallback<>("textselection"); new BlockingCallback<>("textselection"); mManagerService.onSuggestSelection(mSessionId, request, callback); mManagerService.onSuggestSelection(mSessionId, request, callback); Loading @@ -105,9 +110,7 @@ public final class SystemTextClassifier implements TextClassifier { Objects.requireNonNull(request); Objects.requireNonNull(request); Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextClassification> callback = final BlockingCallback<TextClassification> callback = new BlockingCallback<>("textclassification"); new BlockingCallback<>("textclassification"); mManagerService.onClassifyText(mSessionId, request, callback); mManagerService.onClassifyText(mSessionId, request, callback); Loading Loading @@ -137,9 +140,7 @@ public final class SystemTextClassifier implements TextClassifier { } } try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextLinks> callback = final BlockingCallback<TextLinks> callback = new BlockingCallback<>("textlinks"); new BlockingCallback<>("textlinks"); mManagerService.onGenerateLinks(mSessionId, request, callback); mManagerService.onGenerateLinks(mSessionId, request, callback); Loading @@ -159,8 +160,7 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { event.setUserId(mUserId); event.setSystemTextClassifierMetadata(mSystemTcMetadata); event.setUseDefaultTextClassifier(mUseDefault); mManagerService.onSelectionEvent(mSessionId, event); mManagerService.onSelectionEvent(mSessionId, event); } catch (RemoteException e) { } catch (RemoteException e) { Log.e(LOG_TAG, "Error reporting selection event.", e); Log.e(LOG_TAG, "Error reporting selection event.", e); Loading @@ -173,12 +173,11 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { final TextClassificationContext tcContext = event.getEventContext() == null final TextClassificationContext tcContext = ? new TextClassificationContext.Builder(mPackageName, WIDGET_TYPE_UNKNOWN) event.getEventContext() == null ? new TextClassificationContext.Builder( .build() mSystemTcMetadata.getCallingPackageName(), WIDGET_TYPE_UNKNOWN).build() : event.getEventContext(); : event.getEventContext(); tcContext.setUserId(mUserId); tcContext.setSystemTextClassifierMetadata(mSystemTcMetadata); tcContext.setUseDefaultTextClassifier(mUseDefault); event.setEventContext(tcContext); event.setEventContext(tcContext); mManagerService.onTextClassifierEvent(mSessionId, event); mManagerService.onTextClassifierEvent(mSessionId, event); } catch (RemoteException e) { } catch (RemoteException e) { Loading @@ -192,9 +191,7 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextLanguage> callback = final BlockingCallback<TextLanguage> callback = new BlockingCallback<>("textlanguage"); new BlockingCallback<>("textlanguage"); mManagerService.onDetectLanguage(mSessionId, request, callback); mManagerService.onDetectLanguage(mSessionId, request, callback); Loading @@ -214,9 +211,7 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<ConversationActions> callback = final BlockingCallback<ConversationActions> callback = new BlockingCallback<>("conversation-actions"); new BlockingCallback<>("conversation-actions"); mManagerService.onSuggestConversationActions(mSessionId, request, callback); mManagerService.onSuggestConversationActions(mSessionId, request, callback); Loading Loading @@ -256,10 +251,8 @@ public final class SystemTextClassifier implements TextClassifier { printWriter.println("SystemTextClassifier:"); printWriter.println("SystemTextClassifier:"); printWriter.increaseIndent(); printWriter.increaseIndent(); printWriter.printPair("mFallback", mFallback); printWriter.printPair("mFallback", mFallback); printWriter.printPair("mPackageName", mPackageName); printWriter.printPair("mSessionId", mSessionId); printWriter.printPair("mSessionId", mSessionId); printWriter.printPair("mUserId", mUserId); printWriter.printPair("mSystemTcMetadata", mSystemTcMetadata); printWriter.printPair("mUseDefault", mUseDefault); printWriter.decreaseIndent(); printWriter.decreaseIndent(); printWriter.println(); printWriter.println(); } } Loading @@ -275,7 +268,7 @@ public final class SystemTextClassifier implements TextClassifier { @NonNull TextClassificationSessionId sessionId) { @NonNull TextClassificationSessionId sessionId) { mSessionId = Objects.requireNonNull(sessionId); mSessionId = Objects.requireNonNull(sessionId); try { try { classificationContext.setUserId(mUserId); classificationContext.setSystemTextClassifierMetadata(mSystemTcMetadata); mManagerService.onCreateTextClassificationSession(classificationContext, mSessionId); mManagerService.onCreateTextClassificationSession(classificationContext, mSessionId); } catch (RemoteException e) { } catch (RemoteException e) { Log.e(LOG_TAG, "Error starting a new classification session.", e); Log.e(LOG_TAG, "Error starting a new classification session.", e); Loading core/java/android/view/textclassifier/SystemTextClassifierMetadata.aidl 0 → 100644 +19 −0 Original line number Original line 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 core/java/android/view/textclassifier/SystemTextClassifierMetadata.java 0 → 100644 +121 −0 Original line number Original line 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
core/java/android/view/textclassifier/ConversationActions.java +12 −56 Original line number Original line Diff line number Diff line Loading @@ -21,15 +21,12 @@ import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.StringDef; import android.annotation.StringDef; import android.annotation.UserIdInt; import android.app.Person; import android.app.Person; import android.os.Bundle; import android.os.Bundle; import android.os.Parcel; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable; import android.os.UserHandle; import android.text.SpannedString; import android.text.SpannedString; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.Preconditions; import com.android.internal.util.Preconditions; import java.lang.annotation.Retention; import java.lang.annotation.Retention; Loading Loading @@ -317,13 +314,9 @@ public final class ConversationActions implements Parcelable { @NonNull @NonNull @Hint @Hint private final List<String> mHints; private final List<String> mHints; @Nullable private String mCallingPackageName; @UserIdInt private int mUserId = UserHandle.USER_NULL; @NonNull @NonNull private Bundle mExtras; private Bundle mExtras; private boolean mUseDefaultTextClassifier; @Nullable private SystemTextClassifierMetadata mSystemTcMetadata; private Request( private Request( @NonNull List<Message> conversation, @NonNull List<Message> conversation, Loading @@ -345,10 +338,8 @@ public final class ConversationActions implements Parcelable { int maxSuggestions = in.readInt(); int maxSuggestions = in.readInt(); List<String> hints = new ArrayList<>(); List<String> hints = new ArrayList<>(); in.readStringList(hints); in.readStringList(hints); String callingPackageName = in.readString(); int userId = in.readInt(); Bundle extras = in.readBundle(); Bundle extras = in.readBundle(); boolean useDefaultTextClassifier = in.readBoolean(); SystemTextClassifierMetadata systemTcMetadata = in.readParcelable(null); Request request = new Request( Request request = new Request( conversation, conversation, Loading @@ -356,9 +347,7 @@ public final class ConversationActions implements Parcelable { maxSuggestions, maxSuggestions, hints, hints, extras); extras); request.setCallingPackageName(callingPackageName); request.setSystemTextClassifierMetadata(systemTcMetadata); request.setUserId(userId); request.setUseDefaultTextClassifier(useDefaultTextClassifier); return request; return request; } } Loading @@ -368,10 +357,8 @@ public final class ConversationActions implements Parcelable { parcel.writeParcelable(mTypeConfig, flags); parcel.writeParcelable(mTypeConfig, flags); parcel.writeInt(mMaxSuggestions); parcel.writeInt(mMaxSuggestions); parcel.writeStringList(mHints); parcel.writeStringList(mHints); parcel.writeString(mCallingPackageName); parcel.writeInt(mUserId); parcel.writeBundle(mExtras); parcel.writeBundle(mExtras); parcel.writeBoolean(mUseDefaultTextClassifier); parcel.writeParcelable(mSystemTcMetadata, flags); } } @Override @Override Loading Loading @@ -420,63 +407,32 @@ public final class ConversationActions implements Parcelable { return mHints; 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. * Returns the name of the package that sent this request. * This returns {@code null} if no calling package name is set. * This returns {@code null} if no calling package name is set. */ */ @Nullable @Nullable public String getCallingPackageName() { public String getCallingPackageName() { return mCallingPackageName; return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : null; } } /** /** * Sets the id of the user that sent this request. * Sets the information about the {@link SystemTextClassifier} 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. * * * @hide * @hide */ */ void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcData) { mUseDefaultTextClassifier = useDefaultTextClassifier; mSystemTcMetadata = systemTcData; } } /** /** * Returns whether to use the default text classifier to handle this request. This * Returns the information about the {@link SystemTextClassifier} that sent this request. * will be ignored if it is not the system text classifier to handle this request. * * * @hide * @hide */ */ public boolean getUseDefaultTextClassifier() { @Nullable return mUseDefaultTextClassifier; public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { return mSystemTcMetadata; } } /** /** Loading
core/java/android/view/textclassifier/SelectionEvent.java +21 −46 Original line number Original line Diff line number Diff line Loading @@ -19,10 +19,8 @@ package android.view.textclassifier; import android.annotation.IntDef; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.UserIdInt; import android.os.Parcel; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable; import android.os.UserHandle; import android.view.textclassifier.TextClassifier.EntityType; import android.view.textclassifier.TextClassifier.EntityType; import android.view.textclassifier.TextClassifier.WidgetType; import android.view.textclassifier.TextClassifier.WidgetType; Loading Loading @@ -129,7 +127,6 @@ public final class SelectionEvent implements Parcelable { private String mWidgetType = TextClassifier.WIDGET_TYPE_UNKNOWN; private String mWidgetType = TextClassifier.WIDGET_TYPE_UNKNOWN; private @InvocationMethod int mInvocationMethod; private @InvocationMethod int mInvocationMethod; @Nullable private String mWidgetVersion; @Nullable private String mWidgetVersion; private @UserIdInt int mUserId = UserHandle.USER_NULL; @Nullable private String mResultId; @Nullable private String mResultId; private long mEventTime; private long mEventTime; private long mDurationSinceSessionStart; private long mDurationSinceSessionStart; Loading @@ -140,7 +137,7 @@ public final class SelectionEvent implements Parcelable { private int mEnd; private int mEnd; private int mSmartStart; private int mSmartStart; private int mSmartEnd; private int mSmartEnd; private boolean mUseDefaultTextClassifier; @Nullable private SystemTextClassifierMetadata mSystemTcMetadata; SelectionEvent( SelectionEvent( int start, int end, int start, int end, Loading @@ -161,6 +158,7 @@ public final class SelectionEvent implements Parcelable { mEventType = in.readInt(); mEventType = in.readInt(); mEntityType = in.readString(); mEntityType = in.readString(); mWidgetVersion = in.readInt() > 0 ? in.readString() : null; mWidgetVersion = in.readInt() > 0 ? in.readString() : null; // TODO: remove mPackageName once aiai does not need it mPackageName = in.readString(); mPackageName = in.readString(); mWidgetType = in.readString(); mWidgetType = in.readString(); mInvocationMethod = in.readInt(); mInvocationMethod = in.readInt(); Loading @@ -175,8 +173,7 @@ public final class SelectionEvent implements Parcelable { mEnd = in.readInt(); mEnd = in.readInt(); mSmartStart = in.readInt(); mSmartStart = in.readInt(); mSmartEnd = in.readInt(); mSmartEnd = in.readInt(); mUserId = in.readInt(); mSystemTcMetadata = in.readParcelable(null); mUseDefaultTextClassifier = in.readBoolean(); } } @Override @Override Loading @@ -189,6 +186,7 @@ public final class SelectionEvent implements Parcelable { if (mWidgetVersion != null) { if (mWidgetVersion != null) { dest.writeString(mWidgetVersion); dest.writeString(mWidgetVersion); } } // TODO: remove mPackageName once aiai does not need it dest.writeString(mPackageName); dest.writeString(mPackageName); dest.writeString(mWidgetType); dest.writeString(mWidgetType); dest.writeInt(mInvocationMethod); dest.writeInt(mInvocationMethod); Loading @@ -205,8 +203,7 @@ public final class SelectionEvent implements Parcelable { dest.writeInt(mEnd); dest.writeInt(mEnd); dest.writeInt(mSmartStart); dest.writeInt(mSmartStart); dest.writeInt(mSmartEnd); dest.writeInt(mSmartEnd); dest.writeInt(mUserId); dest.writeParcelable(mSystemTcMetadata, flags); dest.writeBoolean(mUseDefaultTextClassifier); } } @Override @Override Loading Loading @@ -409,45 +406,26 @@ public final class SelectionEvent implements Parcelable { */ */ @NonNull @NonNull public String getPackageName() { public String getPackageName() { return mPackageName; return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : ""; } } /** /** * Sets the id of this event's user. * Sets the information about the {@link SystemTextClassifier} that sent this request. * <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. * * * @hide * @hide */ */ void setUseDefaultTextClassifier(boolean useDefaultTextClassifier) { void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcMetadata) { mUseDefaultTextClassifier = useDefaultTextClassifier; mSystemTcMetadata = systemTcMetadata; } } /** /** * Returns whether to use the default text classifier to handle this request. This * Returns the information about the {@link SystemTextClassifier} that sent this request. * will be ignored if it is not the system text classifier to handle this request. * * * @hide * @hide */ */ public boolean getUseDefaultTextClassifier() { @Nullable return mUseDefaultTextClassifier; public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { return mSystemTcMetadata; } } /** /** Loading Loading @@ -476,7 +454,7 @@ public final class SelectionEvent implements Parcelable { mPackageName = context.getPackageName(); mPackageName = context.getPackageName(); mWidgetType = context.getWidgetType(); mWidgetType = context.getWidgetType(); mWidgetVersion = context.getWidgetVersion(); mWidgetVersion = context.getWidgetVersion(); mUserId = context.getUserId(); mSystemTcMetadata = context.getSystemTextClassifierMetadata(); } } /** /** Loading Loading @@ -663,10 +641,9 @@ public final class SelectionEvent implements Parcelable { @Override @Override public int hashCode() { public int hashCode() { return Objects.hash(mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, return Objects.hash(mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mWidgetVersion, mPackageName, mUserId, mWidgetType, mInvocationMethod, mResultId, mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mResultId, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mSystemTcMetadata); mUseDefaultTextClassifier); } } @Override @Override Loading @@ -685,7 +662,6 @@ public final class SelectionEvent implements Parcelable { && Objects.equals(mEntityType, other.mEntityType) && Objects.equals(mEntityType, other.mEntityType) && Objects.equals(mWidgetVersion, other.mWidgetVersion) && Objects.equals(mWidgetVersion, other.mWidgetVersion) && Objects.equals(mPackageName, other.mPackageName) && Objects.equals(mPackageName, other.mPackageName) && mUserId == other.mUserId && Objects.equals(mWidgetType, other.mWidgetType) && Objects.equals(mWidgetType, other.mWidgetType) && mInvocationMethod == other.mInvocationMethod && mInvocationMethod == other.mInvocationMethod && Objects.equals(mResultId, other.mResultId) && Objects.equals(mResultId, other.mResultId) Loading @@ -698,7 +674,7 @@ public final class SelectionEvent implements Parcelable { && mEnd == other.mEnd && mEnd == other.mEnd && mSmartStart == other.mSmartStart && mSmartStart == other.mSmartStart && mSmartEnd == other.mSmartEnd && mSmartEnd == other.mSmartEnd && mUseDefaultTextClassifier == other.mUseDefaultTextClassifier; && mSystemTcMetadata == other.mSystemTcMetadata; } } @Override @Override Loading @@ -706,15 +682,14 @@ public final class SelectionEvent implements Parcelable { return String.format(Locale.US, return String.format(Locale.US, "SelectionEvent {absoluteStart=%d, absoluteEnd=%d, eventType=%d, entityType=%s, " "SelectionEvent {absoluteStart=%d, absoluteEnd=%d, eventType=%d, entityType=%s, " + "widgetVersion=%s, packageName=%s, widgetType=%s, invocationMethod=%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," + "durationSincePreviousEvent=%d, eventIndex=%d," + "sessionId=%s, start=%d, end=%d, smartStart=%d, smartEnd=%d, " + "sessionId=%s, start=%d, end=%d, smartStart=%d, smartEnd=%d, " + "mUseDefaultTextClassifier=%b}", + "systemTcMetadata=%s}", mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mAbsoluteStart, mAbsoluteEnd, mEventType, mEntityType, mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mWidgetVersion, mPackageName, mWidgetType, mInvocationMethod, mUserId, mResultId, mEventTime, mDurationSinceSessionStart, mResultId, mEventTime, mDurationSinceSessionStart, mDurationSincePreviousEvent, mDurationSincePreviousEvent, mEventIndex, mEventIndex, mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mSystemTcMetadata); mSessionId, mStart, mEnd, mSmartStart, mSmartEnd, mUseDefaultTextClassifier); } } public static final @android.annotation.NonNull Creator<SelectionEvent> CREATOR = new Creator<SelectionEvent>() { public static final @android.annotation.NonNull Creator<SelectionEvent> CREATOR = new Creator<SelectionEvent>() { Loading
core/java/android/view/textclassifier/SystemTextClassifier.java +31 −38 Original line number Original line Diff line number Diff line Loading @@ -18,7 +18,6 @@ package android.view.textclassifier; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.UserIdInt; import android.annotation.WorkerThread; import android.annotation.WorkerThread; import android.content.Context; import android.content.Context; import android.os.Bundle; import android.os.Bundle; Loading @@ -39,7 +38,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit; /** /** * Proxy to the system's default TextClassifier. * proxy to the request to TextClassifierService via the TextClassificationManagerService. * * @hide * @hide */ */ @VisibleForTesting(visibility = Visibility.PACKAGE) @VisibleForTesting(visibility = Visibility.PACKAGE) Loading @@ -50,14 +50,19 @@ public final class SystemTextClassifier implements TextClassifier { private final ITextClassifierService mManagerService; private final ITextClassifierService mManagerService; private final TextClassificationConstants mSettings; private final TextClassificationConstants mSettings; private final TextClassifier mFallback; 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; 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( public SystemTextClassifier( Context context, Context context, TextClassificationConstants settings, TextClassificationConstants settings, Loading @@ -66,9 +71,11 @@ public final class SystemTextClassifier implements TextClassifier { ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE)); ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE)); mSettings = Objects.requireNonNull(settings); mSettings = Objects.requireNonNull(settings); mFallback = TextClassifier.NO_OP; mFallback = TextClassifier.NO_OP; mPackageName = Objects.requireNonNull(context.getOpPackageName()); // NOTE: Always set this before sending a request to the manager service otherwise the mUserId = context.getUserId(); // manager service will throw a remote exception. mUseDefault = useDefault; mSystemTcMetadata = new SystemTextClassifierMetadata( Objects.requireNonNull(context.getOpPackageName()), context.getUserId(), useDefault); } } /** /** Loading @@ -80,9 +87,7 @@ public final class SystemTextClassifier implements TextClassifier { Objects.requireNonNull(request); Objects.requireNonNull(request); Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextSelection> callback = final BlockingCallback<TextSelection> callback = new BlockingCallback<>("textselection"); new BlockingCallback<>("textselection"); mManagerService.onSuggestSelection(mSessionId, request, callback); mManagerService.onSuggestSelection(mSessionId, request, callback); Loading @@ -105,9 +110,7 @@ public final class SystemTextClassifier implements TextClassifier { Objects.requireNonNull(request); Objects.requireNonNull(request); Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextClassification> callback = final BlockingCallback<TextClassification> callback = new BlockingCallback<>("textclassification"); new BlockingCallback<>("textclassification"); mManagerService.onClassifyText(mSessionId, request, callback); mManagerService.onClassifyText(mSessionId, request, callback); Loading Loading @@ -137,9 +140,7 @@ public final class SystemTextClassifier implements TextClassifier { } } try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextLinks> callback = final BlockingCallback<TextLinks> callback = new BlockingCallback<>("textlinks"); new BlockingCallback<>("textlinks"); mManagerService.onGenerateLinks(mSessionId, request, callback); mManagerService.onGenerateLinks(mSessionId, request, callback); Loading @@ -159,8 +160,7 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { event.setUserId(mUserId); event.setSystemTextClassifierMetadata(mSystemTcMetadata); event.setUseDefaultTextClassifier(mUseDefault); mManagerService.onSelectionEvent(mSessionId, event); mManagerService.onSelectionEvent(mSessionId, event); } catch (RemoteException e) { } catch (RemoteException e) { Log.e(LOG_TAG, "Error reporting selection event.", e); Log.e(LOG_TAG, "Error reporting selection event.", e); Loading @@ -173,12 +173,11 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { final TextClassificationContext tcContext = event.getEventContext() == null final TextClassificationContext tcContext = ? new TextClassificationContext.Builder(mPackageName, WIDGET_TYPE_UNKNOWN) event.getEventContext() == null ? new TextClassificationContext.Builder( .build() mSystemTcMetadata.getCallingPackageName(), WIDGET_TYPE_UNKNOWN).build() : event.getEventContext(); : event.getEventContext(); tcContext.setUserId(mUserId); tcContext.setSystemTextClassifierMetadata(mSystemTcMetadata); tcContext.setUseDefaultTextClassifier(mUseDefault); event.setEventContext(tcContext); event.setEventContext(tcContext); mManagerService.onTextClassifierEvent(mSessionId, event); mManagerService.onTextClassifierEvent(mSessionId, event); } catch (RemoteException e) { } catch (RemoteException e) { Loading @@ -192,9 +191,7 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<TextLanguage> callback = final BlockingCallback<TextLanguage> callback = new BlockingCallback<>("textlanguage"); new BlockingCallback<>("textlanguage"); mManagerService.onDetectLanguage(mSessionId, request, callback); mManagerService.onDetectLanguage(mSessionId, request, callback); Loading @@ -214,9 +211,7 @@ public final class SystemTextClassifier implements TextClassifier { Utils.checkMainThread(); Utils.checkMainThread(); try { try { request.setCallingPackageName(mPackageName); request.setSystemTextClassifierMetadata(mSystemTcMetadata); request.setUserId(mUserId); request.setUseDefaultTextClassifier(mUseDefault); final BlockingCallback<ConversationActions> callback = final BlockingCallback<ConversationActions> callback = new BlockingCallback<>("conversation-actions"); new BlockingCallback<>("conversation-actions"); mManagerService.onSuggestConversationActions(mSessionId, request, callback); mManagerService.onSuggestConversationActions(mSessionId, request, callback); Loading Loading @@ -256,10 +251,8 @@ public final class SystemTextClassifier implements TextClassifier { printWriter.println("SystemTextClassifier:"); printWriter.println("SystemTextClassifier:"); printWriter.increaseIndent(); printWriter.increaseIndent(); printWriter.printPair("mFallback", mFallback); printWriter.printPair("mFallback", mFallback); printWriter.printPair("mPackageName", mPackageName); printWriter.printPair("mSessionId", mSessionId); printWriter.printPair("mSessionId", mSessionId); printWriter.printPair("mUserId", mUserId); printWriter.printPair("mSystemTcMetadata", mSystemTcMetadata); printWriter.printPair("mUseDefault", mUseDefault); printWriter.decreaseIndent(); printWriter.decreaseIndent(); printWriter.println(); printWriter.println(); } } Loading @@ -275,7 +268,7 @@ public final class SystemTextClassifier implements TextClassifier { @NonNull TextClassificationSessionId sessionId) { @NonNull TextClassificationSessionId sessionId) { mSessionId = Objects.requireNonNull(sessionId); mSessionId = Objects.requireNonNull(sessionId); try { try { classificationContext.setUserId(mUserId); classificationContext.setSystemTextClassifierMetadata(mSystemTcMetadata); mManagerService.onCreateTextClassificationSession(classificationContext, mSessionId); mManagerService.onCreateTextClassificationSession(classificationContext, mSessionId); } catch (RemoteException e) { } catch (RemoteException e) { Log.e(LOG_TAG, "Error starting a new classification session.", e); Log.e(LOG_TAG, "Error starting a new classification session.", e); Loading
core/java/android/view/textclassifier/SystemTextClassifierMetadata.aidl 0 → 100644 +19 −0 Original line number Original line 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
core/java/android/view/textclassifier/SystemTextClassifierMetadata.java 0 → 100644 +121 −0 Original line number Original line 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]; } }; }