Merge "Add column CAN_REPORT_AS_INVALID_NUMBER to the annotated call log."
diff --git a/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java b/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java
index 8c6d586..68d4b95 100644
--- a/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java
+++ b/java/com/android/dialer/calllog/database/AnnotatedCallLogDatabaseHelper.java
@@ -59,7 +59,8 @@
           + (AnnotatedCallLog.IS_VOICEMAIL + " integer, ")
           + (AnnotatedCallLog.TRANSCRIPTION + " integer, ")
           + (AnnotatedCallLog.VOICEMAIL_URI + " text, ")
-          + (AnnotatedCallLog.CALL_TYPE + " integer")
+          + (AnnotatedCallLog.CALL_TYPE + " integer, ")
+          + (AnnotatedCallLog.CAN_REPORT_AS_INVALID_NUMBER + " integer")
           + ");";
 
   /** Deletes all but the first maxRows rows (by timestamp) to keep the table a manageable size. */
diff --git a/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java b/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java
index 7071ab5..1b3e090 100644
--- a/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java
+++ b/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java
@@ -178,6 +178,13 @@
      */
     String CALL_TYPE = "call_type";
 
+    /**
+     * True if the number can be reported as invalid.
+     *
+     * <p>TYPE: INTEGER (boolean)
+     */
+    String CAN_REPORT_AS_INVALID_NUMBER = "can_report_as_invalid_number";
+
     String[] ALL_COMMON_COLUMNS =
         new String[] {
           _ID,
@@ -199,7 +206,8 @@
           FEATURES,
           IS_BUSINESS,
           IS_VOICEMAIL,
-          CALL_TYPE
+          CALL_TYPE,
+          CAN_REPORT_AS_INVALID_NUMBER
         };
   }
 
diff --git a/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java b/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java
index 56e909e..2148627 100644
--- a/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java
+++ b/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java
@@ -268,6 +268,7 @@
         .useMostRecentString(AnnotatedCallLog.PHOTO_URI)
         .useMostRecentLong(AnnotatedCallLog.PHOTO_ID)
         .useMostRecentString(AnnotatedCallLog.LOOKUP_URI)
+        .useMostRecentInt(AnnotatedCallLog.CAN_REPORT_AS_INVALID_NUMBER)
         .combine();
   }
 
@@ -564,6 +565,9 @@
         AnnotatedCallLog.LOOKUP_URI, PhoneLookupSelector.selectLookupUri(phoneLookupInfo));
     contentValues.put(
         AnnotatedCallLog.NUMBER_TYPE_LABEL, PhoneLookupSelector.selectNumberLabel(phoneLookupInfo));
+    contentValues.put(
+        AnnotatedCallLog.CAN_REPORT_AS_INVALID_NUMBER,
+        PhoneLookupSelector.canReportAsInvalidNumber(phoneLookupInfo));
   }
 
   private static Uri numberUri(String number) {
diff --git a/java/com/android/dialer/calllog/datasources/util/RowCombiner.java b/java/com/android/dialer/calllog/datasources/util/RowCombiner.java
index ebb1ba6..6e33db5 100644
--- a/java/com/android/dialer/calllog/datasources/util/RowCombiner.java
+++ b/java/com/android/dialer/calllog/datasources/util/RowCombiner.java
@@ -32,6 +32,13 @@
   }
 
   /** Use the most recent value for the specified column. */
+  public RowCombiner useMostRecentInt(String columnName) {
+    combinedRow.put(
+        columnName, individualRowsSortedByTimestampDesc.get(0).getAsInteger(columnName));
+    return this;
+  }
+
+  /** Use the most recent value for the specified column. */
   public RowCombiner useMostRecentLong(String columnName) {
     combinedRow.put(columnName, individualRowsSortedByTimestampDesc.get(0).getAsLong(columnName));
     return this;
diff --git a/java/com/android/dialer/calllog/model/CoalescedRow.java b/java/com/android/dialer/calllog/model/CoalescedRow.java
index 5cc0568..1824ba1 100644
--- a/java/com/android/dialer/calllog/model/CoalescedRow.java
+++ b/java/com/android/dialer/calllog/model/CoalescedRow.java
@@ -39,6 +39,7 @@
         .setIsBusiness(false)
         .setIsVoicemail(false)
         .setCallType(0)
+        .setCanReportAsInvalidNumber(false)
         .setCoalescedIds(CoalescedIds.getDefaultInstance());
   }
 
