Added URI for selecting distinct phone numbers from AnnotatedCallLog.

This will be used in the PhoneLookupDataSource. A separate URI is necessary because the content provider APIs do not provide a mechanism for querying using the "distinct" keyword.

Bug: 34672501
Test: unit
PiperOrigin-RevId: 175706603
Change-Id: Ia455a18d10afb116d26f69e8b0c7493f4f877d0b
diff --git a/java/com/android/dialer/calllog/database/AnnotatedCallLogContentProvider.java b/java/com/android/dialer/calllog/database/AnnotatedCallLogContentProvider.java
index 68298e3..825e84f 100644
--- a/java/com/android/dialer/calllog/database/AnnotatedCallLogContentProvider.java
+++ b/java/com/android/dialer/calllog/database/AnnotatedCallLogContentProvider.java
@@ -38,6 +38,7 @@
 import com.android.dialer.common.Assert;
 import com.android.dialer.common.LogUtil;
 import java.util.ArrayList;
+import java.util.Arrays;
 
 /** {@link ContentProvider} for the annotated call log. */
 public class AnnotatedCallLogContentProvider extends ContentProvider {
@@ -51,7 +52,8 @@
 
   private static final int ANNOTATED_CALL_LOG_TABLE_CODE = 1;
   private static final int ANNOTATED_CALL_LOG_TABLE_ID_CODE = 2;
-  private static final int COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE = 3;
+  private static final int ANNOTATED_CALL_LOG_TABLE_DISTINCT_NUMBER_CODE = 3;
+  private static final int COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE = 4;
 
   private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
 
@@ -64,6 +66,10 @@
         ANNOTATED_CALL_LOG_TABLE_ID_CODE);
     uriMatcher.addURI(
         AnnotatedCallLogContract.AUTHORITY,
+        AnnotatedCallLog.DISTINCT_PHONE_NUMBERS,
+        ANNOTATED_CALL_LOG_TABLE_DISTINCT_NUMBER_CODE);
+    uriMatcher.addURI(
+        AnnotatedCallLogContract.AUTHORITY,
         CoalescedAnnotatedCallLog.TABLE,
         COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE);
   }
@@ -101,8 +107,6 @@
     switch (match) {
       case ANNOTATED_CALL_LOG_TABLE_ID_CODE:
         queryBuilder.appendWhere(AnnotatedCallLog._ID + "=" + ContentUris.parseId(uri));
-        // fall through
-      case ANNOTATED_CALL_LOG_TABLE_CODE:
         Cursor cursor =
             queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
         if (cursor != null) {
@@ -112,6 +116,31 @@
           LogUtil.w("AnnotatedCallLogContentProvider.query", "cursor was null");
         }
         return cursor;
+      case ANNOTATED_CALL_LOG_TABLE_CODE:
+        cursor =
+            queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
+        if (cursor != null) {
+          cursor.setNotificationUri(
+              getContext().getContentResolver(), AnnotatedCallLog.CONTENT_URI);
+        } else {
+          LogUtil.w("AnnotatedCallLogContentProvider.query", "cursor was null");
+        }
+        return cursor;
+      case ANNOTATED_CALL_LOG_TABLE_DISTINCT_NUMBER_CODE:
+        Assert.checkArgument(
+            Arrays.equals(projection, new String[] {AnnotatedCallLog.NUMBER}),
+            "only NUMBER supported for projection for distinct phone number query, got: %s",
+            Arrays.toString(projection));
+        queryBuilder.setDistinct(true);
+        cursor =
+            queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
+        if (cursor != null) {
+          cursor.setNotificationUri(
+              getContext().getContentResolver(), AnnotatedCallLog.CONTENT_URI);
+        } else {
+          LogUtil.w("AnnotatedCallLogContentProvider.query", "cursor was null");
+        }
+        return cursor;
       case COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE:
         Assert.checkArgument(
             projection == CoalescedAnnotatedCallLog.ALL_COLUMNS,
@@ -170,6 +199,8 @@
           values.put(AnnotatedCallLog._ID, idFromUri);
         }
         break;
+      case ANNOTATED_CALL_LOG_TABLE_DISTINCT_NUMBER_CODE:
+        throw new UnsupportedOperationException();
       case COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE:
         throw new UnsupportedOperationException("coalesced call log does not support inserting");
       default:
@@ -206,6 +237,8 @@
         Assert.checkArgument(id != -1, "error parsing id from uri %s", uri);
         selection = getSelectionWithId(id);
         break;
+      case ANNOTATED_CALL_LOG_TABLE_DISTINCT_NUMBER_CODE:
+        throw new UnsupportedOperationException();
       case COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE:
         throw new UnsupportedOperationException("coalesced call log does not support deleting");
       default:
@@ -244,8 +277,10 @@
             selectionArgs == null, "Do not specify selection args when updating by ID");
         selection = getSelectionWithId(ContentUris.parseId(uri));
         break;
+      case ANNOTATED_CALL_LOG_TABLE_DISTINCT_NUMBER_CODE:
+        throw new UnsupportedOperationException();
       case COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE:
-        throw new UnsupportedOperationException("coalesced call log does not support updating");
+        throw new UnsupportedOperationException();
       default:
         throw new IllegalArgumentException("Unknown uri: " + uri);
     }
@@ -287,6 +322,8 @@
           case ANNOTATED_CALL_LOG_TABLE_ID_CODE:
             // These are allowed values, continue.
             break;
+          case ANNOTATED_CALL_LOG_TABLE_DISTINCT_NUMBER_CODE:
+            throw new UnsupportedOperationException();
           case COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE:
             throw new UnsupportedOperationException(
                 "coalesced call log does not support applyBatch");
diff --git a/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java b/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java
index 4f26f0c..d3318d1 100644
--- a/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java
+++ b/java/com/android/dialer/calllog/database/contract/AnnotatedCallLogContract.java
@@ -211,11 +211,16 @@
   public static final class AnnotatedCallLog implements CommonColumns {
 
     public static final String TABLE = "AnnotatedCallLog";
+    public static final String DISTINCT_PHONE_NUMBERS = "DistinctPhoneNumbers";
 
     /** The content URI for this table. */
     public static final Uri CONTENT_URI =
         Uri.withAppendedPath(AnnotatedCallLogContract.CONTENT_URI, TABLE);
 
+    /** Content URI for selecting the distinct phone numbers from the AnnotatedCallLog. */
+    public static final Uri DISTINCT_NUMBERS_CONTENT_URI =
+        Uri.withAppendedPath(AnnotatedCallLogContract.CONTENT_URI, DISTINCT_PHONE_NUMBERS);
+
     /** The MIME type of a {@link android.content.ContentProvider#getType(Uri)} single entry. */
     public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/annotated_call_log";