Merge "Allow passing the bitmap to ContentSuggestionsService via the extras Bundle"
diff --git a/core/java/android/app/contentsuggestions/ContentSuggestionsManager.java b/core/java/android/app/contentsuggestions/ContentSuggestionsManager.java
index 1bb81b1..1e6ab41 100644
--- a/core/java/android/app/contentsuggestions/ContentSuggestionsManager.java
+++ b/core/java/android/app/contentsuggestions/ContentSuggestionsManager.java
@@ -45,6 +45,17 @@
  */
 @SystemApi
 public final class ContentSuggestionsManager {
+    /**
+     * Key into the extras Bundle passed to {@link #provideContextImage(int, Bundle)}.
+     * This can be used to provide the bitmap to
+     * {@link android.service.contentsuggestions.ContentSuggestionsService}.
+     * The value must be a {@link android.graphics.Bitmap} with the
+     * config {@link android.graphics.Bitmap.Config.HARDWARE}.
+     *
+     * @hide
+     */
+    public static final String EXTRA_BITMAP = "android.contentsuggestions.extra.BITMAP";
+
     private static final String TAG = ContentSuggestionsManager.class.getSimpleName();
 
     /**
@@ -70,7 +81,7 @@
      * system content suggestions service.
      *
      * @param taskId of the task to snapshot.
-     * @param imageContextRequestExtras sent with with request to provide implementation specific
+     * @param imageContextRequestExtras sent with request to provide implementation specific
      *                                  extra information.
      */
     public void provideContextImage(
diff --git a/core/java/android/service/contentsuggestions/ContentSuggestionsService.java b/core/java/android/service/contentsuggestions/ContentSuggestionsService.java
index efc8e87..4bcd39f 100644
--- a/core/java/android/service/contentsuggestions/ContentSuggestionsService.java
+++ b/core/java/android/service/contentsuggestions/ContentSuggestionsService.java
@@ -66,12 +66,17 @@
                 int colorSpaceId, Bundle imageContextRequestExtras) {
 
             Bitmap wrappedBuffer = null;
-            if (contextImage != null) {
-                ColorSpace colorSpace = null;
-                if (colorSpaceId >= 0 && colorSpaceId < ColorSpace.Named.values().length) {
-                    colorSpace = ColorSpace.get(ColorSpace.Named.values()[colorSpaceId]);
+            if (imageContextRequestExtras.containsKey(ContentSuggestionsManager.EXTRA_BITMAP)) {
+                wrappedBuffer = imageContextRequestExtras.getParcelable(
+                        ContentSuggestionsManager.EXTRA_BITMAP);
+            } else {
+                if (contextImage != null) {
+                    ColorSpace colorSpace = null;
+                    if (colorSpaceId >= 0 && colorSpaceId < ColorSpace.Named.values().length) {
+                        colorSpace = ColorSpace.get(ColorSpace.Named.values()[colorSpaceId]);
+                    }
+                    wrappedBuffer = Bitmap.wrapHardwareBuffer(contextImage, colorSpace);
                 }
-                wrappedBuffer = Bitmap.wrapHardwareBuffer(contextImage, colorSpace);
             }
 
             mHandler.sendMessage(
diff --git a/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java b/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java
index 06d9395..7828050 100644
--- a/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java
+++ b/services/contentsuggestions/java/com/android/server/contentsuggestions/ContentSuggestionsPerUserService.java
@@ -22,6 +22,7 @@
 import android.app.ActivityManager;
 import android.app.AppGlobals;
 import android.app.contentsuggestions.ClassificationsRequest;
+import android.app.contentsuggestions.ContentSuggestionsManager;
 import android.app.contentsuggestions.IClassificationsCallback;
 import android.app.contentsuggestions.ISelectionsCallback;
 import android.app.contentsuggestions.SelectionsRequest;
@@ -97,15 +98,19 @@
     void provideContextImageLocked(int taskId, @NonNull Bundle imageContextRequestExtras) {
         RemoteContentSuggestionsService service = ensureRemoteServiceLocked();
         if (service != null) {
-            ActivityManager.TaskSnapshot snapshot =
-                    mActivityTaskManagerInternal.getTaskSnapshotNoRestore(taskId, false);
             GraphicBuffer snapshotBuffer = null;
             int colorSpaceId = 0;
-            if (snapshot != null) {
-                snapshotBuffer = snapshot.getSnapshot();
-                ColorSpace colorSpace = snapshot.getColorSpace();
-                if (colorSpace != null) {
-                    colorSpaceId = colorSpace.getId();
+
+            // Skip taking TaskSnapshot when bitmap is provided.
+            if (!imageContextRequestExtras.containsKey(ContentSuggestionsManager.EXTRA_BITMAP)) {
+                ActivityManager.TaskSnapshot snapshot =
+                        mActivityTaskManagerInternal.getTaskSnapshotNoRestore(taskId, false);
+                if (snapshot != null) {
+                    snapshotBuffer = snapshot.getSnapshot();
+                    ColorSpace colorSpace = snapshot.getColorSpace();
+                    if (colorSpace != null) {
+                        colorSpaceId = colorSpace.getId();
+                    }
                 }
             }