do not display toast for cancelled downloads

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java | 21 
1 file changed, 10 insertions(+), 11 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java 🔗

@@ -211,14 +211,19 @@ public class HttpDownloadConnection implements Transferable {
         mHttpConnectionManager.updateConversationUi(true);
     }
 
-    private void showToastForException(Exception e) {
+    private void showToastForException(final Exception e) {
+        final Call call = mostRecentCall;
+        final boolean cancelled = call != null && call.isCanceled();
+        if (e == null || cancelled) {
+            return;
+        }
         if (e instanceof java.net.UnknownHostException) {
             mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
         } else if (e instanceof java.net.ConnectException) {
             mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
         } else if (e instanceof FileWriterException) {
             mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
-        } else if (!(e instanceof CancellationException)) {
+        } else {
             mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
         }
     }
@@ -268,9 +273,7 @@ public class HttpDownloadConnection implements Transferable {
         private void retrieveFailed(@Nullable Exception e) {
             changeStatus(STATUS_OFFER_CHECK_FILESIZE);
             if (interactive) {
-                if (e != null) {
-                    showToastForException(e);
-                }
+                showToastForException(e);
             } else {
                 HttpDownloadConnection.this.acceptedAutomatically = false;
                 HttpDownloadConnection.this.mXmppConnectionService.getNotificationService().push(message);
@@ -362,8 +365,7 @@ public class HttpDownloadConnection implements Transferable {
             } catch (final SSLHandshakeException e) {
                 changeStatus(STATUS_OFFER);
             } catch (final Exception e) {
-                Log.d(Config.LOGTAG,"problem downloading",e);
-                //TODO do not show toast for cancelled stuff
+                Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": unable to download file", e);
                 if (interactive) {
                     showToastForException(e);
                 } else {
@@ -421,7 +423,7 @@ public class HttpDownloadConnection implements Transferable {
                     outputStream = AbstractConnectionManager.createOutputStream(file, false, false);
                 }
                 int count;
-                byte[] buffer = new byte[4096];
+                final byte[] buffer = new byte[4096];
                 while ((count = inputStream.read(buffer)) != -1) {
                     transmitted += count;
                     try {
@@ -430,9 +432,6 @@ public class HttpDownloadConnection implements Transferable {
                         throw new FileWriterException();
                     }
                     updateProgress(Math.round(((double) transmitted / expected) * 100));
-                    if (canceled) {
-                        throw new CancellationException();
-                    }
                 }
                 outputStream.flush();
             } else {