Search in sources :

Example 21 with URLSpan

use of android.text.style.URLSpan in project AisenWeiBo by wangdan.

the class VideoService method parseStatusURL.

public static void parseStatusURL(List<StatusContent> statusContents) throws TaskException {
    if (statusContents.size() == 0)
        return;
    List<String> shortUrlList = new ArrayList<>();
    Map<String, List<StatusContent>> url2status = new HashMap<>();
    // 把未解析的短连接拎出来
    for (StatusContent statusContent : statusContents) {
        String content = statusContent.getText();
        if (!TextUtils.isEmpty(content)) {
            SpannableString spannableString = SpannableString.valueOf(content);
            Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
            URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
            for (URLSpan urlSpan : urlSpans) {
                if (!urlSpan.getURL().startsWith("http://t.cn/"))
                    continue;
                shortUrlList.add(urlSpan.getURL());
                List<StatusContent> l = url2status.get(urlSpan.getURL());
                if (l == null) {
                    l = new ArrayList<>();
                    url2status.put(urlSpan.getURL(), l);
                }
                if (!l.contains(statusContent))
                    l.add(statusContent);
            }
        }
        if (statusContent.getRetweeted_status() != null) {
            statusContent = statusContent.getRetweeted_status();
            content = statusContent.getText();
            if (!TextUtils.isEmpty(content)) {
                SpannableString spannableString = SpannableString.valueOf(content);
                Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
                URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
                for (URLSpan urlSpan : urlSpans) {
                    if (!urlSpan.getURL().startsWith("http://t.cn/"))
                        continue;
                    shortUrlList.add(urlSpan.getURL());
                    List<StatusContent> l = url2status.get(urlSpan.getURL());
                    if (l == null) {
                        l = new ArrayList<>();
                        url2status.put(urlSpan.getURL(), l);
                    }
                    if (!l.contains(statusContent))
                        l.add(statusContent);
                }
            }
        }
    }
    if (shortUrlList.size() > 0) {
        do {
            String[] parseArr = new String[20];
            for (int i = 0; i < parseArr.length; i++) {
                if (shortUrlList.size() > 0) {
                    parseArr[i] = shortUrlList.remove(0);
                }
            }
            Logger.w(TAG, parseArr);
            UrlsBean urlsBean = SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).urlShort2Long(parseArr);
            for (UrlBean urlBean : urlsBean.getUrls()) {
                String id = KeyGenerator.generateMD5(urlBean.getUrl_short());
                List<StatusContent> statusList = url2status.get(urlBean.getUrl_short());
                for (StatusContent s : statusList) {
                    s.setVideoUrl(urlBean);
                    boolean isNew = false;
                    VideoBean videoBean = SinaDB.getDB().selectById(null, VideoBean.class, id);
                    if (videoBean == null) {
                        videoBean = new VideoBean();
                        isNew = true;
                    }
                    videoBean.setIdStr(id);
                    videoBean.setShortUrl(urlBean.getUrl_short());
                    videoBean.setLongUrl(urlBean.getUrl_long());
                    if (isSinaVideo(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_SINA);
                        s.setVideo(true);
                    } else if (isWeipai(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_WEIPAI);
                        s.setVideo(true);
                    } else if (isMeipai(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_MEIPAI);
                        s.setVideo(true);
                    } else if (isPhoto(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_PHOTO);
                    } else {
                        videoBean.setType(VideoService.TYPE_VIDEO_NONE);
                    }
                    if (isNew) {
                        SinaDB.getDB().insert(null, videoBean);
                    } else {
                        SinaDB.getDB().update(null, videoBean);
                    }
                    Logger.v(TAG, "Id[%s], Type[%d], 短链[%s], 长链[%s]", videoBean.getIdStr(), videoBean.getType(), urlBean.getUrl_short(), urlBean.getUrl_long());
                }
            }
        } while (shortUrlList.size() > 0);
    }
}
Also used : StatusContent(org.aisen.weibo.sina.sinasdk.bean.StatusContent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) SpannableString(android.text.SpannableString) UrlBean(org.aisen.weibo.sina.sinasdk.bean.UrlBean) VideoBean(org.aisen.weibo.sina.support.bean.VideoBean) ArrayList(java.util.ArrayList) List(java.util.List) UrlsBean(org.aisen.weibo.sina.sinasdk.bean.UrlsBean)

