1package eu.siacs.conversations.ui;
2
3import android.content.Intent;
4import android.content.res.ColorStateList;
5import android.databinding.DataBindingUtil;
6import android.os.Bundle;
7import android.util.Log;
8import android.view.View;
9import android.view.WindowManager;
10
11import java.lang.ref.WeakReference;
12
13import eu.siacs.conversations.Config;
14import eu.siacs.conversations.R;
15import eu.siacs.conversations.databinding.ActivityRtpSessionBinding;
16import eu.siacs.conversations.entities.Account;
17import eu.siacs.conversations.entities.Contact;
18import eu.siacs.conversations.services.XmppConnectionService;
19import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
20import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
21import eu.siacs.conversations.xmpp.jingle.RtpEndUserState;
22import rocks.xmpp.addr.Jid;
23
24import static java.util.Arrays.asList;
25
26//TODO if last state was BUSY (or RETRY); we want to reset action to view or something so we don’t automatically call again on recreate
27
28public class RtpSessionActivity extends XmppActivity implements XmppConnectionService.OnJingleRtpConnectionUpdate {
29
30 public static final String EXTRA_WITH = "with";
31 public static final String EXTRA_SESSION_ID = "session_id";
32 public static final String EXTRA_LAST_REPORTED_STATE = "last_reported_state";
33
34 public static final String ACTION_ACCEPT_CALL = "action_accept_call";
35 public static final String ACTION_MAKE_VOICE_CALL = "action_make_voice_call";
36 public static final String ACTION_MAKE_VIDEO_CALL = "action_make_video_call";
37
38 private WeakReference<JingleRtpConnection> rtpConnectionReference;
39
40 private ActivityRtpSessionBinding binding;
41
42 @Override
43 public void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
46 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
47 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
48 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
49 ;
50 Log.d(Config.LOGTAG, "RtpSessionActivity.onCreate()");
51 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
52 }
53
54 @Override
55 public void onStart() {
56 super.onStart();
57 Log.d(Config.LOGTAG, "RtpSessionActivity.onStart()");
58 }
59
60 private void endCall(View view) {
61 if (this.rtpConnectionReference == null) {
62 final Intent intent = getIntent();
63 final Account account = extractAccount(intent);
64 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
65 xmppConnectionService.getJingleConnectionManager().retractSessionProposal(account, with.asBareJid());
66 finish();
67 } else {
68 requireRtpConnection().endCall();
69 }
70 }
71
72 private void rejectCall(View view) {
73 requireRtpConnection().rejectCall();
74 finish();
75 }
76
77 private void acceptCall(View view) {
78 requireRtpConnection().acceptCall();
79 }
80
81 @Override
82 protected void refreshUiReal() {
83
84 }
85
86 @Override
87 public void onNewIntent(final Intent intent) {
88 super.onNewIntent(intent);
89 //TODO reinitialize
90 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
91 Log.d(Config.LOGTAG, "accepting through onNewIntent()");
92 requireRtpConnection().acceptCall();
93 }
94 }
95
96 @Override
97 void onBackendConnected() {
98 final Intent intent = getIntent();
99 final Account account = extractAccount(intent);
100 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
101 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
102 if (sessionId != null) {
103 initializeActivityWithRunningRapSession(account, with, sessionId);
104 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
105 Log.d(Config.LOGTAG, "intent action was accept");
106 requireRtpConnection().acceptCall();
107 }
108 } else if (asList(ACTION_MAKE_VIDEO_CALL, ACTION_MAKE_VOICE_CALL).contains(intent.getAction())) {
109 xmppConnectionService.getJingleConnectionManager().proposeJingleRtpSession(account, with);
110 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
111 } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
112 final String extraLastState = intent.getStringExtra(EXTRA_LAST_REPORTED_STATE);
113 if (extraLastState != null) {
114 Log.d(Config.LOGTAG, "restored last state from intent extra");
115 RtpEndUserState state = RtpEndUserState.valueOf(extraLastState);
116 updateButtonConfiguration(state);
117 updateStateDisplay(state);
118 }
119 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
120 }
121 }
122
123
124 private void initializeActivityWithRunningRapSession(final Account account, Jid with, String sessionId) {
125 final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
126 .findJingleRtpConnection(account, with, sessionId);
127 if (reference == null || reference.get() == null) {
128 finish();
129 return;
130 }
131 this.rtpConnectionReference = reference;
132 final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
133 if (currentState == RtpEndUserState.ENDED) {
134 finish();
135 return;
136 }
137 binding.with.setText(getWith().getDisplayName());
138 updateStateDisplay(currentState);
139 updateButtonConfiguration(currentState);
140 }
141
142 private void reInitializeActivityWithRunningRapSession(final Account account, Jid with, String sessionId) {
143 runOnUiThread(() -> {
144 initializeActivityWithRunningRapSession(account, with, sessionId);
145 });
146 final Intent intent = new Intent(Intent.ACTION_VIEW);
147 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
148 intent.putExtra(EXTRA_WITH, with.toEscapedString());
149 intent.putExtra(EXTRA_SESSION_ID, sessionId);
150 setIntent(intent);
151 }
152
153 private void updateStateDisplay(final RtpEndUserState state) {
154 switch (state) {
155 case INCOMING_CALL:
156 binding.status.setText(R.string.rtp_state_incoming_call);
157 break;
158 case CONNECTING:
159 binding.status.setText(R.string.rtp_state_connecting);
160 break;
161 case CONNECTED:
162 binding.status.setText(R.string.rtp_state_connected);
163 break;
164 case ACCEPTING_CALL:
165 binding.status.setText(R.string.rtp_state_accepting_call);
166 break;
167 case ENDING_CALL:
168 binding.status.setText(R.string.rtp_state_ending_call);
169 break;
170 case FINDING_DEVICE:
171 binding.status.setText(R.string.rtp_state_finding_device);
172 break;
173 case RINGING:
174 binding.status.setText(R.string.rtp_state_ringing);
175 break;
176 case DECLINED_OR_BUSY:
177 binding.status.setText(R.string.rtp_state_declined_or_busy);
178 break;
179 case CONNECTIVITY_ERROR:
180 binding.status.setText(R.string.rtp_state_connectivity_error);
181 break;
182 case APPLICATION_ERROR:
183 binding.status.setText(R.string.rtp_state_application_failure);
184 break;
185 case ENDED:
186 throw new IllegalStateException("Activity should have called finish()");
187 default:
188 throw new IllegalStateException(String.format("State %s has not been handled in UI", state));
189 }
190 }
191
192 private void updateButtonConfiguration(final RtpEndUserState state) {
193 if (state == RtpEndUserState.INCOMING_CALL) {
194 this.binding.rejectCall.setOnClickListener(this::rejectCall);
195 this.binding.rejectCall.setImageResource(R.drawable.ic_call_end_white_48dp);
196 this.binding.rejectCall.show();
197 this.binding.endCall.hide();
198 this.binding.acceptCall.setOnClickListener(this::acceptCall);
199 this.binding.acceptCall.setImageResource(R.drawable.ic_call_white_48dp);
200 this.binding.acceptCall.show();
201 } else if (state == RtpEndUserState.ENDING_CALL) {
202 this.binding.rejectCall.hide();
203 this.binding.endCall.hide();
204 this.binding.acceptCall.hide();
205 } else if (state == RtpEndUserState.DECLINED_OR_BUSY) {
206 this.binding.rejectCall.hide();
207 this.binding.endCall.setOnClickListener(this::exit);
208 this.binding.endCall.setImageResource(R.drawable.ic_clear_white_48dp);
209 this.binding.endCall.show();
210 this.binding.acceptCall.hide();
211 } else if (state == RtpEndUserState.CONNECTIVITY_ERROR || state == RtpEndUserState.APPLICATION_ERROR) {
212 this.binding.rejectCall.setOnClickListener(this::exit);
213 this.binding.rejectCall.setImageResource(R.drawable.ic_clear_white_48dp);
214 this.binding.rejectCall.show();
215 this.binding.endCall.hide();
216 this.binding.acceptCall.setOnClickListener(this::retry);
217 this.binding.acceptCall.setImageResource(R.drawable.ic_replay_white_48dp);
218 this.binding.acceptCall.show();
219 } else {
220 this.binding.rejectCall.hide();
221 this.binding.endCall.setOnClickListener(this::endCall);
222 this.binding.endCall.setImageResource(R.drawable.ic_call_end_white_48dp);
223 this.binding.endCall.show();
224 this.binding.acceptCall.hide();
225 }
226 }
227
228 private void retry(View view) {
229 Log.d(Config.LOGTAG,"attempting retry");
230 }
231
232 private void exit(View view) {
233 finish();
234 }
235
236 private Contact getWith() {
237 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
238 final Account account = id.account;
239 return account.getRoster().getContact(id.with);
240 }
241
242 private JingleRtpConnection requireRtpConnection() {
243 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
244 if (connection == null) {
245 throw new IllegalStateException("No RTP connection found");
246 }
247 return connection;
248 }
249
250 @Override
251 public void onJingleRtpConnectionUpdate(Account account, Jid with, final String sessionId, RtpEndUserState state) {
252 Log.d(Config.LOGTAG, "onJingleRtpConnectionUpdate(" + state + ")");
253 if (with.isBareJid()) {
254 updateRtpSessionProposalState(account, with, state);
255 return;
256 }
257 if (this.rtpConnectionReference == null) {
258 //this happens when going from proposed session to actual session
259 reInitializeActivityWithRunningRapSession(account, with, sessionId);
260 return;
261 }
262 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
263 if (account == id.account && id.with.equals(with) && id.sessionId.equals(sessionId)) {
264 if (state == RtpEndUserState.ENDED) {
265 finish();
266 return;
267 } else if (asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.CONNECTIVITY_ERROR).contains(state)) {
268 resetIntent(account, with, state);
269 }
270 runOnUiThread(() -> {
271 updateStateDisplay(state);
272 updateButtonConfiguration(state);
273 });
274 } else {
275 Log.d(Config.LOGTAG, "received update for other rtp session");
276 }
277 }
278
279 private void updateRtpSessionProposalState(final Account account, final Jid with, final RtpEndUserState state) {
280 final Intent currentIntent = getIntent();
281 final String withExtra = currentIntent == null ? null : currentIntent.getStringExtra(EXTRA_WITH);
282 if (withExtra == null) {
283 return;
284 }
285 if (Jid.ofEscaped(withExtra).asBareJid().equals(with)) {
286 runOnUiThread(() -> {
287 updateStateDisplay(state);
288 updateButtonConfiguration(state);
289 });
290 resetIntent(account, with, state);
291 }
292 }
293
294 private void resetIntent(final Account account, Jid with, final RtpEndUserState state) {
295 Log.d(Config.LOGTAG, "resetting intent");
296 final Intent intent = new Intent(Intent.ACTION_VIEW);
297 intent.putExtra(EXTRA_WITH, with.asBareJid().toEscapedString());
298 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
299 intent.putExtra(EXTRA_LAST_REPORTED_STATE, state.toString());
300 setIntent(intent);
301 }
302}