Search in sources :

Example 1 with AlLinkPreviewModel

use of com.applozic.mobicomkit.uiwidgets.conversation.richmessaging.models.AlLinkPreviewModel in project Applozic-Android-SDK by AppLozic.

the class AlLinkPreview method getMetaTags.

private static AlLinkPreviewModel getMetaTags(Document doc, Message message) {
    AlLinkPreviewModel linkPreviewModel = new AlLinkPreviewModel();
    String url = getValidUrl(message);
    try {
        Elements elements = doc.getElementsByTag("meta");
        String title = doc.select("meta[property=og:title]").attr("content");
        Utils.printLog(ApplozicService.getAppContext(), "LinkTest", "Title : " + title);
        if (!TextUtils.isEmpty(title)) {
            linkPreviewModel.setTitle(title);
        } else {
            linkPreviewModel.setTitle(doc.title());
        }
        // getDescription
        String description = doc.select("meta[name=description]").attr("content");
        if (description.isEmpty() || description == null) {
            description = doc.select("meta[name=Description]").attr("content");
        }
        if (description.isEmpty() || description == null) {
            description = doc.select("meta[property=og:description]").attr("content");
        }
        if (description.isEmpty() || description == null) {
            description = "";
        }
        linkPreviewModel.setDescription(description);
        // getImages
        Elements imageElements = doc.select("meta[property=og:image]");
        if (imageElements.size() > 0) {
            String image = imageElements.attr("content");
            if (!TextUtils.isEmpty(image)) {
                linkPreviewModel.setImageLink(resolveURL(url, image));
            }
        }
        if (TextUtils.isEmpty(linkPreviewModel.getImageLink())) {
            String src = doc.select("link[rel=image_src]").attr("href");
            if (!TextUtils.isEmpty(src)) {
                linkPreviewModel.setImageLink(resolveURL(url, src));
            } else {
                src = doc.select("link[rel=apple-touch-icon]").attr("href");
                if (!TextUtils.isEmpty(src)) {
                    linkPreviewModel.setImageLink(resolveURL(url, src));
                } else {
                    src = doc.select("link[rel=icon]").attr("href");
                    if (!TextUtils.isEmpty(src)) {
                        linkPreviewModel.setImageLink(resolveURL(url, src));
                    }
                }
            }
        }
        // Favicon
        String src = doc.select("link[rel=apple-touch-icon]").attr("href");
        if (!TextUtils.isEmpty(src) && TextUtils.isEmpty(linkPreviewModel.getImageLink())) {
            linkPreviewModel.setImageLink(resolveURL(url, src));
        } else {
            src = doc.select("link[rel=icon]").attr("href");
            if (!TextUtils.isEmpty(src) && TextUtils.isEmpty(linkPreviewModel.getImageLink())) {
                linkPreviewModel.setImageLink(resolveURL(url, src));
            }
        }
        for (Element element : elements) {
            if (element.hasAttr("property")) {
                String strProperty = element.attr("property").toString().trim();
                if (strProperty.equals("og:url")) {
                    linkPreviewModel.setUrl(element.attr("content").toString());
                }
                if (strProperty.equals("og:site_name") && TextUtils.isEmpty(linkPreviewModel.getTitle())) {
                    linkPreviewModel.setTitle(element.attr("content").toString());
                }
            }
        }
        if (TextUtils.isEmpty(linkPreviewModel.getUrl())) {
            URI uri = null;
            try {
                uri = new URI(url);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            linkPreviewModel.setUrl(uri == null ? url : uri.getHost());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return linkPreviewModel;
}
Also used : AlLinkPreviewModel(com.applozic.mobicomkit.uiwidgets.conversation.richmessaging.models.AlLinkPreviewModel) Element(org.jsoup.nodes.Element) URISyntaxException(java.net.URISyntaxException) Elements(org.jsoup.select.Elements) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) HttpStatusException(org.jsoup.HttpStatusException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 2 with AlLinkPreviewModel

use of com.applozic.mobicomkit.uiwidgets.conversation.richmessaging.models.AlLinkPreviewModel in project Applozic-Android-SDK by AppLozic.

the class AlLinkPreview method createView.

public void createView() {
    AlLinkPreviewModel existingLinkModel = getUrlMetaModel();
    if (existingLinkModel != null) {
        updateViews(existingLinkModel);
    } else {
        urlLoadLayout.setVisibility(View.GONE);
        AlTask.execute(new UrlLoader(context, message, new AlCallback() {

            @Override
            public void onSuccess(Object response) {
                updateViews((AlLinkPreviewModel) response);
            }

            @Override
            public void onError(Object error) {
            }
        }));
    }
}
Also used : AlLinkPreviewModel(com.applozic.mobicomkit.uiwidgets.conversation.richmessaging.models.AlLinkPreviewModel) AlCallback(com.applozic.mobicomkit.listners.AlCallback)

Aggregations

AlLinkPreviewModel (com.applozic.mobicomkit.uiwidgets.conversation.richmessaging.models.AlLinkPreviewModel)2 AlCallback (com.applozic.mobicomkit.listners.AlCallback)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpStatusException (org.jsoup.HttpStatusException)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1