Example 22 with URLSpan

use of android.text.style.URLSpan in project EhViewer by seven332.

the class HtmlToSpannedConverter method endA.

private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);
    if (where != len) {
        Href h = (Href) obj;
        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
Also used : URLSpan(android.text.style.URLSpan)

Example 23 with URLSpan

use of android.text.style.URLSpan in project EhViewer by seven332.

the class TextUrl method handleTextUrl.

public static CharSequence handleTextUrl(CharSequence content) {
    Matcher m = URL_PATTERN.matcher(content);
    Spannable spannable = null;
    while (m.find()) {
        // Ensure spannable
        if (spannable == null) {
            if (content instanceof Spannable) {
                spannable = (Spannable) content;
            } else {
                spannable = new SpannableString(content);
            }
        }
        int start = m.start();
        int end = m.end();
        URLSpan[] links = spannable.getSpans(start, end, URLSpan.class);
        if (links.length > 0) {
            // There has been URLSpan already, leave it alone
            continue;
        }
        URLSpan urlSpan = new URLSpan(m.group(0));
        spannable.setSpan(urlSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return spannable == null ? content : spannable;
}
Also used : SpannableString(android.text.SpannableString) Matcher(java.util.regex.Matcher) URLSpan(android.text.style.URLSpan) Spannable(android.text.Spannable)

Example 24 with URLSpan

use of android.text.style.URLSpan in project android_packages_apps_Dialer by LineageOS.

the class Vvm3Subscriber method findSubscribeLink.

@VisibleForTesting
static String findSubscribeLink(@NonNull List<Pattern> patterns, String response) throws ProvisioningException {
    if (patterns.isEmpty()) {
        throw new IllegalArgumentException("empty patterns");
    }
    Spanned doc = Html.fromHtml(response, Html.FROM_HTML_MODE_LEGACY);
    URLSpan[] spans = doc.getSpans(0, doc.length(), URLSpan.class);
    StringBuilder fulltext = new StringBuilder();
    for (URLSpan span : spans) {
        String text = doc.subSequence(doc.getSpanStart(span), doc.getSpanEnd(span)).toString();
        for (Pattern pattern : patterns) {
            if (pattern.matcher(text).matches()) {
                return span.getURL();
            }
        }
        fulltext.append(text);
    }
    throw new ProvisioningException("Subscribe link not found: " + fulltext);
}
Also used : Pattern(java.util.regex.Pattern) URLSpan(android.text.style.URLSpan) Spanned(android.text.Spanned) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 25 with URLSpan

use of android.text.style.URLSpan in project android_packages_apps_Settings by omnirom.

the class SupportDisclaimerDialogFragment method stripUnderlines.

/**
 * Removes the underlines of {@link android.text.style.URLSpan}s.
 */
private static void stripUnderlines(Spannable input) {
    final URLSpan[] urls = input.getSpans(0, input.length(), URLSpan.class);
    for (URLSpan span : urls) {
        final int start = input.getSpanStart(span);
        final int end = input.getSpanEnd(span);
        if (!TextUtils.isEmpty(span.getURL())) {
            input.removeSpan(span);
            input.setSpan(new NoUnderlineUrlSpan(span.getURL()), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
Also used : URLSpan(android.text.style.URLSpan) TextPaint(android.text.TextPaint)

Aggregations

URLSpan (android.text.style.URLSpan)134 SpannableString (android.text.SpannableString)36 Spannable (android.text.Spannable)22 SpannableStringBuilder (android.text.SpannableStringBuilder)21 TextPaint (android.text.TextPaint)21 Spanned (android.text.Spanned)20 TextView (android.widget.TextView)19 ForegroundColorSpan (android.text.style.ForegroundColorSpan)16 StyleSpan (android.text.style.StyleSpan)16 UnderlineSpan (android.text.style.UnderlineSpan)14 Paint (android.graphics.Paint)13 ImageSpan (android.text.style.ImageSpan)13 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)13 View (android.view.View)13 Intent (android.content.Intent)12 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)12 BackgroundColorSpan (android.text.style.BackgroundColorSpan)12 ClickableSpan (android.text.style.ClickableSpan)12 SubscriptSpan (android.text.style.SubscriptSpan)12