Fix crash when making an outgoing call

Fix the Log.wtf exception that occurs because mPendingOutgoingCallIds
does not contain the expected callId

Change-Id: I5cf6b08a2bffd883b89791d3cca9d5fc6b2175f6
diff --git a/src/com/android/telecomm/Call.java b/src/com/android/telecomm/Call.java
index 6660dc3..0b60c4a 100644
--- a/src/com/android/telecomm/Call.java
+++ b/src/com/android/telecomm/Call.java
@@ -155,10 +155,7 @@
     void setCallService(CallServiceWrapper callService) {
         Preconditions.checkNotNull(callService);
 
-        if (mCallService != null) {
-            // Should never be the case, basically covering for potential programming errors.
-            decrementAssociatedCallCount(mCallService);
-        }
+        clearCallService();
 
         callService.incrementAssociatedCallCount();
         mCallService = callService;
@@ -168,8 +165,11 @@
      * Clears the associated call service.
      */
     void clearCallService() {
-        decrementAssociatedCallCount(mCallService);
-        mCallService = null;
+        if (mCallService != null) {
+            decrementAssociatedCallCount(mCallService);
+            mCallService.cancelOutgoingCall(getId());
+            mCallService = null;
+        }
     }
 
     void setCallServiceSelector(ICallServiceSelector selector) {
diff --git a/src/com/android/telecomm/CallServiceWrapper.java b/src/com/android/telecomm/CallServiceWrapper.java
index c4d033c..807f427 100644
--- a/src/com/android/telecomm/CallServiceWrapper.java
+++ b/src/com/android/telecomm/CallServiceWrapper.java
@@ -91,8 +91,10 @@
             @Override public void onSuccess() {
                 if (isServiceValid("isCompatibleWith")) {
                     try {
+                        mAdapter.addPendingOutgoingCallId(callInfo.getId());
                         mServiceInterface.isCompatibleWith(callInfo);
                     } catch (RemoteException e) {
+                        mAdapter.removePendingOutgoingCallId(callInfo.getId());
                         Log.e(CallServiceWrapper.this, e, "Failed checking isCompatibleWith.");
                     }
                 }
@@ -119,7 +121,6 @@
                 if (isServiceValid("call")) {
                     try {
                         mServiceInterface.call(callInfo);
-                        mAdapter.addPendingOutgoingCallId(callId);
                     } catch (RemoteException e) {
                         Log.e(CallServiceWrapper.this, e, "Failed to place call %s", callId);
                     }
@@ -225,6 +226,15 @@
         mAdapter.removePendingIncomingCallId(callId);
     }
 
+    /**
+     * Cancels the outgoing call for the specified call ID.
+     *
+     * @param callId The ID of the call.
+     */
+    void cancelOutgoingCall(String callId) {
+        mAdapter.removePendingOutgoingCallId(callId);
+    }
+
     /** {@inheritDoc} */
     @Override protected void setServiceInterface(IBinder binder) {
         mServiceInterface = ICallService.Stub.asInterface(binder);
diff --git a/src/com/android/telecomm/OutgoingCallProcessor.java b/src/com/android/telecomm/OutgoingCallProcessor.java
index a242866..bd49b66 100644
--- a/src/com/android/telecomm/OutgoingCallProcessor.java
+++ b/src/com/android/telecomm/OutgoingCallProcessor.java
@@ -165,8 +165,9 @@
      *     false otherwise.
      */
     void setIsCompatibleWith(String callId, boolean isCompatible) {
-      if (callId != mCall.getId()) {
-          Log.wtf(this, "setIsCompatibleWith invoked with unexpected call ID: %s", callId);
+      if (!callId.equals(mCall.getId())) {
+          Log.wtf(this, "setIsCompatibleWith invoked with unexpected call ID: %s - expected call"
+                  + " ID: %s", callId, mCall.getId());
           return;
       }