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 void onBackendConnected() {
75 final Intent intent = getIntent();
76 final Account account = extractAccount(intent);
77 final String with = intent.getStringExtra(EXTRA_WITH);
78 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
79 if (with != null && sessionId != null) {
80 final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
81 .findJingleRtpConnection(account, Jid.ofEscaped(with), sessionId);
82 if (reference == null || reference.get() == null) {
83 finish();
84 return;
85 }
86 this.rtpConnectionReference = reference;
87 binding.with.setText(getWith().getDisplayName());
88 final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
89 final String action = intent.getAction();
90 updateStateDisplay(currentState);
91 updateButtonConfiguration(currentState);
92 if (ACTION_ACCEPT.equals(action)) {
93 Log.d(Config.LOGTAG,"intent action was accept");
94 requireRtpConnection().acceptCall();
95 }
96 }
97 }
98
99 private void updateStateDisplay(final RtpEndUserState state) {
100 switch (state) {
101 case INCOMING_CALL:
102 binding.status.setText(R.string.rtp_state_incoming_call);
103 break;
104 case CONNECTING:
105 binding.status.setText(R.string.rtp_state_connecting);
106 break;
107 case CONNECTED:
108 binding.status.setText(R.string.rtp_state_connected);
109 break;
110 case ACCEPTING_CALL:
111 binding.status.setText(R.string.rtp_state_accepting_call);
112 break;
113 case ENDING_CALL:
114 binding.status.setText(R.string.rtp_state_ending_call);
115 break;
116 }
117 }
118
119 private void updateButtonConfiguration(final RtpEndUserState state) {
120 if (state == RtpEndUserState.INCOMING_CALL) {
121 this.binding.rejectCall.show();
122 this.binding.endCall.hide();
123 this.binding.acceptCall.show();
124 } else if (state == RtpEndUserState.ENDING_CALL) {
125 this.binding.rejectCall.hide();
126 this.binding.endCall.hide();
127 this.binding.acceptCall.hide();
128 } else {
129 this.binding.rejectCall.hide();
130 this.binding.endCall.show();
131 this.binding.acceptCall.hide();
132 }
133 }
134
135 private Contact getWith() {
136 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
137 final Account account = id.account;
138 return account.getRoster().getContact(id.with);
139 }
140
141 private JingleRtpConnection requireRtpConnection() {
142 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
143 if (connection == null) {
144 throw new IllegalStateException("No RTP connection found");
145 }
146 return connection;
147 }
148
149 @Override
150 public void onJingleRtpConnectionUpdate(Account account, Jid with, RtpEndUserState state) {
151 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
152 if (account == id.account && id.with.equals(with)) {
153 runOnUiThread(() -> {
154 updateStateDisplay(state);
155 updateButtonConfiguration(state);
156 });
157 } else {
158 Log.d(Config.LOGTAG, "received update for other rtp session");
159 }
160
161 }
162}