Search in sources :

Example 56 with Spanned

use of android.text.Spanned in project AntennaPod by AntennaPod.

the class ChaptersListAdapter method getView.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder;
    Chapter sc = getItem(position);
    // Inflate Layout
    if (convertView == null) {
        holder = new Holder();
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.simplechapter_item, parent, false);
        holder.view = convertView;
        holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
        defaultTextColor = holder.title.getTextColors().getDefaultColor();
        holder.start = (TextView) convertView.findViewById(R.id.txtvStart);
        holder.link = (TextView) convertView.findViewById(R.id.txtvLink);
        holder.butPlayChapter = (ImageButton) convertView.findViewById(R.id.butPlayChapter);
        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }
    holder.title.setText(sc.getTitle());
    holder.start.setText(Converter.getDurationStringLong((int) sc.getStart()));
    if (sc.getLink() != null) {
        holder.link.setVisibility(View.VISIBLE);
        holder.link.setText(sc.getLink());
        Linkify.addLinks(holder.link, Linkify.WEB_URLS);
    } else {
        holder.link.setVisibility(View.GONE);
    }
    holder.link.setMovementMethod(null);
    holder.link.setOnTouchListener((v, event) -> {
        // from
        // http://stackoverflow.com/questions/7236840/android-textview-linkify-intercepts-with-parent-view-gestures
        TextView widget = (TextView) v;
        Object text = widget.getText();
        if (text instanceof Spanned) {
            Spannable buffer = (Spannable) text;
            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;
                }
            }
        }
        return false;
    });
    holder.butPlayChapter.setOnClickListener(v -> {
        if (callback != null) {
            callback.onPlayChapterButtonClicked(position);
        }
    });
    Chapter current = ChapterUtils.getCurrentChapter(media);
    if (current != null) {
        if (current == sc) {
            int playingBackGroundColor;
            if (UserPreferences.getTheme() == R.style.Theme_AntennaPod_Dark) {
                playingBackGroundColor = ContextCompat.getColor(getContext(), R.color.highlight_dark);
            } else {
                playingBackGroundColor = ContextCompat.getColor(getContext(), R.color.highlight_light);
            }
            holder.view.setBackgroundColor(playingBackGroundColor);
        } else {
            holder.view.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
            holder.title.setTextColor(defaultTextColor);
            holder.start.setTextColor(defaultTextColor);
        }
    } else {
        Log.w(TAG, "Could not find out what the current chapter is.");
    }
    return convertView;
}
Also used : Layout(android.text.Layout) LayoutInflater(android.view.LayoutInflater) Chapter(de.danoeh.antennapod.core.feed.Chapter) TextView(android.widget.TextView) Spanned(android.text.Spanned) ClickableSpan(android.text.style.ClickableSpan) Spannable(android.text.Spannable)

Example 57 with Spanned

use of android.text.Spanned in project android_frameworks_base by DirtyUnicorns.

the class TextView method setExtractedText.

/**
     * Apply to this text view the given extracted text, as previously
     * returned by {@link #extractText(ExtractedTextRequest, ExtractedText)}.
     */
public void setExtractedText(ExtractedText text) {
    Editable content = getEditableText();
    if (text.text != null) {
        if (content == null) {
            setText(text.text, TextView.BufferType.EDITABLE);
        } else {
            int start = 0;
            int end = content.length();
            if (text.partialStartOffset >= 0) {
                final int N = content.length();
                start = text.partialStartOffset;
                if (start > N)
                    start = N;
                end = text.partialEndOffset;
                if (end > N)
                    end = N;
            }
            removeParcelableSpans(content, start, end);
            if (TextUtils.equals(content.subSequence(start, end), text.text)) {
                if (text.text instanceof Spanned) {
                    // OK to copy spans only.
                    TextUtils.copySpansFrom((Spanned) text.text, 0, end - start, Object.class, content, start);
                }
            } else {
                content.replace(start, end, text.text);
            }
        }
    }
    // Now set the selection position...  make sure it is in range, to
    // avoid crashes.  If this is a partial update, it is possible that
    // the underlying text may have changed, causing us problems here.
    // Also we just don't want to trust clients to do the right thing.
    Spannable sp = (Spannable) getText();
    final int N = sp.length();
    int start = text.selectionStart;
    if (start < 0)
        start = 0;
    else if (start > N)
        start = N;
    int end = text.selectionEnd;
    if (end < 0)
        end = 0;
    else if (end > N)
        end = N;
    Selection.setSelection(sp, start, end);
    // Finally, update the selection mode.
    if ((text.flags & ExtractedText.FLAG_SELECTING) != 0) {
        MetaKeyKeyListener.startSelecting(this, sp);
    } else {
        MetaKeyKeyListener.stopSelecting(this, sp);
    }
}
Also used : Editable(android.text.Editable) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 58 with Spanned

