RtpSessionActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.content.Intent;
  4import android.databinding.DataBindingUtil;
  5import android.os.Bundle;
  6import android.util.Log;
  7import android.view.View;
  8import android.view.WindowManager;
  9
 10import java.lang.ref.WeakReference;
 11
 12import eu.siacs.conversations.Config;
 13import eu.siacs.conversations.R;
 14import eu.siacs.conversations.databinding.ActivityRtpSessionBinding;
 15import eu.siacs.conversations.entities.Account;
 16import eu.siacs.conversations.entities.Contact;
 17import eu.siacs.conversations.services.XmppConnectionService;
 18import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
 19import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
 20import eu.siacs.conversations.xmpp.jingle.RtpEndUserState;
 21import rocks.xmpp.addr.Jid;
 22
 23public class RtpSessionActivity extends XmppActivity implements XmppConnectionService.OnJingleRtpConnectionUpdate {
 24
 25    public static final String EXTRA_WITH = "with";
 26    public static final String EXTRA_SESSION_ID = "session_id";
 27
 28    private WeakReference<JingleRtpConnection> rtpConnectionReference;
 29
 30    private ActivityRtpSessionBinding binding;
 31
 32    @Override
 33    public void onCreate(Bundle savedInstanceState) {
 34        super.onCreate(savedInstanceState);
 35        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
 36        this.binding.rejectCall.setOnClickListener(this::rejectCall);
 37        this.binding.endCall.setOnClickListener(this::endCall);
 38        this.binding.acceptCall.setOnClickListener(this::acceptCall);
 39    }
 40
 41    @Override
 42    public void onStart() {
 43        super.onStart();
 44        Log.d(Config.LOGTAG,"RtpSessionActivity.onStart()");
 45    }
 46
 47    private void endCall(View view) {
 48        requireRtpConnection().endCall();
 49    }
 50
 51    private void rejectCall(View view) {
 52        requireRtpConnection().rejectCall();
 53        finish();
 54    }
 55
 56    private void acceptCall(View view) {
 57        requireRtpConnection().acceptCall();
 58    }
 59
 60    @Override
 61    protected void refreshUiReal() {
 62
 63    }
 64
 65    @Override
 66    void onBackendConnected() {
 67        final Intent intent = getIntent();
 68        final Account account = extractAccount(intent);
 69        final String with = intent.getStringExtra(EXTRA_WITH);
 70        final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
 71        if (with != null && sessionId != null) {
 72            final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
 73                    .findJingleRtpConnection(account, Jid.ofEscaped(with), sessionId);
 74            if (reference == null || reference.get() == null) {
 75                finish();
 76                return;
 77            }
 78            this.rtpConnectionReference = reference;
 79            binding.with.setText(getWith().getDisplayName());
 80            final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
 81            updateStateDisplay(currentState);
 82            updateButtonConfiguration(currentState);
 83        }
 84    }
 85
 86    private void updateStateDisplay(final RtpEndUserState state) {
 87        switch (state) {
 88            case INCOMING_CALL:
 89                binding.status.setText(R.string.rtp_state_incoming_call);
 90                break;
 91            case CONNECTING:
 92                binding.status.setText(R.string.rtp_state_connecting);
 93                break;
 94            case CONNECTED:
 95                binding.status.setText(R.string.rtp_state_connected);
 96                break;
 97            case ACCEPTING_CALL:
 98                binding.status.setText(R.string.rtp_state_accepting_call);
 99                break;
100            case ENDING_CALL:
101                binding.status.setText(R.string.rtp_state_ending_call);
102                break;
103        }
104    }
105
106    private void updateButtonConfiguration(final RtpEndUserState state) {
107        if (state == RtpEndUserState.INCOMING_CALL) {
108            this.binding.rejectCall.show();
109            this.binding.endCall.hide();
110            this.binding.acceptCall.show();
111        } else if (state == RtpEndUserState.ENDING_CALL) {
112            this.binding.rejectCall.hide();
113            this.binding.endCall.hide();
114            this.binding.acceptCall.hide();
115        } else {
116            this.binding.rejectCall.hide();
117            this.binding.endCall.show();
118            this.binding.acceptCall.hide();
119        }
120    }
121
122    private Contact getWith() {
123        final AbstractJingleConnection.Id id = requireRtpConnection().getId();
124        final Account account = id.account;
125        return account.getRoster().getContact(id.with);
126    }
127
128    private JingleRtpConnection requireRtpConnection() {
129        final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
130        if (connection == null) {
131            throw new IllegalStateException("No RTP connection found");
132        }
133        return connection;
134    }
135
136    @Override
137    public void onJingleRtpConnectionUpdate(Account account, Jid with, RtpEndUserState state) {
138        final AbstractJingleConnection.Id id = requireRtpConnection().getId();
139        if (account == id.account && id.with.equals(with)) {
140            runOnUiThread(()->{
141                updateStateDisplay(state);
142                updateButtonConfiguration(state);
143            });
144        } else {
145            Log.d(Config.LOGTAG,"received update for other rtp session");
146        }
147
148    }
149}