Add CallsManagerListener

Also contains some minor bug fixes:
- add an incoming field to Call
- correcty log failed outgoing calls (previously these were mostly dropped)
- log missed incoming calls

Change-Id: I72dc39efd519302c1f765f4f9c9d04c5095e45a6
diff --git a/src/com/android/telecomm/Call.java b/src/com/android/telecomm/Call.java
index 849c876..82bde9e 100644
--- a/src/com/android/telecomm/Call.java
+++ b/src/com/android/telecomm/Call.java
@@ -41,6 +41,9 @@
     /** Additional contact information beyond handle above, optional. */
     private final ContactInfo mContactInfo;
 
+    /** True if this is an incoming call. */
+    private final boolean mIsIncoming;
+
     /**
      * The time this call was created, typically also the time this call was added to the set
      * of pending outgoing calls (mPendingOutgoingCalls) that's maintained by the switchboard.
@@ -77,9 +80,11 @@
 
     /**
      * Creates an empty call object with a unique call ID.
+     *
+     * @param isIncoming True if this is an incoming call.
      */
-    Call() {
-        this(null, null);
+    Call(boolean isIncoming) {
+        this(null, null, isIncoming);
     }
 
     /**
@@ -87,19 +92,22 @@
      *
      * @param handle The handle to dial.
      * @param contactInfo Information about the entity being called.
+     * @param isIncoming True if this is an incoming call.
      */
-    Call(Uri handle, ContactInfo contactInfo) {
+    Call(Uri handle, ContactInfo contactInfo, boolean isIncoming) {
         mId = UUID.randomUUID().toString();  // UUIDs should provide sufficient uniqueness.
         mState = CallState.NEW;
         mHandle = handle;
         mContactInfo = contactInfo;
+        mIsIncoming = isIncoming;
         mCreationTime = new Date();
     }
 
     /** {@inheritDoc} */
     @Override public String toString() {
         return String.format(Locale.US, "[%s, %s, %s, %s]", mId, mState,
-                mCallService.getComponentName(), Log.pii(mHandle));
+                mCallService == null ? "<null>" : mCallService.getComponentName(),
+                Log.pii(mHandle));
     }
 
     String getId() {
@@ -116,9 +124,12 @@
      * misbehave and they do this very often. The result is that we do not enforce state transitions
      * and instead keep the code resilient to unexpected state changes.
      */
-    void setState(CallState state) {
-        mState = state;
-        clearCallInfo();
+    void setState(CallState newState) {
+        if (mState != newState) {
+            Log.v(this, "setState %s -> %s", mState, newState);
+            mState = newState;
+            clearCallInfo();
+        }
     }
 
     Uri getHandle() {
@@ -133,6 +144,10 @@
         return mContactInfo;
     }
 
+    boolean isIncoming() {
+        return mIsIncoming;
+    }
+
     /**
      * @return The "age" of this call object in milliseconds, which typically also represents the
      *     period since this call was added to the set pending outgoing calls, see mCreationTime.
@@ -222,7 +237,6 @@
             }
             clearCallService();
             clearCallServiceSelector();
-            mState = CallState.ABORTED;
         }
     }