aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
committerlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
commit3c400703e082a1b180b35d891b8fb3460c7d5b87 (patch)
tree28738dd90fc41b4ab71897f38d324828778ad2e3 /src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
parent72114d732427266024cdd6e27cd8d1aa60afae2f (diff)
parentf28d77dc42f6bac5a026e0b1c78562dee8de45ac (diff)
Merge branch 'trz/rebase' into trz/rename
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java80
1 files changed, 54 insertions, 26 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
index 58488c99..5eab0a6b 100644
--- a/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
+++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpUploadConnection.java
@@ -1,7 +1,12 @@
package de.thedevstack.conversationsplus.http;
import android.app.PendingIntent;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.PowerManager;
+import android.util.Pair;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -12,17 +17,19 @@ import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import de.thedevstack.android.logcat.Logging;
+import de.thedevstack.conversationsplus.ConversationsPlusApplication;
+import de.thedevstack.conversationsplus.utils.MessageUtil;
+import de.thedevstack.conversationsplus.utils.StreamUtil;
import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.entities.Account;
-import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
+import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.persistance.FileBackend;
+import de.thedevstack.conversationsplus.services.AbstractConnectionManager;
import de.thedevstack.conversationsplus.services.XmppConnectionService;
import de.thedevstack.conversationsplus.ui.UiCallback;
import de.thedevstack.conversationsplus.utils.CryptoHelper;
-import de.thedevstack.conversationsplus.utils.MessageUtil;
-import de.thedevstack.conversationsplus.utils.StreamUtil;
import de.thedevstack.conversationsplus.utils.Xmlns;
import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived;
@@ -35,16 +42,19 @@ public class HttpUploadConnection implements Transferable {
private XmppConnectionService mXmppConnectionService;
private boolean canceled = false;
+ private boolean delayed = false;
private Account account;
private DownloadableFile file;
private Message message;
+ private String mime;
private URL mGetUrl;
private URL mPutUrl;
private byte[] key = null;
private long transmitted = 0;
- private long expected = 1;
+
+ private InputStream mFileInputStream;
public HttpUploadConnection(HttpConnectionManager httpConnectionManager) {
this.mHttpConnectionManager = httpConnectionManager;
@@ -63,12 +73,15 @@ public class HttpUploadConnection implements Transferable {
@Override
public long getFileSize() {
- return this.file.getExpectedSize();
+ return file == null ? 0 : file.getExpectedSize();
}
@Override
public int getProgress() {
- return (int) ((((double) transmitted) / expected) * 100);
+ if (file == null) {
+ return 0;
+ }
+ return (int) ((((double) transmitted) / file.getExpectedSize()) * 100);
}
@Override
@@ -79,25 +92,34 @@ public class HttpUploadConnection implements Transferable {
private void fail() {
mHttpConnectionManager.finishUploadConnection(this);
message.setTransferable(null);
- mXmppConnectionService.markMessage(message,Message.STATUS_SEND_FAILED);
+ mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
+ StreamUtil.close(mFileInputStream);
}
- public void init(Message message) {
+ public void init(Message message, boolean delay) {
this.message = message;
- message.setTransferable(this);
- mXmppConnectionService.markMessage(message,Message.STATUS_UNSEND);
this.account = message.getConversation().getAccount();
this.file = FileBackend.getFile(message, false);
- this.file.setExpectedSize(this.file.getSize());
-
- if (Config.ENCRYPT_ON_HTTP_UPLOADED) {
+ this.mime = this.file.getMimeType();
+ this.delayed = delay;
+ if (Config.ENCRYPT_ON_HTTP_UPLOADED
+ || message.getEncryption() == Message.ENCRYPTION_AXOLOTL
+ || message.getEncryption() == Message.ENCRYPTION_OTR) {
this.key = new byte[48];
mXmppConnectionService.getRNG().nextBytes(this.key);
- this.file.setKey(this.key);
+ this.file.setKeyAndIv(this.key);
}
-
+ Pair<InputStream,Integer> pair;
+ try {
+ pair = AbstractConnectionManager.createInputStream(file, true);
+ } catch (FileNotFoundException e) {
+ fail();
+ return;
+ }
+ this.file.setExpectedSize(pair.second);
+ this.mFileInputStream = pair.first;
Jid host = account.getXmppConnection().findDiscoItemByFeature(Xmlns.HTTP_UPLOAD);
- IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file);
+ IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file,mime);
mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
@@ -121,6 +143,8 @@ public class HttpUploadConnection implements Transferable {
}
}
});
+ message.setTransferable(this);
+ mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
}
private class FileUploader implements Runnable {
@@ -132,47 +156,51 @@ public class HttpUploadConnection implements Transferable {
private void upload() {
OutputStream os = null;
- InputStream is = null;
HttpURLConnection connection = null;
+ PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid());
try {
+ wakeLock.acquire();
Logging.d(Config.LOGTAG, "uploading to " + mPutUrl.toString());
connection = (HttpURLConnection) mPutUrl.openConnection();
- if (connection instanceof HttpsURLConnection) {
+
+ if (connection instanceof HttpsURLConnection) {
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true);
}
connection.setRequestMethod("PUT");
connection.setFixedLengthStreamingMode((int) file.getExpectedSize());
+ connection.setRequestProperty("Content-Type", mime == null ? "application/octet-stream" : mime);
+ connection.setRequestProperty("User-Agent", ConversationsPlusApplication.getNameAndVersion());
connection.setDoOutput(true);
connection.connect();
os = connection.getOutputStream();
- is = file.createInputStream();
transmitted = 0;
- expected = file.getExpectedSize();
int count = -1;
byte[] buffer = new byte[4096];
- while (((count = is.read(buffer)) != -1) && !canceled) {
+ while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) {
transmitted += count;
os.write(buffer, 0, count);
mXmppConnectionService.updateConversationUi();
}
os.flush();
os.close();
- is.close();
+ mFileInputStream.close();
int code = connection.getResponseCode();
if (code == 200 || code == 201) {
Logging.d(Config.LOGTAG, "finished uploading file");
- Message.FileParams params = message.getFileParams();
if (key != null) {
mGetUrl = new URL(mGetUrl.toString() + "#" + CryptoHelper.bytesToHex(key));
}
MessageUtil.updateFileParams(message, mGetUrl);
+ Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
+ intent.setData(Uri.fromFile(file));
+ mXmppConnectionService.sendBroadcast(intent);
message.setTransferable(null);
message.setCounterpart(message.getConversation().getJid().toBareJid());
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
mXmppConnectionService.getPgpEngine().encrypt(message, new UiCallback<Message>() {
@Override
public void success(Message message) {
- mXmppConnectionService.resendMessage(message);
+ mXmppConnectionService.resendMessage(message,delayed);
}
@Override
@@ -186,7 +214,7 @@ public class HttpUploadConnection implements Transferable {
}
});
} else {
- mXmppConnectionService.resendMessage(message);
+ mXmppConnectionService.resendMessage(message, delayed);
}
} else {
fail();
@@ -195,11 +223,11 @@ public class HttpUploadConnection implements Transferable {
Logging.d(Config.LOGTAG, e.getMessage());
fail();
} finally {
- StreamUtil.close(is);
StreamUtil.close(os);
if (connection != null) {
connection.disconnect();
}
+ wakeLock.release();
}
}
}