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.rejectCall.setOnClickListener(this::rejectCall);
41 this.binding.endCall.setOnClickListener(this::endCall);
42 this.binding.acceptCall.setOnClickListener(this::acceptCall);
43 }
44
45 private void endCall(View view) {
46 requireRtpConnection().endCall();
47 }
48
49 private void rejectCall(View view) {
50 requireRtpConnection().rejectCall();
51 finish();
52 }
53
54 private void acceptCall(View view) {
55 requireRtpConnection().acceptCall();
56 }
57
58 @Override
59 protected void refreshUiReal() {
60
61 }
62
63 @Override
64 void onBackendConnected() {
65 final Intent intent = getIntent();
66 final Account account = extractAccount(intent);
67 final String with = intent.getStringExtra(EXTRA_WITH);
68 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
69 if (with != null && sessionId != null) {
70 final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
71 .findJingleRtpConnection(account, Jid.ofEscaped(with), sessionId);
72 if (reference == null || reference.get() == null) {
73 finish();
74 return;
75 }
76 this.rtpConnectionReference = reference;
77 binding.with.setText(getWith().getDisplayName());
78 final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
79 updateStateDisplay(currentState);
80 updateButtonConfiguration(currentState);
81 }
82 }
83
84 private void updateStateDisplay(final RtpEndUserState state) {
85 switch (state) {
86 case INCOMING_CALL:
87 binding.status.setText(R.string.rtp_state_incoming_call);
88 break;
89 case CONNECTING:
90 binding.status.setText(R.string.rtp_state_connecting);
91 break;
92 case CONNECTED:
93 binding.status.setText(R.string.rtp_state_connected);
94 break;
95 case ACCEPTING_CALL:
96 binding.status.setText(R.string.rtp_state_accepting_call);
97 break;
98 case ENDING_CALL:
99 binding.status.setText(R.string.rtp_state_ending_call);
100 break;
101 }
102 }
103
104 private void updateButtonConfiguration(final RtpEndUserState state) {
105 if (state == RtpEndUserState.INCOMING_CALL) {
106 this.binding.rejectCall.show();
107 this.binding.endCall.hide();
108 this.binding.acceptCall.show();
109 } else if (state == RtpEndUserState.ENDING_CALL) {
110 this.binding.rejectCall.hide();
111 this.binding.endCall.hide();
112 this.binding.acceptCall.hide();
113 } else {
114 this.binding.rejectCall.hide();
115 this.binding.endCall.show();
116 this.binding.acceptCall.hide();
117 }
118 }
119
120 private Contact getWith() {
121 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
122 final Account account = id.account;
123 return account.getRoster().getContact(id.with);
124 }
125
126 private JingleRtpConnection requireRtpConnection() {
127 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
128 if (connection == null) {
129 throw new IllegalStateException("No RTP connection found");
130 }
131 return connection;
132 }
133
134 @Override
135 public void onJingleRtpConnectionUpdate(Account account, Jid with, RtpEndUserState state) {
136 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
137 if (account == id.account && id.with.equals(with)) {
138 runOnUiThread(()->{
139 updateStateDisplay(state);
140 updateButtonConfiguration(state);
141 });
142 } else {
143 Log.d(Config.LOGTAG,"received update for other rtp session");
144 }
145
146 }
147}