fedilab-Android-App/app/src/main/java/app/fedilab/android/asynctasks/RetrieveMetaDataAsyncTask.java

134 lines
5.6 KiB
Java
Raw Normal View History

/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab 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.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.asynctasks;
2018-01-19 18:59:30 +01:00
import android.content.Context;
import android.os.Build;
2021-01-19 17:43:51 +01:00
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.util.Patterns;
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;
import java.util.regex.Matcher;
2017-12-10 18:37:58 +01:00
import java.util.regex.Pattern;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.HttpsConnection;
import app.fedilab.android.interfaces.OnRetrieveMetaDataInterface;
/**
* Created by Thomas on 02/09/2017.
* Retrieves metadata of a remote page
*/
2021-01-19 17:43:51 +01:00
public class RetrieveMetaDataAsyncTask {
2021-01-19 17:43:51 +01:00
private final OnRetrieveMetaDataInterface listener;
private final String sharedSubject;
private final String sharedText;
private final WeakReference<Context> contextWeakReference;
private final boolean shouldFetchMetaData;
private String url;
private boolean error = false;
2021-01-19 17:43:51 +01:00
private String image;
private String title;
private String description;
2019-09-06 17:55:14 +02:00
public RetrieveMetaDataAsyncTask(Context context, boolean shouldFetchMetaData, String sharedSubject, String sharedText, String url, OnRetrieveMetaDataInterface onRetrieveRemoteAccountInterface) {
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);
this.shouldFetchMetaData = shouldFetchMetaData;
2021-01-19 17:43:51 +01:00
doInBackground();
}
2021-01-19 17:43:51 +01:00
protected void doInBackground() {
new Thread(() -> {
if (shouldFetchMetaData) {
execRetrieveMetaDataInBackground();
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> listener.onRetrieveMetaData(error, sharedSubject, sharedText, image, title, description);
mainHandler.post(myRunnable);
}).start();
}
2019-09-06 17:55:14 +02:00
2021-01-19 17:43:51 +01:00
private void execRetrieveMetaDataInBackground() {
2017-11-29 11:55:41 +01:00
String potentialUrl = "";
2018-09-02 13:46:33 +02:00
if (url == null) {
error = true;
2021-01-19 17:43:51 +01:00
return;
2018-09-02 13:46:33 +02:00
}
try {
Matcher matcher;
if (url.startsWith("www."))
url = "http://" + url;
2021-01-19 17:43:51 +01:00
matcher = Patterns.WEB_URL.matcher(url);
2019-09-06 17:55:14 +02:00
while (matcher.find()) {
int matchStart = matcher.start(1);
int matchEnd = matcher.end();
2019-09-06 17:55:14 +02:00
if (matchStart < matchEnd && url.length() >= matchEnd)
potentialUrl = url.substring(matchStart, matchEnd);
}
// If we actually have a URL then make use of it.
if (potentialUrl.length() > 0) {
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 {
2020-03-27 18:57:47 +01:00
2019-05-23 07:46:34 +02:00
String response = new HttpsConnection(this.contextWeakReference.get(), null).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);
String titleEncoded = null;
String descriptionEncoded = null;
2017-12-10 18:37:58 +01:00
while (matcherTitle.find())
titleEncoded = matcherTitle.group(1);
2017-12-10 18:37:58 +01:00
while (matcherDescription.find())
descriptionEncoded = matcherDescription.group(1);
2017-12-10 18:37:58 +01:00
while (matcherImage.find())
image = matcherImage.group(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
2019-09-06 17:55:14 +02:00
if (titleEncoded != null)
title = Html.fromHtml(titleEncoded, Html.FROM_HTML_MODE_LEGACY).toString();
2019-09-06 17:55:14 +02:00
if (descriptionEncoded != null)
description = Html.fromHtml(descriptionEncoded, Html.FROM_HTML_MODE_LEGACY).toString();
2019-09-06 17:55:14 +02:00
} else {
if (titleEncoded != null)
title = Html.fromHtml(titleEncoded).toString();
2019-09-06 17:55:14 +02:00
if (descriptionEncoded != null)
description = Html.fromHtml(descriptionEncoded).toString();
}
2020-02-05 17:06:52 +01:00
} catch (NoSuchAlgorithmException | KeyManagementException | HttpsConnection.HttpsConnectionException e) {
error = true;
}
}
} catch (IOException | IndexOutOfBoundsException e) {
error = true;
}
}
2019-09-06 17:55:14 +02:00
}