Adding some missing incoming CallManager logic.

Change-Id: I0ce0218561a40de02313771e1f36b31914ad6eac
diff --git a/src/com/android/telecomm/CallActivity.java b/src/com/android/telecomm/CallActivity.java
index 350bf56..7fc027f 100644
--- a/src/com/android/telecomm/CallActivity.java
+++ b/src/com/android/telecomm/CallActivity.java
@@ -17,7 +17,6 @@
 package com.android.telecomm;
 
 import android.app.Activity;
-import android.content.Context;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.os.Bundle;
@@ -26,8 +25,6 @@
 import android.util.Log;
 import android.widget.Toast;
 
-import com.android.telecomm.exceptions.RestrictedCallException;
-
 /**
  * Activity that handles system CALL actions and forwards them to {@link CallsManager}.
  * Handles all three CALL action types: CALL, CALL_PRIVILEGED, and CALL_EMERGENCY.
@@ -112,11 +109,7 @@
         // non-trivial/voluminous.
         String handle = intent.getDataString();
         ContactInfo contactInfo = null;
-        try {
-            mCallsManager.processOutgoingCallIntent(handle, contactInfo);
-        } catch (RestrictedCallException e) {
-            // TODO(gilad): Handle or explicitly state to be ignored.
-        }
+        mCallsManager.processOutgoingCallIntent(handle, contactInfo);
     }
 
     /**
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 0afb6e0..9922102 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -20,8 +20,6 @@
 import android.telecomm.CallState;
 import android.util.Log;
 
-import com.android.telecomm.exceptions.RestrictedCallException;
-
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
@@ -38,6 +36,7 @@
  * beyond the com.android.telecomm package boundary.
  */
 public final class CallsManager {
+
     private static final String TAG = CallsManager.class.getSimpleName();
 
     private static final CallsManager INSTANCE = new CallsManager();
@@ -72,10 +71,6 @@
 
     private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
 
-    static CallsManager getInstance() {
-        return INSTANCE;
-    }
-
     /**
      * Initializes the required Telecomm components.
      */
@@ -84,6 +79,10 @@
         mInCallController = new InCallController(this);
     }
 
+    static CallsManager getInstance() {
+        return INSTANCE;
+    }
+
     /**
      * Starts the incoming call sequence by having switchboard confirm with the specified call
      * service that an incoming call actually exists for the specified call token. Upon success,
@@ -103,6 +102,31 @@
     }
 
     /**
+     * Validates the specified call and, upon no objection to connect it, adds the new call to the
+     * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
+     *
+     * @param call The new incoming call.
+     */
+    void handleSuccessfulIncomingCall(Call call) {
+        Log.d(TAG, "handleSuccessfulIncomingCall");
+        Preconditions.checkState(call.getState() == CallState.RINGING);
+
+        String handle = call.getHandle();
+        ContactInfo contactInfo = call.getContactInfo();
+        for (IncomingCallValidator validator : mIncomingCallValidators) {
+            if (!validator.isValid(handle, contactInfo)) {
+                // TODO(gilad): Consider displaying an error message.
+                Log.i(TAG, "Dropping restricted incoming call");
+                return;
+            }
+        }
+
+        // No objection to accept the incoming call, proceed with potentially connecting it (based
+        // on the user's action, or lack thereof).
+        addCall(call);
+    }
+
+    /**
      * Attempts to issue/connect the specified call.  From an (arbitrary) application standpoint,
      * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
      * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
@@ -111,11 +135,13 @@
      * @param handle The handle to dial.
      * @param contactInfo Information about the entity being called.
      */