use of android.text.Spanned in project android_frameworks_base by DirtyUnicorns.

the class TextView method setText.

private void setText(CharSequence text, BufferType type, boolean notifyBefore, int oldlen) {
    if (text == null) {
        text = "";
    }
    // If suggestions are not enabled, remove the suggestion spans from the text
    if (!isSuggestionsEnabled()) {
        text = removeSuggestionSpans(text);
    }
    if (!mUserSetTextScaleX)
        mTextPaint.setTextScaleX(1.0f);
    if (text instanceof Spanned && ((Spanned) text).getSpanStart(TextUtils.TruncateAt.MARQUEE) >= 0) {
        if (ViewConfiguration.get(mContext).isFadingMarqueeEnabled()) {
            setHorizontalFadingEdgeEnabled(true);
            mMarqueeFadeMode = MARQUEE_FADE_NORMAL;
        } else {
            setHorizontalFadingEdgeEnabled(false);
            mMarqueeFadeMode = MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS;
        }
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
    }
    int n = mFilters.length;
    for (int i = 0; i < n; i++) {
        CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
        if (out != null) {
            text = out;
        }
    }
    if (notifyBefore) {
        if (mText != null) {
            oldlen = mText.length();
            sendBeforeTextChanged(mText, 0, oldlen, text.length());
        } else {
            sendBeforeTextChanged("", 0, 0, text.length());
        }
    }
    boolean needEditableForNotification = false;
    if (mListeners != null && mListeners.size() != 0) {
        needEditableForNotification = true;
    }
    if (type == BufferType.EDITABLE || getKeyListener() != null || needEditableForNotification) {
        createEditorIfNeeded();
        mEditor.forgetUndoRedo();
        Editable t = mEditableFactory.newEditable(text);
        text = t;
        setFilters(t, mFilters);
        InputMethodManager imm = InputMethodManager.peekInstance();
        if (imm != null)
            imm.restartInput(this);
    } else if (type == BufferType.SPANNABLE || mMovement != null) {
        text = mSpannableFactory.newSpannable(text);
    } else if (!(text instanceof CharWrapper)) {
        text = TextUtils.stringOrSpannedString(text);
    }
    if (mAutoLinkMask != 0) {
        Spannable s2;
        if (type == BufferType.EDITABLE || text instanceof Spannable) {
            s2 = (Spannable) text;
        } else {
            s2 = mSpannableFactory.newSpannable(text);
        }
        if (Linkify.addLinks(s2, mAutoLinkMask)) {
            text = s2;
            type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;
            /*
                 * We must go ahead and set the text before changing the
                 * movement method, because setMovementMethod() may call
                 * setText() again to try to upgrade the buffer type.
                 */
            mText = text;
            // would prevent an arbitrary cursor displacement.
            if (mLinksClickable && !textCanBeSelected()) {
                setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }
    mBufferType = type;
    mText = text;
    if (mTransformation == null) {
        mTransformed = text;
    } else {
        mTransformed = mTransformation.getTransformation(text, this);
    }
    final int textLength = text.length();
    if (text instanceof Spannable && !mAllowTransformationLengthChange) {
        Spannable sp = (Spannable) text;
        // Remove any ChangeWatchers that might have come from other TextViews.
        final ChangeWatcher[] watchers = sp.getSpans(0, sp.length(), ChangeWatcher.class);
        final int count = watchers.length;
        for (int i = 0; i < count; i++) {
            sp.removeSpan(watchers[i]);
        }
        if (mChangeWatcher == null)
            mChangeWatcher = new ChangeWatcher();
        sp.setSpan(mChangeWatcher, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE | (CHANGE_WATCHER_PRIORITY << Spanned.SPAN_PRIORITY_SHIFT));
        if (mEditor != null)
            mEditor.addSpanWatchers(sp);
        if (mTransformation != null) {
            sp.setSpan(mTransformation, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
        if (mMovement != null) {
            mMovement.initialize(this, (Spannable) text);
            /*
                 * Initializing the movement method will have set the
                 * selection, so reset mSelectionMoved to keep that from
                 * interfering with the normal on-focus selection-setting.
                 */
            if (mEditor != null)
                mEditor.mSelectionMoved = false;
        }
    }
    if (mLayout != null) {
        checkForRelayout();
    }
    sendOnTextChanged(text, 0, oldlen, textLength);
    onTextChanged(text, 0, oldlen, textLength);
    notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);
    if (needEditableForNotification) {
        sendAfterTextChanged((Editable) text);
    }
    // SelectionModifierCursorController depends on textCanBeSelected, which depends on text
    if (mEditor != null)
        mEditor.prepareCursorControllers();
}
Also used : Editable(android.text.Editable) InputMethodManager(android.view.inputmethod.InputMethodManager) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 59 with Spanned

use of android.text.Spanned in project android_frameworks_base by DirtyUnicorns.

the class IconMarginSpan method drawLeadingMargin.

public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
    int st = ((Spanned) text).getSpanStart(this);
    int itop = layout.getLineTop(layout.getLineForOffset(st));
    if (dir < 0)
        x -= mBitmap.getWidth();
    c.drawBitmap(mBitmap, x, itop, p);
}
Also used : Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Example 60 with Spanned

use of android.text.Spanned in project android_frameworks_base by DirtyUnicorns.

the class NotificationColorUtil method invertCharSequenceColors.

/**
     * Inverts all the grayscale colors set by {@link android.text.style.TextAppearanceSpan}s on
     * the text.
     *
     * @param charSequence The text to process.
     * @return The color inverted text.
     */
public CharSequence invertCharSequenceColors(CharSequence charSequence) {
    if (charSequence instanceof Spanned) {
        Spanned ss = (Spanned) charSequence;
        Object[] spans = ss.getSpans(0, ss.length(), Object.class);
        SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
        for (Object span : spans) {
            Object resultSpan = span;
            if (span instanceof TextAppearanceSpan) {
                resultSpan = processTextAppearanceSpan((TextAppearanceSpan) span);
            }
            builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span), ss.getSpanFlags(span));
        }
        return builder;
    }
    return charSequence;
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

Spanned (android.text.Spanned)204 Paint (android.graphics.Paint)81 TextPaint (android.text.TextPaint)63 Spannable (android.text.Spannable)32 SpannableStringBuilder (android.text.SpannableStringBuilder)27 SpannableString (android.text.SpannableString)25 SuggestionSpan (android.text.style.SuggestionSpan)24 Editable (android.text.Editable)22 TextAppearanceSpan (android.text.style.TextAppearanceSpan)15 SpannedString (android.text.SpannedString)14 TypedArray (android.content.res.TypedArray)12 URLSpan (android.text.style.URLSpan)12 View (android.view.View)12 Context (android.content.Context)10 StyleSpan (android.text.style.StyleSpan)9 InputMethodManager (android.view.inputmethod.InputMethodManager)9 TextView (android.widget.TextView)9 Parcelable (android.os.Parcelable)8 WordIterator (android.text.method.WordIterator)7 CharacterStyle (android.text.style.CharacterStyle)7