From 6dd2aba3c6871537d4f94b3f79b44381aa5a1602 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Tue, 16 May 2017 10:42:31 +0200 Subject: [PATCH] (parser utils) class who will get header information of an given Url - will redistribute that to the activity by listener --- .../keylesspalace/tusky/util/ParserUtils.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 app/src/main/java/com/keylesspalace/tusky/util/ParserUtils.java diff --git a/app/src/main/java/com/keylesspalace/tusky/util/ParserUtils.java b/app/src/main/java/com/keylesspalace/tusky/util/ParserUtils.java new file mode 100644 index 000000000..215765261 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/util/ParserUtils.java @@ -0,0 +1,113 @@ +package com.keylesspalace.tusky.util; + +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.os.AsyncTask; +import android.text.TextUtils; +import android.webkit.URLUtil; + +import org.jsoup.Connection; +import org.jsoup.Jsoup; +import org.jsoup.helper.HttpConnection; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; + +import static com.keylesspalace.tusky.util.StringUtils.QUOTE; + +/** + * Inspect and Get the information from an URL + */ +public class ParserUtils { + private static final String TAG = "ParserUtils"; + private ParserListener parserListener; + + public ParserUtils(ParserListener parserListener) { + this.parserListener = parserListener; + } + + // ComposeActivity : EditText inside the onTextChanged + public String getPastedURLText(Context context) { + ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + String pasteData; + if (clipboard.hasPrimaryClip()) { + // get what is in the clipboard + ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0); + pasteData = item.getText().toString(); + + // we have to find an url for start it + if (URLUtil.isValidUrl(pasteData)) { + new ThreadHeaderInfo().execute(pasteData); + } + } + return null; + } + + // parse the HTML page + private HeaderInfo parsePageHeaderInfo(String urlStr) throws Exception { + Connection con = Jsoup.connect(urlStr); + HeaderInfo headerInfo = new HeaderInfo(); + con.userAgent(HttpConnection.DEFAULT_UA); + Document doc = con.get(); + + // get info + String text; + Elements metaOgTitle = doc.select("meta[property=og:title]"); + if (metaOgTitle != null) { + text = metaOgTitle.attr("content"); + } else { + text = doc.title(); + } + + String imageUrl = null; + Elements metaOgImage = doc.select("meta[property=og:image]"); + if (metaOgImage != null) { + imageUrl = metaOgImage.attr("content"); + } + + // set info + headerInfo.baseUrl = urlStr; + if (!TextUtils.isEmpty(text)) { + headerInfo.title = QUOTE + text.toUpperCase() + QUOTE; + } + if (!TextUtils.isEmpty(imageUrl)) { + if (URLUtil.isValidUrl(imageUrl)) { + headerInfo.image = (imageUrl); + } + } + return headerInfo; + } + + public interface ParserListener { + void onReceiveHeaderInfo(HeaderInfo headerInfo); + + void onErrorHeaderInfo(); + } + + public class HeaderInfo { + public String baseUrl; + public String title; + public String image; + } + + private class ThreadHeaderInfo extends AsyncTask { + protected HeaderInfo doInBackground(String... urls) { + try { + String url = urls[0]; + return parsePageHeaderInfo(url); + } catch (Exception e) { + Log.e(TAG, "ThreadHeaderInfo#parsePageHeaderInfo() failed." + e.getMessage()); + return null; + } + } + + protected void onPostExecute(HeaderInfo headerInfo) { + if (headerInfo != null) { + Log.i(TAG, "ThreadHeaderInfo#parsePageHeaderInfo() success." + headerInfo.title + " " + headerInfo.image); + parserListener.onReceiveHeaderInfo(headerInfo); + } else { + parserListener.onErrorHeaderInfo(); + } + } + } +}