@@ -92,6 +93,8 @@
 
   public abstract int callType();
 
+  public abstract boolean canReportAsInvalidNumber();
+
   public abstract CoalescedIds coalescedIds();
 
   /** Builder for {@link CoalescedRow}. */
@@ -139,6 +142,8 @@
 
     public abstract Builder setCallType(int callType);
 
+    public abstract Builder setCanReportAsInvalidNumber(boolean canReportAsInvalidNumber);
+
     public abstract Builder setCoalescedIds(CoalescedIds coalescedIds);
 
     public abstract CoalescedRow build();
diff --git a/java/com/android/dialer/calllog/ui/CoalescedAnnotatedCallLogCursorLoader.java b/java/com/android/dialer/calllog/ui/CoalescedAnnotatedCallLogCursorLoader.java
index 8aefb1a..6d60bdd 100644
--- a/java/com/android/dialer/calllog/ui/CoalescedAnnotatedCallLogCursorLoader.java
+++ b/java/com/android/dialer/calllog/ui/CoalescedAnnotatedCallLogCursorLoader.java
@@ -49,7 +49,8 @@
   private static final int IS_BUSINESS = 17;
   private static final int IS_VOICEMAIL = 18;
   private static final int CALL_TYPE = 19;
-  private static final int COALESCED_IDS = 20;
+  private static final int CAN_REPORT_AS_INVALID_NUMBER = 20;
+  private static final int COALESCED_IDS = 21;
 
   CoalescedAnnotatedCallLogCursorLoader(Context context) {
     // CoalescedAnnotatedCallLog requires that PROJECTION be ALL_COLUMNS and the following params be
@@ -100,6 +101,7 @@
         .setIsBusiness(cursor.getInt(IS_BUSINESS) == 1)
         .setIsVoicemail(cursor.getInt(IS_VOICEMAIL) == 1)
         .setCallType(cursor.getInt(CALL_TYPE))
+        .setCanReportAsInvalidNumber(cursor.getInt(CAN_REPORT_AS_INVALID_NUMBER) == 1)
         .setCoalescedIds(coalescedIds)
         .build();
   }
diff --git a/java/com/android/dialer/phonelookup/PhoneLookupSelector.java b/java/com/android/dialer/phonelookup/PhoneLookupSelector.java
index 0fe9893..c933af7 100644
--- a/java/com/android/dialer/phonelookup/PhoneLookupSelector.java
+++ b/java/com/android/dialer/phonelookup/PhoneLookupSelector.java
@@ -18,6 +18,8 @@
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import com.android.dialer.phonelookup.PhoneLookupInfo.Cp2Info.Cp2ContactInfo;
+import com.android.dialer.phonelookup.PhoneLookupInfo.PeopleApiInfo;
+import com.android.dialer.phonelookup.PhoneLookupInfo.PeopleApiInfo.InfoType;
 
 /**
  * Prioritizes information from a {@link PhoneLookupInfo}.
@@ -107,6 +109,27 @@
   }
 
   /**
+   * Returns true if the number associated with the given {@link PhoneLookupInfo} can be reported as
+   * invalid.
+   *
+   * <p>As we currently report invalid numbers via the People API, only numbers from the People API
+   * can be reported as invalid.
+   */
+  public static boolean canReportAsInvalidNumber(PhoneLookupInfo phoneLookupInfo) {
+    // The presence of Cp2ContactInfo means the number associated with the given PhoneLookupInfo
+    // matches that of a Cp2 (local) contact, and PeopleApiInfo will not be used to display
+    // information like name, photo, etc. We should not allow the user to report the number in this
+    // case as the info displayed is not from the People API.
+    if (phoneLookupInfo.getCp2Info().getCp2ContactInfoCount() > 0) {
+      return false;
+    }
+
+    PeopleApiInfo peopleApiInfo = phoneLookupInfo.getPeopleApiInfo();
+    return peopleApiInfo.getInfoType() != InfoType.UNKNOWN
+        && !peopleApiInfo.getPersonId().isEmpty();
+  }
+
+  /**
    * Arbitrarily select the first contact. In the future, it may make sense to display contact
    * information from all contacts with the same number (for example show the name as "Mom, Dad" or
    * show a synthesized photo containing photos of both "Mom" and "Dad").