JingleConnectionManager.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.telecom.TelecomManager;
   4import android.telecom.VideoProfile;
   5import android.util.Base64;
   6import android.util.Log;
   7
   8import androidx.annotation.Nullable;
   9
  10import com.google.common.base.Objects;
  11import com.google.common.base.Optional;
  12import com.google.common.base.Preconditions;
  13import com.google.common.cache.Cache;
  14import com.google.common.cache.CacheBuilder;
  15import com.google.common.collect.Collections2;
  16import com.google.common.collect.ComparisonChain;
  17import com.google.common.collect.ImmutableSet;
  18
  19import eu.siacs.conversations.R;
  20import eu.siacs.conversations.Config;
  21import eu.siacs.conversations.entities.Account;
  22import eu.siacs.conversations.entities.Contact;
  23import eu.siacs.conversations.entities.Conversation;
  24import eu.siacs.conversations.entities.Conversational;
  25import eu.siacs.conversations.entities.Message;
  26import eu.siacs.conversations.entities.RtpSessionStatus;
  27import eu.siacs.conversations.entities.Transferable;
  28import eu.siacs.conversations.services.AbstractConnectionManager;
  29import eu.siacs.conversations.services.CallIntegration;
  30import eu.siacs.conversations.services.CallIntegrationConnectionService;
  31import eu.siacs.conversations.services.XmppConnectionService;
  32import eu.siacs.conversations.xml.Element;
  33import eu.siacs.conversations.xml.Namespace;
  34import eu.siacs.conversations.xmpp.Jid;
  35import eu.siacs.conversations.xmpp.XmppConnection;
  36import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  37import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
  38import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
  39import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  40import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
  41import eu.siacs.conversations.xmpp.jingle.transports.InbandBytestreamsTransport;
  42import eu.siacs.conversations.xmpp.jingle.transports.Transport;
  43
  44import im.conversations.android.xmpp.model.jingle.Jingle;
  45import im.conversations.android.xmpp.model.stanza.Iq;
  46
  47import java.lang.ref.WeakReference;
  48import java.security.SecureRandom;
  49import java.util.Arrays;
  50import java.util.Collection;
  51import java.util.HashMap;
  52import java.util.List;
  53import java.util.Map;
  54import java.util.Set;
  55import java.util.concurrent.ConcurrentHashMap;
  56import java.util.concurrent.Executors;
  57import java.util.concurrent.ScheduledExecutorService;
  58import java.util.concurrent.ScheduledFuture;
  59import java.util.concurrent.TimeUnit;
  60
  61public class JingleConnectionManager extends AbstractConnectionManager {
  62    public static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
  63            Executors.newSingleThreadScheduledExecutor();
  64    private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals =
  65            new HashMap<>();
  66    private final ConcurrentHashMap<AbstractJingleConnection.Id, AbstractJingleConnection>
  67            connections = new ConcurrentHashMap<>();
  68
  69    private final Cache<PersistableSessionId, TerminatedRtpSession> terminatedSessions =
  70            CacheBuilder.newBuilder().expireAfterWrite(24, TimeUnit.HOURS).build();
  71
  72    public JingleConnectionManager(XmppConnectionService service) {
  73        super(service);
  74    }
  75
  76    static String nextRandomId() {
  77        final byte[] id = new byte[16];
  78        new SecureRandom().nextBytes(id);
  79        return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
  80    }
  81
  82    public void deliverPacket(final Account account, final Iq packet) {
  83        final var jingle = packet.getExtension(Jingle.class);
  84        Preconditions.checkNotNull(
  85                jingle, "Passed iq packet w/o jingle extension to Connection Manager");
  86        final String sessionId = jingle.getSessionId();
  87        final Jingle.Action action = jingle.getAction();
  88        if (sessionId == null) {
  89            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
  90            return;
  91        }
  92        if (action == null) {
  93            respondWithJingleError(account, packet, null, "bad-request", "cancel");
  94            return;
  95        }
  96        final AbstractJingleConnection.Id id =
  97                AbstractJingleConnection.Id.of(account, packet, jingle);
  98        final AbstractJingleConnection existingJingleConnection = connections.get(id);
  99        if (existingJingleConnection != null) {
 100            existingJingleConnection.deliverPacket(packet);
 101        } else if (action == Jingle.Action.SESSION_INITIATE) {
 102            final Jid from = packet.getFrom();
 103            final Content content = jingle.getJingleContent();
 104            final String descriptionNamespace =
 105                    content == null ? null : content.getDescriptionNamespace();
 106            final AbstractJingleConnection connection;
 107            if (Namespace.JINGLE_APPS_FILE_TRANSFER.equals(descriptionNamespace)) {
 108                connection = new JingleFileTransferConnection(this, id, from);
 109            } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace)
 110                    && isUsingClearNet(account)) {
 111                final boolean sessionEnded =
 112                        this.terminatedSessions.asMap().containsKey(PersistableSessionId.of(id));
 113                final boolean stranger =
 114                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 115                final boolean busy = isBusy();
 116                if (busy || sessionEnded || stranger) {
 117                    Log.d(
 118                            Config.LOGTAG,
 119                            id.account.getJid().asBareJid()
 120                                    + ": rejected session with "
 121                                    + id.with
 122                                    + " because busy. sessionEnded="
 123                                    + sessionEnded
 124                                    + ", stranger="
 125                                    + stranger);
 126                    sendSessionTerminate(account, packet, id);
 127                    if (busy || stranger) {
 128                        writeLogMissedIncoming(
 129                                account,
 130                                id.with,
 131                                id.sessionId,
 132                                null,
 133                                System.currentTimeMillis(),
 134                                stranger);
 135                    }
 136                    return;
 137                }
 138                connection = new JingleRtpConnection(this, id, from);
 139            } else {
 140                respondWithJingleError(
 141                        account, packet, "unsupported-info", "feature-not-implemented", "cancel");
 142                return;
 143            }
 144            connections.put(id, connection);
 145            mXmppConnectionService.updateConversationUi();
 146            connection.deliverPacket(packet);
 147            if (connection instanceof JingleRtpConnection rtpConnection) {
 148                addNewIncomingCall(rtpConnection);
 149            }
 150        } else {
 151            Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
 152            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
 153        }
 154    }
 155
 156    private void addNewIncomingCall(final JingleRtpConnection rtpConnection) {
 157        if (true) {
 158            return; // We do this inside the startRinging in the rtpConnection now so that fallback is possible
 159        }
 160        if (rtpConnection.isTerminated()) {
 161            Log.d(
 162                    Config.LOGTAG,
 163                    "skip call integration because something must have gone during initiate");
 164            return;
 165        }
 166        if (CallIntegrationConnectionService.addNewIncomingCall(
 167                mXmppConnectionService, rtpConnection.getId())) {
 168            return;
 169        }
 170        rtpConnection.integrationFailure();
 171    }
 172
 173    private void sendSessionTerminate(
 174            final Account account, final Iq request, final AbstractJingleConnection.Id id) {
 175        mXmppConnectionService.sendIqPacket(
 176                account, request.generateResponse(Iq.Type.RESULT), null);
 177        final var iq = new Iq(Iq.Type.SET);
 178        iq.setTo(id.with);
 179        final var sessionTermination =
 180                iq.addExtension(new Jingle(Jingle.Action.SESSION_TERMINATE, id.sessionId));
 181        sessionTermination.setReason(Reason.BUSY, null);
 182        mXmppConnectionService.sendIqPacket(account, iq, null);
 183    }
 184
 185    private boolean isUsingClearNet(final Account account) {
 186        return !account.isOnion() && !mXmppConnectionService.useTorToConnect();
 187    }
 188
 189    public boolean isBusy() {
 190        for (final AbstractJingleConnection connection : this.connections.values()) {
 191            if (connection instanceof JingleRtpConnection rtpConnection) {
 192                if (connection.isTerminated() && rtpConnection.getCallIntegration().isDestroyed()) {
 193                    continue;
 194                }
 195                return true;
 196            }
 197        }
 198        synchronized (this.rtpSessionProposals) {
 199            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED)) return true;
 200            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING)) return true;
 201            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED)) return true;
 202            return false;
 203        }
 204    }
 205
 206    public boolean hasJingleRtpConnection(final Account account) {
 207        for (AbstractJingleConnection connection : this.connections.values()) {
 208            if (connection instanceof JingleRtpConnection rtpConnection) {
 209                if (rtpConnection.isTerminated()) {
 210                    continue;
 211                }
 212                if (rtpConnection.id.account == account) {
 213                    return true;
 214                }
 215            }
 216        }
 217        return false;
 218    }
 219
 220    private Optional<RtpSessionProposal> findMatchingSessionProposal(
 221            final Account account, final Jid with, final Set<Media> media) {
 222        synchronized (this.rtpSessionProposals) {
 223            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 224                    this.rtpSessionProposals.entrySet()) {
 225                final RtpSessionProposal proposal = entry.getKey();
 226                final DeviceDiscoveryState state = entry.getValue();
 227                final boolean openProposal =
 228                        state == DeviceDiscoveryState.DISCOVERED
 229                                || state == DeviceDiscoveryState.SEARCHING
 230                                || state == DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED;
 231                if (openProposal
 232                        && proposal.account == account
 233                        && proposal.with.equals(with.asBareJid())
 234                        && proposal.media.equals(media)) {
 235                    return Optional.of(proposal);
 236                }
 237            }
 238        }
 239        return Optional.absent();
 240    }
 241
 242    private String hasMatchingRtpSession(final Account account, final Jid with, final Set<Media> media) {
 243        for (AbstractJingleConnection connection : this.connections.values()) {
 244            if (connection instanceof JingleRtpConnection rtpConnection) {
 245                if (rtpConnection.isTerminated()) {
 246                    continue;
 247                }
 248                if (rtpConnection.getId().account == account
 249                        && rtpConnection.getId().with.asBareJid().equals(with.asBareJid())
 250                        && rtpConnection.getMedia().equals(media)) {
 251                    return rtpConnection.getId().sessionId;
 252                }
 253            }
 254        }
 255        return null;
 256    }
 257
 258    private boolean isWithStrangerAndStrangerNotificationsAreOff(final Account account, Jid with) {
 259        final var chatRequestsPref = mXmppConnectionService.getStringPreference("chat_requests", R.string.default_chat_requests);
 260        final var conversation = mXmppConnectionService.findOrCreateConversation(account, with, false, true);
 261        return conversation.isChatRequest(chatRequestsPref);
 262    }
 263
 264    ScheduledFuture<?> schedule(
 265            final Runnable runnable, final long delay, final TimeUnit timeUnit) {
 266        return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, timeUnit);
 267    }
 268
 269    void respondWithJingleError(
 270            final Account account,
 271            final Iq original,
 272            final String jingleCondition,
 273            final String condition,
 274            final String conditionType) {
 275        final Iq response = original.generateResponse(Iq.Type.ERROR);
 276        final Element error = response.addChild("error");
 277        error.setAttribute("type", conditionType);
 278        error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
 279        if (jingleCondition != null) {
 280            error.addChild(jingleCondition, Namespace.JINGLE_ERRORS);
 281        }
 282        account.getXmppConnection().sendIqPacket(response, null);
 283    }
 284
 285    public void deliverMessage(
 286            final Account account,
 287            final Jid to,
 288            final Jid from,
 289            final Element message,
 290            String remoteMsgId,
 291            String serverMsgId,
 292            long timestamp) {
 293        Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
 294        final String sessionId = message.getAttribute("id");
 295        if (sessionId == null) {
 296            return;
 297        }
 298        if ("accept".equals(message.getName()) || "reject".equals(message.getName())) {
 299            for (AbstractJingleConnection connection : connections.values()) {
 300                if (connection instanceof JingleRtpConnection rtpConnection) {
 301                    final AbstractJingleConnection.Id id = connection.getId();
 302                    if (id.account == account && id.sessionId.equals(sessionId)) {
 303                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 304                        return;
 305                    }
 306                }
 307            }
 308            if ("accept".equals(message.getName())) return;
 309        }
 310        final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
 311        // XEP version 0.6.0 sends proceed, reject, ringing to bare jid
 312        final boolean addressedDirectly = to != null && to.equals(account.getJid());
 313        final AbstractJingleConnection.Id id;
 314        if (fromSelf) {
 315            if (to != null && to.isFullJid()) {
 316                id = AbstractJingleConnection.Id.of(account, to, sessionId);
 317            } else {
 318                return;
 319            }
 320        } else {
 321            id = AbstractJingleConnection.Id.of(account, from, sessionId);
 322        }
 323        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 324        if (existingJingleConnection != null) {
 325            if (existingJingleConnection instanceof JingleRtpConnection) {
 326                ((JingleRtpConnection) existingJingleConnection)
 327                        .deliveryMessage(from, message, serverMsgId, timestamp);
 328            } else {
 329                Log.d(
 330                        Config.LOGTAG,
 331                        account.getJid().asBareJid()
 332                                + ": "
 333                                + existingJingleConnection.getClass().getName()
 334                                + " does not support jingle messages");
 335            }
 336            return;
 337        }
 338
 339        if (fromSelf) {
 340            if ("proceed".equals(message.getName())) {
 341                final Conversation c =
 342                        mXmppConnectionService.findOrCreateConversation(
 343                                account, id.with, false, false);
 344                final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
 345                if (previousBusy != null) {
 346                    previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
 347                    if (serverMsgId != null) {
 348                        previousBusy.setServerMsgId(serverMsgId);
 349                    }
 350                    previousBusy.setTime(timestamp);
 351                    mXmppConnectionService.updateMessage(previousBusy, true);
 352                    Log.d(
 353                            Config.LOGTAG,
 354                            id.account.getJid().asBareJid()
 355                                    + ": updated previous busy because call got picked up by another device");
 356                    mXmppConnectionService.getNotificationService().clearMissedCall(previousBusy);
 357                    return;
 358                }
 359            }
 360            // TODO handle reject for cases where we don’t have carbon copies (normally reject is to
 361            // be sent to own bare jid as well)
 362            Log.d(
 363                    Config.LOGTAG,
 364                    account.getJid().asBareJid() + ": ignore jingle message from self");
 365            return;
 366        }
 367
 368        if ("propose".equals(message.getName())) {
 369            final Propose propose = Propose.upgrade(message);
 370            final List<GenericDescription> descriptions = propose.getDescriptions();
 371            final Collection<RtpDescription> rtpDescriptions =
 372                    Collections2.transform(
 373                            Collections2.filter(descriptions, d -> d instanceof RtpDescription),
 374                            input -> (RtpDescription) input);
 375            if (rtpDescriptions.size() > 0
 376                    && rtpDescriptions.size() == descriptions.size()
 377                    && isUsingClearNet(account)) {
 378                final Collection<Media> media =
 379                        Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
 380                if (media.contains(Media.UNKNOWN)) {
 381                    Log.d(
 382                            Config.LOGTAG,
 383                            account.getJid().asBareJid()
 384                                    + ": encountered unknown media in session proposal. "
 385                                    + propose);
 386                    return;
 387                }
 388                final Optional<RtpSessionProposal> matchingSessionProposal =
 389                        findMatchingSessionProposal(account, id.with, ImmutableSet.copyOf(media));
 390                if (matchingSessionProposal.isPresent()) {
 391                    final String ourSessionId = matchingSessionProposal.get().sessionId;
 392                    final String theirSessionId = id.sessionId;
 393                    if (ComparisonChain.start()
 394                                    .compare(ourSessionId, theirSessionId)
 395                                    .compare(
 396                                            account.getJid().toEscapedString(),
 397                                            id.with.toEscapedString())
 398                                    .result()
 399                            > 0) {
 400                        Log.d(
 401                                Config.LOGTAG,
 402                                account.getJid().asBareJid()
 403                                        + ": our session lost tie break. automatically accepting their session. winning Session="
 404                                        + theirSessionId);
 405                        // TODO a retract for this reason should probably include some indication of
 406                        // tie break
 407                        retractSessionProposal(matchingSessionProposal.get());
 408                        final JingleRtpConnection rtpConnection =
 409                                new JingleRtpConnection(this, id, from);
 410                        this.connections.put(id, rtpConnection);
 411                        rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 412                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 413                        addNewIncomingCall(rtpConnection);
 414                        // TODO actually do the automatic accept?!
 415                    } else {
 416                        Log.d(
 417                                Config.LOGTAG,
 418                                account.getJid().asBareJid()
 419                                        + ": our session won tie break. waiting for other party to accept. winningSession="
 420                                        + ourSessionId);
 421                        // TODO reject their session with <tie-break/>?
 422                    }
 423                    return;
 424                }
 425                final boolean stranger =
 426                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 427                if (isBusy() || stranger) {
 428                    writeLogMissedIncoming(
 429                            account,
 430                            id.with.asBareJid(),
 431                            id.sessionId,
 432                            serverMsgId,
 433                            timestamp,
 434                            stranger);
 435                    if (stranger) {
 436                        Log.d(
 437                                Config.LOGTAG,
 438                                id.account.getJid().asBareJid()
 439                                        + ": ignoring call proposal from stranger "
 440                                        + id.with);
 441                        return;
 442                    }
 443                    final int activeDevices = account.activeDevicesWithRtpCapability();
 444                    Log.d(Config.LOGTAG, "active devices with rtp capability: " + activeDevices);
 445                    if (activeDevices == 0) {
 446                        final var reject =
 447                                mXmppConnectionService
 448                                        .getMessageGenerator()
 449                                        .sessionReject(from, sessionId);
 450                        mXmppConnectionService.sendMessagePacket(account, reject);
 451                    } else {
 452                        Log.d(
 453                                Config.LOGTAG,
 454                                id.account.getJid().asBareJid()
 455                                        + ": ignoring proposal because busy on this device but there are other devices");
 456                    }
 457                } else {
 458                    final JingleRtpConnection rtpConnection =
 459                            new JingleRtpConnection(this, id, from);
 460                    this.connections.put(id, rtpConnection);
 461                    rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 462                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 463                    addNewIncomingCall(rtpConnection);
 464                }
 465            } else {
 466                Log.d(
 467                        Config.LOGTAG,
 468                        account.getJid().asBareJid()
 469                                + ": unable to react to proposed session with "
 470                                + rtpDescriptions.size()
 471                                + " rtp descriptions of "
 472                                + descriptions.size()
 473                                + " total descriptions");
 474            }
 475        } else if (addressedDirectly && "proceed".equals(message.getName())) {
 476            synchronized (rtpSessionProposals) {
 477                final RtpSessionProposal proposal =
 478                        getRtpSessionProposal(account, from.asBareJid(), sessionId);
 479                if (proposal != null) {
 480                    rtpSessionProposals.remove(proposal);
 481                    final JingleRtpConnection rtpConnection =
 482                            new JingleRtpConnection(
 483                                    this, id, account.getJid(), proposal.callIntegration);
 484                    rtpConnection.setProposedMedia(proposal.media);
 485                    this.connections.put(id, rtpConnection);
 486                    rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
 487                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 488                } else {
 489                    Log.d(
 490                            Config.LOGTAG,
 491                            account.getJid().asBareJid()
 492                                    + ": no rtp session ("
 493                                    + sessionId
 494                                    + ") proposal found for "
 495                                    + from
 496                                    + " to deliver proceed");
 497                    if (remoteMsgId == null) {
 498                        return;
 499                    }
 500                    final var errorMessage =
 501                            new im.conversations.android.xmpp.model.stanza.Message();
 502                    errorMessage.setTo(from);
 503                    errorMessage.setId(remoteMsgId);
 504                    errorMessage.setType(
 505                            im.conversations.android.xmpp.model.stanza.Message.Type.ERROR);
 506                    final Element error = errorMessage.addChild("error");
 507                    error.setAttribute("code", "404");
 508                    error.setAttribute("type", "cancel");
 509                    error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
 510                    mXmppConnectionService.sendMessagePacket(account, errorMessage);
 511                }
 512            }
 513        } else if (addressedDirectly && "reject".equals(message.getName())) {
 514            final RtpSessionProposal proposal =
 515                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 516            synchronized (rtpSessionProposals) {
 517                if (proposal != null) {
 518                    setTerminalSessionState(proposal, RtpEndUserState.DECLINED_OR_BUSY);
 519                    rtpSessionProposals.remove(proposal);
 520                    proposal.callIntegration.busy();
 521                    writeLogMissedOutgoing(
 522                            account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
 523                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 524                            account,
 525                            proposal.with,
 526                            proposal.sessionId,
 527                            RtpEndUserState.DECLINED_OR_BUSY);
 528                } else {
 529                    Log.d(
 530                            Config.LOGTAG,
 531                            account.getJid().asBareJid()
 532                                    + ": no rtp session proposal found for "
 533                                    + from
 534                                    + " to deliver reject");
 535                }
 536            }
 537        } else if (addressedDirectly && "ringing".equals(message.getName())) {
 538            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + from + " started ringing");
 539            updateProposedSessionDiscovered(
 540                    account, from, sessionId, DeviceDiscoveryState.DISCOVERED);
 541        } else {
 542            Log.d(
 543                    Config.LOGTAG,
 544                    account.getJid()
 545                            + ": received out of order jingle message from="
 546                            + from
 547                            + ", message="
 548                            + message
 549                            + ", addressedDirectly="
 550                            + addressedDirectly);
 551        }
 552    }
 553
 554    private RtpSessionProposal getRtpSessionProposal(
 555            final Account account, Jid from, String sessionId) {
 556        for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
 557            if (rtpSessionProposal.sessionId.equals(sessionId)
 558                    && rtpSessionProposal.with.equals(from)
 559                    && rtpSessionProposal.account.getJid().equals(account.getJid())) {
 560                return rtpSessionProposal;
 561            }
 562        }
 563        return null;
 564    }
 565
 566    private void writeLogMissedOutgoing(
 567            final Account account,
 568            Jid with,
 569            final String sessionId,
 570            String serverMsgId,
 571            long timestamp) {
 572        final Conversation conversation =
 573                mXmppConnectionService.findOrCreateConversation(
 574                        account, with.asBareJid(), false, false);
 575        final Message message =
 576                new Message(conversation, Message.STATUS_SEND, Message.TYPE_RTP_SESSION, sessionId);
 577        message.setBody(new RtpSessionStatus(false, 0).toString());
 578        message.setServerMsgId(serverMsgId);
 579        message.setTime(timestamp);
 580        writeMessage(message);
 581    }
 582
 583    private void writeLogMissedIncoming(
 584            final Account account,
 585            final Jid with,
 586            final String sessionId,
 587            final String serverMsgId,
 588            final long timestamp,
 589            final boolean stranger) {
 590        final Conversation conversation =
 591                mXmppConnectionService.findOrCreateConversation(
 592                        account, with.asBareJid(), false, false);
 593        final Message message =
 594                new Message(
 595                        conversation, Message.STATUS_RECEIVED, Message.TYPE_RTP_SESSION, sessionId);
 596        message.setBody(new RtpSessionStatus(false, 0).toString());
 597        message.setServerMsgId(serverMsgId);
 598        message.setTime(timestamp);
 599        message.setCounterpart(with);
 600        writeMessage(message);
 601        if (stranger) {
 602            return;
 603        }
 604        mXmppConnectionService.getNotificationService().pushMissedCallNow(message);
 605    }
 606
 607    private void writeMessage(final Message message) {
 608        final Conversational conversational = message.getConversation();
 609        if (conversational instanceof Conversation) {
 610            ((Conversation) conversational).add(message);
 611            mXmppConnectionService.databaseBackend.createMessage(message);
 612            mXmppConnectionService.updateConversationUi();
 613        } else {
 614            throw new IllegalStateException("Somehow the conversation in a message was a stub");
 615        }
 616    }
 617
 618    public void startJingleFileTransfer(final Message message) {
 619        Preconditions.checkArgument(
 620                message.isFileOrImage(), "Message is not of type file or image");
 621        final Transferable old = message.getTransferable();
 622        if (old != null) {
 623            old.cancel();
 624        }
 625        final JingleFileTransferConnection connection =
 626                new JingleFileTransferConnection(this, message);
 627        this.connections.put(connection.getId(), connection);
 628        connection.sendSessionInitialize();
 629    }
 630
 631    public Optional<OngoingRtpSession> getOngoingRtpConnection(final Contact contact) {
 632        for (final Map.Entry<AbstractJingleConnection.Id, AbstractJingleConnection> entry :
 633                this.connections.entrySet()) {
 634            if (entry.getValue() instanceof JingleRtpConnection jingleRtpConnection) {
 635                final AbstractJingleConnection.Id id = entry.getKey();
 636                if (id.account == contact.getAccount()
 637                        && id.with.asBareJid().equals(contact.getJid().asBareJid())) {
 638                    return Optional.of(jingleRtpConnection);
 639                }
 640            }
 641        }
 642        synchronized (this.rtpSessionProposals) {
 643            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 644                    this.rtpSessionProposals.entrySet()) {
 645                final RtpSessionProposal proposal = entry.getKey();
 646                if (proposal.account == contact.getAccount()
 647                        && contact.getJid().asBareJid().equals(proposal.with)) {
 648                    final DeviceDiscoveryState preexistingState = entry.getValue();
 649                    if (preexistingState != null
 650                            && preexistingState != DeviceDiscoveryState.FAILED) {
 651                        return Optional.of(proposal);
 652                    }
 653                }
 654            }
 655        }
 656        return Optional.absent();
 657    }
 658
 659    public JingleRtpConnection getOngoingRtpConnection() {
 660        for (final AbstractJingleConnection jingleConnection : this.connections.values()) {
 661            if (jingleConnection instanceof JingleRtpConnection jingleRtpConnection) {
 662                if (jingleRtpConnection.isTerminated()) {
 663                    continue;
 664                }
 665                return jingleRtpConnection;
 666            }
 667        }
 668        return null;
 669    }
 670
 671    void finishConnectionOrThrow(final AbstractJingleConnection connection) {
 672        final AbstractJingleConnection.Id id = connection.getId();
 673        if (this.connections.remove(id) == null) {
 674            throw new IllegalStateException(
 675                    String.format("Unable to finish connection with id=%s", id));
 676        }
 677        // update chat UI to remove 'ongoing call' icon
 678        mXmppConnectionService.updateConversationUi();
 679    }
 680
 681    public boolean fireJingleRtpConnectionStateUpdates() {
 682        for (final AbstractJingleConnection connection : this.connections.values()) {
 683            if (connection instanceof JingleRtpConnection jingleRtpConnection) {
 684                if (jingleRtpConnection.isTerminated()) {
 685                    continue;
 686                }
 687                jingleRtpConnection.fireStateUpdate();
 688                return true;
 689            }
 690        }
 691        return false;
 692    }
 693
 694    public void retractSessionProposal(final Account account, final Jid with) {
 695        synchronized (this.rtpSessionProposals) {
 696            RtpSessionProposal matchingProposal = null;
 697            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
 698                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 699                    matchingProposal = proposal;
 700                    break;
 701                }
 702            }
 703            if (matchingProposal != null) {
 704                retractSessionProposal(matchingProposal, false);
 705            }
 706        }
 707    }
 708
 709    private void retractSessionProposal(final RtpSessionProposal rtpSessionProposal) {
 710        retractSessionProposal(rtpSessionProposal, true);
 711    }
 712
 713    private void retractSessionProposal(
 714            final RtpSessionProposal rtpSessionProposal, final boolean refresh) {
 715        final Account account = rtpSessionProposal.account;
 716        Log.d(
 717                Config.LOGTAG,
 718                account.getJid().asBareJid()
 719                        + ": retracting rtp session proposal with "
 720                        + rtpSessionProposal.with);
 721        this.rtpSessionProposals.remove(rtpSessionProposal);
 722        rtpSessionProposal.callIntegration.retracted();
 723        if (refresh) {
 724            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 725                    account,
 726                    rtpSessionProposal.with,
 727                    rtpSessionProposal.sessionId,
 728                    RtpEndUserState.RETRACTED);
 729        }
 730        final var messagePacket =
 731                mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
 732        writeLogMissedOutgoing(
 733                account,
 734                rtpSessionProposal.with,
 735                rtpSessionProposal.sessionId,
 736                null,
 737                System.currentTimeMillis());
 738        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 739    }
 740
 741    public JingleRtpConnection initializeRtpSession(
 742            final Account account, final Jid with, final Set<Media> media) {
 743        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
 744        final JingleRtpConnection rtpConnection =
 745                new JingleRtpConnection(this, id, account.getJid());
 746        rtpConnection.setProposedMedia(media);
 747        rtpConnection.getCallIntegration().startAudioRouting();
 748        this.connections.put(id, rtpConnection);
 749        rtpConnection.sendSessionInitiate();
 750        return rtpConnection;
 751    }
 752
 753    public @Nullable RtpSessionProposal proposeJingleRtpSession(
 754            final Account account, final Jid with, final Set<Media> media) {
 755        synchronized (this.rtpSessionProposals) {
 756            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 757                    this.rtpSessionProposals.entrySet()) {
 758                final RtpSessionProposal proposal = entry.getKey();
 759                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 760                    final DeviceDiscoveryState preexistingState = entry.getValue();
 761                    if (preexistingState != null
 762                            && preexistingState != DeviceDiscoveryState.FAILED) {
 763                        final RtpEndUserState endUserState = preexistingState.toEndUserState();
 764                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 765                                account, with, proposal.sessionId, endUserState);
 766                        return proposal;
 767                    }
 768                }
 769            }
 770            if (isBusy()) {
 771                if (hasMatchingRtpSession(account, with, media) != null) {
 772                    Log.d(
 773                            Config.LOGTAG,
 774                            "ignoring request to propose jingle session because the other party already created one for us");
 775                    // TODO return something that we can parse the connection of of
 776                    return null;
 777                }
 778                throw new IllegalStateException("There is already a running RTP session");
 779            }
 780            final CallIntegration callIntegration =
 781                    new CallIntegration(mXmppConnectionService.getApplicationContext());
 782            callIntegration.setVideoState(
 783                    Media.audioOnly(media)
 784                            ? VideoProfile.STATE_AUDIO_ONLY
 785                            : VideoProfile.STATE_BIDIRECTIONAL);
 786            callIntegration.setAddress(
 787                    CallIntegration.address(with.asBareJid()), TelecomManager.PRESENTATION_ALLOWED);
 788            final var contact = account.getRoster().getContact(with);
 789            callIntegration.setCallerDisplayName(
 790                    contact.getDisplayName(), TelecomManager.PRESENTATION_ALLOWED);
 791            callIntegration.setInitialAudioDevice(CallIntegration.initialAudioDevice(media));
 792            callIntegration.startAudioRouting();
 793            final RtpSessionProposal proposal =
 794                    RtpSessionProposal.of(account, with.asBareJid(), media, callIntegration);
 795            callIntegration.setCallback(new ProposalStateCallback(proposal));
 796            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
 797            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 798                    account, proposal.with, proposal.sessionId, RtpEndUserState.FINDING_DEVICE);
 799            final var messagePacket =
 800                    mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 801            // in privacy preserving environments 'propose' is only ACKed when we have presence
 802            // subscription (to not leak presence). Therefor a timeout is only appropriate for
 803            // contacts where we can expect the 'ringing' response
 804            final boolean triggerTimeout =
 805                    Config.JINGLE_MESSAGE_INIT_STRICT_DEVICE_TIMEOUT
 806                            || contact.mutualPresenceSubscription();
 807            SCHEDULED_EXECUTOR_SERVICE.schedule(
 808                    () -> {
 809                        final var currentProposalState = rtpSessionProposals.get(proposal);
 810                        Log.d(
 811                                Config.LOGTAG,
 812                                "proposal state after timeout " + currentProposalState);
 813                        if (triggerTimeout
 814                                && Arrays.asList(
 815                                                DeviceDiscoveryState.SEARCHING,
 816                                                DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED)
 817                                        .contains(currentProposalState)) {
 818                            deviceDiscoveryTimeout(account, proposal);
 819                        }
 820                    },
 821                    Config.DEVICE_DISCOVERY_TIMEOUT,
 822                    TimeUnit.MILLISECONDS);
 823            mXmppConnectionService.sendMessagePacket(account, messagePacket);
 824            return proposal;
 825        }
 826    }
 827
 828    private void deviceDiscoveryTimeout(final Account account, final RtpSessionProposal proposal) {
 829        // 'endUserState' is what we display in the UI. There is an argument to use 'BUSY' here
 830        // instead
 831        // we may or may not want to match this with the tone we are playing (see
 832        // callIntegration.error() or callIntegration.busy())
 833        final var endUserState = RtpEndUserState.CONNECTIVITY_ERROR;
 834        Log.d(Config.LOGTAG, "call proposal still in device discovery state after timeout");
 835        setTerminalSessionState(proposal, endUserState);
 836
 837        rtpSessionProposals.remove(proposal);
 838        // error and busy would probably be both appropriate tones to play
 839        // playing the error tone is probably more in line with what happens on a technical level
 840        // and would be a similar UX to what happens when you call a user that doesn't exist
 841        // playing the busy tone might be more in line with what some telephony networks play
 842        proposal.callIntegration.error();
 843        writeLogMissedOutgoing(
 844                account, proposal.with, proposal.sessionId, null, System.currentTimeMillis());
 845        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 846                account, proposal.with, proposal.sessionId, endUserState);
 847
 848        final var retraction =
 849                mXmppConnectionService.getMessageGenerator().sessionRetract(proposal);
 850        mXmppConnectionService.sendMessagePacket(account, retraction);
 851    }
 852
 853    public void sendJingleMessageFinish(
 854            final Contact contact, final String sessionId, final Reason reason) {
 855        final var account = contact.getAccount();
 856        final var messagePacket =
 857                mXmppConnectionService
 858                        .getMessageGenerator()
 859                        .sessionFinish(contact.getJid(), sessionId, reason);
 860        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 861    }
 862
 863    public Optional<RtpSessionProposal> matchingProposal(final Account account, final Jid with) {
 864        synchronized (this.rtpSessionProposals) {
 865            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 866                    this.rtpSessionProposals.entrySet()) {
 867                final RtpSessionProposal proposal = entry.getKey();
 868                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 869                    return Optional.of(proposal);
 870                }
 871            }
 872        }
 873        return Optional.absent();
 874    }
 875
 876    public boolean hasMatchingProposal(final Account account, final Jid with) {
 877        synchronized (this.rtpSessionProposals) {
 878            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 879                    this.rtpSessionProposals.entrySet()) {
 880                final var state = entry.getValue();
 881                final RtpSessionProposal proposal = entry.getKey();
 882                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 883                    // CallIntegrationConnectionService starts RtpSessionActivity with ACTION_VIEW
 884                    // and an EXTRA_LAST_REPORTED_STATE of DISCOVERING devices. however due to
 885                    // possible race conditions the state might have already moved on so we are
 886                    // going
 887                    // to update the UI
 888                    final RtpEndUserState endUserState = state.toEndUserState();
 889                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 890                            account, proposal.with, proposal.sessionId, endUserState);
 891                    return true;
 892                }
 893            }
 894        }
 895        return false;
 896    }
 897
 898    public void deliverIbbPacket(final Account account, final Iq packet) {
 899        final String sid;
 900        final Element payload;
 901        final InbandBytestreamsTransport.PacketType packetType;
 902        if (packet.hasChild("open", Namespace.IBB)) {
 903            packetType = InbandBytestreamsTransport.PacketType.OPEN;
 904            payload = packet.findChild("open", Namespace.IBB);
 905            sid = payload.getAttribute("sid");
 906        } else if (packet.hasChild("data", Namespace.IBB)) {
 907            packetType = InbandBytestreamsTransport.PacketType.DATA;
 908            payload = packet.findChild("data", Namespace.IBB);
 909            sid = payload.getAttribute("sid");
 910        } else if (packet.hasChild("close", Namespace.IBB)) {
 911            packetType = InbandBytestreamsTransport.PacketType.CLOSE;
 912            payload = packet.findChild("close", Namespace.IBB);
 913            sid = payload.getAttribute("sid");
 914        } else {
 915            packetType = null;
 916            payload = null;
 917            sid = null;
 918        }
 919        if (sid == null) {
 920            Log.d(
 921                    Config.LOGTAG,
 922                    account.getJid().asBareJid() + ": unable to deliver ibb packet. missing sid");
 923            account.getXmppConnection().sendIqPacket(packet.generateResponse(Iq.Type.ERROR), null);
 924            return;
 925        }
 926        for (final AbstractJingleConnection connection : this.connections.values()) {
 927            if (connection instanceof JingleFileTransferConnection fileTransfer) {
 928                final Transport transport = fileTransfer.getTransport();
 929                if (transport instanceof InbandBytestreamsTransport inBandTransport) {
 930                    if (sid.equals(inBandTransport.getStreamId())) {
 931                        if (inBandTransport.deliverPacket(packetType, packet.getFrom(), payload)) {
 932                            account.getXmppConnection()
 933                                    .sendIqPacket(packet.generateResponse(Iq.Type.RESULT), null);
 934                        } else {
 935                            account.getXmppConnection()
 936                                    .sendIqPacket(packet.generateResponse(Iq.Type.ERROR), null);
 937                        }
 938                        return;
 939                    }
 940                }
 941            }
 942        }
 943        Log.d(
 944                Config.LOGTAG,
 945                account.getJid().asBareJid() + ": unable to deliver ibb packet with sid=" + sid);
 946        account.getXmppConnection().sendIqPacket(packet.generateResponse(Iq.Type.ERROR), null);
 947    }
 948
 949    public void notifyRebound(final Account account) {
 950        for (final AbstractJingleConnection connection : this.connections.values()) {
 951            connection.notifyRebound();
 952        }
 953        final XmppConnection xmppConnection = account.getXmppConnection();
 954        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 955            resendSessionProposals(account);
 956        }
 957    }
 958
 959    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 960            Account account, Jid with, String sessionId) {
 961        final AbstractJingleConnection.Id id =
 962                AbstractJingleConnection.Id.of(account, with, sessionId);
 963        final AbstractJingleConnection connection = connections.get(id);
 964        if (connection instanceof JingleRtpConnection) {
 965            return new WeakReference<>((JingleRtpConnection) connection);
 966        }
 967        return null;
 968    }
 969
 970    public JingleRtpConnection findJingleRtpConnection(final Account account, final Jid with) {
 971        for (final AbstractJingleConnection connection : this.connections.values()) {
 972            if (connection instanceof JingleRtpConnection rtpConnection) {
 973                if (rtpConnection.isTerminated()) {
 974                    continue;
 975                }
 976                final var id = rtpConnection.getId();
 977                if (id.account == account && account.getJid().equals(with)) {
 978                    return rtpConnection;
 979                }
 980            }
 981        }
 982        return null;
 983    }
 984
 985    private void resendSessionProposals(final Account account) {
 986        synchronized (this.rtpSessionProposals) {
 987            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 988                    this.rtpSessionProposals.entrySet()) {
 989                final RtpSessionProposal proposal = entry.getKey();
 990                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 991                        && proposal.account == account) {
 992                    Log.d(
 993                            Config.LOGTAG,
 994                            account.getJid().asBareJid()
 995                                    + ": resending session proposal to "
 996                                    + proposal.with);
 997                    final var messagePacket =
 998                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 999                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
1000                }
1001            }
1002        }
1003    }
1004
1005    public void updateProposedSessionDiscovered(
1006            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
1007        synchronized (this.rtpSessionProposals) {
1008            final RtpSessionProposal sessionProposal =
1009                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
1010            final DeviceDiscoveryState currentState =
1011                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
1012            if (currentState == null) {
1013                Log.d(
1014                        Config.LOGTAG,
1015                        "unable to find session proposal for session id "
1016                                + sessionId
1017                                + " target="
1018                                + target);
1019                return;
1020            }
1021            if (currentState == DeviceDiscoveryState.DISCOVERED) {
1022                Log.d(
1023                        Config.LOGTAG,
1024                        "session proposal already at discovered. not going to fall back");
1025                return;
1026            }
1027
1028            Log.d(
1029                    Config.LOGTAG,
1030                    account.getJid().asBareJid()
1031                            + ": flagging session "
1032                            + sessionId
1033                            + " as "
1034                            + target);
1035
1036            final RtpEndUserState endUserState = target.toEndUserState();
1037
1038            if (target == DeviceDiscoveryState.FAILED) {
1039                Log.d(Config.LOGTAG, "removing session proposal after failure");
1040                setTerminalSessionState(sessionProposal, endUserState);
1041                this.rtpSessionProposals.remove(sessionProposal);
1042                sessionProposal.getCallIntegration().error();
1043                mXmppConnectionService.notifyJingleRtpConnectionUpdate(
1044                        account, sessionProposal.with, sessionProposal.sessionId, endUserState);
1045                return;
1046            }
1047
1048            this.rtpSessionProposals.put(sessionProposal, target);
1049
1050            if (endUserState == RtpEndUserState.RINGING) {
1051                sessionProposal.callIntegration.setDialing();
1052            }
1053
1054            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
1055                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
1056        }
1057    }
1058
1059    public void rejectRtpSession(final String sessionId) {
1060        for (final AbstractJingleConnection connection : this.connections.values()) {
1061            if (connection.getId().sessionId.equals(sessionId)) {
1062                if (connection instanceof JingleRtpConnection) {
1063                    try {
1064                        ((JingleRtpConnection) connection).rejectCall();
1065                        return;
1066                    } catch (final IllegalStateException e) {
1067                        Log.w(
1068                                Config.LOGTAG,
1069                                "race condition on rejecting call from notification",
1070                                e);
1071                    }
1072                }
1073            }
1074        }
1075    }
1076
1077    public void endRtpSession(final String sessionId) {
1078        for (final AbstractJingleConnection connection : this.connections.values()) {
1079            if (connection.getId().sessionId.equals(sessionId)) {
1080                if (connection instanceof JingleRtpConnection) {
1081                    ((JingleRtpConnection) connection).endCall();
1082                }
1083            }
1084        }
1085    }
1086
1087    public void failProceed(
1088            Account account, final Jid with, final String sessionId, final String message) {
1089        final AbstractJingleConnection.Id id =
1090                AbstractJingleConnection.Id.of(account, with, sessionId);
1091        final AbstractJingleConnection existingJingleConnection = connections.get(id);
1092        if (existingJingleConnection instanceof JingleRtpConnection) {
1093            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
1094        }
1095    }
1096
1097    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
1098        if (connections.containsValue(connection)) {
1099            return;
1100        }
1101        final IllegalStateException e =
1102                new IllegalStateException(
1103                        "JingleConnection has not been registered with connection manager");
1104        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
1105        throw e;
1106    }
1107
1108    void setTerminalSessionState(
1109            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
1110        this.terminatedSessions.put(
1111                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
1112    }
1113
1114    void setTerminalSessionState(final RtpSessionProposal proposal, final RtpEndUserState state) {
1115        this.terminatedSessions.put(
1116                PersistableSessionId.of(proposal), new TerminatedRtpSession(state, proposal.media));
1117    }
1118
1119    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
1120        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
1121    }
1122
1123    private static class PersistableSessionId {
1124        private final Jid with;
1125        private final String sessionId;
1126
1127        private PersistableSessionId(Jid with, String sessionId) {
1128            this.with = with;
1129            this.sessionId = sessionId;
1130        }
1131
1132        public static PersistableSessionId of(final AbstractJingleConnection.Id id) {
1133            return new PersistableSessionId(id.with, id.sessionId);
1134        }
1135
1136        public static PersistableSessionId of(final RtpSessionProposal proposal) {
1137            return new PersistableSessionId(proposal.with, proposal.sessionId);
1138        }
1139
1140        @Override
1141        public boolean equals(Object o) {
1142            if (this == o) return true;
1143            if (o == null || getClass() != o.getClass()) return false;
1144            PersistableSessionId that = (PersistableSessionId) o;
1145            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
1146        }
1147
1148        @Override
1149        public int hashCode() {
1150            return Objects.hashCode(with, sessionId);
1151        }
1152    }
1153
1154    public static class TerminatedRtpSession {
1155        public final RtpEndUserState state;
1156        public final Set<Media> media;
1157
1158        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
1159            this.state = state;
1160            this.media = media;
1161        }
1162    }
1163
1164    public enum DeviceDiscoveryState {
1165        SEARCHING,
1166        SEARCHING_ACKNOWLEDGED,
1167        DISCOVERED,
1168        FAILED;
1169
1170        public RtpEndUserState toEndUserState() {
1171            return switch (this) {
1172                case SEARCHING, SEARCHING_ACKNOWLEDGED -> RtpEndUserState.FINDING_DEVICE;
1173                case DISCOVERED -> RtpEndUserState.RINGING;
1174                default -> RtpEndUserState.CONNECTIVITY_ERROR;
1175            };
1176        }
1177    }
1178
1179    public static class RtpSessionProposal implements OngoingRtpSession {
1180        public final Jid with;
1181        public final String sessionId;
1182        public final Set<Media> media;
1183        private final Account account;
1184        private final CallIntegration callIntegration;
1185
1186        private RtpSessionProposal(
1187                Account account,
1188                Jid with,
1189                String sessionId,
1190                Set<Media> media,
1191                final CallIntegration callIntegration) {
1192            this.account = account;
1193            this.with = with;
1194            this.sessionId = sessionId;
1195            this.media = media;
1196            this.callIntegration = callIntegration;
1197        }
1198
1199        public static RtpSessionProposal of(
1200                Account account,
1201                Jid with,
1202                Set<Media> media,
1203                final CallIntegration callIntegration) {
1204            return new RtpSessionProposal(account, with, nextRandomId(), media, callIntegration);
1205        }
1206
1207        @Override
1208        public boolean equals(Object o) {
1209            if (this == o) return true;
1210            if (o == null || getClass() != o.getClass()) return false;
1211            RtpSessionProposal proposal = (RtpSessionProposal) o;
1212            return Objects.equal(account.getJid(), proposal.account.getJid())
1213                    && Objects.equal(with, proposal.with)
1214                    && Objects.equal(sessionId, proposal.sessionId);
1215        }
1216
1217        @Override
1218        public int hashCode() {
1219            return Objects.hashCode(account.getJid(), with, sessionId);
1220        }
1221
1222        @Override
1223        public Account getAccount() {
1224            return account;
1225        }
1226
1227        @Override
1228        public Jid getWith() {
1229            return with;
1230        }
1231
1232        @Override
1233        public String getSessionId() {
1234            return sessionId;
1235        }
1236
1237        @Override
1238        public CallIntegration getCallIntegration() {
1239            return this.callIntegration;
1240        }
1241
1242        @Override
1243        public Set<Media> getMedia() {
1244            return this.media;
1245        }
1246    }
1247
1248    public class ProposalStateCallback implements CallIntegration.Callback {
1249
1250        private final RtpSessionProposal proposal;
1251
1252        public ProposalStateCallback(final RtpSessionProposal proposal) {
1253            this.proposal = proposal;
1254        }
1255
1256        @Override
1257        public void onCallIntegrationShowIncomingCallUi() {}
1258
1259        @Override
1260        public void onCallIntegrationDisconnect() {
1261            Log.d(Config.LOGTAG, "a phone call has just been started. retracting proposal");
1262            retractSessionProposal(this.proposal);
1263        }
1264
1265        @Override
1266        public void onAudioDeviceChanged(
1267                final CallIntegration.AudioDevice selectedAudioDevice,
1268                final Set<CallIntegration.AudioDevice> availableAudioDevices) {
1269            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
1270                    selectedAudioDevice, availableAudioDevices);
1271        }
1272
1273        @Override
1274        public void onCallIntegrationReject() {}
1275
1276        @Override
1277        public void onCallIntegrationAnswer() {}
1278
1279        @Override
1280        public void onCallIntegrationSilence() {}
1281
1282        @Override
1283        public void onCallIntegrationMicrophoneEnabled(boolean enabled) {}
1284
1285        @Override
1286        public boolean applyDtmfTone(final String dtmf) {
1287            return false;
1288        }
1289    }
1290}