From 3eea84269d2ab7dc921236b24f334b1279e1f50f Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 4 Feb 2019 19:31:28 +0100 Subject: make sure that http upload/download is not startetd multiple times --- .../messenger/http/HttpConnectionManager.java | 69 ++++++++++++++-------- .../messenger/http/HttpDownloadConnection.java | 19 +++--- .../messenger/http/HttpUploadConnection.java | 23 +++++--- 3 files changed, 71 insertions(+), 40 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/pixart/messenger/http/HttpConnectionManager.java b/src/main/java/de/pixart/messenger/http/HttpConnectionManager.java index fbb93d916..0bf0e274e 100644 --- a/src/main/java/de/pixart/messenger/http/HttpConnectionManager.java +++ b/src/main/java/de/pixart/messenger/http/HttpConnectionManager.java @@ -1,5 +1,7 @@ package de.pixart.messenger.http; +import android.util.Log; + import org.apache.http.conn.ssl.StrictHostnameVerifier; import java.io.IOException; @@ -9,6 +11,7 @@ import java.net.Proxy; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -17,6 +20,7 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.X509TrustManager; +import de.pixart.messenger.Config; import de.pixart.messenger.entities.Account; import de.pixart.messenger.entities.Message; import de.pixart.messenger.services.AbstractConnectionManager; @@ -25,28 +29,47 @@ import de.pixart.messenger.utils.TLSSocketFactory; public class HttpConnectionManager extends AbstractConnectionManager { + private final List downloadConnections = new ArrayList<>(); + private final List uploadConnections = new ArrayList<>(); + public HttpConnectionManager(XmppConnectionService service) { super(service); } - private List downloadConnections = new CopyOnWriteArrayList<>(); - private List uploadConnections = new CopyOnWriteArrayList<>(); + public static Proxy getProxy() throws IOException { + return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 8118)); + } - public HttpDownloadConnection createNewDownloadConnection(Message message) { - return this.createNewDownloadConnection(message, false); + public void createNewDownloadConnection(Message message) { + this.createNewDownloadConnection(message, false); } - public HttpDownloadConnection createNewDownloadConnection(Message message, boolean interactive) { - HttpDownloadConnection connection = new HttpDownloadConnection(this); - connection.init(message, interactive); - this.downloadConnections.add(connection); - return connection; + public void createNewDownloadConnection(final Message message, boolean interactive) { + synchronized (this.downloadConnections) { + for (HttpDownloadConnection connection : this.downloadConnections) { + if (connection.getMessage() == message) { + Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": download already in progress"); + return; + } + } + final HttpDownloadConnection connection = new HttpDownloadConnection(message, this); + connection.init(interactive); + this.downloadConnections.add(connection); + } } - public void createNewUploadConnection(Message message, boolean delay) { - HttpUploadConnection connection = new HttpUploadConnection(Method.determine(message.getConversation().getAccount()), this); - connection.init(message, delay); - this.uploadConnections.add(connection); + public void createNewUploadConnection(final Message message, boolean delay) { + synchronized (this.uploadConnections) { + for (HttpUploadConnection connection : this.uploadConnections) { + if (connection.getMessage() == message) { + Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": upload already in progress"); + return; + } + } + HttpUploadConnection connection = new HttpUploadConnection(message, Method.determine(message.getConversation().getAccount()), this); + connection.init(delay); + this.uploadConnections.add(connection); + } } public boolean checkConnection(Message message) { @@ -58,15 +81,19 @@ public class HttpConnectionManager extends AbstractConnectionManager { return mXmppConnectionService.hasInternetConnection(); } - public void finishConnection(HttpDownloadConnection connection) { - this.downloadConnections.remove(connection); + void finishConnection(HttpDownloadConnection connection) { + synchronized (this.downloadConnections) { + this.downloadConnections.remove(connection); + } } - public void finishUploadConnection(HttpUploadConnection httpUploadConnection) { - this.uploadConnections.remove(httpUploadConnection); + void finishUploadConnection(HttpUploadConnection httpUploadConnection) { + synchronized (this.uploadConnections) { + this.uploadConnections.remove(httpUploadConnection); + } } - public void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) { + void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) { final X509TrustManager trustManager; final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive); if (interactive) { @@ -81,8 +108,4 @@ public class HttpConnectionManager extends AbstractConnectionManager { } catch (final KeyManagementException | NoSuchAlgorithmException ignored) { } } - - public static Proxy getProxy() throws IOException { - return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 8118)); - } -} +} \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java index 102a11626..1074c1982 100644 --- a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java +++ b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java @@ -41,7 +41,7 @@ public class HttpDownloadConnection implements Transferable { private XmppConnectionService mXmppConnectionService; private URL mUrl; - private Message message; + private final Message message; private DownloadableFile file; private int mStatus = Transferable.STATUS_UNKNOWN; private boolean acceptedAutomatically = false; @@ -51,8 +51,8 @@ public class HttpDownloadConnection implements Transferable { private Method method = Method.HTTP_UPLOAD; private final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US); - - HttpDownloadConnection(HttpConnectionManager manager) { + HttpDownloadConnection(Message message, HttpConnectionManager manager) { + this.message = message; this.mHttpConnectionManager = manager; this.mXmppConnectionService = manager.getXmppConnectionService(); this.mUseTor = mXmppConnectionService.useTorToConnect(); @@ -72,12 +72,7 @@ public class HttpDownloadConnection implements Transferable { } } - public void init(Message message) { - init(message, false); - } - - public void init(Message message, boolean interactive) { - this.message = message; + public void init(boolean interactive) { this.message.setTransferable(this); try { if (message.hasFileOnRemoteHost()) { @@ -201,6 +196,10 @@ public class HttpDownloadConnection implements Transferable { return this.mProgress; } + public Message getMessage() { + return message; + } + private class FileSizeChecker implements Runnable { private final boolean interactive; @@ -467,4 +466,4 @@ public class HttpDownloadConnection implements Transferable { } } -} +} \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/http/HttpUploadConnection.java b/src/main/java/de/pixart/messenger/http/HttpUploadConnection.java index 49debcb78..92ccf86e1 100644 --- a/src/main/java/de/pixart/messenger/http/HttpUploadConnection.java +++ b/src/main/java/de/pixart/messenger/http/HttpUploadConnection.java @@ -43,14 +43,15 @@ public class HttpUploadConnection implements Transferable { private boolean cancelled = false; private boolean delayed = false; private DownloadableFile file; - private Message message; + private final Message message; private String mime; private SlotRequester.Slot slot; private byte[] key = null; private long transmitted = 0; - public HttpUploadConnection(Method method, HttpConnectionManager httpConnectionManager) { + public HttpUploadConnection(Message message, Method method, HttpConnectionManager httpConnectionManager) { + this.message = message; this.method = method; this.mHttpConnectionManager = httpConnectionManager; this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService(); @@ -87,13 +88,16 @@ public class HttpUploadConnection implements Transferable { } private void fail(String errorMessage) { + finish(); + mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage); + } + + private void finish() { mHttpConnectionManager.finishUploadConnection(this); message.setTransferable(null); - mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage); } - public void init(Message message, boolean delay) { - this.message = message; + public void init(boolean delay) { final Account account = message.getConversation().getAccount(); this.file = mXmppConnectionService.getFileBackend().getFile(message, false); if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { @@ -123,6 +127,7 @@ public class HttpUploadConnection implements Transferable { } else { md5 = null; } + this.file.setExpectedSize(file.getSize() + (file.getKey() != null ? 16 : 0)); message.resetFileParams(); this.mSlotRequester.request(method, account, file, mime, md5, new SlotRequester.OnSlotRequested() { @@ -210,7 +215,7 @@ public class HttpUploadConnection implements Transferable { } mXmppConnectionService.getFileBackend().updateFileParams(message, get); mXmppConnectionService.getFileBackend().updateMediaScanner(file); - message.setTransferable(null); + finish(); message.setCounterpart(message.getConversation().getJid().asBareJid()); mXmppConnectionService.resendMessage(message, delayed); } else { @@ -230,4 +235,8 @@ public class HttpUploadConnection implements Transferable { WakeLockHelper.release(wakeLock); } } -} + + public Message getMessage() { + return message; + } +} \ No newline at end of file -- cgit v1.2.3