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

Commit 646726f2 authored by Abodunrinwa Toki's avatar Abodunrinwa Toki Committed by android-build-merger
Browse files

Merge "Fix smart selection logging bugs." into oc-mr1-dev

am: 9cbaa8c7

Change-Id: I00dfe6ac59782f431b5f50f059055d9537b73ae8
parents ed409dd2 9cbaa8c7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -261,7 +261,7 @@ public final class TextClassification {
         * @hide
         */
        Builder setVersionInfo(@NonNull String versionInfo) {
            mVersionInfo = Preconditions.checkNotNull(mVersionInfo);
            mVersionInfo = Preconditions.checkNotNull(versionInfo);
            return this;
        }

+1 −1
Original line number Diff line number Diff line
@@ -169,7 +169,7 @@ public final class TextSelection {
         * @hide
         */
        Builder setVersionInfo(@NonNull String versionInfo) {
            mVersionInfo = Preconditions.checkNotNull(mVersionInfo);
            mVersionInfo = Preconditions.checkNotNull(versionInfo);
            return this;
        }

+3 −3
Original line number Diff line number Diff line
@@ -253,6 +253,7 @@ public final class SmartSelectionEventTracker {
    private static void debugLog(LogMaker log) {
        if (!DEBUG_LOG_ENABLED) return;

        final String tag = Objects.toString(log.getTaggedData(TAG), "tag");
        final int index = Integer.parseInt(Objects.toString(log.getTaggedData(INDEX), ZERO));

        final String event;
@@ -291,7 +292,6 @@ public final class SmartSelectionEventTracker {
                event = "RESET";
                break;
            case SelectionEvent.EventType.SELECTION_STARTED:
                final String tag = Objects.toString(log.getTaggedData(TAG), "tag");
                String sessionId = Objects.toString(log.getTaggedData(SESSION_ID), "");
                sessionId = sessionId.substring(sessionId.lastIndexOf("-") + 1);
                Log.d(LOG_TAG, String.format("New selection session: %s(%s)", tag, sessionId));
@@ -326,8 +326,8 @@ public final class SmartSelectionEventTracker {
        final String entity = Objects.toString(
                log.getTaggedData(ENTITY_TYPE), TextClassifier.TYPE_UNKNOWN);

        Log.d(LOG_TAG, String.format("%2d: %s, context=%d,%d - old=%d,%d [%s]",
                index, event, eventStart, eventEnd, smartStart, smartEnd, entity));
        Log.d(LOG_TAG, String.format("%2d: %s, context=%d,%d - old=%d,%d [%s] (%s)",
                index, event, eventStart, eventEnd, smartStart, smartEnd, entity, tag));
    }

    /**
+28 −25
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UiThread;
import android.annotation.WorkerThread;
import android.content.Context;
import android.os.AsyncTask;
import android.os.LocaleList;
import android.text.Selection;
@@ -41,6 +40,7 @@ import java.text.BreakIterator;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.regex.Pattern;

/**
 * Helper class for starting selection action mode
@@ -70,8 +70,7 @@ final class SelectionActionModeHelper {
        mTextClassificationHelper = new TextClassificationHelper(
                mTextView.getTextClassifier(), mTextView.getText(),
                0, 1, mTextView.getTextLocales());
        mSelectionTracker =
                new SelectionTracker(mTextView.getContext(), mTextView.isTextEditable());
        mSelectionTracker = new SelectionTracker(mTextView);
    }

    public void startActionModeAsync(boolean adjustSelection) {
@@ -211,7 +210,7 @@ final class SelectionActionModeHelper {
     */
    private static final class SelectionTracker {

        private final Context mContext;
        private final TextView mTextView;
        private SelectionMetricsLogger mLogger;

        private int mOriginalStart;
@@ -221,9 +220,9 @@ final class SelectionActionModeHelper {
        private boolean mSelectionStarted;
        private boolean mAllowReset;

        SelectionTracker(Context context, boolean editable) {
            mContext = Preconditions.checkNotNull(context);
            mLogger = new SelectionMetricsLogger(context, editable);
        SelectionTracker(TextView textView) {
            mTextView = Preconditions.checkNotNull(textView);
            mLogger = new SelectionMetricsLogger(textView);
        }

        /**
@@ -235,7 +234,7 @@ final class SelectionActionModeHelper {
            mOriginalEnd = selectionEnd;
            mSelectionStarted = true;
            mAllowReset = false;
            maybeInvalidateLogger(editableText);
            maybeInvalidateLogger();
            mLogger.logSelectionStarted(text, selectionStart);
        }

@@ -312,9 +311,9 @@ final class SelectionActionModeHelper {
            return false;
        }

        private void maybeInvalidateLogger(boolean editableText) {
            if (mLogger.isEditTextLogger() != editableText) {
                mLogger = new SelectionMetricsLogger(mContext, editableText);
        private void maybeInvalidateLogger() {
            if (mLogger.isEditTextLogger() != mTextView.isTextEditable()) {
                mLogger = new SelectionMetricsLogger(mTextView);
            }
        }
    }
@@ -336,20 +335,22 @@ final class SelectionActionModeHelper {
    private static final class SelectionMetricsLogger {

        private static final String LOG_TAG = "SelectionMetricsLogger";
        private static final Pattern PATTERN_WHITESPACE = Pattern.compile("\\s+");

        private final SmartSelectionEventTracker mDelegate;
        private final boolean mEditTextLogger;
        private final BreakIterator mWordIterator = BreakIterator.getWordInstance();
        private final BreakIterator mWordIterator;
        private int mStartIndex;
        private int mEndIndex;
        private String mText;

        SelectionMetricsLogger(Context context, boolean editable) {
            final @SmartSelectionEventTracker.WidgetType int widgetType = editable
        SelectionMetricsLogger(TextView textView) {
            Preconditions.checkNotNull(textView);
            final @SmartSelectionEventTracker.WidgetType int widgetType = textView.isTextEditable()
                    ? SmartSelectionEventTracker.WidgetType.EDITTEXT
                    : SmartSelectionEventTracker.WidgetType.TEXTVIEW;
            mDelegate = new SmartSelectionEventTracker(context, widgetType);
            mEditTextLogger = editable;
            mDelegate = new SmartSelectionEventTracker(textView.getContext(), widgetType);
            mEditTextLogger = textView.isTextEditable();
            mWordIterator = BreakIterator.getWordInstance(textView.getTextLocale());
        }

        public void logSelectionStarted(CharSequence text, int index) {
@@ -361,7 +362,6 @@ final class SelectionActionModeHelper {
                }
                mWordIterator.setText(mText);
                mStartIndex = index;
                mEndIndex = mWordIterator.following(index);
                mDelegate.logEvent(SelectionEvent.selectionStarted(0));
            } catch (Exception e) {
                // Avoid crashes due to logging.
@@ -424,12 +424,15 @@ final class SelectionActionModeHelper {
            } else if (start < mStartIndex) {
                wordIndices[0] = -countWordsForward(start);
            } else {  // start > mStartIndex
                if (mStartIndex < start && start < mEndIndex) {
                    // If the new selection did not move past the original word,
                    // assume it has not moved.
                    wordIndices[0] = 0;
                } else {
                wordIndices[0] = countWordsBackward(start);

                // For the selection start index, avoid counting a partial word backwards.
                if (!mWordIterator.isBoundary(start)
                        && !isWhitespace(
                                mWordIterator.preceding(start),
                                mWordIterator.following(start))) {
                    // We counted a partial word. Remove it.
                    wordIndices[0]--;
                }
            }

@@ -473,7 +476,7 @@ final class SelectionActionModeHelper {
        }

        private boolean isWhitespace(int start, int end) {
            return mText.substring(start, end).trim().isEmpty();
            return PATTERN_WHITESPACE.matcher(mText.substring(start, end)).matches();
        }
    }