Search in sources :

Example 1 with MDSpan

use of im.actor.runtime.markdown.MDSpan in project actor-platform by actorapp.

the class AndroidMarkdown method writeText.

private static void writeText(MDText[] texts, SpannableStringBuilder builder) {
    for (MDText text : texts) {
        if (text instanceof MDRawText) {
            builder.append(((MDRawText) text).getRawText());
        } else if (text instanceof MDSpan) {
            MDSpan span = (MDSpan) text;
            int start = builder.length();
            writeText(span.getChild(), builder);
            Object spanObj;
            if (span.getSpanType() == MDSpan.TYPE_BOLD) {
                spanObj = new StyleSpan(Typeface.BOLD);
            } else {
                spanObj = new StyleSpan(Typeface.ITALIC);
            }
            builder.setSpan(spanObj, start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (text instanceof MDUrl) {
            final MDUrl url = (MDUrl) text;
            int start = builder.length();
            builder.append(url.getUrlTitle());
            builder.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View view) {
                    Context ctx = view.getContext();
                    if (url.getUrl().startsWith("send:") && view.getTag(R.id.peer) != null && view.getTag(R.id.peer) instanceof Peer) {
                        ActorSDK.sharedActor().getMessenger().sendMessage((Peer) view.getTag(R.id.peer), url.getUrl().replaceFirst("send:", ""));
                    } else {
                        Intent intent = buildChromeIntent().intent;
                        intent.setData(Uri.parse(url.getUrl()));
                        if (intent.resolveActivity(ctx.getPackageManager()) != null) {
                            ctx.startActivity(intent);
                        } else {
                            intent.setData(Uri.parse("http://" + url.getUrl()));
                            if (intent.resolveActivity(ctx.getPackageManager()) != null) {
                                ctx.startActivity(intent);
                            } else {
                                Toast.makeText(view.getContext(), "Unknown URL type", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
            }, start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            throw new RuntimeException("Unknown text type: " + text);
        }
    }
}
Also used : Context(android.content.Context) AndroidContext(im.actor.runtime.android.AndroidContext) ActorContext(im.actor.runtime.actors.ActorContext) MDRawText(im.actor.runtime.markdown.MDRawText) MDUrl(im.actor.runtime.markdown.MDUrl) Peer(im.actor.core.entity.Peer) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MDSpan(im.actor.runtime.markdown.MDSpan) ClickableSpan(android.text.style.ClickableSpan) View(android.view.View) MDText(im.actor.runtime.markdown.MDText) StyleSpan(android.text.style.StyleSpan)

Aggregations

PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 Intent (android.content.Intent)1 CustomTabsIntent (android.support.customtabs.CustomTabsIntent)1 ClickableSpan (android.text.style.ClickableSpan)1 StyleSpan (android.text.style.StyleSpan)1 View (android.view.View)1 Peer (im.actor.core.entity.Peer)1 ActorContext (im.actor.runtime.actors.ActorContext)1 AndroidContext (im.actor.runtime.android.AndroidContext)1 MDRawText (im.actor.runtime.markdown.MDRawText)1 MDSpan (im.actor.runtime.markdown.MDSpan)1 MDText (im.actor.runtime.markdown.MDText)1 MDUrl (im.actor.runtime.markdown.MDUrl)1