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

Commit 8f3105ff authored by Gilles Debunne's avatar Gilles Debunne
Browse files

5402566: Copy/cut icons do not appear if the text is Japanese only

When the word iterator return an empty range, select an arbitrary
character so that cut/copy icons are available.

Change-Id: I16e5a3c7f10886db967d870706da7f2d690af013
parent 2588a077
Loading
Loading
Loading
Loading
+39 −8
Original line number Original line Diff line number Diff line
@@ -8925,10 +8925,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener


        // If a URLSpan (web address, email, phone...) is found at that position, select it.
        // If a URLSpan (web address, email, phone...) is found at that position, select it.
        URLSpan[] urlSpans = ((Spanned) mText).getSpans(minOffset, maxOffset, URLSpan.class);
        URLSpan[] urlSpans = ((Spanned) mText).getSpans(minOffset, maxOffset, URLSpan.class);
        if (urlSpans.length == 1) {
        if (urlSpans.length >= 1) {
            URLSpan url = urlSpans[0];
            URLSpan urlSpan = urlSpans[0];
            selectionStart = ((Spanned) mText).getSpanStart(url);
            selectionStart = ((Spanned) mText).getSpanStart(urlSpan);
            selectionEnd = ((Spanned) mText).getSpanEnd(url);
            selectionEnd = ((Spanned) mText).getSpanEnd(urlSpan);
        } else {
        } else {
            final int shift = prepareWordIterator(minOffset, maxOffset);
            final int shift = prepareWordIterator(minOffset, maxOffset);


@@ -8939,10 +8939,42 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            selectionEnd = mWordIterator.getEnd(maxOffset - shift);
            selectionEnd = mWordIterator.getEnd(maxOffset - shift);
            if (selectionEnd == BreakIterator.DONE) return false;
            if (selectionEnd == BreakIterator.DONE) return false;
            selectionEnd += shift;
            selectionEnd += shift;

            if (selectionStart == selectionEnd) {
                // Possible when the word iterator does not properly handle the text's language
                long range = getCharRange(selectionStart);
                selectionStart = extractRangeStartFromLong(range);
                selectionEnd = extractRangeEndFromLong(range);
            }
        }
        }


        Selection.setSelection((Spannable) mText, selectionStart, selectionEnd);
        Selection.setSelection((Spannable) mText, selectionStart, selectionEnd);
        return true;
        return selectionEnd > selectionStart;
    }

    private long getCharRange(int offset) {
        final int textLength = mText.length();
        if (offset + 1 < textLength) {
            final char currentChar = mText.charAt(offset);
            final char nextChar = mText.charAt(offset + 1);
            if (Character.isSurrogatePair(currentChar, nextChar)) {
                return packRangeInLong(offset,  offset + 2);
            }
        }
        if (offset < textLength) {
            return packRangeInLong(offset,  offset + 1);
        }
        if (offset - 2 >= 0) {
            final char previousChar = mText.charAt(offset - 1);
            final char previousPreviousChar = mText.charAt(offset - 2);
            if (Character.isSurrogatePair(previousPreviousChar, previousChar)) {
                return packRangeInLong(offset - 2,  offset);
            }
        }
        if (offset - 1 >= 0) {
            return packRangeInLong(offset - 1,  offset);
        }
        return packRangeInLong(offset,  offset);
    }
    }


    int prepareWordIterator(int start, int end) {
    int prepareWordIterator(int start, int end) {
@@ -9330,7 +9362,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener


        // Start a new selection
        // Start a new selection
        if (!handled) {
        if (!handled) {
            handled = startSelectionActionMode();
            vibrate = handled = startSelectionActionMode();
        }
        }


        if (vibrate) {
        if (vibrate) {
@@ -10166,8 +10198,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener


        if (!hasSelection()) {
        if (!hasSelection()) {
            // There may already be a selection on device rotation
            // There may already be a selection on device rotation
            boolean currentWordSelected = selectCurrentWord();
            if (!selectCurrentWord()) {
            if (!currentWordSelected) {
                // No word found under cursor or text selection not permitted.
                // No word found under cursor or text selection not permitted.
                return false;
                return false;
            }
            }