conversations-classic/src/main/java/eu/siacs/conversations/ui/RtpSessionActivity.java

303 lines
13 KiB
Java
Raw Normal View History

2020-04-07 09:36:28 +00:00
package eu.siacs.conversations.ui;
2020-04-07 11:15:24 +00:00
import android.content.Intent;
2020-04-08 11:30:12 +00:00
import android.content.res.ColorStateList;
2020-04-07 09:36:28 +00:00
import android.databinding.DataBindingUtil;
import android.os.Bundle;
2020-04-07 11:15:24 +00:00
import android.util.Log;
2020-04-07 09:36:28 +00:00
import android.view.View;
import android.view.WindowManager;
2020-04-07 11:15:24 +00:00
import java.lang.ref.WeakReference;
import eu.siacs.conversations.Config;
2020-04-07 09:36:28 +00:00
import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.ActivityRtpSessionBinding;
2020-04-07 11:15:24 +00:00
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
import eu.siacs.conversations.xmpp.jingle.JingleRtpConnection;
import eu.siacs.conversations.xmpp.jingle.RtpEndUserState;
import rocks.xmpp.addr.Jid;
2020-04-07 09:36:28 +00:00
2020-04-08 07:42:06 +00:00
import static java.util.Arrays.asList;
2020-04-08 09:29:01 +00:00
//TODO if last state was BUSY (or RETRY); we want to reset action to view or something so we dont automatically call again on recreate
2020-04-07 11:15:24 +00:00
public class RtpSessionActivity extends XmppActivity implements XmppConnectionService.OnJingleRtpConnectionUpdate {
2020-04-07 09:36:28 +00:00
public static final String EXTRA_WITH = "with";
2020-04-07 11:15:24 +00:00
public static final String EXTRA_SESSION_ID = "session_id";
public static final String EXTRA_LAST_REPORTED_STATE = "last_reported_state";
2020-04-07 11:15:24 +00:00
2020-04-08 07:42:06 +00:00
public static final String ACTION_ACCEPT_CALL = "action_accept_call";
public static final String ACTION_MAKE_VOICE_CALL = "action_make_voice_call";
public static final String ACTION_MAKE_VIDEO_CALL = "action_make_video_call";
2020-04-07 11:15:24 +00:00
private WeakReference<JingleRtpConnection> rtpConnectionReference;
2020-04-07 09:36:28 +00:00
private ActivityRtpSessionBinding binding;
2020-04-07 14:17:29 +00:00
@Override
2020-04-07 09:36:28 +00:00
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
;
Log.d(Config.LOGTAG, "RtpSessionActivity.onCreate()");
2020-04-07 09:36:28 +00:00
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
2020-04-07 12:22:12 +00:00
}
2020-04-07 14:17:29 +00:00
@Override
public void onStart() {
super.onStart();
Log.d(Config.LOGTAG, "RtpSessionActivity.onStart()");
2020-04-07 14:17:29 +00:00
}
2020-04-07 12:22:12 +00:00
private void endCall(View view) {
2020-04-08 07:42:06 +00:00
if (this.rtpConnectionReference == null) {
final Intent intent = getIntent();
final Account account = extractAccount(intent);
final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
xmppConnectionService.getJingleConnectionManager().retractSessionProposal(account, with.asBareJid());
finish();
} else {
requireRtpConnection().endCall();
}
2020-04-07 09:36:28 +00:00
}
private void rejectCall(View view) {
2020-04-07 11:15:24 +00:00
requireRtpConnection().rejectCall();
finish();
2020-04-07 09:36:28 +00:00
}
private void acceptCall(View view) {
2020-04-07 11:15:24 +00:00
requireRtpConnection().acceptCall();
2020-04-07 09:36:28 +00:00
}
@Override
protected void refreshUiReal() {
}
2020-04-07 19:26:51 +00:00
@Override
public void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
2020-04-09 11:27:13 +00:00
//TODO reinitialize
2020-04-08 07:42:06 +00:00
if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
Log.d(Config.LOGTAG, "accepting through onNewIntent()");
2020-04-07 19:26:51 +00:00
requireRtpConnection().acceptCall();
}
}
2020-04-07 09:36:28 +00:00
@Override
void onBackendConnected() {
2020-04-07 11:15:24 +00:00
final Intent intent = getIntent();
final Account account = extractAccount(intent);
2020-04-08 07:42:06 +00:00
final Jid with = Jid.of(intent.getStringExtra(EXTRA_WITH));
2020-04-07 11:15:24 +00:00
final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
2020-04-08 07:42:06 +00:00
if (sessionId != null) {
initializeActivityWithRunningRapSession(account, with, sessionId);
if (ACTION_ACCEPT_CALL.equals(intent.getAction())) {
Log.d(Config.LOGTAG, "intent action was accept");
requireRtpConnection().acceptCall();
}
2020-04-08 07:42:06 +00:00
} else if (asList(ACTION_MAKE_VIDEO_CALL, ACTION_MAKE_VOICE_CALL).contains(intent.getAction())) {
xmppConnectionService.getJingleConnectionManager().proposeJingleRtpSession(account, with);
binding.with.setText(account.getRoster().getContact(with).getDisplayName());
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
final String extraLastState = intent.getStringExtra(EXTRA_LAST_REPORTED_STATE);
if (extraLastState != null) {
Log.d(Config.LOGTAG, "restored last state from intent extra");
RtpEndUserState state = RtpEndUserState.valueOf(extraLastState);
updateButtonConfiguration(state);
updateStateDisplay(state);
}
binding.with.setText(account.getRoster().getContact(with).getDisplayName());
2020-04-07 11:15:24 +00:00
}
}
2020-04-08 07:42:06 +00:00
private void initializeActivityWithRunningRapSession(final Account account, Jid with, String sessionId) {
final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
.findJingleRtpConnection(account, with, sessionId);
if (reference == null || reference.get() == null) {
finish();
return;
}
this.rtpConnectionReference = reference;
final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
if (currentState == RtpEndUserState.ENDED) {
finish();
return;
}
binding.with.setText(getWith().getDisplayName());
updateStateDisplay(currentState);
updateButtonConfiguration(currentState);
}
private void reInitializeActivityWithRunningRapSession(final Account account, Jid with, String sessionId) {
runOnUiThread(() -> {
initializeActivityWithRunningRapSession(account, with, sessionId);
});
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
intent.putExtra(EXTRA_WITH, with.toEscapedString());
intent.putExtra(EXTRA_SESSION_ID, sessionId);
setIntent(intent);
}
2020-04-07 12:22:12 +00:00
private void updateStateDisplay(final RtpEndUserState state) {
2020-04-07 11:15:24 +00:00
switch (state) {
case INCOMING_CALL:
binding.status.setText(R.string.rtp_state_incoming_call);
break;
case CONNECTING:
binding.status.setText(R.string.rtp_state_connecting);
break;
case CONNECTED:
binding.status.setText(R.string.rtp_state_connected);
break;
case ACCEPTING_CALL:
binding.status.setText(R.string.rtp_state_accepting_call);
break;
2020-04-07 12:22:12 +00:00
case ENDING_CALL:
binding.status.setText(R.string.rtp_state_ending_call);
break;
2020-04-08 07:42:06 +00:00
case FINDING_DEVICE:
binding.status.setText(R.string.rtp_state_finding_device);
break;
case RINGING:
binding.status.setText(R.string.rtp_state_ringing);
2020-04-08 08:46:26 +00:00
break;
case DECLINED_OR_BUSY:
binding.status.setText(R.string.rtp_state_declined_or_busy);
2020-04-08 13:27:17 +00:00
break;
2020-04-08 11:30:12 +00:00
case CONNECTIVITY_ERROR:
binding.status.setText(R.string.rtp_state_connectivity_error);
2020-04-08 08:46:26 +00:00
break;
case APPLICATION_ERROR:
binding.status.setText(R.string.rtp_state_application_failure);
break;
case ENDED:
throw new IllegalStateException("Activity should have called finish()");
default:
throw new IllegalStateException(String.format("State %s has not been handled in UI", state));
2020-04-07 12:22:12 +00:00
}
}
private void updateButtonConfiguration(final RtpEndUserState state) {
if (state == RtpEndUserState.INCOMING_CALL) {
2020-04-08 11:30:12 +00:00
this.binding.rejectCall.setOnClickListener(this::rejectCall);
this.binding.rejectCall.setImageResource(R.drawable.ic_call_end_white_48dp);
2020-04-07 12:22:12 +00:00
this.binding.rejectCall.show();
this.binding.endCall.hide();
2020-04-08 11:30:12 +00:00
this.binding.acceptCall.setOnClickListener(this::acceptCall);
this.binding.acceptCall.setImageResource(R.drawable.ic_call_white_48dp);
2020-04-07 12:22:12 +00:00
this.binding.acceptCall.show();
} else if (state == RtpEndUserState.ENDING_CALL) {
this.binding.rejectCall.hide();
this.binding.endCall.hide();
this.binding.acceptCall.hide();
2020-04-08 08:46:26 +00:00
} else if (state == RtpEndUserState.DECLINED_OR_BUSY) {
this.binding.rejectCall.hide();
2020-04-08 11:30:12 +00:00
this.binding.endCall.setOnClickListener(this::exit);
2020-04-08 08:46:26 +00:00
this.binding.endCall.setImageResource(R.drawable.ic_clear_white_48dp);
this.binding.endCall.show();
this.binding.acceptCall.hide();
} else if (state == RtpEndUserState.CONNECTIVITY_ERROR || state == RtpEndUserState.APPLICATION_ERROR) {
2020-04-08 11:30:12 +00:00
this.binding.rejectCall.setOnClickListener(this::exit);
this.binding.rejectCall.setImageResource(R.drawable.ic_clear_white_48dp);
this.binding.rejectCall.show();
this.binding.endCall.hide();
this.binding.acceptCall.setOnClickListener(this::retry);
this.binding.acceptCall.setImageResource(R.drawable.ic_replay_white_48dp);
this.binding.acceptCall.show();
2020-04-07 12:22:12 +00:00
} else {
this.binding.rejectCall.hide();
2020-04-08 11:30:12 +00:00
this.binding.endCall.setOnClickListener(this::endCall);
2020-04-08 08:46:26 +00:00
this.binding.endCall.setImageResource(R.drawable.ic_call_end_white_48dp);
2020-04-07 12:22:12 +00:00
this.binding.endCall.show();
this.binding.acceptCall.hide();
2020-04-07 11:15:24 +00:00
}
}
2020-04-08 11:30:12 +00:00
private void retry(View view) {
Log.d(Config.LOGTAG,"attempting retry");
2020-04-08 11:30:12 +00:00
}
2020-04-08 08:46:26 +00:00
private void exit(View view) {
finish();
}
2020-04-07 11:15:24 +00:00
private Contact getWith() {
final AbstractJingleConnection.Id id = requireRtpConnection().getId();
final Account account = id.account;
return account.getRoster().getContact(id.with);
}
private JingleRtpConnection requireRtpConnection() {
final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
if (connection == null) {
throw new IllegalStateException("No RTP connection found");
}
return connection;
}
@Override
2020-04-08 07:42:06 +00:00
public void onJingleRtpConnectionUpdate(Account account, Jid with, final String sessionId, RtpEndUserState state) {
2020-04-08 08:46:26 +00:00
Log.d(Config.LOGTAG, "onJingleRtpConnectionUpdate(" + state + ")");
2020-04-08 07:42:06 +00:00
if (with.isBareJid()) {
updateRtpSessionProposalState(account, with, state);
2020-04-08 07:42:06 +00:00
return;
}
if (this.rtpConnectionReference == null) {
//this happens when going from proposed session to actual session
reInitializeActivityWithRunningRapSession(account, with, sessionId);
return;
}
2020-04-07 11:15:24 +00:00
final AbstractJingleConnection.Id id = requireRtpConnection().getId();
2020-04-08 07:42:06 +00:00
if (account == id.account && id.with.equals(with) && id.sessionId.equals(sessionId)) {
2020-04-07 19:26:51 +00:00
if (state == RtpEndUserState.ENDED) {
finish();
return;
} else if (asList(RtpEndUserState.APPLICATION_ERROR, RtpEndUserState.DECLINED_OR_BUSY, RtpEndUserState.CONNECTIVITY_ERROR).contains(state)) {
resetIntent(account, with, state);
2020-04-07 19:26:51 +00:00
}
runOnUiThread(() -> {
2020-04-07 12:22:12 +00:00
updateStateDisplay(state);
updateButtonConfiguration(state);
2020-04-07 11:15:24 +00:00
});
} else {
Log.d(Config.LOGTAG, "received update for other rtp session");
2020-04-07 11:15:24 +00:00
}
2020-04-08 07:42:06 +00:00
}
2020-04-07 09:36:28 +00:00
private void updateRtpSessionProposalState(final Account account, final Jid with, final RtpEndUserState state) {
final Intent currentIntent = getIntent();
final String withExtra = currentIntent == null ? null : currentIntent.getStringExtra(EXTRA_WITH);
if (withExtra == null) {
2020-04-08 07:42:06 +00:00
return;
}
if (Jid.ofEscaped(withExtra).asBareJid().equals(with)) {
2020-04-08 07:42:06 +00:00
runOnUiThread(() -> {
updateStateDisplay(state);
updateButtonConfiguration(state);
});
resetIntent(account, with, state);
2020-04-08 07:42:06 +00:00
}
2020-04-07 09:36:28 +00:00
}
private void resetIntent(final Account account, Jid with, final RtpEndUserState state) {
Log.d(Config.LOGTAG, "resetting intent");
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(EXTRA_WITH, with.asBareJid().toEscapedString());
intent.putExtra(EXTRA_ACCOUNT, account.getJid().toEscapedString());
intent.putExtra(EXTRA_LAST_REPORTED_STATE, state.toString());
setIntent(intent);
}
2020-04-07 09:36:28 +00:00
}