Loading core/java/android/view/textclassifier/ExtrasUtils.java 0 → 100644 +127 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 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.Nullable; import android.app.RemoteAction; import android.content.Intent; import android.os.Bundle; import java.util.ArrayList; /** * Utility class for inserting and retrieving data in TextClassifier request/response extras. * @hide */ public final class ExtrasUtils { private static final String ACTIONS_INTENTS = "actions-intents"; private static final String FOREIGN_LANGUAGE = "foreign-language"; private static final String ENTITY_TYPE = "entity-type"; private static final String SCORE = "score"; private static final String MODEL_VERSION = "model-version"; private static final String MODEL_NAME = "model-name"; private ExtrasUtils() {} /** * Bundles and returns foreign language detection information for TextClassifier responses. */ static Bundle createForeignLanguageExtra( String language, float score, int modelVersion) { final Bundle bundle = new Bundle(); bundle.putString(ENTITY_TYPE, language); bundle.putFloat(SCORE, score); bundle.putInt(MODEL_VERSION, modelVersion); bundle.putString(MODEL_NAME, "langId_v" + modelVersion); return bundle; } /** * Stores {@code extra} as foreign language information in TextClassifier response object's * extras {@code container}. */ static void putForeignLanguageExtra(Bundle container, Bundle extra) { container.putParcelable(FOREIGN_LANGUAGE, extra); } /** * Returns foreign language detection information contained in the TextClassification object. * responses. */ @Nullable public static Bundle getForeignLanguageExtra(TextClassification classification) { return classification.getExtras().getBundle(FOREIGN_LANGUAGE); } /** * Stores {@code actionIntents} information in TextClassifier response object's extras * {@code container}. */ static void putActionsIntents(Bundle container, ArrayList<Intent> actionsIntents) { container.putParcelableArrayList(ACTIONS_INTENTS, actionsIntents); } /** * Returns {@code actionIntents} information contained in the TextClassification object. */ @Nullable public static ArrayList<Intent> getActionsIntents(TextClassification classification) { return classification.getExtras().getParcelableArrayList(ACTIONS_INTENTS); } /** * Returns the first "translate" action found in the {@code classification} object. */ @Nullable public static RemoteAction findTranslateAction(TextClassification classification) { final ArrayList<Intent> actionIntents = getActionsIntents(classification); if (actionIntents != null) { final int size = actionIntents.size(); for (int i = 0; i < size; i++) { if (Intent.ACTION_TRANSLATE.equals(actionIntents.get(i).getAction())) { return classification.getActions().get(i); } } } return null; } /** * Returns the entity type contained in the {@code extra}. */ @Nullable public static String getEntityType(Bundle extra) { return extra.getString(ENTITY_TYPE); } /** * Returns the score contained in the {@code extra}. */ @Nullable public static float getScore(Bundle extra) { return extra.getFloat(SCORE, -1); } /** * Returns the model name contained in the {@code extra}. */ @Nullable public static String getModelName(Bundle extra) { return extra.getString(MODEL_NAME); } } core/java/android/view/textclassifier/TextClassification.java +35 −2 Original line number Diff line number Diff line Loading @@ -378,6 +378,8 @@ public final class TextClassification implements Parcelable { @Nullable private OnClickListener mLegacyOnClickListener; @Nullable private String mId; @Nullable private Bundle mExtras; @NonNull private final ArrayList<Intent> mActionIntents = new ArrayList<>(); @Nullable private Bundle mForeignLanguageExtra; /** * Sets the classified text. Loading Loading @@ -412,8 +414,19 @@ public final class TextClassification implements Parcelable { */ @NonNull public Builder addAction(@NonNull RemoteAction action) { return addAction(action, null); } /** * @param intent the intent in the remote action. * @see #addAction(RemoteAction) * @hide */ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) public Builder addAction(RemoteAction action, @Nullable Intent intent) { Preconditions.checkArgument(action != null); mActions.add(action); mActionIntents.add(intent); return this; } Loading Loading @@ -498,14 +511,34 @@ public final class TextClassification implements Parcelable { return this; } /** * @see #setExtras(Bundle) * @hide */ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) public Builder setForeignLanguageExtra(@Nullable Bundle extra) { mForeignLanguageExtra = extra; return this; } /** * Builds and returns a {@link TextClassification} object. */ @NonNull public TextClassification build() { return new TextClassification(mText, mLegacyIcon, mLegacyLabel, mLegacyIntent, mLegacyOnClickListener, mActions, mEntityConfidence, mId, mExtras == null ? Bundle.EMPTY : mExtras.deepCopy()); mLegacyOnClickListener, mActions, mEntityConfidence, mId, buildExtras()); } private Bundle buildExtras() { final Bundle extras = mExtras == null ? new Bundle() : mExtras.deepCopy(); if (!mActionIntents.isEmpty()) { ExtrasUtils.putActionsIntents(extras, mActionIntents); } if (mForeignLanguageExtra != null) { ExtrasUtils.putForeignLanguageExtra(extras, mForeignLanguageExtra); } return extras.isEmpty() ? Bundle.EMPTY : extras; } } Loading core/java/android/view/textclassifier/TextClassificationSession.java +17 −4 Original line number Diff line number Diff line Loading @@ -71,11 +71,24 @@ final class TextClassificationSession implements TextClassifier { @Override public void onSelectionEvent(SelectionEvent event) { checkDestroyed(); Preconditions.checkNotNull(event); try { if (mEventHelper.sanitizeEvent(event)) { mDelegate.onSelectionEvent(event); } } catch (Exception e) { // Avoid crashing for event reporting. Log.e(LOG_TAG, "Error reporting text classifier selection event", e); } } @Override public void onTextClassifierEvent(TextClassifierEvent event) { try { mDelegate.onTextClassifierEvent(event); } catch (Exception e) { // Avoid crashing for event reporting. Log.e(LOG_TAG, "Error reporting text classifier event", e); } } @Override Loading core/java/android/view/textclassifier/TextClassifier.java +6 −1 Original line number Diff line number Diff line Loading @@ -161,7 +161,12 @@ public interface TextClassifier { * No-op TextClassifier. * This may be used to turn off TextClassifier features. */ TextClassifier NO_OP = new TextClassifier() {}; TextClassifier NO_OP = new TextClassifier() { @Override public String toString() { return "TextClassifier.NO_OP"; } }; /** * Extra that is included on activity intents coming from a TextClassifier when Loading core/java/android/view/textclassifier/TextClassifierEvent.java +33 −3 Original line number Diff line number Diff line Loading @@ -141,6 +141,8 @@ public final class TextClassifierEvent implements Parcelable { @Nullable private final String mLanguage; private final float mScore; @Nullable private final String mModelName; private TextClassifierEvent( int eventCategory, int eventType, Loading @@ -156,7 +158,8 @@ public final class TextClassifierEvent implements Parcelable { int relativeSuggestedWordEndIndex, int[] actionIndex, String language, float score) { float score, String modelVersion) { mEventCategory = eventCategory; mEventType = eventType; mEntityTypes = entityTypes; Loading @@ -172,6 +175,7 @@ public final class TextClassifierEvent implements Parcelable { mActionIndices = actionIndex; mLanguage = language; mScore = score; mModelName = modelVersion; } @Override Loading @@ -196,6 +200,7 @@ public final class TextClassifierEvent implements Parcelable { dest.writeIntArray(mActionIndices); dest.writeString(mLanguage); dest.writeFloat(mScore); dest.writeString(mModelName); } private static TextClassifierEvent readFromParcel(Parcel in) { Loading @@ -214,7 +219,8 @@ public final class TextClassifierEvent implements Parcelable { /* relativeSuggestedWordEndIndex= */ in.readInt(), /* actionIndices= */ in.createIntArray(), /* language= */ in.readString(), /* score= */ in.readFloat()); /* score= */ in.readFloat(), /* modelVersion= */ in.readString()); } /** Loading Loading @@ -264,6 +270,7 @@ public final class TextClassifierEvent implements Parcelable { return mEventIndex; } // TODO: Remove this API. /** * Returns the time this event occurred. This is the number of milliseconds since * January 1, 1970, 00:00:00 GMT. 0 indicates not set. Loading Loading @@ -338,6 +345,15 @@ public final class TextClassifierEvent implements Parcelable { return mScore; } /** * Returns the model name. * @hide */ @Nullable public String getModelName() { return mModelName; } /** * Builder to build a text classifier event. */ Loading @@ -359,6 +375,8 @@ public final class TextClassifierEvent implements Parcelable { @Nullable private String mLanguage; private float mScore; private String mModelName; /** * Creates a builder for building {@link TextClassifierEvent}s. * Loading Loading @@ -407,6 +425,7 @@ public final class TextClassifierEvent implements Parcelable { return this; } // TODO: Remove this API. /** * Sets the time this event occurred. This is the number of milliseconds since * January 1, 1970, 00:00:00 GMT. 0 indicates not set. Loading Loading @@ -500,6 +519,15 @@ public final class TextClassifierEvent implements Parcelable { return this; } /** * Sets the model name string. * @hide */ public Builder setModelName(@Nullable String modelVersion) { mModelName = modelVersion; return this; } /** * Builds and returns a text classifier event. */ Loading @@ -521,7 +549,8 @@ public final class TextClassifierEvent implements Parcelable { mRelativeSuggestedWordEndIndex, mActionIndices, mLanguage, mScore); mScore, mModelName); } // TODO: Add build(boolean validate). } Loading @@ -544,6 +573,7 @@ public final class TextClassifierEvent implements Parcelable { out.append(", mActionIndices=").append(Arrays.toString(mActionIndices)); out.append(", mLanguage=").append(mLanguage); out.append(", mScore=").append(mScore); out.append(", mModelName=").append(mModelName); out.append("}"); return out.toString(); } Loading Loading
core/java/android/view/textclassifier/ExtrasUtils.java 0 → 100644 +127 −0 Original line number Diff line number Diff line /* * Copyright (C) 2019 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.Nullable; import android.app.RemoteAction; import android.content.Intent; import android.os.Bundle; import java.util.ArrayList; /** * Utility class for inserting and retrieving data in TextClassifier request/response extras. * @hide */ public final class ExtrasUtils { private static final String ACTIONS_INTENTS = "actions-intents"; private static final String FOREIGN_LANGUAGE = "foreign-language"; private static final String ENTITY_TYPE = "entity-type"; private static final String SCORE = "score"; private static final String MODEL_VERSION = "model-version"; private static final String MODEL_NAME = "model-name"; private ExtrasUtils() {} /** * Bundles and returns foreign language detection information for TextClassifier responses. */ static Bundle createForeignLanguageExtra( String language, float score, int modelVersion) { final Bundle bundle = new Bundle(); bundle.putString(ENTITY_TYPE, language); bundle.putFloat(SCORE, score); bundle.putInt(MODEL_VERSION, modelVersion); bundle.putString(MODEL_NAME, "langId_v" + modelVersion); return bundle; } /** * Stores {@code extra} as foreign language information in TextClassifier response object's * extras {@code container}. */ static void putForeignLanguageExtra(Bundle container, Bundle extra) { container.putParcelable(FOREIGN_LANGUAGE, extra); } /** * Returns foreign language detection information contained in the TextClassification object. * responses. */ @Nullable public static Bundle getForeignLanguageExtra(TextClassification classification) { return classification.getExtras().getBundle(FOREIGN_LANGUAGE); } /** * Stores {@code actionIntents} information in TextClassifier response object's extras * {@code container}. */ static void putActionsIntents(Bundle container, ArrayList<Intent> actionsIntents) { container.putParcelableArrayList(ACTIONS_INTENTS, actionsIntents); } /** * Returns {@code actionIntents} information contained in the TextClassification object. */ @Nullable public static ArrayList<Intent> getActionsIntents(TextClassification classification) { return classification.getExtras().getParcelableArrayList(ACTIONS_INTENTS); } /** * Returns the first "translate" action found in the {@code classification} object. */ @Nullable public static RemoteAction findTranslateAction(TextClassification classification) { final ArrayList<Intent> actionIntents = getActionsIntents(classification); if (actionIntents != null) { final int size = actionIntents.size(); for (int i = 0; i < size; i++) { if (Intent.ACTION_TRANSLATE.equals(actionIntents.get(i).getAction())) { return classification.getActions().get(i); } } } return null; } /** * Returns the entity type contained in the {@code extra}. */ @Nullable public static String getEntityType(Bundle extra) { return extra.getString(ENTITY_TYPE); } /** * Returns the score contained in the {@code extra}. */ @Nullable public static float getScore(Bundle extra) { return extra.getFloat(SCORE, -1); } /** * Returns the model name contained in the {@code extra}. */ @Nullable public static String getModelName(Bundle extra) { return extra.getString(MODEL_NAME); } }
core/java/android/view/textclassifier/TextClassification.java +35 −2 Original line number Diff line number Diff line Loading @@ -378,6 +378,8 @@ public final class TextClassification implements Parcelable { @Nullable private OnClickListener mLegacyOnClickListener; @Nullable private String mId; @Nullable private Bundle mExtras; @NonNull private final ArrayList<Intent> mActionIntents = new ArrayList<>(); @Nullable private Bundle mForeignLanguageExtra; /** * Sets the classified text. Loading Loading @@ -412,8 +414,19 @@ public final class TextClassification implements Parcelable { */ @NonNull public Builder addAction(@NonNull RemoteAction action) { return addAction(action, null); } /** * @param intent the intent in the remote action. * @see #addAction(RemoteAction) * @hide */ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) public Builder addAction(RemoteAction action, @Nullable Intent intent) { Preconditions.checkArgument(action != null); mActions.add(action); mActionIntents.add(intent); return this; } Loading Loading @@ -498,14 +511,34 @@ public final class TextClassification implements Parcelable { return this; } /** * @see #setExtras(Bundle) * @hide */ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) public Builder setForeignLanguageExtra(@Nullable Bundle extra) { mForeignLanguageExtra = extra; return this; } /** * Builds and returns a {@link TextClassification} object. */ @NonNull public TextClassification build() { return new TextClassification(mText, mLegacyIcon, mLegacyLabel, mLegacyIntent, mLegacyOnClickListener, mActions, mEntityConfidence, mId, mExtras == null ? Bundle.EMPTY : mExtras.deepCopy()); mLegacyOnClickListener, mActions, mEntityConfidence, mId, buildExtras()); } private Bundle buildExtras() { final Bundle extras = mExtras == null ? new Bundle() : mExtras.deepCopy(); if (!mActionIntents.isEmpty()) { ExtrasUtils.putActionsIntents(extras, mActionIntents); } if (mForeignLanguageExtra != null) { ExtrasUtils.putForeignLanguageExtra(extras, mForeignLanguageExtra); } return extras.isEmpty() ? Bundle.EMPTY : extras; } } Loading
core/java/android/view/textclassifier/TextClassificationSession.java +17 −4 Original line number Diff line number Diff line Loading @@ -71,11 +71,24 @@ final class TextClassificationSession implements TextClassifier { @Override public void onSelectionEvent(SelectionEvent event) { checkDestroyed(); Preconditions.checkNotNull(event); try { if (mEventHelper.sanitizeEvent(event)) { mDelegate.onSelectionEvent(event); } } catch (Exception e) { // Avoid crashing for event reporting. Log.e(LOG_TAG, "Error reporting text classifier selection event", e); } } @Override public void onTextClassifierEvent(TextClassifierEvent event) { try { mDelegate.onTextClassifierEvent(event); } catch (Exception e) { // Avoid crashing for event reporting. Log.e(LOG_TAG, "Error reporting text classifier event", e); } } @Override Loading
core/java/android/view/textclassifier/TextClassifier.java +6 −1 Original line number Diff line number Diff line Loading @@ -161,7 +161,12 @@ public interface TextClassifier { * No-op TextClassifier. * This may be used to turn off TextClassifier features. */ TextClassifier NO_OP = new TextClassifier() {}; TextClassifier NO_OP = new TextClassifier() { @Override public String toString() { return "TextClassifier.NO_OP"; } }; /** * Extra that is included on activity intents coming from a TextClassifier when Loading
core/java/android/view/textclassifier/TextClassifierEvent.java +33 −3 Original line number Diff line number Diff line Loading @@ -141,6 +141,8 @@ public final class TextClassifierEvent implements Parcelable { @Nullable private final String mLanguage; private final float mScore; @Nullable private final String mModelName; private TextClassifierEvent( int eventCategory, int eventType, Loading @@ -156,7 +158,8 @@ public final class TextClassifierEvent implements Parcelable { int relativeSuggestedWordEndIndex, int[] actionIndex, String language, float score) { float score, String modelVersion) { mEventCategory = eventCategory; mEventType = eventType; mEntityTypes = entityTypes; Loading @@ -172,6 +175,7 @@ public final class TextClassifierEvent implements Parcelable { mActionIndices = actionIndex; mLanguage = language; mScore = score; mModelName = modelVersion; } @Override Loading @@ -196,6 +200,7 @@ public final class TextClassifierEvent implements Parcelable { dest.writeIntArray(mActionIndices); dest.writeString(mLanguage); dest.writeFloat(mScore); dest.writeString(mModelName); } private static TextClassifierEvent readFromParcel(Parcel in) { Loading @@ -214,7 +219,8 @@ public final class TextClassifierEvent implements Parcelable { /* relativeSuggestedWordEndIndex= */ in.readInt(), /* actionIndices= */ in.createIntArray(), /* language= */ in.readString(), /* score= */ in.readFloat()); /* score= */ in.readFloat(), /* modelVersion= */ in.readString()); } /** Loading Loading @@ -264,6 +270,7 @@ public final class TextClassifierEvent implements Parcelable { return mEventIndex; } // TODO: Remove this API. /** * Returns the time this event occurred. This is the number of milliseconds since * January 1, 1970, 00:00:00 GMT. 0 indicates not set. Loading Loading @@ -338,6 +345,15 @@ public final class TextClassifierEvent implements Parcelable { return mScore; } /** * Returns the model name. * @hide */ @Nullable public String getModelName() { return mModelName; } /** * Builder to build a text classifier event. */ Loading @@ -359,6 +375,8 @@ public final class TextClassifierEvent implements Parcelable { @Nullable private String mLanguage; private float mScore; private String mModelName; /** * Creates a builder for building {@link TextClassifierEvent}s. * Loading Loading @@ -407,6 +425,7 @@ public final class TextClassifierEvent implements Parcelable { return this; } // TODO: Remove this API. /** * Sets the time this event occurred. This is the number of milliseconds since * January 1, 1970, 00:00:00 GMT. 0 indicates not set. Loading Loading @@ -500,6 +519,15 @@ public final class TextClassifierEvent implements Parcelable { return this; } /** * Sets the model name string. * @hide */ public Builder setModelName(@Nullable String modelVersion) { mModelName = modelVersion; return this; } /** * Builds and returns a text classifier event. */ Loading @@ -521,7 +549,8 @@ public final class TextClassifierEvent implements Parcelable { mRelativeSuggestedWordEndIndex, mActionIndices, mLanguage, mScore); mScore, mModelName); } // TODO: Add build(boolean validate). } Loading @@ -544,6 +573,7 @@ public final class TextClassifierEvent implements Parcelable { out.append(", mActionIndices=").append(Arrays.toString(mActionIndices)); out.append(", mLanguage=").append(mLanguage); out.append(", mScore=").append(mScore); out.append(", mModelName=").append(mModelName); out.append("}"); return out.toString(); } Loading