Search in sources :

Example 21 with Annotation

use of android.text.Annotation in project Signal-Android by signalapp.

the class MentionValidatorWatcher method onTextChanged.

@Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
    if (count > 1 && mentionValidator != null && sequence instanceof Spanned) {
        Spanned span = (Spanned) sequence;
        List<Annotation> mentionAnnotations = MentionAnnotation.getMentionAnnotations(span, start, start + count);
        if (mentionAnnotations.size() > 0) {
            invalidMentionAnnotations = mentionValidator.getInvalidMentionAnnotations(mentionAnnotations);
        }
    }
}
Also used : Spanned(android.text.Spanned) Annotation(android.text.Annotation)

Example 22 with Annotation

use of android.text.Annotation in project Signal-Android by signalapp.

the class MentionRendererDelegate method draw.

public void draw(@NonNull Canvas canvas, @NonNull Spanned text, @NonNull Layout layout) {
    Annotation[] annotations = text.getSpans(0, text.length(), Annotation.class);
    for (Annotation annotation : annotations) {
        if (MentionAnnotation.isMentionAnnotation(annotation)) {
            int spanStart = text.getSpanStart(annotation);
            int spanEnd = text.getSpanEnd(annotation);
            int startLine = layout.getLineForOffset(spanStart);
            int endLine = layout.getLineForOffset(spanEnd);
            int startOffset = (int) (layout.getPrimaryHorizontal(spanStart) + -1 * layout.getParagraphDirection(startLine) * horizontalPadding);
            int endOffset = (int) (layout.getPrimaryHorizontal(spanEnd) + layout.getParagraphDirection(endLine) * horizontalPadding);
            MentionRenderer renderer = (startLine == endLine) ? single : multi;
            renderer.draw(canvas, layout, startLine, endLine, startOffset, endOffset);
        }
    }
}
Also used : Annotation(android.text.Annotation)

Example 23 with Annotation

use of android.text.Annotation in project Signal-Android by signalapp.

the class RecipientsAdapter method convertToString.

@Override
public final CharSequence convertToString(Cursor cursor) {
    String name = cursor.getString(RecipientsAdapter.NAME_INDEX);
    int type = cursor.getInt(RecipientsAdapter.TYPE_INDEX);
    String number = cursor.getString(RecipientsAdapter.NUMBER_INDEX).trim();
    String label = cursor.getString(RecipientsAdapter.LABEL_INDEX);
    CharSequence displayLabel = mContactAccessor.phoneTypeToString(mContext, type, label);
    if (number.length() == 0) {
        return number;
    }
    if (name == null) {
        name = "";
    } else {
        // Names with commas are the bane of the recipient editor's existence.
        // We've worked around them by using spans, but there are edge cases
        // where the spans get deleted. Furthermore, having commas in names
        // can be confusing to the user since commas are used as separators
        // between recipients. The best solution is to simply remove commas
        // from names.
        name = name.replace(", ", " ").replace(",", // Make sure we leave a space between parts of names.
        " ");
    }
    String nameAndNumber = RecipientsFormatter.formatNameAndNumber(name, number);
    SpannableString out = new SpannableString(nameAndNumber);
    int len = out.length();
    if (!TextUtils.isEmpty(name)) {
        out.setSpan(new Annotation("name", name), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        out.setSpan(new Annotation("name", number), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    String person_id = cursor.getString(RecipientsAdapter.CONTACT_ID_INDEX);
    out.setSpan(new Annotation("person_id", person_id), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    out.setSpan(new Annotation("label", displayLabel.toString()), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    out.setSpan(new Annotation("number", number), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return out;
}
Also used : SpannableString(android.text.SpannableString) SpannableString(android.text.SpannableString) Annotation(android.text.Annotation)

Example 24 with Annotation

use of android.text.Annotation in project Signal-Android by signalapp.

the class RecipientsEditor method getFieldAt.

private static String getFieldAt(String field, Spanned sp, int start, int end, Context context) {
    Annotation[] a = sp.getSpans(start, end, Annotation.class);
    String fieldValue = getAnnotation(a, field);
    if (TextUtils.isEmpty(fieldValue)) {
        fieldValue = TextUtils.substring(sp, start, end);
    }
    return fieldValue;
}
Also used : SpannableString(android.text.SpannableString) Annotation(android.text.Annotation)

Example 25 with Annotation

use of android.text.Annotation in project android_packages_apps_Settings by omnirom.

the class AnnotationSpan method linkify.

public static CharSequence linkify(CharSequence rawText, LinkInfo... linkInfos) {
    SpannableString msg = new SpannableString(rawText);
    Annotation[] spans = msg.getSpans(0, msg.length(), Annotation.class);
    SpannableStringBuilder builder = new SpannableStringBuilder(msg);
    for (Annotation annotation : spans) {
        final String key = annotation.getValue();
        int start = msg.getSpanStart(annotation);
        int end = msg.getSpanEnd(annotation);
        AnnotationSpan link = null;
        for (LinkInfo linkInfo : linkInfos) {
            if (linkInfo.mAnnotation.equals(key)) {
                link = new AnnotationSpan(linkInfo.mListener);
                break;
            }
        }
        if (link != null) {
            builder.setSpan(link, start, end, msg.getSpanFlags(link));
        }
    }
    return builder;
}
Also used : SpannableString(android.text.SpannableString) SpannableString(android.text.SpannableString) Annotation(android.text.Annotation) SpannableStringBuilder(android.text.SpannableStringBuilder) TextPaint(android.text.TextPaint)

Aggregations

Annotation (android.text.Annotation)25 SpannableString (android.text.SpannableString)15 SpannableStringBuilder (android.text.SpannableStringBuilder)7 Spanned (android.text.Spanned)6 MentionAnnotation (org.thoughtcrime.securesms.components.mention.MentionAnnotation)6 TextPaint (android.text.TextPaint)5 ActivityNotFoundException (android.content.ActivityNotFoundException)3 Context (android.content.Context)3 Intent (android.content.Intent)3 URLSpan (android.text.style.URLSpan)3 View (android.view.View)3 ValueAnimator (android.animation.ValueAnimator)2 SuppressLint (android.annotation.SuppressLint)2 Color (android.graphics.Color)2 PorterDuff (android.graphics.PorterDuff)2 Rect (android.graphics.Rect)2 Typeface (android.graphics.Typeface)2 Uri (android.net.Uri)2 Spannable (android.text.Spannable)2 BackgroundColorSpan (android.text.style.BackgroundColorSpan)2