From acc8d47c6875893ef9e988440c55f8239fda47d1 Mon Sep 17 00:00:00 2001 From: Al Lansley Date: Mon, 19 Aug 2024 16:22:08 +1000 Subject: [PATCH] SES-1571 Large messages show warning toast --- .../conversation/v2/ConversationActivityV2.kt | 16 ++++++++++++++++ .../v2/utilities/AttachmentManager.java | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index a9e5883e7b..f64738b47a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -1840,6 +1840,22 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe val mediaPreppedListener = object : ListenableFuture.Listener { override fun onSuccess(result: Boolean?) { + if (result == null) { + Log.w(TAG, "Media prepper returned a null result - bailing.") + return + } + + // If the attachment was too large or MediaConstraints.isSatisfied failed for some + // other reason then we reset the attachment manager & shown buttons then bail.. + if (!result) { + attachmentManager.clear() + if (isShowingAttachmentOptions) { toggleAttachmentOptions() } + return + } + + // ..otherwise we can attempt to send the attachment(s). + // Note: The only multi-attachment message type is when sending images - all others + // attempt send on initial attachment selection. sendAttachments(attachmentManager.buildSlideDeck().asAttachments(), null) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/AttachmentManager.java b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/AttachmentManager.java index 8225c9c6b4..e9a738637d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/AttachmentManager.java +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/AttachmentManager.java @@ -65,6 +65,9 @@ public class AttachmentManager { private final static String TAG = AttachmentManager.class.getSimpleName(); + // Max attachment size is 10MB, above which we display a warning toast rather than sending the msg + private final long MAX_ATTACHMENTS_FILE_SIZE_BYTES = 10 * 1024 * 1024; + private final @NonNull Context context; private final @NonNull AttachmentListener attachmentListener; @@ -360,9 +363,18 @@ public class AttachmentManager { final @Nullable Slide slide, final @NonNull MediaConstraints constraints) { - return slide == null || - constraints.isSatisfied(context, slide.asAttachment()) || - constraints.canResize(slide.asAttachment()); + // Null attachment? Not satisfied. + if (slide == null) return false; + + // Attachments are excessively large? Not satisfied. + if (slide.asAttachment().getSize() > MAX_ATTACHMENTS_FILE_SIZE_BYTES) { + Toast.makeText(context, R.string.attachmentsErrorSize, Toast.LENGTH_SHORT).show(); + return false; + } + + // Otherwise our constraints-satisfied condition becomes whether we can resize it (obviously + // this will only work on images). + return constraints.canResize(slide.asAttachment()); } public interface AttachmentListener {