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