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 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
128 final JingleRtpConnection rtpConnection = rtpConnectionReference != null ? rtpConnectionReference.get() : null;
129 final AppRTCAudioManager audioManager = rtpConnection == null ? null : rtpConnection.getAudioManager();
130 if (audioManager == null || audioManager.getSelectedAudioDevice() == AppRTCAudioManager.AudioDevice.EARPIECE) {
131 acquireProximityWakeLock();
132 }
133 }
134
135 private void acquireProximityWakeLock() {
136 final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
137 if (powerManager == null) {
138 Log.e(Config.LOGTAG, "power manager not available");
139 return;
140 }
141 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
142 if (this.mProximityWakeLock == null) {
143 this.mProximityWakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, PROXIMITY_WAKE_LOCK_TAG);
144 }
145 if (!this.mProximityWakeLock.isHeld()) {
146 Log.d(Config.LOGTAG, "acquiring proximity wake lock");
147 this.mProximityWakeLock.acquire();
148 }
149 }
150 }
151
152 private void releaseProximityWakeLock() {
153 if (this.mProximityWakeLock != null && mProximityWakeLock.isHeld()) {
154 Log.d(Config.LOGTAG, "releasing proximity wake lock");
155 this.mProximityWakeLock.release();
156 this.mProximityWakeLock = null;
157 }
158 }
159
160 private void putProximityWakeLockInProperState() {
161 if (requireRtpConnection().getAudioManager().getSelectedAudioDevice() == AppRTCAudioManager.AudioDevice.EARPIECE) {
162 acquireProximityWakeLock();
163 } else {
164 releaseProximityWakeLock();
165 }
166 }
167
168 @Override
169 protected void refreshUiReal() {
170
171 }
172
173 @Override
174 public void onNewIntent(final Intent intent) {
175 super.onNewIntent(intent);
176 setIntent(intent);
177 if (xmppConnectionService == null) {
178 Log.d(Config.LOGTAG, "RtpSessionActivity: background service wasn't bound in onNewIntent()");
179 return;
180 }
181 final Account account = extractAccount(intent);
182 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
183 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
184 if (sessionId != null) {
185 Log.d(Config.LOGTAG, "reinitializing from onNewIntent()");
186 initializeActivityWithRunningRtpSession(account, with, sessionId);
187 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
188 Log.d(Config.LOGTAG, "accepting call from onNewIntent()");
189 requestPermissionsAndAcceptCall();
190 resetIntent(intent.getExtras());
191 }
192 } else {
193 throw new IllegalStateException("received onNewIntent without sessionId");
194 }
195 }
196
197 @Override
198 void onBackendConnected() {
199 final Intent intent = getIntent();
200 final String action = intent.getAction();
201 final Account account = extractAccount(intent);
202 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
203 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
204 if (sessionId != null) {
205 initializeActivityWithRunningRtpSession(account, with, sessionId);
206 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
207 Log.d(Config.LOGTAG, "intent action was accept");
208 requestPermissionsAndAcceptCall();
209 resetIntent(intent.getExtras());
210 }
211 } else if (asList(ACTION_MAKE_VIDEO_CALL, ACTION_MAKE_VOICE_CALL).contains(action)) {
212 final Set<Media> media;
213 if (ACTION_MAKE_VIDEO_CALL.equals(action)) {
214 media = ImmutableSet.of(Media.AUDIO, Media.VIDEO);
215 } else {
216 media = ImmutableSet.of(Media.AUDIO);
217 }
218 proposeJingleRtpSession(account, with, media);
219 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
220 } else if (Intent.ACTION_VIEW.equals(action)) {
221 final String extraLastState = intent.getStringExtra(EXTRA_LAST_REPORTED_STATE);
222 if (extraLastState != null) {
223 Log.d(Config.LOGTAG, "restored last state from intent extra");
224 RtpEndUserState state = RtpEndUserState.valueOf(extraLastState);
225 updateButtonConfiguration(state);
226 updateStateDisplay(state);
227 }
228 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
229 }
230 }
231
232 private void proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
233 xmppConnectionService.getJingleConnectionManager().proposeJingleRtpSession(account, with, media);
234 //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
235 putScreenInCallMode();
236 }
237
238 @Override
239 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
240 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
241 if (PermissionUtils.allGranted(grantResults)) {
242 if (requestCode == REQUEST_ACCEPT_CALL) {
243 requireRtpConnection().acceptCall();
244 }
245 } else {
246 @StringRes int res;
247 final String firstDenied = getFirstDenied(grantResults, permissions);
248 if (Manifest.permission.RECORD_AUDIO.equals(firstDenied)) {
249 res = R.string.no_microphone_permission;
250 } else if (Manifest.permission.CAMERA.equals(firstDenied)) {
251 res = R.string.no_camera_permission;
252 } else {
253 throw new IllegalStateException("Invalid permission result request");
254 }
255 Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
256 }
257 }
258
259 @Override
260 public void onStop() {
261 binding.remoteVideo.release();
262 binding.localVideo.release();
263 releaseProximityWakeLock();
264 //TODO maybe we want to finish if call had ended
265 super.onStop();
266 }
267
268 @Override
269 public void onBackPressed() {
270 endCall();
271 super.onBackPressed();
272 }
273
274
275 private void initializeActivityWithRunningRtpSession(final Account account, Jid with, String sessionId) {
276 final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
277 .findJingleRtpConnection(account, with, sessionId);
278 if (reference == null || reference.get() == null) {
279 finish();
280 return;
281 }
282 this.rtpConnectionReference = reference;
283 final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
284 if (currentState == RtpEndUserState.ENDED) {
285 finish();
286 return;
287 }
288 if (currentState == RtpEndUserState.INCOMING_CALL) {
289 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
290 }
291 if (JingleRtpConnection.STATES_SHOWING_ONGOING_CALL.contains(requireRtpConnection().getState())) {
292 putScreenInCallMode();
293 }
294 binding.with.setText(getWith().getDisplayName());
295 updateVideoViews();
296 updateStateDisplay(currentState);
297 updateButtonConfiguration(currentState);
298 }
299
300 private void reInitializeActivityWithRunningRapSession(final Account account, Jid with, String sessionId) {
301 runOnUiThread(() -> {
302 initializeActivityWithRunningRtpSession(account, with, sessionId);
303 });
304 final Intent intent = new Intent(Intent.ACTION_VIEW);
305 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
306 intent.putExtra(EXTRA_WITH, with.toEscapedString());
307 intent.putExtra(EXTRA_SESSION_ID, sessionId);
308 setIntent(intent);
309 }
310
311 private void ensureSurfaceViewRendererIsSetup(final SurfaceViewRenderer surfaceViewRenderer) {
312 surfaceViewRenderer.setVisibility(View.VISIBLE);
313 try {
314 surfaceViewRenderer.init(requireRtpConnection().getEglBaseContext(), null);
315 } catch (IllegalStateException e) {
316 Log.d(Config.LOGTAG, "SurfaceViewRenderer was already initialized");
317 }
318 surfaceViewRenderer.setEnableHardwareScaler(true);
319 }
320
321 private void updateStateDisplay(final RtpEndUserState state) {
322 switch (state) {
323 case INCOMING_CALL:
324 if (getMedia().contains(Media.VIDEO)) {
325 setTitle(R.string.rtp_state_incoming_video_call);
326 } else {
327 setTitle(R.string.rtp_state_incoming_call);
328 }
329 break;
330 case CONNECTING:
331 setTitle(R.string.rtp_state_connecting);
332 break;
333 case CONNECTED:
334 setTitle(R.string.rtp_state_connected);
335 break;
336 case ACCEPTING_CALL:
337 setTitle(R.string.rtp_state_accepting_call);
338 break;
339 case ENDING_CALL:
340 setTitle(R.string.rtp_state_ending_call);
341 break;
342 case FINDING_DEVICE:
343 setTitle(R.string.rtp_state_finding_device);
344 break;
345 case RINGING:
346 setTitle(R.string.rtp_state_ringing);
347 break;
348 case DECLINED_OR_BUSY:
349 setTitle(R.string.rtp_state_declined_or_busy);
350 break;
351 case CONNECTIVITY_ERROR:
352 setTitle(R.string.rtp_state_connectivity_error);
353 break;
354 case APPLICATION_ERROR:
355 setTitle(R.string.rtp_state_application_failure);
356 break;
357 case ENDED:
358 throw new IllegalStateException("Activity should have called finishAndReleaseWakeLock();");
359 default:
360 throw new IllegalStateException(String.format("State %s has not been handled in UI", state));
361 }
362 }
363
364 private Set<Media> getMedia() {
365 return requireRtpConnection().getMedia();
366 }
367
368 @SuppressLint("RestrictedApi")
369 private void updateButtonConfiguration(final RtpEndUserState state) {
370 if (state == RtpEndUserState.INCOMING_CALL) {
371 this.binding.rejectCall.setOnClickListener(this::rejectCall);
372 this.binding.rejectCall.setImageResource(R.drawable.ic_call_end_white_48dp);
373 this.binding.rejectCall.setVisibility(View.VISIBLE);
374 this.binding.endCall.setVisibility(View.INVISIBLE);
375 this.binding.acceptCall.setOnClickListener(this::acceptCall);
376 this.binding.acceptCall.setImageResource(R.drawable.ic_call_white_48dp);
377 this.binding.acceptCall.setVisibility(View.VISIBLE);
378 } else if (state == RtpEndUserState.ENDING_CALL) {
379 this.binding.rejectCall.setVisibility(View.INVISIBLE);
380 this.binding.endCall.setVisibility(View.INVISIBLE);
381 this.binding.acceptCall.setVisibility(View.INVISIBLE);
382 } else if (state == RtpEndUserState.DECLINED_OR_BUSY) {
383 this.binding.rejectCall.setVisibility(View.INVISIBLE);
384 this.binding.endCall.setOnClickListener(this::exit);
385 this.binding.endCall.setImageResource(R.drawable.ic_clear_white_48dp);
386 this.binding.endCall.setVisibility(View.VISIBLE);
387 this.binding.acceptCall.setVisibility(View.INVISIBLE);
388 } else if (state == RtpEndUserState.CONNECTIVITY_ERROR || state == RtpEndUserState.APPLICATION_ERROR) {
389 this.binding.rejectCall.setOnClickListener(this::exit);
390 this.binding.rejectCall.setImageResource(R.drawable.ic_clear_white_48dp);
391 this.binding.rejectCall.setVisibility(View.VISIBLE);
392 this.binding.endCall.setVisibility(View.INVISIBLE);
393 this.binding.acceptCall.setOnClickListener(this::retry);
394 this.binding.acceptCall.setImageResource(R.drawable.ic_replay_white_48dp);
395 this.binding.acceptCall.setVisibility(View.VISIBLE);
396 } else {
397 this.binding.rejectCall.setVisibility(View.INVISIBLE);
398 this.binding.endCall.setOnClickListener(this::endCall);
399 this.binding.endCall.setImageResource(R.drawable.ic_call_end_white_48dp);
400 this.binding.endCall.setVisibility(View.VISIBLE);
401 this.binding.acceptCall.setVisibility(View.INVISIBLE);
402 }
403 updateInCallButtonConfiguration(state);
404 }
405
406 private void updateInCallButtonConfiguration() {
407 updateInCallButtonConfiguration(requireRtpConnection().getEndUserState());
408 }
409
410 @SuppressLint("RestrictedApi")
411 private void updateInCallButtonConfiguration(final RtpEndUserState state) {
412 if (state == RtpEndUserState.CONNECTED) {
413 if (getMedia().contains(Media.VIDEO)) {
414 updateInCallButtonConfigurationVideo(requireRtpConnection().isVideoEnabled());
415 } else {
416 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
417 updateInCallButtonConfigurationSpeaker(
418 audioManager.getSelectedAudioDevice(),
419 audioManager.getAudioDevices().size()
420 );
421 }
422 updateInCallButtonConfigurationMicrophone(requireRtpConnection().isMicrophoneEnabled());
423 } else {
424 this.binding.inCallActionLeft.setVisibility(View.GONE);
425 this.binding.inCallActionRight.setVisibility(View.GONE);
426 }
427 }
428
429 @SuppressLint("RestrictedApi")
430 private void updateInCallButtonConfigurationSpeaker(final AppRTCAudioManager.AudioDevice selectedAudioDevice, final int numberOfChoices) {
431 switch (selectedAudioDevice) {
432 case EARPIECE:
433 this.binding.inCallActionRight.setImageResource(R.drawable.ic_volume_off_black_24dp);
434 if (numberOfChoices >= 2) {
435 this.binding.inCallActionRight.setOnClickListener(this::switchToSpeaker);
436 } else {
437 this.binding.inCallActionRight.setOnClickListener(null);
438 this.binding.inCallActionRight.setClickable(false);
439 }
440 break;
441 case WIRED_HEADSET:
442 this.binding.inCallActionRight.setImageResource(R.drawable.ic_headset_black_24dp);
443 this.binding.inCallActionRight.setOnClickListener(null);
444 this.binding.inCallActionRight.setClickable(false);
445 break;
446 case SPEAKER_PHONE:
447 this.binding.inCallActionRight.setImageResource(R.drawable.ic_volume_up_black_24dp);
448 if (numberOfChoices >= 2) {
449 this.binding.inCallActionRight.setOnClickListener(this::switchToEarpiece);
450 } else {
451 this.binding.inCallActionRight.setOnClickListener(null);
452 this.binding.inCallActionRight.setClickable(false);
453 }
454 break;
455 case BLUETOOTH:
456 this.binding.inCallActionRight.setImageResource(R.drawable.ic_bluetooth_audio_black_24dp);
457 this.binding.inCallActionRight.setOnClickListener(null);
458 this.binding.inCallActionRight.setClickable(false);
459 break;
460 }
461 this.binding.inCallActionRight.setVisibility(View.VISIBLE);
462 }
463
464 @SuppressLint("RestrictedApi")
465 private void updateInCallButtonConfigurationVideo(final boolean videoEnabled) {
466 this.binding.inCallActionRight.setVisibility(View.VISIBLE);
467 if (videoEnabled) {
468 this.binding.inCallActionRight.setImageResource(R.drawable.ic_videocam_black_24dp);
469 this.binding.inCallActionRight.setOnClickListener(this::disableVideo);
470 } else {
471 this.binding.inCallActionRight.setImageResource(R.drawable.ic_videocam_off_black_24dp);
472 this.binding.inCallActionRight.setOnClickListener(this::enableVideo);
473 }
474 }
475
476 private void enableVideo(View view) {
477 requireRtpConnection().setVideoEnabled(true);
478 updateInCallButtonConfigurationVideo(true);
479 }
480
481 private void disableVideo(View view) {
482 requireRtpConnection().setVideoEnabled(false);
483 updateInCallButtonConfigurationVideo(false);
484
485 }
486
487 @SuppressLint("RestrictedApi")
488 private void updateInCallButtonConfigurationMicrophone(final boolean microphoneEnabled) {
489 if (microphoneEnabled) {
490 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_mic_black_24dp);
491 this.binding.inCallActionLeft.setOnClickListener(this::disableMicrophone);
492 } else {
493 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_mic_off_black_24dp);
494 this.binding.inCallActionLeft.setOnClickListener(this::enableMicrophone);
495 }
496 this.binding.inCallActionLeft.setVisibility(View.VISIBLE);
497 }
498
499 private void updateVideoViews() {
500 final Optional<VideoTrack> localVideoTrack = requireRtpConnection().geLocalVideoTrack();
501 if (localVideoTrack.isPresent()) {
502 ensureSurfaceViewRendererIsSetup(binding.localVideo);
503 //paint local view over remote view
504 binding.localVideo.setZOrderMediaOverlay(true);
505 binding.localVideo.setMirror(true);
506 localVideoTrack.get().addSink(binding.localVideo);
507 } else {
508 binding.localVideo.setVisibility(View.GONE);
509 }
510 final Optional<VideoTrack> remoteVideoTrack = requireRtpConnection().getRemoteVideoTrack();
511 if (remoteVideoTrack.isPresent()) {
512 ensureSurfaceViewRendererIsSetup(binding.remoteVideo);
513 remoteVideoTrack.get().addSink(binding.remoteVideo);
514 } else {
515 binding.remoteVideo.setVisibility(View.GONE);
516 }
517 }
518
519 private void disableMicrophone(View view) {
520 JingleRtpConnection rtpConnection = requireRtpConnection();
521 rtpConnection.setMicrophoneEnabled(false);
522 updateInCallButtonConfiguration();
523 }
524
525 private void enableMicrophone(View view) {
526 JingleRtpConnection rtpConnection = requireRtpConnection();
527 rtpConnection.setMicrophoneEnabled(true);
528 updateInCallButtonConfiguration();
529 }
530
531 private void switchToEarpiece(View view) {
532 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE);
533 acquireProximityWakeLock();
534 }
535
536 private void switchToSpeaker(View view) {
537 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE);
538 releaseProximityWakeLock();
539 }
540
541 private void retry(View view) {
542 Log.d(Config.LOGTAG, "attempting retry");
543 final Intent intent = getIntent();
544 final Account account = extractAccount(intent);
545 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
546 this.rtpConnectionReference = null;
547 proposeJingleRtpSession(account, with, ImmutableSet.of(Media.AUDIO, Media.VIDEO));
548 }
549
550 private void exit(View view) {
551 finish();
552 }
553
554 private Contact getWith() {
555 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
556 final Account account = id.account;
557 return account.getRoster().getContact(id.with);
558 }
559
560 private JingleRtpConnection requireRtpConnection() {
561 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
562 if (connection == null) {
563 throw new IllegalStateException("No RTP connection found");
564 }
565 return connection;
566 }
567
568 @Override
569 public void onJingleRtpConnectionUpdate(Account account, Jid with, final String sessionId, RtpEndUserState state) {
570 if (Arrays.asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.DECLINED_OR_BUSY).contains(state)) {
571 releaseProximityWakeLock();
572 }
573 Log.d(Config.LOGTAG, "onJingleRtpConnectionUpdate(" + state + ")");
574 if (with.isBareJid()) {
575 updateRtpSessionProposalState(account, with, state);
576 return;
577 }
578 if (this.rtpConnectionReference == null) {
579 //this happens when going from proposed session to actual session
580 reInitializeActivityWithRunningRapSession(account, with, sessionId);
581 return;
582 }
583 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
584 if (account == id.account && id.with.equals(with) && id.sessionId.equals(sessionId)) {
585 if (state == RtpEndUserState.ENDED) {
586 finish();
587 return;
588 } else if (asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.CONNECTIVITY_ERROR).contains(state)) {
589 resetIntent(account, with, state);
590 }
591 runOnUiThread(() -> {
592 updateStateDisplay(state);
593 updateButtonConfiguration(state);
594 updateVideoViews();
595 });
596 } else {
597 Log.d(Config.LOGTAG, "received update for other rtp session");
598 //TODO if we only ever have one; we might just switch over? Maybe!
599 }
600 }
601
602 @Override
603 public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
604 Log.d(Config.LOGTAG, "onAudioDeviceChanged in activity: selected:" + selectedAudioDevice + ", available:" + availableAudioDevices);
605 try {
606 if (requireRtpConnection().getEndUserState() == RtpEndUserState.CONNECTED && !getMedia().contains(Media.VIDEO)) {
607 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
608 updateInCallButtonConfigurationSpeaker(
609 audioManager.getSelectedAudioDevice(),
610 audioManager.getAudioDevices().size()
611 );
612 }
613 putProximityWakeLockInProperState();
614 } catch (IllegalStateException e) {
615 Log.d(Config.LOGTAG, "RTP connection was not available when audio device changed");
616 }
617 }
618
619 private void updateRtpSessionProposalState(final Account account, final Jid with, final RtpEndUserState state) {
620 final Intent currentIntent = getIntent();
621 final String withExtra = currentIntent == null ? null : currentIntent.getStringExtra(EXTRA_WITH);
622 if (withExtra == null) {
623 return;
624 }
625 if (Jid.ofEscaped(withExtra).asBareJid().equals(with)) {
626 runOnUiThread(() -> {
627 updateStateDisplay(state);
628 updateButtonConfiguration(state);
629 });
630 resetIntent(account, with, state);
631 }
632 }
633
634 private void resetIntent(final Bundle extras) {
635 final Intent intent = new Intent(Intent.ACTION_VIEW);
636 intent.putExtras(extras);
637 setIntent(intent);
638 }
639
640 private void resetIntent(final Account account, Jid with, final RtpEndUserState state) {
641 final Intent intent = new Intent(Intent.ACTION_VIEW);
642 intent.putExtra(EXTRA_WITH, with.asBareJid().toEscapedString());
643 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
644 intent.putExtra(EXTRA_LAST_REPORTED_STATE, state.toString());
645 setIntent(intent);
646 }
647}