Search in sources :

Example 71 with ClickableSpan

use of android.text.style.ClickableSpan in project android_frameworks_base by AOSPA.

the class TextView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    if (mEditor != null) {
        mEditor.onTouchEvent(event);
        if (mEditor.mSelectionModifierCursorController != null && mEditor.mSelectionModifierCursorController.isDragAcceleratorActive()) {
            return true;
        }
    }
    final boolean superResult = super.onTouchEvent(event);
    /*
         * Don't handle the release after a long press, because it will move the selection away from
         * whatever the menu action was trying to affect. If the long press should have triggered an
         * insertion action mode, we can now actually show it.
         */
    if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
        mEditor.mDiscardNextActionUp = false;
        if (mEditor.mIsInsertionActionModeStartPending) {
            mEditor.startInsertionActionMode();
            mEditor.mIsInsertionActionModeStartPending = false;
        }
        return superResult;
    }
    final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();
    if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() && mText instanceof Spannable && mLayout != null) {
        boolean handled = false;
        if (mMovement != null) {
            handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
        }
        final boolean textIsSelectable = isTextSelectable();
        if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), getSelectionEnd(), ClickableSpan.class);
            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }
        if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
            // Show the IME, except when selecting in read-only text.
            final InputMethodManager imm = InputMethodManager.peekInstance();
            viewClicked(imm);
            if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) {
                handled |= imm != null && imm.showSoftInput(this, 0);
            }
            // The above condition ensures that the mEditor is not null
            mEditor.onTouchUpEvent(event);
            handled = true;
        }
        if (handled) {
            return true;
        }
    }
    return superResult;
}
Also used : InputMethodManager(android.view.inputmethod.InputMethodManager) ClickableSpan(android.text.style.ClickableSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 72 with ClickableSpan

use of android.text.style.ClickableSpan in project robolectric by robolectric.

the class AccessibilityUtilTest method setUp.

@Before
public void setUp() throws Exception {
    Rect validViewBounds = new Rect(100, 100, 200, 200);
    // Set the statics back to their default values
    AccessibilityUtil.setRunChecksForRobolectricVersion(null);
    AccessibilityUtil.setSuppressingResultMatcher(null);
    AccessibilityUtil.setRunChecksFromRootView(false);
    AccessibilityUtil.setThrowExceptionForErrors(true);
    labeledView = new View(RuntimeEnvironment.application);
    labeledView.setContentDescription("Something");
    labeledView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    labeledView.setClickable(true);
    // Force the views on the screen so they'll be seen as visible even though they aren't
    // part of a valid view hierarchy
    shadowOf(labeledView).setGlobalVisibleRect(validViewBounds);
    unlabeledView = new View(RuntimeEnvironment.application);
    unlabeledView.setContentDescription(null);
    unlabeledView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    unlabeledView.setClickable(true);
    shadowOf(unlabeledView).setGlobalVisibleRect(validViewBounds);
    parentLayout = new LinearLayout(RuntimeEnvironment.application);
    parentLayout.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    parentLayout.addView(labeledView);
    shadowOf(parentLayout).setGlobalVisibleRect(validViewBounds);
    textViewWithClickableSpan = new TextView(RuntimeEnvironment.application);
    SpannableString spannableString = new SpannableString("Some text");
    ClickableSpan mockClickableSpan = mock(ClickableSpan.class);
    spannableString.setSpan(mockClickableSpan, 0, 1, Spanned.SPAN_COMPOSING);
    textViewWithClickableSpan.setText(spannableString);
    shadowOf(textViewWithClickableSpan).setGlobalVisibleRect(validViewBounds);
}
Also used : SpannableString(android.text.SpannableString) Rect(android.graphics.Rect) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View) ClickableSpan(android.text.style.ClickableSpan) LinearLayout(android.widget.LinearLayout) Before(org.junit.Before)

Example 73 with ClickableSpan

use of android.text.style.ClickableSpan in project android_frameworks_base by ResurrectionRemix.

