diff options
Diffstat (limited to 'src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java')
-rw-r--r-- | src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java | 76 |
1 files changed, 37 insertions, 39 deletions
diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java index 6c726c8b9..2f3ec09ea 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java @@ -2,8 +2,8 @@ package de.pixart.messenger.xmpp.jingle; import android.util.Base64; import android.util.Log; -import android.util.Pair; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -84,6 +84,7 @@ public class JingleConnection implements Transferable { private boolean sentCandidate = false; private boolean acceptedAutomatically = false; + private boolean cancelled = false; private XmppAxolotlMessage mXmppAxolotlMessage; @@ -92,13 +93,9 @@ public class JingleConnection implements Transferable { private OutputStream mFileOutputStream; private InputStream mFileInputStream; - private OnIqPacketReceived responseListener = new OnIqPacketReceived() { - - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - if (packet.getType() != IqPacket.TYPE.RESULT) { - fail(IqParser.extractErrorMessage(packet)); - } + private OnIqPacketReceived responseListener = (account, packet) -> { + if (packet.getType() != IqPacket.TYPE.RESULT) { + fail(IqParser.extractErrorMessage(packet)); } }; private byte[] expectedHash = new byte[0]; @@ -489,36 +486,32 @@ public class JingleConnection implements Transferable { if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) { content.setTransportId(this.transportId); this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false); - Pair<InputStream, Integer> pair; - try { - if (message.getEncryption() == Message.ENCRYPTION_OTR) { - Conversational conversation = this.message.getConversation(); - if (!this.mXmppConnectionService.renewSymmetricKey((Conversation) conversation)) { - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not set symmetric key"); - cancel(); - } - Conversation c = (Conversation) this.message.getConversation(); - this.file.setKeyAndIv(c.getSymmetricKey()); - pair = AbstractConnectionManager.createInputStream(this.file, false); - this.file.setExpectedSize(pair.second); - content.setFileOffer(this.file, true, this.ftVersion); - } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) { - this.file.setKey(mXmppAxolotlMessage.getInnerKey()); - this.file.setIv(mXmppAxolotlMessage.getIV()); - pair = AbstractConnectionManager.createInputStream(this.file, true); - this.file.setExpectedSize(pair.second); - content.setFileOffer(this.file, false, this.ftVersion).addChild(mXmppAxolotlMessage.toElement()); - } else { - pair = AbstractConnectionManager.createInputStream(this.file, false); - this.file.setExpectedSize(pair.second); - content.setFileOffer(this.file, false, this.ftVersion); + if (message.getEncryption() == Message.ENCRYPTION_OTR) { + Conversational conversation = this.message.getConversation(); + if (!this.mXmppConnectionService.renewSymmetricKey((Conversation) conversation)) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not set symmetric key"); + cancel(); } + Conversation c = (Conversation) this.message.getConversation(); + this.file.setKeyAndIv(c.getSymmetricKey()); + this.file.setExpectedSize((file.getSize() / 16 + 1) * 16); + content.setFileOffer(this.file, true, this.ftVersion); + } else if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) { + this.file.setKey(mXmppAxolotlMessage.getInnerKey()); + this.file.setIv(mXmppAxolotlMessage.getIV()); + this.file.setExpectedSize(file.getSize() + 16); + content.setFileOffer(this.file, false, this.ftVersion).addChild(mXmppAxolotlMessage.toElement()); + } else { + this.file.setExpectedSize(file.getSize()); + content.setFileOffer(this.file, false, this.ftVersion); + } + message.resetFileParams(); + try { + this.mFileInputStream = new FileInputStream(file); } catch (FileNotFoundException e) { - cancel(); + abort(); return; } - message.resetFileParams(); - this.mFileInputStream = pair.first; content.setTransportId(this.transportId); content.socks5transport().setChildren(getCandidatesAsElements()); packet.setContent(content); @@ -893,7 +886,7 @@ public class JingleConnection implements Transferable { this.mJingleStatus = JINGLE_STATUS_FINISHED; this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED); this.disconnectSocks5Connections(); - if (this.transport != null && this.transport instanceof JingleInbandTransport) { + if (this.transport instanceof JingleInbandTransport) { this.transport.disconnect(); } this.message.setTransferable(null); @@ -904,8 +897,13 @@ public class JingleConnection implements Transferable { } public void cancel() { + this.cancelled = true; + abort(); + } + + public void abort() { this.disconnectSocks5Connections(); - if (this.transport != null && this.transport instanceof JingleInbandTransport) { + if (this.transport instanceof JingleInbandTransport) { this.transport.disconnect(); } this.sendCancel(); @@ -917,7 +915,7 @@ public class JingleConnection implements Transferable { } this.mJingleConnectionManager.updateConversationUi(true); } else { - this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED); + this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : null); this.message.setTransferable(null); } } @@ -929,7 +927,7 @@ public class JingleConnection implements Transferable { private void fail(String errorMessage) { this.mJingleStatus = JINGLE_STATUS_FAILED; this.disconnectSocks5Connections(); - if (this.transport != null && this.transport instanceof JingleInbandTransport) { + if (this.transport instanceof JingleInbandTransport) { this.transport.disconnect(); } FileBackend.close(mFileInputStream); @@ -944,7 +942,7 @@ public class JingleConnection implements Transferable { } else { this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, - errorMessage); + cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage); this.message.setTransferable(null); } } |