aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/http/HttpConnection.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/siacs/conversations/http/HttpConnection.java')
-rw-r--r--src/eu/siacs/conversations/http/HttpConnection.java84
1 files changed, 52 insertions, 32 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