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 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
414 updateInCallButtonConfiguration(
415 audioManager.getSelectedAudioDevice(),
416 audioManager.getAudioDevices().size(),
417 requireRtpConnection().isMicrophoneEnabled()
418 );
419 } else {
420 this.binding.inCallActionLeft.setVisibility(View.GONE);
421 this.binding.inCallActionRight.setVisibility(View.GONE);
422 }
423 }
424
425 @SuppressLint("RestrictedApi")
426 private void updateInCallButtonConfiguration(final AppRTCAudioManager.AudioDevice selectedAudioDevice, final int numberOfChoices, final boolean microphoneEnabled) {
427 switch (selectedAudioDevice) {
428 case EARPIECE:
429 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_volume_off_black_24dp);
430 if (numberOfChoices >= 2) {
431 this.binding.inCallActionLeft.setOnClickListener(this::switchToSpeaker);
432 } else {
433 this.binding.inCallActionLeft.setOnClickListener(null);
434 this.binding.inCallActionLeft.setClickable(false);
435 }
436 break;
437 case WIRED_HEADSET:
438 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_headset_black_24dp);
439 this.binding.inCallActionLeft.setOnClickListener(null);
440 this.binding.inCallActionLeft.setClickable(false);
441 break;
442 case SPEAKER_PHONE:
443 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_volume_up_black_24dp);
444 if (numberOfChoices >= 2) {
445 this.binding.inCallActionLeft.setOnClickListener(this::switchToEarpiece);
446 } else {
447 this.binding.inCallActionLeft.setOnClickListener(null);
448 this.binding.inCallActionLeft.setClickable(false);
449 }
450 break;
451 case BLUETOOTH:
452 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_bluetooth_audio_black_24dp);
453 this.binding.inCallActionLeft.setOnClickListener(null);
454 this.binding.inCallActionLeft.setClickable(false);
455 break;
456 }
457 this.binding.inCallActionLeft.setVisibility(View.VISIBLE);
458 if (microphoneEnabled) {
459 this.binding.inCallActionRight.setImageResource(R.drawable.ic_mic_black_24dp);
460 this.binding.inCallActionRight.setOnClickListener(this::disableMicrophone);
461 } else {
462 this.binding.inCallActionRight.setImageResource(R.drawable.ic_mic_off_black_24dp);
463 this.binding.inCallActionRight.setOnClickListener(this::enableMicrophone);
464 }
465 this.binding.inCallActionRight.setVisibility(View.VISIBLE);
466 }
467
468 private void updateVideoViews() {
469 final Optional<VideoTrack> localVideoTrack = requireRtpConnection().geLocalVideoTrack();
470 if (localVideoTrack.isPresent()) {
471 ensureSurfaceViewRendererIsSetup(binding.localVideo);
472 //paint local view over remote view
473 binding.localVideo.setZOrderMediaOverlay(true);
474 binding.localVideo.setMirror(true);
475 localVideoTrack.get().addSink(binding.localVideo);
476 } else {
477 binding.localVideo.setVisibility(View.GONE);
478 }
479 final Optional<VideoTrack> remoteVideoTrack = requireRtpConnection().getRemoteVideoTrack();
480 if (remoteVideoTrack.isPresent()) {
481 ensureSurfaceViewRendererIsSetup(binding.remoteVideo);
482 remoteVideoTrack.get().addSink(binding.remoteVideo);
483 } else {
484 binding.remoteVideo.setVisibility(View.GONE);
485 }
486 }
487
488 private void disableMicrophone(View view) {
489 JingleRtpConnection rtpConnection = requireRtpConnection();
490 rtpConnection.setMicrophoneEnabled(false);
491 updateInCallButtonConfiguration();
492 }
493
494 private void enableMicrophone(View view) {
495 JingleRtpConnection rtpConnection = requireRtpConnection();
496 rtpConnection.setMicrophoneEnabled(true);
497 updateInCallButtonConfiguration();
498 }
499
500 private void switchToEarpiece(View view) {
501 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE);
502 acquireProximityWakeLock();
503 }
504
505 private void switchToSpeaker(View view) {
506 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE);
507 releaseProximityWakeLock();
508 }
509
510 private void retry(View view) {
511 Log.d(Config.LOGTAG, "attempting retry");
512 final Intent intent = getIntent();
513 final Account account = extractAccount(intent);
514 final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
515 this.rtpConnectionReference = null;
516 proposeJingleRtpSession(account, with, ImmutableSet.of(Media.AUDIO, Media.VIDEO));
517 }
518
519 private void exit(View view) {
520 finish();
521 }
522
523 private Contact getWith() {
524 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
525 final Account account = id.account;
526 return account.getRoster().getContact(id.with);
527 }
528
529 private JingleRtpConnection requireRtpConnection() {
530 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
531 if (connection == null) {
532 throw new IllegalStateException("No RTP connection found");
533 }
534 return connection;
535 }
536
537 @Override
538 public void onJingleRtpConnectionUpdate(Account account, Jid with, final String sessionId, RtpEndUserState state) {
539 if (Arrays.asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.DECLINED_OR_BUSY).contains(state)) {
540 releaseProximityWakeLock();
541 }
542 Log.d(Config.LOGTAG, "onJingleRtpConnectionUpdate(" + state + ")");
543 if (with.isBareJid()) {
544 updateRtpSessionProposalState(account, with, state);
545 return;
546 }
547 if (this.rtpConnectionReference == null) {
548 //this happens when going from proposed session to actual session
549 reInitializeActivityWithRunningRapSession(account, with, sessionId);
550 return;
551 }
552 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
553 if (account == id.account && id.with.equals(with) && id.sessionId.equals(sessionId)) {
554 if (state == RtpEndUserState.ENDED) {
555 finish();
556 return;
557 } else if (asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.CONNECTIVITY_ERROR).contains(state)) {
558 resetIntent(account, with, state);
559 }
560 runOnUiThread(() -> {
561 updateStateDisplay(state);
562 updateButtonConfiguration(state);
563 updateVideoViews();
564 });
565 } else {
566 Log.d(Config.LOGTAG, "received update for other rtp session");
567 //TODO if we only ever have one; we might just switch over? Maybe!
568 }
569 }
570
571 @Override
572 public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
573 Log.d(Config.LOGTAG, "onAudioDeviceChanged in activity: selected:" + selectedAudioDevice + ", available:" + availableAudioDevices);
574 try {
575 if (requireRtpConnection().getEndUserState() == RtpEndUserState.CONNECTED) {
576 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
577 updateInCallButtonConfiguration(
578 audioManager.getSelectedAudioDevice(),
579 audioManager.getAudioDevices().size(),
580 requireRtpConnection().isMicrophoneEnabled()
581 );
582 }
583 putProximityWakeLockInProperState();
584 } catch (IllegalStateException e) {
585 Log.d(Config.LOGTAG, "RTP connection was not available when audio device changed");
586 }
587 }
588
589 private void updateRtpSessionProposalState(final Account account, final Jid with, final RtpEndUserState state) {
590 final Intent currentIntent = getIntent();
591 final String withExtra = currentIntent == null ? null : currentIntent.getStringExtra(EXTRA_WITH);
592 if (withExtra == null) {
593 return;
594 }
595 if (Jid.ofEscaped(withExtra).asBareJid().equals(with)) {
596 runOnUiThread(() -> {
597 updateStateDisplay(state);
598 updateButtonConfiguration(state);
599 });
600 resetIntent(account, with, state);
601 }
602 }
603
604 private void resetIntent(final Bundle extras) {
605 final Intent intent = new Intent(Intent.ACTION_VIEW);
606 intent.putExtras(extras);
607 setIntent(intent);
608 }
609
610 private void resetIntent(final Account account, Jid with, final RtpEndUserState state) {
611 final Intent intent = new Intent(Intent.ACTION_VIEW);
612 intent.putExtra(EXTRA_WITH, with.asBareJid().toEscapedString());
613 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
614 intent.putExtra(EXTRA_LAST_REPORTED_STATE, state.toString());
615 setIntent(intent);
616 }
617}