Replace broadcast with adding a method in BugreportCallback

BUG: 154298410
Test: BetterBug can work normally in bug report shortcut flow
Change-Id: Ibc1a5a8ac308c303399d28eb8c177096b805fdf9
Merged-In: Ibc1a5a8ac308c303399d28eb8c177096b805fdf9
(cherry picked from commit ec91d7700d9316ba09d639ab55ce94fb72d0f8ec)
diff --git a/core/api/system-current.txt b/core/api/system-current.txt
index c780c5c..bece6a7 100644
--- a/core/api/system-current.txt
+++ b/core/api/system-current.txt
@@ -6998,6 +6998,7 @@
 
   public abstract static class BugreportManager.BugreportCallback {
     ctor public BugreportManager.BugreportCallback();
+    method public void onEarlyReportFinished();
     method public void onError(int);
     method public void onFinished();
     method public void onProgress(@FloatRange(from=0.0f, to=100.0f) float);
diff --git a/core/java/android/os/BugreportManager.java b/core/java/android/os/BugreportManager.java
index fe4d729..46ad7b8 100644
--- a/core/java/android/os/BugreportManager.java
+++ b/core/java/android/os/BugreportManager.java
@@ -26,7 +26,6 @@
 import android.annotation.SystemService;
 import android.app.ActivityManager;
 import android.content.Context;
-import android.content.Intent;
 import android.os.Handler;
 import android.util.Log;
 import android.widget.Toast;
@@ -52,8 +51,6 @@
 public final class BugreportManager {
 
     private static final String TAG = "BugreportManager";
-    private static final String INTENT_UI_INTENSIVE_BUGREPORT_DUMPS_FINISHED =
-            "com.android.internal.intent.action.UI_INTENSIVE_BUGREPORT_DUMPS_FINISHED";
 
     private final Context mContext;
     private final IDumpstate mBinder;
@@ -126,6 +123,12 @@
          * Called when taking bugreport finishes successfully.
          */
         public void onFinished() {}
+
+        /**
+         * Called when it is ready for calling app to show UI, showing any extra UI before this
+         * callback can interfere with bugreport generation.
+         */
+        public void onEarlyReportFinished() {}
     }
 
     /**
@@ -288,21 +291,12 @@
         }
 
         @Override
-        public void onUiIntensiveBugreportDumpsFinished(String callingPackage)
+        public void onUiIntensiveBugreportDumpsFinished()
                 throws RemoteException {
             final long identity = Binder.clearCallingIdentity();
             try {
                 mExecutor.execute(() -> {
-                    // Send intent to let calling app to show UI safely without interfering with
-                    // the bugreport/screenshot generation.
-                    // TODO(b/154298410): When S is ready for API change, add a method in
-                    // BugreportCallback so we can just call the callback instead of using
-                    // broadcast.
-                    Intent intent = new Intent(INTENT_UI_INTENSIVE_BUGREPORT_DUMPS_FINISHED);
-                    intent.setPackage(callingPackage);
-                    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
-                    intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
-                    mContext.sendBroadcast(intent, android.Manifest.permission.DUMP);
+                    mCallback.onEarlyReportFinished();
                 });
             } finally {
                 Binder.restoreCallingIdentity(identity);
diff --git a/core/tests/bugreports/src/com/android/os/bugreports/tests/BugreportManagerTest.java b/core/tests/bugreports/src/com/android/os/bugreports/tests/BugreportManagerTest.java
index 9246a23..f9e3bc6 100644
--- a/core/tests/bugreports/src/com/android/os/bugreports/tests/BugreportManagerTest.java
+++ b/core/tests/bugreports/src/com/android/os/bugreports/tests/BugreportManagerTest.java
@@ -118,6 +118,7 @@
         // Wifi bugreports should not receive any progress.
         assertThat(callback.hasReceivedProgress()).isFalse();
         assertThat(mBugreportFile.length()).isGreaterThan(0L);
+        assertThat(callback.hasEarlyReportFinished()).isTrue();
         assertFdsAreClosed(mBugreportFd);
     }
 
@@ -135,6 +136,7 @@
         // Interactive bugreports show progress updates.
         assertThat(callback.hasReceivedProgress()).isTrue();
         assertThat(mBugreportFile.length()).isGreaterThan(0L);
+        assertThat(callback.hasEarlyReportFinished()).isTrue();
         assertFdsAreClosed(mBugreportFd);
     }
 
@@ -246,6 +248,7 @@
         private int mErrorCode = -1;
         private boolean mSuccess = false;
         private boolean mReceivedProgress = false;
+        private boolean mEarlyReportFinished = false;
         private final Object mLock = new Object();
 
         @Override
@@ -271,6 +274,13 @@
             }
         }
 
+        @Override
+        public void onEarlyReportFinished() {
+            synchronized (mLock) {
+                mEarlyReportFinished = true;
+            }
+        }
+
         /* Indicates completion; and ended up with a success or error. */
         public boolean isDone() {
             synchronized (mLock) {
@@ -295,6 +305,12 @@
                 return mReceivedProgress;
             }
         }
+
+        public boolean hasEarlyReportFinished() {
+            synchronized (mLock) {
+                return mEarlyReportFinished;
+            }
+        }
     }
 
     public static BugreportManager getBugreportManager() {
diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java
index 02815a57..63b9bb3 100644
--- a/packages/Shell/src/com/android/shell/BugreportProgressService.java
+++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java
@@ -378,6 +378,9 @@
             }
         }
 
+        @Override
+        public void onEarlyReportFinished() {}
+
         /**
          * Reads bugreport id and links it to the bugreport info to track a bugreport that is in
          * process. id is incremented in the dumpstate code.
diff --git a/services/core/java/com/android/server/os/BugreportManagerServiceImpl.java b/services/core/java/com/android/server/os/BugreportManagerServiceImpl.java
index 47fcc08..dd507a3 100644
--- a/services/core/java/com/android/server/os/BugreportManagerServiceImpl.java
+++ b/services/core/java/com/android/server/os/BugreportManagerServiceImpl.java
@@ -297,9 +297,8 @@
         }
 
         @Override
-        public void onUiIntensiveBugreportDumpsFinished(String callingPackage)
-                throws RemoteException {
-            mListener.onUiIntensiveBugreportDumpsFinished(callingPackage);
+        public void onUiIntensiveBugreportDumpsFinished() throws RemoteException {
+            mListener.onUiIntensiveBugreportDumpsFinished();
         }
 
         @Override