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