1package eu.siacs.conversations.ui;
2
3import android.Manifest;
4import android.annotation.SuppressLint;
5import android.app.PictureInPictureParams;
6import android.content.ActivityNotFoundException;
7import android.content.Context;
8import android.content.Intent;
9import android.content.pm.PackageManager;
10import android.databinding.DataBindingUtil;
11import android.os.Build;
12import android.os.Bundle;
13import android.os.Handler;
14import android.os.PowerManager;
15import android.os.SystemClock;
16import android.support.annotation.NonNull;
17import android.support.annotation.RequiresApi;
18import android.support.annotation.StringRes;
19import android.util.Log;
20import android.util.Rational;
21import android.view.Menu;
22import android.view.MenuItem;
23import android.view.View;
24import android.view.WindowManager;
25import android.widget.Toast;
26
27import com.google.common.base.Optional;
28import com.google.common.base.Preconditions;
29import com.google.common.base.Throwables;
30import com.google.common.collect.ImmutableList;
31import com.google.common.collect.ImmutableSet;
32import com.google.common.util.concurrent.FutureCallback;
33import com.google.common.util.concurrent.Futures;
34
35import org.checkerframework.checker.nullness.compatqual.NullableDecl;
36import org.webrtc.SurfaceViewRenderer;
37import org.webrtc.VideoTrack;
38
39import java.lang.ref.WeakReference;
40import java.util.Arrays;
41import java.util.Collections;
42import java.util.List;
43import java.util.Set;
44
45import eu.siacs.conversations.Config;
46import eu.siacs.conversations.R;
47import eu.siacs.conversations.databinding.ActivityRtpSessionBinding;
48import eu.siacs.conversations.entities.Account;
49import eu.siacs.conversations.entities.Contact;
50import eu.siacs.conversations.entities.Conversation;
51import eu.siacs.conversations.services.AppRTCAudioManager;
52import eu.siacs.conversations.services.XmppConnectionService;
53import eu.siacs.conversations.ui.util.AvatarWorkerTask;
54import eu.siacs.conversations.ui.util.MainThreadExecutor;
55import eu.siacs.conversations.utils.PermissionUtils;
56import eu.siacs.conversations.utils.TimeFrameUtils;
57import eu.siacs.conversations.xml.Namespace;
58import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
59import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
60import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
61import eu.siacs.conversations.xmpp.jingle.Media;
62import eu.siacs.conversations.xmpp.jingle.RtpEndUserState;
63import eu.siacs.conversations.xmpp.Jid;
64
65import static eu.siacs.conversations.utils.PermissionUtils.getFirstDenied;
66import static java.util.Arrays.asList;
67
68public class RtpSessionActivity extends XmppActivity implements XmppConnectionService.OnJingleRtpConnectionUpdate {
69
70 public static final String EXTRA_WITH = "with";
71 public static final String EXTRA_SESSION_ID = "session_id";
72 public static final String EXTRA_LAST_REPORTED_STATE = "last_reported_state";
73 public static final String EXTRA_LAST_ACTION = "last_action";
74 public static final String ACTION_ACCEPT_CALL = "action_accept_call";
75 public static final String ACTION_MAKE_VOICE_CALL = "action_make_voice_call";
76 public static final String ACTION_MAKE_VIDEO_CALL = "action_make_video_call";
77
78 private static final int CALL_DURATION_UPDATE_INTERVAL = 333;
79
80 private static final List<RtpEndUserState> END_CARD = Arrays.asList(
81 RtpEndUserState.APPLICATION_ERROR,
82 RtpEndUserState.DECLINED_OR_BUSY,
83 RtpEndUserState.CONNECTIVITY_ERROR,
84 RtpEndUserState.CONNECTIVITY_LOST_ERROR,
85 RtpEndUserState.RETRACTED
86 );
87 private static final List<RtpEndUserState> STATES_SHOWING_HELP_BUTTON = Arrays.asList(
88 RtpEndUserState.APPLICATION_ERROR,
89 RtpEndUserState.CONNECTIVITY_ERROR
90 );
91 private static final String PROXIMITY_WAKE_LOCK_TAG = "conversations:in-rtp-session";
92 private static final int REQUEST_ACCEPT_CALL = 0x1111;
93 private WeakReference<JingleRtpConnection> rtpConnectionReference;
94
95 private ActivityRtpSessionBinding binding;
96 private PowerManager.WakeLock mProximityWakeLock;
97
98 private Handler mHandler = new Handler();
99 private Runnable mTickExecutor = new Runnable() {
100 @Override
101 public void run() {
102 updateCallDuration();
103 mHandler.postDelayed(mTickExecutor, CALL_DURATION_UPDATE_INTERVAL);
104 }
105 };
106
107 private static Set<Media> actionToMedia(final String action) {
108 if (ACTION_MAKE_VIDEO_CALL.equals(action)) {
109 return ImmutableSet.of(Media.AUDIO, Media.VIDEO);
110 } else {
111 return ImmutableSet.of(Media.AUDIO);
112 }
113 }
114
115 private static void addSink(final VideoTrack videoTrack, final SurfaceViewRenderer surfaceViewRenderer) {
116 try {
117 videoTrack.addSink(surfaceViewRenderer);
118 } catch (final IllegalStateException e) {
119 Log.e(Config.LOGTAG, "possible race condition on trying to display video track. ignoring", e);
120 }
121 }
122
123 @Override
124 public void onCreate(Bundle savedInstanceState) {
125 super.onCreate(savedInstanceState);
126 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
127 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
128 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
129 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
130 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
131 setSupportActionBar(binding.toolbar);
132 }
133
134 @Override
135 public boolean onCreateOptionsMenu(final Menu menu) {
136 getMenuInflater().inflate(R.menu.activity_rtp_session, menu);
137 final MenuItem help = menu.findItem(R.id.action_help);
138 help.setVisible(isHelpButtonVisible());
139 return super.onCreateOptionsMenu(menu);
140 }
141
142 private boolean isHelpButtonVisible() {
143 try {
144 return STATES_SHOWING_HELP_BUTTON.contains(requireRtpConnection().getEndUserState());
145 } catch (IllegalStateException e) {
146 final Intent intent = getIntent();
147 final String state = intent != null ? intent.getStringExtra(EXTRA_LAST_REPORTED_STATE) : null;
148 if (state != null) {
149 return STATES_SHOWING_HELP_BUTTON.contains(RtpEndUserState.valueOf(state));
150 } else {
151 return false;
152 }
153 }
154 }
155
156 public boolean onOptionsItemSelected(final MenuItem item) {
157 if (item.getItemId() == R.id.action_help) {
158 launchHelpInBrowser();
159 return true;
160 }
161 return super.onOptionsItemSelected(item);
162 }
163
164 private void launchHelpInBrowser() {
165 final Intent intent = new Intent(Intent.ACTION_VIEW, Config.HELP);
166 try {
167 startActivity(intent);
168 } catch (final ActivityNotFoundException e) {
169 Toast.makeText(this, R.string.no_application_found_to_open_link, Toast.LENGTH_LONG).show();
170 }
171 }
172
173 private void endCall(View view) {
174 endCall();
175 }
176
177 private void endCall() {
178 if (this.rtpConnectionReference == null) {
179 retractSessionProposal();
180 finish();
181 } else {
182 requireRtpConnection().endCall();
183 }
184 }
185
186 private void retractSessionProposal() {
187 final Intent intent = getIntent();
188 final String action = intent.getAction();
189 final Account account = extractAccount(intent);
190 final Jid with = Jid.ofEscaped(intent.getStringExtra(EXTRA_WITH));
191 final String state = intent.getStringExtra(EXTRA_LAST_REPORTED_STATE);
192 if (!Intent.ACTION_VIEW.equals(action) || state == null || !END_CARD.contains(RtpEndUserState.valueOf(state))) {
193 resetIntent(account, with, RtpEndUserState.RETRACTED, actionToMedia(intent.getAction()));
194 }
195 xmppConnectionService.getJingleConnectionManager().retractSessionProposal(account, with.asBareJid());
196 }
197
198 private void rejectCall(View view) {
199 requireRtpConnection().rejectCall();
200 finish();
201 }
202
203 private void acceptCall(View view) {
204 requestPermissionsAndAcceptCall();
205 }
206
207 private void requestPermissionsAndAcceptCall() {
208 final List<String> permissions;
209 if (getMedia().contains(Media.VIDEO)) {
210 permissions = ImmutableList.of(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO);
211 } else {
212 permissions = ImmutableList.of(Manifest.permission.RECORD_AUDIO);
213 }
214 if (PermissionUtils.hasPermission(this, permissions, REQUEST_ACCEPT_CALL)) {
215 putScreenInCallMode();
216 checkRecorderAndAcceptCall();
217 }
218 }
219
220 private void checkRecorderAndAcceptCall() {
221 checkMicrophoneAvailability();
222 try {
223 requireRtpConnection().acceptCall();
224 } catch (final IllegalStateException e) {
225 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
226 }
227 }
228
229 private void checkMicrophoneAvailability() {
230 new Thread(() -> {
231 final long start = SystemClock.elapsedRealtime();
232 final boolean isMicrophoneAvailable = AppRTCAudioManager.isMicrophoneAvailable();
233 final long stop = SystemClock.elapsedRealtime();
234 Log.d(Config.LOGTAG, "checking microphone availability took " + (stop - start) + "ms");
235 if (isMicrophoneAvailable) {
236 return;
237 }
238 runOnUiThread(() -> Toast.makeText(this, R.string.microphone_unavailable, Toast.LENGTH_LONG).show());
239 }
240 ).start();
241 }
242
243 private void putScreenInCallMode() {
244 putScreenInCallMode(requireRtpConnection().getMedia());
245 }
246
247 private void putScreenInCallMode(final Set<Media> media) {
248 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
249 if (!media.contains(Media.VIDEO)) {
250 final JingleRtpConnection rtpConnection = rtpConnectionReference != null ? rtpConnectionReference.get() : null;
251 final AppRTCAudioManager audioManager = rtpConnection == null ? null : rtpConnection.getAudioManager();
252 if (audioManager == null || audioManager.getSelectedAudioDevice() == AppRTCAudioManager.AudioDevice.EARPIECE) {
253 acquireProximityWakeLock();
254 }
255 }
256 }
257
258 @SuppressLint("WakelockTimeout")
259 private void acquireProximityWakeLock() {
260 final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
261 if (powerManager == null) {
262 Log.e(Config.LOGTAG, "power manager not available");
263 return;
264 }
265 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
266 if (this.mProximityWakeLock == null) {
267 this.mProximityWakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, PROXIMITY_WAKE_LOCK_TAG);
268 }
269 if (!this.mProximityWakeLock.isHeld()) {
270 Log.d(Config.LOGTAG, "acquiring proximity wake lock");
271 this.mProximityWakeLock.acquire();
272 }
273 }
274 }
275
276 private void releaseProximityWakeLock() {
277 if (this.mProximityWakeLock != null && mProximityWakeLock.isHeld()) {
278 Log.d(Config.LOGTAG, "releasing proximity wake lock");
279 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
280 this.mProximityWakeLock.release(PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
281 } else {
282 this.mProximityWakeLock.release();
283 }
284 this.mProximityWakeLock = null;
285 }
286 }
287
288 private void putProximityWakeLockInProperState(final AppRTCAudioManager.AudioDevice audioDevice) {
289 if (audioDevice == AppRTCAudioManager.AudioDevice.EARPIECE) {
290 acquireProximityWakeLock();
291 } else {
292 releaseProximityWakeLock();
293 }
294 }
295
296 @Override
297 protected void refreshUiReal() {
298
299 }
300
301 @Override
302 public void onNewIntent(final Intent intent) {
303 Log.d(Config.LOGTAG, this.getClass().getName() + ".onNewIntent()");
304 super.onNewIntent(intent);
305 setIntent(intent);
306 if (xmppConnectionService == null) {
307 Log.d(Config.LOGTAG, "RtpSessionActivity: background service wasn't bound in onNewIntent()");
308 return;
309 }
310 final Account account = extractAccount(intent);
311 final Jid with = Jid.ofEscaped(intent.getStringExtra(EXTRA_WITH));
312 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
313 if (sessionId != null) {
314 Log.d(Config.LOGTAG, "reinitializing from onNewIntent()");
315 if (initializeActivityWithRunningRtpSession(account, with, sessionId)) {
316 return;
317 }
318 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
319 Log.d(Config.LOGTAG, "accepting call from onNewIntent()");
320 requestPermissionsAndAcceptCall();
321 resetIntent(intent.getExtras());
322 }
323 } else {
324 throw new IllegalStateException("received onNewIntent without sessionId");
325 }
326 }
327
328 @Override
329 void onBackendConnected() {
330 final Intent intent = getIntent();
331 final String action = intent.getAction();
332 final Account account = extractAccount(intent);
333 final Jid with = Jid.ofEscaped(intent.getStringExtra(EXTRA_WITH));
334 final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
335 if (sessionId != null) {
336 if (initializeActivityWithRunningRtpSession(account, with, sessionId)) {
337 return;
338 }
339 if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
340 Log.d(Config.LOGTAG, "intent action was accept");
341 requestPermissionsAndAcceptCall();
342 resetIntent(intent.getExtras());
343 }
344 } else if (asList(ACTION_MAKE_VIDEO_CALL, ACTION_MAKE_VOICE_CALL).contains(action)) {
345 proposeJingleRtpSession(account, with, actionToMedia(action));
346 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
347 } else if (Intent.ACTION_VIEW.equals(action)) {
348 final String extraLastState = intent.getStringExtra(EXTRA_LAST_REPORTED_STATE);
349 if (extraLastState != null) {
350 Log.d(Config.LOGTAG, "restored last state from intent extra");
351 RtpEndUserState state = RtpEndUserState.valueOf(extraLastState);
352 updateButtonConfiguration(state);
353 updateStateDisplay(state);
354 updateProfilePicture(state);
355 invalidateOptionsMenu();
356 }
357 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
358 }
359 }
360
361 private void proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
362 checkMicrophoneAvailability();
363 if (with.isBareJid()) {
364 xmppConnectionService.getJingleConnectionManager().proposeJingleRtpSession(account, with, media);
365 } else {
366 final String sessionId = xmppConnectionService.getJingleConnectionManager().initializeRtpSession(account, with, media);
367 initializeActivityWithRunningRtpSession(account, with, sessionId);
368 resetIntent(account, with, sessionId);
369 }
370 putScreenInCallMode(media);
371 }
372
373 @Override
374 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
375 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
376 if (PermissionUtils.allGranted(grantResults)) {
377 if (requestCode == REQUEST_ACCEPT_CALL) {
378 checkRecorderAndAcceptCall();
379 }
380 } else {
381 @StringRes int res;
382 final String firstDenied = getFirstDenied(grantResults, permissions);
383 if (Manifest.permission.RECORD_AUDIO.equals(firstDenied)) {
384 res = R.string.no_microphone_permission;
385 } else if (Manifest.permission.CAMERA.equals(firstDenied)) {
386 res = R.string.no_camera_permission;
387 } else {
388 throw new IllegalStateException("Invalid permission result request");
389 }
390 Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
391 }
392 }
393
394 @Override
395 public void onStart() {
396 super.onStart();
397 mHandler.postDelayed(mTickExecutor, CALL_DURATION_UPDATE_INTERVAL);
398 }
399
400 @Override
401 public void onStop() {
402 mHandler.removeCallbacks(mTickExecutor);
403 binding.remoteVideo.release();
404 binding.localVideo.release();
405 final WeakReference<JingleRtpConnection> weakReference = this.rtpConnectionReference;
406 final JingleRtpConnection jingleRtpConnection = weakReference == null ? null : weakReference.get();
407 if (jingleRtpConnection != null) {
408 releaseVideoTracks(jingleRtpConnection);
409 }
410 releaseProximityWakeLock();
411 super.onStop();
412 }
413
414 private void releaseVideoTracks(final JingleRtpConnection jingleRtpConnection) {
415 final Optional<VideoTrack> remoteVideo = jingleRtpConnection.getRemoteVideoTrack();
416 if (remoteVideo.isPresent()) {
417 remoteVideo.get().removeSink(binding.remoteVideo);
418 }
419 final Optional<VideoTrack> localVideo = jingleRtpConnection.getLocalVideoTrack();
420 if (localVideo.isPresent()) {
421 localVideo.get().removeSink(binding.localVideo);
422 }
423 }
424
425 @Override
426 public void onBackPressed() {
427 super.onBackPressed();
428 endCall();
429 }
430
431 @Override
432 public void onUserLeaveHint() {
433 super.onUserLeaveHint();
434 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && deviceSupportsPictureInPicture()) {
435 if (shouldBePictureInPicture()) {
436 startPictureInPicture();
437 return;
438 }
439 }
440 retractSessionProposal();
441 }
442
443 @RequiresApi(api = Build.VERSION_CODES.O)
444 private void startPictureInPicture() {
445 try {
446 enterPictureInPictureMode(
447 new PictureInPictureParams.Builder()
448 .setAspectRatio(new Rational(10, 16))
449 .build()
450 );
451 } catch (final IllegalStateException e) {
452 //this sometimes happens on Samsung phones (possibly when Knox is enabled)
453 Log.w(Config.LOGTAG, "unable to enter picture in picture mode", e);
454 }
455 }
456
457 private boolean deviceSupportsPictureInPicture() {
458 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
459 return getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
460 } else {
461 return false;
462 }
463 }
464
465 private boolean shouldBePictureInPicture() {
466 try {
467 final JingleRtpConnection rtpConnection = requireRtpConnection();
468 return rtpConnection.getMedia().contains(Media.VIDEO) && Arrays.asList(
469 RtpEndUserState.ACCEPTING_CALL,
470 RtpEndUserState.CONNECTING,
471 RtpEndUserState.CONNECTED
472 ).contains(rtpConnection.getEndUserState());
473 } catch (final IllegalStateException e) {
474 return false;
475 }
476 }
477
478 private boolean initializeActivityWithRunningRtpSession(final Account account, Jid with, String sessionId) {
479 final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
480 .findJingleRtpConnection(account, with, sessionId);
481 if (reference == null || reference.get() == null) {
482 final JingleConnectionManager.TerminatedRtpSession terminatedRtpSession = xmppConnectionService
483 .getJingleConnectionManager().getTerminalSessionState(with, sessionId);
484 if (terminatedRtpSession == null) {
485 throw new IllegalStateException("failed to initialize activity with running rtp session. session not found");
486 }
487 initializeWithTerminatedSessionState(account, with, terminatedRtpSession);
488 return true;
489 }
490 this.rtpConnectionReference = reference;
491 final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
492 if (currentState == RtpEndUserState.ENDED) {
493 reference.get().throwStateTransitionException();
494 finish();
495 return true;
496 }
497 final Set<Media> media = getMedia();
498 if (currentState == RtpEndUserState.INCOMING_CALL) {
499 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
500 }
501 if (JingleRtpConnection.STATES_SHOWING_ONGOING_CALL.contains(requireRtpConnection().getState())) {
502 putScreenInCallMode();
503 }
504 binding.with.setText(getWith().getDisplayName());
505 updateVideoViews(currentState);
506 updateStateDisplay(currentState, media);
507 updateButtonConfiguration(currentState, media);
508 updateProfilePicture(currentState);
509 invalidateOptionsMenu();
510 return false;
511 }
512
513 private void initializeWithTerminatedSessionState(final Account account, final Jid with, final JingleConnectionManager.TerminatedRtpSession terminatedRtpSession) {
514 Log.d(Config.LOGTAG, "initializeWithTerminatedSessionState()");
515 if (terminatedRtpSession.state == RtpEndUserState.ENDED) {
516 finish();
517 return;
518 }
519 RtpEndUserState state = terminatedRtpSession.state;
520 resetIntent(account, with, terminatedRtpSession.state, terminatedRtpSession.media);
521 updateButtonConfiguration(state);
522 updateStateDisplay(state);
523 updateProfilePicture(state);
524 updateCallDuration();
525 invalidateOptionsMenu();
526 binding.with.setText(account.getRoster().getContact(with).getDisplayName());
527 }
528
529 private void reInitializeActivityWithRunningRtpSession(final Account account, Jid with, String sessionId) {
530 runOnUiThread(() -> initializeActivityWithRunningRtpSession(account, with, sessionId));
531 resetIntent(account, with, sessionId);
532 }
533
534 private void resetIntent(final Account account, final Jid with, final String sessionId) {
535 final Intent intent = new Intent(Intent.ACTION_VIEW);
536 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
537 intent.putExtra(EXTRA_WITH, with.toEscapedString());
538 intent.putExtra(EXTRA_SESSION_ID, sessionId);
539 setIntent(intent);
540 }
541
542 private void ensureSurfaceViewRendererIsSetup(final SurfaceViewRenderer surfaceViewRenderer) {
543 surfaceViewRenderer.setVisibility(View.VISIBLE);
544 try {
545 surfaceViewRenderer.init(requireRtpConnection().getEglBaseContext(), null);
546 } catch (IllegalStateException e) {
547 Log.d(Config.LOGTAG, "SurfaceViewRenderer was already initialized");
548 }
549 surfaceViewRenderer.setEnableHardwareScaler(true);
550 }
551
552 private void updateStateDisplay(final RtpEndUserState state) {
553 updateStateDisplay(state, Collections.emptySet());
554 }
555
556 private void updateStateDisplay(final RtpEndUserState state, final Set<Media> media) {
557 switch (state) {
558 case INCOMING_CALL:
559 Preconditions.checkArgument(media.size() > 0, "Media must not be empty");
560 if (media.contains(Media.VIDEO)) {
561 setTitle(R.string.rtp_state_incoming_video_call);
562 } else {
563 setTitle(R.string.rtp_state_incoming_call);
564 }
565 break;
566 case CONNECTING:
567 setTitle(R.string.rtp_state_connecting);
568 break;
569 case CONNECTED:
570 setTitle(R.string.rtp_state_connected);
571 break;
572 case ACCEPTING_CALL:
573 setTitle(R.string.rtp_state_accepting_call);
574 break;
575 case ENDING_CALL:
576 setTitle(R.string.rtp_state_ending_call);
577 break;
578 case FINDING_DEVICE:
579 setTitle(R.string.rtp_state_finding_device);
580 break;
581 case RINGING:
582 setTitle(R.string.rtp_state_ringing);
583 break;
584 case DECLINED_OR_BUSY:
585 setTitle(R.string.rtp_state_declined_or_busy);
586 break;
587 case CONNECTIVITY_ERROR:
588 setTitle(R.string.rtp_state_connectivity_error);
589 break;
590 case CONNECTIVITY_LOST_ERROR:
591 setTitle(R.string.rtp_state_connectivity_lost_error);
592 break;
593 case RETRACTED:
594 setTitle(R.string.rtp_state_retracted);
595 break;
596 case APPLICATION_ERROR:
597 setTitle(R.string.rtp_state_application_failure);
598 break;
599 case ENDED:
600 throw new IllegalStateException("Activity should have called finishAndReleaseWakeLock();");
601 default:
602 throw new IllegalStateException(String.format("State %s has not been handled in UI", state));
603 }
604 }
605
606 private void updateProfilePicture(final RtpEndUserState state) {
607 updateProfilePicture(state, null);
608 }
609
610 private void updateProfilePicture(final RtpEndUserState state, final Contact contact) {
611 if (state == RtpEndUserState.INCOMING_CALL || state == RtpEndUserState.ACCEPTING_CALL) {
612 final boolean show = getResources().getBoolean(R.bool.show_avatar_incoming_call);
613 if (show) {
614 binding.contactPhoto.setVisibility(View.VISIBLE);
615 if (contact == null) {
616 AvatarWorkerTask.loadAvatar(getWith(), binding.contactPhoto, R.dimen.publish_avatar_size);
617 } else {
618 AvatarWorkerTask.loadAvatar(contact, binding.contactPhoto, R.dimen.publish_avatar_size);
619 }
620 } else {
621 binding.contactPhoto.setVisibility(View.GONE);
622 }
623 } else {
624 binding.contactPhoto.setVisibility(View.GONE);
625 }
626 }
627
628 private Set<Media> getMedia() {
629 return requireRtpConnection().getMedia();
630 }
631
632 private void updateButtonConfiguration(final RtpEndUserState state) {
633 updateButtonConfiguration(state, Collections.emptySet());
634 }
635
636 @SuppressLint("RestrictedApi")
637 private void updateButtonConfiguration(final RtpEndUserState state, final Set<Media> media) {
638 if (state == RtpEndUserState.ENDING_CALL || isPictureInPicture()) {
639 this.binding.rejectCall.setVisibility(View.INVISIBLE);
640 this.binding.endCall.setVisibility(View.INVISIBLE);
641 this.binding.acceptCall.setVisibility(View.INVISIBLE);
642 } else if (state == RtpEndUserState.INCOMING_CALL) {
643 this.binding.rejectCall.setOnClickListener(this::rejectCall);
644 this.binding.rejectCall.setImageResource(R.drawable.ic_call_end_white_48dp);
645 this.binding.rejectCall.setVisibility(View.VISIBLE);
646 this.binding.endCall.setVisibility(View.INVISIBLE);
647 this.binding.acceptCall.setOnClickListener(this::acceptCall);
648 this.binding.acceptCall.setImageResource(R.drawable.ic_call_white_48dp);
649 this.binding.acceptCall.setVisibility(View.VISIBLE);
650 } else if (state == RtpEndUserState.DECLINED_OR_BUSY) {
651 this.binding.rejectCall.setOnClickListener(this::exit);
652 this.binding.rejectCall.setImageResource(R.drawable.ic_clear_white_48dp);
653 this.binding.rejectCall.setVisibility(View.VISIBLE);
654 this.binding.endCall.setVisibility(View.INVISIBLE);
655 this.binding.acceptCall.setOnClickListener(this::recordVoiceMail);
656 this.binding.acceptCall.setImageResource(R.drawable.ic_voicemail_white_24dp);
657 this.binding.acceptCall.setVisibility(View.VISIBLE);
658 } else if (asList(
659 RtpEndUserState.CONNECTIVITY_ERROR,
660 RtpEndUserState.CONNECTIVITY_LOST_ERROR,
661 RtpEndUserState.APPLICATION_ERROR,
662 RtpEndUserState.RETRACTED
663 ).contains(state)) {
664 this.binding.rejectCall.setOnClickListener(this::exit);
665 this.binding.rejectCall.setImageResource(R.drawable.ic_clear_white_48dp);
666 this.binding.rejectCall.setVisibility(View.VISIBLE);
667 this.binding.endCall.setVisibility(View.INVISIBLE);
668 this.binding.acceptCall.setOnClickListener(this::retry);
669 this.binding.acceptCall.setImageResource(R.drawable.ic_replay_white_48dp);
670 this.binding.acceptCall.setVisibility(View.VISIBLE);
671 } else {
672 this.binding.rejectCall.setVisibility(View.INVISIBLE);
673 this.binding.endCall.setOnClickListener(this::endCall);
674 this.binding.endCall.setImageResource(R.drawable.ic_call_end_white_48dp);
675 this.binding.endCall.setVisibility(View.VISIBLE);
676 this.binding.acceptCall.setVisibility(View.INVISIBLE);
677 }
678 updateInCallButtonConfiguration(state, media);
679 }
680
681 private boolean isPictureInPicture() {
682 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
683 return isInPictureInPictureMode();
684 } else {
685 return false;
686 }
687 }
688
689 private void updateInCallButtonConfiguration() {
690 updateInCallButtonConfiguration(requireRtpConnection().getEndUserState(), requireRtpConnection().getMedia());
691 }
692
693 @SuppressLint("RestrictedApi")
694 private void updateInCallButtonConfiguration(final RtpEndUserState state, final Set<Media> media) {
695 if (state == RtpEndUserState.CONNECTED && !isPictureInPicture()) {
696 Preconditions.checkArgument(media.size() > 0, "Media must not be empty");
697 if (media.contains(Media.VIDEO)) {
698 final JingleRtpConnection rtpConnection = requireRtpConnection();
699 updateInCallButtonConfigurationVideo(rtpConnection.isVideoEnabled(), rtpConnection.isCameraSwitchable());
700 } else {
701 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
702 updateInCallButtonConfigurationSpeaker(
703 audioManager.getSelectedAudioDevice(),
704 audioManager.getAudioDevices().size()
705 );
706 this.binding.inCallActionFarRight.setVisibility(View.GONE);
707 }
708 if (media.contains(Media.AUDIO)) {
709 updateInCallButtonConfigurationMicrophone(requireRtpConnection().isMicrophoneEnabled());
710 } else {
711 this.binding.inCallActionLeft.setVisibility(View.GONE);
712 }
713 } else {
714 this.binding.inCallActionLeft.setVisibility(View.GONE);
715 this.binding.inCallActionRight.setVisibility(View.GONE);
716 this.binding.inCallActionFarRight.setVisibility(View.GONE);
717 }
718 }
719
720 @SuppressLint("RestrictedApi")
721 private void updateInCallButtonConfigurationSpeaker(final AppRTCAudioManager.AudioDevice selectedAudioDevice, final int numberOfChoices) {
722 switch (selectedAudioDevice) {
723 case EARPIECE:
724 this.binding.inCallActionRight.setImageResource(R.drawable.ic_volume_off_black_24dp);
725 if (numberOfChoices >= 2) {
726 this.binding.inCallActionRight.setOnClickListener(this::switchToSpeaker);
727 } else {
728 this.binding.inCallActionRight.setOnClickListener(null);
729 this.binding.inCallActionRight.setClickable(false);
730 }
731 break;
732 case WIRED_HEADSET:
733 this.binding.inCallActionRight.setImageResource(R.drawable.ic_headset_black_24dp);
734 this.binding.inCallActionRight.setOnClickListener(null);
735 this.binding.inCallActionRight.setClickable(false);
736 break;
737 case SPEAKER_PHONE:
738 this.binding.inCallActionRight.setImageResource(R.drawable.ic_volume_up_black_24dp);
739 if (numberOfChoices >= 2) {
740 this.binding.inCallActionRight.setOnClickListener(this::switchToEarpiece);
741 } else {
742 this.binding.inCallActionRight.setOnClickListener(null);
743 this.binding.inCallActionRight.setClickable(false);
744 }
745 break;
746 case BLUETOOTH:
747 this.binding.inCallActionRight.setImageResource(R.drawable.ic_bluetooth_audio_black_24dp);
748 this.binding.inCallActionRight.setOnClickListener(null);
749 this.binding.inCallActionRight.setClickable(false);
750 break;
751 }
752 this.binding.inCallActionRight.setVisibility(View.VISIBLE);
753 }
754
755 @SuppressLint("RestrictedApi")
756 private void updateInCallButtonConfigurationVideo(final boolean videoEnabled, final boolean isCameraSwitchable) {
757 this.binding.inCallActionRight.setVisibility(View.VISIBLE);
758 if (isCameraSwitchable) {
759 this.binding.inCallActionFarRight.setImageResource(R.drawable.ic_flip_camera_android_black_24dp);
760 this.binding.inCallActionFarRight.setVisibility(View.VISIBLE);
761 this.binding.inCallActionFarRight.setOnClickListener(this::switchCamera);
762 } else {
763 this.binding.inCallActionFarRight.setVisibility(View.GONE);
764 }
765 if (videoEnabled) {
766 this.binding.inCallActionRight.setImageResource(R.drawable.ic_videocam_black_24dp);
767 this.binding.inCallActionRight.setOnClickListener(this::disableVideo);
768 } else {
769 this.binding.inCallActionRight.setImageResource(R.drawable.ic_videocam_off_black_24dp);
770 this.binding.inCallActionRight.setOnClickListener(this::enableVideo);
771 }
772 }
773
774 private void switchCamera(final View view) {
775 Futures.addCallback(requireRtpConnection().switchCamera(), new FutureCallback<Boolean>() {
776 @Override
777 public void onSuccess(@NullableDecl Boolean isFrontCamera) {
778 binding.localVideo.setMirror(isFrontCamera);
779 }
780
781 @Override
782 public void onFailure(@NonNull final Throwable throwable) {
783 Log.d(Config.LOGTAG, "could not switch camera", Throwables.getRootCause(throwable));
784 Toast.makeText(RtpSessionActivity.this, R.string.could_not_switch_camera, Toast.LENGTH_LONG).show();
785 }
786 }, MainThreadExecutor.getInstance());
787 }
788
789 private void enableVideo(View view) {
790 requireRtpConnection().setVideoEnabled(true);
791 updateInCallButtonConfigurationVideo(true, requireRtpConnection().isCameraSwitchable());
792 }
793
794 private void disableVideo(View view) {
795 requireRtpConnection().setVideoEnabled(false);
796 updateInCallButtonConfigurationVideo(false, requireRtpConnection().isCameraSwitchable());
797
798 }
799
800 @SuppressLint("RestrictedApi")
801 private void updateInCallButtonConfigurationMicrophone(final boolean microphoneEnabled) {
802 if (microphoneEnabled) {
803 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_mic_black_24dp);
804 this.binding.inCallActionLeft.setOnClickListener(this::disableMicrophone);
805 } else {
806 this.binding.inCallActionLeft.setImageResource(R.drawable.ic_mic_off_black_24dp);
807 this.binding.inCallActionLeft.setOnClickListener(this::enableMicrophone);
808 }
809 this.binding.inCallActionLeft.setVisibility(View.VISIBLE);
810 }
811
812 private void updateCallDuration() {
813 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
814 if (connection == null || connection.getMedia().contains(Media.VIDEO)) {
815 this.binding.duration.setVisibility(View.GONE);
816 return;
817 }
818 final long rtpConnectionStarted = connection.getRtpConnectionStarted();
819 final long rtpConnectionEnded = connection.getRtpConnectionEnded();
820 if (rtpConnectionStarted != 0) {
821 final long ended = rtpConnectionEnded == 0 ? SystemClock.elapsedRealtime() : rtpConnectionEnded;
822 this.binding.duration.setText(TimeFrameUtils.formatTimePassed(rtpConnectionStarted, ended, false));
823 this.binding.duration.setVisibility(View.VISIBLE);
824 } else {
825 this.binding.duration.setVisibility(View.GONE);
826 }
827 }
828
829 private void updateVideoViews(final RtpEndUserState state) {
830 if (END_CARD.contains(state) || state == RtpEndUserState.ENDING_CALL) {
831 binding.localVideo.setVisibility(View.GONE);
832 binding.localVideo.release();
833 binding.remoteVideo.setVisibility(View.GONE);
834 binding.remoteVideo.release();
835 binding.pipLocalMicOffIndicator.setVisibility(View.GONE);
836 if (isPictureInPicture()) {
837 binding.appBarLayout.setVisibility(View.GONE);
838 binding.pipPlaceholder.setVisibility(View.VISIBLE);
839 if (state == RtpEndUserState.APPLICATION_ERROR || state == RtpEndUserState.CONNECTIVITY_ERROR) {
840 binding.pipWarning.setVisibility(View.VISIBLE);
841 binding.pipWaiting.setVisibility(View.GONE);
842 } else {
843 binding.pipWarning.setVisibility(View.GONE);
844 binding.pipWaiting.setVisibility(View.GONE);
845 }
846 } else {
847 binding.appBarLayout.setVisibility(View.VISIBLE);
848 binding.pipPlaceholder.setVisibility(View.GONE);
849 }
850 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
851 return;
852 }
853 if (isPictureInPicture() && (state == RtpEndUserState.CONNECTING || state == RtpEndUserState.ACCEPTING_CALL)) {
854 binding.localVideo.setVisibility(View.GONE);
855 binding.remoteVideo.setVisibility(View.GONE);
856 binding.appBarLayout.setVisibility(View.GONE);
857 binding.pipPlaceholder.setVisibility(View.VISIBLE);
858 binding.pipWarning.setVisibility(View.GONE);
859 binding.pipWaiting.setVisibility(View.VISIBLE);
860 binding.pipLocalMicOffIndicator.setVisibility(View.GONE);
861 return;
862 }
863 final Optional<VideoTrack> localVideoTrack = getLocalVideoTrack();
864 if (localVideoTrack.isPresent() && !isPictureInPicture()) {
865 ensureSurfaceViewRendererIsSetup(binding.localVideo);
866 //paint local view over remote view
867 binding.localVideo.setZOrderMediaOverlay(true);
868 binding.localVideo.setMirror(requireRtpConnection().isFrontCamera());
869 addSink(localVideoTrack.get(), binding.localVideo);
870 } else {
871 binding.localVideo.setVisibility(View.GONE);
872 }
873 final Optional<VideoTrack> remoteVideoTrack = getRemoteVideoTrack();
874 if (remoteVideoTrack.isPresent()) {
875 ensureSurfaceViewRendererIsSetup(binding.remoteVideo);
876 addSink(remoteVideoTrack.get(), binding.remoteVideo);
877 if (state == RtpEndUserState.CONNECTED) {
878 binding.appBarLayout.setVisibility(View.GONE);
879 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
880 } else {
881 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
882 binding.remoteVideo.setVisibility(View.GONE);
883 }
884 if (isPictureInPicture() && !requireRtpConnection().isMicrophoneEnabled()) {
885 binding.pipLocalMicOffIndicator.setVisibility(View.VISIBLE);
886 } else {
887 binding.pipLocalMicOffIndicator.setVisibility(View.GONE);
888 }
889 } else {
890 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
891 binding.remoteVideo.setVisibility(View.GONE);
892 binding.pipLocalMicOffIndicator.setVisibility(View.GONE);
893 }
894 }
895
896 private Optional<VideoTrack> getLocalVideoTrack() {
897 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
898 if (connection == null) {
899 return Optional.absent();
900 }
901 return connection.getLocalVideoTrack();
902 }
903
904 private Optional<VideoTrack> getRemoteVideoTrack() {
905 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
906 if (connection == null) {
907 return Optional.absent();
908 }
909 return connection.getRemoteVideoTrack();
910 }
911
912 private void disableMicrophone(View view) {
913 JingleRtpConnection rtpConnection = requireRtpConnection();
914 rtpConnection.setMicrophoneEnabled(false);
915 updateInCallButtonConfiguration();
916 }
917
918 private void enableMicrophone(View view) {
919 JingleRtpConnection rtpConnection = requireRtpConnection();
920 rtpConnection.setMicrophoneEnabled(true);
921 updateInCallButtonConfiguration();
922 }
923
924 private void switchToEarpiece(View view) {
925 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE);
926 acquireProximityWakeLock();
927 }
928
929 private void switchToSpeaker(View view) {
930 requireRtpConnection().getAudioManager().setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE);
931 releaseProximityWakeLock();
932 }
933
934 private void retry(View view) {
935 final Intent intent = getIntent();
936 final Account account = extractAccount(intent);
937 final Jid with = Jid.ofEscaped(intent.getStringExtra(EXTRA_WITH));
938 final String lastAction = intent.getStringExtra(EXTRA_LAST_ACTION);
939 final String action = intent.getAction();
940 final Set<Media> media = actionToMedia(lastAction == null ? action : lastAction);
941 this.rtpConnectionReference = null;
942 Log.d(Config.LOGTAG, "attempting retry with " + with.toEscapedString());
943 proposeJingleRtpSession(account, with, media);
944 }
945
946 private void exit(final View view) {
947 finish();
948 }
949
950 private void recordVoiceMail(final View view) {
951 final Intent intent = getIntent();
952 final Account account = extractAccount(intent);
953 final Jid with = Jid.ofEscaped(intent.getStringExtra(EXTRA_WITH));
954 final Conversation conversation = xmppConnectionService.findOrCreateConversation(account, with, false, true);
955 final Intent launchIntent = new Intent(this, ConversationsActivity.class);
956 launchIntent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
957 launchIntent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
958 launchIntent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_CLEAR_TOP);
959 launchIntent.putExtra(ConversationsActivity.EXTRA_POST_INIT_ACTION, ConversationsActivity.POST_ACTION_RECORD_VOICE);
960 startActivity(launchIntent);
961 finish();
962 }
963
964 private Contact getWith() {
965 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
966 final Account account = id.account;
967 return account.getRoster().getContact(id.with);
968 }
969
970 private JingleRtpConnection requireRtpConnection() {
971 final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
972 if (connection == null) {
973 throw new IllegalStateException("No RTP connection found");
974 }
975 return connection;
976 }
977
978 @Override
979 public void onJingleRtpConnectionUpdate(Account account, Jid with, final String sessionId, RtpEndUserState state) {
980 Log.d(Config.LOGTAG, "onJingleRtpConnectionUpdate(" + state + ")");
981 if (END_CARD.contains(state)) {
982 Log.d(Config.LOGTAG, "end card reached");
983 releaseProximityWakeLock();
984 runOnUiThread(() -> getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON));
985 }
986 if (with.isBareJid()) {
987 updateRtpSessionProposalState(account, with, state);
988 return;
989 }
990 if (this.rtpConnectionReference == null) {
991 if (END_CARD.contains(state)) {
992 Log.d(Config.LOGTAG, "not reinitializing session");
993 return;
994 }
995 //this happens when going from proposed session to actual session
996 reInitializeActivityWithRunningRtpSession(account, with, sessionId);
997 return;
998 }
999 final AbstractJingleConnection.Id id = requireRtpConnection().getId();
1000 final Set<Media> media = getMedia();
1001 final Contact contact = getWith();
1002 if (account == id.account && id.with.equals(with) && id.sessionId.equals(sessionId)) {
1003 if (state == RtpEndUserState.ENDED) {
1004 finish();
1005 return;
1006 }
1007 runOnUiThread(() -> {
1008 updateStateDisplay(state, media);
1009 updateButtonConfiguration(state, media);
1010 updateVideoViews(state);
1011 updateProfilePicture(state, contact);
1012 invalidateOptionsMenu();
1013 });
1014 if (END_CARD.contains(state)) {
1015 final JingleRtpConnection rtpConnection = requireRtpConnection();
1016 resetIntent(account, with, state, rtpConnection.getMedia());
1017 releaseVideoTracks(rtpConnection);
1018 this.rtpConnectionReference = null;
1019 }
1020 } else {
1021 Log.d(Config.LOGTAG, "received update for other rtp session");
1022 }
1023 }
1024
1025 @Override
1026 public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
1027 Log.d(Config.LOGTAG, "onAudioDeviceChanged in activity: selected:" + selectedAudioDevice + ", available:" + availableAudioDevices);
1028 try {
1029 if (getMedia().contains(Media.VIDEO)) {
1030 Log.d(Config.LOGTAG, "nothing to do; in video mode");
1031 return;
1032 }
1033 final RtpEndUserState endUserState = requireRtpConnection().getEndUserState();
1034 if (endUserState == RtpEndUserState.CONNECTED) {
1035 final AppRTCAudioManager audioManager = requireRtpConnection().getAudioManager();
1036 updateInCallButtonConfigurationSpeaker(
1037 audioManager.getSelectedAudioDevice(),
1038 audioManager.getAudioDevices().size()
1039 );
1040 } else if (END_CARD.contains(endUserState)) {
1041 Log.d(Config.LOGTAG, "onAudioDeviceChanged() nothing to do because end card has been reached");
1042 } else {
1043 putProximityWakeLockInProperState(selectedAudioDevice);
1044 }
1045 } catch (IllegalStateException e) {
1046 Log.d(Config.LOGTAG, "RTP connection was not available when audio device changed");
1047 }
1048 }
1049
1050 private void updateRtpSessionProposalState(final Account account, final Jid with, final RtpEndUserState state) {
1051 final Intent currentIntent = getIntent();
1052 final String withExtra = currentIntent == null ? null : currentIntent.getStringExtra(EXTRA_WITH);
1053 if (withExtra == null) {
1054 return;
1055 }
1056 if (Jid.ofEscaped(withExtra).asBareJid().equals(with)) {
1057 runOnUiThread(() -> {
1058 updateStateDisplay(state);
1059 updateButtonConfiguration(state);
1060 updateProfilePicture(state);
1061 invalidateOptionsMenu();
1062 });
1063 resetIntent(account, with, state, actionToMedia(currentIntent.getAction()));
1064 }
1065 }
1066
1067 private void resetIntent(final Bundle extras) {
1068 final Intent intent = new Intent(Intent.ACTION_VIEW);
1069 intent.putExtras(extras);
1070 setIntent(intent);
1071 }
1072
1073 private void resetIntent(final Account account, Jid with, final RtpEndUserState state, final Set<Media> media) {
1074 final Intent intent = new Intent(Intent.ACTION_VIEW);
1075 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
1076 if (account.getRoster().getContact(with).getPresences().anySupport(Namespace.JINGLE_MESSAGE)) {
1077 intent.putExtra(EXTRA_WITH, with.asBareJid().toEscapedString());
1078 } else {
1079 intent.putExtra(EXTRA_WITH, with.toEscapedString());
1080 }
1081 intent.putExtra(EXTRA_LAST_REPORTED_STATE, state.toString());
1082 intent.putExtra(EXTRA_LAST_ACTION, media.contains(Media.VIDEO) ? ACTION_MAKE_VIDEO_CALL : ACTION_MAKE_VOICE_CALL);
1083 setIntent(intent);
1084 }
1085}