Expose methods to set a wallpaper directly from bitmap

Bug: 188248576
Test: manual
Change-Id: I153582c89a969c30f31047cf578b7bebdace5be0
diff --git a/src/com/android/wallpaper/model/LiveWallpaperInfo.java b/src/com/android/wallpaper/model/LiveWallpaperInfo.java
index 26d2daf..676ae32 100755
--- a/src/com/android/wallpaper/model/LiveWallpaperInfo.java
+++ b/src/com/android/wallpaper/model/LiveWallpaperInfo.java
@@ -77,7 +77,7 @@
     public static final String ATTR_SERVICE = "service";
 
     /**
-     * Create a new {@link LiveWallpaperInfo} from an XML {@link AttributeSet}
+     * Creates a new {@link LiveWallpaperInfo} from an XML {@link AttributeSet}
      * @param context used to construct the {@link android.app.WallpaperInfo} associated with the
      *                new {@link LiveWallpaperInfo}
      * @param categoryId Id of the category the new wallpaper will belong to
@@ -94,6 +94,17 @@
         }
         String packageName = attrs.getAttributeValue(null, ATTR_PACKAGE);
         String serviceName = attrs.getAttributeValue(null, ATTR_SERVICE);
+        return fromPackageAndServiceName(context, categoryId, wallpaperId, packageName,
+                serviceName);
+    }
+
+    /**
+     * Creates a new {@link LiveWallpaperInfo} from its individual components
+     * @return a newly created {@link LiveWallpaperInfo} or {@code null} if one couldn't be created.
+     */
+    @Nullable
+    public static LiveWallpaperInfo fromPackageAndServiceName(Context context, String categoryId,
+            String wallpaperId, String packageName, String serviceName) {
         if (TextUtils.isEmpty(serviceName)) {
             Log.w(TAG, "Live wallpaper declaration without service: " + wallpaperId);
             return null;
diff --git a/src/com/android/wallpaper/module/DefaultWallpaperPersister.java b/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
index 3b1fd91..abe1728 100755
--- a/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
+++ b/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
@@ -349,7 +349,7 @@
     @Override
     public boolean finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl,
             int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId) {
-        return finalizeWallpaperForRotatingComponent(attributions, actionUrl, actionLabelRes,
+        return saveStaticWallpaperMetadata(attributions, actionUrl, actionLabelRes,
                 actionIconRes, collectionId, wallpaperId);
     }
 
@@ -366,18 +366,12 @@
             return false;
         }
 
-        return finalizeWallpaperForRotatingComponent(attributions, actionUrl, actionLabelRes,
+        return saveStaticWallpaperMetadata(attributions, actionUrl, actionLabelRes,
                 actionIconRes, collectionId, wallpaperId);
     }
 
-    /**
-     * Finalizes wallpaper metadata by persisting them to SharedPreferences and finalizes the
-     * wallpaper image for live rotating components by copying the "preview" image to the "final"
-     * image file location.
-     *
-     * @return Whether the operation was successful.
-     */
-    private boolean finalizeWallpaperForRotatingComponent(List<String> attributions,
+    @Override
+    public boolean saveStaticWallpaperMetadata(List<String> attributions,
             String actionUrl,
             int actionLabelRes,
             int actionIconRes,
@@ -463,27 +457,25 @@
                 scaledCropRect.top,
                 scaledCropRect.width(),
                 scaledCropRect.height());
-
-        // Set wallpaper to home-only instead of both home and lock if there's a distinct lock-only
-        // static wallpaper set so we don't override the lock wallpaper.
-        boolean isLockWallpaperSet = isSeparateLockScreenWallpaperSet();
-
-        int whichWallpaper = (isLockWallpaperSet)
-                ? WallpaperManagerCompat.FLAG_SYSTEM
-                : WallpaperManagerCompat.FLAG_SYSTEM | WallpaperManagerCompat.FLAG_LOCK;
+        int whichWallpaper = getDefaultWhichWallpaper();
 
         return setBitmapToWallpaperManagerCompat(wallpaperBitmap, false /* allowBackup */,
                 whichWallpaper);
     }
 
-    /**
-     * Sets a wallpaper bitmap to the {@link WallpaperManagerCompat}.
-     *
-     * @return an integer wallpaper ID. This is an actual wallpaper ID on N and later versions of
-     * Android, otherwise on pre-N versions of Android will return a positive integer when the
-     * operation was successful and zero if the operation encountered an error.
+    /*
+     * Note: this method will return use home-only (FLAG_SYSTEM) instead of both home and lock
+     * if there's a distinct lock-only static wallpaper set so we don't override the lock wallpaper.
      */
-    private int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
+    @Override
+    public int getDefaultWhichWallpaper() {
+        return isSeparateLockScreenWallpaperSet()
+                ? WallpaperManagerCompat.FLAG_SYSTEM
+                : WallpaperManagerCompat.FLAG_SYSTEM | WallpaperManagerCompat.FLAG_LOCK;
+    }
+
+    @Override
+    public int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
             int whichWallpaper) {
         ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
         if (wallpaperBitmap.compress(CompressFormat.PNG, DEFAULT_COMPRESS_QUALITY, tmpOut)) {
diff --git a/src/com/android/wallpaper/module/WallpaperPersister.java b/src/com/android/wallpaper/module/WallpaperPersister.java
index ec26065..07ccbcd 100755
--- a/src/com/android/wallpaper/module/WallpaperPersister.java
+++ b/src/com/android/wallpaper/module/WallpaperPersister.java
@@ -23,6 +23,7 @@
 import androidx.annotation.Nullable;
 
 import com.android.wallpaper.asset.Asset;
+import com.android.wallpaper.compat.WallpaperManagerCompat;
 import com.android.wallpaper.model.WallpaperInfo;
 
 import java.util.List;
@@ -113,6 +114,43 @@
                                              String collectionId, int wallpaperId);
 
     /**
+     * Finalizes wallpaper metadata by persisting them to SharedPreferences and finalizes the
+     * wallpaper image for live rotating components by copying the "preview" image to the "final"
+     * image file location.
+     *
+     * @param attributions List of attribution items.
+     * @param actionUrl    The action or "explore" URL for the wallpaper.
+     * @param actionLabelRes Resource ID of the action label
+     * @param actionIconRes Resource ID of the action icon
+     * @param collectionId ID of this wallpaper's collection.
+     * @param wallpaperId  Wallpaper ID that uniquely identifies the wallpaper image.
+     * @return Whether the operation was successful.
+     */
+    boolean saveStaticWallpaperMetadata(List<String> attributions,
+            String actionUrl,
+            int actionLabelRes,
+            int actionIconRes,
+            String collectionId,
+            int wallpaperId);
+
+    /**
+     * @return the flag indicating which wallpaper to set when we're trying to set a wallpaper with
+     * no user intervention. The idea is that if there's a static wallpaper on lock, we will only
+     * override home, otherwise both
+     */
+    int getDefaultWhichWallpaper();
+
+    /**
+     * Sets a wallpaper bitmap to the {@link WallpaperManagerCompat}.
+     *
+     * @return an integer wallpaper ID. This is an actual wallpaper ID on N and later versions of
+     * Android, otherwise on pre-N versions of Android will return a positive integer when the
+     * operation was successful and zero if the operation encountered an error.
+     */
+    int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
+            int whichWallpaper);
+
+    /**
      * Saves the last wallpaper which showed a preview from this app.
      */
     void setWallpaperInfoInPreview(WallpaperInfo wallpaper);
diff --git a/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java b/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java
index 5c1ddbd..02cba0e 100644
--- a/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java
+++ b/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java
@@ -226,4 +226,18 @@
     public int getWallpaperPosition() {
         return mWallpaperPosition;
     }
+
+    public boolean saveStaticWallpaperMetadata(List<String> attributions, String actionUrl,
+            int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId) {
+        return false;
+    }
+
+    public int getDefaultWhichWallpaper() {
+        return 0;
+    }
+
+    public int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
+            int whichWallpaper) {
+        return 0;
+    }
 }