Sorting out extracts for shared URLs.

This commit is contained in:
PhotonQyv 2017-09-01 14:02:09 +01:00
parent a82db61579
commit e27cd6a147
2 changed files with 157 additions and 1 deletions

View File

@ -100,6 +100,7 @@ import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.drawers.AccountsSearchAdapter;
import fr.gouv.etalab.mastodon.drawers.DraftsListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.helper.ParserUtils;
import fr.gouv.etalab.mastodon.interfaces.OnPostStatusActionInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveSearcAccountshInterface;
@ -118,7 +119,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
* Toot activity class
*/
public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface {
public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface, ParserUtils.ParserListener {
private String visibility;
@ -241,6 +242,14 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
tootReply = b.getParcelable("tootReply");
sharedContent = b.getString("sharedContent", null);
sharedSubject = b.getString("sharedSubject", null);
// ACTION_SEND_TEXT route
if (sharedContent != null)
{
// Do Extract from URL here
final ParserUtils parser = new ParserUtils(this);
}
// ACTION_SEND route
if (b.getInt("uriNumber", 0) == 1) {
@ -1313,5 +1322,12 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
}
}
@Override
public void onReceiveHeaderInfo(ParserUtils.HeaderInfo headerInfo) {
}
@Override
public void onErrorHeaderInfo() {
Toast.makeText(this.getApplicationContext(), "An error has occurred.", Toast.LENGTH_SHORT).show();
}
}

View File

@ -0,0 +1,140 @@
package fr.gouv.etalab.mastodon.helper;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
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 java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import static com.keylesspalace.tusky.util.StringUtils.QUOTE;
/**
* Inspect and get the information from a URL.
*/
public class ParserUtils {
public final static String carriageReturn = System.getProperty("line.separator");
final private static String QUOTE = "\"";
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();
// If we share with an app, it's not only an url
List<String> strings = extractUrl(pasteData);
if (strings.size() > 0) {
String url = strings.get(0); // we assume that the first url is the good one
if (URLUtil.isValidUrl(url)) {
new ThreadHeaderInfo().execute(url);
}
}
}
return null;
}
public void putInClipboardManager(Context context, String string) {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", string);
clipboard.setPrimaryClip(clip);
}
/** 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 + QUOTE;
}
if (!TextUtils.isEmpty(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();
}
}
}
static List<String> extractUrl(String text) {
List<String> links = new ArrayList<>();
Matcher m = Patterns.WEB_URL.matcher(text);
while (m.find()) {
String url = m.group();
links.add(url);
}
return links;
}
}