From 5e39d45d9a57eca60efde4b39dd506ab041da95c Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Sat, 12 Mar 2022 21:30:03 -0500 Subject: [PATCH] ConnectionService: handle connection events on the main thread Not handling these events on the main thread could leave the connection in an inconsistent state. --- .../cheogram/android/ConnectionService.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/cheogram/java/com/cheogram/android/ConnectionService.java b/src/cheogram/java/com/cheogram/android/ConnectionService.java index be497a8b8..c66889a47 100644 --- a/src/cheogram/java/com/cheogram/android/ConnectionService.java +++ b/src/cheogram/java/com/cheogram/android/ConnectionService.java @@ -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