ContentCaptureManager is notified when FLAG_SECURE is set dynamically on
windows.

Fixes: 130377514
Test: # manual verification
Change-Id: I18126de8284a0c95ff61107031ffff941ca26be7
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 4abf924..2914f6c 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -126,7 +126,6 @@
 import android.view.autofill.AutofillManager.AutofillClient;
 import android.view.autofill.AutofillPopupWindow;
 import android.view.autofill.IAutofillWindowPresenter;
-import android.view.contentcapture.ContentCaptureContext;
 import android.view.contentcapture.ContentCaptureManager;
 import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
 import android.widget.AdapterView;
@@ -840,7 +839,7 @@
     /** The autofill manager. Always access via {@link #getAutofillManager()}. */
     @Nullable private AutofillManager mAutofillManager;
 
-    /** The content capture manager. Always access via {@link #getContentCaptureManager()}. */
+    /** The content capture manager. Access via {@link #getContentCaptureManager()}. */
     @Nullable private ContentCaptureManager mContentCaptureManager;
 
     private final ArrayList<Application.ActivityLifecycleCallbacks> mActivityLifecycleCallbacks =
@@ -1092,12 +1091,11 @@
                 case CONTENT_CAPTURE_START:
                     //TODO(b/111276913): decide whether the InteractionSessionId should be
                     // saved / restored in the activity bundle - probably not
-                    int flags = 0;
-                    if ((getWindow().getAttributes().flags
-                            & WindowManager.LayoutParams.FLAG_SECURE) != 0) {
-                        flags |= ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE;
+                    final Window window = getWindow();
+                    if (window != null) {
+                        cm.updateWindowAttributes(window.getAttributes());
                     }
-                    cm.onActivityCreated(mToken, getComponentName(), flags);
+                    cm.onActivityCreated(mToken, getComponentName());
                     break;
                 case CONTENT_CAPTURE_RESUME:
                     cm.onActivityResumed();
@@ -3785,6 +3783,9 @@
             View decor = mDecor;
             if (decor != null && decor.getParent() != null) {
                 getWindowManager().updateViewLayout(decor, params);
+                if (mContentCaptureManager != null) {
+                    mContentCaptureManager.updateWindowAttributes(params);
+                }
             }
         }
     }
diff --git a/core/java/android/view/contentcapture/ContentCaptureManager.java b/core/java/android/view/contentcapture/ContentCaptureManager.java
index 8460840..e3c2bd1 100644
--- a/core/java/android/view/contentcapture/ContentCaptureManager.java
+++ b/core/java/android/view/contentcapture/ContentCaptureManager.java
@@ -37,6 +37,7 @@
 import android.util.Log;
 import android.view.View;
 import android.view.ViewStructure;
+import android.view.WindowManager;
 import android.view.contentcapture.ContentCaptureSession.FlushReason;
 
 import com.android.internal.annotations.GuardedBy;
@@ -343,10 +344,9 @@
     /** @hide */
     @UiThread
     public void onActivityCreated(@NonNull IBinder applicationToken,
-            @NonNull ComponentName activityComponent, int flags) {
+            @NonNull ComponentName activityComponent) {
         if (mOptions.lite) return;
         synchronized (mLock) {
-            mFlags |= flags;
             getMainContentCaptureSession().start(applicationToken, activityComponent, mFlags);
         }
     }
@@ -499,6 +499,32 @@
     }
 
     /**
+     * Called by apps to update flag secure when window attributes change.
+     *
+     * @hide
+     */
+    public void updateWindowAttributes(@NonNull WindowManager.LayoutParams params) {
+        if (sDebug) {
+            Log.d(TAG, "updateWindowAttributes(): window flags=" + params.flags);
+        }
+        final boolean flagSecureEnabled =
+                (params.flags & WindowManager.LayoutParams.FLAG_SECURE) != 0;
+
+        MainContentCaptureSession mainSession;
+        synchronized (mLock) {
+            if (flagSecureEnabled) {
+                mFlags |= ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE;
+            } else {
+                mFlags &= ~ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE;
+            }
+            mainSession = mMainSession;
+        }
+        if (mainSession != null) {
+            mainSession.setDisabled(flagSecureEnabled);
+        }
+    }
+
+    /**
      * Gets whether content capture is enabled for the given user.
      *
      * <p>This method is typically used by the content capture service settings page, so it can
diff --git a/core/java/android/view/contentcapture/MainContentCaptureSession.java b/core/java/android/view/contentcapture/MainContentCaptureSession.java
index 8673fbe..7241664 100644
--- a/core/java/android/view/contentcapture/MainContentCaptureSession.java
+++ b/core/java/android/view/contentcapture/MainContentCaptureSession.java
@@ -593,7 +593,7 @@
     }
 
     /**
-     * Called by ContentCaptureManager.setContentCaptureEnabled
+     * Sets the disabled state of content capture.
      *
      * @return whether disabled state was changed.
      */