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

Commit 358b028f authored by Abodunrinwa Toki's avatar Abodunrinwa Toki Committed by Android (Google) Code Review
Browse files

Merge "Add a signature to TextSelection and TextClassification"

parents a094c374 008f387e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -49081,6 +49081,7 @@ package android.view.textclassifier {
    method public android.content.Intent getSecondaryIntent(int);
    method public java.lang.CharSequence getSecondaryLabel(int);
    method public android.view.View.OnClickListener getSecondaryOnClickListener(int);
    method public java.lang.String getSignature();
    method public java.lang.String getText();
  }
@@ -49095,6 +49096,7 @@ package android.view.textclassifier {
    method public android.view.textclassifier.TextClassification.Builder setLabel(java.lang.String);
    method public android.view.textclassifier.TextClassification.Builder setOnClickListener(android.view.View.OnClickListener);
    method public android.view.textclassifier.TextClassification.Builder setPrimaryAction(android.content.Intent, java.lang.String, android.graphics.drawable.Drawable, android.view.View.OnClickListener);
    method public android.view.textclassifier.TextClassification.Builder setSignature(java.lang.String);
    method public android.view.textclassifier.TextClassification.Builder setText(java.lang.String);
  }
@@ -49159,12 +49161,14 @@ package android.view.textclassifier {
    method public int getEntityCount();
    method public int getSelectionEndIndex();
    method public int getSelectionStartIndex();
    method public java.lang.String getSignature();
  }
  public static final class TextSelection.Builder {
    ctor public TextSelection.Builder(int, int);
    method public android.view.textclassifier.TextSelection build();
    method public android.view.textclassifier.TextSelection.Builder setEntityType(java.lang.String, float);
    method public android.view.textclassifier.TextSelection.Builder setSignature(java.lang.String);
  }
  public static final class TextSelection.Options {
+21 −37
Original line number Diff line number Diff line
@@ -109,8 +109,7 @@ public final class TextClassification {
    @NonNull private final List<Intent> mSecondaryIntents;
    @NonNull private final List<OnClickListener> mSecondaryOnClickListeners;
    @NonNull private final EntityConfidence<String> mEntityConfidence;
    private int mLogType;
    @NonNull private final String mVersionInfo;
    @NonNull private final String mSignature;

    private TextClassification(
            @Nullable String text,
@@ -123,8 +122,7 @@ public final class TextClassification {
            @NonNull List<Intent> secondaryIntents,
            @NonNull List<OnClickListener> secondaryOnClickListeners,
            @NonNull Map<String, Float> entityConfidence,
            int logType,
            @NonNull String versionInfo) {
            @NonNull String signature) {
        Preconditions.checkArgument(secondaryLabels.size() == secondaryIntents.size());
        Preconditions.checkArgument(secondaryIcons.size() == secondaryIntents.size());
        Preconditions.checkArgument(secondaryOnClickListeners.size() == secondaryIntents.size());
@@ -138,8 +136,7 @@ public final class TextClassification {
        mSecondaryIntents = secondaryIntents;
        mSecondaryOnClickListeners = secondaryOnClickListeners;
        mEntityConfidence = new EntityConfidence<>(entityConfidence);
        mLogType = logType;
        mVersionInfo = versionInfo;
        mSignature = signature;
    }

    /**
@@ -315,30 +312,26 @@ public final class TextClassification {
    }

    /**
     * Returns the MetricsLogger subtype for the action that is performed for this result.
     * @hide
     */
    public int getLogType() {
        return mLogType;
    }

    /**
     * Returns information about the classifier model used to generate this TextClassification.
     * @hide
     * Returns the signature for this object.
     * The TextClassifier that generates this object may use it as a way to internally identify
     * this object.
     */
    @NonNull
    public String getVersionInfo() {
        return mVersionInfo;
    public String getSignature() {
        return mSignature;
    }

    @Override
    public String toString() {
        return String.format("TextClassification {"
        return String.format(Locale.US, "TextClassification {"
                        + "text=%s, entities=%s, "
                        + "primaryLabel=%s, secondaryLabels=%s, "
                        + "primaryIntent=%s, secondaryIntents=%s}",
                        + "primaryIntent=%s, secondaryIntents=%s, "
                        + "signature=%s}",
                mText, mEntityConfidence,
                mPrimaryLabel, mSecondaryLabels, mPrimaryIntent, mSecondaryIntents);
                mPrimaryLabel, mSecondaryLabels,
                mPrimaryIntent, mSecondaryIntents,
                mSignature);
    }

    /**
@@ -383,8 +376,7 @@ public final class TextClassification {
        @Nullable String mPrimaryLabel;
        @Nullable Intent mPrimaryIntent;
        @Nullable OnClickListener mPrimaryOnClickListener;
        private int mLogType;
        @NonNull private String mVersionInfo = "";
        @NonNull private String mSignature = "";

        /**
         * Sets the classified text.
@@ -508,20 +500,12 @@ public final class TextClassification {
        }

        /**
         * Sets the MetricsLogger subtype for the action that is performed for this result.
         * @hide
         */
        public Builder setLogType(int type) {
            mLogType = type;
            return this;
        }

        /**
         * Sets information about the classifier model used to generate this TextClassification.
         * @hide
         * Sets a signature for the TextClassification object.
         * The TextClassifier that generates the TextClassification object may use it as a way to
         * internally identify the TextClassification object.
         */
        Builder setVersionInfo(@NonNull String versionInfo) {
            mVersionInfo = Preconditions.checkNotNull(versionInfo);
        public Builder setSignature(@NonNull String signature) {
            mSignature = Preconditions.checkNotNull(signature);
            return this;
        }

@@ -535,7 +519,7 @@ public final class TextClassification {
                    mPrimaryIntent, mPrimaryOnClickListener,
                    mSecondaryIcons, mSecondaryLabels,
                    mSecondaryIntents, mSecondaryOnClickListeners,
                    mEntityConfidence, mLogType, mVersionInfo);
                    mEntityConfidence, mSignature);
        }
    }

+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ import java.lang.annotation.RetentionPolicy;
public interface TextClassifier {

    /** @hide */
    String DEFAULT_LOG_TAG = "TextClassifierImpl";
    String DEFAULT_LOG_TAG = "androidtc";

    String TYPE_UNKNOWN = "";
    String TYPE_OTHER = "other";
+15 −34
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import android.provider.Settings;
import android.text.util.Linkify;
import android.util.Patterns;
import android.view.View.OnClickListener;
import android.widget.TextViewMetrics;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.logging.MetricsLogger;
@@ -122,8 +121,8 @@ final class TextClassifierImpl implements TextClassifier {
                        tsBuilder.setEntityType(results[i].mCollection, results[i].mScore);
                    }
                    return tsBuilder
                            .setLogSource(LOG_TAG)
                            .setVersionInfo(getVersionInfo())
                            .setSignature(
                                    getSignature(string, selectionStartIndex, selectionEndIndex))
                            .build();
                } else {
                    // We can not trust the result. Log the issue and ignore the result.
@@ -155,8 +154,7 @@ final class TextClassifierImpl implements TextClassifier {
                                getHintFlags(string, startIndex, endIndex));
                if (results.length > 0) {
                    final TextClassification classificationResult =
                            createClassificationResult(
                                    results, string.subSequence(startIndex, endIndex));
                            createClassificationResult(results, string, startIndex, endIndex);
                    return classificationResult;
                }
            }
@@ -230,13 +228,13 @@ final class TextClassifierImpl implements TextClassifier {
        }
    }

    @NonNull
    private String getVersionInfo() {
    private String getSignature(String text, int start, int end) {
        synchronized (mSmartSelectionLock) {
            if (mLocale != null) {
                return String.format("%s_v%d", mLocale.toLanguageTag(), mVersion);
            }
            return "";
            final String versionInfo = (mLocale != null)
                    ? String.format(Locale.US, "%s_v%d", mLocale.toLanguageTag(), mVersion)
                    : "";
            final int hash = Objects.hash(text, start, end, mContext.getPackageName());
            return String.format(Locale.US, "%s|%s|%d", LOG_TAG, versionInfo, hash);
        }
    }

@@ -372,9 +370,11 @@ final class TextClassifierImpl implements TextClassifier {
    }

    private TextClassification createClassificationResult(
            SmartSelection.ClassificationResult[] classifications, CharSequence text) {
            SmartSelection.ClassificationResult[] classifications,
            String text, int start, int end) {
        final String classifiedText = text.substring(start, end);
        final TextClassification.Builder builder = new TextClassification.Builder()
                .setText(text.toString());
                .setText(classifiedText);

        final int size = classifications.length;
        for (int i = 0; i < size; i++) {
@@ -382,11 +382,9 @@ final class TextClassifierImpl implements TextClassifier {
        }

        final String type = getHighestScoringType(classifications);
        builder.setLogType(IntentFactory.getLogType(type));

        addActions(builder, IntentFactory.create(mContext, type, text.toString()));
        addActions(builder, IntentFactory.create(mContext, type, text));

        return builder.setVersionInfo(getVersionInfo()).build();
        return builder.setSignature(getSignature(text, start, end)).build();
    }

    /** Extends the classification with the intents that can be resolved. */
@@ -564,22 +562,5 @@ final class TextClassifierImpl implements TextClassifier {
                    return null;
            }
        }

        @Nullable
        public static int getLogType(String type) {
            type = type.trim().toLowerCase(Locale.ENGLISH);
            switch (type) {
                case TextClassifier.TYPE_EMAIL:
                    return TextViewMetrics.SUBTYPE_ASSIST_MENU_ITEM_EMAIL;
                case TextClassifier.TYPE_PHONE:
                    return TextViewMetrics.SUBTYPE_ASSIST_MENU_ITEM_PHONE;
                case TextClassifier.TYPE_ADDRESS:
                    return TextViewMetrics.SUBTYPE_ASSIST_MENU_ITEM_ADDRESS;
                case TextClassifier.TYPE_URL:
                    return TextViewMetrics.SUBTYPE_ASSIST_MENU_ITEM_URL;
                default:
                    return TextViewMetrics.SUBTYPE_ASSIST_MENU_ITEM_OTHER;
            }
        }
    }
}
+21 −38
Original line number Diff line number Diff line
@@ -37,17 +37,15 @@ public final class TextSelection {
    private final int mStartIndex;
    private final int mEndIndex;
    @NonNull private final EntityConfidence<String> mEntityConfidence;
    @NonNull private final String mLogSource;
    @NonNull private final String mVersionInfo;
    @NonNull private final String mSignature;

    private TextSelection(
            int startIndex, int endIndex, @NonNull EntityConfidence<String> entityConfidence,
            @NonNull String logSource, @NonNull String versionInfo) {
            int startIndex, int endIndex, @NonNull Map<String, Float> entityConfidence,
            @NonNull String signature) {
        mStartIndex = startIndex;
        mEndIndex = endIndex;
        mEntityConfidence = new EntityConfidence<>(entityConfidence);
        mLogSource = logSource;
        mVersionInfo = versionInfo;
        mSignature = signature;
    }

    /**
@@ -95,27 +93,21 @@ public final class TextSelection {
    }

    /**
     * Returns a tag for the source classifier used to generate this result.
     * @hide
     */
    @NonNull
    public String getSourceClassifier() {
        return mLogSource;
    }

    /**
     * Returns information about the classifier model used to generate this TextSelection.
     * @hide
     * Returns the signature for this object.
     * The TextClassifier that generates this object may use it as a way to internally identify
     * this object.
     */
    @NonNull
    public String getVersionInfo() {
        return mVersionInfo;
    public String getSignature() {
        return mSignature;
    }

    @Override
    public String toString() {
        return String.format(Locale.US,
                "TextSelection {%d, %d, %s}", mStartIndex, mEndIndex, mEntityConfidence);
        return String.format(
                Locale.US,
                "TextSelection {startIndex=%d, endIndex=%d, entities=%s, signature=%s}",
                mStartIndex, mEndIndex, mEntityConfidence, mSignature);
    }

    /**
@@ -126,8 +118,7 @@ public final class TextSelection {
        private final int mStartIndex;
        private final int mEndIndex;
        @NonNull private final Map<String, Float> mEntityConfidence = new ArrayMap<>();
        @NonNull private String mLogSource = "";
        @NonNull private String mVersionInfo = "";
        @NonNull private String mSignature = "";

        /**
         * Creates a builder used to build {@link TextSelection} objects.
@@ -157,20 +148,13 @@ public final class TextSelection {
        }

        /**
         * Sets a tag for the source classifier used to generate this result.
         * @hide
         */
        Builder setLogSource(@NonNull String logSource) {
            mLogSource = Preconditions.checkNotNull(logSource);
            return this;
        }

        /**
         * Sets information about the classifier model used to generate this TextSelection.
         * @hide
         * Sets a signature for the TextSelection object.
         *
         * The TextClassifier that generates the TextSelection object may use it as a way to
         * internally identify the TextSelection object.
         */
        Builder setVersionInfo(@NonNull String versionInfo) {
            mVersionInfo = Preconditions.checkNotNull(versionInfo);
        public Builder setSignature(@NonNull String signature) {
            mSignature = Preconditions.checkNotNull(signature);
            return this;
        }

@@ -179,8 +163,7 @@ public final class TextSelection {
         */
        public TextSelection build() {
            return new TextSelection(
                    mStartIndex, mEndIndex, new EntityConfidence<>(mEntityConfidence),  mLogSource,
                    mVersionInfo);
                    mStartIndex, mEndIndex, mEntityConfidence, mSignature);
        }
    }

Loading