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