From d95b904b5b5fd68765e81a10c70edd34049d94c6 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 20 Feb 2019 21:44:06 +0100 Subject: [PATCH] show file too large instead of generic delivery failed --- .../conversations/parser/AbstractParser.java | 35 ++++++++++++++++--- .../ui/ConversationFragment.java | 12 +++++-- .../ui/adapter/MessageAdapter.java | 17 +++++++-- src/main/res/values/strings.xml | 1 + 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/AbstractParser.java b/src/main/java/eu/siacs/conversations/parser/AbstractParser.java index 05297d74d..47a1e18a1 100644 --- a/src/main/java/eu/siacs/conversations/parser/AbstractParser.java +++ b/src/main/java/eu/siacs/conversations/parser/AbstractParser.java @@ -1,8 +1,11 @@ package eu.siacs.conversations.parser; + import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import eu.siacs.conversations.entities.Account; @@ -127,14 +130,36 @@ public abstract class AbstractParser { public static String extractErrorMessage(Element packet) { final Element error = packet.findChild("error"); if (error != null && error.getChildren().size() > 0) { + final List errorNames = orderedElementNames(error.getChildren()); final String text = error.findChildContent("text"); if (text != null && !text.trim().isEmpty()) { - return text; - } else { - return error.getChildren().get(0).getName().replace("-"," "); + return prefixError(errorNames)+text; + } else if (errorNames.size() > 0){ + return prefixError(errorNames)+errorNames.get(0).replace("-"," "); } - } else { - return null; } + return null; + } + + private static String prefixError(List errorNames) { + if (errorNames.size() > 0) { + return errorNames.get(0)+'\u001f'; + } + return ""; + } + + private static List orderedElementNames(List children) { + List names = new ArrayList<>(); + for(Element child : children) { + final String name = child.getName(); + if (name != null && !name.equals("text")) { + if ("urn:ietf:params:xml:ns:xmpp-stanzas".equals(child.getNamespace())) { + names.add(name); + } else { + names.add(0, name); + } + } + } + return names; } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 9aa2390c2..314b8fe73 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -1624,9 +1624,17 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private void showErrorMessage(final Message message) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.error_message); - builder.setMessage(message.getErrorMessage()); + final String errorMessage = message.getErrorMessage(); + final String[] errorMessageParts = errorMessage == null ? new String[0] : errorMessage.split("\\u001f"); + final String displayError; + if (errorMessageParts.length == 2) { + displayError = errorMessageParts[1]; + } else { + displayError = errorMessage; + } + builder.setMessage(displayError); builder.setNegativeButton(R.string.copy_to_clipboard, (dialog, which) -> { - activity.copyTextToClipboard(message.getErrorMessage(),R.string.error_message); + activity.copyTextToClipboard(displayError,R.string.error_message); Toast.makeText(activity,R.string.error_message_copied_to_clipboard, Toast.LENGTH_SHORT).show(); }); builder.setPositiveButton(R.string.confirm, null); diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index a3a80b257..5b9e8c04a 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -219,10 +219,23 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie } break; case Message.STATUS_SEND_FAILED: - if (Message.ERROR_MESSAGE_CANCELLED.equals(message.getErrorMessage())) { + final String errorMessage = message.getErrorMessage(); + if (Message.ERROR_MESSAGE_CANCELLED.equals(errorMessage)) { info = getContext().getString(R.string.cancelled); } else { - info = getContext().getString(R.string.send_failed); + final String[] errorParts = errorMessage.split("\\u001f", 2); + if (errorParts.length == 2) { + switch (errorParts[0]) { + case "file-too-large": + info = getContext().getString(R.string.file_too_large); + break; + default: + info = getContext().getString(R.string.send_failed); + break; + } + } else { + info = getContext().getString(R.string.send_failed); + } } error = true; break; diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 3564369b8..06ae2f786 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -855,4 +855,5 @@ This private group chat has no participants. Manage privileges Search participants + File too large