Use the new incoming call confirmation APIs.

Update Telecomm and Telecomm test package to use the new
confirm-incoming-call APIs including confirmIncomingCall and
handleConfirmedIncomingCall.  Test call service always confirms all
incoming calls.

Also adds a list of unconfirmed incoming calls to the adapter. The
purpose of this is for the adapter to know which incoming call
confirmations to accept and which to ignore.

Change-Id: I914f25c45e5c887ca84d181d3dc173f25577e520
diff --git a/src/com/android/telecomm/CallServiceAdapter.java b/src/com/android/telecomm/CallServiceAdapter.java
index 6308567..9a9d0db 100644
--- a/src/com/android/telecomm/CallServiceAdapter.java
+++ b/src/com/android/telecomm/CallServiceAdapter.java
@@ -20,9 +20,13 @@
 import android.os.Looper;
 import android.telecomm.CallInfo;
 import android.telecomm.ICallServiceAdapter;
+import android.util.Log;
 
+import com.google.android.collect.Sets;
 import com.google.common.base.Strings;
 
+import java.util.Set;
+
 /**
  * Used by call services in order to update state and control calls while the call service is bound
  * to Telecomm. Each call service is given its own instance for the lifetime of the binding between
@@ -34,6 +38,7 @@
  * TODO(santoscordon): Do we need Binder.clear/restoreCallingIdentity() in the service methods?
  */
 public final class CallServiceAdapter extends ICallServiceAdapter.Stub {
+    private static final String TAG = CallServiceAdapter.class.getSimpleName();
 
     private final CallsManager mCallsManager;
 
@@ -42,6 +47,14 @@
     /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */
     private final Handler mHandler = new Handler(Looper.getMainLooper());
 
+    /** The list of unconfirmed incoming call IDs. Contains only IDs for incoming calls which are
+     * pending confirmation from the call service. Entries are added by the call service when a
+     * confirmation request is sent and removed when the confirmation is received or it times out.
+     * See {@link IncomingCallsManager} for more information about the incoming sequence and its
+     * timeouts.
+     */
+    private final Set<String> mUnconfirmedIncomingCallIds = Sets.newHashSet();
+
     /**
      * Persists the specified parameters.
      */
@@ -51,20 +64,23 @@
     }
 
     /** {@inheritDoc} */
-    @Override public void getNextCallId() {
-        // TODO(santoscordon): needs response object.
-    }
-
-    /** {@inheritDoc} */
     @Override public void setCompatibleWith(String callId, boolean isCompatible) {
         // TODO(santoscordon): fill in.
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override public void handleIncomingCall(CallInfo callInfo) {
-        // TODO(santoscordon): fill in.
+    /** {@inheritDoc} */
+    @Override public void handleConfirmedIncomingCall(final CallInfo callInfo) {
+        checkValidCallId(callInfo.getId());
+        mHandler.post(new Runnable() {
+            @Override public void run() {
+                if (mUnconfirmedIncomingCallIds.remove(callInfo.getId())) {
+                    // TODO(santoscordon): Uncomment when ready.
+                    // mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo);
+                } else {
+                    Log.wtf(TAG, "Call service confirming unknown incoming call " + callInfo);
+                }
+            }
+        });
     }
 
     /** {@inheritDoc} */
@@ -128,13 +144,32 @@
     }
 
     /**
+     * Adds a call ID to the list of unconfirmed incoming call IDs. Only calls with call IDs in the
+     * list will be handled by {@link #handleConfirmedIncomingCall}.
+     *
+     * @param callId The ID of the call.
+     */
+    void addUnconfirmedIncomingCallId(String callId) {
+        mUnconfirmedIncomingCallIds.add(callId);
+    }
+
+    /**
+     * Removed a call ID from the list of unconfirmed incoming call IDs.
+     *
+     * @param callId The ID of the call.
+     */
+    void removeUnconfirmedIncomingCallId(String callId) {
+        mUnconfirmedIncomingCallIds.remove(callId);
+    }
+
+    /**
      * Throws an IllegalArgumentException if the specified call ID is invalid.
      *
      * @param callId The call ID to check.
      */
     private void checkValidCallId(String callId) {
         if (Strings.isNullOrEmpty(callId)) {
-            throw new IllegalArgumentException();
+            throw new IllegalArgumentException("Invalid call ID.");
         }
     }
 }