apps/Phone: Support for CDMA caller name presentation (CNAP)

Phone app changes to support the network mechanism used in CDMA
to send caller name information from the network during call setup.
diff --git a/src/com/android/phone/CallCard.java b/src/com/android/phone/CallCard.java
index 1fc197b..1ad3cf7 100755
--- a/src/com/android/phone/CallCard.java
+++ b/src/com/android/phone/CallCard.java
@@ -532,10 +532,20 @@
                     if (DBG) log("- displayMainCallStatus: using data we already have...");
                     if (o instanceof CallerInfo) {
                         CallerInfo ci = (CallerInfo) o;
+                        // Update CNAP information if Phone state change occurred
+                        ci.cnapName = conn.getCnapName();
+                        ci.numberPresentation = conn.getNumberPresentation();
+                        ci.namePresentation = conn.getCnapNamePresentation();
+                        if (DBG) log("- displayMainCallStatus: CNAP data from Connection: "
+                                + "CNAP name=" + ci.cnapName
+                                + ", Number/Name Presentation=" + ci.numberPresentation);
                         if (DBG) log("   ==> Got CallerInfo; updating display: ci = " + ci);
                         updateDisplayForPerson(ci, presentation, false, call);
                     } else if (o instanceof PhoneUtils.CallerInfoToken){
                         CallerInfo ci = ((PhoneUtils.CallerInfoToken) o).currentInfo;
+                        if (DBG) log("- displayMainCallStatus: CNAP data from Connection: "
+                                + "CNAP name=" + ci.cnapName
+                                + ", Number/Name Presentation=" + ci.numberPresentation);
                         if (DBG) log("   ==> Got CallerInfoToken; updating display: ci = " + ci);
                         updateDisplayForPerson(ci, presentation, true, call);
                     } else {
@@ -619,7 +629,20 @@
             // If the object is a textview instead, we update it as we need to.
             if (DBG) log("callerinfo query complete, updating ui from displayMainCallStatus()");
             Call call = (Call) cookie;
-            updateDisplayForPerson(ci, Connection.PRESENTATION_ALLOWED, false, call);
+            Connection conn = call.getEarliestConnection();
+            PhoneUtils.CallerInfoToken cit =
+                   PhoneUtils.startGetCallerInfo(getContext(), conn, this, null);
+            int presentation = conn.getNumberPresentation();
+            if (DBG) log("- onQueryComplete: presentation=" + presentation
+                    + ", contactExists=" + ci.contactExists);
+            // Depending on whether there was a contact match or not, we want to pass in different
+            // CallerInfo (for CNAP). Therefore if ci.contactExists then use the ci passed in.
+            // Otherwise, regenerate the CIT from the Connection and use the CallerInfo from there.
+            if (ci.contactExists) {
+                updateDisplayForPerson(ci, Connection.PRESENTATION_ALLOWED, false, call);
+            } else {
+                updateDisplayForPerson(cit.currentInfo, presentation, false, call);
+            }
             updatePhotoForCallState(call);
 
         } else if (cookie instanceof TextView){
@@ -1076,13 +1099,29 @@
             if (TextUtils.isEmpty(info.name)) {
                 if (TextUtils.isEmpty(info.phoneNumber)) {
                     name =  getPresentationString(presentation);
+                } else if (presentation != Connection.PRESENTATION_ALLOWED) {
+                    // This case should never happen since the network should never send a phone #
+                    // AND a restricted presentation. However we leave it here in case of weird
+                    // network behavior
+                    name = getPresentationString(presentation);
+                } else if (!TextUtils.isEmpty(info.cnapName)) {
+                    name = info.cnapName;
+                    info.name = info.cnapName;
+                    displayNumber = info.phoneNumber;
                 } else {
                     name = info.phoneNumber;
                 }
             } else {
-                name = info.name;
-                displayNumber = info.phoneNumber;
-                label = info.phoneLabel;
+                if (presentation != Connection.PRESENTATION_ALLOWED) {
+                    // This case should never happen since the network should never send a name
+                    // AND a restricted presentation. However we leave it here in case of weird
+                    // network behavior
+                    name = getPresentationString(presentation);
+                } else {
+                    name = info.name;
+                    displayNumber = info.phoneNumber;
+                    label = info.phoneLabel;
+                }
             }
             personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.person_id);
         } else {
diff --git a/src/com/android/phone/CallNotifier.java b/src/com/android/phone/CallNotifier.java
index 8448218..1c674ec 100755
--- a/src/com/android/phone/CallNotifier.java
+++ b/src/com/android/phone/CallNotifier.java
@@ -684,6 +684,7 @@
         if (c != null) {
             final String number = c.getAddress();
             final int presentation = c.getNumberPresentation();
+            if (DBG) log("- onDisconnect: presentation=" + presentation);
             final long date = c.getCreateTime();
             final long duration = c.getDurationMillis();
             final Connection.DisconnectCause cause = c.getDisconnectCause();
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index af89636..7e3ab08 100755
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -1041,6 +1041,15 @@
             // handling case where number is null (caller id hidden) as well.
             if (!TextUtils.isEmpty(number)) {
                 cit.currentInfo.phoneNumber = number;
+                // Store CNAP information retrieved from the Connection
+                cit.currentInfo.cnapName =  c.getCnapName();
+                cit.currentInfo.name = cit.currentInfo.cnapName; // This can still get overwritten
+                                                                 // by ContactInfo later
+                cit.currentInfo.numberPresentation = c.getNumberPresentation();
+                cit.currentInfo.namePresentation = c.getCnapNamePresentation();
+                if (DBG) log("startGetCallerInfo: CNAP Info from FW: name="
+                        + cit.currentInfo.cnapName
+                        + ", Name/Number Pres=" + cit.currentInfo.numberPresentation);
                 cit.asyncQuery = CallerInfoAsyncQuery.startQuery(QUERY_TOKEN, context,
                         number, sCallerInfoQueryListener, c);
                 cit.asyncQuery.addQueryListener(QUERY_TOKEN, listener, cookie);
@@ -1097,7 +1106,19 @@
             public void onQueryComplete(int token, Object cookie, CallerInfo ci){
                 if (DBG) log("query complete, updating connection.userdata");
 
-                ((Connection) cookie).setUserData(ci);
+                // Added a check if CallerInfo is coming from ContactInfo or from Connection.
+                // If no ContactInfo, then we want to use CNAP information coming from network
+                if (DBG) log("- onQueryComplete: contactExists=" + ci.contactExists);
+                if (ci.contactExists) {
+                    ((Connection) cookie).setUserData(ci);
+                } else {
+                    CallerInfo newCi = getCallerInfo(null, (Connection) cookie);
+                    if (newCi != null) {
+                        newCi.phoneNumber = ci.phoneNumber; // To get formatted phone number
+                        ((Connection) cookie).setUserData(newCi);
+                    }
+                    else ((Connection) cookie).setUserData(ci);
+                }
             }
         };