the class TextView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    if (mEditor != null) {
        mEditor.onTouchEvent(event);
        if (mEditor.mSelectionModifierCursorController != null && mEditor.mSelectionModifierCursorController.isDragAcceleratorActive()) {
            return true;
        }
    }
    final boolean superResult = super.onTouchEvent(event);
    /*
         * Don't handle the release after a long press, because it will move the selection away from
         * whatever the menu action was trying to affect. If the long press should have triggered an
         * insertion action mode, we can now actually show it.
         */
    if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
        mEditor.mDiscardNextActionUp = false;
        if (mEditor.mIsInsertionActionModeStartPending) {
            mEditor.startInsertionActionMode();
            mEditor.mIsInsertionActionModeStartPending = false;
        }
        return superResult;
    }
    final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();
    if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() && mText instanceof Spannable && mLayout != null) {
        boolean handled = false;
        if (mMovement != null) {
            handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
        }
        final boolean textIsSelectable = isTextSelectable();
        if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), getSelectionEnd(), ClickableSpan.class);
            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }
        if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
            // Show the IME, except when selecting in read-only text.
            final InputMethodManager imm = InputMethodManager.peekInstance();
            viewClicked(imm);
            if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) {
                handled |= imm != null && imm.showSoftInput(this, 0);
            }
            // The above condition ensures that the mEditor is not null
            mEditor.onTouchUpEvent(event);
            handled = true;
        }
        if (handled) {
            return true;
        }
    }
    return superResult;
}
Also used : InputMethodManager(android.view.inputmethod.InputMethodManager) ClickableSpan(android.text.style.ClickableSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 74 with ClickableSpan

use of android.text.style.ClickableSpan in project android_frameworks_base by ResurrectionRemix.

the class LinkMovementMethod method action.

private boolean action(int what, TextView widget, Spannable buffer) {
    Layout layout = widget.getLayout();
    int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
    int areatop = widget.getScrollY();
    int areabot = areatop + widget.getHeight() - padding;
    int linetop = layout.getLineForVertical(areatop);
    int linebot = layout.getLineForVertical(areabot);
    int first = layout.getLineStart(linetop);
    int last = layout.getLineEnd(linebot);
    ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);
    int a = Selection.getSelectionStart(buffer);
    int b = Selection.getSelectionEnd(buffer);
    int selStart = Math.min(a, b);
    int selEnd = Math.max(a, b);
    if (selStart < 0) {
        if (buffer.getSpanStart(FROM_BELOW) >= 0) {
            selStart = selEnd = buffer.length();
        }
    }
    if (selStart > last)
        selStart = selEnd = Integer.MAX_VALUE;
    if (selEnd < first)
        selStart = selEnd = -1;
    switch(what) {
        case CLICK:
            if (selStart == selEnd) {
                return false;
            }
            ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);
            if (link.length != 1)
                return false;
            link[0].onClick(widget);
            break;
        case UP:
            int beststart, bestend;
            beststart = -1;
            bestend = -1;
            for (int i = 0; i < candidates.length; i++) {
                int end = buffer.getSpanEnd(candidates[i]);
                if (end < selEnd || selStart == selEnd) {
                    if (end > bestend) {
                        beststart = buffer.getSpanStart(candidates[i]);
                        bestend = end;
                    }
                }
            }
            if (beststart >= 0) {
                Selection.setSelection(buffer, bestend, beststart);
                return true;
            }
            break;
        case DOWN:
            beststart = Integer.MAX_VALUE;
            bestend = Integer.MAX_VALUE;
            for (int i = 0; i < candidates.length; i++) {
                int start = buffer.getSpanStart(candidates[i]);
                if (start > selStart || selStart == selEnd) {
                    if (start < beststart) {
                        beststart = start;
                        bestend = buffer.getSpanEnd(candidates[i]);
                    }
                }
            }
            if (bestend < Integer.MAX_VALUE) {
                Selection.setSelection(buffer, beststart, bestend);
                return true;
            }
            break;
    }
    return false;
}
Also used : Layout(android.text.Layout) ClickableSpan(android.text.style.ClickableSpan)

Example 75 with ClickableSpan

use of android.text.style.ClickableSpan in project android_frameworks_base by crdroidandroid.

the class LinkMovementMethod method onTouchEvent.

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();
        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);
        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
        if (link.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
                link[0].onClick(widget);
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
            }
            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }
    return super.onTouchEvent(widget, buffer, event);
}
Also used : Layout(android.text.Layout) ClickableSpan(android.text.style.ClickableSpan)

Aggregations

ClickableSpan (android.text.style.ClickableSpan)133 View (android.view.View)76 TextView (android.widget.TextView)55 TextPaint (android.text.TextPaint)50 SpannableString (android.text.SpannableString)46 Layout (android.text.Layout)37 Spannable (android.text.Spannable)28 SpannableStringBuilder (android.text.SpannableStringBuilder)24 Intent (android.content.Intent)17 NonNull (androidx.annotation.NonNull)15 Paint (android.graphics.Paint)14 Context (android.content.Context)12 Spanned (android.text.Spanned)12 URLSpan (android.text.style.URLSpan)12 ImageView (android.widget.ImageView)12 StyleSpan (android.text.style.StyleSpan)9 Uri (android.net.Uri)8 SuppressLint (android.annotation.SuppressLint)7 InputMethodManager (android.view.inputmethod.InputMethodManager)7 DialogInterface (android.content.DialogInterface)6