From 61f58b3dbde32eedd08f893977311405d5f465c6 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 20 Sep 2016 20:02:25 +0200 Subject: [PATCH] add timeouts to HTTPUrlConnections and allow cancelation of all sending files --- .../siacs/conversations/http/HttpDownloadConnection.java | 4 ++++ .../eu/siacs/conversations/http/HttpUploadConnection.java | 4 +++- .../eu/siacs/conversations/ui/ConversationFragment.java | 7 ++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java index 741ea98df..a9bffe3ee 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java @@ -212,6 +212,8 @@ public class HttpDownloadConnection implements Transferable { if (connection instanceof HttpsURLConnection) { mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); } + connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000); + connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000); connection.connect(); String contentLength = connection.getHeaderField("Content-Length"); connection.disconnect(); @@ -279,6 +281,8 @@ public class HttpDownloadConnection implements Transferable { long size = file.getSize(); connection.setRequestProperty("Range", "bytes="+size+"-"); } + connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000); + connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000); connection.connect(); is = new BufferedInputStream(connection.getInputStream()); boolean serverResumed = "bytes".equals(connection.getHeaderField("Accept-Ranges")); diff --git a/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java index 1bb70690e..1bd6a8e4f 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java @@ -171,10 +171,12 @@ public class HttpUploadConnection implements Transferable { connection.setRequestProperty("Content-Type", mime == null ? "application/octet-stream" : mime); connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getIdentityName()); connection.setDoOutput(true); + connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000); + connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000); connection.connect(); os = connection.getOutputStream(); transmitted = 0; - int count = -1; + int count; byte[] buffer = new byte[4096]; while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) { transmitted += count; diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index c6f6b8cd3..3490a712f 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -573,9 +573,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa downloadFile.setVisible(true); downloadFile.setTitle(activity.getString(R.string.download_x_file,UIHelper.getFileDescriptionString(activity, m))); } - if ((t != null && !(t instanceof TransferablePlaceholder)) - || (m.isFileOrImage() && (m.getStatus() == Message.STATUS_WAITING - || m.getStatus() == Message.STATUS_OFFERED))) { + boolean waitingOfferedSending = m.getStatus() == Message.STATUS_WAITING + || m.getStatus() == Message.STATUS_UNSEND + || m.getStatus() == Message.STATUS_OFFERED; + if ((t != null && !(t instanceof TransferablePlaceholder)) || waitingOfferedSending && m.needsUploading()) { cancelTransmission.setVisible(true); } if (treatAsFile) {