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;
}
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) {
}
}));
}
}
Aggregations