JingleConnection.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.util.Base64;
   4import android.util.Log;
   5
   6import java.io.FileInputStream;
   7import java.io.FileNotFoundException;
   8import java.io.IOException;
   9import java.io.InputStream;
  10import java.io.OutputStream;
  11import java.util.ArrayList;
  12import java.util.Arrays;
  13import java.util.Collections;
  14import java.util.Iterator;
  15import java.util.List;
  16import java.util.Map.Entry;
  17import java.util.concurrent.ConcurrentHashMap;
  18
  19import eu.siacs.conversations.Config;
  20import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  21import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
  22import eu.siacs.conversations.entities.Account;
  23import eu.siacs.conversations.entities.Conversation;
  24import eu.siacs.conversations.entities.DownloadableFile;
  25import eu.siacs.conversations.entities.Message;
  26import eu.siacs.conversations.entities.Presence;
  27import eu.siacs.conversations.entities.ServiceDiscoveryResult;
  28import eu.siacs.conversations.entities.Transferable;
  29import eu.siacs.conversations.entities.TransferablePlaceholder;
  30import eu.siacs.conversations.parser.IqParser;
  31import eu.siacs.conversations.persistance.FileBackend;
  32import eu.siacs.conversations.services.AbstractConnectionManager;
  33import eu.siacs.conversations.services.XmppConnectionService;
  34import eu.siacs.conversations.utils.CryptoHelper;
  35import eu.siacs.conversations.xml.Element;
  36import eu.siacs.conversations.xml.Namespace;
  37import eu.siacs.conversations.xmpp.OnIqPacketReceived;
  38import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  39import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  40import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  41import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  42import rocks.xmpp.addr.Jid;
  43
  44public class JingleConnection implements Transferable {
  45
  46    private static final String JET_OMEMO_CIPHER = "urn:xmpp:ciphers:aes-128-gcm-nopadding";
  47
  48    private static final int JINGLE_STATUS_INITIATED = 0;
  49    private static final int JINGLE_STATUS_ACCEPTED = 1;
  50    private static final int JINGLE_STATUS_FINISHED = 4;
  51    static final int JINGLE_STATUS_TRANSMITTING = 5;
  52    private static final int JINGLE_STATUS_FAILED = 99;
  53    private static final int JINGLE_STATUS_OFFERED = -1;
  54    private JingleConnectionManager mJingleConnectionManager;
  55    private XmppConnectionService mXmppConnectionService;
  56    private Content.Version ftVersion = Content.Version.FT_3;
  57
  58    private int ibbBlockSize = 8192;
  59
  60    private int mJingleStatus = JINGLE_STATUS_OFFERED;
  61    private int mStatus = Transferable.STATUS_UNKNOWN;
  62    private Message message;
  63    private String sessionId;
  64    private Account account;
  65    private Jid initiator;
  66    private Jid responder;
  67    private List<JingleCandidate> candidates = new ArrayList<>();
  68    private ConcurrentHashMap<String, JingleSocks5Transport> connections = new ConcurrentHashMap<>();
  69
  70    private String transportId;
  71    private Element fileOffer;
  72    private DownloadableFile file = null;
  73
  74    private String contentName;
  75    private String contentCreator;
  76    private Transport initialTransport;
  77    private boolean remoteSupportsOmemoJet;
  78
  79    private int mProgress = 0;
  80
  81    private boolean receivedCandidate = false;
  82    private boolean sentCandidate = false;
  83
  84    private boolean acceptedAutomatically = false;
  85    private boolean cancelled = false;
  86
  87    private XmppAxolotlMessage mXmppAxolotlMessage;
  88
  89    private JingleTransport transport = null;
  90
  91    private OutputStream mFileOutputStream;
  92    private InputStream mFileInputStream;
  93
  94    private OnIqPacketReceived responseListener = (account, packet) -> {
  95        if (packet.getType() != IqPacket.TYPE.RESULT) {
  96            fail(IqParser.extractErrorMessage(packet));
  97        }
  98    };
  99    private byte[] expectedHash = new byte[0];
 100    private final OnFileTransmissionStatusChanged onFileTransmissionStatusChanged = new OnFileTransmissionStatusChanged() {
 101
 102        @Override
 103        public void onFileTransmitted(DownloadableFile file) {
 104            if (responding()) {
 105                if (expectedHash.length > 0 && !Arrays.equals(expectedHash, file.getSha1Sum())) {
 106                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": hashes did not match");
 107                }
 108                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": file transmitted(). we are responding");
 109                sendSuccess();
 110                mXmppConnectionService.getFileBackend().updateFileParams(message);
 111                mXmppConnectionService.databaseBackend.createMessage(message);
 112                mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED);
 113                if (acceptedAutomatically) {
 114                    message.markUnread();
 115                    if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 116                        account.getPgpDecryptionService().decrypt(message, true);
 117                    } else {
 118                        mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> JingleConnection.this.mXmppConnectionService.getNotificationService().push(message));
 119
 120                    }
 121                    Log.d(Config.LOGTAG, "successfully transmitted file:" + file.getAbsolutePath() + " (" + CryptoHelper.bytesToHex(file.getSha1Sum()) + ")");
 122                    return;
 123                } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 124                    account.getPgpDecryptionService().decrypt(message, true);
 125                }
 126            } else {
 127                if (ftVersion == Content.Version.FT_5) { //older Conversations will break when receiving a session-info
 128                    sendHash();
 129                }
 130                if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 131                    account.getPgpDecryptionService().decrypt(message, false);
 132                }
 133                if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
 134                    file.delete();
 135                }
 136            }
 137            Log.d(Config.LOGTAG, "successfully transmitted file:" + file.getAbsolutePath() + " (" + CryptoHelper.bytesToHex(file.getSha1Sum()) + ")");
 138            if (message.getEncryption() != Message.ENCRYPTION_PGP) {
 139                mXmppConnectionService.getFileBackend().updateMediaScanner(file);
 140            }
 141        }
 142
 143        @Override
 144        public void onFileTransferAborted() {
 145            JingleConnection.this.sendCancel();
 146            JingleConnection.this.fail();
 147        }
 148    };
 149    private OnTransportConnected onIbbTransportConnected = new OnTransportConnected() {
 150        @Override
 151        public void failed() {
 152            Log.d(Config.LOGTAG, "ibb open failed");
 153        }
 154
 155        @Override
 156        public void established() {
 157            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ibb transport connected. sending file");
 158            mJingleStatus = JINGLE_STATUS_TRANSMITTING;
 159            JingleConnection.this.transport.send(file, onFileTransmissionStatusChanged);
 160        }
 161    };
 162    private OnProxyActivated onProxyActivated = new OnProxyActivated() {
 163
 164        @Override
 165        public void success() {
 166            if (initiator.equals(account.getJid())) {
 167                Log.d(Config.LOGTAG, "we were initiating. sending file");
 168                transport.send(file, onFileTransmissionStatusChanged);
 169            } else {
 170                transport.receive(file, onFileTransmissionStatusChanged);
 171                Log.d(Config.LOGTAG, "we were responding. receiving file");
 172            }
 173        }
 174
 175        @Override
 176        public void failed() {
 177            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": proxy activation failed");
 178            if (initiating()) {
 179                sendFallbackToIbb();
 180            }
 181        }
 182    };
 183
 184    public JingleConnection(JingleConnectionManager mJingleConnectionManager) {
 185        this.mJingleConnectionManager = mJingleConnectionManager;
 186        this.mXmppConnectionService = mJingleConnectionManager
 187                .getXmppConnectionService();
 188    }
 189
 190    private boolean responding() {
 191        return responder != null && responder.equals(account.getJid());
 192    }
 193
 194    private boolean initiating() {
 195        return initiator.equals(account.getJid());
 196    }
 197
 198    InputStream getFileInputStream() {
 199        return this.mFileInputStream;
 200    }
 201
 202    OutputStream getFileOutputStream() throws IOException {
 203        if (this.file == null) {
 204            Log.d(Config.LOGTAG, "file object was not assigned");
 205            return null;
 206        }
 207        this.file.getParentFile().mkdirs();
 208        this.file.createNewFile();
 209        this.mFileOutputStream = AbstractConnectionManager.createOutputStream(this.file);
 210        return this.mFileOutputStream;
 211    }
 212
 213    public String getSessionId() {
 214        return this.sessionId;
 215    }
 216
 217    public Account getAccount() {
 218        return this.account;
 219    }
 220
 221    public Jid getCounterPart() {
 222        return this.message.getCounterpart();
 223    }
 224
 225    public void deliverPacket(JinglePacket packet) {
 226        boolean returnResult = true;
 227        if (packet.isAction("session-terminate")) {
 228            Reason reason = packet.getReason();
 229            if (reason != null) {
 230                if (reason.hasChild("cancel")) {
 231                    this.fail();
 232                } else if (reason.hasChild("success")) {
 233                    this.receiveSuccess();
 234                } else {
 235                    this.fail();
 236                }
 237            } else {
 238                this.fail();
 239            }
 240        } else if (packet.isAction("session-accept")) {
 241            returnResult = receiveAccept(packet);
 242        } else if (packet.isAction("session-info")) {
 243            Element checksum = packet.getChecksum();
 244            Element file = checksum == null ? null : checksum.findChild("file");
 245            Element hash = file == null ? null : file.findChild("hash", "urn:xmpp:hashes:2");
 246            if (hash != null && "sha-1".equalsIgnoreCase(hash.getAttribute("algo"))) {
 247                try {
 248                    this.expectedHash = Base64.decode(hash.getContent(), Base64.DEFAULT);
 249                } catch (Exception e) {
 250                    this.expectedHash = new byte[0];
 251                }
 252            }
 253        } else if (packet.isAction("transport-info")) {
 254            returnResult = receiveTransportInfo(packet);
 255        } else if (packet.isAction("transport-replace")) {
 256            if (packet.getJingleContent().hasIbbTransport()) {
 257                returnResult = this.receiveFallbackToIbb(packet);
 258            } else {
 259                returnResult = false;
 260                Log.d(Config.LOGTAG, "trying to fallback to something unknown"
 261                        + packet.toString());
 262            }
 263        } else if (packet.isAction("transport-accept")) {
 264            returnResult = this.receiveTransportAccept(packet);
 265        } else {
 266            Log.d(Config.LOGTAG, "packet arrived in connection. action was "
 267                    + packet.getAction());
 268            returnResult = false;
 269        }
 270        IqPacket response;
 271        if (returnResult) {
 272            response = packet.generateResponse(IqPacket.TYPE.RESULT);
 273
 274        } else {
 275            response = packet.generateResponse(IqPacket.TYPE.ERROR);
 276        }
 277        mXmppConnectionService.sendIqPacket(account, response, null);
 278    }
 279
 280    public void init(final Message message) {
 281        if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 282            Conversation conversation = (Conversation) message.getConversation();
 283            conversation.getAccount().getAxolotlService().prepareKeyTransportMessage(conversation, xmppAxolotlMessage -> {
 284                if (xmppAxolotlMessage != null) {
 285                    init(message, xmppAxolotlMessage);
 286                } else {
 287                    fail();
 288                }
 289            });
 290        } else {
 291            init(message, null);
 292        }
 293    }
 294
 295    private void init(Message message, XmppAxolotlMessage xmppAxolotlMessage) {
 296        this.mXmppAxolotlMessage = xmppAxolotlMessage;
 297        this.contentCreator = "initiator";
 298        this.contentName = this.mJingleConnectionManager.nextRandomId();
 299        this.message = message;
 300        this.account = message.getConversation().getAccount();
 301        final List<String> remoteFeatures = getRemoteFeatures();
 302        upgradeNamespace(remoteFeatures);
 303        this.initialTransport = remoteFeatures.contains(Namespace.JINGLE_TRANSPORTS_S5B) ? Transport.SOCKS : Transport.IBB;
 304        this.remoteSupportsOmemoJet = remoteFeatures.contains(Namespace.JINGLE_ENCRYPTED_TRANSPORT_OMEMO);
 305        this.message.setTransferable(this);
 306        this.mStatus = Transferable.STATUS_UPLOADING;
 307        this.initiator = this.account.getJid();
 308        this.responder = this.message.getCounterpart();
 309        this.sessionId = this.mJingleConnectionManager.nextRandomId();
 310        this.transportId = this.mJingleConnectionManager.nextRandomId();
 311        if (this.initialTransport == Transport.IBB) {
 312            this.sendInitRequest();
 313        } else {
 314            gatherAndConnectDirectCandidates();
 315            this.mJingleConnectionManager.getPrimaryCandidate(account, initiating(), (success, candidate) -> {
 316                if (success) {
 317                    final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
 318                    connections.put(candidate.getCid(), socksConnection);
 319                    socksConnection.connect(new OnTransportConnected() {
 320
 321                        @Override
 322                        public void failed() {
 323                            Log.d(Config.LOGTAG, String.format("connection to our own proxy65 candidate failed (%s:%d)", candidate.getHost(), candidate.getPort()));
 324                            sendInitRequest();
 325                        }
 326
 327                        @Override
 328                        public void established() {
 329                            Log.d(Config.LOGTAG, "successfully connected to our own proxy65 candidate");
 330                            mergeCandidate(candidate);
 331                            sendInitRequest();
 332                        }
 333                    });
 334                    mergeCandidate(candidate);
 335                } else {
 336                    Log.d(Config.LOGTAG, "no proxy65 candidate of our own was found");
 337                    sendInitRequest();
 338                }
 339            });
 340        }
 341
 342    }
 343
 344    private void gatherAndConnectDirectCandidates() {
 345        final List<JingleCandidate> directCandidates;
 346        if (Config.USE_DIRECT_JINGLE_CANDIDATES) {
 347            if (account.isOnion() || mXmppConnectionService.useTorToConnect()) {
 348                directCandidates = Collections.emptyList();
 349            } else {
 350                directCandidates = DirectConnectionUtils.getLocalCandidates(account.getJid());
 351            }
 352        } else {
 353            directCandidates = Collections.emptyList();
 354        }
 355        for (JingleCandidate directCandidate : directCandidates) {
 356            final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, directCandidate);
 357            connections.put(directCandidate.getCid(), socksConnection);
 358            candidates.add(directCandidate);
 359        }
 360    }
 361
 362    private void upgradeNamespace(List<String> remoteFeatures) {
 363        if (remoteFeatures.contains(Content.Version.FT_5.getNamespace())) {
 364            this.ftVersion = Content.Version.FT_5;
 365        } else if (remoteFeatures.contains(Content.Version.FT_4.getNamespace())) {
 366            this.ftVersion = Content.Version.FT_4;
 367        }
 368    }
 369
 370    private List<String> getRemoteFeatures() {
 371        Jid jid = this.message.getCounterpart();
 372        String resource = jid != null ? jid.getResource() : null;
 373        if (resource != null) {
 374            Presence presence = this.account.getRoster().getContact(jid).getPresences().getPresences().get(resource);
 375            ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null;
 376            return result == null ? Collections.emptyList() : result.getFeatures();
 377        } else {
 378            return Collections.emptyList();
 379        }
 380    }
 381
 382    public void init(Account account, JinglePacket packet) {
 383        this.mJingleStatus = JINGLE_STATUS_INITIATED;
 384        Conversation conversation = this.mXmppConnectionService
 385                .findOrCreateConversation(account,
 386                        packet.getFrom().asBareJid(), false, false);
 387        this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
 388        this.message.setStatus(Message.STATUS_RECEIVED);
 389        this.mStatus = Transferable.STATUS_OFFER;
 390        this.message.setTransferable(this);
 391        final Jid from = packet.getFrom();
 392        this.message.setCounterpart(from);
 393        this.account = account;
 394        this.initiator = packet.getFrom();
 395        this.responder = this.account.getJid();
 396        this.sessionId = packet.getSessionId();
 397        Content content = packet.getJingleContent();
 398        this.contentCreator = content.getAttribute("creator");
 399        this.initialTransport = content.hasSocks5Transport() ? Transport.SOCKS : Transport.IBB;
 400        this.contentName = content.getAttribute("name");
 401        this.transportId = content.getTransportId();
 402
 403        mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
 404
 405        if (this.initialTransport == Transport.SOCKS) {
 406            this.mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
 407        } else if (this.initialTransport == Transport.IBB) {
 408            final String receivedBlockSize = content.ibbTransport().getAttribute("block-size");
 409            if (receivedBlockSize != null) {
 410                try {
 411                    this.ibbBlockSize = Math.min(Integer.parseInt(receivedBlockSize), this.ibbBlockSize);
 412                } catch (NumberFormatException e) {
 413                    Log.d(Config.LOGTAG, "number format exception " + e.getMessage());
 414                    this.sendCancel();
 415                    this.fail();
 416                    return;
 417                }
 418            } else {
 419                Log.d(Config.LOGTAG, "received block size was null");
 420                this.sendCancel();
 421                this.fail();
 422                return;
 423            }
 424        }
 425        this.ftVersion = content.getVersion();
 426        if (ftVersion == null) {
 427            this.sendCancel();
 428            this.fail();
 429            return;
 430        }
 431        this.fileOffer = content.getFileOffer(this.ftVersion);
 432
 433
 434        if (fileOffer != null) {
 435            boolean remoteIsUsingJet = false;
 436            Element encrypted = fileOffer.findChild("encrypted", AxolotlService.PEP_PREFIX);
 437            if (encrypted == null) {
 438                final Element security = content.findChild("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
 439                if (security != null && AxolotlService.PEP_PREFIX.equals(security.getAttribute("type"))) {
 440                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received jingle file offer with JET");
 441                    encrypted = security.findChild("encrypted", AxolotlService.PEP_PREFIX);
 442                    remoteIsUsingJet = true;
 443                }
 444            }
 445            if (encrypted != null) {
 446                this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid());
 447            }
 448            Element fileSize = fileOffer.findChild("size");
 449            final String path = fileOffer.findChildContent("name");
 450            if (path != null) {
 451                AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
 452                if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
 453                    message.setType(Message.TYPE_IMAGE);
 454                    message.setRelativeFilePath(message.getUuid() + "." + extension.main);
 455                } else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
 456                    if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
 457                        message.setType(Message.TYPE_IMAGE);
 458                        message.setRelativeFilePath(message.getUuid() + "." + extension.secondary);
 459                    } else {
 460                        message.setType(Message.TYPE_FILE);
 461                        message.setRelativeFilePath(message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : ""));
 462                    }
 463                    message.setEncryption(Message.ENCRYPTION_PGP);
 464                } else {
 465                    message.setType(Message.TYPE_FILE);
 466                    message.setRelativeFilePath(message.getUuid() + (extension.main != null ? ("." + extension.main) : ""));
 467                }
 468                long size = parseLong(fileSize, 0);
 469                message.setBody(Long.toString(size));
 470                conversation.add(message);
 471                mJingleConnectionManager.updateConversationUi(true);
 472                this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
 473                if (mXmppAxolotlMessage != null) {
 474                    XmppAxolotlMessage.XmppAxolotlKeyTransportMessage transportMessage = account.getAxolotlService().processReceivingKeyTransportMessage(mXmppAxolotlMessage, false);
 475                    if (transportMessage != null) {
 476                        message.setEncryption(Message.ENCRYPTION_AXOLOTL);
 477                        this.file.setKey(transportMessage.getKey());
 478                        this.file.setIv(transportMessage.getIv());
 479                        message.setFingerprint(transportMessage.getFingerprint());
 480                    } else {
 481                        Log.d(Config.LOGTAG, "could not process KeyTransportMessage");
 482                    }
 483                }
 484                message.resetFileParams();
 485                //legacy OMEMO encrypted file transfers reported the file size after encryption
 486                //JET reports the plain text size. however lower levels of our receiving code still
 487                //expect the cipher text size. so we just + 16 bytes (auth tag size) here
 488                this.file.setExpectedSize(size + (remoteIsUsingJet ? 16 : 0));
 489                if (mJingleConnectionManager.hasStoragePermission()
 490                        && size < this.mJingleConnectionManager.getAutoAcceptFileSize()
 491                        && mXmppConnectionService.isDataSaverDisabled()) {
 492                    Log.d(Config.LOGTAG, "auto accepting file from " + packet.getFrom());
 493                    this.acceptedAutomatically = true;
 494                    this.sendAccept();
 495                } else {
 496                    message.markUnread();
 497                    Log.d(Config.LOGTAG,
 498                            "not auto accepting new file offer with size: "
 499                                    + size
 500                                    + " allowed size:"
 501                                    + this.mJingleConnectionManager
 502                                    .getAutoAcceptFileSize());
 503                    this.mXmppConnectionService.getNotificationService().push(message);
 504                }
 505                Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize());
 506            } else {
 507                this.sendCancel();
 508                this.fail();
 509            }
 510        } else {
 511            this.sendCancel();
 512            this.fail();
 513        }
 514    }
 515
 516    private static long parseLong(final Element element, final long l) {
 517        final String input = element == null ? null : element.getContent();
 518        if (input == null) {
 519            return l;
 520        }
 521        try {
 522            return Long.parseLong(input);
 523        } catch (Exception e) {
 524            return l;
 525        }
 526    }
 527
 528    private void sendInitRequest() {
 529        JinglePacket packet = this.bootstrapPacket("session-initiate");
 530        Content content = new Content(this.contentCreator, this.contentName);
 531        if (message.isFileOrImage()) {
 532            content.setTransportId(this.transportId);
 533            this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
 534            if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 535                this.file.setKey(mXmppAxolotlMessage.getInnerKey());
 536                this.file.setIv(mXmppAxolotlMessage.getIV());
 537                //legacy OMEMO encrypted file transfer reported file size of the encrypted file
 538                //JET uses the file size of the plain text file. The difference is only 16 bytes (auth tag)
 539                this.file.setExpectedSize(file.getSize() + (this.remoteSupportsOmemoJet ? 0 : 16));
 540                final Element file = content.setFileOffer(this.file, false, this.ftVersion);
 541                if (remoteSupportsOmemoJet) {
 542                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": remote announced support for JET");
 543                    final Element security = new Element("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
 544                    security.setAttribute("name", this.contentName);
 545                    security.setAttribute("cipher", JET_OMEMO_CIPHER);
 546                    security.setAttribute("type", AxolotlService.PEP_PREFIX);
 547                    security.addChild(mXmppAxolotlMessage.toElement());
 548                    content.addChild(security);
 549                } else {
 550                    file.addChild(mXmppAxolotlMessage.toElement());
 551                }
 552            } else {
 553                this.file.setExpectedSize(file.getSize());
 554                content.setFileOffer(this.file, false, this.ftVersion);
 555            }
 556            message.resetFileParams();
 557            try {
 558                this.mFileInputStream = new FileInputStream(file);
 559            } catch (FileNotFoundException e) {
 560                abort();
 561                return;
 562            }
 563            content.setTransportId(this.transportId);
 564            if (this.initialTransport == Transport.IBB) {
 565                content.ibbTransport().setAttribute("block-size", Integer.toString(this.ibbBlockSize));
 566            } else {
 567                content.socks5transport().setChildren(getCandidatesAsElements());
 568            }
 569            packet.setContent(content);
 570            this.sendJinglePacket(packet, (account, response) -> {
 571                if (response.getType() == IqPacket.TYPE.RESULT) {
 572                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": other party received offer");
 573                    if (mJingleStatus == JINGLE_STATUS_OFFERED) {
 574                        mJingleStatus = JINGLE_STATUS_INITIATED;
 575                        mXmppConnectionService.markMessage(message, Message.STATUS_OFFERED);
 576                    } else {
 577                        Log.d(Config.LOGTAG, "received ack for offer when status was " + mJingleStatus);
 578                    }
 579                } else {
 580                    fail(IqParser.extractErrorMessage(response));
 581                }
 582            });
 583
 584        }
 585    }
 586
 587    private void sendHash() {
 588        JinglePacket packet = this.bootstrapPacket("session-info");
 589        packet.addChecksum(file.getSha1Sum(), ftVersion.getNamespace());
 590        this.sendJinglePacket(packet);
 591    }
 592
 593    private List<Element> getCandidatesAsElements() {
 594        List<Element> elements = new ArrayList<>();
 595        for (JingleCandidate c : this.candidates) {
 596            if (c.isOurs()) {
 597                elements.add(c.toElement());
 598            }
 599        }
 600        return elements;
 601    }
 602
 603    private void sendAccept() {
 604        mJingleStatus = JINGLE_STATUS_ACCEPTED;
 605        this.mStatus = Transferable.STATUS_DOWNLOADING;
 606        this.mJingleConnectionManager.updateConversationUi(true);
 607        if (initialTransport == Transport.SOCKS) {
 608            sendAcceptSocks();
 609        } else {
 610            sendAcceptIbb();
 611        }
 612    }
 613
 614    private void sendAcceptSocks() {
 615        gatherAndConnectDirectCandidates();
 616        this.mJingleConnectionManager.getPrimaryCandidate(this.account, initiating(), (success, candidate) -> {
 617            final JinglePacket packet = bootstrapPacket("session-accept");
 618            final Content content = new Content(contentCreator, contentName);
 619            content.setFileOffer(fileOffer, ftVersion);
 620            content.setTransportId(transportId);
 621            if (success && candidate != null && !equalCandidateExists(candidate)) {
 622                final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
 623                connections.put(candidate.getCid(), socksConnection);
 624                socksConnection.connect(new OnTransportConnected() {
 625
 626                    @Override
 627                    public void failed() {
 628                        Log.d(Config.LOGTAG, "connection to our own proxy65 candidate failed");
 629                        content.socks5transport().setChildren(getCandidatesAsElements());
 630                        packet.setContent(content);
 631                        sendJinglePacket(packet);
 632                        connectNextCandidate();
 633                    }
 634
 635                    @Override
 636                    public void established() {
 637                        Log.d(Config.LOGTAG, "connected to proxy65 candidate");
 638                        mergeCandidate(candidate);
 639                        content.socks5transport().setChildren(getCandidatesAsElements());
 640                        packet.setContent(content);
 641                        sendJinglePacket(packet);
 642                        connectNextCandidate();
 643                    }
 644                });
 645            } else {
 646                Log.d(Config.LOGTAG, "did not find a proxy65 candidate for ourselves");
 647                content.socks5transport().setChildren(getCandidatesAsElements());
 648                packet.setContent(content);
 649                sendJinglePacket(packet);
 650                connectNextCandidate();
 651            }
 652        });
 653    }
 654
 655    private void sendAcceptIbb() {
 656        this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
 657        final JinglePacket packet = bootstrapPacket("session-accept");
 658        final Content content = new Content(contentCreator, contentName);
 659        content.setFileOffer(fileOffer, ftVersion);
 660        content.setTransportId(transportId);
 661        content.ibbTransport().setAttribute("block-size", this.ibbBlockSize);
 662        packet.setContent(content);
 663        this.transport.receive(file, onFileTransmissionStatusChanged);
 664        this.sendJinglePacket(packet);
 665    }
 666
 667    private JinglePacket bootstrapPacket(String action) {
 668        JinglePacket packet = new JinglePacket();
 669        packet.setAction(action);
 670        packet.setFrom(account.getJid());
 671        packet.setTo(this.message.getCounterpart());
 672        packet.setSessionId(this.sessionId);
 673        packet.setInitiator(this.initiator);
 674        return packet;
 675    }
 676
 677    private void sendJinglePacket(JinglePacket packet) {
 678        mXmppConnectionService.sendIqPacket(account, packet, responseListener);
 679    }
 680
 681    private void sendJinglePacket(JinglePacket packet, OnIqPacketReceived callback) {
 682        mXmppConnectionService.sendIqPacket(account, packet, callback);
 683    }
 684
 685    private boolean receiveAccept(JinglePacket packet) {
 686        if (this.mJingleStatus != JINGLE_STATUS_INITIATED) {
 687            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received out of order session-accept");
 688            return false;
 689        }
 690        this.mJingleStatus = JINGLE_STATUS_ACCEPTED;
 691        mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
 692        Content content = packet.getJingleContent();
 693        if (content.hasSocks5Transport()) {
 694            mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
 695            this.connectNextCandidate();
 696            return true;
 697        } else if (content.hasIbbTransport()) {
 698            String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
 699            if (receivedBlockSize != null) {
 700                try {
 701                    int bs = Integer.parseInt(receivedBlockSize);
 702                    if (bs > this.ibbBlockSize) {
 703                        this.ibbBlockSize = bs;
 704                    }
 705                } catch (Exception e) {
 706                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in session-accept");
 707                }
 708            }
 709            this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
 710            this.transport.connect(onIbbTransportConnected);
 711            return true;
 712        } else {
 713            return false;
 714        }
 715    }
 716
 717    private boolean receiveTransportInfo(JinglePacket packet) {
 718        Content content = packet.getJingleContent();
 719        if (content.hasSocks5Transport()) {
 720            if (content.socks5transport().hasChild("activated")) {
 721                if ((this.transport != null) && (this.transport instanceof JingleSocks5Transport)) {
 722                    onProxyActivated.success();
 723                } else {
 724                    String cid = content.socks5transport().findChild("activated").getAttribute("cid");
 725                    Log.d(Config.LOGTAG, "received proxy activated (" + cid
 726                            + ")prior to choosing our own transport");
 727                    JingleSocks5Transport connection = this.connections.get(cid);
 728                    if (connection != null) {
 729                        connection.setActivated(true);
 730                    } else {
 731                        Log.d(Config.LOGTAG, "activated connection not found");
 732                        this.sendCancel();
 733                        this.fail();
 734                    }
 735                }
 736                return true;
 737            } else if (content.socks5transport().hasChild("proxy-error")) {
 738                onProxyActivated.failed();
 739                return true;
 740            } else if (content.socks5transport().hasChild("candidate-error")) {
 741                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received candidate error");
 742                this.receivedCandidate = true;
 743                if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
 744                    this.connect();
 745                }
 746                return true;
 747            } else if (content.socks5transport().hasChild("candidate-used")) {
 748                String cid = content.socks5transport().findChild("candidate-used").getAttribute("cid");
 749                if (cid != null) {
 750                    Log.d(Config.LOGTAG, "candidate used by counterpart:" + cid);
 751                    JingleCandidate candidate = getCandidate(cid);
 752                    if (candidate == null) {
 753                        Log.d(Config.LOGTAG, "could not find candidate with cid=" + cid);
 754                        return false;
 755                    }
 756                    candidate.flagAsUsedByCounterpart();
 757                    this.receivedCandidate = true;
 758                    if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
 759                        this.connect();
 760                    } else {
 761                        Log.d(Config.LOGTAG, "ignoring because file is already in transmission or we haven't sent our candidate yet status=" + mJingleStatus + " sentCandidate=" + sentCandidate);
 762                    }
 763                    return true;
 764                } else {
 765                    return false;
 766                }
 767            } else {
 768                return false;
 769            }
 770        } else {
 771            return true;
 772        }
 773    }
 774
 775    private void connect() {
 776        final JingleSocks5Transport connection = chooseConnection();
 777        this.transport = connection;
 778        if (connection == null) {
 779            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not find suitable candidate");
 780            this.disconnectSocks5Connections();
 781            if (initiating()) {
 782                this.sendFallbackToIbb();
 783            }
 784        } else {
 785            final JingleCandidate candidate = connection.getCandidate();
 786            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": elected candidate " + candidate.getHost() + ":" + candidate.getPort());
 787            this.mJingleStatus = JINGLE_STATUS_TRANSMITTING;
 788            if (connection.needsActivation()) {
 789                if (connection.getCandidate().isOurs()) {
 790                    final String sid;
 791                    if (ftVersion == Content.Version.FT_3) {
 792                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": use session ID instead of transport ID to activate proxy");
 793                        sid = getSessionId();
 794                    } else {
 795                        sid = getTransportId();
 796                    }
 797                    Log.d(Config.LOGTAG, "candidate "
 798                            + connection.getCandidate().getCid()
 799                            + " was our proxy. going to activate");
 800                    IqPacket activation = new IqPacket(IqPacket.TYPE.SET);
 801                    activation.setTo(connection.getCandidate().getJid());
 802                    activation.query("http://jabber.org/protocol/bytestreams")
 803                            .setAttribute("sid", sid);
 804                    activation.query().addChild("activate")
 805                            .setContent(this.getCounterPart().toString());
 806                    mXmppConnectionService.sendIqPacket(account, activation, (account, response) -> {
 807                        if (response.getType() != IqPacket.TYPE.RESULT) {
 808                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + response.toString());
 809                            sendProxyError();
 810                            onProxyActivated.failed();
 811                        } else {
 812                            sendProxyActivated(connection.getCandidate().getCid());
 813                            onProxyActivated.success();
 814                        }
 815                    });
 816                } else {
 817                    Log.d(Config.LOGTAG,
 818                            "candidate "
 819                                    + connection.getCandidate().getCid()
 820                                    + " was a proxy. waiting for other party to activate");
 821                }
 822            } else {
 823                if (initiating()) {
 824                    Log.d(Config.LOGTAG, "we were initiating. sending file");
 825                    connection.send(file, onFileTransmissionStatusChanged);
 826                } else {
 827                    Log.d(Config.LOGTAG, "we were responding. receiving file");
 828                    connection.receive(file, onFileTransmissionStatusChanged);
 829                }
 830            }
 831        }
 832    }
 833
 834    private JingleSocks5Transport chooseConnection() {
 835        JingleSocks5Transport connection = null;
 836        for (Entry<String, JingleSocks5Transport> cursor : connections
 837                .entrySet()) {
 838            JingleSocks5Transport currentConnection = cursor.getValue();
 839            // Log.d(Config.LOGTAG,"comparing candidate: "+currentConnection.getCandidate().toString());
 840            if (currentConnection.isEstablished()
 841                    && (currentConnection.getCandidate().isUsedByCounterpart() || (!currentConnection
 842                    .getCandidate().isOurs()))) {
 843                // Log.d(Config.LOGTAG,"is usable");
 844                if (connection == null) {
 845                    connection = currentConnection;
 846                } else {
 847                    if (connection.getCandidate().getPriority() < currentConnection
 848                            .getCandidate().getPriority()) {
 849                        connection = currentConnection;
 850                    } else if (connection.getCandidate().getPriority() == currentConnection
 851                            .getCandidate().getPriority()) {
 852                        // Log.d(Config.LOGTAG,"found two candidates with same priority");
 853                        if (initiating()) {
 854                            if (currentConnection.getCandidate().isOurs()) {
 855                                connection = currentConnection;
 856                            }
 857                        } else {
 858                            if (!currentConnection.getCandidate().isOurs()) {
 859                                connection = currentConnection;
 860                            }
 861                        }
 862                    }
 863                }
 864            }
 865        }
 866        return connection;
 867    }
 868
 869    private void sendSuccess() {
 870        JinglePacket packet = bootstrapPacket("session-terminate");
 871        Reason reason = new Reason();
 872        reason.addChild("success");
 873        packet.setReason(reason);
 874        this.sendJinglePacket(packet);
 875        this.disconnectSocks5Connections();
 876        this.mJingleStatus = JINGLE_STATUS_FINISHED;
 877        this.message.setStatus(Message.STATUS_RECEIVED);
 878        this.message.setTransferable(null);
 879        this.mXmppConnectionService.updateMessage(message, false);
 880        this.mJingleConnectionManager.finishConnection(this);
 881    }
 882
 883    private void sendFallbackToIbb() {
 884        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending fallback to ibb");
 885        JinglePacket packet = this.bootstrapPacket("transport-replace");
 886        Content content = new Content(this.contentCreator, this.contentName);
 887        this.transportId = this.mJingleConnectionManager.nextRandomId();
 888        content.setTransportId(this.transportId);
 889        content.ibbTransport().setAttribute("block-size",
 890                Integer.toString(this.ibbBlockSize));
 891        packet.setContent(content);
 892        this.sendJinglePacket(packet);
 893    }
 894
 895
 896    private boolean receiveFallbackToIbb(JinglePacket packet) {
 897        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": receiving fallback to ibb");
 898        final String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
 899        if (receivedBlockSize != null) {
 900            try {
 901                final int bs = Integer.parseInt(receivedBlockSize);
 902                if (bs < this.ibbBlockSize) {
 903                    this.ibbBlockSize = bs;
 904                }
 905            } catch (NumberFormatException e) {
 906                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-replace");
 907            }
 908        }
 909        this.transportId = packet.getJingleContent().getTransportId();
 910        this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
 911
 912        final JinglePacket answer = bootstrapPacket("transport-accept");
 913
 914        final Content content = new Content(contentCreator, contentName);
 915        content.ibbTransport().setAttribute("block-size", this.ibbBlockSize);
 916        content.ibbTransport().setAttribute("sid", this.transportId);
 917        answer.setContent(content);
 918
 919
 920        if (initiating()) {
 921            this.sendJinglePacket(answer, (account, response) -> {
 922                if (response.getType() == IqPacket.TYPE.RESULT) {
 923                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + " recipient ACKed our transport-accept. creating ibb");
 924                    transport.connect(onIbbTransportConnected);
 925                }
 926            });
 927        } else {
 928            this.transport.receive(file, onFileTransmissionStatusChanged);
 929            this.sendJinglePacket(answer);
 930        }
 931        return true;
 932    }
 933
 934    private boolean receiveTransportAccept(JinglePacket packet) {
 935        if (packet.getJingleContent().hasIbbTransport()) {
 936            String receivedBlockSize = packet.getJingleContent().ibbTransport()
 937                    .getAttribute("block-size");
 938            if (receivedBlockSize != null) {
 939                try {
 940                    int bs = Integer.parseInt(receivedBlockSize);
 941                    if (bs < this.ibbBlockSize) {
 942                        this.ibbBlockSize = bs;
 943                    }
 944                } catch (NumberFormatException e) {
 945                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-accept");
 946                }
 947            }
 948            this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
 949
 950            //might be receive instead if we are not initiating
 951            if (initiating()) {
 952                this.transport.connect(onIbbTransportConnected);
 953            } else {
 954                this.transport.receive(file, onFileTransmissionStatusChanged);
 955            }
 956            return true;
 957        } else {
 958            return false;
 959        }
 960    }
 961
 962    private void receiveSuccess() {
 963        if (initiating()) {
 964            this.mJingleStatus = JINGLE_STATUS_FINISHED;
 965            this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED);
 966            this.disconnectSocks5Connections();
 967            if (this.transport instanceof JingleInbandTransport) {
 968                this.transport.disconnect();
 969            }
 970            this.message.setTransferable(null);
 971            this.mJingleConnectionManager.finishConnection(this);
 972        } else {
 973            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received session-terminate/success while responding");
 974        }
 975    }
 976
 977    @Override
 978    public void cancel() {
 979        this.cancelled = true;
 980        abort();
 981    }
 982
 983    public void abort() {
 984        this.disconnectSocks5Connections();
 985        if (this.transport instanceof JingleInbandTransport) {
 986            this.transport.disconnect();
 987        }
 988        this.sendCancel();
 989        this.mJingleConnectionManager.finishConnection(this);
 990        if (responding()) {
 991            this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
 992            if (this.file != null) {
 993                file.delete();
 994            }
 995            this.mJingleConnectionManager.updateConversationUi(true);
 996        } else {
 997            this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : null);
 998            this.message.setTransferable(null);
 999        }
