mirror of
https://codeberg.org/tom79/Fedilab.git
synced 2025-01-07 00:20:08 +02:00
Comment issue #276 - Fix crashes when sharing only text
This commit is contained in:
parent
d7bd889d35
commit
7f48b03781
3 changed files with 120 additions and 102 deletions
|
@ -308,98 +308,106 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
|
||||||
if (matchStart < matchEnd && sharedText.length() >= matchEnd)
|
if (matchStart < matchEnd && sharedText.length() >= matchEnd)
|
||||||
url[0] = sharedText.substring(matchStart, matchEnd);
|
url[0] = sharedText.substring(matchStart, matchEnd);
|
||||||
}
|
}
|
||||||
new Thread(() -> {
|
if (url[0] != null) {
|
||||||
if (url[0].startsWith("www."))
|
new Thread(() -> {
|
||||||
url[0] = "http://" + url[0];
|
if (url[0].startsWith("www."))
|
||||||
Matcher matcherPattern = Patterns.WEB_URL.matcher(url[0]);
|
url[0] = "http://" + url[0];
|
||||||
String potentialUrl = null;
|
Matcher matcherPattern = Patterns.WEB_URL.matcher(url[0]);
|
||||||
while (matcherPattern.find()) {
|
String potentialUrl = null;
|
||||||
int matchStart = matcherPattern.start(1);
|
while (matcherPattern.find()) {
|
||||||
int matchEnd = matcherPattern.end();
|
int matchStart = matcherPattern.start(1);
|
||||||
if (matchStart < matchEnd && url[0].length() >= matchEnd)
|
int matchEnd = matcherPattern.end();
|
||||||
potentialUrl = url[0].substring(matchStart, matchEnd);
|
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) {
|
// If we actually have a URL then make use of it.
|
||||||
Pattern titlePattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:title[\"']\\s+content=[\"']([^>]*)[\"']");
|
if (potentialUrl != null && potentialUrl.length() > 0) {
|
||||||
Pattern descriptionPattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:description[\"']\\s+content=[\"']([^>]*)[\"']");
|
Pattern titlePattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:title[\"']\\s+content=[\"']([^>]*)[\"']");
|
||||||
Pattern imagePattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:image[\"']\\s+content=[\"']([^>]*)[\"']");
|
Pattern descriptionPattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:description[\"']\\s+content=[\"']([^>]*)[\"']");
|
||||||
try {
|
Pattern imagePattern = Pattern.compile("meta[ a-zA-Z=\"'-]+property=[\"']og:image[\"']\\s+content=[\"']([^>]*)[\"']");
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
try {
|
||||||
.connectTimeout(10, TimeUnit.SECONDS)
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.writeTimeout(10, TimeUnit.SECONDS)
|
.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
.proxy(Helper.getProxy(getApplication().getApplicationContext()))
|
.writeTimeout(10, TimeUnit.SECONDS)
|
||||||
.readTimeout(10, TimeUnit.SECONDS).build();
|
.proxy(Helper.getProxy(getApplication().getApplicationContext()))
|
||||||
Request request = new Request.Builder()
|
.readTimeout(10, TimeUnit.SECONDS).build();
|
||||||
.url(potentialUrl)
|
Request request = new Request.Builder()
|
||||||
.build();
|
.url(potentialUrl)
|
||||||
client.newCall(request).enqueue(new Callback() {
|
.build();
|
||||||
@Override
|
client.newCall(request).enqueue(new Callback() {
|
||||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
@Override
|
||||||
e.printStackTrace();
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||||
runOnUiThread(() -> Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show());
|
e.printStackTrace();
|
||||||
}
|
|
||||||
|
|
||||||
@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());
|
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;
|
||||||
|
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/")) {
|
} else if (type.startsWith("image/") || type.startsWith("video/")) {
|
||||||
|
|
|
@ -385,6 +385,8 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
composeAdapter.addSharing(null, null, sharedDescription, null, sharedContent, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
public void addSharing(String url, String title, String description, String subject, String content, String saveFilePath) {
|
||||||
int position = statusList.size() - 1;
|
int position = statusList.size() - 1;
|
||||||
statusList.get(position).text = title != null ? title : subject;
|
if (title != null || subject != null) {
|
||||||
statusList.get(position).text += "\n\n";
|
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 += description != null ? description : content;
|
||||||
statusList.get(position).text += "\n\n";
|
statusList.get(position).text += "\n\n";
|
||||||
statusList.get(position).text += url;
|
if (url != null) {
|
||||||
Attachment attachment = new Attachment();
|
statusList.get(position).text += url;
|
||||||
attachment.mimeType = "image/*";
|
}
|
||||||
String extension = "jpg";
|
if (saveFilePath != null) {
|
||||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_", Locale.getDefault());
|
Attachment attachment = new Attachment();
|
||||||
attachment.local_path = saveFilePath;
|
attachment.mimeType = "image/*";
|
||||||
Date now = new Date();
|
String extension = "jpg";
|
||||||
attachment.filename = formatter.format(now) + "." + extension;
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_", Locale.getDefault());
|
||||||
if (statusList.get(position).media_attachments == null) {
|
attachment.local_path = saveFilePath;
|
||||||
statusList.get(position).media_attachments = new ArrayList<>();
|
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);
|
notifyItemChanged(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue