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    public static final String ACTION_ACCEPT = "accept";
 29
 30    private WeakReference<JingleRtpConnection> rtpConnectionReference;
 31
 32    private ActivityRtpSessionBinding binding;
 33
 34    @Override
 35    public void onCreate(Bundle savedInstanceState) {
 36        super.onCreate(savedInstanceState);
 37        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
 38                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
 39                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
 40                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
 41        ;
 42        Log.d(Config.LOGTAG, "RtpSessionActivity.onCreate()");
 43        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
 44        this.binding.rejectCall.setOnClickListener(this::rejectCall);
 45        this.binding.endCall.setOnClickListener(this::endCall);
 46        this.binding.acceptCall.setOnClickListener(this::acceptCall);
 47    }
 48
 49    @Override
 50    public void onStart() {
 51        super.onStart();
 52        Log.d(Config.LOGTAG, "RtpSessionActivity.onStart()");
 53    }
 54
 55    private void endCall(View view) {
 56        requireRtpConnection().endCall();
 57    }
 58
 59    private void rejectCall(View view) {
 60        requireRtpConnection().rejectCall();
 61        finish();
 62    }
 63
 64    private void acceptCall(View view) {
 65        requireRtpConnection().acceptCall();
 66    }
 67
 68    @Override
 69    protected void refreshUiReal() {
 70
 71    }
 72
 73    @Override
 74    public void onNewIntent(final Intent intent) {
 75        super.onNewIntent(intent);
 76        if (ACTION_ACCEPT.equals(intent.getAction())) {
 77            Log.d(Config.LOGTAG,"accepting through onNewIntent()");
 78            requireRtpConnection().acceptCall();
 79        }
 80    }
 81
 82    @Override
 83    void onBackendConnected() {
 84        final Intent intent = getIntent();
 85        final Account account = extractAccount(intent);
 86        final String with = intent.getStringExtra(EXTRA_WITH);
 87        final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
 88        if (with != null && sessionId != null) {
 89            final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
 90                    .findJingleRtpConnection(account, Jid.ofEscaped(with), sessionId);
 91            if (reference == null || reference.get() == null) {
 92                finish();
 93                return;
 94            }
 95            this.rtpConnectionReference = reference;
 96            binding.with.setText(getWith().getDisplayName());
 97            final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
 98            final String action = intent.getAction();
 99            updateStateDisplay(currentState);
100            updateButtonConfiguration(currentState);
101            if (ACTION_ACCEPT.equals(action)) {
102                Log.d(Config.LOGTAG,"intent action was accept");
103                requireRtpConnection().acceptCall();
104            }
105        }
106    }
107
108    private void updateStateDisplay(final RtpEndUserState state) {
109        switch (state) {
110            case INCOMING_CALL:
111                binding.status.setText(R.string.rtp_state_incoming_call);
112                break;
113            case CONNECTING:
114                binding.status.setText(R.string.rtp_state_connecting);
115                break;
116            case CONNECTED:
117                binding.status.setText(R.string.rtp_state_connected);
118                break;
119            case ACCEPTING_CALL:
120                binding.status.setText(R.string.rtp_state_accepting_call);
121                break;
122            case ENDING_CALL:
123                binding.status.setText(R.string.rtp_state_ending_call);
124                break;
125        }
126    }
127
128    private void updateButtonConfiguration(final RtpEndUserState state) {
129        if (state == RtpEndUserState.INCOMING_CALL) {
130            this.binding.rejectCall.show();
131            this.binding.endCall.hide();
132            this.binding.acceptCall.show();
133        } else if (state == RtpEndUserState.ENDING_CALL) {
134            this.binding.rejectCall.hide();
135            this.binding.endCall.hide();
136            this.binding.acceptCall.hide();
137        } else {
138            this.binding.rejectCall.hide();
139            this.binding.endCall.show();
140            this.binding.acceptCall.hide();
141        }
142    }
143
144    private Contact getWith() {
145        final AbstractJingleConnection.Id id = requireRtpConnection().getId();
146        final Account account = id.account;
147        return account.getRoster().getContact(id.with);
148    }
149
150    private JingleRtpConnection requireRtpConnection() {
151        final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
152        if (connection == null) {
153            throw new IllegalStateException("No RTP connection found");
154        }
155        return connection;
156    }
157
158    @Override
159    public void onJingleRtpConnectionUpdate(Account account, Jid with, RtpEndUserState state) {
160        final AbstractJingleConnection.Id id = requireRtpConnection().getId();
161        if (account == id.account && id.with.equals(with)) {
162            if (state == RtpEndUserState.ENDED) {
163                finish();
164                return;
165            }
166            runOnUiThread(() -> {
167                updateStateDisplay(state);
168                updateButtonConfiguration(state);
169            });
170        } else {
171            Log.d(Config.LOGTAG, "received update for other rtp session");
172        }
173
174    }
175}