2017-09-02 10:13:40 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Mastalab
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
package fr.gouv.etalab.mastodon.asynctasks;
|
|
|
|
|
2018-01-19 18:59:30 +01:00
|
|
|
import android.content.Context;
|
2017-09-02 10:13:40 +02:00
|
|
|
import android.os.AsyncTask;
|
2017-11-29 11:35:26 +01:00
|
|
|
import android.os.Build;
|
2018-03-13 19:20:00 +01:00
|
|
|
import android.text.Html;
|
2017-11-29 11:35:26 +01:00
|
|
|
import android.util.Patterns;
|
2017-09-02 10:13:40 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-01-19 18:59:30 +01:00
|
|
|
import java.lang.ref.WeakReference;
|
2017-12-10 18:37:58 +01:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2017-11-29 11:35:26 +01:00
|
|
|
import java.util.regex.Matcher;
|
2017-12-10 18:37:58 +01:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import fr.gouv.etalab.mastodon.client.HttpsConnection;
|
2017-11-29 11:35:26 +01:00
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
2017-09-02 10:13:40 +02:00
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 02/09/2017.
|
|
|
|
* Retrieves metadata of a remote page
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class RetrieveMetaDataAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
|
|
|
|
|
|
private OnRetrieveMetaDataInterface listener;
|
|
|
|
private String url;
|
|
|
|
private boolean error = false;
|
2018-08-16 12:08:38 +02:00
|
|
|
private String image, title, description, sharedSubject, sharedText;
|
2018-01-19 18:59:30 +01:00
|
|
|
private WeakReference<Context> contextWeakReference;
|
2017-09-02 10:13:40 +02:00
|
|
|
|
2018-08-16 12:08:38 +02:00
|
|
|
public RetrieveMetaDataAsyncTask(Context context, String sharedSubject, String sharedText,String url, OnRetrieveMetaDataInterface onRetrieveRemoteAccountInterface){
|
2017-09-02 10:13:40 +02:00
|
|
|
this.url = url;
|
|
|
|
this.listener = onRetrieveRemoteAccountInterface;
|
2018-08-16 12:08:38 +02:00
|
|
|
this.sharedText = sharedText;
|
|
|
|
this.sharedSubject = sharedSubject;
|
2018-01-19 18:59:30 +01:00
|
|
|
this.contextWeakReference = new WeakReference<>(context);
|
2017-09-02 10:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
2017-11-29 11:55:41 +01:00
|
|
|
String potentialUrl = "";
|
2018-09-02 13:46:33 +02:00
|
|
|
if (url == null) {
|
|
|
|
error = true;
|
|
|
|
return null;
|
|
|
|
}
|
2017-09-02 10:13:40 +02:00
|
|
|
try {
|
2017-11-29 11:35:26 +01:00
|
|
|
Matcher matcher;
|
2017-11-29 12:32:08 +01:00
|
|
|
if (url.startsWith("www."))
|
|
|
|
url = "http://" + url;
|
|
|
|
|
2017-11-29 11:35:26 +01:00
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
|
|
|
|
matcher = Patterns.WEB_URL.matcher(url);
|
|
|
|
else
|
|
|
|
matcher = Helper.urlPattern.matcher(url);
|
|
|
|
while (matcher.find()){
|
|
|
|
int matchStart = matcher.start(1);
|
|
|
|
int matchEnd = matcher.end();
|
|
|
|
if(matchStart < matchEnd && url.length() >= matchEnd)
|
|
|
|
potentialUrl = url.substring(matchStart, matchEnd);
|
2017-09-02 10:13:40 +02:00
|
|
|
}
|
2017-11-29 11:35:26 +01:00
|
|
|
// If we actually have a URL then make use of it.
|
|
|
|
if (potentialUrl.length() > 0) {
|
2018-03-23 19:39:01 +01:00
|
|
|
Pattern titlePattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:title[\"']\\s+content=[\"']([^>]*)[\"']");
|
|
|
|
Pattern descriptionPattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:description[\"']\\s+content=[\"']([^>]*)[\"']");
|
|
|
|
Pattern imagePattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:image[\"']\\s+content=[\"']([^>]*)[\"']");
|
2017-12-10 18:37:58 +01:00
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(this.contextWeakReference.get()).get(potentialUrl);
|
2017-12-10 18:37:58 +01:00
|
|
|
Matcher matcherTitle = titlePattern.matcher(response);
|
|
|
|
Matcher matcherDescription = descriptionPattern.matcher(response);
|
|
|
|
Matcher matcherImage = imagePattern.matcher(response);
|
2018-03-13 19:20:00 +01:00
|
|
|
String titleEncoded = null;
|
|
|
|
String descriptionEncoded = null;
|
2017-12-10 18:37:58 +01:00
|
|
|
while (matcherTitle.find())
|
2018-03-13 19:20:00 +01:00
|
|
|
titleEncoded = matcherTitle.group(1);
|
2017-12-10 18:37:58 +01:00
|
|
|
while (matcherDescription.find())
|
2018-03-13 19:20:00 +01:00
|
|
|
descriptionEncoded = matcherDescription.group(1);
|
2017-12-10 18:37:58 +01:00
|
|
|
while (matcherImage.find())
|
|
|
|
image = matcherImage.group(1);
|
2018-03-23 19:39:01 +01:00
|
|
|
|
2018-03-13 19:20:00 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
2018-03-23 19:39:01 +01:00
|
|
|
if( titleEncoded != null)
|
|
|
|
title = Html.fromHtml(titleEncoded, Html.FROM_HTML_MODE_LEGACY).toString();
|
|
|
|
if( descriptionEncoded != null)
|
|
|
|
description = Html.fromHtml(descriptionEncoded, Html.FROM_HTML_MODE_LEGACY).toString();
|
2018-03-13 19:20:00 +01:00
|
|
|
}else {
|
|
|
|
//noinspection deprecation
|
2018-03-23 19:39:01 +01:00
|
|
|
if( titleEncoded != null)
|
|
|
|
title = Html.fromHtml(titleEncoded).toString();
|
|
|
|
if( descriptionEncoded != null)
|
|
|
|
description = Html.fromHtml(descriptionEncoded).toString();
|
2018-03-13 19:20:00 +01:00
|
|
|
}
|
2017-12-10 18:37:58 +01:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-29 11:35:26 +01:00
|
|
|
}
|
2017-09-02 10:13:40 +02:00
|
|
|
}
|
|
|
|
} catch (IOException | IndexOutOfBoundsException e) {
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
2018-08-16 12:08:38 +02:00
|
|
|
listener.onRetrieveMetaData(error, sharedSubject, sharedText, image, title, description);
|
2017-09-02 10:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|