1package eu.siacs.conversations.ui;
2
3import android.Manifest;
4import android.annotation.SuppressLint;
5import android.content.Context;
6import android.content.Intent;
7import android.databinding.DataBindingUtil;
8import android.os.Build;
9import android.os.Bundle;
10import android.os.PowerManager;
11import android.support.annotation.NonNull;
12import android.support.annotation.StringRes;
13import android.util.Log;
14import android.view.View;
15import android.view.WindowManager;
16import android.widget.Toast;
17
18import com.google.common.base.Optional;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
21
22import org.webrtc.RendererCommon;
23import org.webrtc.SurfaceViewRenderer;
24import org.webrtc.VideoTrack;
25
26import java.lang.ref.WeakReference;
27import java.util.Arrays;
28import java.util.List;
29import java.util.Set;
30
31import eu.siacs.conversations.Config;
32import eu.siacs.conversations.R;
33import eu.siacs.conversations.databinding.ActivityRtpSessionBinding;
34import eu.siacs.conversations.entities.Account;
35import eu.siacs.conversations.entities.Contact;
36import eu.siacs.conversations.services.AppRTCAudioManager;
37import eu.siacs.conversations.services.XmppConnectionService;
38import eu.siacs.conversations.utils.PermissionUtils;
39import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
40import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
41import eu.siacs.conversations.xmpp.jingle.Media;
42import eu.siacs.conversations.xmpp.jingle.RtpEndUserState;
43import rocks.xmpp.addr.Jid;
44
45import static eu.siacs.conversations.utils.PermissionUtils.getFirstDenied;
46import static java.util.Arrays.asList;
47
48public class RtpSessionActivity extends XmppActivity implements XmppConnectionService.OnJingleRtpConnectionUpdate {
49
50 private static final String PROXIMITY_WAKE_LOCK_TAG = "conversations:in-rtp-session";
51
52 private static final int REQUEST_ACCEPT_CALL = 0x1111;
53
54 public static final String EXTRA_WITH = "with";
55 public static final String EXTRA_SESSION_ID = "session_id";
56 public static final String EXTRA_LAST_REPORTED_STATE = "last_reported_state";
57
58 public static final String ACTION_ACCEPT_CALL = "action_accept_call";
59 public static final String ACTION_MAKE_VOICE_CALL = "action_make_voice_call";
60 public static final String ACTION_MAKE_VIDEO_CALL = "action_make_video_call";
61
62
63 private WeakReference<JingleRtpConnection> rtpConnectionReference;
64
65 private ActivityRtpSessionBinding binding;
66 private PowerManager.WakeLock mProximityWakeLock;
67
68 @Override
69 public void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
72 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
73 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
74 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
75 Log.d(Config.LOGTAG, "RtpSessionActivity.onCreate()");
76 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
77 setSupportActionBar(binding.toolbar);
78 }
79
80 @Override
81 public void onStart() {
82 super.onStart();
83 Log.d(Config.LOGTAG, "RtpSessionActivity.onStart()");
84 }
85
86 private void endCall(View view) {
87 endCall();
88 }
89
90 private void endCall() {
91 if (this.rtpConnectionReference == null) {
92 final Intent intent = getIntent();
93 final Account account = extractAccount(intent);
94 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
95 xmppConnectionService.getJingleConnectionManager().retractSessionProposal(account, with.asBareJid());
96 finish();
97 } else {
98 requireRtpConnection().endCall();
99 }
100 }
101
102 private void rejectCall(View view) {
103 requireRtpConnection().rejectCall();
104 finish();
105 }
106
107 private void acceptCall(View view) {
108 requestPermissionsAndAcceptCall();
109 }
110
111 private void requestPermissionsAndAcceptCall() {
112 final List<String> permissions;
113 if (getMedia().contains(Media.VIDEO)) {
114 permissions = ImmutableList.of(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO);
115 } else {
116 permissions = ImmutableList.of(Manifest.permission.RECORD_AUDIO);
117 }
118 if (PermissionUtils.hasPermission(this, permissions, REQUEST_ACCEPT_CALL)) {
119 //TODO like wise the propose; we might just wait here for the audio manager to come up
120 putScreenInCallMode();
121 requireRtpConnection().acceptCall();
122 }
123 }
124
125 @SuppressLint("WakelockTimeout")
126 private void putScreenInCallMode() {
127 //TODO for video calls we actually do want to keep the screen on
128 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
129 final JingleRtpConnection rtpConnection = rtpConnectionReference != null ? rtpConnectionReference.get() : null;
130 final AppRTCAudioManager audioManager = rtpConnection == null ? null : rtpConnection.getAudioManager();
131 if (audioManager == null || audioManager.getSelectedAudioDevice() == AppRTCAudioManager.AudioDevice.EARPIECE) {
132 acquireProximityWakeLock();
133 }
134 }
135
136 private void acquireProximityWakeLock() {
137 final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
138 if (powerManager == null) {
139 Log.e(Config.LOGTAG, "power manager not available");
140 return;
141 }
142 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
143 if (this.mProximityWakeLock == null) {
144 this.mProximityWakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, PROXIMITY_WAKE_LOCK_TAG);
145 }
146 if (!this.mProximityWakeLock.isHeld()) {
147 Log.d(Config.LOGTAG, "acquiring proximity wake lock");
148 this.mProximityWakeLock.acquire();
149 }
150 }
151 }
152
153 private void releaseProximityWakeLock() {
154 if (this.mProximityWakeLock != null && mProximityWakeLock.isHeld()) {
155 Log.d(Config.LOGTAG, "releasing proximity wake lock");
156 this.mProximityWakeLock.release();
157 this.mProximityWakeLock = null;
158 }
159 }
160
161 private void putProximityWakeLockInProperState() {
162 if (requireRtpConnection().getAudioManager().getSelectedAudioDevice() == AppRTCAudioManager.AudioDevice.EARPIECE) {
163 acquireProximityWakeLock();
164 } else {
165 releaseProximityWakeLock();
166 }
167 }
168
169 @Override
170 protected void refreshUiReal() {
171
172 }
173
174 @Override
175 public void onNewIntent(final Intent intent) {
176 super.onNewIntent(intent);
177 setIntent(intent);
178 if (xmppConnectionService == null) {
179 Log.d(Config.LOGTAG, "RtpSessionActivity: background service wasn't bound in onNewIntent()");
180 return;
181 }
182 final Account account = extractAccount(intent);
183 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
184 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
185 if (sessionId != null) {
186 Log.d(Config.LOGTAG, "reinitializing from onNewIntent()");
187 initializeActivityWithRunningRtpSession(account, with, sessionId);
188 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
189 Log.d(Config.LOGTAG, "accepting call from onNewIntent()");
190 requestPermissionsAndAcceptCall();
191 resetIntent(intent.getExtras());
192 }
193 } else {
194 throw new IllegalStateException("received onNewIntent without sessionId");
195 }
196 }
197
198 @Override
199 void onBackendConnected() {
200 final Intent intent = getIntent();
201 final String action = intent.getAction();
202 final Account account = extractAccount(intent);
203 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
204 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
205 if (sessionId != null) {
206 initializeActivityWithRunningRtpSession(account, with, sessionId);
207 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
208 Log.d(Config.LOGTAG, "intent action was accept");
209 requestPermissionsAndAcceptCall();
210 resetIntent(intent.getExtras());
211 }
212 } else if (asList(ACTION_MAKE_VIDEO_CALL, ACTION_MAKE_VOICE_CALL).contains(action)) {
213 proposeJingleRtpSession(account, with, actionToMedia(action));
214 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
215 } else if (Intent.ACTION_VIEW.equals(action)) {
216 final String extraLastState = intent.getStringExtra(EXTRA_LAST_REPORTED_STATE);
217 if (extraLastState != null) {
218 Log.d(Config.LOGTAG, "restored last state from intent extra");
219 RtpEndUserState state = RtpEndUserState.valueOf(extraLastState);
220 updateButtonConfiguration(state);
221 updateStateDisplay(state);
222 }
223 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
224 }
225 }
226
227 private static Set<Media> actionToMedia(final String action) {
228 if (ACTION_MAKE_VIDEO_CALL.equals(action)) {
229 return ImmutableSet.of(Media.AUDIO, Media.VIDEO);
230 } else {
231 return ImmutableSet.of(Media.AUDIO);
232 }
233 }
234
235 private void proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
236 xmppConnectionService.getJingleConnectionManager().proposeJingleRtpSession(account, with, media);
237 //TODO maybe we don’t want to acquire a wake lock just yet and wait for audio manager to discover what speaker we are using
238 putScreenInCallMode();
239 }
240
241 @Override
242 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
243 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
244 if (PermissionUtils.allGranted(grantResults)) {
245 if (requestCode == REQUEST_ACCEPT_CALL) {
246 requireRtpConnection().acceptCall();
247 }
248 } else {
249 @StringRes int res;
250 final String firstDenied = getFirstDenied(grantResults, permissions);
251 if (Manifest.permission.RECORD_AUDIO.equals(firstDenied)) {
252 res = R.string.no_microphone_permission;
253 } else if (Manifest.permission.CAMERA.equals(firstDenied)) {
254 res = R.string.no_camera_permission;
255 } else {
256 throw new IllegalStateException("Invalid permission result request");
257 }
258 Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
259 }
260 }
261
262 @Override
263 public void onStop() {
264 binding.remoteVideo.release();
265 binding.localVideo.release();
266 releaseProximityWakeLock();
267 //TODO maybe we want to finish if call had ended
268 super.onStop();
269 }
270
271 @Override
272 public void onBackPressed() {
273 endCall();
274 super.onBackPressed();
275 }
276
277
278 private void initializeActivityWithRunningRtpSession(final Account account, Jid with, String sessionId) {
279 final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
280 .findJingleRtpConnection(account, with, sessionId);
281 if (reference == null || reference.get() == null) {
282 finish();
283 return;
284 }
285 this.rtpConnectionReference = reference;
286 final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
287 if (currentState == RtpEndUserState.ENDED) {
288 finish();
289 return;
290 }
291 if (currentState == RtpEndUserState.INCOMING_CALL) {
292 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
293 }
294 if (JingleRtpConnection.STATES_SHOWING_ONGOING_CALL.contains(requireRtpConnection().getState())) {
295 putScreenInCallMode();
296 }
297 binding.with.setText(getWith().getDisplayName());
298 updateVideoViews();
299 updateStateDisplay(currentState);
300 updateButtonConfiguration(currentState);
301 }
302
303 private void reInitializeActivityWithRunningRapSession(final Account account, Jid with, String sessionId) {
304 runOnUiThread(() -> {
305 initializeActivityWithRunningRtpSession(account, with, sessionId);
306 });
307 final Intent intent = new Intent(Intent.ACTION_VIEW);
308 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
309 intent.putExtra(EXTRA_WITH, with.toEscapedString());
310 intent.putExtra(EXTRA_SESSION_ID, sessionId);
311 setIntent(intent);
312 }
313
314 private void ensureSurfaceViewRendererIsSetup(final SurfaceViewRenderer surfaceViewRenderer) {
315 surfaceViewRenderer.setVisibility(View.VISIBLE);
316 try {
317 surfaceViewRenderer.init(requireRtpConnection().getEglBaseContext(), null);
318 } catch (IllegalStateException e) {
319 Log.d(Config.LOGTAG, "SurfaceViewRenderer was already initialized");
320 }
321 surfaceViewRenderer.setEnableHardwareScaler(true);
322 }
323
324 private void updateStateDisplay(final RtpEndUserState state) {
325 switch (state) {
326 case INCOMING_CALL:
327 if (getMedia().contains(Media.VIDEO)) {
328 setTitle(R.string.rtp_state_incoming_video_call);
329 } else {
330 setTitle(R.string.rtp_state_incoming_call);
331 }
332 break;
333 case CONNECTING:
334 setTitle(R.string.rtp_state_connecting);
335 break;
336 case CONNECTED:
337 setTitle(R.string.rtp_state_connected);
338 break;
339 case ACCEPTING_CALL:
340 setTitle(R.string.rtp_state_accepting_call);
341 break;
342 case ENDING_CALL:
343 setTitle(R.string.rtp_state_ending_call);
344 break;
345 case FINDING_DEVICE:
346 setTitle(R.string.rtp_state_finding_device);
347 break;
348 case RINGING:
349 setTitle(R.string.rtp_state_ringing);
350 break;
351 case DECLINED_OR_BUSY:
352 setTitle(R.string.rtp_state_declined_or_busy);
353 break;
354 case CONNECTIVITY_ERROR:
355 setTitle(R.string.rtp_state_connectivity_error);
356 break;
357 case APPLICATION_ERROR:
358 setTitle(R.string.rtp_state_application_failure);
359 break;
360 case ENDED:
361 throw new IllegalStateException("Activity should have called finishAndReleaseWakeLock();");
362 default:
363 throw new IllegalStateException(String.format("State %s has not been handled in UI", state));
364 }
365 }
366
367 private Set<Media> getMedia() {
368 return requireRtpConnection().getMedia();
369 }
370
371 @SuppressLint("RestrictedApi")
372 private void updateButtonConfiguration(final RtpEndUserState state) {
373 if (state == RtpEndUserState.INCOMING_CALL) {
374 this.binding.rejectCall.setOnClickListener(this::rejectCall);
375 this.binding.rejectCall.setImageResource(R.drawable.ic_call_end_white_48dp);
376 this.binding.rejectCall.setVisibility(View.VISIBLE);
377 this.binding.endCall.setVisibility(View.INVISIBLE);
378 this.binding.acceptCall.setOnClickListener(this::acceptCall);
379 this.binding.acceptCall.setImageResource(R.drawable.ic_call_white_48dp);
380 this.binding.acceptCall.setVisibility(View.VISIBLE);
381 } else if (state == RtpEndUserState.ENDING_CALL) {
382 this.binding.rejectCall.setVisibility(View.INVISIBLE);
383 this.binding.endCall.setVisibility(View.INVISIBLE);
384 this.binding.acceptCall.setVisibility(View.INVISIBLE);
385 } else if (state == RtpEndUserState.DECLINED_OR_BUSY) {
386 this.binding.rejectCall.setVisibility(View.INVISIBLE);
387 this.binding.endCall.setOnClickListener(this::exit);
388 this.binding.endCall.setImageResource(R.drawable.ic_clear_white_48dp);
389 this.binding.endCall.setVisibility(View.VISIBLE);
390 this.binding.acceptCall.setVisibility(View.INVISIBLE);
391 } else if (state == RtpEndUserState.CONNECTIVITY_ERROR || state == RtpEndUserState.APPLICATION_ERROR) {
392 this.binding.rejectCall.setOnClickListener(this::exit);
393 this.binding.rejectCall.setImageResource(R.drawable.ic_clear_white_48dp);
394 this.binding.rejectCall.setVisibility(View.VISIBLE);
395 this.binding.endCall.setVisibility(View.INVISIBLE);
396 this.binding.acceptCall.setOnClickListener(this::retry);
397 this.binding.acceptCall.setImageResource(R.drawable.ic_replay_white_48dp);
398 this.binding.acceptCall.setVisibility(View.VISIBLE);
399 } else {
400 this.binding.rejectCall.setVisibility(View.INVISIBLE);
401 this.binding.endCall.setOnClickListener(this::endCall);
402 this.binding.endCall.setImageResource(R.drawable.ic_call_end_white_48dp);
403 this.binding.endCall.setVisibility(View.VISIBLE);
404 this.binding.acceptCall.setVisibility(View.INVISIBLE);
405 }
406 updateInCallButtonConfiguration(state);
407 }
408
409 private void updateInCallButtonConfiguration() {
410 updateInCallButtonConfiguration(requireRtpConnection().getEndUserState());
411 }
412
413 @SuppressLint("RestrictedApi")
414 private void updateInCallButtonConfiguration(final RtpEndUserState state) {
415 if (state == RtpEndUserState.CONNECTED) {
416 if (getMedia().contains(Media.VIDEO)) {
417 updateInCallButtonConfigurationVideo(requireRtpConnection().isVideoEnabled());
418 } else {
419 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
420 updateInCallButtonConfigurationSpeaker(
421 audioManager.getSelectedAudioDevice(),
422 audioManager.getAudioDevices().size()
423 );
424 }
425 updateInCallButtonConfigurationMicrophone(requireRtpConnection().isMicrophoneEnabled());
426 } else {
427 this.binding.inCallActionLeft.setVisibility(View.GONE);
428 this.binding.inCallActionRight.setVisibility(View.GONE);
429 }
430 }
431
432 @SuppressLint("RestrictedApi")
433 private void updateInCallButtonConfigurationSpeaker(final AppRTCAudioManager.AudioDevice selectedAudioDevice, final int numberOfChoices) {
434 switch (selectedAudioDevice) {
435 case EARPIECE:
436 this.binding.inCallActionRight.setImageResource(R.drawable.ic_volume_off_black_24dp);
437 if (numberOfChoices >= 2) {
438 this.binding.inCallActionRight.setOnClickListener(this::switchToSpeaker);
439 } else {
440 this.binding.inCallActionRight.setOnClickListener(null);
441 this.binding.inCallActionRight.setClickable(false);
442 }
443 break;
444 case WIRED_HEADSET:
445 this.binding.inCallActionRight.setImageResource(R.drawable.ic_headset_black_24dp);
446 this.binding.inCallActionRight.setOnClickListener(null);
447 this.binding.inCallActionRight.setClickable(false);
448 break;
449 case SPEAKER_PHONE:
450 this.binding.inCallActionRight.setImageResource(R.drawable.ic_volume_up_black_24dp);
451 if (numberOfChoices >= 2) {
452 this.binding.inCallActionRight.setOnClickListener(this::switchToEarpiece);
453 } else {
454 this.binding.inCallActionRight.setOnClickListener(null);
455 this.binding.inCallActionRight.setClickable(false);
456 }
457 break;
458 case BLUETOOTH:
459 this.binding.inCallActionRight.setImageResource(R.drawable.ic_bluetooth_audio_black_24dp);
460 this.binding.inCallActionRight.setOnClickListener(null);
461 this.binding.inCallActionRight.setClickable(false);
462 break;
463 }
464 this.binding.inCallActionRight.setVisibility(View.VISIBLE);
465 }
466
467 @SuppressLint("RestrictedApi")
468 private void updateInCallButtonConfigurationVideo(final boolean videoEnabled) {
469 this.binding.inCallActionRight.setVisibility(View.VISIBLE);
470 if (videoEnabled) {
471 this.binding.inCallActionRight.setImageResource(R.drawable.ic_videocam_black_24dp);
472 this.binding.inCallActionRight.setOnClickListener(this::disableVideo);
473 } else {
474 this.binding.inCallActionRight.setImageResource(R.drawable.ic_videocam_off_black_24dp);
475 this.binding.inCallActionRight.setOnClickListener(this::enableVideo);
476 }
477 }
478
479 private void enableVideo(View view) {
480 requireRtpConnection().setVideoEnabled(true);
481 updateInCallButtonConfigurationVideo(true);
482 }
483
484 private void disableVideo(View view) {
485 requireRtpConnection().setVideoEnabled(false);
486 updateInCallButtonConfigurationVideo(false);
487
488 }
489
490 @SuppressLint("RestrictedApi")
491 private void updateInCallButtonConfigurationMicrophone(final boolean microphoneEnabled) {
492 if (microphoneEnabled) {
493 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_mic_black_24dp);
494 this.binding.inCallActionLeft.setOnClickListener(this::disableMicrophone);
495 } else {
496 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_mic_off_black_24dp);
497 this.binding.inCallActionLeft.setOnClickListener(this::enableMicrophone);
498 }
499 this.binding.inCallActionLeft.setVisibility(View.VISIBLE);
500 }
501
502 private void updateVideoViews() {
503 final Optional<VideoTrack> localVideoTrack = requireRtpConnection().geLocalVideoTrack();
504 if (localVideoTrack.isPresent()) {
505 ensureSurfaceViewRendererIsSetup(binding.localVideo);
506 //paint local view over remote view
507 binding.localVideo.setZOrderMediaOverlay(true);
508 binding.localVideo.setMirror(true);
509 localVideoTrack.get().addSink(binding.localVideo);
510 } else {
511 binding.localVideo.setVisibility(View.GONE);
512 }
513 final Optional<VideoTrack> remoteVideoTrack = requireRtpConnection().getRemoteVideoTrack();
514 if (remoteVideoTrack.isPresent()) {
515 ensureSurfaceViewRendererIsSetup(binding.remoteVideo);
516 remoteVideoTrack.get().addSink(binding.remoteVideo);
517 } else {
518 binding.remoteVideo.setVisibility(View.GONE);
519 }
520 }
521
522 private void disableMicrophone(View view) {
523 JingleRtpConnection rtpConnection = requireRtpConnection();
524 rtpConnection.setMicrophoneEnabled(false);
525 updateInCallButtonConfiguration();
526 }
527
528 private void enableMicrophone(View view) {
529 JingleRtpConnection rtpConnection = requireRtpConnection();
530 rtpConnection.setMicrophoneEnabled(true);
531 updateInCallButtonConfiguration();
532 }
533
534 private void switchToEarpiece(View view) {
535 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE);
536 acquireProximityWakeLock();
537 }
538
539 private void switchToSpeaker(View view) {
540 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE);
541 releaseProximityWakeLock();
542 }
543
544 private void retry(View view) {
545 Log.d(Config.LOGTAG, "attempting retry");
546 final Intent intent = getIntent();
547 final Account account = extractAccount(intent);
548 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
549 this.rtpConnectionReference = null;
550 proposeJingleRtpSession(account, with, ImmutableSet.of(Media.AUDIO, Media.VIDEO));
551 }
552
553 private void exit(View view) {
554 finish();
555 }
556
557 private Contact getWith() {
558 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
559 final Account account = id.account;
560 return account.getRoster().getContact(id.with);
561 }
562
563 private JingleRtpConnection requireRtpConnection() {
564 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
565 if (connection == null) {
566 throw new IllegalStateException("No RTP connection found");
567 }
568 return connection;
569 }
570
571 @Override
572 public void onJingleRtpConnectionUpdate(Account account, Jid with, final String sessionId, RtpEndUserState state) {
573 if (Arrays.asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.DECLINED_OR_BUSY).contains(state)) {
574 releaseProximityWakeLock();
575 }
576 Log.d(Config.LOGTAG, "onJingleRtpConnectionUpdate(" + state + ")");
577 if (with.isBareJid()) {
578 updateRtpSessionProposalState(account, with, state);
579 return;
580 }
581 if (this.rtpConnectionReference == null) {
582 //this happens when going from proposed session to actual session
583 reInitializeActivityWithRunningRapSession(account, with, sessionId);
584 return;
585 }
586 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
587 if (account == id.account && id.with.equals(with) && id.sessionId.equals(sessionId)) {
588 if (state == RtpEndUserState.ENDED) {
589 finish();
590 return;
591 } else if (asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.CONNECTIVITY_ERROR).contains(state)) {
592 //todo remember if we were video
593 resetIntent(account, with, state);
594 }
595 runOnUiThread(() -> {
596 updateStateDisplay(state);
597 updateButtonConfiguration(state);
598 //TODO kill video when in final or error stages
599 updateVideoViews();
600 });
601 } else {
602 Log.d(Config.LOGTAG, "received update for other rtp session");
603 //TODO if we only ever have one; we might just switch over? Maybe!
604 }
605 }
606
607 @Override
608 public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
609 Log.d(Config.LOGTAG, "onAudioDeviceChanged in activity: selected:" + selectedAudioDevice + ", available:" + availableAudioDevices);
610 try {
611 if (requireRtpConnection().getEndUserState() == RtpEndUserState.CONNECTED && !getMedia().contains(Media.VIDEO)) {
612 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
613 updateInCallButtonConfigurationSpeaker(
614 audioManager.getSelectedAudioDevice(),
615 audioManager.getAudioDevices().size()
616 );
617 }
618 putProximityWakeLockInProperState();
619 } catch (IllegalStateException e) {
620 Log.d(Config.LOGTAG, "RTP connection was not available when audio device changed");
621 }
622 }
623
624 private void updateRtpSessionProposalState(final Account account, final Jid with, final RtpEndUserState state) {
625 final Intent currentIntent = getIntent();
626 final String withExtra = currentIntent == null ? null : currentIntent.getStringExtra(EXTRA_WITH);
627 if (withExtra == null) {
628 return;
629 }
630 if (Jid.ofEscaped(withExtra).asBareJid().equals(with)) {
631 runOnUiThread(() -> {
632 updateStateDisplay(state);
633 updateButtonConfiguration(state);
634 });
635 resetIntent(account, with, state);
636 }
637 }
638
639 private void resetIntent(final Bundle extras) {
640 final Intent intent = new Intent(Intent.ACTION_VIEW);
641 intent.putExtras(extras);
642 setIntent(intent);
643 }
644
645 private void resetIntent(final Account account, Jid with, final RtpEndUserState state) {
646 final Intent intent = new Intent(Intent.ACTION_VIEW);
647 intent.putExtra(EXTRA_WITH, with.asBareJid().toEscapedString());
648 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
649 intent.putExtra(EXTRA_LAST_REPORTED_STATE, state.toString());
650 setIntent(intent);
651 }
652}