diff options
author | iNPUTmice <daniel@gultsch.de> | 2014-10-14 19:27:49 +0200 |
---|---|---|
committer | iNPUTmice <daniel@gultsch.de> | 2014-10-14 19:27:49 +0200 |
commit | 262128c8f9152330b61bc5b4358337ff2fd0fdcf (patch) | |
tree | 5cde45244e7a6ed933025abb608f7c8b9d395c6f /src/eu/siacs/conversations/http | |
parent | 031b739af8122e0c6a2926c3f9db78e207560f64 (diff) |
recreate http connection after restart. fixes stale download button. better failing
Diffstat (limited to 'src/eu/siacs/conversations/http')
-rw-r--r-- | src/eu/siacs/conversations/http/HttpConnection.java | 51 | ||||
-rw-r--r-- | src/eu/siacs/conversations/http/HttpConnectionManager.java | 14 |
2 files changed, 47 insertions, 18 deletions
diff --git a/src/eu/siacs/conversations/http/HttpConnection.java b/src/eu/siacs/conversations/http/HttpConnection.java index 7ed981363..ff8037fb3 100644 --- a/src/eu/siacs/conversations/http/HttpConnection.java +++ b/src/eu/siacs/conversations/http/HttpConnection.java @@ -10,9 +10,7 @@ import java.net.URL; import javax.net.ssl.HttpsURLConnection; import android.graphics.BitmapFactory; -import android.util.Log; -import eu.siacs.conversations.Config; import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.Message; @@ -26,6 +24,7 @@ public class HttpConnection implements Downloadable { private URL mUrl; private Message message; private DownloadableFile file; + private long mPreviousFileSize = Long.MIN_VALUE; public HttpConnection(HttpConnectionManager manager) { this.mHttpConnectionManager = manager; @@ -44,39 +43,60 @@ public class HttpConnection implements Downloadable { mUrl = new URL(message.getBody()); this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false); message.setType(Message.TYPE_IMAGE); - mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_CHECKING); + message.setStatus(Message.STATUS_RECEIVED_CHECKING); + mXmppConnectionService.updateConversationUi(); checkFileSize(); } catch (MalformedURLException e) { this.cancel(); } } + public void init(Message message, URL url) { + this.message = message; + this.message.setDownloadable(this); + this.mUrl = url; + this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false); + this.mPreviousFileSize = message.getImageParams().size; + message.setType(Message.TYPE_IMAGE); + message.setStatus(Message.STATUS_RECEIVED_CHECKING); + mXmppConnectionService.updateConversationUi(); + checkFileSize(); + } + private void checkFileSize() { new Thread(new FileSizeChecker()).start(); } public void cancel() { mXmppConnectionService.markMessage(message, Message.STATUS_RECEPTION_FAILED); - Log.d(Config.LOGTAG,"canceled download"); + mHttpConnectionManager.finishConnection(this); + } + + private void finish() { + message.setStatus(Message.STATUS_RECEIVED); + mXmppConnectionService.updateMessage(message); + mHttpConnectionManager.finishConnection(this); } private class FileSizeChecker implements Runnable { @Override public void run() { + long size; try { - long size = retrieveFileSize(); - file.setExpectedSize(size); - message.setBody(mUrl.toString()+","+String.valueOf(size)); - if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) { - mXmppConnectionService.updateMessage(message); - start(); - } else { - message.setStatus(Message.STATUS_RECEIVED_OFFER); - mXmppConnectionService.updateMessage(message); - } + size = retrieveFileSize(); } catch (IOException e) { cancel(); + return; + } + file.setExpectedSize(size); + message.setBody(mUrl.toString()+","+String.valueOf(size)); + if (size <= mHttpConnectionManager.getAutoAcceptFileSize() || size == mPreviousFileSize) { + mXmppConnectionService.updateMessage(message); + start(); + } else { + message.setStatus(Message.STATUS_RECEIVED_OFFER); + mXmppConnectionService.updateMessage(message); } } @@ -107,8 +127,7 @@ public class HttpConnection implements Downloadable { mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING); download(); updateImageBounds(); - message.setStatus(Message.STATUS_RECEIVED); - mXmppConnectionService.updateMessage(message); + finish(); } catch (IOException e) { cancel(); } diff --git a/src/eu/siacs/conversations/http/HttpConnectionManager.java b/src/eu/siacs/conversations/http/HttpConnectionManager.java index 4d6dce126..7393cf36e 100644 --- a/src/eu/siacs/conversations/http/HttpConnectionManager.java +++ b/src/eu/siacs/conversations/http/HttpConnectionManager.java @@ -1,5 +1,6 @@ package eu.siacs.conversations.http; +import java.net.URL; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -12,8 +13,6 @@ public class HttpConnectionManager extends AbstractConnectionManager { public HttpConnectionManager(XmppConnectionService service) { super(service); } - - private XmppConnectionService mXmppConnectionService; private List<HttpConnection> connections = new CopyOnWriteArrayList<HttpConnection>(); @@ -24,4 +23,15 @@ public class HttpConnectionManager extends AbstractConnectionManager { this.connections.add(connection); return connection; } + + public HttpConnection createNewConnection(Message message, URL url) { + HttpConnection connection = new HttpConnection(this); + connection.init(message,url); + this.connections.add(connection); + return connection; + } + + public void finishConnection(HttpConnection connection) { + this.connections.remove(connection); + } } |