JingleConnectionManager.java

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