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 public void onCreate(Bundle savedInstanceState) {
33 getWindow().addFlags(
34 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
35 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
36 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
37 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
38 super.onCreate(savedInstanceState);
39 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
40 this.binding.acceptCall.setOnClickListener(this::acceptCall);
41 this.binding.rejectCall.setOnClickListener(this::rejectCall);
42 }
43
44 private void rejectCall(View view) {
45 requireRtpConnection().rejectCall();
46 finish();
47 }
48
49 private void acceptCall(View view) {
50 requireRtpConnection().acceptCall();
51 }
52
53 @Override
54 protected void refreshUiReal() {
55
56 }
57
58 @Override
59 void onBackendConnected() {
60 final Intent intent = getIntent();
61 final Account account = extractAccount(intent);
62 final String with = intent.getStringExtra(EXTRA_WITH);
63 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
64 if (with != null && sessionId != null) {
65 final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
66 .findJingleRtpConnection(account, Jid.ofEscaped(with), sessionId);
67 if (reference == null || reference.get() == null) {
68 finish();
69 return;
70 }
71 this.rtpConnectionReference = reference;
72 binding.with.setText(getWith().getDisplayName());
73 showState(requireRtpConnection().getEndUserState());
74 }
75 }
76
77 private void showState(final RtpEndUserState state) {
78 switch (state) {
79 case INCOMING_CALL:
80 binding.status.setText(R.string.rtp_state_incoming_call);
81 break;
82 case CONNECTING:
83 binding.status.setText(R.string.rtp_state_connecting);
84 break;
85 case CONNECTED:
86 binding.status.setText(R.string.rtp_state_connected);
87 break;
88 case ACCEPTING_CALL:
89 binding.status.setText(R.string.rtp_state_accepting_call);
90 break;
91 }
92 }
93
94 private Contact getWith() {
95 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
96 final Account account = id.account;
97 return account.getRoster().getContact(id.with);
98 }
99
100 private JingleRtpConnection requireRtpConnection() {
101 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
102 if (connection == null) {
103 throw new IllegalStateException("No RTP connection found");
104 }
105 return connection;
106 }
107
108 @Override
109 public void onJingleRtpConnectionUpdate(Account account, Jid with, RtpEndUserState state) {
110 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
111 if (account == id.account && id.with.equals(with)) {
112 runOnUiThread(()->{
113 showState(state);
114 });
115 } else {
116 Log.d(Config.LOGTAG,"received update for other rtp session");
117 }
118
119 }
120}