fix NPE in CSW#oCC when the ServiceInterface is cleared out

A crash cluster revealed a race condition that causes an NPE on CF
devices. Although this is not repro on local devices, a fix should
be merged.

Fixes: 364811868
Test: build + ran the failing test
Flag: com.android.server.telecom.flags.csw_service_interface_is_null
Change-Id: I05f5a767c47e43f163335dc87a78ea883a55f277
diff --git a/flags/telecom_connection_service_wrapper_flags.aconfig b/flags/telecom_connection_service_wrapper_flags.aconfig
index 38e5e13..8e77af5 100644
--- a/flags/telecom_connection_service_wrapper_flags.aconfig
+++ b/flags/telecom_connection_service_wrapper_flags.aconfig
@@ -7,4 +7,15 @@
   namespace: "telecom"
   description: "Ensure that the associatedCallCount of CS and RCS is accurately being tracked."
   bug: "286154316"
+}
+
+# OWNER=tjstuart TARGET=24Q4
+flag {
+  name: "csw_service_interface_is_null"
+  namespace: "telecom"
+  description: "fix potential NPE in onCreateConnection when the ServiceInterface is cleared out"
+  bug: "364811868"
+    metadata {
+        purpose: PURPOSE_BUGFIX
+      }
 }
\ No newline at end of file
diff --git a/src/com/android/server/telecom/ConnectionServiceWrapper.java b/src/com/android/server/telecom/ConnectionServiceWrapper.java
index bf25f38..328b8ac 100644
--- a/src/com/android/server/telecom/ConnectionServiceWrapper.java
+++ b/src/com/android/server/telecom/ConnectionServiceWrapper.java
@@ -1712,14 +1712,19 @@
                         .setRttPipeToInCall(call.getCsToInCallRttPipeForCs())
                         .build();
                 try {
-                    mServiceInterface.createConnection(
-                            call.getConnectionManagerPhoneAccount(),
-                            callId,
-                            connectionRequest,
-                            call.shouldAttachToExistingConnection(),
-                            call.isUnknown(),
-                            Log.getExternalSession(TELECOM_ABBREVIATION));
-
+                    if (mFlags.cswServiceInterfaceIsNull() && mServiceInterface == null) {
+                        mPendingResponses.remove(callId).handleCreateConnectionFailure(
+                                new DisconnectCause(DisconnectCause.ERROR,
+                                        "CSW#oCC ServiceInterface is null"));
+                    } else {
+                        mServiceInterface.createConnection(
+                                call.getConnectionManagerPhoneAccount(),
+                                callId,
+                                connectionRequest,
+                                call.shouldAttachToExistingConnection(),
+                                call.isUnknown(),
+                                Log.getExternalSession(TELECOM_ABBREVIATION));
+                    }
                 } catch (RemoteException e) {
                     Log.e(this, e, "Failure to createConnection -- %s", getComponentName());
                     mPendingResponses.remove(callId).handleCreateConnectionFailure(