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