-    void processOutgoingCallIntent(String handle, ContactInfo contactInfo)
-            throws RestrictedCallException {
-
+    void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
         for (OutgoingCallValidator validator : mOutgoingCallValidators) {
-            validator.validate(handle, contactInfo);
+            if (!validator.isValid(handle, contactInfo)) {
+                // TODO(gilad): Display an error message.
+                Log.i(TAG, "Dropping restricted outgoing call.");
+                return;
+            }
         }
 
         // No objection to issue the call, proceed with trying to put it through.
@@ -133,37 +159,7 @@
         // placed call from the call service so there is no need to set it here. Instead, check that
         // the state is appropriate.
         Preconditions.checkState(call.getState() == CallState.DIALING);
-
         addCall(call);
-
-        mInCallController.addCall(call.toCallInfo());
-    }
-
-    /**
-     * Adds a new incoming call to the list of live calls and notifies the in-call app.
-     *
-     * @param call The new incoming call.
-     */
-    void handleSuccessfulIncomingCall(Call call) {
-        Log.d(TAG, "handleSuccessfulIncomingCall");
-        Preconditions.checkState(call.getState() == CallState.RINGING);
-        addCall(call);
-        mInCallController.addCall(call.toCallInfo());
-    }
-
-    /*
-     * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
-     * tells the in-call controller to unbind since it is not needed.
-     */
-    void updateInCall() {
-        if (mCalls.isEmpty()) {
-            mInCallController.unbind();
-            return;
-        }
-
-        for (Call call : mCalls.values()) {
-            mInCallController.addCall(call.toCallInfo());
-        }
     }
 
     /**
@@ -254,6 +250,37 @@
     }
 
     /**
+     * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
+     * tells the in-call controller to unbind since it is not needed.
+     */
+    void updateInCall() {
+        if (mCalls.isEmpty()) {
+            mInCallController.unbind();
+        } else {
+            for (Call call : mCalls.values()) {
+                addInCallEntry(call);
+            }
+        }
+    }
+
+    /**
+     * Adds the specified call to the main list of live calls.
+     *
+     * @param call The call to add.
+     */
+    private void addCall(Call call) {
+        mCalls.put(call.getId(), call);
+        addInCallEntry(call);
+    }
+
+    /**
+     * Notifies the in-call app of the specified (new) call.
+     */
+    private void addInCallEntry(Call call) {
+        mInCallController.addCall(call.toCallInfo());
+    }
+
+    /**
      * Sets the specified state on the specified call.
      *
      * @param callId The ID of the call to update.
@@ -279,13 +306,4 @@
             // TODO(santoscordon): Notify the in-call app whenever a call changes state.
         }
     }
-
-    /**
-     * Adds the specified call to the main list of live calls.
-     *
-     * @param call The call to add.
-     */
-    private void addCall(Call call) {
-        mCalls.put(call.getId(), call);
-    }
 }
diff --git a/src/com/android/telecomm/IncomingCallValidator.java b/src/com/android/telecomm/IncomingCallValidator.java
index 95ff477..75ad10b 100644
--- a/src/com/android/telecomm/IncomingCallValidator.java
+++ b/src/com/android/telecomm/IncomingCallValidator.java
@@ -1,6 +1,27 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package com.android.telecomm;
 
-// Can be used to reject incoming calls, see OutgoingCallFilter regarding
-// outgoing calls.
+/**
+ * Implementations can be used to reject incoming calls based on arbitrary restrictions across call
+ * services (e.g. suppressing calls from certain phone numbers regardless if they are received over
+ * PSTN or WiFi). See OutgoingCallValidator regarding outgoing calls.
+ */
 public interface IncomingCallValidator {
+
+    boolean isValid(String handle, ContactInfo contactInfo);
 }
diff --git a/src/com/android/telecomm/OutgoingCallValidator.java b/src/com/android/telecomm/OutgoingCallValidator.java
index 5e998c6..0553b58 100644
--- a/src/com/android/telecomm/OutgoingCallValidator.java
+++ b/src/com/android/telecomm/OutgoingCallValidator.java
@@ -1,15 +1,31 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package com.android.telecomm;
 
-import com.android.telecomm.exceptions.RestrictedCallException;
-
-// Can be used to prevent outgoing calls based on arbitrary restrictions across
-// call services (e.g. black listing a phone number regardless if it is
-// attempted over PSTN or WiFi).  That being the case, FDN which is specific to
-// GSM may need to be implemented separately since these policies are generally
-// invoked before a particular call service is selected.
-// See http://en.wikipedia.org/wiki/Fixed_Dialing_Number and IncomingCallFilter
-// regarding incoming calls.
+/**
+ * Implementations can be used to suppress certain classes of outgoing calls based on arbitrary
+ * restrictions across call services (e.g. black-listing some phone numbers regardless if these
+ * are attempted over PSTN or WiFi). That being the case, FDN which is specific to GSM may need
+ * to be implemented separately since classes implementing this interface are generally invoked
+ * before any given call service is selected.
+ * See http://en.wikipedia.org/wiki/Fixed_Dialing_Number and/or IncomingCallValidator regarding
+ * incoming calls.
+ */
 public interface OutgoingCallValidator {
-  public boolean validate(String userInput, ContactInfo contactInfo)
-      throws RestrictedCallException;
+
+    boolean isValid(String handle, ContactInfo contactInfo);
 }
diff --git a/src/com/android/telecomm/exceptions/RestrictedCallException.java b/src/com/android/telecomm/exceptions/RestrictedCallException.java
deleted file mode 100644
index 616f4fd..0000000
--- a/src/com/android/telecomm/exceptions/RestrictedCallException.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.android.telecomm.exceptions;
-
-public class RestrictedCallException extends Exception {
-  public RestrictedCallException() {
-    super();
-  }
-
-  public RestrictedCallException(String message) {
-    super(message);
-  }
-
-  public RestrictedCallException(String message, Throwable cause) {
-    super(message, cause);
-  }
-
-  public RestrictedCallException(Throwable cause) {
-    super(cause);
-  }
-}