Perform clean up when a call-service dies.

Also add some test call service logic to make this logic
easily testable.

Bug: 13546896

Change-Id: I7e1518488b4cd2d7752c3fee32816cecf734e388
diff --git a/src/com/android/telecomm/CallServiceAdapter.java b/src/com/android/telecomm/CallServiceAdapter.java
index e30d338..f95953a 100644
--- a/src/com/android/telecomm/CallServiceAdapter.java
+++ b/src/com/android/telecomm/CallServiceAdapter.java
@@ -23,7 +23,9 @@
 import com.android.internal.telecomm.ICallServiceAdapter;
 import com.google.android.collect.Sets;
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
 
+import java.util.Collections;
 import java.util.Set;
 
 /**
@@ -222,6 +224,35 @@
     }
 
     /**
+     * Called when the associated call service dies.
+     */
+    void handleCallServiceDeath() {
+        if (!mPendingIncomingCallIds.isEmpty()) {
+            // Here and in the for loop below, we need to iterate through a copy because the code
+            // inside the loop will modify the original list.
+            for (String callId : ImmutableList.copyOf(mPendingIncomingCallIds)) {
+                mIncomingCallsManager.handleFailedIncomingCall(callId);
+            }
+
+            if (!mPendingIncomingCallIds.isEmpty()) {
+                Log.wtf(this, "Pending incoming calls did not get cleared.");
+                mPendingIncomingCallIds.clear();
+            }
+        }
+
+        if (!mPendingOutgoingCallIds.isEmpty()) {
+            for (String callId : ImmutableList.copyOf(mPendingOutgoingCallIds)) {
+                mOutgoingCallsManager.handleFailedCallAttempt(callId, "Call service disconnected.");
+            }
+
+            if (!mPendingOutgoingCallIds.isEmpty()) {
+                Log.wtf(this, "Pending outgoing calls did not get cleared.");
+                mPendingOutgoingCallIds.clear();
+            }
+        }
+    }
+
+    /**
      * Throws an IllegalArgumentException if the specified call ID is invalid.
      *
      * @param callId The call ID to check.