mirror of
https://codeberg.org/tom79/Fedilab.git
synced 2024-12-23 01:00:04 +02:00
Fix issue #276 - Problem when sharing text with URL
This commit is contained in:
parent
3883af2f78
commit
60a1b2bfa4
2 changed files with 102 additions and 94 deletions
|
@ -852,103 +852,111 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
|
||||||
boolean fetchSharedMedia = sharedpreferences.getBoolean(getString(R.string.SET_RETRIEVE_METADATA_IF_URL_FROM_EXTERAL), true);
|
boolean fetchSharedMedia = sharedpreferences.getBoolean(getString(R.string.SET_RETRIEVE_METADATA_IF_URL_FROM_EXTERAL), true);
|
||||||
boolean fetchShareContent = sharedpreferences.getBoolean(getString(R.string.SET_SHARE_DETAILS), true);
|
boolean fetchShareContent = sharedpreferences.getBoolean(getString(R.string.SET_SHARE_DETAILS), true);
|
||||||
if (url[0] != null && count == 1 && (fetchShareContent || fetchSharedMedia)) {
|
if (url[0] != null && count == 1 && (fetchShareContent || fetchSharedMedia)) {
|
||||||
new Thread(() -> {
|
if (!url[0].trim().equalsIgnoreCase(sharedText.trim())) {
|
||||||
if (url[0].startsWith("www."))
|
Bundle b = new Bundle();
|
||||||
url[0] = "http://" + url[0];
|
b.putString(Helper.ARG_SHARE_TITLE, sharedSubject);
|
||||||
Matcher matcherPattern = Patterns.WEB_URL.matcher(url[0]);
|
b.putString(Helper.ARG_SHARE_DESCRIPTION, sharedText);
|
||||||
String potentialUrl = null;
|
CrossActionHelper.doCrossShare(BaseMainActivity.this, b);
|
||||||
while (matcherPattern.find()) {
|
} else {
|
||||||
int matchStart = matcherPattern.start(1);
|
new Thread(() -> {
|
||||||
int matchEnd = matcherPattern.end();
|
if (url[0].startsWith("www."))
|
||||||
if (matchStart < matchEnd && url[0].length() >= matchEnd)
|
url[0] = "http://" + url[0];
|
||||||
potentialUrl = url[0].substring(matchStart, matchEnd);
|
Matcher matcherPattern = Patterns.WEB_URL.matcher(url[0]);
|
||||||
}
|
String potentialUrl = null;
|
||||||
// If we actually have a URL then make use of it.
|
while (matcherPattern.find()) {
|
||||||
if (potentialUrl != null && potentialUrl.length() > 0) {
|
int matchStart = matcherPattern.start(1);
|
||||||
Pattern titlePattern = Pattern.compile("<meta [^>]*property=[\"']og:title[\"'] [^>]*content=[\"']([^'^\"]+?)[\"'][^>]*>");
|
int matchEnd = matcherPattern.end();
|
||||||
Pattern descriptionPattern = Pattern.compile("<meta [^>]*property=[\"']og:description[\"'] [^>]*content=[\"']([^'^\"]+?)[\"'][^>]*>");
|
if (matchStart < matchEnd && url[0].length() >= matchEnd)
|
||||||
Pattern imagePattern = Pattern.compile("<meta [^>]*property=[\"']og:image[\"'] [^>]*content=[\"']([^'^\"]+?)[\"'][^>]*>");
|
potentialUrl = url[0].substring(matchStart, matchEnd);
|
||||||
|
}
|
||||||
|
// If we actually have a URL then make use of it.
|
||||||
|
if (potentialUrl != null && potentialUrl.length() > 0) {
|
||||||
|
Pattern titlePattern = Pattern.compile("<meta [^>]*property=[\"']og:title[\"'] [^>]*content=[\"']([^'^\"]+?)[\"'][^>]*>");
|
||||||
|
Pattern descriptionPattern = Pattern.compile("<meta [^>]*property=[\"']og:description[\"'] [^>]*content=[\"']([^'^\"]+?)[\"'][^>]*>");
|
||||||
|
Pattern imagePattern = Pattern.compile("<meta [^>]*property=[\"']og:image[\"'] [^>]*content=[\"']([^'^\"]+?)[\"'][^>]*>");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.connectTimeout(10, TimeUnit.SECONDS)
|
.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
.writeTimeout(10, TimeUnit.SECONDS)
|
.writeTimeout(10, TimeUnit.SECONDS)
|
||||||
.proxy(Helper.getProxy(getApplication().getApplicationContext()))
|
.proxy(Helper.getProxy(getApplication().getApplicationContext()))
|
||||||
.readTimeout(10, TimeUnit.SECONDS).build();
|
.readTimeout(10, TimeUnit.SECONDS).build();
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(potentialUrl)
|
.url(potentialUrl)
|
||||||
.build();
|
.build();
|
||||||
client.newCall(request).enqueue(new Callback() {
|
client.newCall(request).enqueue(new Callback() {
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
runOnUiThread(() -> Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResponse(@NonNull Call call, @NonNull final Response response) {
|
|
||||||
if (response.isSuccessful()) {
|
|
||||||
try {
|
|
||||||
String data = response.body().string();
|
|
||||||
Matcher matcherTitle;
|
|
||||||
matcherTitle = titlePattern.matcher(data);
|
|
||||||
Matcher matcherDescription = descriptionPattern.matcher(data);
|
|
||||||
Matcher matcherImage = imagePattern.matcher(data);
|
|
||||||
String titleEncoded = null;
|
|
||||||
String descriptionEncoded = null;
|
|
||||||
if (fetchShareContent) {
|
|
||||||
while (matcherTitle.find())
|
|
||||||
titleEncoded = matcherTitle.group(1);
|
|
||||||
while (matcherDescription.find())
|
|
||||||
descriptionEncoded = matcherDescription.group(1);
|
|
||||||
}
|
|
||||||
String image = null;
|
|
||||||
if (fetchSharedMedia) {
|
|
||||||
while (matcherImage.find())
|
|
||||||
image = matcherImage.group(1);
|
|
||||||
}
|
|
||||||
String title = null;
|
|
||||||
String description = null;
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
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();
|
|
||||||
} else {
|
|
||||||
if (titleEncoded != null)
|
|
||||||
title = Html.fromHtml(titleEncoded).toString();
|
|
||||||
if (descriptionEncoded != null)
|
|
||||||
description = Html.fromHtml(descriptionEncoded).toString();
|
|
||||||
}
|
|
||||||
String finalImage = image;
|
|
||||||
String finalTitle = title;
|
|
||||||
String finalDescription = description;
|
|
||||||
|
|
||||||
|
|
||||||
runOnUiThread(() -> {
|
|
||||||
Bundle b = new Bundle();
|
|
||||||
b.putString(Helper.ARG_SHARE_URL, url[0]);
|
|
||||||
b.putString(Helper.ARG_SHARE_URL_MEDIA, finalImage);
|
|
||||||
b.putString(Helper.ARG_SHARE_TITLE, finalTitle);
|
|
||||||
b.putString(Helper.ARG_SHARE_DESCRIPTION, finalDescription);
|
|
||||||
b.putString(Helper.ARG_SHARE_SUBJECT, sharedSubject);
|
|
||||||
b.putString(Helper.ARG_SHARE_CONTENT, sharedText);
|
|
||||||
CrossActionHelper.doCrossShare(BaseMainActivity.this, b);
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
runOnUiThread(() -> Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show());
|
runOnUiThread(() -> Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
@Override
|
||||||
}).start();
|
public void onResponse(@NonNull Call call, @NonNull final Response response) {
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
try {
|
||||||
|
String data = response.body().string();
|
||||||
|
Matcher matcherTitle;
|
||||||
|
matcherTitle = titlePattern.matcher(data);
|
||||||
|
Matcher matcherDescription = descriptionPattern.matcher(data);
|
||||||
|
Matcher matcherImage = imagePattern.matcher(data);
|
||||||
|
String titleEncoded = null;
|
||||||
|
String descriptionEncoded = null;
|
||||||
|
if (fetchShareContent) {
|
||||||
|
while (matcherTitle.find())
|
||||||
|
titleEncoded = matcherTitle.group(1);
|
||||||
|
while (matcherDescription.find())
|
||||||
|
descriptionEncoded = matcherDescription.group(1);
|
||||||
|
}
|
||||||
|
String image = null;
|
||||||
|
if (fetchSharedMedia) {
|
||||||
|
while (matcherImage.find())
|
||||||
|
image = matcherImage.group(1);
|
||||||
|
}
|
||||||
|
String title = null;
|
||||||
|
String description = null;
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
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();
|
||||||
|
} else {
|
||||||
|
if (titleEncoded != null)
|
||||||
|
title = Html.fromHtml(titleEncoded).toString();
|
||||||
|
if (descriptionEncoded != null)
|
||||||
|
description = Html.fromHtml(descriptionEncoded).toString();
|
||||||
|
}
|
||||||
|
String finalImage = image;
|
||||||
|
String finalTitle = title;
|
||||||
|
String finalDescription = description;
|
||||||
|
|
||||||
|
|
||||||
|
runOnUiThread(() -> {
|
||||||
|
Bundle b = new Bundle();
|
||||||
|
b.putString(Helper.ARG_SHARE_URL, url[0]);
|
||||||
|
b.putString(Helper.ARG_SHARE_URL_MEDIA, finalImage);
|
||||||
|
b.putString(Helper.ARG_SHARE_TITLE, finalTitle);
|
||||||
|
b.putString(Helper.ARG_SHARE_DESCRIPTION, finalDescription);
|
||||||
|
b.putString(Helper.ARG_SHARE_SUBJECT, sharedSubject);
|
||||||
|
b.putString(Helper.ARG_SHARE_CONTENT, sharedText);
|
||||||
|
CrossActionHelper.doCrossShare(BaseMainActivity.this, b);
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
runOnUiThread(() -> Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Bundle b = new Bundle();
|
Bundle b = new Bundle();
|
||||||
b.putString(Helper.ARG_SHARE_TITLE, sharedSubject);
|
b.putString(Helper.ARG_SHARE_TITLE, sharedSubject);
|
||||||
|
|
|
@ -434,7 +434,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
||||||
}
|
}
|
||||||
if (description != null && description.trim().length() > 0) {
|
if (description != null && description.trim().length() > 0) {
|
||||||
statusList.get(position).text += description + "\n\n";
|
statusList.get(position).text += description + "\n\n";
|
||||||
if (!description.contains(url)) {
|
if (url != null && !description.contains(url)) {
|
||||||
statusList.get(position).text += url;
|
statusList.get(position).text += url;
|
||||||
}
|
}
|
||||||
} else if (content != null && content.trim().length() > 0) {
|
} else if (content != null && content.trim().length() > 0) {
|
||||||
|
|
Loading…
Reference in a new issue