(parser utils) class who will get header information of an given Url

- will redistribute that to the activity by listener
main
torrentcome 8 years ago
parent 711ba62632
commit 1485fb6af1
  1. 113
      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<String, Void, HeaderInfo> {
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();
}
}
}
}
Loading…
Cancel
Save