JingleConnectionManager.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.util.Base64;
   4import android.util.Log;
   5
   6import com.google.common.base.Objects;
   7import com.google.common.base.Optional;
   8import com.google.common.base.Preconditions;
   9import com.google.common.cache.Cache;
  10import com.google.common.cache.CacheBuilder;
  11import com.google.common.collect.Collections2;
  12import com.google.common.collect.ComparisonChain;
  13import com.google.common.collect.ImmutableSet;
  14
  15import java.lang.ref.WeakReference;
  16import java.security.SecureRandom;
  17import java.util.Collection;
  18import java.util.Collections;
  19import java.util.HashMap;
  20import java.util.List;
  21import java.util.Map;
  22import java.util.Set;
  23import java.util.concurrent.ConcurrentHashMap;
  24import java.util.concurrent.Executors;
  25import java.util.concurrent.ScheduledExecutorService;
  26import java.util.concurrent.ScheduledFuture;
  27import java.util.concurrent.TimeUnit;
  28
  29import eu.siacs.conversations.Config;
  30import eu.siacs.conversations.entities.Account;
  31import eu.siacs.conversations.entities.Contact;
  32import eu.siacs.conversations.entities.Conversation;
  33import eu.siacs.conversations.entities.Conversational;
  34import eu.siacs.conversations.entities.Message;
  35import eu.siacs.conversations.entities.RtpSessionStatus;
  36import eu.siacs.conversations.entities.Transferable;
  37import eu.siacs.conversations.services.AbstractConnectionManager;
  38import eu.siacs.conversations.services.XmppConnectionService;
  39import eu.siacs.conversations.xml.Element;
  40import eu.siacs.conversations.xml.Namespace;
  41import eu.siacs.conversations.xmpp.Jid;
  42import eu.siacs.conversations.xmpp.OnIqPacketReceived;
  43import eu.siacs.conversations.xmpp.XmppConnection;
  44import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  45import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
  46import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
  47import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  48import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
  49import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  50import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
  51import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  52import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
  53
  54public class JingleConnectionManager extends AbstractConnectionManager {
  55    static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
  56            Executors.newSingleThreadScheduledExecutor();
  57    final ToneManager toneManager;
  58    private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals =
  59            new HashMap<>();
  60    private final ConcurrentHashMap<AbstractJingleConnection.Id, AbstractJingleConnection>
  61            connections = new ConcurrentHashMap<>();
  62
  63    private final Cache<PersistableSessionId, TerminatedRtpSession> terminatedSessions =
  64            CacheBuilder.newBuilder().expireAfterWrite(24, TimeUnit.HOURS).build();
  65
  66    private final HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
  67
  68    public JingleConnectionManager(XmppConnectionService service) {
  69        super(service);
  70        this.toneManager = new ToneManager(service);
  71    }
  72
  73    static String nextRandomId() {
  74        final byte[] id = new byte[16];
  75        new SecureRandom().nextBytes(id);
  76        return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
  77    }
  78
  79    public void deliverPacket(final Account account, final JinglePacket packet) {
  80        final String sessionId = packet.getSessionId();
  81        if (sessionId == null) {
  82            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
  83            return;
  84        }
  85        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
  86        final AbstractJingleConnection existingJingleConnection = connections.get(id);
  87        if (existingJingleConnection != null) {
  88            existingJingleConnection.deliverPacket(packet);
  89        } else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
  90            final Jid from = packet.getFrom();
  91            final Content content = packet.getJingleContent();
  92            final String descriptionNamespace =
  93                    content == null ? null : content.getDescriptionNamespace();
  94            final AbstractJingleConnection connection;
  95            if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
  96                connection = new JingleFileTransferConnection(this, id, from);
  97            } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace)
  98                    && isUsingClearNet(account)) {
  99                final boolean sessionEnded =
 100                        this.terminatedSessions.asMap().containsKey(PersistableSessionId.of(id));
 101                final boolean stranger =
 102                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 103                if (isBusy() || sessionEnded || stranger) {
 104                    Log.d(
 105                            Config.LOGTAG,
 106                            id.account.getJid().asBareJid()
 107                                    + ": rejected session with "
 108                                    + id.with
 109                                    + " because busy. sessionEnded="
 110                                    + sessionEnded
 111                                    + ", stranger="
 112                                    + stranger);
 113                    mXmppConnectionService.sendIqPacket(
 114                            account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
 115                    final JinglePacket sessionTermination =
 116                            new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
 117                    sessionTermination.setTo(id.with);
 118                    sessionTermination.setReason(Reason.BUSY, null);
 119                    mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
 120                    return;
 121                }
 122                connection = new JingleRtpConnection(this, id, from);
 123            } else {
 124                respondWithJingleError(
 125                        account, packet, "unsupported-info", "feature-not-implemented", "cancel");
 126                return;
 127            }
 128            connections.put(id, connection);
 129            mXmppConnectionService.updateConversationUi();
 130            connection.deliverPacket(packet);
 131        } else {
 132            Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
 133            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
 134        }
 135    }
 136
 137    private boolean isUsingClearNet(final Account account) {
 138        return !account.isOnion() && !mXmppConnectionService.useTorToConnect();
 139    }
 140
 141    public boolean isBusy() {
 142        if (mXmppConnectionService.isPhoneInCall()) {
 143            return true;
 144        }
 145        for (AbstractJingleConnection connection : this.connections.values()) {
 146            if (connection instanceof JingleRtpConnection) {
 147                if (((JingleRtpConnection) connection).isTerminated()) {
 148                    continue;
 149                }
 150                return true;
 151            }
 152        }
 153        synchronized (this.rtpSessionProposals) {
 154            return this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED)
 155                    || this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING)
 156                    || this.rtpSessionProposals.containsValue(
 157                            DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED);
 158        }
 159    }
 160
 161    public boolean hasJingleRtpConnection(final Account account) {
 162        for (AbstractJingleConnection connection : this.connections.values()) {
 163            if (connection instanceof JingleRtpConnection) {
 164                final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
 165                if (rtpConnection.isTerminated()) {
 166                    continue;
 167                }
 168                if (rtpConnection.id.account == account) {
 169                    return true;
 170                }
 171            }
 172        }
 173        return false;
 174    }
 175
 176    public void notifyPhoneCallStarted() {
 177        for (AbstractJingleConnection connection : connections.values()) {
 178            if (connection instanceof JingleRtpConnection) {
 179                final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
 180                if (rtpConnection.isTerminated()) {
 181                    continue;
 182                }
 183                rtpConnection.notifyPhoneCall();
 184            }
 185        }
 186    }
 187
 188
 189    private Optional<RtpSessionProposal> findMatchingSessionProposal(
 190            final Account account, final Jid with, final Set<Media> media) {
 191        synchronized (this.rtpSessionProposals) {
 192            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 193                    this.rtpSessionProposals.entrySet()) {
 194                final RtpSessionProposal proposal = entry.getKey();
 195                final DeviceDiscoveryState state = entry.getValue();
 196                final boolean openProposal =
 197                        state == DeviceDiscoveryState.DISCOVERED
 198                                || state == DeviceDiscoveryState.SEARCHING
 199                                || state == DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED;
 200                if (openProposal
 201                        && proposal.account == account
 202                        && proposal.with.equals(with.asBareJid())
 203                        && proposal.media.equals(media)) {
 204                    return Optional.of(proposal);
 205                }
 206            }
 207        }
 208        return Optional.absent();
 209    }
 210
 211    private boolean hasMatchingRtpSession(
 212            final Account account, final Jid with, final Set<Media> media) {
 213        for (AbstractJingleConnection connection : this.connections.values()) {
 214            if (connection instanceof JingleRtpConnection) {
 215                final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
 216                if (rtpConnection.isTerminated()) {
 217                    continue;
 218                }
 219                if (rtpConnection.getId().account == account
 220                        && rtpConnection.getId().with.asBareJid().equals(with.asBareJid())
 221                        && rtpConnection.getMedia().equals(media)) {
 222                    return true;
 223                }
 224            }
 225        }
 226        return false;
 227    }
 228
 229    private boolean isWithStrangerAndStrangerNotificationsAreOff(final Account account, Jid with) {
 230        final boolean notifyForStrangers =
 231                mXmppConnectionService.getNotificationService().notificationsFromStrangers();
 232        if (notifyForStrangers) {
 233            return false;
 234        }
 235        final Contact contact = account.getRoster().getContact(with);
 236        return !contact.showInContactList();
 237    }
 238
 239    ScheduledFuture<?> schedule(
 240            final Runnable runnable, final long delay, final TimeUnit timeUnit) {
 241        return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, timeUnit);
 242    }
 243
 244    void respondWithJingleError(
 245            final Account account,
 246            final IqPacket original,
 247            String jingleCondition,
 248            String condition,
 249            String conditionType) {
 250        final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
 251        final Element error = response.addChild("error");
 252        error.setAttribute("type", conditionType);
 253        error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
 254        error.addChild(jingleCondition, Namespace.JINGLE_ERRORS);
 255        account.getXmppConnection().sendIqPacket(response, null);
 256    }
 257
 258    public void deliverMessage(
 259            final Account account,
 260            final Jid to,
 261            final Jid from,
 262            final Element message,
 263            String remoteMsgId,
 264            String serverMsgId,
 265            long timestamp) {
 266        Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
 267        final String sessionId = message.getAttribute("id");
 268        if (sessionId == null) {
 269            return;
 270        }
 271        if ("accept".equals(message.getName())) {
 272            for (AbstractJingleConnection connection : connections.values()) {
 273                if (connection instanceof JingleRtpConnection) {
 274                    final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
 275                    final AbstractJingleConnection.Id id = connection.getId();
 276                    if (id.account == account && id.sessionId.equals(sessionId)) {
 277                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 278                        return;
 279                    }
 280                }
 281            }
 282            return;
 283        }
 284        final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
 285        // XEP version 0.6.0 sends proceed, reject, ringing to bare jid
 286        final boolean addressedDirectly = to != null && to.equals(account.getJid());
 287        final AbstractJingleConnection.Id id;
 288        if (fromSelf) {
 289            if (to != null && to.isFullJid()) {
 290                id = AbstractJingleConnection.Id.of(account, to, sessionId);
 291            } else {
 292                return;
 293            }
 294        } else {
 295            id = AbstractJingleConnection.Id.of(account, from, sessionId);
 296        }
 297        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 298        if (existingJingleConnection != null) {
 299            if (existingJingleConnection instanceof JingleRtpConnection) {
 300                ((JingleRtpConnection) existingJingleConnection)
 301                        .deliveryMessage(from, message, serverMsgId, timestamp);
 302            } else {
 303                Log.d(
 304                        Config.LOGTAG,
 305                        account.getJid().asBareJid()
 306                                + ": "
 307                                + existingJingleConnection.getClass().getName()
 308                                + " does not support jingle messages");
 309            }
 310            return;
 311        }
 312
 313        if (fromSelf) {
 314            if ("proceed".equals(message.getName())) {
 315                final Conversation c =
 316                        mXmppConnectionService.findOrCreateConversation(
 317                                account, id.with, false, false);
 318                final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
 319                if (previousBusy != null) {
 320                    previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
 321                    if (serverMsgId != null) {
 322                        previousBusy.setServerMsgId(serverMsgId);
 323                    }
 324                    previousBusy.setTime(timestamp);
 325                    mXmppConnectionService.updateMessage(previousBusy, true);
 326                    Log.d(
 327                            Config.LOGTAG,
 328                            id.account.getJid().asBareJid()
 329                                    + ": updated previous busy because call got picked up by another device");
 330                    return;
 331                }
 332            }
 333            // TODO handle reject for cases where we don’t have carbon copies (normally reject is to
 334            // be sent to own bare jid as well)
 335            Log.d(
 336                    Config.LOGTAG,
 337                    account.getJid().asBareJid() + ": ignore jingle message from self");
 338            return;
 339        }
 340
 341        if ("propose".equals(message.getName())) {
 342            final Propose propose = Propose.upgrade(message);
 343            final List<GenericDescription> descriptions = propose.getDescriptions();
 344            final Collection<RtpDescription> rtpDescriptions =
 345                    Collections2.transform(
 346                            Collections2.filter(descriptions, d -> d instanceof RtpDescription),
 347                            input -> (RtpDescription) input);
 348            if (rtpDescriptions.size() > 0
 349                    && rtpDescriptions.size() == descriptions.size()
 350                    && isUsingClearNet(account)) {
 351                final Collection<Media> media =
 352                        Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
 353                if (media.contains(Media.UNKNOWN)) {
 354                    Log.d(
 355                            Config.LOGTAG,
 356                            account.getJid().asBareJid()
 357                                    + ": encountered unknown media in session proposal. "
 358                                    + propose);
 359                    return;
 360                }
 361                final Optional<RtpSessionProposal> matchingSessionProposal =
 362                        findMatchingSessionProposal(account, id.with, ImmutableSet.copyOf(media));
 363                if (matchingSessionProposal.isPresent()) {
 364                    final String ourSessionId = matchingSessionProposal.get().sessionId;
 365                    final String theirSessionId = id.sessionId;
 366                    if (ComparisonChain.start()
 367                                    .compare(ourSessionId, theirSessionId)
 368                                    .compare(
 369                                            account.getJid().toEscapedString(),
 370                                            id.with.toEscapedString())
 371                                    .result()
 372                            > 0) {
 373                        Log.d(
 374                                Config.LOGTAG,
 375                                account.getJid().asBareJid()
 376                                        + ": our session lost tie break. automatically accepting their session. winning Session="
 377                                        + theirSessionId);
 378                        // TODO a retract for this reason should probably include some indication of
 379                        // tie break
 380                        retractSessionProposal(matchingSessionProposal.get());
 381                        final JingleRtpConnection rtpConnection =
 382                                new JingleRtpConnection(this, id, from);
 383                        this.connections.put(id, rtpConnection);
 384                        rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 385                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 386                        // TODO actually do the automatic accept?!
 387                    } else {
 388                        Log.d(
 389                                Config.LOGTAG,
 390                                account.getJid().asBareJid()
 391                                        + ": our session won tie break. waiting for other party to accept. winningSession="
 392                                        + ourSessionId);
 393                        // TODO reject their session with <tie-break/>?
 394                    }
 395                    return;
 396                }
 397                final boolean stranger =
 398                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 399                if (isBusy() || stranger) {
 400                    writeLogMissedIncoming(
 401                            account, id.with.asBareJid(), id.sessionId, serverMsgId, timestamp);
 402                    if (stranger) {
 403                        Log.d(
 404                                Config.LOGTAG,
 405                                id.account.getJid().asBareJid()
 406                                        + ": ignoring call proposal from stranger "
 407                                        + id.with);
 408                        return;
 409                    }
 410                    final int activeDevices = account.activeDevicesWithRtpCapability();
 411                    Log.d(Config.LOGTAG, "active devices with rtp capability: " + activeDevices);
 412                    if (activeDevices == 0) {
 413                        final MessagePacket reject =
 414                                mXmppConnectionService
 415                                        .getMessageGenerator()
 416                                        .sessionReject(from, sessionId);
 417                        mXmppConnectionService.sendMessagePacket(account, reject);
 418                    } else {
 419                        Log.d(
 420                                Config.LOGTAG,
 421                                id.account.getJid().asBareJid()
 422                                        + ": ignoring proposal because busy on this device but there are other devices");
 423                    }
 424                } else {
 425                    final JingleRtpConnection rtpConnection =
 426                            new JingleRtpConnection(this, id, from);
 427                    this.connections.put(id, rtpConnection);
 428                    rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 429                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 430                }
 431            } else {
 432                Log.d(
 433                        Config.LOGTAG,
 434                        account.getJid().asBareJid()
 435                                + ": unable to react to proposed session with "
 436                                + rtpDescriptions.size()
 437                                + " rtp descriptions of "
 438                                + descriptions.size()
 439                                + " total descriptions");
 440            }
 441        } else if (addressedDirectly && "proceed".equals(message.getName())) {
 442            synchronized (rtpSessionProposals) {
 443                final RtpSessionProposal proposal =
 444                        getRtpSessionProposal(account, from.asBareJid(), sessionId);
 445                if (proposal != null) {
 446                    rtpSessionProposals.remove(proposal);
 447                    final JingleRtpConnection rtpConnection =
 448                            new JingleRtpConnection(this, id, account.getJid());
 449                    rtpConnection.setProposedMedia(proposal.media);
 450                    this.connections.put(id, rtpConnection);
 451                    rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
 452                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 453                } else {
 454                    Log.d(
 455                            Config.LOGTAG,
 456                            account.getJid().asBareJid()
 457                                    + ": no rtp session ("
 458                                    + sessionId
 459                                    + ") proposal found for "
 460                                    + from
 461                                    + " to deliver proceed");
 462                    if (remoteMsgId == null) {
 463                        return;
 464                    }
 465                    final MessagePacket errorMessage = new MessagePacket();
 466                    errorMessage.setTo(from);
 467                    errorMessage.setId(remoteMsgId);
 468                    errorMessage.setType(MessagePacket.TYPE_ERROR);
 469                    final Element error = errorMessage.addChild("error");
 470                    error.setAttribute("code", "404");
 471                    error.setAttribute("type", "cancel");
 472                    error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
 473                    mXmppConnectionService.sendMessagePacket(account, errorMessage);
 474                }
 475            }
 476        } else if (addressedDirectly && "reject".equals(message.getName())) {
 477            final RtpSessionProposal proposal =
 478                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 479            synchronized (rtpSessionProposals) {
 480                if (proposal != null && rtpSessionProposals.remove(proposal) != null) {
 481                    writeLogMissedOutgoing(
 482                            account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
 483                    toneManager.transition(RtpEndUserState.DECLINED_OR_BUSY, proposal.media);
 484                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 485                            account,
 486                            proposal.with,
 487                            proposal.sessionId,
 488                            RtpEndUserState.DECLINED_OR_BUSY);
 489                } else {
 490                    Log.d(
 491                            Config.LOGTAG,
 492                            account.getJid().asBareJid()
 493                                    + ": no rtp session proposal found for "
 494                                    + from
 495                                    + " to deliver reject");
 496                }
 497            }
 498        } else if (addressedDirectly && "ringing".equals(message.getName())) {
 499            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + from + " started ringing");
 500            updateProposedSessionDiscovered(
 501                    account, from, sessionId, DeviceDiscoveryState.DISCOVERED);
 502        } else {
 503            Log.d(
 504                    Config.LOGTAG,
 505                    account.getJid()
 506                            + ": retrieved out of order jingle message from "
 507                            + from
 508                            + message
 509                            + ", addressedDirectly="
 510                            + addressedDirectly);
 511        }
 512    }
 513
 514    private RtpSessionProposal getRtpSessionProposal(
 515            final Account account, Jid from, String sessionId) {
 516        for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
 517            if (rtpSessionProposal.sessionId.equals(sessionId)
 518                    && rtpSessionProposal.with.equals(from)
 519                    && rtpSessionProposal.account.getJid().equals(account.getJid())) {
 520                return rtpSessionProposal;
 521            }
 522        }
 523        return null;
 524    }
 525
 526    private void writeLogMissedOutgoing(
 527            final Account account,
 528            Jid with,
 529            final String sessionId,
 530            String serverMsgId,
 531            long timestamp) {
 532        final Conversation conversation =
 533                mXmppConnectionService.findOrCreateConversation(
 534                        account, with.asBareJid(), false, false);
 535        final Message message =
 536                new Message(conversation, Message.STATUS_SEND, Message.TYPE_RTP_SESSION, sessionId);
 537        message.setBody(new RtpSessionStatus(false, 0).toString());
 538        message.setServerMsgId(serverMsgId);
 539        message.setTime(timestamp);
 540        writeMessage(message);
 541    }
 542
 543    private void writeLogMissedIncoming(
 544            final Account account,
 545            Jid with,
 546            final String sessionId,
 547            String serverMsgId,
 548            long timestamp) {
 549        final Conversation conversation =
 550                mXmppConnectionService.findOrCreateConversation(
 551                        account, with.asBareJid(), false, false);
 552        final Message message =
 553                new Message(
 554                        conversation, Message.STATUS_RECEIVED, Message.TYPE_RTP_SESSION, sessionId);
 555        message.setBody(new RtpSessionStatus(false, 0).toString());
 556        message.setServerMsgId(serverMsgId);
 557        message.setTime(timestamp);
 558        writeMessage(message);
 559    }
 560
 561    private void writeMessage(final Message message) {
 562        final Conversational conversational = message.getConversation();
 563        if (conversational instanceof Conversation) {
 564            ((Conversation) conversational).add(message);
 565            mXmppConnectionService.databaseBackend.createMessage(message);
 566            mXmppConnectionService.updateConversationUi();
 567        } else {
 568            throw new IllegalStateException("Somehow the conversation in a message was a stub");
 569        }
 570    }
 571
 572    public void startJingleFileTransfer(final Message message) {
 573        Preconditions.checkArgument(
 574                message.isFileOrImage(), "Message is not of type file or image");
 575        final Transferable old = message.getTransferable();
 576        if (old != null) {
 577            old.cancel();
 578        }
 579        final Account account = message.getConversation().getAccount();
 580        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
 581        final JingleFileTransferConnection connection =
 582                new JingleFileTransferConnection(this, id, account.getJid());
 583        mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
 584        this.connections.put(id, connection);
 585        connection.init(message);
 586    }
 587
 588    public Optional<OngoingRtpSession> getOngoingRtpConnection(final Contact contact) {
 589        for (final Map.Entry<AbstractJingleConnection.Id, AbstractJingleConnection> entry :
 590                this.connections.entrySet()) {
 591            if (entry.getValue() instanceof JingleRtpConnection) {
 592                final AbstractJingleConnection.Id id = entry.getKey();
 593                if (id.account == contact.getAccount()
 594                        && id.with.asBareJid().equals(contact.getJid().asBareJid())) {
 595                    return Optional.of(id);
 596                }
 597            }
 598        }
 599        synchronized (this.rtpSessionProposals) {
 600            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 601                    this.rtpSessionProposals.entrySet()) {
 602                final RtpSessionProposal proposal = entry.getKey();
 603                if (proposal.account == contact.getAccount()
 604                        && contact.getJid().asBareJid().equals(proposal.with)) {
 605                    final DeviceDiscoveryState preexistingState = entry.getValue();
 606                    if (preexistingState != null
 607                            && preexistingState != DeviceDiscoveryState.FAILED) {
 608                        return Optional.of(proposal);
 609                    }
 610                }
 611            }
 612        }
 613        return Optional.absent();
 614    }
 615
 616    void finishConnection(final AbstractJingleConnection connection) {
 617        this.connections.remove(connection.getId());
 618    }
 619
 620    void finishConnectionOrThrow(final AbstractJingleConnection connection) {
 621        final AbstractJingleConnection.Id id = connection.getId();
 622        if (this.connections.remove(id) == null) {
 623            throw new IllegalStateException(
 624                    String.format("Unable to finish connection with id=%s", id.toString()));
 625        }
 626    }
 627
 628    public boolean fireJingleRtpConnectionStateUpdates() {
 629        boolean firedUpdates = false;
 630        for (final AbstractJingleConnection connection : this.connections.values()) {
 631            if (connection instanceof JingleRtpConnection) {
 632                final JingleRtpConnection jingleRtpConnection = (JingleRtpConnection) connection;
 633                if (jingleRtpConnection.isTerminated()) {
 634                    continue;
 635                }
 636                jingleRtpConnection.fireStateUpdate();
 637                firedUpdates = true;
 638            }
 639        }
 640        return firedUpdates;
 641    }
 642
 643    void getPrimaryCandidate(
 644            final Account account,
 645            final boolean initiator,
 646            final OnPrimaryCandidateFound listener) {
 647        if (Config.DISABLE_PROXY_LOOKUP) {
 648            listener.onPrimaryCandidateFound(false, null);
 649            return;
 650        }
 651        if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
 652            final Jid proxy =
 653                    account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
 654            if (proxy != null) {
 655                IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
 656                iq.setTo(proxy);
 657                iq.query(Namespace.BYTE_STREAMS);
 658                account.getXmppConnection()
 659                        .sendIqPacket(
 660                                iq,
 661                                new OnIqPacketReceived() {
 662
 663                                    @Override
 664                                    public void onIqPacketReceived(
 665                                            Account account, IqPacket packet) {
 666                                        final Element streamhost =
 667                                                packet.query()
 668                                                        .findChild(
 669                                                                "streamhost",
 670                                                                Namespace.BYTE_STREAMS);
 671                                        final String host =
 672                                                streamhost == null
 673                                                        ? null
 674                                                        : streamhost.getAttribute("host");
 675                                        final String port =
 676                                                streamhost == null
 677                                                        ? null
 678                                                        : streamhost.getAttribute("port");
 679                                        if (host != null && port != null) {
 680                                            try {
 681                                                JingleCandidate candidate =
 682                                                        new JingleCandidate(nextRandomId(), true);
 683                                                candidate.setHost(host);
 684                                                candidate.setPort(Integer.parseInt(port));
 685                                                candidate.setType(JingleCandidate.TYPE_PROXY);
 686                                                candidate.setJid(proxy);
 687                                                candidate.setPriority(
 688                                                        655360 + (initiator ? 30 : 0));
 689                                                primaryCandidates.put(
 690                                                        account.getJid().asBareJid(), candidate);
 691                                                listener.onPrimaryCandidateFound(true, candidate);
 692                                            } catch (final NumberFormatException e) {
 693                                                listener.onPrimaryCandidateFound(false, null);
 694                                            }
 695                                        } else {
 696                                            listener.onPrimaryCandidateFound(false, null);
 697                                        }
 698                                    }
 699                                });
 700            } else {
 701                listener.onPrimaryCandidateFound(false, null);
 702            }
 703
 704        } else {
 705            listener.onPrimaryCandidateFound(
 706                    true, this.primaryCandidates.get(account.getJid().asBareJid()));
 707        }
 708    }
 709
 710    public void retractSessionProposal(final Account account, final Jid with) {
 711        synchronized (this.rtpSessionProposals) {
 712            RtpSessionProposal matchingProposal = null;
 713            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
 714                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 715                    matchingProposal = proposal;
 716                    break;
 717                }
 718            }
 719            if (matchingProposal != null) {
 720                retractSessionProposal(matchingProposal);
 721            }
 722        }
 723    }
 724
 725    private void retractSessionProposal(RtpSessionProposal rtpSessionProposal) {
 726        final Account account = rtpSessionProposal.account;
 727        toneManager.transition(RtpEndUserState.ENDED, rtpSessionProposal.media);
 728        Log.d(
 729                Config.LOGTAG,
 730                account.getJid().asBareJid()
 731                        + ": retracting rtp session proposal with "
 732                        + rtpSessionProposal.with);
 733        this.rtpSessionProposals.remove(rtpSessionProposal);
 734        final MessagePacket messagePacket =
 735                mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
 736        writeLogMissedOutgoing(
 737                account,
 738                rtpSessionProposal.with,
 739                rtpSessionProposal.sessionId,
 740                null,
 741                System.currentTimeMillis());
 742        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 743    }
 744
 745    public String initializeRtpSession(
 746            final Account account, final Jid with, final Set<Media> media) {
 747        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
 748        final JingleRtpConnection rtpConnection =
 749                new JingleRtpConnection(this, id, account.getJid());
 750        rtpConnection.setProposedMedia(media);
 751        this.connections.put(id, rtpConnection);
 752        rtpConnection.sendSessionInitiate();
 753        return id.sessionId;
 754    }
 755
 756    public void proposeJingleRtpSession(
 757            final Account account, final Jid with, final Set<Media> media) {
 758        synchronized (this.rtpSessionProposals) {
 759            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 760                    this.rtpSessionProposals.entrySet()) {
 761                RtpSessionProposal proposal = entry.getKey();
 762                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 763                    final DeviceDiscoveryState preexistingState = entry.getValue();
 764                    if (preexistingState != null
 765                            && preexistingState != DeviceDiscoveryState.FAILED) {
 766                        final RtpEndUserState endUserState = preexistingState.toEndUserState();
 767                        toneManager.transition(endUserState, media);
 768                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 769                                account, with, proposal.sessionId, endUserState);
 770                        return;
 771                    }
 772                }
 773            }
 774            if (isBusy()) {
 775                if (hasMatchingRtpSession(account, with, media)) {
 776                    Log.d(
 777                            Config.LOGTAG,
 778                            "ignoring request to propose jingle session because the other party already created one for us");
 779                    return;
 780                }
 781                throw new IllegalStateException(
 782                        "There is already a running RTP session. This should have been caught by the UI");
 783            }
 784            final RtpSessionProposal proposal =
 785                    RtpSessionProposal.of(account, with.asBareJid(), media);
 786            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
 787            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 788                    account, proposal.with, proposal.sessionId, RtpEndUserState.FINDING_DEVICE);
 789            final MessagePacket messagePacket =
 790                    mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 791            mXmppConnectionService.sendMessagePacket(account, messagePacket);
 792        }
 793    }
 794
 795    public boolean hasMatchingProposal(final Account account, final Jid with) {
 796        synchronized (this.rtpSessionProposals) {
 797            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 798                    this.rtpSessionProposals.entrySet()) {
 799                final RtpSessionProposal proposal = entry.getKey();
 800                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 801                    return true;
 802                }
 803            }
 804        }
 805        return false;
 806    }
 807
 808    public void deliverIbbPacket(Account account, IqPacket packet) {
 809        final String sid;
 810        final Element payload;
 811        if (packet.hasChild("open", Namespace.IBB)) {
 812            payload = packet.findChild("open", Namespace.IBB);
 813            sid = payload.getAttribute("sid");
 814        } else if (packet.hasChild("data", Namespace.IBB)) {
 815            payload = packet.findChild("data", Namespace.IBB);
 816            sid = payload.getAttribute("sid");
 817        } else if (packet.hasChild("close", Namespace.IBB)) {
 818            payload = packet.findChild("close", Namespace.IBB);
 819            sid = payload.getAttribute("sid");
 820        } else {
 821            payload = null;
 822            sid = null;
 823        }
 824        if (sid != null) {
 825            for (final AbstractJingleConnection connection : this.connections.values()) {
 826                if (connection instanceof JingleFileTransferConnection) {
 827                    final JingleFileTransferConnection fileTransfer =
 828                            (JingleFileTransferConnection) connection;
 829                    final JingleTransport transport = fileTransfer.getTransport();
 830                    if (transport instanceof JingleInBandTransport) {
 831                        final JingleInBandTransport inBandTransport =
 832                                (JingleInBandTransport) transport;
 833                        if (inBandTransport.matches(account, sid)) {
 834                            inBandTransport.deliverPayload(packet, payload);
 835                        }
 836                        return;
 837                    }
 838                }
 839            }
 840        }
 841        Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
 842        account.getXmppConnection()
 843                .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 844    }
 845
 846    public void notifyRebound(final Account account) {
 847        for (final AbstractJingleConnection connection : this.connections.values()) {
 848            connection.notifyRebound();
 849        }
 850        final XmppConnection xmppConnection = account.getXmppConnection();
 851        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 852            resendSessionProposals(account);
 853        }
 854    }
 855
 856    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 857            Account account, Jid with, String sessionId) {
 858        final AbstractJingleConnection.Id id =
 859                AbstractJingleConnection.Id.of(account, with, sessionId);
 860        final AbstractJingleConnection connection = connections.get(id);
 861        if (connection instanceof JingleRtpConnection) {
 862            return new WeakReference<>((JingleRtpConnection) connection);
 863        }
 864        return null;
 865    }
 866
 867    private void resendSessionProposals(final Account account) {
 868        synchronized (this.rtpSessionProposals) {
 869            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 870                    this.rtpSessionProposals.entrySet()) {
 871                final RtpSessionProposal proposal = entry.getKey();
 872                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 873                        && proposal.account == account) {
 874                    Log.d(
 875                            Config.LOGTAG,
 876                            account.getJid().asBareJid()
 877                                    + ": resending session proposal to "
 878                                    + proposal.with);
 879                    final MessagePacket messagePacket =
 880                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 881                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
 882                }
 883            }
 884        }
 885    }
 886
 887    public void updateProposedSessionDiscovered(
 888            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
 889        synchronized (this.rtpSessionProposals) {
 890            final RtpSessionProposal sessionProposal =
 891                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 892            final DeviceDiscoveryState currentState =
 893                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
 894            if (currentState == null) {
 895                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
 896                return;
 897            }
 898            if (currentState == DeviceDiscoveryState.DISCOVERED) {
 899                Log.d(
 900                        Config.LOGTAG,
 901                        "session proposal already at discovered. not going to fall back");
 902                return;
 903            }
 904            this.rtpSessionProposals.put(sessionProposal, target);
 905            final RtpEndUserState endUserState = target.toEndUserState();
 906            toneManager.transition(endUserState, sessionProposal.media);
 907            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 908                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
 909            Log.d(
 910                    Config.LOGTAG,
 911                    account.getJid().asBareJid()
 912                            + ": flagging session "
 913                            + sessionId
 914                            + " as "
 915                            + target);
 916        }
 917    }
 918
 919    public void rejectRtpSession(final String sessionId) {
 920        for (final AbstractJingleConnection connection : this.connections.values()) {
 921            if (connection.getId().sessionId.equals(sessionId)) {
 922                if (connection instanceof JingleRtpConnection) {
 923                    try {
 924                        ((JingleRtpConnection) connection).rejectCall();
 925                        return;
 926                    } catch (final IllegalStateException e) {
 927                        Log.w(
 928                                Config.LOGTAG,
 929                                "race condition on rejecting call from notification",
 930                                e);
 931                    }
 932                }
 933            }
 934        }
 935    }
 936
 937    public void endRtpSession(final String sessionId) {
 938        for (final AbstractJingleConnection connection : this.connections.values()) {
 939            if (connection.getId().sessionId.equals(sessionId)) {
 940                if (connection instanceof JingleRtpConnection) {
 941                    ((JingleRtpConnection) connection).endCall();
 942                }
 943            }
 944        }
 945    }
 946
 947    public void failProceed(Account account, final Jid with, final String sessionId, final String message) {
 948        final AbstractJingleConnection.Id id =
 949                AbstractJingleConnection.Id.of(account, with, sessionId);
 950        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 951        if (existingJingleConnection instanceof JingleRtpConnection) {
 952            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
 953        }
 954    }
 955
 956    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
 957        if (connections.containsValue(connection)) {
 958            return;
 959        }
 960        final IllegalStateException e =
 961                new IllegalStateException(
 962                        "JingleConnection has not been registered with connection manager");
 963        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
 964        throw e;
 965    }
 966
 967    void setTerminalSessionState(
 968            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
 969        this.terminatedSessions.put(
 970                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
 971    }
 972
 973    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
 974        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
 975    }
 976
 977    private static class PersistableSessionId {
 978        private final Jid with;
 979        private final String sessionId;
 980
 981        private PersistableSessionId(Jid with, String sessionId) {
 982            this.with = with;
 983            this.sessionId = sessionId;
 984        }
 985
 986        public static PersistableSessionId of(AbstractJingleConnection.Id id) {
 987            return new PersistableSessionId(id.with, id.sessionId);
 988        }
 989
 990        @Override
 991        public boolean equals(Object o) {
 992            if (this == o) return true;
 993            if (o == null || getClass() != o.getClass()) return false;
 994            PersistableSessionId that = (PersistableSessionId) o;
 995            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
 996        }
 997
 998        @Override
 999        public int hashCode() {
1000            return Objects.hashCode(with, sessionId);
1001        }
1002    }
1003
1004    public static class TerminatedRtpSession {
1005        public final RtpEndUserState state;
1006        public final Set<Media> media;
1007
1008        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
1009            this.state = state;
1010            this.media = media;
1011        }
1012    }
1013
1014    public enum DeviceDiscoveryState {
1015        SEARCHING,
1016        SEARCHING_ACKNOWLEDGED,
1017        DISCOVERED,
1018        FAILED;
1019
1020        public RtpEndUserState toEndUserState() {
1021            switch (this) {
1022                case SEARCHING:
1023                case SEARCHING_ACKNOWLEDGED:
1024                    return RtpEndUserState.FINDING_DEVICE;
1025                case DISCOVERED:
1026                    return RtpEndUserState.RINGING;
1027                default:
1028                    return RtpEndUserState.CONNECTIVITY_ERROR;
1029            }
1030        }
1031    }
1032
1033    public static class RtpSessionProposal implements OngoingRtpSession {
1034        public final Jid with;
1035        public final String sessionId;
1036        public final Set<Media> media;
1037        private final Account account;
1038
1039        private RtpSessionProposal(Account account, Jid with, String sessionId) {
1040            this(account, with, sessionId, Collections.emptySet());
1041        }
1042
1043        private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
1044            this.account = account;
1045            this.with = with;
1046            this.sessionId = sessionId;
1047            this.media = media;
1048        }
1049
1050        public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
1051            return new RtpSessionProposal(account, with, nextRandomId(), media);
1052        }
1053
1054        @Override
1055        public boolean equals(Object o) {
1056            if (this == o) return true;
1057            if (o == null || getClass() != o.getClass()) return false;
1058            RtpSessionProposal proposal = (RtpSessionProposal) o;
1059            return Objects.equal(account.getJid(), proposal.account.getJid())
1060                    && Objects.equal(with, proposal.with)
1061                    && Objects.equal(sessionId, proposal.sessionId);
1062        }
1063
1064        @Override
1065        public int hashCode() {
1066            return Objects.hashCode(account.getJid(), with, sessionId);
1067        }
1068
1069        @Override
1070        public Account getAccount() {
1071            return account;
1072        }
1073
1074        @Override
1075        public Jid getWith() {
1076            return with;
1077        }
1078
1079        @Override
1080        public String getSessionId() {
1081            return sessionId;
1082        }
1083    }
1084}