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

150 lines
5.3 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-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-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";
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);
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_rtp_session);
this.binding.rejectCall.setOnClickListener(this::rejectCall);
2020-04-07 12:22:12 +00:00
this.binding.endCall.setOnClickListener(this::endCall);
this.binding.acceptCall.setOnClickListener(this::acceptCall);
}
2020-04-07 14:17:29 +00:00
@Override
public void onStart() {
super.onStart();
Log.d(Config.LOGTAG,"RtpSessionActivity.onStart()");
}
2020-04-07 12:22:12 +00:00
private void endCall(View view) {
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() {
}
@Override
void onBackendConnected() {
2020-04-07 11:15:24 +00:00
final Intent intent = getIntent();
final Account account = extractAccount(intent);
final String with = intent.getStringExtra(EXTRA_WITH);
final String sessionId = intent.getStringExtra(EXTRA_SESSION_ID);
if (with != null && sessionId != null) {
final WeakReference<JingleRtpConnection> reference = xmppConnectionService.getJingleConnectionManager()
.findJingleRtpConnection(account, Jid.ofEscaped(with), sessionId);
if (reference == null || reference.get() == null) {
finish();
return;
}
this.rtpConnectionReference = reference;
binding.with.setText(getWith().getDisplayName());
2020-04-07 12:22:12 +00:00
final RtpEndUserState currentState = requireRtpConnection().getEndUserState();
updateStateDisplay(currentState);
updateButtonConfiguration(currentState);
2020-04-07 11:15:24 +00:00
}
}
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;
}
}
private void updateButtonConfiguration(final RtpEndUserState state) {
if (state == RtpEndUserState.INCOMING_CALL) {
this.binding.rejectCall.show();
this.binding.endCall.hide();
this.binding.acceptCall.show();
} else if (state == RtpEndUserState.ENDING_CALL) {
this.binding.rejectCall.hide();
this.binding.endCall.hide();
this.binding.acceptCall.hide();
} else {
this.binding.rejectCall.hide();
this.binding.endCall.show();
this.binding.acceptCall.hide();
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
public void onJingleRtpConnectionUpdate(Account account, Jid with, RtpEndUserState state) {
final AbstractJingleConnection.Id id = requireRtpConnection().getId();
if (account == id.account && id.with.equals(with)) {
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 09:36:28 +00:00
}
}