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

Commit 8a986629 authored by Abodunrinwa Toki's avatar Abodunrinwa Toki Committed by android-build-merger
Browse files

Merge "Introduce SmartSelectionEventTracker." into oc-mr1-dev am: 16da1f54

am: f44588af

Change-Id: I3fcbd9d99c844068ec793c4f80319b85a9f90043
parents 6598a033 f44588af
Loading
Loading
Loading
Loading
+25 −2
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ public final class TextClassification {
    @NonNull private final EntityConfidence<String> mEntityConfidence;
    @NonNull private final List<String> mEntities;
    private int mLogType;
    @NonNull private final String mVersionInfo;

    private TextClassification(
            @Nullable String text,
@@ -56,7 +57,8 @@ public final class TextClassification {
            @Nullable Intent intent,
            @Nullable OnClickListener onClickListener,
            @NonNull EntityConfidence<String> entityConfidence,
            int logType) {
            int logType,
            @NonNull String versionInfo) {
        mText = text;
        mIcon = icon;
        mLabel = label;
@@ -65,6 +67,7 @@ public final class TextClassification {
        mEntityConfidence = new EntityConfidence<>(entityConfidence);
        mEntities = mEntityConfidence.getEntities();
        mLogType = logType;
        mVersionInfo = versionInfo;
    }

    /**
@@ -145,6 +148,15 @@ public final class TextClassification {
        return mLogType;
    }

    /**
     * Returns information about the classifier model used to generate this TextClassification.
     * @hide
     */
    @NonNull
    public String getVersionInfo() {
        return mVersionInfo;
    }

    @Override
    public String toString() {
        return String.format("TextClassification {"
@@ -179,6 +191,7 @@ public final class TextClassification {
        @NonNull private final EntityConfidence<String> mEntityConfidence =
                new EntityConfidence<>();
        private int mLogType;
        @NonNull private String mVersionInfo = "";

        /**
         * Sets the classified text.
@@ -243,12 +256,22 @@ public final class TextClassification {
            return this;
        }

        /**
         * Sets information about the classifier model used to generate this TextClassification.
         * @hide
         */
        Builder setVersionInfo(@NonNull String versionInfo) {
            mVersionInfo = Preconditions.checkNotNull(mVersionInfo);
            return this;
        }

        /**
         * Builds and returns a {@link TextClassification} object.
         */
        public TextClassification build() {
            return new TextClassification(
                    mText, mIcon, mLabel, mIntent, mOnClickListener, mEntityConfidence, mLogType);
                    mText, mIcon, mLabel, mIntent, mOnClickListener, mEntityConfidence,
                    mLogType, mVersionInfo);
        }
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -34,6 +34,11 @@ import java.lang.annotation.RetentionPolicy;
 */
public interface TextClassifier {

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

    /** @hide */
    String TYPE_UNKNOWN = "";  // TODO: Make this public API.
    String TYPE_OTHER = "other";
    String TYPE_EMAIL = "email";
    String TYPE_PHONE = "phone";
@@ -43,7 +48,7 @@ public interface TextClassifier {
    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @StringDef({
            TYPE_OTHER, TYPE_EMAIL, TYPE_PHONE, TYPE_ADDRESS, TYPE_URL
            TYPE_UNKNOWN, TYPE_OTHER, TYPE_EMAIL, TYPE_PHONE, TYPE_ADDRESS, TYPE_URL
    })
    @interface EntityType {}

+21 −5
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ import java.util.regex.Pattern;
 */
final class TextClassifierImpl implements TextClassifier {

    private static final String LOG_TAG = "TextClassifierImpl";
    private static final String LOG_TAG = DEFAULT_LOG_TAG;
    private static final String MODEL_DIR = "/etc/textclassifier/";
    private static final String MODEL_FILE_REGEX = "textclassifier\\.smartselection\\.(.*)\\.model";
    private static final String UPDATED_MODEL_FILE_PATH =
@@ -86,6 +86,8 @@ final class TextClassifierImpl implements TextClassifier {
    @GuardedBy("mSmartSelectionLock") // Do not access outside this lock.
    private Locale mLocale;
    @GuardedBy("mSmartSelectionLock") // Do not access outside this lock.
    private int mVersion;
    @GuardedBy("mSmartSelectionLock") // Do not access outside this lock.
    private SmartSelection mSmartSelection;

    TextClassifierImpl(Context context) {
@@ -108,8 +110,7 @@ final class TextClassifierImpl implements TextClassifier {
                if (start <= end
                        && start >= 0 && end <= string.length()
                        && start <= selectionStartIndex && end >= selectionEndIndex) {
                    final TextSelection.Builder tsBuilder = new TextSelection.Builder(start, end)
                            .setLogSource(LOG_TAG);
                    final TextSelection.Builder tsBuilder = new TextSelection.Builder(start, end);
                    final SmartSelection.ClassificationResult[] results =
                            smartSelection.classifyText(
                                    string, start, end,
@@ -118,7 +119,10 @@ final class TextClassifierImpl implements TextClassifier {
                    for (int i = 0; i < size; i++) {
                        tsBuilder.setEntityType(results[i].mCollection, results[i].mScore);
                    }
                    return tsBuilder.build();
                    return tsBuilder
                            .setLogSource(LOG_TAG)
                            .setVersionInfo(getVersionInfo())
                            .build();
                } else {
                    // We can not trust the result. Log the issue and ignore the result.
                    Log.d(LOG_TAG, "Got bad indices for input text. Ignoring result.");
@@ -202,6 +206,16 @@ final class TextClassifierImpl implements TextClassifier {
        }
    }

    @NonNull
    private String getVersionInfo() {
        synchronized (mSmartSelectionLock) {
            if (mLocale != null) {
                return String.format("%s_v%d", mLocale.toLanguageTag(), mVersion);
            }
            return "";
        }
    }

    @GuardedBy("mSmartSelectionLock") // Do not call outside this lock.
    private ParcelFileDescriptor getFdLocked(Locale locale) throws FileNotFoundException {
        ParcelFileDescriptor updateFd;
@@ -256,9 +270,11 @@ final class TextClassifierImpl implements TextClassifier {
        final int factoryVersion = SmartSelection.getVersion(factoryFd.getFd());
        if (updateVersion > factoryVersion) {
            closeAndLogError(factoryFd);
            mVersion = updateVersion;
            return updateFd;
        } else {
            closeAndLogError(updateFd);
            mVersion = factoryVersion;
            return factoryFd;
        }
    }
@@ -374,7 +390,7 @@ final class TextClassifierImpl implements TextClassifier {
                builder.setLabel(label != null ? label.toString() : null);
            }
        }
        return builder.build();
        return builder.setVersionInfo(getVersionInfo()).build();
    }

    private static int getHintFlags(CharSequence text, int start, int end) {
+25 −2
Original line number Diff line number Diff line
@@ -35,15 +35,17 @@ public final class TextSelection {
    @NonNull private final EntityConfidence<String> mEntityConfidence;
    @NonNull private final List<String> mEntities;
    @NonNull private final String mLogSource;
    @NonNull private final String mVersionInfo;

    private TextSelection(
            int startIndex, int endIndex, @NonNull EntityConfidence<String> entityConfidence,
            @NonNull String logSource) {
            @NonNull String logSource, @NonNull String versionInfo) {
        mStartIndex = startIndex;
        mEndIndex = endIndex;
        mEntityConfidence = new EntityConfidence<>(entityConfidence);
        mEntities = mEntityConfidence.getEntities();
        mLogSource = logSource;
        mVersionInfo = versionInfo;
    }

    /**
@@ -94,10 +96,20 @@ 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
     */
    @NonNull
    public String getVersionInfo() {
        return mVersionInfo;
    }

    @Override
    public String toString() {
        return String.format("TextSelection {%d, %d, %s}",
@@ -114,6 +126,7 @@ public final class TextSelection {
        @NonNull private final EntityConfidence<String> mEntityConfidence =
                new EntityConfidence<>();
        @NonNull private String mLogSource = "";
        @NonNull private String mVersionInfo = "";

        /**
         * Creates a builder used to build {@link TextSelection} objects.
@@ -151,11 +164,21 @@ public final class TextSelection {
            return this;
        }

        /**
         * Sets information about the classifier model used to generate this TextSelection.
         * @hide
         */
        Builder setVersionInfo(@NonNull String versionInfo) {
            mVersionInfo = Preconditions.checkNotNull(mVersionInfo);
            return this;
        }

        /**
         * Builds and returns {@link TextSelection} object.
         */
        public TextSelection build() {
            return new TextSelection(mStartIndex, mEndIndex, mEntityConfidence, mLogSource);
            return new TextSelection(
                    mStartIndex, mEndIndex, mEntityConfidence, mLogSource, mVersionInfo);
        }
    }
}
+560 −0

File added.

Preview size limit exceeded, changes collapsed.