diff options
author | iNPUTmice <daniel@gultsch.de> | 2014-10-15 19:32:12 +0200 |
---|---|---|
committer | iNPUTmice <daniel@gultsch.de> | 2014-10-15 19:32:12 +0200 |
commit | cb4069f0f213ed1a38ce074d981b0c367dc2cdfd (patch) | |
tree | 9004af99e6da679e640fe97bdcdbf469832b8ab5 /src/eu/siacs/conversations/http | |
parent | 1927a3d99c3a8e23535680ceb29e7dbb78bda185 (diff) |
refactored file download status. make image http download available for carbon copied (sent) messages as well
Diffstat (limited to 'src/eu/siacs/conversations/http')
-rw-r--r-- | src/eu/siacs/conversations/http/HttpConnection.java | 84 | ||||
-rw-r--r-- | src/eu/siacs/conversations/http/HttpConnectionManager.java | 11 |
2 files changed, 57 insertions, 38 deletions
diff --git a/src/eu/siacs/conversations/http/HttpConnection.java b/src/eu/siacs/conversations/http/HttpConnection.java index ff8037fb..c53b9fd5 100644 --- a/src/eu/siacs/conversations/http/HttpConnection.java +++ b/src/eu/siacs/conversations/http/HttpConnection.java @@ -24,7 +24,8 @@ public class HttpConnection implements Downloadable { private URL mUrl; private Message message; private DownloadableFile file; - private long mPreviousFileSize = Long.MIN_VALUE; + private long mPreviousFileSize = 0; + private int mStatus = Downloadable.STATUS_UNKNOWN; public HttpConnection(HttpConnectionManager manager) { this.mHttpConnectionManager = manager; @@ -33,6 +34,7 @@ public class HttpConnection implements Downloadable { @Override public void start() { + changeStatus(STATUS_DOWNLOADING); new Thread(new FileDownloader()).start(); } @@ -41,43 +43,46 @@ public class HttpConnection implements Downloadable { this.message.setDownloadable(this); try { mUrl = new URL(message.getBody()); - this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false); - message.setType(Message.TYPE_IMAGE); - message.setStatus(Message.STATUS_RECEIVED_CHECKING); - mXmppConnectionService.updateConversationUi(); + this.file = mXmppConnectionService.getFileBackend() + .getConversationsFile(message, false); 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.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() { + changeStatus(STATUS_CHECKING); new Thread(new FileSizeChecker()).start(); } public void cancel() { - mXmppConnectionService.markMessage(message, Message.STATUS_RECEPTION_FAILED); mHttpConnectionManager.finishConnection(this); + message.setDownloadable(null); + message.setBody(mUrl.toString()); + mXmppConnectionService.updateMessage(message); } - + private void finish() { - message.setStatus(Message.STATUS_RECEIVED); - mXmppConnectionService.updateMessage(message); + message.setDownloadable(null); mHttpConnectionManager.finishConnection(this); } + private void changeStatus(int status) { + this.mStatus = status; + mXmppConnectionService.updateConversationUi(); + } + private class FileSizeChecker implements Runnable { @Override @@ -90,21 +95,21 @@ public class HttpConnection implements Downloadable { return; } file.setExpectedSize(size); - message.setBody(mUrl.toString()+","+String.valueOf(size)); - if (size <= mHttpConnectionManager.getAutoAcceptFileSize() || size == mPreviousFileSize) { - mXmppConnectionService.updateMessage(message); + message.setType(Message.TYPE_IMAGE); + if (size <= mHttpConnectionManager.getAutoAcceptFileSize() + || size == mPreviousFileSize) { start(); } else { - message.setStatus(Message.STATUS_RECEIVED_OFFER); - mXmppConnectionService.updateMessage(message); + changeStatus(STATUS_OFFER); } } private long retrieveFileSize() throws IOException { - HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); + HttpURLConnection connection = (HttpURLConnection) mUrl + .openConnection(); connection.setRequestMethod("HEAD"); if (connection instanceof HttpsURLConnection) { - + } String contentLength = connection.getHeaderField("Content-Length"); if (contentLength == null) { @@ -118,13 +123,12 @@ public class HttpConnection implements Downloadable { } } - + private class FileDownloader implements Runnable { @Override public void run() { try { - mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING); download(); updateImageBounds(); finish(); @@ -132,13 +136,15 @@ public class HttpConnection implements Downloadable { cancel(); } } - + private void download() throws IOException { - HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); + HttpURLConnection connection = (HttpURLConnection) mUrl + .openConnection(); if (connection instanceof HttpsURLConnection) { - + } - BufferedInputStream is = new BufferedInputStream(connection.getInputStream()); + BufferedInputStream is = new BufferedInputStream( + connection.getInputStream()); OutputStream os = file.createOutputStream(); int count = -1; byte[] buffer = new byte[1024]; @@ -149,17 +155,31 @@ public class HttpConnection implements Downloadable { os.close(); is.close(); } - + private void updateImageBounds() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; - message.setBody(mUrl.toString()+","+file.getSize() + ',' + message.setBody(mUrl.toString() + "," + file.getSize() + ',' + imageWidth + ',' + imageHeight); - + mXmppConnectionService.updateMessage(message); + } + + } + + @Override + public int getStatus() { + return this.mStatus; + } + + @Override + public long getFileSize() { + if (this.file != null) { + return this.file.getExpectedSize(); + } else { + return 0; } - } }
\ No newline at end of file diff --git a/src/eu/siacs/conversations/http/HttpConnectionManager.java b/src/eu/siacs/conversations/http/HttpConnectionManager.java index 7393cf36..011ecc3f 100644 --- a/src/eu/siacs/conversations/http/HttpConnectionManager.java +++ b/src/eu/siacs/conversations/http/HttpConnectionManager.java @@ -13,24 +13,23 @@ public class HttpConnectionManager extends AbstractConnectionManager { public HttpConnectionManager(XmppConnectionService service) { super(service); } - + private List<HttpConnection> connections = new CopyOnWriteArrayList<HttpConnection>(); - - + public HttpConnection createNewConnection(Message message) { HttpConnection connection = new HttpConnection(this); connection.init(message); this.connections.add(connection); return connection; } - + public HttpConnection createNewConnection(Message message, URL url) { HttpConnection connection = new HttpConnection(this); - connection.init(message,url); + connection.init(message, url); this.connections.add(connection); return connection; } - + public void finishConnection(HttpConnection connection) { this.connections.remove(connection); } |