Fix NPE when answering a call that has already gone away

Stephen Paul Weber created

Change summary

src/cheogram/java/com/cheogram/android/ConnectionService.java | 23 +++-
1 file changed, 17 insertions(+), 6 deletions(-)

Detailed changes

src/cheogram/java/com/cheogram/android/ConnectionService.java 🔗

@@ -220,7 +220,7 @@ public class ConnectionService extends android.telecom.ConnectionService {
 			if (sessionId == null || !sessionId.equals(this.sessionId)) return;
 			if (rtpConnection == null) {
 				this.with = with; // Store full JID of connection
-				rtpConnection = xmppConnectionService.getJingleConnectionManager().findJingleRtpConnection(account, with, sessionId);
+				findRtpConnection();
 			}
 
 			setStatusHints(new StatusHints(null, gatewayIcon, null));
@@ -269,15 +269,20 @@ public class ConnectionService extends android.telecom.ConnectionService {
 		public void onAnswer() {
 			// For incoming calls, a connection update may not have been triggered before answering
 			// so we have to acquire the rtp connection object here
-			this.rtpConnection = xmppConnectionService.getJingleConnectionManager().findJingleRtpConnection(account, with, sessionId);
-
-			rtpConnection.get().acceptCall();
+			findRtpConnection();
+			if (rtpConnection == null || rtpConnection.get() == null) {
+				close(new DisconnectCause(DisconnectCause.CANCELED));
+			} else {
+				rtpConnection.get().acceptCall();
+			}
 		}
 
 		@Override
 		public void onReject() {
-			this.rtpConnection = xmppConnectionService.getJingleConnectionManager().findJingleRtpConnection(account, with, sessionId);
-			rtpConnection.get().rejectCall();
+			findRtpConnection();
+			if (rtpConnection != null && rtpConnection.get() != null) {
+				rtpConnection.get().rejectCall();
+			}
 			close(new DisconnectCause(DisconnectCause.LOCAL));
 		}
 
@@ -316,6 +321,12 @@ public class ConnectionService extends android.telecom.ConnectionService {
 			if (c) postDial();
 		}
 
+		protected void findRtpConnection() {
+			if (rtpConnection != null) return;
+
+			rtpConnection = xmppConnectionService.getJingleConnectionManager().findJingleRtpConnection(account, with, sessionId);
+		}
+
 		protected void sleep(int ms) {
 			try {
 				Thread.sleep(ms);