Comment issue #276 - Fix crashes when sharing only text

This commit is contained in:
Thomas 2022-07-25 15:50:17 +02:00
parent d7bd889d35
commit 7f48b03781
3 changed files with 120 additions and 102 deletions

View file

@ -308,98 +308,106 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
if (matchStart < matchEnd && sharedText.length() >= matchEnd)
url[0] = sharedText.substring(matchStart, matchEnd);
}
new Thread(() -> {
if (url[0].startsWith("www."))
url[0] = "http://" + url[0];
Matcher matcherPattern = Patterns.WEB_URL.matcher(url[0]);
String potentialUrl = null;
while (matcherPattern.find()) {
int matchStart = matcherPattern.start(1);
int matchEnd = matcherPattern.end();
if (matchStart < matchEnd && url[0].length() >= matchEnd)
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[ 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=[\"']([^>]*)[\"']");
try {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.proxy(Helper.getProxy(getApplication().getApplicationContext()))
.readTimeout(10, TimeUnit.SECONDS).build();
Request request = new Request.Builder()
.url(potentialUrl)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
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;
while (matcherTitle.find())
titleEncoded = matcherTitle.group(1);
while (matcherDescription.find())
descriptionEncoded = matcherDescription.group(1);
String image = null;
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 {
if (url[0] != null) {
new Thread(() -> {
if (url[0].startsWith("www."))
url[0] = "http://" + url[0];
Matcher matcherPattern = Patterns.WEB_URL.matcher(url[0]);
String potentialUrl = null;
while (matcherPattern.find()) {
int matchStart = matcherPattern.start(1);
int matchEnd = matcherPattern.end();
if (matchStart < matchEnd && url[0].length() >= matchEnd)
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[ 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=[\"']([^>]*)[\"']");
try {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.proxy(Helper.getProxy(getApplication().getApplicationContext()))
.readTimeout(10, TimeUnit.SECONDS).build();
Request request = new Request.Builder()
.url(potentialUrl)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
e.printStackTrace();
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();
@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;
while (matcherTitle.find())
titleEncoded = matcherTitle.group(1);
while (matcherDescription.find())
descriptionEncoded = matcherDescription.group(1);
String image = null;
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 {
Bundle b = new Bundle();
b.putString(Helper.ARG_SHARE_TITLE, sharedSubject);
b.putString(Helper.ARG_SHARE_DESCRIPTION, sharedText);
CrossActionHelper.doCrossShare(BaseMainActivity.this, b);
}
}
} else if (type.startsWith("image/") || type.startsWith("video/")) {

View file

@ -385,6 +385,8 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
}
});
} else {
composeAdapter.addSharing(null, null, sharedDescription, null, sharedContent, null);
}
}

View file

@ -411,22 +411,30 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
*/
public void addSharing(String url, String title, String description, String subject, String content, String saveFilePath) {
int position = statusList.size() - 1;
statusList.get(position).text = title != null ? title : subject;
statusList.get(position).text += "\n\n";
if (title != null || subject != null) {
statusList.get(position).text = title != null ? title : subject;
statusList.get(position).text += "\n\n";
} else {
statusList.get(position).text = "";
}
statusList.get(position).text += description != null ? description : content;
statusList.get(position).text += "\n\n";
statusList.get(position).text += url;
Attachment attachment = new Attachment();
attachment.mimeType = "image/*";
String extension = "jpg";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_", Locale.getDefault());
attachment.local_path = saveFilePath;
Date now = new Date();
attachment.filename = formatter.format(now) + "." + extension;
if (statusList.get(position).media_attachments == null) {
statusList.get(position).media_attachments = new ArrayList<>();
if (url != null) {
statusList.get(position).text += url;
}
if (saveFilePath != null) {
Attachment attachment = new Attachment();
attachment.mimeType = "image/*";
String extension = "jpg";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_", Locale.getDefault());
attachment.local_path = saveFilePath;
Date now = new Date();
attachment.filename = formatter.format(now) + "." + extension;
if (statusList.get(position).media_attachments == null) {
statusList.get(position).media_attachments = new ArrayList<>();
}
statusList.get(position).media_attachments.add(attachment);
}
statusList.get(position).media_attachments.add(attachment);
notifyItemChanged(position);
}