ConnectionService: handle connection events on the main thread

Not handling these events on the main thread could leave the connection
in an inconsistent state.
This commit is contained in:
Peter Cai 2022-03-12 21:30:03 -05:00
parent d8d49e03a0
commit 5e39d45d9a
1 changed files with 27 additions and 14 deletions

View File

@ -11,6 +11,8 @@ import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.telecom.CallAudioState;
import android.telecom.Connection;
import android.telecom.ConnectionRequest;
@ -54,6 +56,8 @@ import eu.siacs.conversations.xmpp.jingle.Media;
import eu.siacs.conversations.xmpp.jingle.RtpEndUserState;
public class ConnectionService extends android.telecom.ConnectionService {
private final Handler mHandler = new Handler(Looper.getMainLooper());
public XmppConnectionService xmppConnectionService = null;
protected ServiceConnection mConnection = new ServiceConnection() {
@Override
@ -271,28 +275,35 @@ public class ConnectionService extends android.telecom.ConnectionService {
// so we have to acquire the rtp connection object here
this.rtpConnection = xmppConnectionService.getJingleConnectionManager().findJingleRtpConnection(account, with, sessionId);
rtpConnection.get().acceptCall();
mHandler.post(() -> {
rtpConnection.get().acceptCall();
});
}
@Override
public void onReject() {
this.rtpConnection = xmppConnectionService.getJingleConnectionManager().findJingleRtpConnection(account, with, sessionId);
rtpConnection.get().rejectCall();
setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
mHandler.post(() -> {
rtpConnection.get().rejectCall();
setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
});
}
@Override
public void onDisconnect() {
if (rtpConnection == null || rtpConnection.get() == null) {
xmppConnectionService.getJingleConnectionManager().retractSessionProposal(account, with.asBareJid());
} else {
rtpConnection.get().endCall();
}
destroy();
xmppConnectionService.setDiallerIntegrationActive(false);
xmppConnectionService.removeRtpConnectionUpdateListener(
(XmppConnectionService.OnJingleRtpConnectionUpdate) this
);
mHandler.post(() -> {
if (rtpConnection == null || rtpConnection.get() == null) {
xmppConnectionService.getJingleConnectionManager().retractSessionProposal(account, with.asBareJid());
} else {
rtpConnection.get().endCall();
}
destroy();
xmppConnectionService.setDiallerIntegrationActive(false);
xmppConnectionService.removeRtpConnectionUpdateListener(
(XmppConnectionService.OnJingleRtpConnectionUpdate) this
);
});
}
@Override
@ -302,7 +313,9 @@ public class ConnectionService extends android.telecom.ConnectionService {
@Override
public void onPlayDtmfTone(char c) {
rtpConnection.get().applyDtmfTone("" + c);
mHandler.post(() -> {
rtpConnection.get().applyDtmfTone("" + c);
});
}
@Override