From cb4069f0f213ed1a38ce074d981b0c367dc2cdfd Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Wed, 15 Oct 2014 19:32:12 +0200 Subject: refactored file download status. make image http download available for carbon copied (sent) messages as well --- .../siacs/conversations/http/HttpConnection.java | 84 +++++++++++++--------- .../conversations/http/HttpConnectionManager.java | 11 ++- 2 files changed, 57 insertions(+), 38 deletions(-) (limited to 'src/eu/siacs/conversations/http') diff --git a/src/eu/siacs/conversations/http/HttpConnection.java b/src/eu/siacs/conversations/http/HttpConnection.java index ff8037fb3..c53b9fd54 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 7393cf36e..011ecc3f5 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 connections = new CopyOnWriteArrayList(); - - + 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); } -- cgit v1.2.3