Fixed content capture whitelist for specific activities.

Test: manual verification (it cannot be fully verified using the current CTS setup)
Test: atest CtsContentCaptureServiceTestCases:android.contentcaptureservice.cts.WhitelistTest
Test: atest FrameworksCoreTests:android.content.ContentCaptureOptionsTest
Test: atest CtsContentCaptureServiceTestCases # sanity check
Test: m update-api

Fixes: 130573023
Change-Id: I2c76a01bd98c4154c4c59099f1368232d2dba80d
diff --git a/core/java/android/content/ContentCaptureOptions.java b/core/java/android/content/ContentCaptureOptions.java
index 76c4fb8..cb2142c 100644
--- a/core/java/android/content/ContentCaptureOptions.java
+++ b/core/java/android/content/ContentCaptureOptions.java
@@ -24,6 +24,9 @@
 import android.util.ArraySet;
 import android.util.Log;
 import android.view.contentcapture.ContentCaptureManager;
+import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
+
+import com.android.internal.annotations.VisibleForTesting;
 
 import java.io.PrintWriter;
 
@@ -78,12 +81,19 @@
      */
     public final boolean lite;
 
+    /**
+     * Constructor for "lite" objects that are just used to enable a {@link ContentCaptureManager}
+     * for contexts belonging to the content capture service app.
+     */
     public ContentCaptureOptions(int loggingLevel) {
         this(/* lite= */ true, loggingLevel, /* maxBufferSize= */ 0,
                 /* idleFlushingFrequencyMs= */ 0, /* textChangeFlushingFrequencyMs= */ 0,
                 /* logHistorySize= */ 0, /* whitelistedComponents= */ null);
     }
 
+    /**
+     * Default constructor.
+     */
     public ContentCaptureOptions(int loggingLevel, int maxBufferSize, int idleFlushingFrequencyMs,
             int textChangeFlushingFrequencyMs, int logHistorySize,
             @Nullable ArraySet<ComponentName> whitelistedComponents) {
@@ -91,6 +101,16 @@
                 textChangeFlushingFrequencyMs, logHistorySize, whitelistedComponents);
     }
 
+    /** @hide */
+    @VisibleForTesting
+    public ContentCaptureOptions(@Nullable ArraySet<ComponentName> whitelistedComponents) {
+        this(ContentCaptureManager.LOGGING_LEVEL_VERBOSE,
+                ContentCaptureManager.DEFAULT_MAX_BUFFER_SIZE,
+                ContentCaptureManager.DEFAULT_IDLE_FLUSHING_FREQUENCY_MS,
+                ContentCaptureManager.DEFAULT_TEXT_CHANGE_FLUSHING_FREQUENCY_MS,
+                ContentCaptureManager.DEFAULT_LOG_HISTORY_SIZE, whitelistedComponents);
+    }
+
     private ContentCaptureOptions(boolean lite, int loggingLevel, int maxBufferSize,
             int idleFlushingFrequencyMs, int textChangeFlushingFrequencyMs, int logHistorySize,
             @Nullable ArraySet<ComponentName> whitelistedComponents) {
@@ -103,10 +123,6 @@
         this.whitelistedComponents = whitelistedComponents;
     }
 
-    /**
-     * @hide
-     */
-    @TestApi
     public static ContentCaptureOptions forWhitelistingItself() {
         final ActivityThread at = ActivityThread.currentActivityThread();
         if (at == null) {
@@ -120,19 +136,27 @@
             throw new SecurityException("Thou shall not pass!");
         }
 
-        final ContentCaptureOptions options = new ContentCaptureOptions(
-                ContentCaptureManager.LOGGING_LEVEL_VERBOSE,
-                ContentCaptureManager.DEFAULT_MAX_BUFFER_SIZE,
-                ContentCaptureManager.DEFAULT_IDLE_FLUSHING_FREQUENCY_MS,
-                ContentCaptureManager.DEFAULT_TEXT_CHANGE_FLUSHING_FREQUENCY_MS,
-                ContentCaptureManager.DEFAULT_LOG_HISTORY_SIZE,
-                /* whitelistedComponents= */ null);
+        final ContentCaptureOptions options =
+                new ContentCaptureOptions(/* whitelistedComponents= */ null);
         // Always log, as it's used by test only
         Log.i(TAG, "forWhitelistingItself(" + packageName + "): " + options);
 
         return options;
     }
 
+    /** @hide */
+    @VisibleForTesting
+    public boolean isWhitelisted(@NonNull Context context) {
+        if (whitelistedComponents == null) return true; // whole package is whitelisted
+        final ContentCaptureClient client = context.getContentCaptureClient();
+        if (client == null) {
+            // Shouldn't happen, but it doesn't hurt to check...
+            Log.w(TAG, "isWhitelisted(): no ContentCaptureClient on " + context);
+            return false;
+        }
+        return whitelistedComponents.contains(client.contentCaptureClientGetComponentName());
+    }
+
     @Override
     public String toString() {
         if (lite) {