1000    }
1001
1002    private void fail() {
1003        fail(null);
1004    }
1005
1006    private void fail(String errorMessage) {
1007        this.mJingleStatus = JINGLE_STATUS_FAILED;
1008        this.disconnectSocks5Connections();
1009        if (this.transport instanceof JingleInbandTransport) {
1010            this.transport.disconnect();
1011        }
1012        FileBackend.close(mFileInputStream);
1013        FileBackend.close(mFileOutputStream);
1014        if (this.message != null) {
1015            if (responding()) {
1016                this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
1017                if (this.file != null) {
1018                    file.delete();
1019                }
1020                this.mJingleConnectionManager.updateConversationUi(true);
1021            } else {
1022                this.mXmppConnectionService.markMessage(this.message,
1023                        Message.STATUS_SEND_FAILED,
1024                        cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
1025                this.message.setTransferable(null);
1026            }
1027        }
1028        this.mJingleConnectionManager.finishConnection(this);
1029    }
1030
1031    private void sendCancel() {
1032        JinglePacket packet = bootstrapPacket("session-terminate");
1033        Reason reason = new Reason();
1034        reason.addChild("cancel");
1035        packet.setReason(reason);
1036        this.sendJinglePacket(packet);
1037    }
1038
1039    private void connectNextCandidate() {
1040        for (JingleCandidate candidate : this.candidates) {
1041            if ((!connections.containsKey(candidate.getCid()) && (!candidate
1042                    .isOurs()))) {
1043                this.connectWithCandidate(candidate);
1044                return;
1045            }
1046        }
1047        this.sendCandidateError();
1048    }
1049
1050    private void connectWithCandidate(final JingleCandidate candidate) {
1051        final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
1052                this, candidate);
1053        connections.put(candidate.getCid(), socksConnection);
1054        socksConnection.connect(new OnTransportConnected() {
1055
1056            @Override
1057            public void failed() {
1058                Log.d(Config.LOGTAG,
1059                        "connection failed with " + candidate.getHost() + ":"
1060                                + candidate.getPort());
1061                connectNextCandidate();
1062            }
1063
1064            @Override
1065            public void established() {
1066                Log.d(Config.LOGTAG,
1067                        "established connection with " + candidate.getHost()
1068                                + ":" + candidate.getPort());
1069                sendCandidateUsed(candidate.getCid());
1070            }
1071        });
1072    }
1073
1074    private void disconnectSocks5Connections() {
1075        Iterator<Entry<String, JingleSocks5Transport>> it = this.connections
1076                .entrySet().iterator();
1077        while (it.hasNext()) {
1078            Entry<String, JingleSocks5Transport> pairs = it.next();
1079            pairs.getValue().disconnect();
1080            it.remove();
1081        }
1082    }
1083
1084    private void sendProxyActivated(String cid) {
1085        final JinglePacket packet = bootstrapPacket("transport-info");
1086        final Content content = new Content(this.contentCreator, this.contentName);
1087        content.setTransportId(this.transportId);
1088        content.socks5transport().addChild("activated").setAttribute("cid", cid);
1089        packet.setContent(content);
1090        this.sendJinglePacket(packet);
1091    }
1092
1093    private void sendProxyError() {
1094        final JinglePacket packet = bootstrapPacket("transport-info");
1095        final Content content = new Content(this.contentCreator, this.contentName);
1096        content.setTransportId(this.transportId);
1097        content.socks5transport().addChild("proxy-error");
1098        packet.setContent(content);
1099        this.sendJinglePacket(packet);
1100    }
1101
1102    private void sendCandidateUsed(final String cid) {
1103        JinglePacket packet = bootstrapPacket("transport-info");
1104        Content content = new Content(this.contentCreator, this.contentName);
1105        content.setTransportId(this.transportId);
1106        content.socks5transport().addChild("candidate-used").setAttribute("cid", cid);
1107        packet.setContent(content);
1108        this.sentCandidate = true;
1109        if ((receivedCandidate) && (mJingleStatus == JINGLE_STATUS_ACCEPTED)) {
1110            connect();
1111        }
1112        this.sendJinglePacket(packet);
1113    }
1114
1115    private void sendCandidateError() {
1116        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending candidate error");
1117        JinglePacket packet = bootstrapPacket("transport-info");
1118        Content content = new Content(this.contentCreator, this.contentName);
1119        content.setTransportId(this.transportId);
1120        content.socks5transport().addChild("candidate-error");
1121        packet.setContent(content);
1122        this.sentCandidate = true;
1123        this.sendJinglePacket(packet);
1124        if (receivedCandidate && mJingleStatus == JINGLE_STATUS_ACCEPTED) {
1125            connect();
1126        }
1127    }
1128
1129    public int getJingleStatus() {
1130        return this.mJingleStatus;
1131    }
1132
1133    private boolean equalCandidateExists(JingleCandidate candidate) {
1134        for (JingleCandidate c : this.candidates) {
1135            if (c.equalValues(candidate)) {
1136                return true;
1137            }
1138        }
1139        return false;
1140    }
1141
1142    private void mergeCandidate(JingleCandidate candidate) {
1143        for (JingleCandidate c : this.candidates) {
1144            if (c.equals(candidate)) {
1145                return;
1146            }
1147        }
1148        this.candidates.add(candidate);
1149    }
1150
1151    private void mergeCandidates(List<JingleCandidate> candidates) {
1152        Collections.sort(candidates, (a, b) -> Integer.compare(b.getPriority(), a.getPriority()));
1153        for (JingleCandidate c : candidates) {
1154            mergeCandidate(c);
1155        }
1156    }
1157
1158    private JingleCandidate getCandidate(String cid) {
1159        for (JingleCandidate c : this.candidates) {
1160            if (c.getCid().equals(cid)) {
1161                return c;
1162            }
1163        }
1164        return null;
1165    }
1166
1167    void updateProgress(int i) {
1168        this.mProgress = i;
1169        mJingleConnectionManager.updateConversationUi(false);
1170    }
1171
1172    public String getTransportId() {
1173        return this.transportId;
1174    }
1175
1176    public Content.Version getFtVersion() {
1177        return this.ftVersion;
1178    }
1179
1180    public boolean hasTransportId(String sid) {
1181        return sid.equals(this.transportId);
1182    }
1183
1184    public JingleTransport getTransport() {
1185        return this.transport;
1186    }
1187
1188    public boolean start() {
1189        if (account.getStatus() == Account.State.ONLINE) {
1190            if (mJingleStatus == JINGLE_STATUS_INITIATED) {
1191                new Thread(this::sendAccept).start();
1192            }
1193            return true;
1194        } else {
1195            return false;
1196        }
1197    }
1198
1199    @Override
1200    public int getStatus() {
1201        return this.mStatus;
1202    }
1203
1204    @Override
1205    public long getFileSize() {
1206        if (this.file != null) {
1207            return this.file.getExpectedSize();
1208        } else {
1209            return 0;
1210        }
1211    }
1212
1213    @Override
1214    public int getProgress() {
1215        return this.mProgress;
1216    }
1217
1218    public AbstractConnectionManager getConnectionManager() {
1219        return this.mJingleConnectionManager;
1220    }
1221
1222    interface OnProxyActivated {
1223        void success();
1224
1225        void failed();
1226    }
1227}