Android Open Source - karin Status Text Builder






From Project

Back to project page karin.

License

The source code is released under:

MIT License

If you think the Android project karin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cc.wanko.karin.app.textbuilder;
//from   w w w  .ja v a  2  s. c o m
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import twitter4j.MediaEntity;
import twitter4j.Status;
import twitter4j.URLEntity;
import twitter4j.UserMentionEntity;

/**
 * Created by eagletmt on 14/05/06.
 */
public class StatusTextBuilder {
    private Context context;

    public StatusTextBuilder(Context context) {
        this.context = context;
    }

    public SpannableStringBuilder buildStatus(Status status) {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        List<Segment> segments = new ArrayList<Segment>();
        for (URLEntity entity : status.getURLEntities()) {
            segments.add(new UrlSegment(entity));
        }
        for (UserMentionEntity entity : status.getUserMentionEntities()) {
            segments.add(new MentionSegment(entity));
        }
        for (MediaEntity entity : status.getMediaEntities()) {
            segments.add(new MediaSegment(entity));
        }

        Collections.sort(segments, new Comparator<Segment>() {
            @Override
            public int compare(Segment s1, Segment s2) {
                return s1.start - s2.start;
            }
        });

        String text = status.getText();
        int textIndex = 0;
        for (final Segment segment : segments) {
            if (textIndex != segment.start) {
                builder.append(text.substring(textIndex, segment.start));
            }
            textIndex = segment.end;

            int spanStart = builder.length();
            builder.append(segment.text);
            builder.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    segment.onClick(context);
                }
            }, spanStart, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (textIndex != text.length()) {
            builder.append(text.substring(textIndex));
        }

        return builder;
    }
}




Java Source Code List

cc.wanko.karin.app.activities.MainActivity.java
cc.wanko.karin.app.activities.OAuthActivity.java
cc.wanko.karin.app.activities.UserListActivity.java
cc.wanko.karin.app.activities.UserStatusesActivity.java
cc.wanko.karin.app.adapters.MainPagerAdapter.java
cc.wanko.karin.app.adapters.StatusListAdapter.java
cc.wanko.karin.app.adapters.UserListListAdapter.java
cc.wanko.karin.app.client.HomeTimelineSource.java
cc.wanko.karin.app.client.StatusSource.java
cc.wanko.karin.app.client.TwitterProvider.java
cc.wanko.karin.app.client.UserListSource.java
cc.wanko.karin.app.client.UserStatusSource.java
cc.wanko.karin.app.database.Database.java
cc.wanko.karin.app.fragments.StatusListFragment.java
cc.wanko.karin.app.fragments.UserListListFragment.java
cc.wanko.karin.app.textbuilder.MediaSegment.java
cc.wanko.karin.app.textbuilder.MentionSegment.java
cc.wanko.karin.app.textbuilder.Segment.java
cc.wanko.karin.app.textbuilder.StatusTextBuilder.java
cc.wanko.karin.app.textbuilder.UrlSegment.java
cc.wanko.karin.app.utils.LruImageCache.java
cc.wanko.karin.app.utils.RoboViewHolder.java